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