From ebb7d18ce6dadd190a07d59c2b678d7bee4b19d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20J=C3=B8rgen=20Ottosen?= Date: Mon, 7 Mar 2011 21:50:12 +0000 Subject: [PATCH] update with make_guard_if and varients [SVN r69632] --- .../boost/multi_index/detail/scope_guard.hpp | 169 ++++++++++++++++++ 1 file changed, 169 insertions(+) diff --git a/include/boost/multi_index/detail/scope_guard.hpp b/include/boost/multi_index/detail/scope_guard.hpp index caaf333..5f82165 100644 --- a/include/boost/multi_index/detail/scope_guard.hpp +++ b/include/boost/multi_index/detail/scope_guard.hpp @@ -42,6 +42,18 @@ namespace detail{ * * NB: CodeWarrior Pro 8 seems to have problems looking up safe_execute * without an explicit qualification. + * + * We also define the following variants of the idiom: + * + * - make_guard_if_c( ... ) + * - make_guard_if( ... ) + * - make_obj_guard_if_c( ... ) + * - make_obj_guard_if( ... ) + * which may be used with a compile-time constant to yield + * a "null_guard" if the boolean compile-time parameter is false, + * or conversely, the guard is only constructed if the constant is true. + * This is useful to avoid extra tagging, because the returned + * null_guard can be optimzed comlpetely away by the compiler. */ class scope_guard_impl_base @@ -81,6 +93,35 @@ private: typedef const scope_guard_impl_base& scope_guard; +struct null_guard : public scope_guard_impl_base +{ + template< class T1 > + null_guard( const T1& ) + { } + + template< class T1, class T2 > + null_guard( const T1&, const T2& ) + { } + + template< class T1, class T2, class T3 > + null_guard( const T1&, const T2&, const T3& ) + { } + + template< class T1, class T2, class T3, class T4 > + null_guard( const T1&, const T2&, const T3&, const T4& ) + { } + + template< class T1, class T2, class T3, class T4, class T5 > + null_guard( const T1&, const T2&, const T3&, const T4&, const T5& ) + { } +}; + +template< bool cond, class T > +struct null_guard_return +{ + typedef typename boost::mpl::if_c::type type; +}; + template class scope_guard_impl0:public scope_guard_impl_base { @@ -100,6 +141,20 @@ inline scope_guard_impl0 make_guard(F fun) return scope_guard_impl0(fun); } +template +inline typename null_guard_return >::type +make_guard_if_c(F fun) +{ + return typename null_guard_return >::type(fun); +} + +template +inline typename null_guard_return >::type +make_guard_if(F fun) +{ + return make_guard_if(fun); +} + template class scope_guard_impl1:public scope_guard_impl_base { @@ -119,6 +174,20 @@ inline scope_guard_impl1 make_guard(F fun,P1 p1) return scope_guard_impl1(fun,p1); } +template +inline typename null_guard_return >::type +make_guard_if_c(F fun,P1 p1) +{ + return typename null_guard_return >::type(fun,p1); +} + +template +inline typename null_guard_return >::type +make_guard_if(F fun,P1 p1) +{ + return make_guard_if_c(fun,p1); +} + template class scope_guard_impl2:public scope_guard_impl_base { @@ -139,6 +208,20 @@ inline scope_guard_impl2 make_guard(F fun,P1 p1,P2 p2) return scope_guard_impl2(fun,p1,p2); } +template +inline typename null_guard_return >::type +make_guard_if_c(F fun,P1 p1,P2 p2) +{ + return typename null_guard_return >::type(fun,p1,p2); +} + +template +inline typename null_guard_return >::type +make_guard_if(F fun,P1 p1,P2 p2) +{ + return make_guard_if_c(fun,p1,p2); +} + template class scope_guard_impl3:public scope_guard_impl_base { @@ -160,6 +243,20 @@ inline scope_guard_impl3 make_guard(F fun,P1 p1,P2 p2,P3 p3) return scope_guard_impl3(fun,p1,p2,p3); } +template +inline typename null_guard_return >::type +make_guard_if_c(F fun,P1 p1,P2 p2,P3 p3) +{ + return typename null_guard_return >::type(fun,p1,p2,p3); +} + +template +inline typename null_guard_return< C::value,scope_guard_impl3 >::type +make_guard_if(F fun,P1 p1,P2 p2,P3 p3) +{ + return make_guard_if_c(fun,p1,p2,p3); +} + template class scope_guard_impl4:public scope_guard_impl_base { @@ -184,6 +281,22 @@ inline scope_guard_impl4 make_guard( return scope_guard_impl4(fun,p1,p2,p3,p4); } +template +inline typename null_guard_return >::type +make_guard_if_c( + F fun,P1 p1,P2 p2,P3 p3,P4 p4) +{ + return typename null_guard_return >::type(fun,p1,p2,p3,p4); +} + +template +inline typename null_guard_return >::type +make_guard_if( + F fun,P1 p1,P2 p2,P3 p3,P4 p4) +{ + return make_guard_if_c(fun,p1,p2,p3,p4); +} + template class obj_scope_guard_impl0:public scope_guard_impl_base { @@ -203,6 +316,20 @@ inline obj_scope_guard_impl0 make_obj_guard(Obj& obj,MemFun mem_fun) return obj_scope_guard_impl0(obj,mem_fun); } +template +inline typename null_guard_return >::type +make_obj_guard_if_c(Obj& obj,MemFun mem_fun) +{ + return typename null_guard_return >::type(obj,mem_fun); +} + +template +inline typename null_guard_return >::type +make_obj_guard_if(Obj& obj,MemFun mem_fun) +{ + return make_obj_guard_if_c(obj,mem_fun); +} + template class obj_scope_guard_impl1:public scope_guard_impl_base { @@ -225,6 +352,20 @@ inline obj_scope_guard_impl1 make_obj_guard( return obj_scope_guard_impl1(obj,mem_fun,p1); } +template +inline typename null_guard_return >::type +make_obj_guard_if_c( Obj& obj,MemFun mem_fun,P1 p1) +{ + return typename null_guard_return >::type(obj,mem_fun,p1); +} + +template +inline typename null_guard_return >::type +make_obj_guard_if( Obj& obj,MemFun mem_fun,P1 p1) +{ + return make_obj_guard_of_c(obj,mem_fun,p1); +} + template class obj_scope_guard_impl2:public scope_guard_impl_base { @@ -249,6 +390,20 @@ make_obj_guard(Obj& obj,MemFun mem_fun,P1 p1,P2 p2) return obj_scope_guard_impl2(obj,mem_fun,p1,p2); } +template +inline typename null_guard_return >::type +make_obj_guard_if_c(Obj& obj,MemFun mem_fun,P1 p1,P2 p2) +{ + return typename null_guard_return >::type(obj,mem_fun,p1,p2); +} + +template +inline typename null_guard_return >::type +make_obj_guard_if(Obj& obj,MemFun mem_fun,P1 p1,P2 p2) +{ + return make_obj_guard_if_c(obj,mem_fun,p1,p2); +} + template class obj_scope_guard_impl3:public scope_guard_impl_base { @@ -274,6 +429,20 @@ make_obj_guard(Obj& obj,MemFun mem_fun,P1 p1,P2 p2,P3 p3) return obj_scope_guard_impl3(obj,mem_fun,p1,p2,p3); } +template +inline typename null_guard_return >::type +make_obj_guard_if_c(Obj& obj,MemFun mem_fun,P1 p1,P2 p2,P3 p3) +{ + return typename null_guard_return >::type(obj,mem_fun,p1,p2,p3); +} + +template +inline typename null_guard_return >::type +make_obj_guard_if(Obj& obj,MemFun mem_fun,P1 p1,P2 p2,P3 p3) +{ + return make_obj_guard_if_c(obj,mem_fun,p1,p2,p3); +} + } /* namespace multi_index::detail */ } /* namespace multi_index */