blob: b1d646f030ab4eab6ad278784bbf0fc915ecdcf7 [file] [log] [blame]
shiqiand2014562008-07-03 22:38:12 +00001// 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
shiqiand2014562008-07-03 22:38:12 +000049#include <gtest/internal/gtest-string.h>
50#include <gtest/internal/gtest-internal.h>
shiqiand2014562008-07-03 22:38:12 +000051
52namespace testing {
53
54// The Message class works like an ostream repeater.
55//
56// Typical usage:
57//
58// 1. You stream a bunch of values to a Message object.
59// It will remember the text in a StrStream.
60// 2. Then you stream the Message object to an ostream.
61// This causes the text in the Message to be streamed
62// to the ostream.
63//
64// For example;
65//
66// testing::Message foo;
67// foo << 1 << " != " << 2;
68// std::cout << foo;
69//
70// will print "1 != 2".
71//
72// Message is not intended to be inherited from. In particular, its
73// destructor is not virtual.
74//
75// Note that StrStream behaves differently in gcc and in MSVC. You
76// can stream a NULL char pointer to it in the former, but not in the
77// latter (it causes an access violation if you do). The Message
78// class hides this difference by treating a NULL char pointer as
79// "(null)".
80class Message {
81 private:
82 // The type of basic IO manipulators (endl, ends, and flush) for
83 // narrow streams.
84 typedef std::ostream& (*BasicNarrowIoManip)(std::ostream&);
85
86 public:
87 // Constructs an empty Message.
88 // We allocate the StrStream separately because it otherwise each use of
89 // ASSERT/EXPECT in a procedure adds over 200 bytes to the procedure's
90 // stack frame leading to huge stack frames in some cases; gcc does not reuse
91 // the stack space.
92 Message() : ss_(new internal::StrStream) {}
93
94 // Copy constructor.
95 Message(const Message& msg) : ss_(new internal::StrStream) { // NOLINT
96 *ss_ << msg.GetString();
97 }
98
99 // Constructs a Message from a C-string.
100 explicit Message(const char* str) : ss_(new internal::StrStream) {
101 *ss_ << str;
102 }
103
104 ~Message() { delete ss_; }
105#ifdef __SYMBIAN32__
106 // Streams a value (either a pointer or not) to this object.
107 template <typename T>
108 inline Message& operator <<(const T& value) {
109 StreamHelper(typename internal::is_pointer<T>::type(), value);
110 return *this;
111 }
112#else
113 // Streams a non-pointer value to this object.
114 template <typename T>
115 inline Message& operator <<(const T& val) {
116 ::GTestStreamToHelper(ss_, val);
117 return *this;
118 }
119
120 // Streams a pointer value to this object.
121 //
122 // This function is an overload of the previous one. When you
123 // stream a pointer to a Message, this definition will be used as it
124 // is more specialized. (The C++ Standard, section
125 // [temp.func.order].) If you stream a non-pointer, then the
126 // previous definition will be used.
127 //
128 // The reason for this overload is that streaming a NULL pointer to
129 // ostream is undefined behavior. Depending on the compiler, you
130 // may get "0", "(nil)", "(null)", or an access violation. To
131 // ensure consistent result across compilers, we always treat NULL
132 // as "(null)".
133 template <typename T>
134 inline Message& operator <<(T* const& pointer) { // NOLINT
135 if (pointer == NULL) {
136 *ss_ << "(null)";
137 } else {
138 ::GTestStreamToHelper(ss_, pointer);
139 }
140 return *this;
141 }
142#endif // __SYMBIAN32__
143
144 // Since the basic IO manipulators are overloaded for both narrow
145 // and wide streams, we have to provide this specialized definition
146 // of operator <<, even though its body is the same as the
147 // templatized version above. Without this definition, streaming
148 // endl or other basic IO manipulators to Message will confuse the
149 // compiler.
150 Message& operator <<(BasicNarrowIoManip val) {
151 *ss_ << val;
152 return *this;
153 }
154
155 // Instead of 1/0, we want to see true/false for bool values.
156 Message& operator <<(bool b) {
157 return *this << (b ? "true" : "false");
158 }
159
160 // These two overloads allow streaming a wide C string to a Message
161 // using the UTF-8 encoding.
162 Message& operator <<(const wchar_t* wide_c_str) {
163 return *this << internal::String::ShowWideCString(wide_c_str);
164 }
165 Message& operator <<(wchar_t* wide_c_str) {
166 return *this << internal::String::ShowWideCString(wide_c_str);
167 }
168
169#if GTEST_HAS_STD_WSTRING
170 // Converts the given wide string to a narrow string using the UTF-8
171 // encoding, and streams the result to this Message object.
172 Message& operator <<(const ::std::wstring& wstr);
173#endif // GTEST_HAS_STD_WSTRING
174
175#if GTEST_HAS_GLOBAL_WSTRING
176 // Converts the given wide string to a narrow string using the UTF-8
177 // encoding, and streams the result to this Message object.
178 Message& operator <<(const ::wstring& wstr);
179#endif // GTEST_HAS_GLOBAL_WSTRING
180
181 // Gets the text streamed to this object so far as a String.
182 // Each '\0' character in the buffer is replaced with "\\0".
183 //
184 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
185 internal::String GetString() const {
186 return internal::StrStreamToString(ss_);
187 }
188
189 private:
190#ifdef __SYMBIAN32__
191 // These are needed as the Nokia Symbian Compiler cannot decide between
192 // const T& and const T* in a function template. The Nokia compiler _can_
193 // decide between class template specializations for T and T*, so a
194 // tr1::type_traits-like is_pointer works, and we can overload on that.
195 template <typename T>
196 inline void StreamHelper(internal::true_type dummy, T* pointer) {
197 if (pointer == NULL) {
198 *ss_ << "(null)";
199 } else {
200 ::GTestStreamToHelper(ss_, pointer);
201 }
202 }
203 template <typename T>
204 inline void StreamHelper(internal::false_type dummy, const T& value) {
205 ::GTestStreamToHelper(ss_, value);
206 }
207#endif // __SYMBIAN32__
208
209 // We'll hold the text streamed to this object here.
210 internal::StrStream* const ss_;
211
212 // We declare (but don't implement) this to prevent the compiler
213 // from implementing the assignment operator.
214 void operator=(const Message&);
215};
216
217// Streams a Message to an ostream.
218inline std::ostream& operator <<(std::ostream& os, const Message& sb) {
219 return os << sb.GetString();
220}
221
222} // namespace testing
223
224#endif // GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_