]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: recguard.h | |
e54c96f1 | 3 | // Purpose: interface of wxRecursionGuardFlag |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /** | |
10 | @class wxRecursionGuardFlag | |
7c913512 FM |
11 | |
12 | This is a completely opaque class which exists only to be used with | |
3ed3a1c8 | 13 | wxRecursionGuard, please see the example in that class' documentation. |
7c913512 | 14 | |
3ed3a1c8 BP |
15 | @remarks |
16 | ||
17 | wxRecursionGuardFlag object must be declared @c static or the recursion | |
18 | would never be detected. | |
7c913512 | 19 | |
23324ae1 | 20 | @library{wxbase} |
9602ce3d | 21 | @category{misc} |
23324ae1 | 22 | */ |
7c913512 | 23 | class wxRecursionGuardFlag |
23324ae1 FM |
24 | { |
25 | public: | |
7c913512 | 26 | |
23324ae1 FM |
27 | }; |
28 | ||
29 | ||
e54c96f1 | 30 | |
23324ae1 FM |
31 | /** |
32 | @class wxRecursionGuard | |
7c913512 | 33 | |
23324ae1 FM |
34 | wxRecursionGuard is a very simple class which can be used to prevent reentrancy |
35 | problems in a function. It is not thread-safe and so should be used only in | |
36 | single-threaded programs or in combination with some thread synchronization | |
37 | mechanisms. | |
7c913512 FM |
38 | |
39 | wxRecursionGuard is always used together with the | |
23324ae1 | 40 | wxRecursionGuardFlag like in this example: |
7c913512 | 41 | |
23324ae1 FM |
42 | @code |
43 | void Foo() | |
3ed3a1c8 BP |
44 | { |
45 | static wxRecursionGuardFlag s_flag; | |
46 | wxRecursionGuard guard(s_flag); | |
47 | if ( guard.IsInside() ) | |
23324ae1 | 48 | { |
3ed3a1c8 BP |
49 | // don't allow reentrancy |
50 | return; | |
23324ae1 | 51 | } |
3ed3a1c8 BP |
52 | |
53 | ... | |
54 | } | |
23324ae1 | 55 | @endcode |
7c913512 | 56 | |
23324ae1 | 57 | As you can see, wxRecursionGuard simply tests the flag value and sets it to |
7c913512 | 58 | @true if it hadn't been already set. |
3ed3a1c8 | 59 | IsInside() allows testing the old flag |
23324ae1 FM |
60 | value. The advantage of using this class compared to directly manipulating the |
61 | flag is that the flag is always reset in the wxRecursionGuard destructor and so | |
62 | you don't risk to forget to do it even if the function returns in an unexpected | |
63 | way (for example because an exception has been thrown). | |
7c913512 | 64 | |
23324ae1 | 65 | @library{wxbase} |
9602ce3d | 66 | @category{misc} |
23324ae1 | 67 | */ |
7c913512 | 68 | class wxRecursionGuard |
23324ae1 FM |
69 | { |
70 | public: | |
71 | /** | |
3ed3a1c8 | 72 | A wxRecursionGuard object must always be initialized with a @c static |
23324ae1 | 73 | wxRecursionGuardFlag. The constructor saves the |
7c913512 | 74 | value of the flag to be able to return the correct value from |
23324ae1 FM |
75 | IsInside(). |
76 | */ | |
77 | wxRecursionGuard(wxRecursionGuardFlag& flag); | |
78 | ||
79 | /** | |
80 | The destructor resets the flag value so that the function can be entered again | |
81 | the next time. | |
3ed3a1c8 BP |
82 | |
83 | @note This is not virtual, so this class is not meant to be derived | |
84 | from (besides, there is absolutely no reason to do it anyhow). | |
23324ae1 FM |
85 | */ |
86 | ~wxRecursionGuard(); | |
87 | ||
88 | /** | |
cdbcf4c2 | 89 | Returns @true if we're already inside the code block "protected" by this |
3ed3a1c8 BP |
90 | wxRecursionGuard (i.e. between this line and the end of current scope). |
91 | Usually the function using wxRecursionGuard takes some specific actions | |
92 | in such case (may be simply returning) to prevent reentrant calls to itself. | |
93 | ||
23324ae1 FM |
94 | If this method returns @false, it is safe to continue. |
95 | */ | |
328f5751 | 96 | bool IsInside() const; |
23324ae1 | 97 | }; |
e54c96f1 | 98 |