34 lines
845 B
C++
34 lines
845 B
C++
#pragma once
|
|
|
|
#include "bts_no_copy.h"
|
|
#include <memory>
|
|
|
|
namespace bts {
|
|
namespace utils {
|
|
|
|
template <class T> 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<decltype(DEFER_UNIQUE(__DEFER_PROC_))> \
|
|
DEFER_UNIQUE(__DEFER_CONTAINER_)(DEFER_UNIQUE(__DEFER_PROC_));
|
|
|
|
} // namespace utils
|
|
} // namespace bts
|