gtest/include/gtest/gtest-message.h
Go to the documentation of this file.
1 // Copyright 2005, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: wan@google.com (Zhanyong Wan)
31 //
32 // The Google C++ Testing Framework (Google Test)
33 //
34 // This header file defines the Message class.
35 //
36 // IMPORTANT NOTE: Due to limitation of the C++ language, we have to
37 // leave some internal implementation details in this header file.
38 // They are clearly marked by comments like this:
39 //
40 // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
41 //
42 // Such code is NOT meant to be used by a user directly, and is subject
43 // to CHANGE WITHOUT NOTICE. Therefore DO NOT DEPEND ON IT in a user
44 // program!
45 
46 #ifndef GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
47 #define GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
48 
49 #include <limits>
50 
51 #include "gtest/internal/gtest-port.h"
52 
53 // Ensures that there is at least one operator<< in the global namespace.
54 // See Message& operator<<(...) below for why.
55 void operator<<(const testing::internal::Secret &, int);
56 
57 namespace testing
58 {
59 
60 // The Message class works like an ostream repeater.
61 //
62 // Typical usage:
63 //
64 // 1. You stream a bunch of values to a Message object.
65 // It will remember the text in a stringstream.
66 // 2. Then you stream the Message object to an ostream.
67 // This causes the text in the Message to be streamed
68 // to the ostream.
69 //
70 // For example;
71 //
72 // testing::Message foo;
73 // foo << 1 << " != " << 2;
74 // std::cout << foo;
75 //
76 // will print "1 != 2".
77 //
78 // Message is not intended to be inherited from. In particular, its
79 // destructor is not virtual.
80 //
81 // Note that stringstream behaves differently in gcc and in MSVC. You
82 // can stream a NULL char pointer to it in the former, but not in the
83 // latter (it causes an access violation if you do). The Message
84 // class hides this difference by treating a NULL char pointer as
85 // "(null)".
86 class GTEST_API_ Message
87 {
88 private:
89  // The type of basic IO manipulators (endl, ends, and flush) for
90  // narrow streams.
91  typedef std::ostream & (*BasicNarrowIoManip)(std::ostream &);
92 
93 public:
94  // Constructs an empty Message.
95  Message();
96 
97  // Copy constructor.
98  Message(const Message & msg) : ss_(new ::std::stringstream) // NOLINT
99  {
100  *ss_ << msg.GetString();
101  }
102 
103  // Constructs a Message from a C-string.
104  explicit Message(const char * str) : ss_(new ::std::stringstream)
105  {
106  *ss_ << str;
107  }
108 
109 #if GTEST_OS_SYMBIAN
110  // Streams a value (either a pointer or not) to this object.
111  template <typename T>
112  inline Message & operator <<(const T & value)
113  {
114  StreamHelper(typename internal::is_pointer<T>::type(), value);
115  return *this;
116  }
117 #else
118  // Streams a non-pointer value to this object.
119  template <typename T>
120  inline Message & operator <<(const T & val)
121  {
122  // Some libraries overload << for STL containers. These
123  // overloads are defined in the global namespace instead of ::std.
124  //
125  // C++'s symbol lookup rule (i.e. Koenig lookup) says that these
126  // overloads are visible in either the std namespace or the global
127  // namespace, but not other namespaces, including the testing
128  // namespace which Google Test's Message class is in.
129  //
130  // To allow STL containers (and other types that has a << operator
131  // defined in the global namespace) to be used in Google Test
132  // assertions, testing::Message must access the custom << operator
133  // from the global namespace. With this using declaration,
134  // overloads of << defined in the global namespace and those
135  // visible via Koenig lookup are both exposed in this function.
136  using ::operator <<;
137  *ss_ << val;
138  return *this;
139  }
140 
141  // Streams a pointer value to this object.
142  //
143  // This function is an overload of the previous one. When you
144  // stream a pointer to a Message, this definition will be used as it
145  // is more specialized. (The C++ Standard, section
146  // [temp.func.order].) If you stream a non-pointer, then the
147  // previous definition will be used.
148  //
149  // The reason for this overload is that streaming a NULL pointer to
150  // ostream is undefined behavior. Depending on the compiler, you
151  // may get "0", "(nil)", "(null)", or an access violation. To
152  // ensure consistent result across compilers, we always treat NULL
153  // as "(null)".
154  template <typename T>
155  inline Message & operator <<(T * const & pointer) // NOLINT
156  {
157  if (pointer == NULL)
158  {
159  *ss_ << "(null)";
160  }
161 
162  else
163  {
164  *ss_ << pointer;
165  }
166 
167  return *this;
168  }
169 #endif // GTEST_OS_SYMBIAN
170 
171  // Since the basic IO manipulators are overloaded for both narrow
172  // and wide streams, we have to provide this specialized definition
173  // of operator <<, even though its body is the same as the
174  // templatized version above. Without this definition, streaming
175  // endl or other basic IO manipulators to Message will confuse the
176  // compiler.
177  Message & operator <<(BasicNarrowIoManip val)
178  {
179  *ss_ << val;
180  return *this;
181  }
182 
183  // Instead of 1/0, we want to see true/false for bool values.
185  {
186  return *this << (b ? "true" : "false");
187  }
188 
189  // These two overloads allow streaming a wide C string to a Message
190  // using the UTF-8 encoding.
191  Message & operator <<(const wchar_t * wide_c_str);
192  Message & operator <<(wchar_t * wide_c_str);
193 
194 #if GTEST_HAS_STD_WSTRING
195  // Converts the given wide string to a narrow string using the UTF-8
196  // encoding, and streams the result to this Message object.
198 #endif // GTEST_HAS_STD_WSTRING
199 
200 #if GTEST_HAS_GLOBAL_WSTRING
201  // Converts the given wide string to a narrow string using the UTF-8
202  // encoding, and streams the result to this Message object.
204 #endif // GTEST_HAS_GLOBAL_WSTRING
205 
206  // Gets the text streamed to this object so far as an std::string.
207  // Each '\0' character in the buffer is replaced with "\\0".
208  //
209  // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
210  std::string GetString() const;
211 
212 private:
213 
214 #if GTEST_OS_SYMBIAN
215  // These are needed as the Nokia Symbian Compiler cannot decide between
216  // const T& and const T* in a function template. The Nokia compiler _can_
217  // decide between class template specializations for T and T*, so a
218  // tr1::type_traits-like is_pointer works, and we can overload on that.
219  template <typename T>
220  inline void StreamHelper(internal::true_type /*is_pointer*/, T * pointer)
221  {
222  if (pointer == NULL)
223  {
224  *ss_ << "(null)";
225  }
226 
227  else
228  {
229  *ss_ << pointer;
230  }
231  }
232  template <typename T>
233  inline void StreamHelper(internal::false_type /*is_pointer*/,
234  const T & value)
235  {
236  // See the comments in Message& operator <<(const T&) above for why
237  // we need this using statement.
238  using ::operator <<;
239  *ss_ << value;
240  }
241 #endif // GTEST_OS_SYMBIAN
242 
243  // We'll hold the text streamed to this object here.
245 
246  // We declare (but don't implement) this to prevent the compiler
247  // from implementing the assignment operator.
248  void operator=(const Message &);
249 };
250 
251 // Streams a Message to an ostream.
252 inline std::ostream & operator <<(std::ostream & os, const Message & sb)
253 {
254  return os << sb.GetString();
255 }
256 
257 namespace internal
258 {
259 
260 // Converts a streamable value to an std::string. A NULL pointer is
261 // converted to "(null)". When the input value is a ::string,
262 // ::std::string, ::wstring, or ::std::wstring object, each NUL
263 // character in it is replaced with "\\0".
264 template <typename T>
265 std::string StreamableToString(const T & streamable)
266 {
267  return (Message() << streamable).GetString();
268 }
269 
270 } // namespace internal
271 } // namespace testing
272 
273 #endif // GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
#define GTEST_API_
const char Message[]
Definition: strings.h:102
std::ostream & operator<<(std::ostream &os, const TestPartResult &result)
std::string StreamableToString(const T &streamable)
std::string GetString() const
void operator<<(const testing::internal::Secret &, int)


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:12