From 1c689400a0d0395715c05acc88c4239a3558fa5a Mon Sep 17 00:00:00 2001 From: mat007 Date: Sun, 20 Feb 2011 00:45:58 +0000 Subject: [PATCH] Refactoring to work around g++ 4.3.2 git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@246 860be788-9bd5-4423-9f1e-828f051e677b --- src/libraries/turtle/format.hpp | 124 ++++++++++-------- src/libraries/turtle/is_formattable.hpp | 14 +- src/libraries/turtle/is_functor.hpp | 1 + src/libraries/turtle/is_serializable.hpp | 8 +- src/tests/turtle_test/is_formattable_test.cpp | 22 +--- .../turtle_test/is_serializable_test.cpp | 20 +-- 6 files changed, 104 insertions(+), 85 deletions(-) diff --git a/src/libraries/turtle/format.hpp b/src/libraries/turtle/format.hpp index d73f952..cab7f8f 100644 --- a/src/libraries/turtle/format.hpp +++ b/src/libraries/turtle/format.hpp @@ -13,12 +13,54 @@ #include "is_formattable.hpp" #include #include +#include +#include +#include +#include #include #include #include namespace mock { +namespace detail +{ + template< typename T > + void serialize( std::ostream& s, const T& t, + BOOST_DEDUCED_TYPENAME boost::enable_if< + BOOST_DEDUCED_TYPENAME detail::is_formattable< std::ostream, T > + >::type* = 0 ) + { + format( s, t ); + } + template< typename T > + void serialize( std::ostream& s, const T& t, + BOOST_DEDUCED_TYPENAME boost::enable_if< + boost::mpl::and_< + boost::mpl::not_< + BOOST_DEDUCED_TYPENAME detail::is_formattable< std::ostream, T > + >, + BOOST_DEDUCED_TYPENAME detail::is_serializable< std::ostream, T > + > + >::type* = 0 ) + { + s << t; + } + template< typename T > + void serialize( std::ostream& s, const T&, + BOOST_DEDUCED_TYPENAME boost::disable_if< + boost::mpl::or_< + BOOST_DEDUCED_TYPENAME detail::is_formattable< std::ostream, T >, + BOOST_DEDUCED_TYPENAME detail::is_serializable< std::ostream, T > + > + >::type* = 0 ) + { + s << '?'; + } + + BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(is_container, const_iterator, false) +} + template< typename T > class formatter { @@ -38,42 +80,20 @@ namespace mock const T* t_; }; -namespace detail -{ template< typename T > - void serialize( std::ostream& s, const T& t, - BOOST_DEDUCED_TYPENAME boost::enable_if< - BOOST_DEDUCED_TYPENAME protect::is_serializable< std::ostream, T > >::type* = 0 ) + formatter< T > format( const T& t ) { - s << t; + return formatter< T >( t ); } - template< typename T > - void serialize( std::ostream& s, const T&, - BOOST_DEDUCED_TYPENAME boost::disable_if< - BOOST_DEDUCED_TYPENAME protect::is_serializable< std::ostream, T > >::type* = 0 ) - { - s << "?"; - } -} - template< typename T > - BOOST_DEDUCED_TYPENAME boost::enable_if< - BOOST_DEDUCED_TYPENAME mock::detail::is_formattable< std::ostream, T >, - std::ostream& - >::type - operator<<( std::ostream& s, const formatter< T >& f ) - { - format( s, *f ); - return s; - } template< typename T > BOOST_DEDUCED_TYPENAME boost::disable_if< - BOOST_DEDUCED_TYPENAME mock::detail::is_formattable< std::ostream, T >, + detail::is_container< T >, std::ostream& >::type operator<<( std::ostream& s, const formatter< T >& f ) { - mock::detail::serialize( s, *f ); + detail::serialize( s, *f ); return s; } @@ -90,45 +110,43 @@ namespace detail return s << '"' << *f << '"'; } - template< typename T > - formatter< T > format( const T& t ) + template< typename T1, typename T2 > + std::ostream& operator<<( std::ostream& s, const formatter< std::pair< T1, T2 > >& f ) { - return formatter< T >( t ); + return s << '(' << mock::format( f->first ) + << ',' << mock::format( f->second ) << ')'; } - template< typename Container > - void format( std::ostream& s, const Container& c, - BOOST_DEDUCED_TYPENAME Container::const_iterator* = 0 ) + template< typename T > + BOOST_DEDUCED_TYPENAME boost::enable_if< + boost::function_types::is_callable_builtin< T* >, + std::ostream& + >::type + operator<<( std::ostream& s, const formatter< T* >& ) + { + return s << '?'; + } + + template< typename T > + BOOST_DEDUCED_TYPENAME boost::enable_if< + detail::is_container< T >, + std::ostream& + >::type + operator<<( std::ostream& s, const formatter< T >& f ) { s << '('; - // if an error is generated by the line below it means Container is + // if an error is generated by the line below it means T is // being mismatched for a container because it has a typedef // const_iterator : the easiest solution would be to add a format // function for Container as well. - for( BOOST_DEDUCED_TYPENAME Container::const_iterator it = c.begin(); - it != c.end(); ++it ) + for( BOOST_DEDUCED_TYPENAME T::const_iterator it = f->begin(); + it != f->end(); ++it ) { - if( it != c.begin() ) + if( it != f->begin() ) s << ','; s << mock::format( *it ); } - s << ')'; - } - - template< typename T1, typename T2 > - void format( std::ostream& s, const std::pair< T1, T2 >& p ) - { - s << '(' << mock::format( p.first ) - << ',' << mock::format( p.second ) << ')'; - } - - template< typename T > - void format( std::ostream& s, T, - BOOST_DEDUCED_TYPENAME boost::enable_if< - BOOST_DEDUCED_TYPENAME - boost::function_types::is_callable_builtin< T > >::type* = 0 ) - { - s << '?'; + return s << ')'; } } diff --git a/src/libraries/turtle/is_formattable.hpp b/src/libraries/turtle/is_formattable.hpp index 78162ac..af67f59 100644 --- a/src/libraries/turtle/is_formattable.hpp +++ b/src/libraries/turtle/is_formattable.hpp @@ -11,6 +11,7 @@ #include "yes_no_type.hpp" #include "sink.hpp" +#include #ifdef _MSC_VER #pragma warning( push ) @@ -18,14 +19,16 @@ #endif namespace mock +{ +namespace detail +{ +namespace formattable { template< typename S > detail::yes_type format( S&, detail::sink ); -namespace detail -{ template< typename S, typename T > - struct is_formattable + struct impl { static S* s; static T* t; @@ -34,6 +37,11 @@ namespace detail // solution would be to add a format function for T as well. enum { value = sizeof( yes_type(), format( *s, *t ), yes_type() ) == sizeof( yes_type ) }; }; +} + template< typename S, typename T > + struct is_formattable + : boost::mpl::bool_< formattable::impl< S, T >::value > + {}; } } diff --git a/src/libraries/turtle/is_functor.hpp b/src/libraries/turtle/is_functor.hpp index e8d1d42..2d28de7 100644 --- a/src/libraries/turtle/is_functor.hpp +++ b/src/libraries/turtle/is_functor.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include namespace mock diff --git a/src/libraries/turtle/is_serializable.hpp b/src/libraries/turtle/is_serializable.hpp index 73dd874..f499026 100644 --- a/src/libraries/turtle/is_serializable.hpp +++ b/src/libraries/turtle/is_serializable.hpp @@ -11,6 +11,7 @@ #include "yes_no_type.hpp" #include "sink.hpp" +#include #ifdef _MSC_VER #pragma warning( push ) @@ -21,13 +22,13 @@ namespace mock { namespace detail { -namespace protect +namespace serializable { template< typename S > yes_type operator<<( S&, sink ); template< typename S, typename T > - struct is_serializable + struct impl { static S* s; static T* t; @@ -37,6 +38,9 @@ namespace protect enum { value = sizeof( yes_type(), (*s << *t), yes_type() ) == sizeof( yes_type ) }; }; } + template< typename S, typename T > + struct is_serializable : boost::mpl::bool_< serializable::impl< S, T >::value > + {}; } } diff --git a/src/tests/turtle_test/is_formattable_test.cpp b/src/tests/turtle_test/is_formattable_test.cpp index df3d3f7..724da75 100644 --- a/src/tests/turtle_test/is_formattable_test.cpp +++ b/src/tests/turtle_test/is_formattable_test.cpp @@ -12,14 +12,14 @@ #define BOOST_LIB_NAME boost_unit_test_framework #include -BOOST_STATIC_ASSERT(( ! mock::detail::is_formattable< std::ostream, int >::value )); +BOOST_MPL_ASSERT_NOT(( mock::detail::is_formattable< std::ostream, int > )); namespace { struct non_formattable {}; } -BOOST_STATIC_ASSERT(( ! mock::detail::is_formattable< std::ostream, non_formattable >::value )); +BOOST_MPL_ASSERT_NOT(( mock::detail::is_formattable< std::ostream, non_formattable > )); namespace { @@ -28,19 +28,7 @@ namespace void format( std::ostream&, const formattable& ); } -BOOST_STATIC_ASSERT(( mock::detail::is_formattable< std::ostream, formattable >::value )); - -namespace nm -{ - struct formattable {}; -} - -namespace mock -{ - void format( std::ostream&, nm::formattable ); -} - -BOOST_STATIC_ASSERT(( mock::detail::is_formattable< std::ostream, nm::formattable >::value )); +BOOST_MPL_ASSERT(( mock::detail::is_formattable< std::ostream, formattable > )); namespace { @@ -48,7 +36,7 @@ namespace {}; } -BOOST_STATIC_ASSERT(( mock::detail::is_formattable< std::ostream, derived_from_formattable >::value )); +BOOST_MPL_ASSERT(( mock::detail::is_formattable< std::ostream, derived_from_formattable > )); namespace { @@ -58,4 +46,4 @@ namespace }; } -BOOST_STATIC_ASSERT(( mock::detail::is_formattable< std::ostream, convertible_to_formattable >::value )); +BOOST_MPL_ASSERT(( mock::detail::is_formattable< std::ostream, convertible_to_formattable > )); diff --git a/src/tests/turtle_test/is_serializable_test.cpp b/src/tests/turtle_test/is_serializable_test.cpp index b22f640..dffef36 100644 --- a/src/tests/turtle_test/is_serializable_test.cpp +++ b/src/tests/turtle_test/is_serializable_test.cpp @@ -13,15 +13,15 @@ #define BOOST_LIB_NAME boost_unit_test_framework #include -BOOST_STATIC_ASSERT(( mock::detail::protect::is_serializable< std::ostream, int >::value )); -BOOST_STATIC_ASSERT(( mock::detail::protect::is_serializable< std::ostream, std::string >::value )); +BOOST_MPL_ASSERT(( mock::detail::is_serializable< std::ostream, int > )); +BOOST_MPL_ASSERT(( mock::detail::is_serializable< std::ostream, std::string > )); namespace { struct declared_but_not_defined; } -BOOST_STATIC_ASSERT(( ! mock::detail::protect::is_serializable< std::ostream, declared_but_not_defined >::value )); +BOOST_MPL_ASSERT_NOT(( mock::detail::is_serializable< std::ostream, declared_but_not_defined > )); namespace { @@ -29,7 +29,7 @@ namespace {}; } -BOOST_STATIC_ASSERT(( ! mock::detail::protect::is_serializable< std::ostream, non_serializable >::value )); +BOOST_MPL_ASSERT_NOT(( mock::detail::is_serializable< std::ostream, non_serializable > )); namespace { @@ -39,7 +39,7 @@ namespace std::ostream& operator<<( std::ostream& s, const serializable& ); } -BOOST_STATIC_ASSERT(( mock::detail::protect::is_serializable< std::ostream, serializable >::value )); +BOOST_MPL_ASSERT(( mock::detail::is_serializable< std::ostream, serializable > )); namespace { @@ -50,7 +50,7 @@ namespace std::ostream& operator<<( std::ostream& s, const template_serializable< T >& ); } -BOOST_STATIC_ASSERT(( mock::detail::protect::is_serializable< std::ostream, template_serializable< int > >::value )); +BOOST_MPL_ASSERT(( mock::detail::is_serializable< std::ostream, template_serializable< int > > )); namespace { @@ -60,7 +60,7 @@ namespace }; } -BOOST_STATIC_ASSERT(( mock::detail::protect::is_serializable< std::ostream, convertible_to_base >::value )); +BOOST_MPL_ASSERT(( mock::detail::is_serializable< std::ostream, convertible_to_base > )); namespace { @@ -70,7 +70,7 @@ namespace }; } -BOOST_STATIC_ASSERT(( ! mock::detail::protect::is_serializable< std::ostream, convertible_to_string >::value )); +BOOST_MPL_ASSERT_NOT(( mock::detail::is_serializable< std::ostream, convertible_to_string > )); namespace { @@ -80,7 +80,7 @@ namespace }; } -BOOST_STATIC_ASSERT(( mock::detail::protect::is_serializable< std::ostream, convertible_to_serializable >::value )); +BOOST_MPL_ASSERT(( mock::detail::is_serializable< std::ostream, convertible_to_serializable > )); namespace { @@ -88,4 +88,4 @@ namespace {}; } -BOOST_STATIC_ASSERT(( mock::detail::protect::is_serializable< std::ostream, derived_from_serializable >::value )); +BOOST_MPL_ASSERT(( mock::detail::is_serializable< std::ostream, derived_from_serializable > ));