From 8aada79971b772fda92131053fa03021cfbc625a Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Mon, 7 May 2007 10:24:55 +0000 Subject: Added a facility for handling scoped executions. --- src/scoped_execute.h | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/scoped_execute.h (limited to 'src/scoped_execute.h') diff --git a/src/scoped_execute.h b/src/scoped_execute.h new file mode 100644 index 00000000..da3f388e --- /dev/null +++ b/src/scoped_execute.h @@ -0,0 +1,73 @@ +#ifndef _SCOPE_EXECUTE_H +#define _SCOPE_EXECUTE_H + +template +class scoped_variable : public boost::noncopyable +{ + T& var; + T prev; + bool enabled; + +public: + explicit scoped_variable(T& _var) + : var(_var), prev(var), enabled(true) {} + explicit scoped_variable(T& _var, const T& value) + : var(_var), prev(var), enabled(true) { + var = value; + } + ~scoped_variable() { + if (enabled) + var = prev; + } + + void clear() { + enabled = false; + } +}; + +template +class scoped_execute : public boost::noncopyable +{ + typedef boost::function function_t; + + function_t code; + T arg; + bool enabled; + +public: + explicit scoped_execute(const function_t& _code, T _arg) + : code(_code), arg(_arg), enabled(true) {} + + ~scoped_execute() { + if (enabled) + code(arg); + } + + void clear() { + enabled = false; + } +}; + +template <> +class scoped_execute : public boost::noncopyable +{ + typedef boost::function function_t; + + function_t code; + bool enabled; + +public: + explicit scoped_execute(const function_t& _code) + : code(_code), enabled(true) {} + + ~scoped_execute() { + if (enabled) + code(); + } + + void clear() { + enabled = false; + } +}; + +#endif // _SCOPE_EXECUTE_H -- cgit v1.2.3