]>
Commit | Line | Data |
---|---|---|
23324ae1 | 1 | ///////////////////////////////////////////////////////////////////////////// |
7c913512 | 2 | // Name: scopeguard.h |
e54c96f1 | 3 | // Purpose: interface of global functions |
7c913512 | 4 | // Author: wxWidgets team |
526954c5 | 5 | // Licence: wxWindows licence |
7c913512 FM |
6 | ///////////////////////////////////////////////////////////////////////////// |
7 | ||
bcffb4d1 | 8 | /** |
4f48d306 FM |
9 | @class wxScopeGuard |
10 | ||
bcffb4d1 VZ |
11 | Scope guard is an object which allows executing an action on scope exit. |
12 | ||
13 | The objects of this class must be constructed using wxMakeGuard() function. | |
4f48d306 FM |
14 | |
15 | @nolibrary | |
16 | @category{misc} | |
bcffb4d1 VZ |
17 | */ |
18 | class wxScopeGuard | |
19 | { | |
20 | public: | |
21 | /** | |
22 | Call this method to dismiss the execution of the action on scope exit. | |
23 | ||
24 | A typical example: | |
25 | @code | |
26 | Update1(); | |
27 | ||
28 | // ensure that changes done so far are rolled back if the next | |
29 | // operation throws | |
30 | wxScopeGuard guard = wxMakeGuard(RollBack); | |
31 | Update2(); | |
32 | ||
33 | // it didn't throw so commit the changes, i.e. avoid rolling back | |
34 | guard.Dismiss(); | |
35 | @endcode | |
36 | */ | |
37 | void Dismiss(); | |
38 | }; | |
39 | ||
b21126db | 40 | /** @addtogroup group_funcmacro_misc */ |
bcffb4d1 VZ |
41 | //@{ |
42 | /** | |
43 | Returns a scope guard object which will call the specified function with | |
44 | the given parameters on scope exit. | |
45 | ||
46 | This function is overloaded to take several parameters up to some | |
47 | implementation-defined (but relatively low) limit. | |
48 | ||
49 | The @a func should be a functor taking parameters of the types P1, ..., PN, | |
50 | i.e. the expression @c func(p1, ..., pN) should be valid. | |
51 | */ | |
52 | template <typename F, typename P1, ..., typename PN> | |
53 | wxScopeGuard wxMakeGuard(F func, P1 p1, ..., PN pN); | |
54 | ||
55 | //@} | |
56 | ||
b21126db | 57 | /** @addtogroup group_funcmacro_misc */ |
7c913512 | 58 | //@{ |
23324ae1 | 59 | /** |
bcffb4d1 VZ |
60 | Ensure that the global @a function with a few (up to some |
61 | implementation-defined limit) is executed on scope exit, whether due to a | |
62 | normal function return or because an exception has been thrown. | |
63 | ||
64 | A typical example of its usage: | |
7c913512 | 65 | |
23324ae1 FM |
66 | @code |
67 | void *buf = malloc(size); | |
7fa7088e | 68 | wxON_BLOCK_EXIT1(free, buf); |
23324ae1 | 69 | @endcode |
7c913512 | 70 | |
23324ae1 | 71 | Please see the original article by Andrei Alexandrescu and Petru Marginean |
7fa7088e BP |
72 | published in December 2000 issue of C/C++ Users Journal for more details. |
73 | ||
74 | @see wxON_BLOCK_EXIT_OBJ0() | |
75 | ||
76 | @header{wx/scopeguard.h} | |
77 | */ | |
bcffb4d1 | 78 | #define wxON_BLOCK_EXIT(function, ...) |
7fa7088e BP |
79 | #define wxON_BLOCK_EXIT0(function) |
80 | #define wxON_BLOCK_EXIT1(function, p1) | |
81 | #define wxON_BLOCK_EXIT2(function, p1, p2) | |
bcffb4d1 | 82 | #define wxON_BLOCK_EXIT3(function, p1, p2, p3) |
7fa7088e BP |
83 | //@} |
84 | ||
b21126db | 85 | /** @addtogroup group_funcmacro_misc */ |
7fa7088e BP |
86 | //@{ |
87 | /** | |
bcffb4d1 | 88 | This family of macros is similar to wxON_BLOCK_EXIT(), but calls a method |
7fa7088e | 89 | of the given object instead of a free function. |
7c913512 | 90 | |
7fa7088e | 91 | @header{wx/scopeguard.h} |
23324ae1 | 92 | */ |
bcffb4d1 | 93 | #define wxON_BLOCK_EXIT_OBJ(object, method, ...) |
7fa7088e BP |
94 | #define wxON_BLOCK_EXIT_OBJ0(object, method) |
95 | #define wxON_BLOCK_EXIT_OBJ1(object, method, p1) | |
96 | #define wxON_BLOCK_EXIT_OBJ2(object, method, p1, p2) | |
bcffb4d1 | 97 | #define wxON_BLOCK_EXIT_OBJ3(object, method, p1, p2, p3) |
23324ae1 FM |
98 | //@} |
99 | ||
b21126db | 100 | /** @addtogroup group_funcmacro_misc */ |
51c679d5 VZ |
101 | //@{ |
102 | /** | |
bcffb4d1 | 103 | This family of macros is similar to wxON_BLOCK_OBJ(), but calls a method |
51c679d5 VZ |
104 | of @c this object instead of a method of the specified object. |
105 | ||
106 | @header{wx/scopeguard.h} | |
107 | */ | |
bcffb4d1 | 108 | #define wxON_BLOCK_EXIT_THIS(method, ...) |
51c679d5 VZ |
109 | #define wxON_BLOCK_EXIT_THIS0(method) |
110 | #define wxON_BLOCK_EXIT_THIS1(method, p1) | |
111 | #define wxON_BLOCK_EXIT_THIS2(method, p1, p2) | |
bcffb4d1 | 112 | #define wxON_BLOCK_EXIT_THIS3(method, p1, p2, p3) |
51c679d5 VZ |
113 | //@} |
114 | ||
b21126db | 115 | /** @addtogroup group_funcmacro_misc */ |
d2a48d5c VZ |
116 | //@{ |
117 | /** | |
118 | This macro sets a variable to the specified value on scope exit. | |
119 | ||
120 | Example of usage: | |
121 | @code | |
122 | void foo() | |
123 | { | |
124 | bool isDoingSomething = true; | |
125 | { | |
126 | wxON_BLOCK_EXIT_SET(isDoingSomething, false); | |
127 | ... do something ... | |
128 | } | |
129 | ... isDoingSomething is false now ... | |
130 | } | |
131 | @endcode | |
132 | ||
e196e523 VZ |
133 | Notice that @a value is copied, i.e. stored by value, so it can be a |
134 | temporary object returned by a function call, for example. | |
135 | ||
d2a48d5c VZ |
136 | @see wxON_BLOCK_EXIT_OBJ0(), wxON_BLOCK_EXIT_NULL() |
137 | ||
138 | @header{wx/scopeguard.h} | |
139 | */ | |
140 | #define wxON_BLOCK_EXIT_SET(var, value) | |
141 | ||
142 | /** | |
143 | This macro sets the pointer passed to it as argument to NULL on scope exit. | |
144 | ||
145 | It must be used instead of wxON_BLOCK_EXIT_SET() when the value being set | |
146 | is @c NULL. | |
147 | ||
148 | @header{wx/scopeguard.h} | |
149 | */ | |
150 | #define wxON_BLOCK_EXIT_NULL(ptr) | |
151 | ||
152 | //@} | |
153 |