2012-05-08 09:10:49 +02:00
|
|
|
/*
|
|
|
|
* Created by Phil on 8/5/2012.
|
|
|
|
* Copyright 2012 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
*/
|
2014-04-23 08:03:15 +02:00
|
|
|
#ifndef TWOBLUECUBES_CATCH_TOSTRING_H_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_TOSTRING_H_INCLUDED
|
2012-05-08 09:10:49 +02:00
|
|
|
|
|
|
|
#include "catch_common.h"
|
2013-04-16 23:55:31 +02:00
|
|
|
|
2012-05-08 09:10:49 +02:00
|
|
|
#include <sstream>
|
2013-03-04 12:19:15 +01:00
|
|
|
#include <iomanip>
|
|
|
|
#include <limits>
|
2013-11-07 11:35:59 +01:00
|
|
|
#include <vector>
|
2014-04-23 19:19:19 +02:00
|
|
|
#include <cstddef>
|
2012-05-08 09:10:49 +02:00
|
|
|
|
2012-08-01 09:17:07 +02:00
|
|
|
#ifdef __OBJC__
|
|
|
|
#include "catch_objc_arc.hpp"
|
|
|
|
#endif
|
|
|
|
|
2015-05-19 19:37:58 +02:00
|
|
|
#ifdef CATCH_CONFIG_CPP11_TUPLE
|
2014-09-04 01:17:36 +02:00
|
|
|
#include <tuple>
|
2015-05-19 19:37:58 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CATCH_CONFIG_CPP11_IS_ENUM
|
2014-09-04 01:17:36 +02:00
|
|
|
#include <type_traits>
|
|
|
|
#endif
|
|
|
|
|
2012-05-15 08:42:26 +02:00
|
|
|
namespace Catch {
|
2014-09-01 18:35:01 +02:00
|
|
|
|
|
|
|
// Why we're here.
|
|
|
|
template<typename T>
|
|
|
|
std::string toString( T const& value );
|
|
|
|
|
2014-09-04 01:12:25 +02:00
|
|
|
// Built in overloads
|
|
|
|
|
|
|
|
std::string toString( std::string const& value );
|
|
|
|
std::string toString( std::wstring const& value );
|
|
|
|
std::string toString( const char* const value );
|
|
|
|
std::string toString( char* const value );
|
|
|
|
std::string toString( const wchar_t* const value );
|
|
|
|
std::string toString( wchar_t* const value );
|
|
|
|
std::string toString( int value );
|
|
|
|
std::string toString( unsigned long value );
|
|
|
|
std::string toString( unsigned int value );
|
|
|
|
std::string toString( const double value );
|
|
|
|
std::string toString( const float value );
|
|
|
|
std::string toString( bool value );
|
|
|
|
std::string toString( char value );
|
|
|
|
std::string toString( signed char value );
|
|
|
|
std::string toString( unsigned char value );
|
|
|
|
|
2015-07-23 20:03:33 +02:00
|
|
|
#ifdef CATCH_CONFIG_CPP11_LONG_LONG
|
|
|
|
std::string toString( long long value );
|
|
|
|
std::string toString( unsigned long long value );
|
|
|
|
#endif
|
|
|
|
|
2014-09-04 01:12:25 +02:00
|
|
|
#ifdef CATCH_CONFIG_CPP11_NULLPTR
|
|
|
|
std::string toString( std::nullptr_t );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __OBJC__
|
|
|
|
std::string toString( NSString const * const& nsstring );
|
2017-03-14 19:54:14 +01:00
|
|
|
std::string toString( NSString * CATCH_ARC_STRONG & nsstring );
|
2014-09-04 01:12:25 +02:00
|
|
|
std::string toString( NSObject* const& nsObject );
|
|
|
|
#endif
|
|
|
|
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2013-04-21 00:12:17 +02:00
|
|
|
namespace Detail {
|
2013-04-16 23:55:31 +02:00
|
|
|
|
2015-07-23 20:03:33 +02:00
|
|
|
extern const std::string unprintableString;
|
2014-09-08 09:14:59 +02:00
|
|
|
|
2017-04-05 09:53:10 +02:00
|
|
|
#if !defined(CATCH_CONFIG_CPP11_STREAM_INSERTABLE_CHECK)
|
2013-04-21 00:12:17 +02:00
|
|
|
struct BorgType {
|
|
|
|
template<typename T> BorgType( T const& );
|
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2015-06-30 09:41:55 +02:00
|
|
|
struct TrueType { char sizer[1]; };
|
|
|
|
struct FalseType { char sizer[2]; };
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2013-04-21 00:12:17 +02:00
|
|
|
TrueType& testStreamable( std::ostream& );
|
|
|
|
FalseType testStreamable( FalseType );
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-04-21 00:12:17 +02:00
|
|
|
FalseType operator<<( std::ostream const&, BorgType const& );
|
2013-04-16 23:55:31 +02:00
|
|
|
|
2013-04-21 00:12:17 +02:00
|
|
|
template<typename T>
|
|
|
|
struct IsStreamInsertable {
|
|
|
|
static std::ostream &s;
|
2013-04-23 19:58:56 +02:00
|
|
|
static T const&t;
|
2013-04-21 00:12:17 +02:00
|
|
|
enum { value = sizeof( testStreamable(s << t) ) == sizeof( TrueType ) };
|
|
|
|
};
|
2017-04-05 09:53:10 +02:00
|
|
|
#else
|
|
|
|
template<typename T>
|
|
|
|
class IsStreamInsertable {
|
|
|
|
template<typename SS, typename TT>
|
|
|
|
static auto test(int)
|
|
|
|
-> decltype( std::declval<SS&>() << std::declval<TT>(), std::true_type() );
|
|
|
|
|
|
|
|
template<typename, typename>
|
|
|
|
static auto test(...) -> std::false_type;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static const bool value = decltype(test<std::ostream,const T&>(0))::value;
|
|
|
|
};
|
|
|
|
#endif
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2015-05-19 19:37:58 +02:00
|
|
|
#if defined(CATCH_CONFIG_CPP11_IS_ENUM)
|
2014-09-01 18:35:01 +02:00
|
|
|
template<typename T,
|
2014-09-08 09:14:59 +02:00
|
|
|
bool IsEnum = std::is_enum<T>::value
|
2014-09-01 18:35:01 +02:00
|
|
|
>
|
|
|
|
struct EnumStringMaker
|
|
|
|
{
|
2014-09-08 09:14:59 +02:00
|
|
|
static std::string convert( T const& ) { return unprintableString; }
|
2014-09-01 18:35:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct EnumStringMaker<T,true>
|
|
|
|
{
|
|
|
|
static std::string convert( T const& v )
|
|
|
|
{
|
|
|
|
return ::Catch::toString(
|
|
|
|
static_cast<typename std::underlying_type<T>::type>(v)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
2013-04-21 00:12:17 +02:00
|
|
|
template<bool C>
|
|
|
|
struct StringMakerBase {
|
2015-05-19 19:37:58 +02:00
|
|
|
#if defined(CATCH_CONFIG_CPP11_IS_ENUM)
|
2014-09-01 18:35:01 +02:00
|
|
|
template<typename T>
|
|
|
|
static std::string convert( T const& v )
|
|
|
|
{
|
|
|
|
return EnumStringMaker<T>::convert( v );
|
|
|
|
}
|
|
|
|
#else
|
2013-04-21 00:12:17 +02:00
|
|
|
template<typename T>
|
2014-09-08 09:14:59 +02:00
|
|
|
static std::string convert( T const& ) { return unprintableString; }
|
2014-09-01 18:35:01 +02:00
|
|
|
#endif
|
2013-04-21 00:12:17 +02:00
|
|
|
};
|
2012-05-15 08:42:26 +02:00
|
|
|
|
2013-04-21 00:12:17 +02:00
|
|
|
template<>
|
|
|
|
struct StringMakerBase<true> {
|
|
|
|
template<typename T>
|
|
|
|
static std::string convert( T const& _value ) {
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << _value;
|
|
|
|
return oss.str();
|
|
|
|
}
|
2012-05-08 09:10:49 +02:00
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2014-05-23 19:41:02 +02:00
|
|
|
std::string rawMemoryToString( const void *object, std::size_t size );
|
2014-04-22 09:19:11 +02:00
|
|
|
|
2014-01-07 18:25:27 +01:00
|
|
|
template<typename T>
|
2014-05-23 19:41:02 +02:00
|
|
|
inline std::string rawMemoryToString( const T& object ) {
|
|
|
|
return rawMemoryToString( &object, sizeof(object) );
|
2014-01-07 18:25:27 +01:00
|
|
|
}
|
|
|
|
|
2013-03-04 12:19:15 +01:00
|
|
|
} // end namespace Detail
|
|
|
|
|
|
|
|
template<typename T>
|
2013-04-21 00:12:17 +02:00
|
|
|
struct StringMaker :
|
|
|
|
Detail::StringMakerBase<Detail::IsStreamInsertable<T>::value> {};
|
2013-04-16 23:55:31 +02:00
|
|
|
|
2013-03-04 12:19:15 +01:00
|
|
|
template<typename T>
|
|
|
|
struct StringMaker<T*> {
|
2013-07-03 09:25:11 +02:00
|
|
|
template<typename U>
|
|
|
|
static std::string convert( U* p ) {
|
2012-05-08 09:10:49 +02:00
|
|
|
if( !p )
|
2015-07-01 08:33:27 +02:00
|
|
|
return "NULL";
|
2014-01-07 18:25:27 +01:00
|
|
|
else
|
|
|
|
return Detail::rawMemoryToString( p );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename R, typename C>
|
|
|
|
struct StringMaker<R C::*> {
|
|
|
|
static std::string convert( R C::* p ) {
|
|
|
|
if( !p )
|
2015-07-01 08:33:27 +02:00
|
|
|
return "NULL";
|
2014-01-07 18:25:27 +01:00
|
|
|
else
|
|
|
|
return Detail::rawMemoryToString( p );
|
2013-03-04 12:19:15 +01:00
|
|
|
}
|
|
|
|
};
|
2012-05-08 09:10:49 +02:00
|
|
|
|
2013-09-17 23:22:47 +02:00
|
|
|
namespace Detail {
|
|
|
|
template<typename InputIterator>
|
2013-09-21 19:45:42 +02:00
|
|
|
std::string rangeToString( InputIterator first, InputIterator last );
|
2013-09-17 23:22:47 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 09:14:59 +02:00
|
|
|
//template<typename T, typename Allocator>
|
|
|
|
//struct StringMaker<std::vector<T, Allocator> > {
|
|
|
|
// static std::string convert( std::vector<T,Allocator> const& v ) {
|
|
|
|
// return Detail::rangeToString( v.begin(), v.end() );
|
|
|
|
// }
|
|
|
|
//};
|
|
|
|
|
2013-09-17 23:22:47 +02:00
|
|
|
template<typename T, typename Allocator>
|
2014-09-08 09:14:59 +02:00
|
|
|
std::string toString( std::vector<T,Allocator> const& v ) {
|
|
|
|
return Detail::rangeToString( v.begin(), v.end() );
|
|
|
|
}
|
2012-05-08 09:10:49 +02:00
|
|
|
|
2014-09-04 01:31:11 +02:00
|
|
|
|
2015-05-19 19:37:58 +02:00
|
|
|
#ifdef CATCH_CONFIG_CPP11_TUPLE
|
2015-03-04 19:33:31 +01:00
|
|
|
|
|
|
|
// toString for tuples
|
2014-09-04 01:31:11 +02:00
|
|
|
namespace TupleDetail {
|
|
|
|
template<
|
|
|
|
typename Tuple,
|
|
|
|
std::size_t N = 0,
|
|
|
|
bool = (N < std::tuple_size<Tuple>::value)
|
|
|
|
>
|
|
|
|
struct ElementPrinter {
|
|
|
|
static void print( const Tuple& tuple, std::ostream& os )
|
|
|
|
{
|
|
|
|
os << ( N ? ", " : " " )
|
|
|
|
<< Catch::toString(std::get<N>(tuple));
|
|
|
|
ElementPrinter<Tuple,N+1>::print(tuple,os);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<
|
|
|
|
typename Tuple,
|
|
|
|
std::size_t N
|
|
|
|
>
|
|
|
|
struct ElementPrinter<Tuple,N,false> {
|
|
|
|
static void print( const Tuple&, std::ostream& ) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ...Types>
|
|
|
|
struct StringMaker<std::tuple<Types...>> {
|
|
|
|
|
|
|
|
static std::string convert( const std::tuple<Types...>& tuple )
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
|
|
|
os << '{';
|
|
|
|
TupleDetail::ElementPrinter<std::tuple<Types...>>::print( tuple, os );
|
|
|
|
os << " }";
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
};
|
2015-05-19 19:37:58 +02:00
|
|
|
#endif // CATCH_CONFIG_CPP11_TUPLE
|
2014-09-04 01:31:11 +02:00
|
|
|
|
2013-03-04 12:19:15 +01:00
|
|
|
namespace Detail {
|
|
|
|
template<typename T>
|
2014-04-23 08:03:15 +02:00
|
|
|
std::string makeString( T const& value ) {
|
2013-03-04 12:19:15 +01:00
|
|
|
return StringMaker<T>::convert( value );
|
2013-07-03 20:14:59 +02:00
|
|
|
}
|
2012-05-08 09:10:49 +02:00
|
|
|
} // end namespace Detail
|
|
|
|
|
|
|
|
/// \brief converts any type to a string
|
|
|
|
///
|
2013-07-03 20:14:59 +02:00
|
|
|
/// The default template forwards on to ostringstream - except when an
|
|
|
|
/// ostringstream overload does not exist - in which case it attempts to detect
|
2012-05-08 09:10:49 +02:00
|
|
|
/// that and writes {?}.
|
|
|
|
/// Overload (not specialise) this template for custom typs that you don't want
|
|
|
|
/// to provide an ostream overload for.
|
|
|
|
template<typename T>
|
2013-04-23 19:58:56 +02:00
|
|
|
std::string toString( T const& value ) {
|
2013-03-04 12:19:15 +01:00
|
|
|
return StringMaker<T>::convert( value );
|
2012-05-08 09:10:49 +02:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-08-01 09:17:07 +02:00
|
|
|
|
2013-09-21 19:45:42 +02:00
|
|
|
namespace Detail {
|
|
|
|
template<typename InputIterator>
|
|
|
|
std::string rangeToString( InputIterator first, InputIterator last ) {
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "{ ";
|
|
|
|
if( first != last ) {
|
2014-12-09 19:49:58 +01:00
|
|
|
oss << Catch::toString( *first );
|
|
|
|
for( ++first ; first != last ; ++first )
|
|
|
|
oss << ", " << Catch::toString( *first );
|
2013-09-21 19:45:42 +02:00
|
|
|
}
|
|
|
|
oss << " }";
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-08 09:10:49 +02:00
|
|
|
} // end namespace Catch
|
|
|
|
|
2014-04-23 08:03:15 +02:00
|
|
|
#endif // TWOBLUECUBES_CATCH_TOSTRING_H_INCLUDED
|