Chris@16: /* Copyright 2003-2013 Joaquin M Lopez Munoz. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@16: * See http://www.boost.org/libs/multi_index for library home page. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_MULTI_INDEX_DETAIL_SCOPE_GUARD_HPP Chris@16: #define BOOST_MULTI_INDEX_DETAIL_SCOPE_GUARD_HPP Chris@16: Chris@101: #if defined(_MSC_VER) Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost{ Chris@16: Chris@16: namespace multi_index{ Chris@16: Chris@16: namespace detail{ Chris@16: Chris@16: /* Until some official version of the ScopeGuard idiom makes it into Boost, Chris@16: * we locally define our own. This is a merely reformated version of Chris@16: * ScopeGuard.h as defined in: Chris@16: * Alexandrescu, A., Marginean, P.:"Generic: Change the Way You Chris@16: * Write Exception-Safe Code - Forever", C/C++ Users Jornal, Dec 2000, Chris@16: * http://www.drdobbs.com/184403758 Chris@16: * with the following modifications: Chris@16: * - General pretty formatting (pretty to my taste at least.) Chris@16: * - Naming style changed to standard C++ library requirements. Chris@16: * - Added scope_guard_impl4 and obj_scope_guard_impl3, (Boost.MultiIndex Chris@16: * needs them). A better design would provide guards for many more Chris@16: * arguments through the Boost Preprocessor Library. Chris@16: * - Added scope_guard_impl_base::touch (see below.) Chris@16: * - Removed RefHolder and ByRef, whose functionality is provided Chris@16: * already by Boost.Ref. Chris@16: * - Removed static make_guard's and make_obj_guard's, so that the code Chris@16: * will work even if BOOST_NO_MEMBER_TEMPLATES is defined. This forces Chris@16: * us to move some private ctors to public, though. Chris@16: * Chris@16: * NB: CodeWarrior Pro 8 seems to have problems looking up safe_execute Chris@16: * without an explicit qualification. Chris@16: * Chris@16: * We also define the following variants of the idiom: Chris@16: * Chris@16: * - make_guard_if_c( ... ) Chris@16: * - make_guard_if( ... ) Chris@16: * - make_obj_guard_if_c( ... ) Chris@16: * - make_obj_guard_if( ... ) Chris@16: * which may be used with a compile-time constant to yield Chris@16: * a "null_guard" if the boolean compile-time parameter is false, Chris@16: * or conversely, the guard is only constructed if the constant is true. Chris@16: * This is useful to avoid extra tagging, because the returned Chris@16: * null_guard can be optimzed comlpetely away by the compiler. Chris@16: */ Chris@16: Chris@16: class scope_guard_impl_base Chris@16: { Chris@16: public: Chris@16: scope_guard_impl_base():dismissed_(false){} Chris@16: void dismiss()const{dismissed_=true;} Chris@16: Chris@16: /* This helps prevent some "unused variable" warnings under, for instance, Chris@16: * GCC 3.2. Chris@16: */ Chris@16: void touch()const{} Chris@16: Chris@16: protected: Chris@16: ~scope_guard_impl_base(){} Chris@16: Chris@16: scope_guard_impl_base(const scope_guard_impl_base& other): Chris@16: dismissed_(other.dismissed_) Chris@16: { Chris@16: other.dismiss(); Chris@16: } Chris@16: Chris@16: template Chris@16: static void safe_execute(J& j){ Chris@16: BOOST_TRY{ Chris@16: if(!j.dismissed_)j.execute(); Chris@16: } Chris@16: BOOST_CATCH(...){} Chris@16: BOOST_CATCH_END Chris@16: } Chris@16: Chris@16: mutable bool dismissed_; Chris@16: Chris@16: private: Chris@16: scope_guard_impl_base& operator=(const scope_guard_impl_base&); Chris@16: }; Chris@16: Chris@16: typedef const scope_guard_impl_base& scope_guard; Chris@16: Chris@16: struct null_guard : public scope_guard_impl_base Chris@16: { Chris@16: template< class T1 > Chris@16: null_guard( const T1& ) Chris@16: { } Chris@16: Chris@16: template< class T1, class T2 > Chris@16: null_guard( const T1&, const T2& ) Chris@16: { } Chris@16: Chris@16: template< class T1, class T2, class T3 > Chris@16: null_guard( const T1&, const T2&, const T3& ) Chris@16: { } Chris@16: Chris@16: template< class T1, class T2, class T3, class T4 > Chris@16: null_guard( const T1&, const T2&, const T3&, const T4& ) Chris@16: { } Chris@16: Chris@16: template< class T1, class T2, class T3, class T4, class T5 > Chris@16: null_guard( const T1&, const T2&, const T3&, const T4&, const T5& ) Chris@16: { } Chris@16: }; Chris@16: Chris@16: template< bool cond, class T > Chris@16: struct null_guard_return Chris@16: { Chris@16: typedef typename boost::mpl::if_c::type type; Chris@16: }; Chris@16: Chris@16: template Chris@16: class scope_guard_impl0:public scope_guard_impl_base Chris@16: { Chris@16: public: Chris@16: scope_guard_impl0(F fun):fun_(fun){} Chris@16: ~scope_guard_impl0(){scope_guard_impl_base::safe_execute(*this);} Chris@16: void execute(){fun_();} Chris@16: Chris@16: protected: Chris@16: Chris@16: F fun_; Chris@16: }; Chris@16: Chris@16: template Chris@16: inline scope_guard_impl0 make_guard(F fun) Chris@16: { Chris@16: return scope_guard_impl0(fun); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename null_guard_return >::type Chris@16: make_guard_if_c(F fun) Chris@16: { Chris@16: return typename null_guard_return >::type(fun); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename null_guard_return >::type Chris@16: make_guard_if(F fun) Chris@16: { Chris@16: return make_guard_if(fun); Chris@16: } Chris@16: Chris@16: template Chris@16: class scope_guard_impl1:public scope_guard_impl_base Chris@16: { Chris@16: public: Chris@16: scope_guard_impl1(F fun,P1 p1):fun_(fun),p1_(p1){} Chris@16: ~scope_guard_impl1(){scope_guard_impl_base::safe_execute(*this);} Chris@16: void execute(){fun_(p1_);} Chris@16: Chris@16: protected: Chris@16: F fun_; Chris@16: const P1 p1_; Chris@16: }; Chris@16: Chris@16: template Chris@16: inline scope_guard_impl1 make_guard(F fun,P1 p1) Chris@16: { Chris@16: return scope_guard_impl1(fun,p1); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename null_guard_return >::type Chris@16: make_guard_if_c(F fun,P1 p1) Chris@16: { Chris@16: return typename null_guard_return >::type(fun,p1); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename null_guard_return >::type Chris@16: make_guard_if(F fun,P1 p1) Chris@16: { Chris@16: return make_guard_if_c(fun,p1); Chris@16: } Chris@16: Chris@16: template Chris@16: class scope_guard_impl2:public scope_guard_impl_base Chris@16: { Chris@16: public: Chris@16: scope_guard_impl2(F fun,P1 p1,P2 p2):fun_(fun),p1_(p1),p2_(p2){} Chris@16: ~scope_guard_impl2(){scope_guard_impl_base::safe_execute(*this);} Chris@16: void execute(){fun_(p1_,p2_);} Chris@16: Chris@16: protected: Chris@16: F fun_; Chris@16: const P1 p1_; Chris@16: const P2 p2_; Chris@16: }; Chris@16: Chris@16: template Chris@16: inline scope_guard_impl2 make_guard(F fun,P1 p1,P2 p2) Chris@16: { Chris@16: return scope_guard_impl2(fun,p1,p2); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename null_guard_return >::type Chris@16: make_guard_if_c(F fun,P1 p1,P2 p2) Chris@16: { Chris@16: return typename null_guard_return >::type(fun,p1,p2); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename null_guard_return >::type Chris@16: make_guard_if(F fun,P1 p1,P2 p2) Chris@16: { Chris@16: return make_guard_if_c(fun,p1,p2); Chris@16: } Chris@16: Chris@16: template Chris@16: class scope_guard_impl3:public scope_guard_impl_base Chris@16: { Chris@16: public: Chris@16: scope_guard_impl3(F fun,P1 p1,P2 p2,P3 p3):fun_(fun),p1_(p1),p2_(p2),p3_(p3){} Chris@16: ~scope_guard_impl3(){scope_guard_impl_base::safe_execute(*this);} Chris@16: void execute(){fun_(p1_,p2_,p3_);} Chris@16: Chris@16: protected: Chris@16: F fun_; Chris@16: const P1 p1_; Chris@16: const P2 p2_; Chris@16: const P3 p3_; Chris@16: }; Chris@16: Chris@16: template Chris@16: inline scope_guard_impl3 make_guard(F fun,P1 p1,P2 p2,P3 p3) Chris@16: { Chris@16: return scope_guard_impl3(fun,p1,p2,p3); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename null_guard_return >::type Chris@16: make_guard_if_c(F fun,P1 p1,P2 p2,P3 p3) Chris@16: { Chris@16: return typename null_guard_return >::type(fun,p1,p2,p3); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename null_guard_return< C::value,scope_guard_impl3 >::type Chris@16: make_guard_if(F fun,P1 p1,P2 p2,P3 p3) Chris@16: { Chris@16: return make_guard_if_c(fun,p1,p2,p3); Chris@16: } Chris@16: Chris@16: template Chris@16: class scope_guard_impl4:public scope_guard_impl_base Chris@16: { Chris@16: public: Chris@16: scope_guard_impl4(F fun,P1 p1,P2 p2,P3 p3,P4 p4): Chris@16: fun_(fun),p1_(p1),p2_(p2),p3_(p3),p4_(p4){} Chris@16: ~scope_guard_impl4(){scope_guard_impl_base::safe_execute(*this);} Chris@16: void execute(){fun_(p1_,p2_,p3_,p4_);} Chris@16: Chris@16: protected: Chris@16: F fun_; Chris@16: const P1 p1_; Chris@16: const P2 p2_; Chris@16: const P3 p3_; Chris@16: const P4 p4_; Chris@16: }; Chris@16: Chris@16: template Chris@16: inline scope_guard_impl4 make_guard( Chris@16: F fun,P1 p1,P2 p2,P3 p3,P4 p4) Chris@16: { Chris@16: return scope_guard_impl4(fun,p1,p2,p3,p4); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename null_guard_return >::type Chris@16: make_guard_if_c( Chris@16: F fun,P1 p1,P2 p2,P3 p3,P4 p4) Chris@16: { Chris@16: return typename null_guard_return >::type(fun,p1,p2,p3,p4); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename null_guard_return >::type Chris@16: make_guard_if( Chris@16: F fun,P1 p1,P2 p2,P3 p3,P4 p4) Chris@16: { Chris@16: return make_guard_if_c(fun,p1,p2,p3,p4); Chris@16: } Chris@16: Chris@16: template Chris@16: class obj_scope_guard_impl0:public scope_guard_impl_base Chris@16: { Chris@16: public: Chris@16: obj_scope_guard_impl0(Obj& obj,MemFun mem_fun):obj_(obj),mem_fun_(mem_fun){} Chris@16: ~obj_scope_guard_impl0(){scope_guard_impl_base::safe_execute(*this);} Chris@16: void execute(){(obj_.*mem_fun_)();} Chris@16: Chris@16: protected: Chris@16: Obj& obj_; Chris@16: MemFun mem_fun_; Chris@16: }; Chris@16: Chris@16: template Chris@16: inline obj_scope_guard_impl0 make_obj_guard(Obj& obj,MemFun mem_fun) Chris@16: { Chris@16: return obj_scope_guard_impl0(obj,mem_fun); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename null_guard_return >::type Chris@16: make_obj_guard_if_c(Obj& obj,MemFun mem_fun) Chris@16: { Chris@16: return typename null_guard_return >::type(obj,mem_fun); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename null_guard_return >::type Chris@16: make_obj_guard_if(Obj& obj,MemFun mem_fun) Chris@16: { Chris@16: return make_obj_guard_if_c(obj,mem_fun); Chris@16: } Chris@16: Chris@16: template Chris@16: class obj_scope_guard_impl1:public scope_guard_impl_base Chris@16: { Chris@16: public: Chris@16: obj_scope_guard_impl1(Obj& obj,MemFun mem_fun,P1 p1): Chris@16: obj_(obj),mem_fun_(mem_fun),p1_(p1){} Chris@16: ~obj_scope_guard_impl1(){scope_guard_impl_base::safe_execute(*this);} Chris@16: void execute(){(obj_.*mem_fun_)(p1_);} Chris@16: Chris@16: protected: Chris@16: Obj& obj_; Chris@16: MemFun mem_fun_; Chris@16: const P1 p1_; Chris@16: }; Chris@16: Chris@16: template Chris@16: inline obj_scope_guard_impl1 make_obj_guard( Chris@16: Obj& obj,MemFun mem_fun,P1 p1) Chris@16: { Chris@16: return obj_scope_guard_impl1(obj,mem_fun,p1); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename null_guard_return >::type Chris@16: make_obj_guard_if_c( Obj& obj,MemFun mem_fun,P1 p1) Chris@16: { Chris@16: return typename null_guard_return >::type(obj,mem_fun,p1); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename null_guard_return >::type Chris@16: make_obj_guard_if( Obj& obj,MemFun mem_fun,P1 p1) Chris@16: { Chris@16: return make_obj_guard_if_c(obj,mem_fun,p1); Chris@16: } Chris@16: Chris@16: template Chris@16: class obj_scope_guard_impl2:public scope_guard_impl_base Chris@16: { Chris@16: public: Chris@16: obj_scope_guard_impl2(Obj& obj,MemFun mem_fun,P1 p1,P2 p2): Chris@16: obj_(obj),mem_fun_(mem_fun),p1_(p1),p2_(p2) Chris@16: {} Chris@16: ~obj_scope_guard_impl2(){scope_guard_impl_base::safe_execute(*this);} Chris@16: void execute(){(obj_.*mem_fun_)(p1_,p2_);} Chris@16: Chris@16: protected: Chris@16: Obj& obj_; Chris@16: MemFun mem_fun_; Chris@16: const P1 p1_; Chris@16: const P2 p2_; Chris@16: }; Chris@16: Chris@16: template Chris@16: inline obj_scope_guard_impl2 Chris@16: make_obj_guard(Obj& obj,MemFun mem_fun,P1 p1,P2 p2) Chris@16: { Chris@16: return obj_scope_guard_impl2(obj,mem_fun,p1,p2); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename null_guard_return >::type Chris@16: make_obj_guard_if_c(Obj& obj,MemFun mem_fun,P1 p1,P2 p2) Chris@16: { Chris@16: return typename null_guard_return >::type(obj,mem_fun,p1,p2); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename null_guard_return >::type Chris@16: make_obj_guard_if(Obj& obj,MemFun mem_fun,P1 p1,P2 p2) Chris@16: { Chris@16: return make_obj_guard_if_c(obj,mem_fun,p1,p2); Chris@16: } Chris@16: Chris@16: template Chris@16: class obj_scope_guard_impl3:public scope_guard_impl_base Chris@16: { Chris@16: public: Chris@16: obj_scope_guard_impl3(Obj& obj,MemFun mem_fun,P1 p1,P2 p2,P3 p3): Chris@16: obj_(obj),mem_fun_(mem_fun),p1_(p1),p2_(p2),p3_(p3) Chris@16: {} Chris@16: ~obj_scope_guard_impl3(){scope_guard_impl_base::safe_execute(*this);} Chris@16: void execute(){(obj_.*mem_fun_)(p1_,p2_,p3_);} Chris@16: Chris@16: protected: Chris@16: Obj& obj_; Chris@16: MemFun mem_fun_; Chris@16: const P1 p1_; Chris@16: const P2 p2_; Chris@16: const P3 p3_; Chris@16: }; Chris@16: Chris@16: template Chris@16: inline obj_scope_guard_impl3 Chris@16: make_obj_guard(Obj& obj,MemFun mem_fun,P1 p1,P2 p2,P3 p3) Chris@16: { Chris@16: return obj_scope_guard_impl3(obj,mem_fun,p1,p2,p3); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename null_guard_return >::type Chris@16: make_obj_guard_if_c(Obj& obj,MemFun mem_fun,P1 p1,P2 p2,P3 p3) Chris@16: { Chris@16: return typename null_guard_return >::type(obj,mem_fun,p1,p2,p3); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename null_guard_return >::type Chris@16: make_obj_guard_if(Obj& obj,MemFun mem_fun,P1 p1,P2 p2,P3 p3) Chris@16: { Chris@16: return make_obj_guard_if_c(obj,mem_fun,p1,p2,p3); Chris@16: } Chris@16: Chris@16: } /* namespace multi_index::detail */ Chris@16: Chris@16: } /* namespace multi_index */ Chris@16: Chris@16: } /* namespace boost */ Chris@16: Chris@16: #endif