Move back to static StringMaker<T>::convert

This avoids some breakage from the modernization
This commit is contained in:
Martin Hořeňovský 2017-05-21 23:40:05 +02:00
parent 31f5e2ed81
commit 67914d8b86
5 changed files with 66 additions and 64 deletions

View File

@ -25,7 +25,7 @@ If you don't want to provide an ```operator <<``` overload, or you want to conve
namespace Catch { namespace Catch {
template<> template<>
struct StringMaker<T> { struct StringMaker<T> {
std::string operator()( T const& value ) { static std::string convert( T const& value ) {
return convertMyTypeToString( value ); return convertMyTypeToString( value );
} }
}; };

View File

@ -128,7 +128,7 @@ namespace Detail {
template<> template<>
struct StringMaker<Catch::Detail::Approx> { struct StringMaker<Catch::Detail::Approx> {
std::string operator()(Catch::Detail::Approx const& value) { static std::string convert(Catch::Detail::Approx const& value) {
return value.toString(); return value.toString();
} }
}; };

View File

@ -61,16 +61,18 @@ namespace Catch {
template <typename T> template <typename T>
struct StringMaker { struct StringMaker {
template <typename Fake = T> template <typename Fake = T>
static
typename std::enable_if<::Catch::Detail::IsStreamInsertable<Fake>::value, std::string>::type typename std::enable_if<::Catch::Detail::IsStreamInsertable<Fake>::value, std::string>::type
operator()(const Fake& t) { convert(const Fake& t) {
std::stringstream sstr; std::stringstream sstr;
sstr << t; sstr << t;
return sstr.str(); return sstr.str();
} }
template <typename Fake = T> template <typename Fake = T>
static
typename std::enable_if<!::Catch::Detail::IsStreamInsertable<Fake>::value, std::string>::type typename std::enable_if<!::Catch::Detail::IsStreamInsertable<Fake>::value, std::string>::type
operator()(const Fake&) { convert(const Fake&) {
return "{?}"; return "{?}";
} }
}; };
@ -83,7 +85,7 @@ namespace Catch {
// Should be preferably called fully qualified, like ::Catch::Detail::stringify // Should be preferably called fully qualified, like ::Catch::Detail::stringify
template <typename T> template <typename T>
std::string stringify(const T& e) { std::string stringify(const T& e) {
return ::Catch::StringMaker<typename std::remove_cv<typename std::remove_reference<T>::type>::type>{}(e); return ::Catch::StringMaker<typename std::remove_cv<typename std::remove_reference<T>::type>::type>::convert(e);
} }
} // namespace Detail } // namespace Detail
@ -93,110 +95,110 @@ namespace Catch {
template<> template<>
struct StringMaker<std::string> { struct StringMaker<std::string> {
std::string operator()(const std::string& str); static std::string convert(const std::string& str);
}; };
template<> template<>
struct StringMaker<std::wstring> { struct StringMaker<std::wstring> {
std::string operator()(const std::wstring& wstr); static std::string convert(const std::wstring& wstr);
}; };
template<> template<>
struct StringMaker<char const *> { struct StringMaker<char const *> {
std::string operator()(char const * str); static std::string convert(char const * str);
}; };
template<> template<>
struct StringMaker<char *> { struct StringMaker<char *> {
std::string operator()(char * str); static std::string convert(char * str);
}; };
template<> template<>
struct StringMaker<wchar_t const *> { struct StringMaker<wchar_t const *> {
std::string operator()(wchar_t const * str); static std::string convert(wchar_t const * str);
}; };
template<> template<>
struct StringMaker<wchar_t *> { struct StringMaker<wchar_t *> {
std::string operator()(wchar_t * str); static std::string convert(wchar_t * str);
}; };
template<int SZ> template<int SZ>
struct StringMaker<char[SZ]> { struct StringMaker<char[SZ]> {
std::string operator()(const char* str) { static std::string convert(const char* str) {
return ::Catch::Detail::stringify(std::string{ str }); return ::Catch::Detail::stringify(std::string{ str });
} }
}; };
template<int SZ> template<int SZ>
struct StringMaker<signed char[SZ]> { struct StringMaker<signed char[SZ]> {
std::string operator()(const char* str) { static std::string convert(const char* str) {
return ::Catch::Detail::stringify(std::string{ str }); return ::Catch::Detail::stringify(std::string{ str });
} }
}; };
template<int SZ> template<int SZ>
struct StringMaker<unsigned char[SZ]> { struct StringMaker<unsigned char[SZ]> {
std::string operator()(const char* str) { static std::string convert(const char* str) {
return ::Catch::Detail::stringify(std::string{ str }); return ::Catch::Detail::stringify(std::string{ str });
} }
}; };
template<> template<>
struct StringMaker<int> { struct StringMaker<int> {
std::string operator()(int value); static std::string convert(int value);
}; };
template<> template<>
struct StringMaker<long> { struct StringMaker<long> {
std::string operator()(long value); static std::string convert(long value);
}; };
template<> template<>
struct StringMaker<long long> { struct StringMaker<long long> {
std::string operator()(long long value); static std::string convert(long long value);
}; };
template<> template<>
struct StringMaker<unsigned int> { struct StringMaker<unsigned int> {
std::string operator()(unsigned int value); static std::string convert(unsigned int value);
}; };
template<> template<>
struct StringMaker<unsigned long> { struct StringMaker<unsigned long> {
std::string operator()(unsigned long value); static std::string convert(unsigned long value);
}; };
template<> template<>
struct StringMaker<unsigned long long> { struct StringMaker<unsigned long long> {
std::string operator()(unsigned long long value); static std::string convert(unsigned long long value);
}; };
template<> template<>
struct StringMaker<bool> { struct StringMaker<bool> {
std::string operator()(bool b); static std::string convert(bool b);
}; };
template<> template<>
struct StringMaker<char> { struct StringMaker<char> {
std::string operator()(char c); static std::string convert(char c);
}; };
template<> template<>
struct StringMaker<signed char> { struct StringMaker<signed char> {
std::string operator()(signed char c); static std::string convert(signed char c);
}; };
template<> template<>
struct StringMaker<unsigned char> { struct StringMaker<unsigned char> {
std::string operator()(unsigned char c); static std::string convert(unsigned char c);
}; };
template<> template<>
struct StringMaker<std::nullptr_t> { struct StringMaker<std::nullptr_t> {
std::string operator()(std::nullptr_t); static std::string convert(std::nullptr_t);
}; };
template<> template<>
struct StringMaker<float> { struct StringMaker<float> {
std::string operator()(float value); static std::string convert(float value);
}; };
template<> template<>
struct StringMaker<double> { struct StringMaker<double> {
std::string operator()(double value); static std::string convert(double value);
}; };
template <typename T> template <typename T>
struct StringMaker<T*> { struct StringMaker<T*> {
template <typename U> template <typename U>
std::string operator()(U* p) { static std::string convert(U* p) {
if (p) { if (p) {
return ::Catch::Detail::rawMemoryToString(p); return ::Catch::Detail::rawMemoryToString(p);
} else { } else {
@ -207,7 +209,7 @@ namespace Catch {
template <typename R, typename C> template <typename R, typename C>
struct StringMaker<R C::*> { struct StringMaker<R C::*> {
std::string operator()(R C::* p) { static std::string convert(R C::* p) {
if (p) { if (p) {
return ::Catch::Detail::rawMemoryToString(p); return ::Catch::Detail::rawMemoryToString(p);
} else { } else {
@ -233,7 +235,7 @@ namespace Catch {
template<typename T, typename Allocator> template<typename T, typename Allocator>
struct StringMaker<std::vector<T, Allocator> > { struct StringMaker<std::vector<T, Allocator> > {
std::string operator()( std::vector<T,Allocator> const& v ) { static std::string convert( std::vector<T,Allocator> const& v ) {
return ::Catch::Detail::rangeToString( v.begin(), v.end() ); return ::Catch::Detail::rangeToString( v.begin(), v.end() );
} }
}; };
@ -241,7 +243,7 @@ namespace Catch {
// === Pair === // === Pair ===
template<typename T1, typename T2> template<typename T1, typename T2>
struct StringMaker<std::pair<T1, T2> > { struct StringMaker<std::pair<T1, T2> > {
std::string operator()(const std::pair<T1, T2>& pair) { static std::string convert(const std::pair<T1, T2>& pair) {
std::ostringstream oss; std::ostringstream oss;
oss << "{ " oss << "{ "
<< ::Catch::Detail::stringify(pair.first) << ::Catch::Detail::stringify(pair.first)
@ -281,7 +283,7 @@ namespace Catch {
template<typename ...Types> template<typename ...Types>
struct StringMaker<std::tuple<Types...>> { struct StringMaker<std::tuple<Types...>> {
std::string operator()(const std::tuple<Types...>& tuple) { static std::string convert(const std::tuple<Types...>& tuple) {
std::ostringstream os; std::ostringstream os;
os << '{'; os << '{';
Detail::TupleElementPrinter<std::tuple<Types...>>::print(tuple, os); Detail::TupleElementPrinter<std::tuple<Types...>>::print(tuple, os);
@ -293,7 +295,7 @@ namespace Catch {
template<typename T> template<typename T>
struct EnumStringMaker { struct EnumStringMaker {
std::string operator()(const T& t) { static std::string convert(const T& t) {
return ::Catch::Detail::stringify(static_cast<typename std::underlying_type<T>::type>(t)); return ::Catch::Detail::stringify(static_cast<typename std::underlying_type<T>::type>(t));
} }
}; };
@ -301,15 +303,15 @@ namespace Catch {
#ifdef __OBJC__ #ifdef __OBJC__
template<> template<>
struct StringMaker<NSString const *> { struct StringMaker<NSString const *> {
std::string operator()(NSString const* const& nsstring); static std::string convert(NSString const* const& nsstring);
}; };
template<> template<>
struct StringMaker<NSString * CATCH_ARC_STRONG> { struct StringMaker<NSString * CATCH_ARC_STRONG> {
std::string operator()(NSString * CATCH_ARC_STRONG const& nsstring); static std::string convert(NSString * CATCH_ARC_STRONG const& nsstring);
}; };
template<> template<>
struct StringMaker<NSObject *> { struct StringMaker<NSObject *> {
std::string operator()(NSObject* const& nsObject); static std::string convert(NSObject* const& nsObject);
}; };
#endif #endif

View File

@ -76,7 +76,7 @@ std::string fpToString( T value, int precision ) {
// //
//// ======================================================= //// //// ======================================================= ////
std::string StringMaker<std::string>::operator()(const std::string& str) { std::string StringMaker<std::string>::convert(const std::string& str) {
if (!getCurrentContext().getConfig()->showInvisibles()) { if (!getCurrentContext().getConfig()->showInvisibles()) {
return '"' + str + '"'; return '"' + str + '"';
} }
@ -99,7 +99,7 @@ std::string StringMaker<std::string>::operator()(const std::string& str) {
return s; return s;
} }
std::string StringMaker<std::wstring>::operator()(const std::wstring& wstr) { std::string StringMaker<std::wstring>::convert(const std::wstring& wstr) {
std::string s; std::string s;
s.reserve(wstr.size()); s.reserve(wstr.size());
for (auto c : wstr) { for (auto c : wstr) {
@ -108,28 +108,28 @@ std::string StringMaker<std::wstring>::operator()(const std::wstring& wstr) {
return ::Catch::Detail::stringify(s); return ::Catch::Detail::stringify(s);
} }
std::string StringMaker<char const*>::operator()(char const* str) { std::string StringMaker<char const*>::convert(char const* str) {
if (str) { if (str) {
return ::Catch::Detail::stringify(std::string{ str }); return ::Catch::Detail::stringify(std::string{ str });
} else { } else {
return{ "{null string}" }; return{ "{null string}" };
} }
} }
std::string StringMaker<char*>::operator()(char* str) { std::string StringMaker<char*>::convert(char* str) {
if (str) { if (str) {
return ::Catch::Detail::stringify(std::string{ str }); return ::Catch::Detail::stringify(std::string{ str });
} else { } else {
return{ "{null string}" }; return{ "{null string}" };
} }
} }
std::string StringMaker<wchar_t const*>::operator()(wchar_t const * str) { std::string StringMaker<wchar_t const*>::convert(wchar_t const * str) {
if (str) { if (str) {
return ::Catch::Detail::stringify(std::wstring{ str }); return ::Catch::Detail::stringify(std::wstring{ str });
} else { } else {
return{ "{null string}" }; return{ "{null string}" };
} }
} }
std::string StringMaker<wchar_t *>::operator()(wchar_t * str) { std::string StringMaker<wchar_t *>::convert(wchar_t * str) {
if (str) { if (str) {
return ::Catch::Detail::stringify(std::wstring{ str }); return ::Catch::Detail::stringify(std::wstring{ str });
} else { } else {
@ -138,13 +138,13 @@ std::string StringMaker<wchar_t *>::operator()(wchar_t * str) {
} }
std::string StringMaker<int>::operator()(int value) { std::string StringMaker<int>::convert(int value) {
return ::Catch::Detail::stringify(static_cast<long long>(value)); return ::Catch::Detail::stringify(static_cast<long long>(value));
} }
std::string StringMaker<long>::operator()(long value) { std::string StringMaker<long>::convert(long value) {
return ::Catch::Detail::stringify(static_cast<long long>(value)); return ::Catch::Detail::stringify(static_cast<long long>(value));
} }
std::string StringMaker<long long>::operator()(long long value) { std::string StringMaker<long long>::convert(long long value) {
std::ostringstream oss; std::ostringstream oss;
oss << value; oss << value;
if (value > Detail::hexThreshold) { if (value > Detail::hexThreshold) {
@ -153,13 +153,13 @@ std::string StringMaker<long long>::operator()(long long value) {
return oss.str(); return oss.str();
} }
std::string StringMaker<unsigned int>::operator()(unsigned int value) { std::string StringMaker<unsigned int>::convert(unsigned int value) {
return ::Catch::Detail::stringify(static_cast<unsigned long long>(value)); return ::Catch::Detail::stringify(static_cast<unsigned long long>(value));
} }
std::string StringMaker<unsigned long>::operator()(unsigned long value) { std::string StringMaker<unsigned long>::convert(unsigned long value) {
return ::Catch::Detail::stringify(static_cast<unsigned long long>(value)); return ::Catch::Detail::stringify(static_cast<unsigned long long>(value));
} }
std::string StringMaker<unsigned long long>::operator()(unsigned long long value) { std::string StringMaker<unsigned long long>::convert(unsigned long long value) {
std::ostringstream oss; std::ostringstream oss;
oss << value; oss << value;
if (value > Detail::hexThreshold) { if (value > Detail::hexThreshold) {
@ -169,11 +169,11 @@ std::string StringMaker<unsigned long long>::operator()(unsigned long long value
} }
std::string StringMaker<bool>::operator()(bool b) { std::string StringMaker<bool>::convert(bool b) {
return b ? "true" : "false"; return b ? "true" : "false";
} }
std::string StringMaker<char>::operator()(char value) { std::string StringMaker<char>::convert(char value) {
if (value == '\r') { if (value == '\r') {
return "'\\r'"; return "'\\r'";
} else if (value == '\f') { } else if (value == '\f') {
@ -190,37 +190,37 @@ std::string StringMaker<char>::operator()(char value) {
return chstr; return chstr;
} }
} }
std::string StringMaker<signed char>::operator()(signed char c) { std::string StringMaker<signed char>::convert(signed char c) {
return ::Catch::Detail::stringify(static_cast<char>(c)); return ::Catch::Detail::stringify(static_cast<char>(c));
} }
std::string StringMaker<unsigned char>::operator()(unsigned char c) { std::string StringMaker<unsigned char>::convert(unsigned char c) {
return ::Catch::Detail::stringify(static_cast<char>(c)); return ::Catch::Detail::stringify(static_cast<char>(c));
} }
std::string StringMaker<std::nullptr_t>::operator()(std::nullptr_t) { std::string StringMaker<std::nullptr_t>::convert(std::nullptr_t) {
return "nullptr"; return "nullptr";
} }
std::string StringMaker<float>::operator()(float value) { std::string StringMaker<float>::convert(float value) {
return fpToString(value, 5) + 'f'; return fpToString(value, 5) + 'f';
} }
std::string StringMaker<double>::operator()(double value) { std::string StringMaker<double>::convert(double value) {
return fpToString(value, 10); return fpToString(value, 10);
} }
#ifdef __OBJC__ #ifdef __OBJC__
std::string StringMaker<NSString const *>::operator()(NSString const * const& nsstring) { std::string StringMaker<NSString const *>::convert(NSString const * const& nsstring) {
if (!nsstring) if (!nsstring)
return "nil"; return "nil";
return "@" + toString([nsstring UTF8String]); return "@" + toString([nsstring UTF8String]);
} }
std::string StringMaker<NSString * CATCH_ARC_STRONG>::operator()(NSString * CATCH_ARC_STRONG const& nsstring) { std::string StringMaker<NSString * CATCH_ARC_STRONG>::convert(NSString * CATCH_ARC_STRONG const& nsstring) {
if (!nsstring) if (!nsstring)
return "nil"; return "nil";
return "@" + toString([nsstring UTF8String]); return "@" + toString([nsstring UTF8String]);
} }
std::string StringMaker<NSObject *>::operator()(NSObject * const& nsObject) { std::string StringMaker<NSObject *>::convert(NSObject * const& nsObject) {
return ::Catch::Detail::stringify([nsObject description]); return ::Catch::Detail::stringify([nsObject description]);
} }
#endif #endif

View File

@ -22,13 +22,13 @@ std::ostream& operator<<(std::ostream& os, const has_maker_and_operator&) {
namespace Catch { namespace Catch {
template<> template<>
struct StringMaker<has_maker> { struct StringMaker<has_maker> {
std::string operator()( const has_maker& ) { static std::string convert( const has_maker& ) {
return "StringMaker<has_maker>"; return "StringMaker<has_maker>";
} }
}; };
template<> template<>
struct StringMaker<has_maker_and_operator> { struct StringMaker<has_maker_and_operator> {
std::string operator()( const has_maker_and_operator& ) { static std::string convert( const has_maker_and_operator& ) {
return "StringMaker<has_maker_and_operator>"; return "StringMaker<has_maker_and_operator>";
} }
}; };