From d9072df2946363cd6b44b333e8b4315d70dd86d4 Mon Sep 17 00:00:00 2001 From: mat007 Date: Mon, 17 Jan 2011 23:16:45 +0000 Subject: [PATCH] Added lazy serialization of constraints git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@197 860be788-9bd5-4423-9f1e-828f051e677b --- src/libraries/turtle/check.hpp | 33 +++++++++++++++++++++------------ src/libraries/turtle/lambda.hpp | 2 ++ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/libraries/turtle/check.hpp b/src/libraries/turtle/check.hpp index 0314036..2083e3e 100644 --- a/src/libraries/turtle/check.hpp +++ b/src/libraries/turtle/check.hpp @@ -13,6 +13,7 @@ #include "constraints.hpp" #include "operators.hpp" #include "format.hpp" +#include "lambda.hpp" #include #include #include @@ -30,8 +31,8 @@ namespace detail BOOST_CONCEPT_USAGE( FunctorCompatible ) { boost::require_boolean_expr( - // if an error is generated by the line below it means an argument - // passed to 'with' was of the wrong type. + // if an error is generated by the line below it means + // an argument passed to 'with' was of the wrong type. functor_accepts( actual_argument_type ) ); } @@ -48,8 +49,8 @@ namespace detail BOOST_CONCEPT_USAGE( EqualityComparable ) { boost::require_boolean_expr( - // if an error is generated by the line below it means an argument - // passed to 'with' was of the wrong type. + // if an error is generated by the line below it means + // an argument passed to 'with' was of the wrong type. actual_argument_type == expected_argument_type ); } @@ -68,7 +69,7 @@ namespace detail BOOST_DEDUCED_TYPENAME boost::enable_if< BOOST_DEDUCED_TYPENAME detail::is_functor< Functor > >::type* = 0 ) - : desc_( mock::format( f ) ) + : desc_( bind( &serialize< Functor >, _1, f ) ) { BOOST_CONCEPT_ASSERT(( FunctorCompatible< Functor, Actual > )); f_ = f; @@ -80,18 +81,18 @@ namespace detail BOOST_DEDUCED_TYPENAME boost::disable_if< BOOST_DEDUCED_TYPENAME detail::is_functor< Expected > >::type* = 0 ) - : desc_( mock::format( expected ) ) + : desc_( bind( &serialize< Expected >, _1, expected ) ) { BOOST_CONCEPT_ASSERT(( EqualityComparable< Expected, Actual > )); f_ = mock::equal( expected ).f_; if( ! f_ ) std::invalid_argument( "invalid constraint" ); } - template< typename Functor > - explicit check( const constraint< Functor >& ph ) - : desc_( mock::format( ph.f_ ) ) + template< typename Constraint > + explicit check( const constraint< Constraint >& ph ) + : desc_( bind( &serialize< Constraint >, _1, ph.f_ ) ) { - BOOST_CONCEPT_ASSERT(( FunctorCompatible< Functor, Actual > )); + BOOST_CONCEPT_ASSERT(( FunctorCompatible< Constraint, Actual > )); f_ = ph.f_; if( ! f_ ) std::invalid_argument( "invalid constraint" ); @@ -104,12 +105,20 @@ namespace detail friend std::ostream& operator<<( std::ostream& s, const check& c ) { - return s << c.desc_; + c.desc_( s ); + return s; + } + + private: + template< typename T > + static void serialize( std::ostream& s, T t ) + { + s << mock::format( t ); } private: boost::function< bool( Actual ) > f_; - std::string desc_; + boost::function< void( std::ostream& ) > desc_; }; } } diff --git a/src/libraries/turtle/lambda.hpp b/src/libraries/turtle/lambda.hpp index 641de0a..42fac3d 100644 --- a/src/libraries/turtle/lambda.hpp +++ b/src/libraries/turtle/lambda.hpp @@ -14,6 +14,7 @@ #include #else #include +#include #endif #include @@ -25,6 +26,7 @@ namespace detail using boost::bind; #else using boost::phoenix::bind; + using boost::phoenix::arg_names::_1; #endif template< typename Result, typename Signature >