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