1 /***************************************************************************
2  *      Mechanized Assault and Exploration Reloaded Projectfile            *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19 
20 #ifndef utility_scopedoperationH
21 #define utility_scopedoperationH
22 
23 #include <functional>
24 
25 #include "maxrconfig.h"
26 
27 /**
28  * Generic RAII-class that calls a function object on its destruction.
29  *
30  * @tparam FunctionType The type of the function to be executed.
31  *                      Can be any callable object (including lambdas).
32  */
33 template<typename FunctionType = std::function<void()>>
34 class cScopedOperation
35 {
36 public:
cScopedOperation(const FunctionType & function_)37 	explicit cScopedOperation (const FunctionType& function_) :
38 		function (function_),
39 		dismissed (false)
40 	{}
cScopedOperation(cScopedOperation<FunctionType> && other)41 	cScopedOperation (cScopedOperation<FunctionType>&& other) :
42 		function (std::move (other.function)),
43 		dismissed (false)
44 	{
45 		other.dismissed = true;
46 	}
47 	cScopedOperation<FunctionType>& operator= (cScopedOperation<FunctionType> && other)
48 	{
49 		function = std::move (other.function);
50 		dismissed = false;
51 		other.dismissed = true;
52 	}
53 
~cScopedOperation()54 	~cScopedOperation()
55 	{
56 		if (!dismissed) function();
57 	}
58 
59 	/**
60 	 * Dismisses the scoped operations so that it will not call the function
61 	 * when it is destroyed.
62 	 */
dismiss()63 	void dismiss()
64 	{
65 		dismissed = true;
66 	}
67 private:
68 	cScopedOperation (const cScopedOperation<FunctionType>&) MAXR_DELETE_FUNCTION;
69 	cScopedOperation& operator= (const cScopedOperation<FunctionType>&) MAXR_DELETE_FUNCTION;
70 
71 	FunctionType function;
72 	bool dismissed;
73 };
74 
75 /**
76  * Helper function to create scoped operation objects.
77  *
78  * @tparam FunctionType Type of the function to create the scoped operation for.
79  * @param function The callable object to bind to the scoped operation.
80  * @return The scoped operation that will call the passed function object on its destruction.
81  */
82 template<typename FunctionType>
makeScopedOperation(const FunctionType & function)83 cScopedOperation<FunctionType> makeScopedOperation (const FunctionType& function)
84 {
85 	return cScopedOperation<FunctionType> (function);
86 }
87 
88 #endif // utility_scopedoperationH
89