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 // The Message class works like an ostream repeater.
60 //
61 // Typical usage:
62 //
63 // 1. You stream a bunch of values to a Message object.
64 // It will remember the text in a stringstream.
65 // 2. Then you stream the Message object to an ostream.
66 // This causes the text in the Message to be streamed
67 // to the ostream.
68 //
69 // For example;
70 //
71 // testing::Message foo;
72 // foo << 1 << " != " << 2;
73 // std::cout << foo;
74 //
75 // will print "1 != 2".
76 //
77 // Message is not intended to be inherited from. In particular, its
78 // destructor is not virtual.
79 //
80 // Note that stringstream behaves differently in gcc and in MSVC. You
81 // can stream a NULL char pointer to it in the former, but not in the
82 // latter (it causes an access violation if you do). The Message
83 // class hides this difference by treating a NULL char pointer as
84 // "(null)".
85 class GTEST_API_ Message
86 {
87 private:
88 // The type of basic IO manipulators (endl, ends, and flush) for
89 // narrow streams.
90 typedef std::ostream& (*BasicNarrowIoManip)(std::ostream&);
91
92 public:
93 // Constructs an empty Message.
94 Message();
95
96 // Copy constructor.
Message(const Message & msg)97 Message(const Message& msg) : ss_(new ::std::stringstream)
98 { // NOLINT
99 *ss_ << msg.GetString();
100 }
101
102 // Constructs a Message from a C-string.
Message(const char * str)103 explicit Message(const char* str) : ss_(new ::std::stringstream)
104 {
105 *ss_ << str;
106 }
107
108 #if GTEST_OS_SYMBIAN
109 // Streams a value (either a pointer or not) to this object.
110 template <typename T>
111 inline Message& operator<<(const T& value)
112 {
113 StreamHelper(typename internal::is_pointer<T>::type(), value);
114 return *this;
115 }
116 #else
117 // Streams a non-pointer value to this object.
118 template <typename T>
119 inline Message& operator<<(const T& val)
120 {
121 // Some libraries overload << for STL containers. These
122 // overloads are defined in the global namespace instead of ::std.
123 //
124 // C++'s symbol lookup rule (i.e. Koenig lookup) says that these
125 // overloads are visible in either the std namespace or the global
126 // namespace, but not other namespaces, including the testing
127 // namespace which Google Test's Message class is in.
128 //
129 // To allow STL containers (and other types that has a << operator
130 // defined in the global namespace) to be used in Google Test
131 // assertions, testing::Message must access the custom << operator
132 // from the global namespace. With this using declaration,
133 // overloads of << defined in the global namespace and those
134 // visible via Koenig lookup are both exposed in this function.
135 using ::operator<<;
136 *ss_ << val;
137 return *this;
138 }
139
140 // Streams a pointer value to this object.
141 //
142 // This function is an overload of the previous one. When you
143 // stream a pointer to a Message, this definition will be used as it
144 // is more specialized. (The C++ Standard, section
145 // [temp.func.order].) If you stream a non-pointer, then the
146 // previous definition will be used.
147 //
148 // The reason for this overload is that streaming a NULL pointer to
149 // ostream is undefined behavior. Depending on the compiler, you
150 // may get "0", "(nil)", "(null)", or an access violation. To
151 // ensure consistent result across compilers, we always treat NULL
152 // as "(null)".
153 template <typename T>
154 inline Message& operator<<(T* const& pointer)
155 { // NOLINT
156 if (pointer == NULL)
157 {
158 *ss_ << "(null)";
159 }
160 else
161 {
162 *ss_ << pointer;
163 }
164 return *this;
165 }
166 #endif // GTEST_OS_SYMBIAN
167
168 // Since the basic IO manipulators are overloaded for both narrow
169 // and wide streams, we have to provide this specialized definition
170 // of operator <<, even though its body is the same as the
171 // templatized version above. Without this definition, streaming
172 // endl or other basic IO manipulators to Message will confuse the
173 // compiler.
174 Message& operator<<(BasicNarrowIoManip val)
175 {
176 *ss_ << val;
177 return *this;
178 }
179
180 // Instead of 1/0, we want to see true/false for bool values.
181 Message& operator<<(bool b)
182 {
183 return *this << (b ? "true" : "false");
184 }
185
186 // These two overloads allow streaming a wide C string to a Message
187 // using the UTF-8 encoding.
188 Message& operator<<(const wchar_t* wide_c_str);
189 Message& operator<<(wchar_t* wide_c_str);
190
191 #if GTEST_HAS_STD_WSTRING
192 // Converts the given wide string to a narrow string using the UTF-8
193 // encoding, and streams the result to this Message object.
194 Message& operator<<(const ::std::wstring& wstr);
195 #endif // GTEST_HAS_STD_WSTRING
196
197 #if GTEST_HAS_GLOBAL_WSTRING
198 // Converts the given wide string to a narrow string using the UTF-8
199 // encoding, and streams the result to this Message object.
200 Message& operator<<(const ::wstring& wstr);
201 #endif // GTEST_HAS_GLOBAL_WSTRING
202
203 // Gets the text streamed to this object so far as an std::string.
204 // Each '\0' character in the buffer is replaced with "\\0".
205 //
206 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
207 std::string GetString() const;
208
209 private:
210 #if GTEST_OS_SYMBIAN
211 // These are needed as the Nokia Symbian Compiler cannot decide between
212 // const T& and const T* in a function template. The Nokia compiler _can_
213 // decide between class template specializations for T and T*, so a
214 // tr1::type_traits-like is_pointer works, and we can overload on that.
215 template <typename T>
StreamHelper(internal::true_type,T * pointer)216 inline void StreamHelper(internal::true_type /*is_pointer*/, T* pointer)
217 {
218 if (pointer == NULL)
219 {
220 *ss_ << "(null)";
221 }
222 else
223 {
224 *ss_ << pointer;
225 }
226 }
227 template <typename T>
StreamHelper(internal::false_type,const T & value)228 inline void StreamHelper(internal::false_type /*is_pointer*/,
229 const T& value)
230 {
231 // See the comments in Message& operator <<(const T&) above for why
232 // we need this using statement.
233 using ::operator<<;
234 *ss_ << value;
235 }
236 #endif // GTEST_OS_SYMBIAN
237
238 // We'll hold the text streamed to this object here.
239 const internal::scoped_ptr< ::std::stringstream> ss_;
240
241 // We declare (but don't implement) this to prevent the compiler
242 // from implementing the assignment operator.
243 void operator=(const Message&);
244 };
245
246 // Streams a Message to an ostream.
247 inline std::ostream& operator<<(std::ostream& os, const Message& sb)
248 {
249 return os << sb.GetString();
250 }
251
252 namespace internal
253 {
254 // Converts a streamable value to an std::string. A NULL pointer is
255 // converted to "(null)". When the input value is a ::string,
256 // ::std::string, ::wstring, or ::std::wstring object, each NUL
257 // character in it is replaced with "\\0".
258 template <typename T>
StreamableToString(const T & streamable)259 std::string StreamableToString(const T& streamable)
260 {
261 return (Message() << streamable).GetString();
262 }
263
264 } // namespace internal
265 } // namespace testing
266
267 #endif // GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
268