| 123456789101112131415161718192021222324252627 |
- #pragma once
- #include <functional>
- #include <type_traits>
- #define CONCAT2(A, B) A##B
- #define CONCAT(A, B) CONCAT2(A, B)
- #define scoped_state(prop, value) \
- auto CONCAT(scoped_state_, __LINE__) = detail::ScopedState(prop, value)
- namespace jvalidate::detail {
- class ScopedState {
- private:
- std::function<void()> reset_;
- public:
- template <typename T, typename S>
- requires(std::is_constructible_v<T, S>) ScopedState(T & prop, S value)
- : reset_([reset = prop, &prop]() { prop = reset; }) {
- prop = std::move(value);
- }
- ~ScopedState() { reset_(); }
- explicit operator bool() const { return true; }
- };
- }
|