scopeguard.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: scopeguard.h
  3. // Purpose: interface of global functions
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxScopeGuard
  9. Scope guard is an object which allows executing an action on scope exit.
  10. The objects of this class must be constructed using wxMakeGuard() function.
  11. @nolibrary
  12. @category{misc}
  13. */
  14. class wxScopeGuard
  15. {
  16. public:
  17. /**
  18. Call this method to dismiss the execution of the action on scope exit.
  19. A typical example:
  20. @code
  21. Update1();
  22. // ensure that changes done so far are rolled back if the next
  23. // operation throws
  24. wxScopeGuard guard = wxMakeGuard(RollBack);
  25. Update2();
  26. // it didn't throw so commit the changes, i.e. avoid rolling back
  27. guard.Dismiss();
  28. @endcode
  29. */
  30. void Dismiss();
  31. };
  32. /** @addtogroup group_funcmacro_misc */
  33. //@{
  34. /**
  35. Returns a scope guard object which will call the specified function with
  36. the given parameters on scope exit.
  37. This function is overloaded to take several parameters up to some
  38. implementation-defined (but relatively low) limit.
  39. The @a func should be a functor taking parameters of the types P1, ..., PN,
  40. i.e. the expression @c func(p1, ..., pN) should be valid.
  41. */
  42. template <typename F, typename P1, ..., typename PN>
  43. wxScopeGuard wxMakeGuard(F func, P1 p1, ..., PN pN);
  44. //@}
  45. /** @addtogroup group_funcmacro_misc */
  46. //@{
  47. /**
  48. Ensure that the global @a function with a few (up to some
  49. implementation-defined limit) is executed on scope exit, whether due to a
  50. normal function return or because an exception has been thrown.
  51. A typical example of its usage:
  52. @code
  53. void *buf = malloc(size);
  54. wxON_BLOCK_EXIT1(free, buf);
  55. @endcode
  56. Please see the original article by Andrei Alexandrescu and Petru Marginean
  57. published in December 2000 issue of C/C++ Users Journal for more details.
  58. @see wxON_BLOCK_EXIT_OBJ0()
  59. @header{wx/scopeguard.h}
  60. */
  61. #define wxON_BLOCK_EXIT(function, ...)
  62. #define wxON_BLOCK_EXIT0(function)
  63. #define wxON_BLOCK_EXIT1(function, p1)
  64. #define wxON_BLOCK_EXIT2(function, p1, p2)
  65. #define wxON_BLOCK_EXIT3(function, p1, p2, p3)
  66. //@}
  67. /** @addtogroup group_funcmacro_misc */
  68. //@{
  69. /**
  70. This family of macros is similar to wxON_BLOCK_EXIT(), but calls a method
  71. of the given object instead of a free function.
  72. @header{wx/scopeguard.h}
  73. */
  74. #define wxON_BLOCK_EXIT_OBJ(object, method, ...)
  75. #define wxON_BLOCK_EXIT_OBJ0(object, method)
  76. #define wxON_BLOCK_EXIT_OBJ1(object, method, p1)
  77. #define wxON_BLOCK_EXIT_OBJ2(object, method, p1, p2)
  78. #define wxON_BLOCK_EXIT_OBJ3(object, method, p1, p2, p3)
  79. //@}
  80. /** @addtogroup group_funcmacro_misc */
  81. //@{
  82. /**
  83. This family of macros is similar to wxON_BLOCK_OBJ(), but calls a method
  84. of @c this object instead of a method of the specified object.
  85. @header{wx/scopeguard.h}
  86. */
  87. #define wxON_BLOCK_EXIT_THIS(method, ...)
  88. #define wxON_BLOCK_EXIT_THIS0(method)
  89. #define wxON_BLOCK_EXIT_THIS1(method, p1)
  90. #define wxON_BLOCK_EXIT_THIS2(method, p1, p2)
  91. #define wxON_BLOCK_EXIT_THIS3(method, p1, p2, p3)
  92. //@}
  93. /** @addtogroup group_funcmacro_misc */
  94. //@{
  95. /**
  96. This macro sets a variable to the specified value on scope exit.
  97. Example of usage:
  98. @code
  99. void foo()
  100. {
  101. bool isDoingSomething = true;
  102. {
  103. wxON_BLOCK_EXIT_SET(isDoingSomething, false);
  104. ... do something ...
  105. }
  106. ... isDoingSomething is false now ...
  107. }
  108. @endcode
  109. Notice that @a value is copied, i.e. stored by value, so it can be a
  110. temporary object returned by a function call, for example.
  111. @see wxON_BLOCK_EXIT_OBJ0(), wxON_BLOCK_EXIT_NULL()
  112. @header{wx/scopeguard.h}
  113. */
  114. #define wxON_BLOCK_EXIT_SET(var, value)
  115. /**
  116. This macro sets the pointer passed to it as argument to NULL on scope exit.
  117. It must be used instead of wxON_BLOCK_EXIT_SET() when the value being set
  118. is @c NULL.
  119. @header{wx/scopeguard.h}
  120. */
  121. #define wxON_BLOCK_EXIT_NULL(ptr)
  122. //@}