Avoid out of line template definition

This commit is contained in:
Corentin Jabot 2017-12-31 18:35:55 +01:00
parent f580591bf8
commit f14e49ec77
2 changed files with 63 additions and 114 deletions

View File

@ -63,7 +63,6 @@ namespace Detail {
rss << std::setw(2) << static_cast<unsigned>(bytes[i]); rss << std::setw(2) << static_cast<unsigned>(bytes[i]);
return rss.str(); return rss.str();
} }
}
template<typename T> template<typename T>
@ -86,14 +85,13 @@ std::string fpToString( T value, int precision ) {
return d; return d;
} }
//// ======================================================= //// //// ======================================================= ////
// //
// Out-of-line defs for full specialization of StringMaker // Out-of-line defs for full specialization of StringMaker
// //
//// ======================================================= //// //// ======================================================= ////
std::string StringMaker<std::string>::convert(const std::string& str) { std::string convert(const std::string& str) {
if (!getCurrentContext().getConfig()->showInvisibles()) { if (!getCurrentContext().getConfig()->showInvisibles()) {
return '"' + str + '"'; return '"' + str + '"';
} }
@ -116,7 +114,7 @@ std::string StringMaker<std::string>::convert(const std::string& str) {
return s; return s;
} }
std::string StringMaker<std::wstring>::convert(const std::wstring& wstr) { std::string 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) {
@ -125,43 +123,41 @@ std::string StringMaker<std::wstring>::convert(const std::wstring& wstr) {
return ::Catch::Detail::stringify(s); return ::Catch::Detail::stringify(s);
} }
std::string StringMaker<char const*>::convert(char const* str) { std::string 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*>::convert(char* str) { std::string 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*>::convert(wchar_t const * str) { std::string 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 *>::convert(wchar_t * str) { std::string convert(wchar_t * 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 convert(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>::convert(long value) { std::string 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>::convert(long long value) { std::string convert(long long value) {
ReusableStringStream rss; ReusableStringStream rss;
rss << value; rss << value;
if (value > Detail::hexThreshold) { if (value > Detail::hexThreshold) {
@ -170,13 +166,13 @@ std::string StringMaker<long long>::convert(long long value) {
return rss.str(); return rss.str();
} }
std::string StringMaker<unsigned int>::convert(unsigned int value) { std::string 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>::convert(unsigned long value) { std::string 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>::convert(unsigned long long value) { std::string convert(unsigned long long value) {
ReusableStringStream rss; ReusableStringStream rss;
rss << value; rss << value;
if (value > Detail::hexThreshold) { if (value > Detail::hexThreshold) {
@ -186,11 +182,12 @@ std::string StringMaker<unsigned long long>::convert(unsigned long long value) {
} }
std::string StringMaker<bool>::convert(bool b) { std::string convert(bool b) {
return b ? "true" : "false"; return b ? "true" : "false";
} }
std::string StringMaker<char>::convert(char value) {
std::string convert(char value) {
if (value == '\r') { if (value == '\r') {
return "'\\r'"; return "'\\r'";
} else if (value == '\f') { } else if (value == '\f') {
@ -207,31 +204,25 @@ std::string StringMaker<char>::convert(char value) {
return chstr; return chstr;
} }
} }
std::string StringMaker<signed char>::convert(signed char c) { std::string 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>::convert(unsigned char c) { std::string 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>::convert(std::nullptr_t) { std::string convert(std::nullptr_t) {
return "nullptr"; return "nullptr";
} }
std::string StringMaker<float>::convert(float value) { std::string convert(float value) {
return fpToString(value, 5) + 'f'; return fpToString(value, 5) + 'f';
} }
std::string StringMaker<double>::convert(double value) { std::string convert(double value) {
return fpToString(value, 10); return fpToString(value, 10);
} }
std::string ratio_string<std::atto>::symbol() { return "a"; } }
std::string ratio_string<std::femto>::symbol() { return "f"; }
std::string ratio_string<std::pico>::symbol() { return "p"; }
std::string ratio_string<std::nano>::symbol() { return "n"; }
std::string ratio_string<std::micro>::symbol() { return "u"; }
std::string ratio_string<std::milli>::symbol() { return "m"; }
} // end namespace Catch } // end namespace Catch

View File

@ -37,6 +37,26 @@ namespace Catch {
extern const std::string unprintableString; extern const std::string unprintableString;
extern std::string convert(const std::string& str);
extern std::string convert(const std::wstring& wstr);
extern std::string convert(char const* str);
extern std::string convert(char* str);
extern std::string convert(wchar_t const * str);
extern std::string convert(wchar_t * str);
extern std::string convert(int value);
extern std::string convert(long value);
extern std::string convert(long long value);
extern std::string convert(unsigned int value);
extern std::string convert(unsigned long value);
extern std::string convert(unsigned long long value);
extern std::string convert(bool b);
extern std::string convert(char value);
extern std::string convert(signed char c);
extern std::string convert(unsigned char c);
extern std::string convert(std::nullptr_t);
extern std::string convert(float value);
extern std::string convert(double value);
std::string rawMemoryToString( const void *object, std::size_t size ); std::string rawMemoryToString( const void *object, std::size_t size );
template<typename T> template<typename T>
@ -44,6 +64,15 @@ namespace Catch {
return rawMemoryToString( &object, sizeof(object) ); return rawMemoryToString( &object, sizeof(object) );
} }
template <typename T, typename _ = void>
class HasConvertFunction : public std::false_type {};
template <typename T>
class HasConvertFunction<T,
decltype (Detail::convert(typename std::remove_reference<T>::type()))> : public std::true_type {};
template<typename T> template<typename T>
class IsStreamInsertable { class IsStreamInsertable {
template<typename SS, typename TT> template<typename SS, typename TT>
@ -54,7 +83,7 @@ namespace Catch {
static auto test(...)->std::false_type; static auto test(...)->std::false_type;
public: public:
static const bool value = decltype(test<std::ostream, const T&>(0))::value; static const bool value = decltype(test<std::ostream, const T&>(0))::value && !HasConvertFunction<T>::value;
}; };
} // namespace Detail } // namespace Detail
@ -77,6 +106,12 @@ namespace Catch {
convert(const Fake&) { convert(const Fake&) {
return Detail::unprintableString; return Detail::unprintableString;
} }
template <typename CT = T>
static
typename std::enable_if<Detail::HasConvertFunction<CT>::value, std::string>::type
convert(const CT& t) {
return Detail::convert(t);
}
}; };
namespace Detail { namespace Detail {
@ -93,32 +128,6 @@ namespace Catch {
// Some predefined specializations // Some predefined specializations
template<>
struct StringMaker<std::string> {
static std::string convert(const std::string& str);
};
template<>
struct StringMaker<std::wstring> {
static std::string convert(const std::wstring& wstr);
};
template<>
struct StringMaker<char const *> {
static std::string convert(char const * str);
};
template<>
struct StringMaker<char *> {
static std::string convert(char * str);
};
template<>
struct StringMaker<wchar_t const *> {
static std::string convert(wchar_t const * str);
};
template<>
struct StringMaker<wchar_t *> {
static std::string convert(wchar_t * str);
};
template<int SZ> template<int SZ>
struct StringMaker<char[SZ]> { struct StringMaker<char[SZ]> {
static std::string convert(const char* str) { static std::string convert(const char* str) {
@ -138,63 +147,6 @@ namespace Catch {
} }
}; };
template<>
struct StringMaker<int> {
static std::string convert(int value);
};
template<>
struct StringMaker<long> {
static std::string convert(long value);
};
template<>
struct StringMaker<long long> {
static std::string convert(long long value);
};
template<>
struct StringMaker<unsigned int> {
static std::string convert(unsigned int value);
};
template<>
struct StringMaker<unsigned long> {
static std::string convert(unsigned long value);
};
template<>
struct StringMaker<unsigned long long> {
static std::string convert(unsigned long long value);
};
template<>
struct StringMaker<bool> {
static std::string convert(bool b);
};
template<>
struct StringMaker<char> {
static std::string convert(char c);
};
template<>
struct StringMaker<signed char> {
static std::string convert(signed char c);
};
template<>
struct StringMaker<unsigned char> {
static std::string convert(unsigned char c);
};
template<>
struct StringMaker<std::nullptr_t> {
static std::string convert(std::nullptr_t);
};
template<>
struct StringMaker<float> {
static std::string convert(float value);
};
template<>
struct StringMaker<double> {
static std::string convert(double value);
};
template <typename T> template <typename T>
struct StringMaker<T*> { struct StringMaker<T*> {
template <typename U> template <typename U>
@ -460,6 +412,12 @@ struct ratio_string<std::milli> {
return std::string(timeStamp); return std::string(timeStamp);
} }
}; };
inline std::string ratio_string<std::atto>::symbol() { return "a"; }
inline std::string ratio_string<std::femto>::symbol() { return "f"; }
inline std::string ratio_string<std::pico>::symbol() { return "p"; }
inline std::string ratio_string<std::nano>::symbol() { return "n"; }
inline std::string ratio_string<std::micro>::symbol() { return "u"; }
inline std::string ratio_string<std::milli>::symbol() { return "m"; }
} }
#endif // CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER #endif // CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER