diff options
author | John Wiegley <johnw@newartisans.com> | 2007-05-07 10:24:55 +0000 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2008-04-13 03:38:38 -0400 |
commit | 8aada79971b772fda92131053fa03021cfbc625a (patch) | |
tree | fbb25be31a419958167d1d166ab0f35d3b7f6a86 /src/scoped_execute.h | |
parent | 65e4fc7ebff0245fe13f99663c341e000a29af48 (diff) | |
download | fork-ledger-8aada79971b772fda92131053fa03021cfbc625a.tar.gz fork-ledger-8aada79971b772fda92131053fa03021cfbc625a.tar.bz2 fork-ledger-8aada79971b772fda92131053fa03021cfbc625a.zip |
Added a facility for handling scoped executions.
Diffstat (limited to 'src/scoped_execute.h')
-rw-r--r-- | src/scoped_execute.h | 73 |
1 files changed, 73 insertions, 0 deletions
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 <typename T> +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 <typename T> +class scoped_execute : public boost::noncopyable +{ + typedef boost::function<void (T)> 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<void> : public boost::noncopyable +{ + typedef boost::function<void ()> 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 |