scope_guard.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* Copyright 2003-2020 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/multi_index for library home page.
  7. */
  8. #ifndef BOOST_MULTI_INDEX_DETAIL_SCOPE_GUARD_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_SCOPE_GUARD_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/core/no_exceptions_support.hpp>
  14. #include <boost/mpl/if.hpp>
  15. namespace boost{
  16. namespace multi_index{
  17. namespace detail{
  18. /* Until some official version of the ScopeGuard idiom makes it into Boost,
  19. * we locally define our own. This is a merely reformated version of
  20. * ScopeGuard.h as defined in:
  21. * Alexandrescu, A., Marginean, P.:"Generic<Programming>: Change the Way You
  22. * Write Exception-Safe Code - Forever", C/C++ Users Jornal, Dec 2000,
  23. * http://www.drdobbs.com/184403758
  24. * with the following modifications:
  25. * - General pretty formatting (pretty to my taste at least.)
  26. * - Naming style changed to standard C++ library requirements.
  27. * - Added scope_guard_impl4 and obj_scope_guard_impl3, (Boost.MultiIndex
  28. * needs them). A better design would provide guards for many more
  29. * arguments through the Boost Preprocessor Library.
  30. * - Added scope_guard_impl_base::touch (see below.)
  31. * - Removed RefHolder and ByRef, whose functionality is provided
  32. * already by Boost.Ref.
  33. * - Removed static make_guard's and make_obj_guard's, so that the code
  34. * will work even if BOOST_NO_MEMBER_TEMPLATES is defined. This forces
  35. * us to move some private ctors to public, though.
  36. *
  37. * NB: CodeWarrior Pro 8 seems to have problems looking up safe_execute
  38. * without an explicit qualification.
  39. *
  40. * We also define the following variants of the idiom:
  41. *
  42. * - make_guard_if_c<bool>( ... )
  43. * - make_guard_if<IntegralConstant>( ... )
  44. * - make_obj_guard_if_c<bool>( ... )
  45. * - make_obj_guard_if<IntegralConstant>( ... )
  46. * which may be used with a compile-time constant to yield
  47. * a "null_guard" if the boolean compile-time parameter is false,
  48. * or conversely, the guard is only constructed if the constant is true.
  49. * This is useful to avoid extra tagging, because the returned
  50. * null_guard can be optimzed comlpetely away by the compiler.
  51. */
  52. class scope_guard_impl_base
  53. {
  54. public:
  55. scope_guard_impl_base():dismissed_(false){}
  56. void dismiss()const{dismissed_=true;}
  57. /* This helps prevent some "unused variable" warnings under, for instance,
  58. * GCC 3.2.
  59. */
  60. void touch()const{}
  61. protected:
  62. ~scope_guard_impl_base(){}
  63. scope_guard_impl_base(const scope_guard_impl_base& other):
  64. dismissed_(other.dismissed_)
  65. {
  66. other.dismiss();
  67. }
  68. template<typename J>
  69. static void safe_execute(J& j){
  70. BOOST_TRY{
  71. if(!j.dismissed_)j.execute();
  72. }
  73. BOOST_CATCH(...){}
  74. BOOST_CATCH_END
  75. }
  76. mutable bool dismissed_;
  77. private:
  78. scope_guard_impl_base& operator=(const scope_guard_impl_base&);
  79. };
  80. typedef const scope_guard_impl_base& scope_guard;
  81. struct null_guard : public scope_guard_impl_base
  82. {
  83. template< class T1 >
  84. null_guard( const T1& )
  85. { }
  86. template< class T1, class T2 >
  87. null_guard( const T1&, const T2& )
  88. { }
  89. template< class T1, class T2, class T3 >
  90. null_guard( const T1&, const T2&, const T3& )
  91. { }
  92. template< class T1, class T2, class T3, class T4 >
  93. null_guard( const T1&, const T2&, const T3&, const T4& )
  94. { }
  95. template< class T1, class T2, class T3, class T4, class T5 >
  96. null_guard( const T1&, const T2&, const T3&, const T4&, const T5& )
  97. { }
  98. };
  99. template< bool cond, class T >
  100. struct null_guard_return
  101. {
  102. typedef typename boost::mpl::if_c<cond,T,null_guard>::type type;
  103. };
  104. template<typename F>
  105. class scope_guard_impl0:public scope_guard_impl_base
  106. {
  107. public:
  108. scope_guard_impl0(F fun):fun_(fun){}
  109. ~scope_guard_impl0(){scope_guard_impl_base::safe_execute(*this);}
  110. void execute(){fun_();}
  111. protected:
  112. F fun_;
  113. };
  114. template<typename F>
  115. inline scope_guard_impl0<F> make_guard(F fun)
  116. {
  117. return scope_guard_impl0<F>(fun);
  118. }
  119. template<bool cond, typename F>
  120. inline typename null_guard_return<cond,scope_guard_impl0<F> >::type
  121. make_guard_if_c(F fun)
  122. {
  123. return typename null_guard_return<cond,scope_guard_impl0<F> >::type(fun);
  124. }
  125. template<typename C, typename F>
  126. inline typename null_guard_return<C::value,scope_guard_impl0<F> >::type
  127. make_guard_if(F fun)
  128. {
  129. return make_guard_if<C::value>(fun);
  130. }
  131. template<typename F,typename P1>
  132. class scope_guard_impl1:public scope_guard_impl_base
  133. {
  134. public:
  135. scope_guard_impl1(F fun,P1 p1):fun_(fun),p1_(p1){}
  136. ~scope_guard_impl1(){scope_guard_impl_base::safe_execute(*this);}
  137. void execute(){fun_(p1_);}
  138. protected:
  139. F fun_;
  140. const P1 p1_;
  141. };
  142. template<typename F,typename P1>
  143. inline scope_guard_impl1<F,P1> make_guard(F fun,P1 p1)
  144. {
  145. return scope_guard_impl1<F,P1>(fun,p1);
  146. }
  147. template<bool cond, typename F,typename P1>
  148. inline typename null_guard_return<cond,scope_guard_impl1<F,P1> >::type
  149. make_guard_if_c(F fun,P1 p1)
  150. {
  151. return typename null_guard_return<cond,scope_guard_impl1<F,P1> >::type(fun,p1);
  152. }
  153. template<typename C, typename F,typename P1>
  154. inline typename null_guard_return<C::value,scope_guard_impl1<F,P1> >::type
  155. make_guard_if(F fun,P1 p1)
  156. {
  157. return make_guard_if_c<C::value>(fun,p1);
  158. }
  159. template<typename F,typename P1,typename P2>
  160. class scope_guard_impl2:public scope_guard_impl_base
  161. {
  162. public:
  163. scope_guard_impl2(F fun,P1 p1,P2 p2):fun_(fun),p1_(p1),p2_(p2){}
  164. ~scope_guard_impl2(){scope_guard_impl_base::safe_execute(*this);}
  165. void execute(){fun_(p1_,p2_);}
  166. protected:
  167. F fun_;
  168. const P1 p1_;
  169. const P2 p2_;
  170. };
  171. template<typename F,typename P1,typename P2>
  172. inline scope_guard_impl2<F,P1,P2> make_guard(F fun,P1 p1,P2 p2)
  173. {
  174. return scope_guard_impl2<F,P1,P2>(fun,p1,p2);
  175. }
  176. template<bool cond, typename F,typename P1,typename P2>
  177. inline typename null_guard_return<cond,scope_guard_impl2<F,P1,P2> >::type
  178. make_guard_if_c(F fun,P1 p1,P2 p2)
  179. {
  180. return typename null_guard_return<cond,scope_guard_impl2<F,P1,P2> >::type(fun,p1,p2);
  181. }
  182. template<typename C, typename F,typename P1,typename P2>
  183. inline typename null_guard_return<C::value,scope_guard_impl2<F,P1,P2> >::type
  184. make_guard_if(F fun,P1 p1,P2 p2)
  185. {
  186. return make_guard_if_c<C::value>(fun,p1,p2);
  187. }
  188. template<typename F,typename P1,typename P2,typename P3>
  189. class scope_guard_impl3:public scope_guard_impl_base
  190. {
  191. public:
  192. scope_guard_impl3(F fun,P1 p1,P2 p2,P3 p3):fun_(fun),p1_(p1),p2_(p2),p3_(p3){}
  193. ~scope_guard_impl3(){scope_guard_impl_base::safe_execute(*this);}
  194. void execute(){fun_(p1_,p2_,p3_);}
  195. protected:
  196. F fun_;
  197. const P1 p1_;
  198. const P2 p2_;
  199. const P3 p3_;
  200. };
  201. template<typename F,typename P1,typename P2,typename P3>
  202. inline scope_guard_impl3<F,P1,P2,P3> make_guard(F fun,P1 p1,P2 p2,P3 p3)
  203. {
  204. return scope_guard_impl3<F,P1,P2,P3>(fun,p1,p2,p3);
  205. }
  206. template<bool cond,typename F,typename P1,typename P2,typename P3>
  207. inline typename null_guard_return<cond,scope_guard_impl3<F,P1,P2,P3> >::type
  208. make_guard_if_c(F fun,P1 p1,P2 p2,P3 p3)
  209. {
  210. return typename null_guard_return<cond,scope_guard_impl3<F,P1,P2,P3> >::type(fun,p1,p2,p3);
  211. }
  212. template<typename C,typename F,typename P1,typename P2,typename P3>
  213. inline typename null_guard_return< C::value,scope_guard_impl3<F,P1,P2,P3> >::type
  214. make_guard_if(F fun,P1 p1,P2 p2,P3 p3)
  215. {
  216. return make_guard_if_c<C::value>(fun,p1,p2,p3);
  217. }
  218. template<typename F,typename P1,typename P2,typename P3,typename P4>
  219. class scope_guard_impl4:public scope_guard_impl_base
  220. {
  221. public:
  222. scope_guard_impl4(F fun,P1 p1,P2 p2,P3 p3,P4 p4):
  223. fun_(fun),p1_(p1),p2_(p2),p3_(p3),p4_(p4){}
  224. ~scope_guard_impl4(){scope_guard_impl_base::safe_execute(*this);}
  225. void execute(){fun_(p1_,p2_,p3_,p4_);}
  226. protected:
  227. F fun_;
  228. const P1 p1_;
  229. const P2 p2_;
  230. const P3 p3_;
  231. const P4 p4_;
  232. };
  233. template<typename F,typename P1,typename P2,typename P3,typename P4>
  234. inline scope_guard_impl4<F,P1,P2,P3,P4> make_guard(
  235. F fun,P1 p1,P2 p2,P3 p3,P4 p4)
  236. {
  237. return scope_guard_impl4<F,P1,P2,P3,P4>(fun,p1,p2,p3,p4);
  238. }
  239. template<bool cond, typename F,typename P1,typename P2,typename P3,typename P4>
  240. inline typename null_guard_return<cond,scope_guard_impl4<F,P1,P2,P3,P4> >::type
  241. make_guard_if_c(
  242. F fun,P1 p1,P2 p2,P3 p3,P4 p4)
  243. {
  244. return typename null_guard_return<cond,scope_guard_impl4<F,P1,P2,P3,P4> >::type(fun,p1,p2,p3,p4);
  245. }
  246. template<typename C, typename F,typename P1,typename P2,typename P3,typename P4>
  247. inline typename null_guard_return<C::value,scope_guard_impl4<F,P1,P2,P3,P4> >::type
  248. make_guard_if(
  249. F fun,P1 p1,P2 p2,P3 p3,P4 p4)
  250. {
  251. return make_guard_if_c<C::value>(fun,p1,p2,p3,p4);
  252. }
  253. template<class Obj,typename MemFun>
  254. class obj_scope_guard_impl0:public scope_guard_impl_base
  255. {
  256. public:
  257. obj_scope_guard_impl0(Obj& obj,MemFun mem_fun):obj_(obj),mem_fun_(mem_fun){}
  258. ~obj_scope_guard_impl0(){scope_guard_impl_base::safe_execute(*this);}
  259. void execute(){(obj_.*mem_fun_)();}
  260. protected:
  261. Obj& obj_;
  262. MemFun mem_fun_;
  263. };
  264. template<class Obj,typename MemFun>
  265. inline obj_scope_guard_impl0<Obj,MemFun> make_obj_guard(Obj& obj,MemFun mem_fun)
  266. {
  267. return obj_scope_guard_impl0<Obj,MemFun>(obj,mem_fun);
  268. }
  269. template<bool cond, class Obj,typename MemFun>
  270. inline typename null_guard_return<cond,obj_scope_guard_impl0<Obj,MemFun> >::type
  271. make_obj_guard_if_c(Obj& obj,MemFun mem_fun)
  272. {
  273. return typename null_guard_return<cond,obj_scope_guard_impl0<Obj,MemFun> >::type(obj,mem_fun);
  274. }
  275. template<typename C, class Obj,typename MemFun>
  276. inline typename null_guard_return<C::value,obj_scope_guard_impl0<Obj,MemFun> >::type
  277. make_obj_guard_if(Obj& obj,MemFun mem_fun)
  278. {
  279. return make_obj_guard_if_c<C::value>(obj,mem_fun);
  280. }
  281. template<class Obj,typename MemFun,typename P1>
  282. class obj_scope_guard_impl1:public scope_guard_impl_base
  283. {
  284. public:
  285. obj_scope_guard_impl1(Obj& obj,MemFun mem_fun,P1 p1):
  286. obj_(obj),mem_fun_(mem_fun),p1_(p1){}
  287. ~obj_scope_guard_impl1(){scope_guard_impl_base::safe_execute(*this);}
  288. void execute(){(obj_.*mem_fun_)(p1_);}
  289. protected:
  290. Obj& obj_;
  291. MemFun mem_fun_;
  292. const P1 p1_;
  293. };
  294. template<class Obj,typename MemFun,typename P1>
  295. inline obj_scope_guard_impl1<Obj,MemFun,P1> make_obj_guard(
  296. Obj& obj,MemFun mem_fun,P1 p1)
  297. {
  298. return obj_scope_guard_impl1<Obj,MemFun,P1>(obj,mem_fun,p1);
  299. }
  300. template<bool cond, class Obj,typename MemFun,typename P1>
  301. inline typename null_guard_return<cond,obj_scope_guard_impl1<Obj,MemFun,P1> >::type
  302. make_obj_guard_if_c( Obj& obj,MemFun mem_fun,P1 p1)
  303. {
  304. return typename null_guard_return<cond,obj_scope_guard_impl1<Obj,MemFun,P1> >::type(obj,mem_fun,p1);
  305. }
  306. template<typename C, class Obj,typename MemFun,typename P1>
  307. inline typename null_guard_return<C::value,obj_scope_guard_impl1<Obj,MemFun,P1> >::type
  308. make_obj_guard_if( Obj& obj,MemFun mem_fun,P1 p1)
  309. {
  310. return make_obj_guard_if_c<C::value>(obj,mem_fun,p1);
  311. }
  312. template<class Obj,typename MemFun,typename P1,typename P2>
  313. class obj_scope_guard_impl2:public scope_guard_impl_base
  314. {
  315. public:
  316. obj_scope_guard_impl2(Obj& obj,MemFun mem_fun,P1 p1,P2 p2):
  317. obj_(obj),mem_fun_(mem_fun),p1_(p1),p2_(p2)
  318. {}
  319. ~obj_scope_guard_impl2(){scope_guard_impl_base::safe_execute(*this);}
  320. void execute(){(obj_.*mem_fun_)(p1_,p2_);}
  321. protected:
  322. Obj& obj_;
  323. MemFun mem_fun_;
  324. const P1 p1_;
  325. const P2 p2_;
  326. };
  327. template<class Obj,typename MemFun,typename P1,typename P2>
  328. inline obj_scope_guard_impl2<Obj,MemFun,P1,P2>
  329. make_obj_guard(Obj& obj,MemFun mem_fun,P1 p1,P2 p2)
  330. {
  331. return obj_scope_guard_impl2<Obj,MemFun,P1,P2>(obj,mem_fun,p1,p2);
  332. }
  333. template<bool cond, class Obj,typename MemFun,typename P1,typename P2>
  334. inline typename null_guard_return<cond,obj_scope_guard_impl2<Obj,MemFun,P1,P2> >::type
  335. make_obj_guard_if_c(Obj& obj,MemFun mem_fun,P1 p1,P2 p2)
  336. {
  337. return typename null_guard_return<cond,obj_scope_guard_impl2<Obj,MemFun,P1,P2> >::type(obj,mem_fun,p1,p2);
  338. }
  339. template<typename C, class Obj,typename MemFun,typename P1,typename P2>
  340. inline typename null_guard_return<C::value,obj_scope_guard_impl2<Obj,MemFun,P1,P2> >::type
  341. make_obj_guard_if(Obj& obj,MemFun mem_fun,P1 p1,P2 p2)
  342. {
  343. return make_obj_guard_if_c<C::value>(obj,mem_fun,p1,p2);
  344. }
  345. template<class Obj,typename MemFun,typename P1,typename P2,typename P3>
  346. class obj_scope_guard_impl3:public scope_guard_impl_base
  347. {
  348. public:
  349. obj_scope_guard_impl3(Obj& obj,MemFun mem_fun,P1 p1,P2 p2,P3 p3):
  350. obj_(obj),mem_fun_(mem_fun),p1_(p1),p2_(p2),p3_(p3)
  351. {}
  352. ~obj_scope_guard_impl3(){scope_guard_impl_base::safe_execute(*this);}
  353. void execute(){(obj_.*mem_fun_)(p1_,p2_,p3_);}
  354. protected:
  355. Obj& obj_;
  356. MemFun mem_fun_;
  357. const P1 p1_;
  358. const P2 p2_;
  359. const P3 p3_;
  360. };
  361. template<class Obj,typename MemFun,typename P1,typename P2,typename P3>
  362. inline obj_scope_guard_impl3<Obj,MemFun,P1,P2,P3>
  363. make_obj_guard(Obj& obj,MemFun mem_fun,P1 p1,P2 p2,P3 p3)
  364. {
  365. return obj_scope_guard_impl3<Obj,MemFun,P1,P2,P3>(obj,mem_fun,p1,p2,p3);
  366. }
  367. template<bool cond, class Obj,typename MemFun,typename P1,typename P2,typename P3>
  368. inline typename null_guard_return<cond,obj_scope_guard_impl3<Obj,MemFun,P1,P2,P3> >::type
  369. make_obj_guard_if_c(Obj& obj,MemFun mem_fun,P1 p1,P2 p2,P3 p3)
  370. {
  371. return typename null_guard_return<cond,obj_scope_guard_impl3<Obj,MemFun,P1,P2,P3> >::type(obj,mem_fun,p1,p2,p3);
  372. }
  373. template<typename C, class Obj,typename MemFun,typename P1,typename P2,typename P3>
  374. inline typename null_guard_return<C::value,obj_scope_guard_impl3<Obj,MemFun,P1,P2,P3> >::type
  375. make_obj_guard_if(Obj& obj,MemFun mem_fun,P1 p1,P2 p2,P3 p3)
  376. {
  377. return make_obj_guard_if_c<C::value>(obj,mem_fun,p1,p2,p3);
  378. }
  379. } /* namespace multi_index::detail */
  380. } /* namespace multi_index */
  381. } /* namespace boost */
  382. #endif