From ed817c5b15fd86de2cf353364546ae7fb3e68325 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 15 Mar 2005 14:15:38 +0000 Subject: [PATCH] Relational operators added [SVN r27669] --- include/boost/bind.hpp | 29 +++++++++++++ test/Jamfile | 1 + test/Jamfile.v2 | 1 + test/bind_rel_test.cpp | 97 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 test/bind_rel_test.cpp diff --git a/include/boost/bind.hpp b/include/boost/bind.hpp index f6b65a4..78b315f 100644 --- a/include/boost/bind.hpp +++ b/include/boost/bind.hpp @@ -1149,6 +1149,35 @@ template return bind_t ( logical_not(), list_type(f) ); } +// relational operators + +#define BOOST_BIND_OPERATOR( op, name ) \ +\ +struct name \ +{ \ + template bool operator()(V const & v, W const & w) const { return v op w; } \ +}; \ + \ +template \ + bind_t< bool, name, list2< bind_t, typename add_value::type > > \ + operator op (bind_t const & f, A2 a2) \ +{ \ + typedef typename add_value::type B2; \ + typedef list2< bind_t, B2> list_type; \ + return bind_t ( name(), list_type(f, a2) ); \ +} + +BOOST_BIND_OPERATOR( ==, equal ) +BOOST_BIND_OPERATOR( !=, not_equal ) + +BOOST_BIND_OPERATOR( <, less ) +BOOST_BIND_OPERATOR( <=, less_equal ) + +BOOST_BIND_OPERATOR( >, greater ) +BOOST_BIND_OPERATOR( >=, greater_equal ) + +#undef BOOST_BIND_OPERATOR + } // namespace _bi // visit_each diff --git a/test/Jamfile b/test/Jamfile index 3eda1e1..e76f026 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -24,6 +24,7 @@ DEPENDS all : bind ; [ run bind_cv_test.cpp ] [ run bind_stateful_test.cpp ] [ run bind_not_test.cpp ] + [ run bind_rel_test.cpp ] [ run mem_fn_test.cpp ] [ run mem_fn_void_test.cpp ] [ run mem_fn_derived_test.cpp ] diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 53721a0..ee2a9d7 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -18,6 +18,7 @@ test-suite "bind" [ run bind_cv_test.cpp ] [ run bind_stateful_test.cpp ] [ run bind_not_test.cpp ] + [ run bind_rel_test.cpp ] [ run mem_fn_test.cpp ] [ run mem_fn_void_test.cpp ] [ run mem_fn_derived_test.cpp ] diff --git a/test/bind_rel_test.cpp b/test/bind_rel_test.cpp new file mode 100644 index 0000000..5c50b6f --- /dev/null +++ b/test/bind_rel_test.cpp @@ -0,0 +1,97 @@ +#include + +#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_rel_test.cpp - ==, !=, <, <=, >, >= operators +// +// Copyright (c) 2005 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 + +#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300) +#pragma warning(push, 3) +#endif + +#include + +#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300) +#pragma warning(pop) +#endif + +#include + +int f( int x ) +{ + return x + x; +} + +int g( int x ) +{ + return 2 * x; +} + +int main() +{ + int x = 4; + int y = x + x; + + // bind op value + + BOOST_TEST( ( boost::bind( f, _1 ) == y )( x ) ); + BOOST_TEST( !( ( boost::bind( f, _1 ) != y )( x ) ) ); + + BOOST_TEST( !( ( boost::bind( f, _1 ) < y )( x ) ) ); + BOOST_TEST( ( boost::bind( f, _1 ) < y + 1 )( x ) ); + + BOOST_TEST( !( ( boost::bind( f, _1 ) > y )( x ) ) ); + BOOST_TEST( ( boost::bind( f, _1 ) > y - 1 )( x ) ); + + BOOST_TEST( !( ( boost::bind( f, _1 ) <= y - 1 )( x ) ) ); + BOOST_TEST( ( boost::bind( f, _1 ) <= y )( x ) ); + BOOST_TEST( ( boost::bind( f, _1 ) <= y + 1 )( x ) ); + + BOOST_TEST( !( ( boost::bind( f, _1 ) >= y + 1 )( x ) ) ); + BOOST_TEST( ( boost::bind( f, _1 ) >= y )( x ) ); + BOOST_TEST( ( boost::bind( f, _1 ) >= y - 1 )( x ) ); + + // bind op ref + + BOOST_TEST( ( boost::bind( f, _1 ) == boost::ref( y ) )( x ) ); + BOOST_TEST( !( ( boost::bind( f, _1 ) != boost::ref( y ) )( x ) ) ); + BOOST_TEST( !( ( boost::bind( f, _1 ) < boost::ref( y ) )( x ) ) ); + BOOST_TEST( !( ( boost::bind( f, _1 ) > boost::ref( y ) )( x ) ) ); + BOOST_TEST( ( boost::bind( f, _1 ) <= boost::ref( y ) )( x ) ); + BOOST_TEST( ( boost::bind( f, _1 ) >= boost::ref( y ) )( x ) ); + + // bind op placeholder + + BOOST_TEST( ( boost::bind( f, _1 ) == _2 )( x, y ) ); + BOOST_TEST( !( ( boost::bind( f, _1 ) != _2 )( x, y ) ) ); + BOOST_TEST( !( ( boost::bind( f, _1 ) < _2 )( x, y ) ) ); + BOOST_TEST( !( ( boost::bind( f, _1 ) > _2 )( x, y ) ) ); + BOOST_TEST( ( boost::bind( f, _1 ) <= _2 )( x, y ) ); + BOOST_TEST( ( boost::bind( f, _1 ) >= _2 )( x, y ) ); + + // bind op bind + + // important: bind( f, _1 ) and bind( g, _1 ) have the same type + BOOST_TEST( ( boost::bind( f, _1 ) == boost::bind( g, _1 ) )( x ) ); + BOOST_TEST( !( ( boost::bind( f, _1 ) != boost::bind( g, _1 ) )( x ) ) ); + BOOST_TEST( !( ( boost::bind( f, _1 ) < boost::bind( g, _1 ) )( x ) ) ); + BOOST_TEST( ( boost::bind( f, _1 ) <= boost::bind( g, _1 ) )( x ) ); + BOOST_TEST( !( ( boost::bind( f, _1 ) > boost::bind( g, _1 ) )( x ) ) ); + BOOST_TEST( ( boost::bind( f, _1 ) >= boost::bind( g, _1 ) )( x ) ); + + return boost::report_errors(); +}