CppUnit - The Unit Testing Library

CppUnit project page | FAQ

StringHelper.h
Go to the documentation of this file.
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
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
21template<typename T>
22 typename std::enable_if<!std::is_enum<T>::value, std::string>::type toString(const T& x)
23{
24 OStringStream ost;
25 ost << x;
26
27 return ost.str();
28}
29
30template<typename T>
31 typename std::enable_if<std::is_enum<T>::value, std::string>::type toString(const T& x)
32{
33 OStringStream ost;
34 ost << static_cast<typename std::underlying_type<T>::type>(x);
35
36 return ost.str();
37}
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
Portability.h
CPPUNIT_NS_END
#define CPPUNIT_NS_END
Definition Portability.h:106
CPPUNIT_NS_BEGIN
#define CPPUNIT_NS_BEGIN
Definition Portability.h:105
Stream.h
StringHelper
Methods for converting values to strings. Replaces CPPUNIT_NS::StringTools::toString.
Definition StringHelper.h:16
StringHelper::toString
std::enable_if<!std::is_enum< T >::value, std::string >::type toString(const T &x)
Definition StringHelper.h:22

| Send comments to: CppUnit Developers