scopeguard.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/scopeguard.h
  3. // Purpose: declares wxwxScopeGuard and related macros
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 03.07.2003
  7. // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. /*
  11. Acknowledgements: this header is heavily based on (well, almost the exact
  12. copy of) ScopeGuard.h by Andrei Alexandrescu and Petru Marginean published
  13. in December 2000 issue of C/C++ Users Journal.
  14. http://www.cuj.com/documents/cujcexp1812alexandr/
  15. */
  16. #ifndef _WX_SCOPEGUARD_H_
  17. #define _WX_SCOPEGUARD_H_
  18. #include "wx/defs.h"
  19. #include "wx/except.h"
  20. // ----------------------------------------------------------------------------
  21. // helpers
  22. // ----------------------------------------------------------------------------
  23. #ifdef __WATCOMC__
  24. // WATCOM-FIXME: C++ of Open Watcom 1.3 doesn't like OnScopeExit() created
  25. // through template so it must be workarounded with dedicated inlined macro.
  26. // For compatibility with Watcom compilers wxPrivate::OnScopeExit must be
  27. // replaced with wxPrivateOnScopeExit but in user code (for everyone who
  28. // doesn't care about OW compatibility) wxPrivate::OnScopeExit still works.
  29. #define wxPrivateOnScopeExit(guard) \
  30. { \
  31. if ( !(guard).WasDismissed() ) \
  32. { \
  33. wxTRY \
  34. { \
  35. (guard).Execute(); \
  36. } \
  37. wxCATCH_ALL(;) \
  38. } \
  39. }
  40. #define wxPrivateUse(n) wxUnusedVar(n)
  41. #else
  42. namespace wxPrivate
  43. {
  44. // in the original implementation this was a member template function of
  45. // ScopeGuardImplBase but gcc 2.8 which is still used for OS/2 doesn't
  46. // support member templates and so we must make it global
  47. template <class ScopeGuardImpl>
  48. void OnScopeExit(ScopeGuardImpl& guard)
  49. {
  50. if ( !guard.WasDismissed() )
  51. {
  52. // we're called from ScopeGuardImpl dtor and so we must not throw
  53. wxTRY
  54. {
  55. guard.Execute();
  56. }
  57. wxCATCH_ALL(;) // do nothing, just eat the exception
  58. }
  59. }
  60. // just to avoid the warning about unused variables
  61. template <class T>
  62. void Use(const T& WXUNUSED(t))
  63. {
  64. }
  65. } // namespace wxPrivate
  66. #define wxPrivateOnScopeExit(n) wxPrivate::OnScopeExit(n)
  67. #define wxPrivateUse(n) wxPrivate::Use(n)
  68. #endif
  69. // ============================================================================
  70. // wxScopeGuard for functions and functors
  71. // ============================================================================
  72. // ----------------------------------------------------------------------------
  73. // wxScopeGuardImplBase: used by wxScopeGuardImpl[0..N] below
  74. // ----------------------------------------------------------------------------
  75. class wxScopeGuardImplBase
  76. {
  77. public:
  78. wxScopeGuardImplBase() : m_wasDismissed(false) { }
  79. wxScopeGuardImplBase(const wxScopeGuardImplBase& other)
  80. : m_wasDismissed(other.m_wasDismissed)
  81. {
  82. other.Dismiss();
  83. }
  84. void Dismiss() const { m_wasDismissed = true; }
  85. // for OnScopeExit() only (we can't make it friend, unfortunately)!
  86. bool WasDismissed() const { return m_wasDismissed; }
  87. protected:
  88. ~wxScopeGuardImplBase() { }
  89. // must be mutable for copy ctor to work
  90. mutable bool m_wasDismissed;
  91. private:
  92. wxScopeGuardImplBase& operator=(const wxScopeGuardImplBase&);
  93. };
  94. // wxScopeGuard is just a reference, see the explanation in CUJ article
  95. typedef const wxScopeGuardImplBase& wxScopeGuard;
  96. // ----------------------------------------------------------------------------
  97. // wxScopeGuardImpl0: scope guard for actions without parameters
  98. // ----------------------------------------------------------------------------
  99. template <class F>
  100. class wxScopeGuardImpl0 : public wxScopeGuardImplBase
  101. {
  102. public:
  103. static wxScopeGuardImpl0<F> MakeGuard(F fun)
  104. {
  105. return wxScopeGuardImpl0<F>(fun);
  106. }
  107. ~wxScopeGuardImpl0() { wxPrivateOnScopeExit(*this); }
  108. void Execute() { m_fun(); }
  109. protected:
  110. wxScopeGuardImpl0(F fun) : m_fun(fun) { }
  111. F m_fun;
  112. wxScopeGuardImpl0& operator=(const wxScopeGuardImpl0&);
  113. };
  114. template <class F>
  115. inline wxScopeGuardImpl0<F> wxMakeGuard(F fun)
  116. {
  117. return wxScopeGuardImpl0<F>::MakeGuard(fun);
  118. }
  119. // ----------------------------------------------------------------------------
  120. // wxScopeGuardImpl1: scope guard for actions with 1 parameter
  121. // ----------------------------------------------------------------------------
  122. template <class F, class P1>
  123. class wxScopeGuardImpl1 : public wxScopeGuardImplBase
  124. {
  125. public:
  126. static wxScopeGuardImpl1<F, P1> MakeGuard(F fun, P1 p1)
  127. {
  128. return wxScopeGuardImpl1<F, P1>(fun, p1);
  129. }
  130. ~wxScopeGuardImpl1() { wxPrivateOnScopeExit(* this); }
  131. void Execute() { m_fun(m_p1); }
  132. protected:
  133. wxScopeGuardImpl1(F fun, P1 p1) : m_fun(fun), m_p1(p1) { }
  134. F m_fun;
  135. const P1 m_p1;
  136. wxScopeGuardImpl1& operator=(const wxScopeGuardImpl1&);
  137. };
  138. template <class F, class P1>
  139. inline wxScopeGuardImpl1<F, P1> wxMakeGuard(F fun, P1 p1)
  140. {
  141. return wxScopeGuardImpl1<F, P1>::MakeGuard(fun, p1);
  142. }
  143. // ----------------------------------------------------------------------------
  144. // wxScopeGuardImpl2: scope guard for actions with 2 parameters
  145. // ----------------------------------------------------------------------------
  146. template <class F, class P1, class P2>
  147. class wxScopeGuardImpl2 : public wxScopeGuardImplBase
  148. {
  149. public:
  150. static wxScopeGuardImpl2<F, P1, P2> MakeGuard(F fun, P1 p1, P2 p2)
  151. {
  152. return wxScopeGuardImpl2<F, P1, P2>(fun, p1, p2);
  153. }
  154. ~wxScopeGuardImpl2() { wxPrivateOnScopeExit(*this); }
  155. void Execute() { m_fun(m_p1, m_p2); }
  156. protected:
  157. wxScopeGuardImpl2(F fun, P1 p1, P2 p2) : m_fun(fun), m_p1(p1), m_p2(p2) { }
  158. F m_fun;
  159. const P1 m_p1;
  160. const P2 m_p2;
  161. wxScopeGuardImpl2& operator=(const wxScopeGuardImpl2&);
  162. };
  163. template <class F, class P1, class P2>
  164. inline wxScopeGuardImpl2<F, P1, P2> wxMakeGuard(F fun, P1 p1, P2 p2)
  165. {
  166. return wxScopeGuardImpl2<F, P1, P2>::MakeGuard(fun, p1, p2);
  167. }
  168. // ----------------------------------------------------------------------------
  169. // wxScopeGuardImpl3: scope guard for actions with 3 parameters
  170. // ----------------------------------------------------------------------------
  171. template <class F, class P1, class P2, class P3>
  172. class wxScopeGuardImpl3 : public wxScopeGuardImplBase
  173. {
  174. public:
  175. static wxScopeGuardImpl3<F, P1, P2, P3> MakeGuard(F fun, P1 p1, P2 p2, P3 p3)
  176. {
  177. return wxScopeGuardImpl3<F, P1, P2, P3>(fun, p1, p2, p3);
  178. }
  179. ~wxScopeGuardImpl3() { wxPrivateOnScopeExit(*this); }
  180. void Execute() { m_fun(m_p1, m_p2, m_p3); }
  181. protected:
  182. wxScopeGuardImpl3(F fun, P1 p1, P2 p2, P3 p3)
  183. : m_fun(fun), m_p1(p1), m_p2(p2), m_p3(p3) { }
  184. F m_fun;
  185. const P1 m_p1;
  186. const P2 m_p2;
  187. const P3 m_p3;
  188. wxScopeGuardImpl3& operator=(const wxScopeGuardImpl3&);
  189. };
  190. template <class F, class P1, class P2, class P3>
  191. inline wxScopeGuardImpl3<F, P1, P2, P3> wxMakeGuard(F fun, P1 p1, P2 p2, P3 p3)
  192. {
  193. return wxScopeGuardImpl3<F, P1, P2, P3>::MakeGuard(fun, p1, p2, p3);
  194. }
  195. // ============================================================================
  196. // wxScopeGuards for object methods
  197. // ============================================================================
  198. // ----------------------------------------------------------------------------
  199. // wxObjScopeGuardImpl0
  200. // ----------------------------------------------------------------------------
  201. template <class Obj, class MemFun>
  202. class wxObjScopeGuardImpl0 : public wxScopeGuardImplBase
  203. {
  204. public:
  205. static wxObjScopeGuardImpl0<Obj, MemFun>
  206. MakeObjGuard(Obj& obj, MemFun memFun)
  207. {
  208. return wxObjScopeGuardImpl0<Obj, MemFun>(obj, memFun);
  209. }
  210. ~wxObjScopeGuardImpl0() { wxPrivateOnScopeExit(*this); }
  211. void Execute() { (m_obj.*m_memfun)(); }
  212. protected:
  213. wxObjScopeGuardImpl0(Obj& obj, MemFun memFun)
  214. : m_obj(obj), m_memfun(memFun) { }
  215. Obj& m_obj;
  216. MemFun m_memfun;
  217. };
  218. template <class Obj, class MemFun>
  219. inline wxObjScopeGuardImpl0<Obj, MemFun> wxMakeObjGuard(Obj& obj, MemFun memFun)
  220. {
  221. return wxObjScopeGuardImpl0<Obj, MemFun>::MakeObjGuard(obj, memFun);
  222. }
  223. template <class Obj, class MemFun, class P1>
  224. class wxObjScopeGuardImpl1 : public wxScopeGuardImplBase
  225. {
  226. public:
  227. static wxObjScopeGuardImpl1<Obj, MemFun, P1>
  228. MakeObjGuard(Obj& obj, MemFun memFun, P1 p1)
  229. {
  230. return wxObjScopeGuardImpl1<Obj, MemFun, P1>(obj, memFun, p1);
  231. }
  232. ~wxObjScopeGuardImpl1() { wxPrivateOnScopeExit(*this); }
  233. void Execute() { (m_obj.*m_memfun)(m_p1); }
  234. protected:
  235. wxObjScopeGuardImpl1(Obj& obj, MemFun memFun, P1 p1)
  236. : m_obj(obj), m_memfun(memFun), m_p1(p1) { }
  237. Obj& m_obj;
  238. MemFun m_memfun;
  239. const P1 m_p1;
  240. };
  241. template <class Obj, class MemFun, class P1>
  242. inline wxObjScopeGuardImpl1<Obj, MemFun, P1>
  243. wxMakeObjGuard(Obj& obj, MemFun memFun, P1 p1)
  244. {
  245. return wxObjScopeGuardImpl1<Obj, MemFun, P1>::MakeObjGuard(obj, memFun, p1);
  246. }
  247. template <class Obj, class MemFun, class P1, class P2>
  248. class wxObjScopeGuardImpl2 : public wxScopeGuardImplBase
  249. {
  250. public:
  251. static wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>
  252. MakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2)
  253. {
  254. return wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>(obj, memFun, p1, p2);
  255. }
  256. ~wxObjScopeGuardImpl2() { wxPrivateOnScopeExit(*this); }
  257. void Execute() { (m_obj.*m_memfun)(m_p1, m_p2); }
  258. protected:
  259. wxObjScopeGuardImpl2(Obj& obj, MemFun memFun, P1 p1, P2 p2)
  260. : m_obj(obj), m_memfun(memFun), m_p1(p1), m_p2(p2) { }
  261. Obj& m_obj;
  262. MemFun m_memfun;
  263. const P1 m_p1;
  264. const P2 m_p2;
  265. };
  266. template <class Obj, class MemFun, class P1, class P2>
  267. inline wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>
  268. wxMakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2)
  269. {
  270. return wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>::
  271. MakeObjGuard(obj, memFun, p1, p2);
  272. }
  273. template <class Obj, class MemFun, class P1, class P2, class P3>
  274. class wxObjScopeGuardImpl3 : public wxScopeGuardImplBase
  275. {
  276. public:
  277. static wxObjScopeGuardImpl3<Obj, MemFun, P1, P2, P3>
  278. MakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2, P3 p3)
  279. {
  280. return wxObjScopeGuardImpl3<Obj, MemFun, P1, P2, P3>(obj, memFun, p1, p2, p3);
  281. }
  282. ~wxObjScopeGuardImpl3() { wxPrivateOnScopeExit(*this); }
  283. void Execute() { (m_obj.*m_memfun)(m_p1, m_p2, m_p3); }
  284. protected:
  285. wxObjScopeGuardImpl3(Obj& obj, MemFun memFun, P1 p1, P2 p2, P3 p3)
  286. : m_obj(obj), m_memfun(memFun), m_p1(p1), m_p2(p2), m_p3(p3) { }
  287. Obj& m_obj;
  288. MemFun m_memfun;
  289. const P1 m_p1;
  290. const P2 m_p2;
  291. const P3 m_p3;
  292. };
  293. template <class Obj, class MemFun, class P1, class P2, class P3>
  294. inline wxObjScopeGuardImpl3<Obj, MemFun, P1, P2, P3>
  295. wxMakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2, P3 p3)
  296. {
  297. return wxObjScopeGuardImpl3<Obj, MemFun, P1, P2, P3>::
  298. MakeObjGuard(obj, memFun, p1, p2, p3);
  299. }
  300. // ----------------------------------------------------------------------------
  301. // wxVariableSetter: use the same technique as for wxScopeGuard to allow
  302. // setting a variable to some value on block exit
  303. // ----------------------------------------------------------------------------
  304. namespace wxPrivate
  305. {
  306. // empty class just to be able to define a reference to it
  307. class VariableSetterBase : public wxScopeGuardImplBase { };
  308. typedef const VariableSetterBase& VariableSetter;
  309. template <typename T, typename U>
  310. class VariableSetterImpl : public VariableSetterBase
  311. {
  312. public:
  313. VariableSetterImpl(T& var, U value)
  314. : m_var(var),
  315. m_value(value)
  316. {
  317. }
  318. ~VariableSetterImpl() { wxPrivateOnScopeExit(*this); }
  319. void Execute() { m_var = m_value; }
  320. private:
  321. T& m_var;
  322. const U m_value;
  323. // suppress the warning about assignment operator not being generated
  324. VariableSetterImpl<T, U>& operator=(const VariableSetterImpl<T, U>&);
  325. };
  326. template <typename T>
  327. class VariableNullerImpl : public VariableSetterBase
  328. {
  329. public:
  330. VariableNullerImpl(T& var)
  331. : m_var(var)
  332. {
  333. }
  334. ~VariableNullerImpl() { wxPrivateOnScopeExit(*this); }
  335. void Execute() { m_var = NULL; }
  336. private:
  337. T& m_var;
  338. VariableNullerImpl<T>& operator=(const VariableNullerImpl<T>&);
  339. };
  340. } // namespace wxPrivate
  341. template <typename T, typename U>
  342. inline
  343. wxPrivate::VariableSetterImpl<T, U> wxMakeVarSetter(T& var, U value)
  344. {
  345. return wxPrivate::VariableSetterImpl<T, U>(var, value);
  346. }
  347. // calling wxMakeVarSetter(ptr, NULL) doesn't work because U is deduced to be
  348. // "int" and subsequent assignment of "U" to "T *" fails, so provide a special
  349. // function for this special case
  350. template <typename T>
  351. inline
  352. wxPrivate::VariableNullerImpl<T> wxMakeVarNuller(T& var)
  353. {
  354. return wxPrivate::VariableNullerImpl<T>(var);
  355. }
  356. // ============================================================================
  357. // macros for declaring unnamed scoped guards (which can't be dismissed)
  358. // ============================================================================
  359. // NB: the original code has a single (and much nicer) ON_BLOCK_EXIT macro
  360. // but this results in compiler warnings about unused variables and I
  361. // didn't find a way to work around this other than by having different
  362. // macros with different names or using a less natural syntax for passing
  363. // the arguments (e.g. as Boost preprocessor sequences, which would mean
  364. // having to write wxON_BLOCK_EXIT(fwrite, (buf)(size)(n)(fp)) instead of
  365. // wxON_BLOCK_EXIT4(fwrite, buf, size, n, fp)).
  366. #define wxGuardName wxMAKE_UNIQUE_NAME(wxScopeGuard)
  367. #define wxON_BLOCK_EXIT0_IMPL(n, f) \
  368. wxScopeGuard n = wxMakeGuard(f); \
  369. wxPrivateUse(n)
  370. #define wxON_BLOCK_EXIT0(f) \
  371. wxON_BLOCK_EXIT0_IMPL(wxGuardName, f)
  372. #define wxON_BLOCK_EXIT_OBJ0_IMPL(n, o, m) \
  373. wxScopeGuard n = wxMakeObjGuard(o, m); \
  374. wxPrivateUse(n)
  375. #define wxON_BLOCK_EXIT_OBJ0(o, m) \
  376. wxON_BLOCK_EXIT_OBJ0_IMPL(wxGuardName, o, &m)
  377. #define wxON_BLOCK_EXIT_THIS0(m) \
  378. wxON_BLOCK_EXIT_OBJ0(*this, m)
  379. #define wxON_BLOCK_EXIT1_IMPL(n, f, p1) \
  380. wxScopeGuard n = wxMakeGuard(f, p1); \
  381. wxPrivateUse(n)
  382. #define wxON_BLOCK_EXIT1(f, p1) \
  383. wxON_BLOCK_EXIT1_IMPL(wxGuardName, f, p1)
  384. #define wxON_BLOCK_EXIT_OBJ1_IMPL(n, o, m, p1) \
  385. wxScopeGuard n = wxMakeObjGuard(o, m, p1); \
  386. wxPrivateUse(n)
  387. #define wxON_BLOCK_EXIT_OBJ1(o, m, p1) \
  388. wxON_BLOCK_EXIT_OBJ1_IMPL(wxGuardName, o, &m, p1)
  389. #define wxON_BLOCK_EXIT_THIS1(m, p1) \
  390. wxON_BLOCK_EXIT_OBJ1(*this, m, p1)
  391. #define wxON_BLOCK_EXIT2_IMPL(n, f, p1, p2) \
  392. wxScopeGuard n = wxMakeGuard(f, p1, p2); \
  393. wxPrivateUse(n)
  394. #define wxON_BLOCK_EXIT2(f, p1, p2) \
  395. wxON_BLOCK_EXIT2_IMPL(wxGuardName, f, p1, p2)
  396. #define wxON_BLOCK_EXIT_OBJ2_IMPL(n, o, m, p1, p2) \
  397. wxScopeGuard n = wxMakeObjGuard(o, m, p1, p2); \
  398. wxPrivateUse(n)
  399. #define wxON_BLOCK_EXIT_OBJ2(o, m, p1, p2) \
  400. wxON_BLOCK_EXIT_OBJ2_IMPL(wxGuardName, o, &m, p1, p2)
  401. #define wxON_BLOCK_EXIT_THIS2(m, p1, p2) \
  402. wxON_BLOCK_EXIT_OBJ2(*this, m, p1, p2)
  403. #define wxON_BLOCK_EXIT3_IMPL(n, f, p1, p2, p3) \
  404. wxScopeGuard n = wxMakeGuard(f, p1, p2, p3); \
  405. wxPrivateUse(n)
  406. #define wxON_BLOCK_EXIT3(f, p1, p2, p3) \
  407. wxON_BLOCK_EXIT3_IMPL(wxGuardName, f, p1, p2, p3)
  408. #define wxON_BLOCK_EXIT_OBJ3_IMPL(n, o, m, p1, p2, p3) \
  409. wxScopeGuard n = wxMakeObjGuard(o, m, p1, p2, p3); \
  410. wxPrivateUse(n)
  411. #define wxON_BLOCK_EXIT_OBJ3(o, m, p1, p2, p3) \
  412. wxON_BLOCK_EXIT_OBJ3_IMPL(wxGuardName, o, &m, p1, p2, p3)
  413. #define wxON_BLOCK_EXIT_THIS3(m, p1, p2, p3) \
  414. wxON_BLOCK_EXIT_OBJ3(*this, m, p1, p2, p3)
  415. #define wxSetterName wxMAKE_UNIQUE_NAME(wxVarSetter)
  416. #define wxON_BLOCK_EXIT_SET_IMPL(n, var, value) \
  417. wxPrivate::VariableSetter n = wxMakeVarSetter(var, value); \
  418. wxPrivateUse(n)
  419. #define wxON_BLOCK_EXIT_SET(var, value) \
  420. wxON_BLOCK_EXIT_SET_IMPL(wxSetterName, var, value)
  421. #define wxON_BLOCK_EXIT_NULL_IMPL(n, var) \
  422. wxPrivate::VariableSetter n = wxMakeVarNuller(var); \
  423. wxPrivateUse(n)
  424. #define wxON_BLOCK_EXIT_NULL(ptr) \
  425. wxON_BLOCK_EXIT_NULL_IMPL(wxSetterName, ptr)
  426. #endif // _WX_SCOPEGUARD_H_