recguard.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/recguard.h
  3. // Purpose: declaration and implementation of wxRecursionGuard class
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 14.08.2003
  7. // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_RECGUARD_H_
  11. #define _WX_RECGUARD_H_
  12. #include "wx/defs.h"
  13. // ----------------------------------------------------------------------------
  14. // wxRecursionGuardFlag is used with wxRecursionGuard
  15. // ----------------------------------------------------------------------------
  16. typedef int wxRecursionGuardFlag;
  17. // ----------------------------------------------------------------------------
  18. // wxRecursionGuard is the simplest way to protect a function from reentrancy
  19. // ----------------------------------------------------------------------------
  20. class WXDLLIMPEXP_BASE wxRecursionGuard
  21. {
  22. public:
  23. wxRecursionGuard(wxRecursionGuardFlag& flag)
  24. : m_flag(flag)
  25. {
  26. m_isInside = flag++ != 0;
  27. }
  28. ~wxRecursionGuard()
  29. {
  30. wxASSERT_MSG( m_flag > 0, wxT("unbalanced wxRecursionGuards!?") );
  31. m_flag--;
  32. }
  33. bool IsInside() const { return m_isInside; }
  34. private:
  35. wxRecursionGuardFlag& m_flag;
  36. // true if the flag had been already set when we were created
  37. bool m_isInside;
  38. };
  39. #endif // _WX_RECGUARD_H_