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 | */ |
Phil Nash | 328a469 | 2014-04-23 07:03:15 +0100 | [diff] [blame] | 8 | #ifndef TWOBLUECUBES_CATCH_TOSTRING_H_INCLUDED |
| 9 | #define TWOBLUECUBES_CATCH_TOSTRING_H_INCLUDED |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 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 | c2b0c6f | 2013-11-07 10:35:59 +0000 | [diff] [blame] | 17 | #include <vector> |
Phil Nash | 1a6f2a0 | 2014-04-23 18:19:19 +0100 | [diff] [blame] | 18 | #include <cstddef> |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 19 | |
Phil Nash | 0dc9e43 | 2012-08-01 08:17:07 +0100 | [diff] [blame] | 20 | #ifdef __OBJC__ |
| 21 | #include "catch_objc_arc.hpp" |
| 22 | #endif |
| 23 | |
Phil Nash | 2efc114 | 2012-05-15 07:42:26 +0100 | [diff] [blame] | 24 | namespace Catch { |
Phil Nash | 003960d | 2013-04-20 23:12:17 +0100 | [diff] [blame] | 25 | namespace Detail { |
Phil Nash | 5062d3e | 2013-04-16 22:55:31 +0100 | [diff] [blame] | 26 | |
Phil Nash | 9fff9e4 | 2013-04-20 23:18:44 +0100 | [diff] [blame] | 27 | // SFINAE is currently disabled by default for all compilers. |
| 28 | // If the non SFINAE version of IsStreamInsertable is ambiguous for you |
Phil Nash | 4dd3f68 | 2013-04-22 08:19:17 +0100 | [diff] [blame] | 29 | // and your compiler supports SFINAE, try #defining CATCH_CONFIG_SFINAE |
| 30 | #ifdef CATCH_CONFIG_SFINAE |
Phil Nash | 5062d3e | 2013-04-16 22:55:31 +0100 | [diff] [blame] | 31 | |
Phil Nash | 5062d3e | 2013-04-16 22:55:31 +0100 | [diff] [blame] | 32 | template<typename T> |
| 33 | class IsStreamInsertableHelper { |
| 34 | template<int N> struct TrueIfSizeable : TrueType {}; |
| 35 | |
| 36 | template<typename T2> |
| 37 | static TrueIfSizeable<sizeof((*(std::ostream*)0) << *((T2 const*)0))> dummy(T2*); |
| 38 | static FalseType dummy(...); |
| 39 | |
| 40 | public: |
| 41 | typedef SizedIf<sizeof(dummy((T*)0))> type; |
| 42 | }; |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 43 | |
Phil Nash | 5062d3e | 2013-04-16 22:55:31 +0100 | [diff] [blame] | 44 | template<typename T> |
| 45 | struct IsStreamInsertable : IsStreamInsertableHelper<T>::type {}; |
| 46 | |
Phil Nash | 5062d3e | 2013-04-16 22:55:31 +0100 | [diff] [blame] | 47 | #else |
| 48 | |
Phil Nash | 003960d | 2013-04-20 23:12:17 +0100 | [diff] [blame] | 49 | struct BorgType { |
| 50 | template<typename T> BorgType( T const& ); |
| 51 | }; |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 52 | |
Phil Nash | 003960d | 2013-04-20 23:12:17 +0100 | [diff] [blame] | 53 | TrueType& testStreamable( std::ostream& ); |
| 54 | FalseType testStreamable( FalseType ); |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 55 | |
Phil Nash | 003960d | 2013-04-20 23:12:17 +0100 | [diff] [blame] | 56 | FalseType operator<<( std::ostream const&, BorgType const& ); |
Phil Nash | 2efc114 | 2012-05-15 07:42:26 +0100 | [diff] [blame] | 57 | |
Phil Nash | 003960d | 2013-04-20 23:12:17 +0100 | [diff] [blame] | 58 | template<typename T> |
| 59 | struct IsStreamInsertable { |
| 60 | static std::ostream &s; |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 61 | static T const&t; |
Phil Nash | 003960d | 2013-04-20 23:12:17 +0100 | [diff] [blame] | 62 | enum { value = sizeof( testStreamable(s << t) ) == sizeof( TrueType ) }; |
| 63 | }; |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 64 | |
Phil Nash | 003960d | 2013-04-20 23:12:17 +0100 | [diff] [blame] | 65 | #endif |
| 66 | |
| 67 | template<bool C> |
| 68 | struct StringMakerBase { |
| 69 | template<typename T> |
| 70 | static std::string convert( T const& ) { return "{?}"; } |
| 71 | }; |
| 72 | |
| 73 | template<> |
| 74 | struct StringMakerBase<true> { |
| 75 | template<typename T> |
| 76 | static std::string convert( T const& _value ) { |
| 77 | std::ostringstream oss; |
| 78 | oss << _value; |
| 79 | return oss.str(); |
| 80 | } |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 81 | }; |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 82 | |
Phil Nash | 7cbf740 | 2014-05-23 18:41:02 +0100 | [diff] [blame] | 83 | std::string rawMemoryToString( const void *object, std::size_t size ); |
Phil Nash | 48153e8 | 2014-04-22 08:19:11 +0100 | [diff] [blame] | 84 | |
Phil Nash | e091018 | 2014-01-07 17:25:27 +0000 | [diff] [blame] | 85 | template<typename T> |
Phil Nash | 7cbf740 | 2014-05-23 18:41:02 +0100 | [diff] [blame] | 86 | inline std::string rawMemoryToString( const T& object ) { |
| 87 | return rawMemoryToString( &object, sizeof(object) ); |
Phil Nash | e091018 | 2014-01-07 17:25:27 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 90 | } // end namespace Detail |
| 91 | |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 92 | template<typename T> |
Phil Nash | 1e2f1d1 | 2013-09-14 19:58:45 +0100 | [diff] [blame] | 93 | std::string toString( T const& value ); |
| 94 | |
| 95 | template<typename T> |
Phil Nash | 003960d | 2013-04-20 23:12:17 +0100 | [diff] [blame] | 96 | struct StringMaker : |
| 97 | Detail::StringMakerBase<Detail::IsStreamInsertable<T>::value> {}; |
Phil Nash | 5062d3e | 2013-04-16 22:55:31 +0100 | [diff] [blame] | 98 | |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 99 | template<typename T> |
| 100 | struct StringMaker<T*> { |
Phil Nash | 503d5d0 | 2013-07-03 08:25:11 +0100 | [diff] [blame] | 101 | template<typename U> |
| 102 | static std::string convert( U* p ) { |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 103 | if( !p ) |
| 104 | return INTERNAL_CATCH_STRINGIFY( NULL ); |
Phil Nash | e091018 | 2014-01-07 17:25:27 +0000 | [diff] [blame] | 105 | else |
| 106 | return Detail::rawMemoryToString( p ); |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | template<typename R, typename C> |
| 111 | struct StringMaker<R C::*> { |
| 112 | static std::string convert( R C::* p ) { |
| 113 | if( !p ) |
| 114 | return INTERNAL_CATCH_STRINGIFY( NULL ); |
| 115 | else |
| 116 | return Detail::rawMemoryToString( p ); |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 117 | } |
| 118 | }; |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 119 | |
Andy Sawyer | 0dbcf21 | 2013-09-17 22:22:47 +0100 | [diff] [blame] | 120 | namespace Detail { |
| 121 | template<typename InputIterator> |
Andy Sawyer | d6f23a9 | 2013-09-21 18:45:42 +0100 | [diff] [blame] | 122 | std::string rangeToString( InputIterator first, InputIterator last ); |
Andy Sawyer | 0dbcf21 | 2013-09-17 22:22:47 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | template<typename T, typename Allocator> |
| 126 | struct StringMaker<std::vector<T, Allocator> > { |
| 127 | static std::string convert( std::vector<T,Allocator> const& v ) { |
| 128 | return Detail::rangeToString( v.begin(), v.end() ); |
| 129 | } |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 130 | }; |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 131 | |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 132 | namespace Detail { |
| 133 | template<typename T> |
Phil Nash | 328a469 | 2014-04-23 07:03:15 +0100 | [diff] [blame] | 134 | std::string makeString( T const& value ) { |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 135 | return StringMaker<T>::convert( value ); |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 136 | } |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 137 | } // end namespace Detail |
| 138 | |
| 139 | /// \brief converts any type to a string |
| 140 | /// |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame] | 141 | /// The default template forwards on to ostringstream - except when an |
| 142 | /// ostringstream overload does not exist - in which case it attempts to detect |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 143 | /// that and writes {?}. |
| 144 | /// Overload (not specialise) this template for custom typs that you don't want |
| 145 | /// to provide an ostream overload for. |
| 146 | template<typename T> |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 147 | std::string toString( T const& value ) { |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 148 | return StringMaker<T>::convert( value ); |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 149 | } |
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 | // Built in overloads |
| 152 | |
Phil Nash | 328a469 | 2014-04-23 07:03:15 +0100 | [diff] [blame] | 153 | std::string toString( std::string const& value ); |
| 154 | std::string toString( std::wstring const& value ); |
| 155 | std::string toString( const char* const value ); |
| 156 | std::string toString( char* const value ); |
| 157 | std::string toString( int value ); |
| 158 | std::string toString( unsigned long value ); |
| 159 | std::string toString( unsigned int value ); |
| 160 | std::string toString( const double value ); |
| 161 | std::string toString( bool value ); |
| 162 | std::string toString( char value ); |
| 163 | std::string toString( signed char value ); |
| 164 | std::string toString( unsigned char value ); |
Phil Nash | 86ad634 | 2012-12-14 07:49:18 +0000 | [diff] [blame] | 165 | |
Phil Nash | 5ec53b2 | 2012-05-10 07:58:48 +0100 | [diff] [blame] | 166 | #ifdef CATCH_CONFIG_CPP11_NULLPTR |
Phil Nash | 328a469 | 2014-04-23 07:03:15 +0100 | [diff] [blame] | 167 | std::string toString( std::nullptr_t ); |
Phil Nash | 5ec53b2 | 2012-05-10 07:58:48 +0100 | [diff] [blame] | 168 | #endif |
| 169 | |
Phil Nash | 0dc9e43 | 2012-08-01 08:17:07 +0100 | [diff] [blame] | 170 | #ifdef __OBJC__ |
Phil Nash | 328a469 | 2014-04-23 07:03:15 +0100 | [diff] [blame] | 171 | std::string toString( NSString const * const& nsstring ); |
| 172 | std::string toString( NSString * CATCH_ARC_STRONG const& nsstring ); |
| 173 | std::string toString( NSObject* const& nsObject ); |
Phil Nash | 0dc9e43 | 2012-08-01 08:17:07 +0100 | [diff] [blame] | 174 | #endif |
| 175 | |
Andy Sawyer | d6f23a9 | 2013-09-21 18:45:42 +0100 | [diff] [blame] | 176 | namespace Detail { |
| 177 | template<typename InputIterator> |
| 178 | std::string rangeToString( InputIterator first, InputIterator last ) { |
| 179 | std::ostringstream oss; |
| 180 | oss << "{ "; |
| 181 | if( first != last ) { |
| 182 | oss << toString( *first ); |
| 183 | for( ++first ; first != last ; ++first ) { |
| 184 | oss << ", " << toString( *first ); |
| 185 | } |
| 186 | } |
| 187 | oss << " }"; |
| 188 | return oss.str(); |
| 189 | } |
| 190 | } |
| 191 | |
Phil Nash | 81a122e | 2012-05-08 08:10:49 +0100 | [diff] [blame] | 192 | } // end namespace Catch |
| 193 | |
Phil Nash | 328a469 | 2014-04-23 07:03:15 +0100 | [diff] [blame] | 194 | #endif // TWOBLUECUBES_CATCH_TOSTRING_H_INCLUDED |