nlib
Printer.h
Go to the documentation of this file.
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
41  : buf_(rhs.buf_), cur_(rhs.cur_), cap_(rhs.cap_) {
42  rhs.buf_ = nullptr;
43  rhs.cur_ = rhs.cap_ = 0U;
44  }
45  DummyStream& operator=(DummyStream&& rhs) NLIB_NOEXCEPT;
46 #endif
47  DummyStream(DummyStream& rhs, move_tag) NLIB_NOEXCEPT // NOLINT
48  : buf_(rhs.buf_), cur_(rhs.cur_), cap_(rhs.cap_) {
49  rhs.buf_ = nullptr;
50  rhs.cur_ = rhs.cap_ = 0U;
51  }
52  DummyStream& assign(DummyStream& rhs, move_tag) NLIB_NOEXCEPT; // NOLINT
53 
54  public:
55  NLIB_DEPRECATED void swap(DummyStream& rhs) {
56  std::swap(buf_, rhs.buf_);
57  std::swap(cur_, rhs.cur_);
58  std::swap(cap_, rhs.cap_);
59  }
60  DummyStream& append(const char* str);
61 
62  DummyStream& operator<<(char c);
63  DummyStream& operator<<(signed char c) {
64  *this << static_cast<int>(c);
65  return *this;
66  }
67  DummyStream& operator<<(unsigned char c) {
68  *this << static_cast<int>(c);
69  return *this;
70  }
71  DummyStream& operator<<(wchar_t c);
72  DummyStream& operator<<(int value);
73  DummyStream& operator<<(unsigned int value);
74  DummyStream& operator<<(long value); // NOLINT
75  DummyStream& operator<<(unsigned long value); // NOLINT
76  DummyStream& operator<<(long long value); // NOLINT
77  DummyStream& operator<<(unsigned long long value); // NOLINT
78  DummyStream& operator<<(float value);
79  DummyStream& operator<<(double value);
80  DummyStream& operator<<(bool value);
81  DummyStream& operator<<(const char* str);
82  DummyStream& operator<<(const wchar_t* str);
83 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES
84  template <class DUMMY = void>
85 #endif
86  DummyStream& operator<<(const std::string& str) {
87  *this << str.c_str();
88  return *this;
89  }
90 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES
91  template <class DUMMY = void>
92 #endif
93  DummyStream& operator<<(const std::wstring& str) {
94  *this << str.c_str();
95  return *this;
96  }
97  template <class T>
98  DummyStream& operator<<(const T& rhs) {
99  PrintTo(rhs, this);
100  return *this;
101  }
102 
103  private:
104  char* buf_;
105  uint32_t cur_;
106  uint32_t cap_;
107  NLIB_DISALLOW_COPY_AND_ASSIGN(DummyStream);
108 };
109 
110 template <class T>
111 void PrintToArray(const T* a, size_t n, DummyStream* ostr) {
112  if (n == 0) {
113  *ostr << "{}";
114  } else {
115  *ostr << "{" << a[0];
116  size_t nn = (n > 8) ? 8 : n;
117  for (size_t i = 1; i < nn; ++i) {
118  *ostr << ", " << a[i];
119  }
120  if (nn == n) {
121  *ostr << "}";
122  } else {
123  *ostr << ", ...}";
124  }
125  }
126 }
127 
128 template <class T, size_t N>
129 inline void PrintTo(const T (&a)[N], DummyStream* ostr) {
130  PrintToArray(&a[0], N, ostr);
131 }
132 
133 template<size_t N>
134 inline void PrintTo(const char (&str)[N], DummyStream* ostr) {
135  *ostr << str;
136 }
137 
138 template<size_t N>
139 inline void PrintTo(const wchar_t (&str)[N], DummyStream* ostr) {
140  *ostr << str;
141 }
142 
143 template <class T, class Alloc>
144 inline void PrintTo(const std::vector<T, Alloc>& value, DummyStream* ostr) {
145  PrintToArray(&value[0], value.size(), ostr);
146 }
147 
148 template <class T1, class T2>
149 void PrintTo(const std::pair<T1, T2>& value, DummyStream* ostr) {
150  PrintTo("(", ostr);
151  PrintTo(value.first, ostr);
152  PrintTo(", ", ostr);
153  PrintTo(value.second, ostr);
154  PrintTo(")", ostr);
155 }
156 
157 template <class T>
158 inline void PrintTo(const T& value, DummyStream* ostr) {
159  NLIB_UNUSED(value);
160  *ostr << "?????";
161 }
162 
163 template <class T, class DUMMY>
164 inline void PrintTo(const T& value, DUMMY* ostr) {
165  // NOTE:
166  // DUMMY must be a template, not DummyStream.
167  // It is to use user-defined PrintTo(UserType, STREAM* ostr) prior to this function.
168  //
169  // Though you can write as follows using gtest:
170  // void PrintTo(const Bar& bar, std::ostream* os) { ... }
171  // using nlib::testing, we have to write like this:
172  // template<class OSTREAM> void PrintTo(const Bar& bar, OSTREAM* os) { ... }
173  *ostr << value;
174 }
175 
176 } // namespace detail_func
177 
178 template <class T>
179 inline std::string PrintToString(const T& value) {
180  // NOTE:
181  // we have to make nlib::testing work if we cannot use std::ostream.
182  ::nlib_ns::testing::detail_func::DummyStream str;
183 
184  // NOTE:
185  // ADL is performed, if PrintTo for type T is defined.
186  // otherwise, ::nlib_ns::testing::detail_func::PrintTo is used.
187  using ::nlib_ns::testing::detail_func::PrintTo;
188  PrintTo(value, &str);
189  return std::string(str.c_str());
190 }
191 
192 } // namespace testing
193 NLIB_NAMESPACE_END
194 
195 #if defined(_MSC_VER) && defined(nx_testing_EXPORTS)
196 #undef NLIB_VIS_PUBLIC
197 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
198 #endif
199 
200 #endif // INCLUDE_NN_NLIB_TESTING_PRINTER_H_
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:179
Definition: Base64.h:25
#define NLIB_DEPRECATED
Indicates that a function or something has been deprecated.
Definition: Config.h:109
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:89
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:105
A file that contains the configuration information for each development environment.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:245