Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Created by Phil on 8/5/2012. |
| 3 | * Copyright 2012 Two Blue Cubes Ltd. All rights reserved. |
| 4 | * |
| 5 | * Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 6 | * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 7 | */ |
| 8 | #ifndef TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED |
| 9 | #define TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED |
| 10 | |
| 11 | #include "catch_common.h" |
Phil Nash | 5062d3e | 2013-04-16 22:55:31 +0100 | [diff] [blame] | 12 | #include "catch_sfinae.hpp" |
| 13 | |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 14 | #include <sstream> |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 15 | #include <iomanip> |
| 16 | #include <limits> |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 17 | |
Phil Nash | 0dc9e43 | 2012-08-01 08:17:07 +0100 | [diff] [blame] | 18 | #ifdef __OBJC__ |
| 19 | #include "catch_objc_arc.hpp" |
| 20 | #endif |
| 21 | |
Phil Nash | 2efc114 | 2012-05-15 07:42:26 +0100 | [diff] [blame] | 22 | namespace Catch { |
Phil Nash | 003960d | 2013-04-20 23:12:17 +0100 | [diff] [blame] | 23 | namespace Detail { |
Phil Nash | 5062d3e | 2013-04-16 22:55:31 +0100 | [diff] [blame] | 24 | |
Phil Nash | 9fff9e4 | 2013-04-20 23:18:44 +0100 | [diff] [blame] | 25 | // SFINAE is currently disabled by default for all compilers. |
| 26 | // If the non SFINAE version of IsStreamInsertable is ambiguous for you |
Phil Nash | 4dd3f68 | 2013-04-22 08:19:17 +0100 | [diff] [blame] | 27 | // and your compiler supports SFINAE, try #defining CATCH_CONFIG_SFINAE |
| 28 | #ifdef CATCH_CONFIG_SFINAE |
Phil Nash | 5062d3e | 2013-04-16 22:55:31 +0100 | [diff] [blame] | 29 | |
Phil Nash | 5062d3e | 2013-04-16 22:55:31 +0100 | [diff] [blame] | 30 | template<typename T> |
| 31 | class IsStreamInsertableHelper { |
| 32 | template<int N> struct TrueIfSizeable : TrueType {}; |
| 33 | |
| 34 | template<typename T2> |
| 35 | static TrueIfSizeable<sizeof((*(std::ostream*)0) << *((T2 const*)0))> dummy(T2*); |
| 36 | static FalseType dummy(...); |
| 37 | |
| 38 | public: |
| 39 | typedef SizedIf<sizeof(dummy((T*)0))> type; |
| 40 | }; |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 41 | |
Phil Nash | 5062d3e | 2013-04-16 22:55:31 +0100 | [diff] [blame] | 42 | template<typename T> |
| 43 | struct IsStreamInsertable : IsStreamInsertableHelper<T>::type {}; |
| 44 | |
Phil Nash | 5062d3e | 2013-04-16 22:55:31 +0100 | [diff] [blame] | 45 | #else |
| 46 | |
Phil Nash | 003960d | 2013-04-20 23:12:17 +0100 | [diff] [blame] | 47 | struct BorgType { |
| 48 | template<typename T> BorgType( T const& ); |
| 49 | }; |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 50 | |
Phil Nash | 003960d | 2013-04-20 23:12:17 +0100 | [diff] [blame] | 51 | TrueType& testStreamable( std::ostream& ); |
| 52 | FalseType testStreamable( FalseType ); |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 53 | |
Phil Nash | 003960d | 2013-04-20 23:12:17 +0100 | [diff] [blame] | 54 | FalseType operator<<( std::ostream const&, BorgType const& ); |
Phil Nash | 2efc114 | 2012-05-15 07:42:26 +0100 | [diff] [blame] | 55 | |
Phil Nash | 003960d | 2013-04-20 23:12:17 +0100 | [diff] [blame] | 56 | template<typename T> |
| 57 | struct IsStreamInsertable { |
| 58 | static std::ostream &s; |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 59 | static T const&t; |
Phil Nash | 003960d | 2013-04-20 23:12:17 +0100 | [diff] [blame] | 60 | enum { value = sizeof( testStreamable(s << t) ) == sizeof( TrueType ) }; |
| 61 | }; |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 62 | |
Phil Nash | 003960d | 2013-04-20 23:12:17 +0100 | [diff] [blame] | 63 | #endif |
| 64 | |
| 65 | template<bool C> |
| 66 | struct StringMakerBase { |
| 67 | template<typename T> |
| 68 | static std::string convert( T const& ) { return "{?}"; } |
| 69 | }; |
| 70 | |
| 71 | template<> |
| 72 | struct StringMakerBase<true> { |
| 73 | template<typename T> |
| 74 | static std::string convert( T const& _value ) { |
| 75 | std::ostringstream oss; |
| 76 | oss << _value; |
| 77 | return oss.str(); |
| 78 | } |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 79 | }; |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 80 | |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 81 | } // end namespace Detail |
| 82 | |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 83 | template<typename T> |
Phil Nash | 1e2f1d1 | 2013-09-14 19:58:45 +0100 | [diff] [blame] | 84 | std::string toString( T const& value ); |
| 85 | |
| 86 | template<typename T> |
Phil Nash | 003960d | 2013-04-20 23:12:17 +0100 | [diff] [blame] | 87 | struct StringMaker : |
| 88 | Detail::StringMakerBase<Detail::IsStreamInsertable<T>::value> {}; |
Phil Nash | 5062d3e | 2013-04-16 22:55:31 +0100 | [diff] [blame] | 89 | |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 90 | template<typename T> |
| 91 | struct StringMaker<T*> { |
Phil Nash | 503d5d0 | 2013-07-03 08:25:11 +0100 | [diff] [blame] | 92 | template<typename U> |
| 93 | static std::string convert( U* p ) { |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 94 | if( !p ) |
| 95 | return INTERNAL_CATCH_STRINGIFY( NULL ); |
| 96 | std::ostringstream oss; |
| 97 | oss << p; |
| 98 | return oss.str(); |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 99 | } |
| 100 | }; |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 101 | |
Andy Sawyer | 0dbcf21 | 2013-09-17 22:22:47 +0100 | [diff] [blame] | 102 | namespace Detail { |
| 103 | template<typename InputIterator> |
Andy Sawyer | d6f23a9 | 2013-09-21 18:45:42 +0100 | [diff] [blame^] | 104 | std::string rangeToString( InputIterator first, InputIterator last ); |
Andy Sawyer | 0dbcf21 | 2013-09-17 22:22:47 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | template<typename T, typename Allocator> |
| 108 | struct StringMaker<std::vector<T, Allocator> > { |
| 109 | static std::string convert( std::vector<T,Allocator> const& v ) { |
| 110 | return Detail::rangeToString( v.begin(), v.end() ); |
| 111 | } |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 112 | }; |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 113 | |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 114 | namespace Detail { |
| 115 | template<typename T> |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 116 | inline std::string makeString( T const& value ) { |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 117 | return StringMaker<T>::convert( value ); |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 118 | } |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 119 | } // end namespace Detail |
| 120 | |
| 121 | /// \brief converts any type to a string |
| 122 | /// |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 123 | /// The default template forwards on to ostringstream - except when an |
| 124 | /// ostringstream overload does not exist - in which case it attempts to detect |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 125 | /// that and writes {?}. |
| 126 | /// Overload (not specialise) this template for custom typs that you don't want |
| 127 | /// to provide an ostream overload for. |
| 128 | template<typename T> |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 129 | std::string toString( T const& value ) { |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 130 | return StringMaker<T>::convert( value ); |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 131 | } |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 132 | |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 133 | // Built in overloads |
| 134 | |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 135 | inline std::string toString( std::string const& value ) { |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 136 | return "\"" + value + "\""; |
| 137 | } |
| 138 | |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 139 | inline std::string toString( std::wstring const& value ) { |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 140 | std::ostringstream oss; |
| 141 | oss << "\""; |
| 142 | for(size_t i = 0; i < value.size(); ++i ) |
| 143 | oss << static_cast<char>( value[i] <= 0xff ? value[i] : '?'); |
| 144 | oss << "\""; |
| 145 | return oss.str(); |
| 146 | } |
| 147 | |
| 148 | inline std::string toString( const char* const value ) { |
| 149 | return value ? Catch::toString( std::string( value ) ) : std::string( "{null string}" ); |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 150 | } |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 151 | |
| 152 | inline std::string toString( char* const value ) { |
| 153 | return Catch::toString( static_cast<const char*>( value ) ); |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 154 | } |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 155 | |
| 156 | inline std::string toString( int value ) { |
| 157 | std::ostringstream oss; |
| 158 | oss << value; |
| 159 | return oss.str(); |
| 160 | } |
| 161 | |
| 162 | inline std::string toString( unsigned long value ) { |
| 163 | std::ostringstream oss; |
| 164 | if( value > 8192 ) |
| 165 | oss << "0x" << std::hex << value; |
| 166 | else |
| 167 | oss << value; |
| 168 | return oss.str(); |
| 169 | } |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 170 | |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 171 | inline std::string toString( unsigned int value ) { |
Phil Nash | 8d69208 | 2012-05-11 19:05:53 +0100 | [diff] [blame] | 172 | return toString( static_cast<unsigned long>( value ) ); |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 173 | } |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 174 | |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 175 | inline std::string toString( const double value ) { |
| 176 | std::ostringstream oss; |
Phil Nash | 2ddb9d3 | 2013-08-15 18:39:55 +0100 | [diff] [blame] | 177 | oss << std::setprecision( 10 ) |
| 178 | << std::fixed |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 179 | << value; |
Phil Nash | 2ddb9d3 | 2013-08-15 18:39:55 +0100 | [diff] [blame] | 180 | std::string d = oss.str(); |
| 181 | std::size_t i = d.find_last_not_of( '0' ); |
| 182 | if( i != std::string::npos && i != d.size()-1 ) { |
| 183 | if( d[i] == '.' ) |
| 184 | i++; |
| 185 | d = d.substr( 0, i+1 ); |
| 186 | } |
| 187 | return d; |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 188 | } |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 189 | |
| 190 | inline std::string toString( bool value ) { |
| 191 | return value ? "true" : "false"; |
| 192 | } |
| 193 | |
Phil Nash | 78372d0 | 2012-06-06 08:06:40 +0100 | [diff] [blame] | 194 | inline std::string toString( char value ) { |
| 195 | return value < ' ' |
Phil Nash | 86ad634 | 2012-12-14 07:49:18 +0000 | [diff] [blame] | 196 | ? toString( static_cast<unsigned int>( value ) ) |
Phil Nash | 78372d0 | 2012-06-06 08:06:40 +0100 | [diff] [blame] | 197 | : Detail::makeString( value ); |
| 198 | } |
| 199 | |
| 200 | inline std::string toString( signed char value ) { |
| 201 | return toString( static_cast<char>( value ) ); |
| 202 | } |
| 203 | |
Phil Nash | 86ad634 | 2012-12-14 07:49:18 +0000 | [diff] [blame] | 204 | inline std::string toString( unsigned char value ) { |
| 205 | return toString( static_cast<char>( value ) ); |
| 206 | } |
| 207 | |
Phil Nash | 5ec53b2 | 2012-05-10 07:58:48 +0100 | [diff] [blame] | 208 | #ifdef CATCH_CONFIG_CPP11_NULLPTR |
Konrad Rudolph | 84434be | 2012-05-23 11:22:49 +0100 | [diff] [blame] | 209 | inline std::string toString( std::nullptr_t ) { |
Phil Nash | 5ec53b2 | 2012-05-10 07:58:48 +0100 | [diff] [blame] | 210 | return "nullptr"; |
| 211 | } |
| 212 | #endif |
| 213 | |
Phil Nash | 0dc9e43 | 2012-08-01 08:17:07 +0100 | [diff] [blame] | 214 | #ifdef __OBJC__ |
Phil Nash | 0dc9e43 | 2012-08-01 08:17:07 +0100 | [diff] [blame] | 215 | inline std::string toString( NSString const * const& nsstring ) { |
Phil Nash | 32e70b2 | 2013-03-12 18:49:22 +0000 | [diff] [blame] | 216 | if( !nsstring ) |
| 217 | return "nil"; |
Phil Nash | 0dc9e43 | 2012-08-01 08:17:07 +0100 | [diff] [blame] | 218 | return std::string( "@\"" ) + [nsstring UTF8String] + "\""; |
| 219 | } |
| 220 | inline std::string toString( NSString * CATCH_ARC_STRONG const& nsstring ) { |
Phil Nash | 32e70b2 | 2013-03-12 18:49:22 +0000 | [diff] [blame] | 221 | if( !nsstring ) |
| 222 | return "nil"; |
Phil Nash | 0dc9e43 | 2012-08-01 08:17:07 +0100 | [diff] [blame] | 223 | return std::string( "@\"" ) + [nsstring UTF8String] + "\""; |
| 224 | } |
| 225 | inline std::string toString( NSObject* const& nsObject ) { |
| 226 | return toString( [nsObject description] ); |
| 227 | } |
| 228 | #endif |
| 229 | |
Andy Sawyer | d6f23a9 | 2013-09-21 18:45:42 +0100 | [diff] [blame^] | 230 | namespace Detail { |
| 231 | template<typename InputIterator> |
| 232 | std::string rangeToString( InputIterator first, InputIterator last ) { |
| 233 | std::ostringstream oss; |
| 234 | oss << "{ "; |
| 235 | if( first != last ) { |
| 236 | oss << toString( *first ); |
| 237 | for( ++first ; first != last ; ++first ) { |
| 238 | oss << ", " << toString( *first ); |
| 239 | } |
| 240 | } |
| 241 | oss << " }"; |
| 242 | return oss.str(); |
| 243 | } |
| 244 | } |
| 245 | |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 246 | } // end namespace Catch |
| 247 | |
| 248 | #endif // TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED |