on_block_exit.h 513 B

1234567891011121314151617181920212223242526
  1. #pragma once
  2. #include <functional>
  3. namespace jvalidate::detail {
  4. class OnBlockExit {
  5. private:
  6. std::function<void()> callback_;
  7. public:
  8. OnBlockExit() = default;
  9. template <typename F> OnBlockExit(F && callback) : callback_(callback) {}
  10. OnBlockExit(OnBlockExit && other) { std::swap(other.callback_, callback_); }
  11. OnBlockExit & operator=(OnBlockExit && other) {
  12. std::swap(other.callback_, callback_);
  13. return *this;
  14. }
  15. ~OnBlockExit() {
  16. if (callback_) {
  17. callback_();
  18. }
  19. }
  20. };
  21. }