Boost logo

Boost-Commit :

From: pdimov_at_[hidden]
Date: 2008-02-15 13:40:38


Author: pdimov
Date: 2008-02-15 13:40:36 EST (Fri, 15 Feb 2008)
New Revision: 43269
URL: http://svn.boost.org/trac/boost/changeset/43269

Log:
Added support for &&, ||
Added:
   trunk/libs/bind/test/bind_and_or_test.cpp (contents, props changed)
Text files modified:
   trunk/boost/bind.hpp | 26 ++++++++++++++++++++++++++
   trunk/libs/bind/test/Jamfile.v2 | 1 +
   2 files changed, 27 insertions(+), 0 deletions(-)

Modified: trunk/boost/bind.hpp
==============================================================================
--- trunk/boost/bind.hpp (original)
+++ trunk/boost/bind.hpp 2008-02-15 13:40:36 EST (Fri, 15 Feb 2008)
@@ -248,6 +248,9 @@
     }
 };
 
+struct logical_and;
+struct logical_or;
+
 template< class A1, class A2 > class list2: private storage2< A1, A2 >
 {
 private:
@@ -294,6 +297,26 @@
         unwrapper<F const>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]);
     }
 
+ template<class A> bool operator()( type<bool>, logical_and & /*f*/, A & a, int )
+ {
+ return a[ base_type::a1_ ] && a[ base_type::a2_ ];
+ }
+
+ template<class A> bool operator()( type<bool>, logical_and const & /*f*/, A & a, int ) const
+ {
+ return a[ base_type::a1_ ] && a[ base_type::a2_ ];
+ }
+
+ template<class A> bool operator()( type<bool>, logical_or & /*f*/, A & a, int )
+ {
+ return a[ base_type::a1_ ] || a[ base_type::a2_ ];
+ }
+
+ template<class A> bool operator()( type<bool>, logical_or const & /*f*/, A & a, int ) const
+ {
+ return a[ base_type::a1_ ] || a[ base_type::a2_ ];
+ }
+
     template<class V> void accept(V & v) const
     {
         base_type::accept(v);
@@ -1158,6 +1181,9 @@
 BOOST_BIND_OPERATOR( >, greater )
 BOOST_BIND_OPERATOR( >=, greater_equal )
 
+BOOST_BIND_OPERATOR( &&, logical_and )
+BOOST_BIND_OPERATOR( ||, logical_or )
+
 #undef BOOST_BIND_OPERATOR
 
 #if defined(__GNUC__) && BOOST_WORKAROUND(__GNUC__, < 3)

Modified: trunk/libs/bind/test/Jamfile.v2
==============================================================================
--- trunk/libs/bind/test/Jamfile.v2 (original)
+++ trunk/libs/bind/test/Jamfile.v2 2008-02-15 13:40:36 EST (Fri, 15 Feb 2008)
@@ -27,6 +27,7 @@
       [ run bind_visit_test.cpp ]
       [ run bind_placeholder_test.cpp ]
       [ run bind_rvalue_test.cpp ]
+ [ run bind_and_or_test.cpp ]
       [ run mem_fn_test.cpp ]
       [ run mem_fn_void_test.cpp ]
       [ run mem_fn_derived_test.cpp ]

Added: trunk/libs/bind/test/bind_and_or_test.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/bind/test/bind_and_or_test.cpp 2008-02-15 13:40:36 EST (Fri, 15 Feb 2008)
@@ -0,0 +1,84 @@
+#include <boost/config.hpp>
+
+#if defined(BOOST_MSVC)
+#pragma warning(disable: 4786) // identifier truncated in debug info
+#pragma warning(disable: 4710) // function not inlined
+#pragma warning(disable: 4711) // function selected for automatic inline expansion
+#pragma warning(disable: 4514) // unreferenced inline removed
+#endif
+
+//
+// bind_and_or_test.cpp - &&, || operators
+//
+// Copyright (c) 2008 Peter Dimov
+//
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#include <boost/bind.hpp>
+
+#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
+#pragma warning(push, 3)
+#endif
+
+#include <iostream>
+
+#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
+#pragma warning(pop)
+#endif
+
+#include <boost/detail/lightweight_test.hpp>
+
+bool f( bool x )
+{
+ return x;
+}
+
+bool g( bool x )
+{
+ return !x;
+}
+
+bool h()
+{
+ BOOST_ERROR( "Short-circuit evaluation failure" );
+ return false;
+}
+
+template< class F, class A1, class A2, class R > void test( F f, A1 a1, A2 a2, R r )
+{
+ BOOST_TEST( f( a1, a2 ) == r );
+}
+
+int main()
+{
+ // &&
+
+ test( boost::bind( f, true ) && boost::bind( g, true ), false, false, f( true ) && g( true ) );
+ test( boost::bind( f, true ) && boost::bind( g, false ), false, false, f( true ) && g( false ) );
+
+ test( boost::bind( f, false ) && boost::bind( h ), false, false, f( false ) && h() );
+
+ test( boost::bind( f, _1 ) && boost::bind( g, _2 ), true, true, f( true ) && g( true ) );
+ test( boost::bind( f, _1 ) && boost::bind( g, _2 ), true, false, f( true ) && g( false ) );
+
+ test( boost::bind( f, _1 ) && boost::bind( h ), false, false, f( false ) && h() );
+
+ // ||
+
+ test( boost::bind( f, false ) || boost::bind( g, true ), false, false, f( false ) || g( true ) );
+ test( boost::bind( f, false ) || boost::bind( g, false ), false, false, f( false ) || g( false ) );
+
+ test( boost::bind( f, true ) || boost::bind( h ), false, false, f( true ) || h() );
+
+ test( boost::bind( f, _1 ) || boost::bind( g, _2 ), false, true, f( false ) || g( true ) );
+ test( boost::bind( f, _1 ) || boost::bind( g, _2 ), false, false, f( false ) || g( false ) );
+
+ test( boost::bind( f, _1 ) || boost::bind( h ), true, false, f( true ) || h() );
+
+ //
+
+ return boost::report_errors();
+}


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