diff --git a/docs/tostring.md b/docs/tostring.md index f961ccc1..05a6b79b 100644 --- a/docs/tostring.md +++ b/docs/tostring.md @@ -25,7 +25,7 @@ If you don't want to provide an ```operator <<``` overload, or you want to conve namespace Catch { template<> struct StringMaker { - std::string operator()( T const& value ) { + static std::string convert( T const& value ) { return convertMyTypeToString( value ); } }; diff --git a/include/internal/catch_approx.hpp b/include/internal/catch_approx.hpp index 9c974f6c..4235d0bc 100644 --- a/include/internal/catch_approx.hpp +++ b/include/internal/catch_approx.hpp @@ -128,7 +128,7 @@ namespace Detail { template<> struct StringMaker { - std::string operator()(Catch::Detail::Approx const& value) { + static std::string convert(Catch::Detail::Approx const& value) { return value.toString(); } }; diff --git a/include/internal/catch_tostring.h b/include/internal/catch_tostring.h index 6a20f9ef..91fa9ed9 100644 --- a/include/internal/catch_tostring.h +++ b/include/internal/catch_tostring.h @@ -61,17 +61,19 @@ namespace Catch { template struct StringMaker { template + static typename std::enable_if<::Catch::Detail::IsStreamInsertable::value, std::string>::type - operator()(const Fake& t) { - std::stringstream sstr; - sstr << t; - return sstr.str(); + convert(const Fake& t) { + std::stringstream sstr; + sstr << t; + return sstr.str(); } template + static typename std::enable_if::value, std::string>::type - operator()(const Fake&) { - return "{?}"; + convert(const Fake&) { + return "{?}"; } }; @@ -83,7 +85,7 @@ namespace Catch { // Should be preferably called fully qualified, like ::Catch::Detail::stringify template std::string stringify(const T& e) { - return ::Catch::StringMaker::type>::type>{}(e); + return ::Catch::StringMaker::type>::type>::convert(e); } } // namespace Detail @@ -93,110 +95,110 @@ namespace Catch { template<> struct StringMaker { - std::string operator()(const std::string& str); + static std::string convert(const std::string& str); }; template<> struct StringMaker { - std::string operator()(const std::wstring& wstr); + static std::string convert(const std::wstring& wstr); }; template<> struct StringMaker { - std::string operator()(char const * str); + static std::string convert(char const * str); }; template<> struct StringMaker { - std::string operator()(char * str); + static std::string convert(char * str); }; template<> struct StringMaker { - std::string operator()(wchar_t const * str); + static std::string convert(wchar_t const * str); }; template<> struct StringMaker { - std::string operator()(wchar_t * str); + static std::string convert(wchar_t * str); }; template struct StringMaker { - std::string operator()(const char* str) { + static std::string convert(const char* str) { return ::Catch::Detail::stringify(std::string{ str }); } }; template struct StringMaker { - std::string operator()(const char* str) { + static std::string convert(const char* str) { return ::Catch::Detail::stringify(std::string{ str }); } }; template struct StringMaker { - std::string operator()(const char* str) { + static std::string convert(const char* str) { return ::Catch::Detail::stringify(std::string{ str }); } }; template<> struct StringMaker { - std::string operator()(int value); + static std::string convert(int value); }; template<> struct StringMaker { - std::string operator()(long value); + static std::string convert(long value); }; template<> struct StringMaker { - std::string operator()(long long value); + static std::string convert(long long value); }; template<> struct StringMaker { - std::string operator()(unsigned int value); + static std::string convert(unsigned int value); }; template<> struct StringMaker { - std::string operator()(unsigned long value); + static std::string convert(unsigned long value); }; template<> struct StringMaker { - std::string operator()(unsigned long long value); + static std::string convert(unsigned long long value); }; template<> struct StringMaker { - std::string operator()(bool b); + static std::string convert(bool b); }; template<> struct StringMaker { - std::string operator()(char c); + static std::string convert(char c); }; template<> struct StringMaker { - std::string operator()(signed char c); + static std::string convert(signed char c); }; template<> struct StringMaker { - std::string operator()(unsigned char c); + static std::string convert(unsigned char c); }; template<> struct StringMaker { - std::string operator()(std::nullptr_t); + static std::string convert(std::nullptr_t); }; template<> struct StringMaker { - std::string operator()(float value); + static std::string convert(float value); }; template<> struct StringMaker { - std::string operator()(double value); + static std::string convert(double value); }; template struct StringMaker { template - std::string operator()(U* p) { + static std::string convert(U* p) { if (p) { return ::Catch::Detail::rawMemoryToString(p); } else { @@ -207,7 +209,7 @@ namespace Catch { template struct StringMaker { - std::string operator()(R C::* p) { + static std::string convert(R C::* p) { if (p) { return ::Catch::Detail::rawMemoryToString(p); } else { @@ -233,7 +235,7 @@ namespace Catch { template struct StringMaker > { - std::string operator()( std::vector const& v ) { + static std::string convert( std::vector const& v ) { return ::Catch::Detail::rangeToString( v.begin(), v.end() ); } }; @@ -241,7 +243,7 @@ namespace Catch { // === Pair === template struct StringMaker > { - std::string operator()(const std::pair& pair) { + static std::string convert(const std::pair& pair) { std::ostringstream oss; oss << "{ " << ::Catch::Detail::stringify(pair.first) @@ -281,7 +283,7 @@ namespace Catch { template struct StringMaker> { - std::string operator()(const std::tuple& tuple) { + static std::string convert(const std::tuple& tuple) { std::ostringstream os; os << '{'; Detail::TupleElementPrinter>::print(tuple, os); @@ -293,7 +295,7 @@ namespace Catch { template struct EnumStringMaker { - std::string operator()(const T& t) { + static std::string convert(const T& t) { return ::Catch::Detail::stringify(static_cast::type>(t)); } }; @@ -301,15 +303,15 @@ namespace Catch { #ifdef __OBJC__ template<> struct StringMaker { - std::string operator()(NSString const* const& nsstring); + static std::string convert(NSString const* const& nsstring); }; template<> struct StringMaker { - std::string operator()(NSString * CATCH_ARC_STRONG const& nsstring); + static std::string convert(NSString * CATCH_ARC_STRONG const& nsstring); }; template<> struct StringMaker { - std::string operator()(NSObject* const& nsObject); + static std::string convert(NSObject* const& nsObject); }; #endif diff --git a/include/internal/catch_tostring.hpp b/include/internal/catch_tostring.hpp index 06046095..03881249 100644 --- a/include/internal/catch_tostring.hpp +++ b/include/internal/catch_tostring.hpp @@ -76,7 +76,7 @@ std::string fpToString( T value, int precision ) { // //// ======================================================= //// -std::string StringMaker::operator()(const std::string& str) { +std::string StringMaker::convert(const std::string& str) { if (!getCurrentContext().getConfig()->showInvisibles()) { return '"' + str + '"'; } @@ -99,7 +99,7 @@ std::string StringMaker::operator()(const std::string& str) { return s; } -std::string StringMaker::operator()(const std::wstring& wstr) { +std::string StringMaker::convert(const std::wstring& wstr) { std::string s; s.reserve(wstr.size()); for (auto c : wstr) { @@ -108,28 +108,28 @@ std::string StringMaker::operator()(const std::wstring& wstr) { return ::Catch::Detail::stringify(s); } -std::string StringMaker::operator()(char const* str) { +std::string StringMaker::convert(char const* str) { if (str) { return ::Catch::Detail::stringify(std::string{ str }); } else { return{ "{null string}" }; } } -std::string StringMaker::operator()(char* str) { +std::string StringMaker::convert(char* str) { if (str) { return ::Catch::Detail::stringify(std::string{ str }); } else { return{ "{null string}" }; } } -std::string StringMaker::operator()(wchar_t const * str) { +std::string StringMaker::convert(wchar_t const * str) { if (str) { return ::Catch::Detail::stringify(std::wstring{ str }); } else { return{ "{null string}" }; } } -std::string StringMaker::operator()(wchar_t * str) { +std::string StringMaker::convert(wchar_t * str) { if (str) { return ::Catch::Detail::stringify(std::wstring{ str }); } else { @@ -138,13 +138,13 @@ std::string StringMaker::operator()(wchar_t * str) { } -std::string StringMaker::operator()(int value) { +std::string StringMaker::convert(int value) { return ::Catch::Detail::stringify(static_cast(value)); } -std::string StringMaker::operator()(long value) { +std::string StringMaker::convert(long value) { return ::Catch::Detail::stringify(static_cast(value)); } -std::string StringMaker::operator()(long long value) { +std::string StringMaker::convert(long long value) { std::ostringstream oss; oss << value; if (value > Detail::hexThreshold) { @@ -153,13 +153,13 @@ std::string StringMaker::operator()(long long value) { return oss.str(); } -std::string StringMaker::operator()(unsigned int value) { +std::string StringMaker::convert(unsigned int value) { return ::Catch::Detail::stringify(static_cast(value)); } -std::string StringMaker::operator()(unsigned long value) { +std::string StringMaker::convert(unsigned long value) { return ::Catch::Detail::stringify(static_cast(value)); } -std::string StringMaker::operator()(unsigned long long value) { +std::string StringMaker::convert(unsigned long long value) { std::ostringstream oss; oss << value; if (value > Detail::hexThreshold) { @@ -169,11 +169,11 @@ std::string StringMaker::operator()(unsigned long long value } -std::string StringMaker::operator()(bool b) { +std::string StringMaker::convert(bool b) { return b ? "true" : "false"; } -std::string StringMaker::operator()(char value) { +std::string StringMaker::convert(char value) { if (value == '\r') { return "'\\r'"; } else if (value == '\f') { @@ -190,37 +190,37 @@ std::string StringMaker::operator()(char value) { return chstr; } } -std::string StringMaker::operator()(signed char c) { +std::string StringMaker::convert(signed char c) { return ::Catch::Detail::stringify(static_cast(c)); } -std::string StringMaker::operator()(unsigned char c) { +std::string StringMaker::convert(unsigned char c) { return ::Catch::Detail::stringify(static_cast(c)); } -std::string StringMaker::operator()(std::nullptr_t) { +std::string StringMaker::convert(std::nullptr_t) { return "nullptr"; } -std::string StringMaker::operator()(float value) { +std::string StringMaker::convert(float value) { return fpToString(value, 5) + 'f'; } -std::string StringMaker::operator()(double value) { +std::string StringMaker::convert(double value) { return fpToString(value, 10); } #ifdef __OBJC__ -std::string StringMaker::operator()(NSString const * const& nsstring) { +std::string StringMaker::convert(NSString const * const& nsstring) { if (!nsstring) return "nil"; return "@" + toString([nsstring UTF8String]); } -std::string StringMaker::operator()(NSString * CATCH_ARC_STRONG const& nsstring) { +std::string StringMaker::convert(NSString * CATCH_ARC_STRONG const& nsstring) { if (!nsstring) return "nil"; return "@" + toString([nsstring UTF8String]); } -std::string StringMaker::operator()(NSObject * const& nsObject) { +std::string StringMaker::convert(NSObject * const& nsObject) { return ::Catch::Detail::stringify([nsObject description]); } #endif diff --git a/projects/SelfTest/ToStringWhich.cpp b/projects/SelfTest/ToStringWhich.cpp index 8bd1b6f9..fc658a7a 100644 --- a/projects/SelfTest/ToStringWhich.cpp +++ b/projects/SelfTest/ToStringWhich.cpp @@ -22,13 +22,13 @@ std::ostream& operator<<(std::ostream& os, const has_maker_and_operator&) { namespace Catch { template<> struct StringMaker { - std::string operator()( const has_maker& ) { + static std::string convert( const has_maker& ) { return "StringMaker"; } }; template<> struct StringMaker { - std::string operator()( const has_maker_and_operator& ) { + static std::string convert( const has_maker_and_operator& ) { return "StringMaker"; } };