Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r69632 - trunk/boost/multi_index/detail
From: nesotto_at_[hidden]
Date: 2011-03-07 16:50:13


Author: nesotto
Date: 2011-03-07 16:50:12 EST (Mon, 07 Mar 2011)
New Revision: 69632
URL: http://svn.boost.org/trac/boost/changeset/69632

Log:
update with make_guard_if and varients
Text files modified:
   trunk/boost/multi_index/detail/scope_guard.hpp | 169 ++++++++++++++++++++++++++++++++++++++++
   1 files changed, 169 insertions(+), 0 deletions(-)

Modified: trunk/boost/multi_index/detail/scope_guard.hpp
==============================================================================
--- trunk/boost/multi_index/detail/scope_guard.hpp (original)
+++ trunk/boost/multi_index/detail/scope_guard.hpp 2011-03-07 16:50:12 EST (Mon, 07 Mar 2011)
@@ -42,6 +42,18 @@
  *
  * 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<bool>( ... )
+ * - make_guard_if<IntegralConstant>( ... )
+ * - make_obj_guard_if_c<bool>( ... )
+ * - make_obj_guard_if<IntegralConstant>( ... )
+ * 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 @@
 
 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<cond,T,null_guard>::type type;
+};
+
 template<typename F>
 class scope_guard_impl0:public scope_guard_impl_base
 {
@@ -100,6 +141,20 @@
   return scope_guard_impl0<F>(fun);
 }
 
+template<bool cond, typename F>
+inline typename null_guard_return<cond,scope_guard_impl0<F> >::type
+make_guard_if_c(F fun)
+{
+ return typename null_guard_return<cond,scope_guard_impl0<F> >::type(fun);
+}
+
+template<typename C, typename F>
+inline typename null_guard_return<C::value,scope_guard_impl0<F> >::type
+make_guard_if(F fun)
+{
+ return make_guard_if<C::value>(fun);
+}
+
 template<typename F,typename P1>
 class scope_guard_impl1:public scope_guard_impl_base
 {
@@ -119,6 +174,20 @@
   return scope_guard_impl1<F,P1>(fun,p1);
 }
 
+template<bool cond, typename F,typename P1>
+inline typename null_guard_return<cond,scope_guard_impl1<F,P1> >::type
+make_guard_if_c(F fun,P1 p1)
+{
+ return typename null_guard_return<cond,scope_guard_impl1<F,P1> >::type(fun,p1);
+}
+
+template<typename C, typename F,typename P1>
+inline typename null_guard_return<C::value,scope_guard_impl1<F,P1> >::type
+make_guard_if(F fun,P1 p1)
+{
+ return make_guard_if_c<C::value>(fun,p1);
+}
+
 template<typename F,typename P1,typename P2>
 class scope_guard_impl2:public scope_guard_impl_base
 {
@@ -139,6 +208,20 @@
   return scope_guard_impl2<F,P1,P2>(fun,p1,p2);
 }
 
+template<bool cond, typename F,typename P1,typename P2>
+inline typename null_guard_return<cond,scope_guard_impl2<F,P1,P2> >::type
+make_guard_if_c(F fun,P1 p1,P2 p2)
+{
+ return typename null_guard_return<cond,scope_guard_impl2<F,P1,P2> >::type(fun,p1,p2);
+}
+
+template<typename C, typename F,typename P1,typename P2>
+inline typename null_guard_return<C::value,scope_guard_impl2<F,P1,P2> >::type
+make_guard_if(F fun,P1 p1,P2 p2)
+{
+ return make_guard_if_c<C::value>(fun,p1,p2);
+}
+
 template<typename F,typename P1,typename P2,typename P3>
 class scope_guard_impl3:public scope_guard_impl_base
 {
@@ -160,6 +243,20 @@
   return scope_guard_impl3<F,P1,P2,P3>(fun,p1,p2,p3);
 }
 
+template<bool cond,typename F,typename P1,typename P2,typename P3>
+inline typename null_guard_return<cond,scope_guard_impl3<F,P1,P2,P3> >::type
+make_guard_if_c(F fun,P1 p1,P2 p2,P3 p3)
+{
+ return typename null_guard_return<cond,scope_guard_impl3<F,P1,P2,P3> >::type(fun,p1,p2,p3);
+}
+
+template<typename C,typename F,typename P1,typename P2,typename P3>
+inline typename null_guard_return< C::value,scope_guard_impl3<F,P1,P2,P3> >::type
+make_guard_if(F fun,P1 p1,P2 p2,P3 p3)
+{
+ return make_guard_if_c<C::value>(fun,p1,p2,p3);
+}
+
 template<typename F,typename P1,typename P2,typename P3,typename P4>
 class scope_guard_impl4:public scope_guard_impl_base
 {
@@ -184,6 +281,22 @@
   return scope_guard_impl4<F,P1,P2,P3,P4>(fun,p1,p2,p3,p4);
 }
 
+template<bool cond, typename F,typename P1,typename P2,typename P3,typename P4>
+inline typename null_guard_return<cond,scope_guard_impl4<F,P1,P2,P3,P4> >::type
+make_guard_if_c(
+ F fun,P1 p1,P2 p2,P3 p3,P4 p4)
+{
+ return typename null_guard_return<cond,scope_guard_impl4<F,P1,P2,P3,P4> >::type(fun,p1,p2,p3,p4);
+}
+
+template<typename C, typename F,typename P1,typename P2,typename P3,typename P4>
+inline typename null_guard_return<C::value,scope_guard_impl4<F,P1,P2,P3,P4> >::type
+make_guard_if(
+ F fun,P1 p1,P2 p2,P3 p3,P4 p4)
+{
+ return make_guard_if_c<C::value>(fun,p1,p2,p3,p4);
+}
+
 template<class Obj,typename MemFun>
 class obj_scope_guard_impl0:public scope_guard_impl_base
 {
@@ -203,6 +316,20 @@
   return obj_scope_guard_impl0<Obj,MemFun>(obj,mem_fun);
 }
 
+template<bool cond, class Obj,typename MemFun>
+inline typename null_guard_return<cond,obj_scope_guard_impl0<Obj,MemFun> >::type
+make_obj_guard_if_c(Obj& obj,MemFun mem_fun)
+{
+ return typename null_guard_return<cond,obj_scope_guard_impl0<Obj,MemFun> >::type(obj,mem_fun);
+}
+
+template<typename C, class Obj,typename MemFun>
+inline typename null_guard_return<C::value,obj_scope_guard_impl0<Obj,MemFun> >::type
+make_obj_guard_if(Obj& obj,MemFun mem_fun)
+{
+ return make_obj_guard_if_c<C::value>(obj,mem_fun);
+}
+
 template<class Obj,typename MemFun,typename P1>
 class obj_scope_guard_impl1:public scope_guard_impl_base
 {
@@ -225,6 +352,20 @@
   return obj_scope_guard_impl1<Obj,MemFun,P1>(obj,mem_fun,p1);
 }
 
+template<bool cond, class Obj,typename MemFun,typename P1>
+inline typename null_guard_return<cond,obj_scope_guard_impl1<Obj,MemFun,P1> >::type
+make_obj_guard_if_c( Obj& obj,MemFun mem_fun,P1 p1)
+{
+ return typename null_guard_return<cond,obj_scope_guard_impl1<Obj,MemFun,P1> >::type(obj,mem_fun,p1);
+}
+
+template<typename C, class Obj,typename MemFun,typename P1>
+inline typename null_guard_return<C::value,obj_scope_guard_impl1<Obj,MemFun,P1> >::type
+make_obj_guard_if( Obj& obj,MemFun mem_fun,P1 p1)
+{
+ return make_obj_guard_of_c<C::value>(obj,mem_fun,p1);
+}
+
 template<class Obj,typename MemFun,typename P1,typename P2>
 class obj_scope_guard_impl2:public scope_guard_impl_base
 {
@@ -249,6 +390,20 @@
   return obj_scope_guard_impl2<Obj,MemFun,P1,P2>(obj,mem_fun,p1,p2);
 }
 
+template<bool cond, class Obj,typename MemFun,typename P1,typename P2>
+inline typename null_guard_return<cond,obj_scope_guard_impl2<Obj,MemFun,P1,P2> >::type
+make_obj_guard_if_c(Obj& obj,MemFun mem_fun,P1 p1,P2 p2)
+{
+ return typename null_guard_return<cond,obj_scope_guard_impl2<Obj,MemFun,P1,P2> >::type(obj,mem_fun,p1,p2);
+}
+
+template<typename C, class Obj,typename MemFun,typename P1,typename P2>
+inline typename null_guard_return<C::value,obj_scope_guard_impl2<Obj,MemFun,P1,P2> >::type
+make_obj_guard_if(Obj& obj,MemFun mem_fun,P1 p1,P2 p2)
+{
+ return make_obj_guard_if_c<C::value>(obj,mem_fun,p1,p2);
+}
+
 template<class Obj,typename MemFun,typename P1,typename P2,typename P3>
 class obj_scope_guard_impl3:public scope_guard_impl_base
 {
@@ -274,6 +429,20 @@
   return obj_scope_guard_impl3<Obj,MemFun,P1,P2,P3>(obj,mem_fun,p1,p2,p3);
 }
 
+template<bool cond, class Obj,typename MemFun,typename P1,typename P2,typename P3>
+inline typename null_guard_return<cond,obj_scope_guard_impl3<Obj,MemFun,P1,P2,P3> >::type
+make_obj_guard_if_c(Obj& obj,MemFun mem_fun,P1 p1,P2 p2,P3 p3)
+{
+ return typename null_guard_return<cond,obj_scope_guard_impl3<Obj,MemFun,P1,P2,P3> >::type(obj,mem_fun,p1,p2,p3);
+}
+
+template<typename C, class Obj,typename MemFun,typename P1,typename P2,typename P3>
+inline typename null_guard_return<C::value,obj_scope_guard_impl3<Obj,MemFun,P1,P2,P3> >::type
+make_obj_guard_if(Obj& obj,MemFun mem_fun,P1 p1,P2 p2,P3 p3)
+{
+ return make_obj_guard_if_c<C::value>(obj,mem_fun,p1,p2,p3);
+}
+
 } /* namespace multi_index::detail */
 
 } /* namespace multi_index */


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk