From bfe10df38aa9a07a4eceb460f13780d33be9dd62 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 6 Jul 2006 13:47:26 +0000 Subject: [PATCH] is_placeholder, is_bind_expression added. [SVN r34468] --- include/boost/bind.hpp | 38 ++++++++++++++++ include/boost/bind/arg.hpp | 30 +++++++++++- include/boost/is_placeholder.hpp | 31 +++++++++++++ test/Jamfile | 1 + test/Jamfile.v2 | 1 + test/bind_placeholder_test.cpp | 78 ++++++++++++++++++++++++++++++++ 6 files changed, 177 insertions(+), 2 deletions(-) create mode 100644 include/boost/is_placeholder.hpp create mode 100644 test/bind_placeholder_test.cpp diff --git a/include/boost/bind.hpp b/include/boost/bind.hpp index bece8a7..0ecb8bc 100644 --- a/include/boost/bind.hpp +++ b/include/boost/bind.hpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -933,11 +934,32 @@ namespace _bi #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) || (__SUNPRO_CC >= 0x530) +#if 0 + template struct add_value { typedef _bi::value type; }; +#else + +template< class T, int I > struct add_value_2 +{ + typedef boost::arg type; +}; + +template< class T > struct add_value_2< T, 0 > +{ + typedef _bi::value< T > type; +}; + +template struct add_value +{ + typedef typename add_value_2< T, boost::is_placeholder< T >::value >::type type; +}; + +#endif + template struct add_value< value > { typedef _bi::value type; @@ -1194,6 +1216,22 @@ template void visit_each( V & v, _bi::bind_t #endif +// is_bind_expression + +template< class T > struct is_bind_expression +{ + enum _vt { value = 0 }; +}; + +#if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) + +template< class R, class F, class L > struct is_bind_expression< _bi::bind_t< R, F, L > > +{ + enum _vt { value = 1 }; +}; + +#endif + // bind #ifndef BOOST_BIND diff --git a/include/boost/bind/arg.hpp b/include/boost/bind/arg.hpp index 90e966e..0d5cd03 100644 --- a/include/boost/bind/arg.hpp +++ b/include/boost/bind/arg.hpp @@ -19,18 +19,44 @@ // See http://www.boost.org/libs/bind/bind.html for documentation. // +#include +#include + namespace boost { -template class arg +template< int I > struct arg { + arg() + { + } + + template< class T > arg( T const & /* t */ ) + { + // static assert I == is_placeholder::value + typedef char T_must_be_placeholder[ I == is_placeholder::value? 1: -1 ]; + } }; -template bool operator==(arg const &, arg const &) +template< int I > bool operator==( arg const &, arg const & ) { return true; } +#if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) + +template< int I > struct is_placeholder< arg > +{ + enum _vt { value = I }; +}; + +template< int I > struct is_placeholder< arg (*) () > +{ + enum _vt { value = I }; +}; + +#endif + } // namespace boost #endif // #ifndef BOOST_BIND_ARG_HPP_INCLUDED diff --git a/include/boost/is_placeholder.hpp b/include/boost/is_placeholder.hpp new file mode 100644 index 0000000..5f1b544 --- /dev/null +++ b/include/boost/is_placeholder.hpp @@ -0,0 +1,31 @@ +#ifndef BOOST_IS_PLACEHOLDER_HPP_INCLUDED +#define BOOST_IS_PLACEHOLDER_HPP_INCLUDED + +// MS compatible compilers support #pragma once + +#if defined( _MSC_VER ) && ( _MSC_VER >= 1020 ) +# pragma once +#endif + + +// is_placeholder.hpp - TR1 is_placeholder metafunction +// +// Copyright (c) 2006 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 + + +namespace boost +{ + +template< class T > struct is_placeholder +{ + enum _vt { value = 0 }; +}; + +} // namespace boost + +#endif // #ifndef BOOST_IS_PLACEHOLDER_HPP_INCLUDED diff --git a/test/Jamfile b/test/Jamfile index 9cb627c..39bfe7b 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -32,6 +32,7 @@ DEPENDS all : bind ; [ compile bind_unary_addr.cpp ] [ run bind_dm3_test.cpp ] [ run bind_visit_test.cpp ] + [ run bind_placeholder_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 170166b..8b15a58 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -26,6 +26,7 @@ test-suite "bind" [ compile bind_unary_addr.cpp ] [ run bind_dm3_test.cpp ] [ run bind_visit_test.cpp ] + [ run bind_placeholder_test.cpp ] [ run mem_fn_test.cpp ] [ run mem_fn_void_test.cpp ] [ run mem_fn_derived_test.cpp ] diff --git a/test/bind_placeholder_test.cpp b/test/bind_placeholder_test.cpp new file mode 100644 index 0000000..90ba2dd --- /dev/null +++ b/test/bind_placeholder_test.cpp @@ -0,0 +1,78 @@ +#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_placeholder_test.cpp - test custom placeholders +// +// Copyright (c) 2006 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 + +// + +long f( long a, long b, long c, long d, long e, long f, long g, long h, long i ) +{ + return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h + 100000000 * i; +} + +template< int I > struct custom_placeholder +{ +}; + +template< int I > struct boost::is_placeholder< custom_placeholder< I > > +{ + enum { value = I }; +}; + +int main() +{ + int const x1 = 1; + int const x2 = 2; + int const x3 = 3; + int const x4 = 4; + int const x5 = 5; + int const x6 = 6; + int const x7 = 7; + int const x8 = 8; + int const x9 = 9; + + custom_placeholder<1> p1; + custom_placeholder<2> p2; + custom_placeholder<3> p3; + custom_placeholder<4> p4; + custom_placeholder<5> p5; + custom_placeholder<6> p6; + custom_placeholder<7> p7; + custom_placeholder<8> p8; + custom_placeholder<9> p9; + + BOOST_TEST( + boost::bind( f, p1, p2, p3, p4, p5, p6, p7, p8, p9 ) + ( x1, x2, x3, x4, x5, x6, x7, x8, x9 ) == 987654321L ); + + return boost::report_errors(); +}