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