nlib
Printer.h
[詳解]
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_TESTING_PRINTER_H_
17 #define INCLUDE_NN_NLIB_TESTING_PRINTER_H_
18 
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 #include "nn/nlib/Config.h"
24 
25 #if defined(_MSC_VER) && defined(nx_testing_EXPORTS)
26 #undef NLIB_VIS_PUBLIC
27 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
28 #endif
29 
30 NLIB_NAMESPACE_BEGIN
31 namespace testing {
32 namespace detail_func {
33 
34 class NLIB_VIS_PUBLIC DummyStream NLIB_FINAL {
35  public:
36  DummyStream() : buf_(nullptr), cur_(0U), cap_(0U) {}
37  ~DummyStream();
38  const char* c_str() const { return buf_ ? buf_ : ""; }
39 #ifdef __cpp_rvalue_references
40  DummyStream(DummyStream&& rhs) NLIB_NOEXCEPT : buf_(rhs.buf_), cur_(rhs.cur_), cap_(rhs.cap_) {
41  rhs.buf_ = nullptr;
42  rhs.cur_ = rhs.cap_ = 0U;
43  }
44  DummyStream& operator=(DummyStream&& rhs) NLIB_NOEXCEPT;
45 #endif
46  DummyStream(DummyStream& rhs, move_tag) NLIB_NOEXCEPT : buf_(rhs.buf_),
47  cur_(rhs.cur_),
48  cap_(rhs.cap_) {
49  rhs.buf_ = nullptr;
50  rhs.cur_ = rhs.cap_ = 0U;
51  }
52  DummyStream& assign(DummyStream& rhs, move_tag) NLIB_NOEXCEPT;
53 
54  public:
55  DummyStream& append(const char* str);
56 
57  DummyStream& operator<<(char c);
58  DummyStream& operator<<(signed char c) {
59  *this << static_cast<int>(c);
60  return *this;
61  }
62  DummyStream& operator<<(unsigned char c) {
63  *this << static_cast<int>(c);
64  return *this;
65  }
66  DummyStream& operator<<(wchar_t c);
67  DummyStream& operator<<(int value);
68  DummyStream& operator<<(unsigned int value);
69  DummyStream& operator<<(long value);
70  DummyStream& operator<<(unsigned long value);
71  DummyStream& operator<<(long long value);
72  DummyStream& operator<<(unsigned long long value);
73  DummyStream& operator<<(float value);
74  DummyStream& operator<<(double value);
75  DummyStream& operator<<(bool value);
76  DummyStream& operator<<(const char* str);
77  DummyStream& operator<<(const wchar_t* str);
78  DummyStream& operator<<(const nlib_utf16_t* str);
79  DummyStream& operator<<(const nlib_utf32_t* str);
80 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES
81  template<class DUMMY = void>
82 #endif
83  DummyStream& operator<<(const std::string& str) {
84  *this << str.c_str();
85  return *this;
86  }
87 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES
88  template<class DUMMY = void>
89 #endif
90  DummyStream& operator<<(const std::wstring& str) {
91  *this << str.c_str();
92  return *this;
93  }
94  template<class T>
95  DummyStream& operator<<(const T& rhs) {
96  PrintTo(rhs, this);
97  return *this;
98  }
99 
100  private:
101  char* buf_;
102  uint32_t cur_;
103  uint32_t cap_;
104  NLIB_DISALLOW_COPY_AND_ASSIGN(DummyStream);
105 };
106 
107 template<class T>
108 void PrintToArray(const T* a, size_t n, DummyStream* ostr) {
109  if (n == 0) {
110  *ostr << "{}";
111  } else {
112  *ostr << "{" << a[0];
113  size_t nn = (n > 8) ? 8 : n;
114  for (size_t i = 1; i < nn; ++i) {
115  *ostr << ", " << a[i];
116  }
117  if (nn == n) {
118  *ostr << "}";
119  } else {
120  *ostr << ", ...}";
121  }
122  }
123 }
124 
125 template<class T, size_t N>
126 inline void PrintTo(const T (&a)[N], DummyStream* ostr) {
127  PrintToArray(&a[0], N, ostr);
128 }
129 
130 template<size_t N>
131 inline void PrintTo(const char (&str)[N], DummyStream* ostr) {
132  *ostr << str;
133 }
134 
135 template<size_t N>
136 inline void PrintTo(const wchar_t (&str)[N], DummyStream* ostr) {
137  *ostr << str;
138 }
139 
140 template<class T, class Alloc>
141 inline void PrintTo(const std::vector<T, Alloc>& value, DummyStream* ostr) {
142  PrintToArray(&value[0], value.size(), ostr);
143 }
144 
145 template<class T1, class T2>
146 void PrintTo(const std::pair<T1, T2>& value, DummyStream* ostr) {
147  PrintTo("(", ostr);
148  PrintTo(value.first, ostr);
149  PrintTo(", ", ostr);
150  PrintTo(value.second, ostr);
151  PrintTo(")", ostr);
152 }
153 
154 template<class T>
155 inline void PrintTo(const T& value, DummyStream* ostr) {
156  NLIB_UNUSED(value);
157  *ostr << "?????";
158 }
159 
160 template<class T, class DUMMY>
161 inline void PrintTo(const T& value, DUMMY* ostr) {
162  // NOTE:
163  // DUMMY must be a template, not DummyStream.
164  // It is to use user-defined PrintTo(UserType, STREAM* ostr) prior to this function.
165  //
166  // Though you can write as follows using gtest:
167  // void PrintTo(const Bar& bar, std::ostream* os) { ... }
168  // using nlib::testing, we have to write like this:
169  // template<class OSTREAM> void PrintTo(const Bar& bar, OSTREAM* os) { ... }
170  *ostr << value;
171 }
172 
173 } // namespace detail_func
174 
175 template<class T>
176 inline std::string PrintToString(const T& value) {
177  // NOTE:
178  // we have to make nlib::testing work if we cannot use std::ostream.
179  ::nlib_ns::testing::detail_func::DummyStream str;
180 
181  // NOTE:
182  // ADL is performed, if PrintTo for type T is defined.
183  // otherwise, ::nlib_ns::testing::detail_func::PrintTo is used.
184  using ::nlib_ns::testing::detail_func::PrintTo;
185  PrintTo(value, &str);
186  return std::string(str.c_str());
187 }
188 
189 } // namespace testing
190 NLIB_NAMESPACE_END
191 
192 #if defined(_MSC_VER) && defined(nx_testing_EXPORTS)
193 #undef NLIB_VIS_PUBLIC
194 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
195 #endif
196 
197 #endif // INCLUDE_NN_NLIB_TESTING_PRINTER_H_
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:183
Definition: Base64.h:25
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:87
uint32_t nlib_utf32_t
char32_tが利用できる場合はchar32_tに、そうでない場合はuint32_tにtypedefされます。 ...
Definition: Platform.h:289
uint16_t nlib_utf16_t
char16_tが利用できる場合はchar16_tに、そうでない場合はuint16_tにtypedefされます。 ...
Definition: Platform.h:288
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:109
開発環境別の設定が書かれるファイルです。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:250