2008-03-08 13:52:38 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: recguard.h
|
2008-03-10 15:24:38 +00:00
|
|
|
// Purpose: interface of wxRecursionGuardFlag
|
2008-03-08 13:52:38 +00:00
|
|
|
// Author: wxWidgets team
|
|
|
|
// RCS-ID: $Id$
|
2010-07-13 13:29:13 +00:00
|
|
|
// Licence: wxWindows licence
|
2008-03-08 13:52:38 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
@class wxRecursionGuardFlag
|
2008-03-08 14:43:31 +00:00
|
|
|
|
|
|
|
This is a completely opaque class which exists only to be used with
|
2008-05-04 09:04:38 +00:00
|
|
|
wxRecursionGuard, please see the example in that class' documentation.
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-05-04 09:04:38 +00:00
|
|
|
@remarks
|
|
|
|
|
|
|
|
wxRecursionGuardFlag object must be declared @c static or the recursion
|
|
|
|
would never be detected.
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
@library{wxbase}
|
2008-06-21 05:53:53 +00:00
|
|
|
@category{misc}
|
2008-03-08 13:52:38 +00:00
|
|
|
*/
|
2008-03-08 14:43:31 +00:00
|
|
|
class wxRecursionGuardFlag
|
2008-03-08 13:52:38 +00:00
|
|
|
{
|
|
|
|
public:
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-03-10 15:24:38 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
/**
|
|
|
|
@class wxRecursionGuard
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
wxRecursionGuard is a very simple class which can be used to prevent reentrancy
|
|
|
|
problems in a function. It is not thread-safe and so should be used only in
|
|
|
|
single-threaded programs or in combination with some thread synchronization
|
|
|
|
mechanisms.
|
2008-03-08 14:43:31 +00:00
|
|
|
|
|
|
|
wxRecursionGuard is always used together with the
|
2008-03-08 13:52:38 +00:00
|
|
|
wxRecursionGuardFlag like in this example:
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
@code
|
|
|
|
void Foo()
|
2008-05-04 09:04:38 +00:00
|
|
|
{
|
|
|
|
static wxRecursionGuardFlag s_flag;
|
|
|
|
wxRecursionGuard guard(s_flag);
|
|
|
|
if ( guard.IsInside() )
|
2008-03-08 13:52:38 +00:00
|
|
|
{
|
2008-05-04 09:04:38 +00:00
|
|
|
// don't allow reentrancy
|
|
|
|
return;
|
2008-03-08 13:52:38 +00:00
|
|
|
}
|
2008-05-04 09:04:38 +00:00
|
|
|
|
|
|
|
...
|
|
|
|
}
|
2008-03-08 13:52:38 +00:00
|
|
|
@endcode
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
As you can see, wxRecursionGuard simply tests the flag value and sets it to
|
2008-03-08 14:43:31 +00:00
|
|
|
@true if it hadn't been already set.
|
2008-05-04 09:04:38 +00:00
|
|
|
IsInside() allows testing the old flag
|
2008-03-08 13:52:38 +00:00
|
|
|
value. The advantage of using this class compared to directly manipulating the
|
|
|
|
flag is that the flag is always reset in the wxRecursionGuard destructor and so
|
|
|
|
you don't risk to forget to do it even if the function returns in an unexpected
|
|
|
|
way (for example because an exception has been thrown).
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
@library{wxbase}
|
2008-06-21 05:53:53 +00:00
|
|
|
@category{misc}
|
2008-03-08 13:52:38 +00:00
|
|
|
*/
|
2008-03-08 14:43:31 +00:00
|
|
|
class wxRecursionGuard
|
2008-03-08 13:52:38 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2008-05-04 09:04:38 +00:00
|
|
|
A wxRecursionGuard object must always be initialized with a @c static
|
2008-03-08 13:52:38 +00:00
|
|
|
wxRecursionGuardFlag. The constructor saves the
|
2008-03-08 14:43:31 +00:00
|
|
|
value of the flag to be able to return the correct value from
|
2008-03-08 13:52:38 +00:00
|
|
|
IsInside().
|
|
|
|
*/
|
|
|
|
wxRecursionGuard(wxRecursionGuardFlag& flag);
|
|
|
|
|
|
|
|
/**
|
|
|
|
The destructor resets the flag value so that the function can be entered again
|
|
|
|
the next time.
|
2008-05-04 09:04:38 +00:00
|
|
|
|
|
|
|
@note This is not virtual, so this class is not meant to be derived
|
|
|
|
from (besides, there is absolutely no reason to do it anyhow).
|
2008-03-08 13:52:38 +00:00
|
|
|
*/
|
|
|
|
~wxRecursionGuard();
|
|
|
|
|
|
|
|
/**
|
2008-04-01 13:59:28 +00:00
|
|
|
Returns @true if we're already inside the code block "protected" by this
|
2008-05-04 09:04:38 +00:00
|
|
|
wxRecursionGuard (i.e. between this line and the end of current scope).
|
|
|
|
Usually the function using wxRecursionGuard takes some specific actions
|
|
|
|
in such case (may be simply returning) to prevent reentrant calls to itself.
|
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
If this method returns @false, it is safe to continue.
|
|
|
|
*/
|
2008-03-09 16:24:26 +00:00
|
|
|
bool IsInside() const;
|
2008-03-08 13:52:38 +00:00
|
|
|
};
|
2008-03-10 15:24:38 +00:00
|
|
|
|