diff options
| author | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2016-12-16 13:37:29 +0100 |
|---|---|---|
| committer | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2016-12-16 14:05:07 +0100 |
| commit | 25ccf3dd39c283c54313d8d26374e493e0e5f1a6 (patch) | |
| tree | 84f33574340463e0e8a075c308b833adb1979bea /include/cppunit/tools/StringHelper.h | |
| parent | fcc0062e64edff6a64fb6d0ce9d8695b6373d1f2 (diff) | |
| download | cppunit-25ccf3dd39c283c54313d8d26374e493e0e5f1a6.tar.gz | |
extract the code to turn a variable into a string
Diffstat (limited to 'include/cppunit/tools/StringHelper.h')
| -rw-r--r-- | include/cppunit/tools/StringHelper.h | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/include/cppunit/tools/StringHelper.h b/include/cppunit/tools/StringHelper.h new file mode 100644 index 0000000..3301045 --- /dev/null +++ b/include/cppunit/tools/StringHelper.h @@ -0,0 +1,45 @@ +#ifndef CPPUNIT_TOOLS_STRINGHELPER_H +#define CPPUNIT_TOOLS_STRINGHELPER_H + +#include <cppunit/Portability.h> +#include <cppunit/portability/Stream.h> +#include <string> +#include <type_traits> + + +CPPUNIT_NS_BEGIN + + +/*! \brief Methods for converting values to strings. Replaces CPPUNIT_NS::StringTools::toString + */ +namespace StringHelper +{ + +// work around to handle C++11 enum class correctly. We need an own conversion to std::string +// as there is no implicit coversion to int for enum class. + +template<typename T> +typename std::enable_if<!std::is_enum<T>::value, std::string>::type toString(const T& x) +{ + OStringStream ost; + ost << x; + + return ost.str(); +} + +template<typename T> +typename std::enable_if<std::is_enum<T>::value, std::string>::type toString(const T& x) +{ + OStringStream ost; + ost << static_cast<typename std::underlying_type<T>::type>(x); + + return ost.str(); +} + +} + + +CPPUNIT_NS_END + +#endif // CPPUNIT_TOOLS_STRINGHELPER_H + |
