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