libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNAtScopeExit.h
Go to the documentation of this file.
1 /* Copyright 2009-2015 INSA Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNAtScopeExit.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNAtScopeExit_HEADER
23 #define CRNAtScopeExit_HEADER
24 
25 #include <functional>
26 
27 namespace crn
28 {
32  class Destroyer
33  {
34  public:
38  Destroyer(const std::function<void(void)> &f):atdestroy(f) { }
42  Destroyer(std::function<void(void)> &&f):atdestroy(std::move(f)) { }
44  ~Destroyer() { atdestroy(); }
45  private:
46  std::function<void(void)> atdestroy;
47  };
48 }
49 
55 #define CRNAtScopeExit_UNIFY_VAR(VAR) CRNAtScopeExit_UNIFY_VAR2(VAR,__LINE__)
56 
60 #define CRNAtScopeExit_UNIFY_VAR2(VAR,CODE) CRNAtScopeExit_UNIFY_VAR3(VAR,CODE)
61 
65 #define CRNAtScopeExit_UNIFY_VAR3(VAR,CODE) VAR##CODE
66 
73 #define AtScopeExit(functor) crn::Destroyer CRNAtScopeExit_UNIFY_VAR(at_scope_exit)(functor)
74 
76 #endif
77 
78 
An objects that calls a functor when destroyed.
Destroyer(std::function< void(void)> &&f)
Constructor.
~Destroyer()
Destructor that calls the functor.
Destroyer(const std::function< void(void)> &f)
Constructor.