Line data Source code
1 : #ifndef CPPUNIT_TOOLS_STRINGHELPER_H 2 : #define CPPUNIT_TOOLS_STRINGHELPER_H 3 : 4 : #include <cppunit/Portability.h> 5 : #include <cppunit/portability/Stream.h> 6 : #include <string> 7 : #include <type_traits> 8 : 9 : 10 : CPPUNIT_NS_BEGIN 11 : 12 : 13 : /*! \brief Methods for converting values to strings. Replaces CPPUNIT_NS::StringTools::toString 14 : */ 15 : namespace StringHelper 16 : { 17 : 18 : // work around to handle C++11 enum class correctly. We need an own conversion to std::string 19 : // as there is no implicit coversion to int for enum class. 20 : 21 : template<typename T> 22 2 : typename std::enable_if<!std::is_enum<T>::value, std::string>::type toString(const T& x) 23 : { 24 2 : OStringStream ost; 25 2 : ost << x; 26 : 27 4 : return ost.str(); 28 2 : } 29 : 30 : template<typename T> 31 0 : typename std::enable_if<std::is_enum<T>::value, std::string>::type toString(const T& x) 32 : { 33 0 : OStringStream ost; 34 0 : ost << static_cast<typename std::underlying_type<T>::type>(x); 35 : 36 0 : return ost.str(); 37 0 : } 38 : 39 : template<> inline std::string toString(const signed char& x) 40 : { 41 : return toString(static_cast<int>(x)); 42 : } 43 : 44 : template<> inline std::string toString(const unsigned char& x) 45 : { 46 : return toString(static_cast<unsigned int>(x)); 47 : } 48 : 49 : } 50 : 51 : 52 : CPPUNIT_NS_END 53 : 54 : #endif // CPPUNIT_TOOLS_STRINGHELPER_H 55 :