nlib
Printer.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_TESTING_PRINTER_H_
4 #define INCLUDE_NN_NLIB_TESTING_PRINTER_H_
5 
6 #include <string>
7 #include <utility>
8 #include <vector>
9 
10 #include "nn/nlib/Config.h"
11 
12 #if defined(_MSC_VER) && defined(nx_testing_EXPORTS)
13 #undef NLIB_VIS_PUBLIC
14 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
15 #endif
16 
17 NLIB_NAMESPACE_BEGIN
18 namespace testing {
19 
20 namespace detail_func {
21 
22 class DummyStream NLIB_FINAL {
23  public:
24  DummyStream() : m_Str() {}
25  void swap(std::string& rhs) { m_Str.swap(rhs); } // NOLINT
26  const std::string& str() const { return m_Str; }
27  std::string& str() { return m_Str; }
28 
29  public:
30  DummyStream& operator<<(int32_t value);
31  DummyStream& operator<<(uint32_t value);
32  DummyStream& operator<<(int64_t value);
33  DummyStream& operator<<(uint64_t value);
34  DummyStream& operator<<(float value);
35  DummyStream& operator<<(double value);
36  DummyStream& operator<<(bool value);
37  DummyStream& operator<<(const char* str);
38  DummyStream& operator<<(const wchar_t* str);
39  DummyStream& operator<<(const std::string& str);
40  DummyStream& operator<<(const std::wstring& str);
41  DummyStream& operator<<(char c);
42  DummyStream& operator<<(wchar_t c);
43  DummyStream& operator<<(unsigned char c);
44  DummyStream& operator<<(signed char c);
45  template <class T>
46  DummyStream& operator<<(const T& rhs) {
47  return Op(*this, rhs);
48  }
49 
50  private:
51  std::string m_Str;
52 };
53 
54 // PrintTo
55 NLIB_VIS_PUBLIC void PrintTo(int32_t value, DummyStream* ostr);
56 NLIB_VIS_PUBLIC void PrintTo(uint32_t x, DummyStream* ostr);
57 NLIB_VIS_PUBLIC void PrintTo(int64_t x, DummyStream* ostr);
58 NLIB_VIS_PUBLIC void PrintTo(uint64_t x, DummyStream* ostr);
59 NLIB_VIS_PUBLIC void PrintTo(float x, DummyStream* ostr);
60 NLIB_VIS_PUBLIC void PrintTo(double x, DummyStream* ostr);
61 NLIB_VIS_PUBLIC void PrintTo(bool x, DummyStream* ostr);
62 NLIB_VIS_PUBLIC void PrintTo(const char* str, DummyStream* ostr);
63 NLIB_VIS_PUBLIC void PrintTo(const wchar_t* str, DummyStream* ostr);
64 NLIB_VIS_PUBLIC void PrintTo(char c, DummyStream* ostr);
65 NLIB_VIS_PUBLIC void PrintTo(wchar_t c, DummyStream* ostr);
66 
67 inline void PrintTo(const std::string& str, DummyStream* ostr) { PrintTo(str.c_str(), ostr); }
68 
69 inline void PrintTo(const std::wstring& str, DummyStream* ostr) { PrintTo(str.c_str(), ostr); }
70 
71 inline void PrintTo(signed char c, DummyStream* ostr) { PrintTo(static_cast<char>(c), ostr); }
72 
73 inline void PrintTo(unsigned char c, DummyStream* ostr) { PrintTo(static_cast<char>(c), ostr); }
74 
75 template <class T>
76 void PrintToArray(const T* a, size_t n, DummyStream* ostr) {
77  if (n == 0) {
78  PrintTo("{}", ostr);
79  } else {
80  PrintTo("{", ostr);
81  PrintTo(a[0], ostr);
82  size_t nn = (n > 8) ? 8 : n;
83  for (size_t i = 1; i < nn; ++i) {
84  PrintTo(", ", ostr);
85  PrintTo(a[i], ostr);
86  }
87  PrintTo((nn == n) ? "}" : ", ...}", ostr);
88  }
89 }
90 
91 template <class T, size_t N>
92 inline void PrintTo(const T (&a)[N], DummyStream* ostr) {
93  PrintToArray(&a[0], N, ostr);
94 }
95 
96 template <class T, class Alloc>
97 inline void PrintTo(const std::vector<T, Alloc>& value, DummyStream* ostr) {
98  PrintToArray(&value[0], value.size(), ostr);
99 }
100 
101 template <class T1, class T2>
102 void PrintTo(const std::pair<T1, T2>& value, DummyStream* ostr) {
103  PrintTo("(", ostr);
104  PrintTo(value.first, ostr);
105  PrintTo(", ", ostr);
106  PrintTo(value.second, ostr);
107  PrintTo(")", ostr);
108 }
109 
110 // NOTE: Add overloads of PrintTo if needed.
111 
112 // DummyStream::operator<<
113 #define NLIB_DEF_DSOP_(tp) \
114 inline DummyStream& DummyStream::operator<<(tp x) { \
115  PrintTo(x, this); \
116  return *this; \
117 }
118 
119 NLIB_DEF_DSOP_(int32_t)
120 NLIB_DEF_DSOP_(uint32_t)
121 NLIB_DEF_DSOP_(int64_t)
122 NLIB_DEF_DSOP_(uint64_t)
123 NLIB_DEF_DSOP_(float) // NOLINT
124 NLIB_DEF_DSOP_(double) // NOLINT
125 NLIB_DEF_DSOP_(bool) // NOLINT
126 NLIB_DEF_DSOP_(const char*)
127 NLIB_DEF_DSOP_(const wchar_t*)
128 NLIB_DEF_DSOP_(const std::string&)
129 NLIB_DEF_DSOP_(const std::wstring&)
130 NLIB_DEF_DSOP_(char) // NOLINT
131 NLIB_DEF_DSOP_(wchar_t)
132 NLIB_DEF_DSOP_(signed char)
133 NLIB_DEF_DSOP_(unsigned char)
134 
135 #undef NLIB_DEF_DSOP_
136 
137 #define NLIB_DEF_FUNCOP_(tp, coerce_tp) \
138 inline DummyStream& Op(DummyStream& os, tp x) { \
139  PrintTo(static_cast<coerce_tp>(x), &os); \
140  return os; \
141  } // NOLINT
142 NLIB_DEF_FUNCOP_(int, int32_t)
143 NLIB_DEF_FUNCOP_(unsigned int, uint32_t)
144 NLIB_DEF_FUNCOP_(short, int32_t) // NOLINT
145 NLIB_DEF_FUNCOP_(unsigned short, uint32_t) // NOLINT
146 NLIB_DEF_FUNCOP_(long, int32_t) // NOLINT
147 NLIB_DEF_FUNCOP_(unsigned long, uint32_t) // NOLINT
148 NLIB_DEF_FUNCOP_(long long, int32_t) // NOLINT
149 NLIB_DEF_FUNCOP_(unsigned long long, uint32_t) // NOLINT
150 #undef NLIB_DEF_FUNCOP_
151 
152 template <class T>
153 inline DummyStream& Op(DummyStream& ostr, const T& x) { // NOLINT
154  // This is the last(default) function.....
155  NLIB_UNUSED(x);
156  // TODO(nishida_kenji): Print byte sequence of x?
157  ostr.str().append("?????");
158  return ostr;
159 }
160 
161 template <class T, class DUMMY>
162 inline void PrintTo(const T& value, DUMMY* ostr) {
163  // NOTE:
164  // DUMMY must be a template, not DummyStream.
165  // It is to use user-defined PrintTo(UserType, STREAM* ostr) prior to this function.
166  //
167  // Though you can write as follows using gtest:
168  // void PrintTo(const Bar& bar, std::ostream* os) { ... }
169  // using nlib::testing, we have to write like this:
170  // template<class OSTREAM> void PrintTo(const Bar& bar, OSTREAM* os) { ... }
171  *ostr << value;
172 }
173 
174 } // namespace detail_func
175 
176 template <class T>
177 inline NLIB_VIS_HIDDEN std::string PrintToString(const T& value) {
178  // NOTE:
179  // we have to make nlib::testing work if we cannot use std::ostream.
180  ::nlib_ns::testing::detail_func::DummyStream str;
181 
182  // NOTE:
183  // ADL is performed, if PrintTo for type T is defined.
184  // otherwise, ::nlib_ns::testing::detail_func::PrintTo is used.
185  using ::nlib_ns::testing::detail_func::PrintTo;
186  PrintTo(value, &str);
187  return str.str();
188 }
189 
190 } // namespace testing
191 NLIB_NAMESPACE_END
192 
193 #if defined(_MSC_VER) && defined(nx_testing_EXPORTS)
194 #undef NLIB_VIS_PUBLIC
195 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
196 #endif
197 
198 #endif // INCLUDE_NN_NLIB_TESTING_PRINTER_H_
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:50
STL namespace.
A file that contains the configuration information for each development environment.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51