aboutsummaryrefslogtreecommitdiff
path: root/external/glim/raii.hpp
blob: a09d357769493d1d87c9fb15ff2071e5ab122888 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <functional>
namespace glim {

// http://stackoverflow.com/questions/2121607/any-raii-template-in-boost-or-c0x/

/// RAII helper. Keeps the functor and runs it in the destructor.
/// Example: \code auto unmap = raiiFun ([&]() {munmap (fd, size);}); \endcode
template<typename Fun> struct RAIIFun {
  Fun _fun;
  RAIIFun (RAIIFun&&) = default;
  RAIIFun (const RAIIFun&) = default;
  template<typename FunArg> RAIIFun (FunArg&& fun): _fun (std::forward<Fun> (fun)) {}
  ~RAIIFun() {_fun();}
};

/// The idea to name it `finally` comes from http://www.codeproject.com/Tips/476970/finally-clause-in-Cplusplus.
/// Example: \code finally unmap ([&]() {munmap (fd, size);}); \endcode
typedef RAIIFun<std::function<void(void)>> finally;

/// Runs the given functor when going out of scope.
/// Example: \code
///   auto closeFd = raiiFun ([&]() {close (fd);});
///   auto unmap = raiiFun ([&]() {munmap (fd, size);});
/// \endcode
template<typename Fun> RAIIFun<Fun> raiiFun (const Fun& fun) {return RAIIFun<Fun> (fun);}

/// Runs the given functor when going out of scope.
/// Example: \code
///   auto closeFd = raiiFun ([&]() {close (fd);});
///   auto unmap = raiiFun ([&]() {munmap (fd, size);});
/// \endcode
template<typename Fun> RAIIFun<Fun> raiiFun (Fun&& fun) {return RAIIFun<Fun> (std::move (fun));}

}