raii: scoped_function takes a nullary void functor

This commit is contained in:
Danny Robson 2015-11-11 17:01:00 +11:00
parent 3531b09794
commit 11256c2645

View File

@ -19,6 +19,8 @@
#include "preprocessor.hpp" #include "preprocessor.hpp"
#include <functional>
/// Defines a translation-unit-unique variable useful for unnamed scoped variables /// Defines a translation-unit-unique variable useful for unnamed scoped variables
#define raii PASTE(__unique_, __COUNTER__) #define raii PASTE(__unique_, __COUNTER__)
@ -38,16 +40,20 @@ namespace util {
/// Executes a function upon object destruction /// Executes a function upon object destruction
template <typename T>
struct scoped_function { struct scoped_function {
explicit scoped_function (T &&_func): explicit scoped_function (std::function<void(void)> &&_func):
func (std::move (_func)) func (std::move (_func))
{ ; } { ; }
~scoped_function () ~scoped_function ()
{ func (); } { func (); }
T func; void clear (void)
{
func = [] (void) { ; };
}
std::function<void(void)> func;
}; };
} }