Files
spark/src/lib/utils/defer.h
T
2017-08-07 17:21:12 -04:00

39 lines
741 B
C++
Executable File

#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