#pragma once #include "bts_no_copy.h" #include namespace bts { namespace utils { template class DeferredAction { public: explicit DeferredAction(T& t) : _action(std::addressof(t)) { } ~DeferredAction() { if (_action) (*_action)(); } private: BTS_NO_COPY(DeferredAction); T * _action; }; #define DEFER_CONCAT_IMPL(x, y) x##y #define DEFER_CONCAT(x, y) DEFER_CONCAT_IMPL(x, y) #define DEFER_UNIQUE(prefix) DEFER_CONCAT(prefix, __LINE__ ) #define DEFER(...) \ auto DEFER_UNIQUE(__DEFER_PROC_) = [&]() { __VA_ARGS__;}; \ bts::utils::DeferredAction DEFER_UNIQUE(__DEFER_CONTAINER_)(DEFER_UNIQUE(__DEFER_PROC_)); }} // namespace