mirror of
https://github.com/mat007/turtle.git
synced 2026-06-22 12:13:43 +00:00
Refactoring
git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@168 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
parent
0cc0d83d1f
commit
542984c2b6
2 changed files with 9 additions and 37 deletions
|
|
@ -10,9 +10,8 @@
|
||||||
#define MOCK_FORMAT_HPP_INCLUDED
|
#define MOCK_FORMAT_HPP_INCLUDED
|
||||||
|
|
||||||
#include <boost/type_traits.hpp>
|
#include <boost/type_traits.hpp>
|
||||||
#include <boost/static_assert.hpp>
|
|
||||||
#include <boost/utility/enable_if.hpp>
|
#include <boost/utility/enable_if.hpp>
|
||||||
#include <boost/type_traits/integral_constant.hpp>
|
#include <boost/exception/detail/is_output_streamable.hpp>
|
||||||
#ifndef MOCK_NO_STL_FORMAT
|
#ifndef MOCK_NO_STL_FORMAT
|
||||||
#include <boost/detail/container_fwd.hpp>
|
#include <boost/detail/container_fwd.hpp>
|
||||||
#endif // MOCK_NO_STL_FORMAT
|
#endif // MOCK_NO_STL_FORMAT
|
||||||
|
|
@ -23,55 +22,28 @@ namespace mock
|
||||||
{
|
{
|
||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
namespace protect
|
|
||||||
{
|
|
||||||
struct eater
|
|
||||||
{
|
|
||||||
template< typename T >
|
|
||||||
eater( const T& ) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct eaten
|
|
||||||
{};
|
|
||||||
|
|
||||||
template< typename S >
|
|
||||||
eaten operator<<( S&, const eater& );
|
|
||||||
|
|
||||||
template< typename T >
|
|
||||||
struct is_serializable_impl
|
|
||||||
{
|
|
||||||
static std::ostream* s;
|
|
||||||
static T* t;
|
|
||||||
enum { value = sizeof( *s << *t ) == sizeof( std::ostream& ) };
|
|
||||||
};
|
|
||||||
|
|
||||||
template< typename T >
|
|
||||||
struct is_serializable : boost::integral_constant< bool, is_serializable_impl< T >::value >
|
|
||||||
{};
|
|
||||||
|
|
||||||
template< typename T >
|
template< typename T >
|
||||||
std::string serialize( const T& t,
|
std::string serialize( const T& t,
|
||||||
BOOST_DEDUCED_TYPENAME boost::enable_if<
|
BOOST_DEDUCED_TYPENAME boost::enable_if<
|
||||||
BOOST_DEDUCED_TYPENAME protect::is_serializable< T > >::type* = 0 )
|
BOOST_DEDUCED_TYPENAME boost::is_output_streamable< T > >::type* = 0 )
|
||||||
{
|
{
|
||||||
std::stringstream s;
|
std::stringstream s;
|
||||||
static_cast< std::ostream& >( s ) << std::boolalpha << t;
|
s << std::boolalpha << t;
|
||||||
return s.str();
|
return s.str();
|
||||||
}
|
}
|
||||||
template< typename T >
|
template< typename T >
|
||||||
std::string serialize( const T&,
|
std::string serialize( const T&,
|
||||||
BOOST_DEDUCED_TYPENAME boost::disable_if<
|
BOOST_DEDUCED_TYPENAME boost::disable_if<
|
||||||
BOOST_DEDUCED_TYPENAME protect::is_serializable< T > >::type* = 0 )
|
BOOST_DEDUCED_TYPENAME boost::is_output_streamable< T > >::type* = 0 )
|
||||||
{
|
{
|
||||||
return "?";
|
return "?";
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template< typename T >
|
template< typename T >
|
||||||
std::string format( const T& t )
|
std::string format( const T& t )
|
||||||
{
|
{
|
||||||
return detail::protect::serialize( t );
|
return detail::serialize( t );
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::string format( const char* s )
|
inline std::string format( const char* s )
|
||||||
|
|
|
||||||
|
|
@ -57,9 +57,9 @@ namespace
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE( custom_type_convertible_to_base_type_yields_its_value_when_serialized )
|
BOOST_AUTO_TEST_CASE( custom_type_convertible_to_base_type_yields_an_interrogation_mark_when_serialized )
|
||||||
{
|
{
|
||||||
BOOST_CHECK_EQUAL( "12", mock::format( convertible_to_int() ) );
|
BOOST_CHECK_EQUAL( "?", mock::format( convertible_to_int() ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
|
|
@ -77,9 +77,9 @@ namespace
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE( custom_type_convertible_to_another_type_serializable_in_standard_stream_yields_its_value_when_serialized )
|
BOOST_AUTO_TEST_CASE( custom_type_convertible_to_another_type_serializable_in_standard_stream_yields_an_interrogation_mark_when_serialized )
|
||||||
{
|
{
|
||||||
BOOST_CHECK_EQUAL( "serializable", mock::format( convertible_to_serializable() ) );
|
BOOST_CHECK_EQUAL( "?", mock::format( convertible_to_serializable() ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE( booleans_are_serialized_as_booleans )
|
BOOST_AUTO_TEST_CASE( booleans_are_serialized_as_booleans )
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue