recguard.h 2.9 KB

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