nlib
Printer.h
[詳解]
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 namespace detail_func {
20 
21 class NLIB_VIS_PUBLIC DummyStream NLIB_FINAL {
22  public:
23  DummyStream() : buf_(NULL), cur_(0), cap_(0) {}
24  ~DummyStream();
25  const char* c_str() const { return buf_ ? buf_ : ""; }
26 
27  public:
28  void swap(DummyStream& rhs) {
29  std::swap(buf_, rhs.buf_);
30  std::swap(cur_, rhs.cur_);
31  std::swap(cap_, rhs.cap_);
32  }
33  DummyStream& append(const char* str);
34 
35  DummyStream& operator<<(char c);
36  DummyStream& operator<<(signed char c) {
37  *this << static_cast<int>(c);
38  return *this;
39  }
40  DummyStream& operator<<(unsigned char c) {
41  *this << static_cast<int>(c);
42  return *this;
43  }
44  DummyStream& operator<<(wchar_t c);
45  DummyStream& operator<<(int value);
46  DummyStream& operator<<(unsigned int value);
47  DummyStream& operator<<(long value); // NOLINT
48  DummyStream& operator<<(unsigned long value); // NOLINT
49  DummyStream& operator<<(long long value); // NOLINT
50  DummyStream& operator<<(unsigned long long value); // NOLINT
51  DummyStream& operator<<(float value);
52  DummyStream& operator<<(double value);
53  DummyStream& operator<<(bool value);
54  DummyStream& operator<<(const char* str);
55  DummyStream& operator<<(const wchar_t* str);
56 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES
57  template <class DUMMY = void>
58 #endif
59  DummyStream& operator<<(const std::string& str) {
60  *this << str.c_str();
61  return *this;
62  }
63 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES
64  template <class DUMMY = void>
65 #endif
66  DummyStream& operator<<(const std::wstring& str) {
67  *this << str.c_str();
68  return *this;
69  }
70  template <class T>
71  DummyStream& operator<<(const T& rhs) {
72  PrintTo(rhs, this);
73  return *this;
74  }
75 
76  private:
77  char* buf_;
78  uint32_t cur_;
79  uint32_t cap_;
80  NLIB_DISALLOW_COPY_AND_ASSIGN(DummyStream);
81 };
82 
83 template <class T>
84 void PrintToArray(const T* a, size_t n, DummyStream* ostr) {
85  if (n == 0) {
86  *ostr << "{}";
87  } else {
88  *ostr << "{" << a[0];
89  size_t nn = (n > 8) ? 8 : n;
90  for (size_t i = 1; i < nn; ++i) {
91  *ostr << ", " << a[i];
92  }
93  if (nn == n) {
94  *ostr << "}";
95  } else {
96  *ostr << ", ...}";
97  }
98  }
99 }
100 
101 template <class T, size_t N>
102 inline void PrintTo(const T (&a)[N], DummyStream* ostr) {
103  PrintToArray(&a[0], N, ostr);
104 }
105 
106 template<size_t N>
107 inline void PrintTo(const char (&str)[N], DummyStream* ostr) {
108  *ostr << str;
109 }
110 
111 template<size_t N>
112 inline void PrintTo(const wchar_t (&str)[N], DummyStream* ostr) {
113  *ostr << str;
114 }
115 
116 template <class T, class Alloc>
117 inline void PrintTo(const std::vector<T, Alloc>& value, DummyStream* ostr) {
118  PrintToArray(&value[0], value.size(), ostr);
119 }
120 
121 template <class T1, class T2>
122 void PrintTo(const std::pair<T1, T2>& value, DummyStream* ostr) {
123  PrintTo("(", ostr);
124  PrintTo(value.first, ostr);
125  PrintTo(", ", ostr);
126  PrintTo(value.second, ostr);
127  PrintTo(")", ostr);
128 }
129 
130 template <class T>
131 inline void PrintTo(const T& value, DummyStream* ostr) {
132  NLIB_UNUSED(value);
133  *ostr << "?????";
134 }
135 
136 template <class T, class DUMMY>
137 inline void PrintTo(const T& value, DUMMY* ostr) {
138  // NOTE:
139  // DUMMY must be a template, not DummyStream.
140  // It is to use user-defined PrintTo(UserType, STREAM* ostr) prior to this function.
141  //
142  // Though you can write as follows using gtest:
143  // void PrintTo(const Bar& bar, std::ostream* os) { ... }
144  // using nlib::testing, we have to write like this:
145  // template<class OSTREAM> void PrintTo(const Bar& bar, OSTREAM* os) { ... }
146  *ostr << value;
147 }
148 
149 } // namespace detail_func
150 
151 template <class T>
152 inline std::string PrintToString(const T& value) {
153  // NOTE:
154  // we have to make nlib::testing work if we cannot use std::ostream.
155  ::nlib_ns::testing::detail_func::DummyStream str;
156 
157  // NOTE:
158  // ADL is performed, if PrintTo for type T is defined.
159  // otherwise, ::nlib_ns::testing::detail_func::PrintTo is used.
160  using ::nlib_ns::testing::detail_func::PrintTo;
161  PrintTo(value, &str);
162  return std::string(str.c_str());
163 }
164 
165 } // namespace testing
166 NLIB_NAMESPACE_END
167 
168 #if defined(_MSC_VER) && defined(nx_testing_EXPORTS)
169 #undef NLIB_VIS_PUBLIC
170 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
171 #endif
172 
173 #endif // INCLUDE_NN_NLIB_TESTING_PRINTER_H_
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:145
Definition: Base64.h:8
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:61
開発環境別の設定が書かれるファイルです。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:211