blob: 9f024d0c9c77c0ed965ef4531d7725bd964f5518 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// Copyright 2007, 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// Google Mock - a framework for writing C++ mock classes.
33//
34// This file implements a universal value printer that can print a
35// value of any type T:
36//
37// void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
38//
zhanyong.wan7e571ef2009-03-31 18:26:29 +000039// A user can teach this function how to print a class type T by
40// defining either operator<<() or PrintTo() in the namespace that
41// defines T. More specifically, the FIRST defined function in the
42// following list will be used (assuming T is defined in namespace
43// foo):
44//
45// 1. foo::PrintTo(const T&, ostream*)
46// 2. operator<<(ostream&, const T&) defined in either foo or the
47// global namespace.
48//
49// If none of the above is defined, it will print the debug string of
50// the value if it is a protocol buffer, or print the raw bytes in the
51// value otherwise.
zhanyong.wan4a5330d2009-02-19 00:36:44 +000052//
53// To aid debugging: when T is a reference type, the address of the
54// value is also printed; when T is a (const) char pointer, both the
55// pointer value and the NUL-terminated string it points to are
shiqiane35fdd92008-12-10 05:08:54 +000056// printed.
57//
zhanyong.wance198ff2009-02-12 01:34:27 +000058// We also provide some convenient wrappers:
shiqiane35fdd92008-12-10 05:08:54 +000059//
zhanyong.wan4a5330d2009-02-19 00:36:44 +000060// // Prints a value as the given type to a string.
61// string ::testing::internal::UniversalPrinter<T>::PrintToString(value);
62//
63// // Prints a value tersely: for a reference type, the referenced
64// // value (but not the address) is printed; for a (const) char
65// // pointer, the NUL-terminated string (but not the pointer) is
66// // printed.
67// void ::testing::internal::UniversalTersePrint(const T& value, ostream*);
68//
zhanyong.wanb8243162009-06-04 05:48:20 +000069// // Prints value using the type inferred by the compiler. The difference
70// // from UniversalTersePrint() is that this function prints both the
71// // pointer and the NUL-terminated string for a (const) char pointer.
72// void ::testing::internal::UniversalPrint(const T& value, ostream*);
73//
zhanyong.wan4a5330d2009-02-19 00:36:44 +000074// // Prints the fields of a tuple tersely to a string vector, one
75// // element for each field.
76// std::vector<string> UniversalTersePrintTupleFieldsToStrings(
77// const Tuple& value);
zhanyong.wanb8243162009-06-04 05:48:20 +000078//
79// Known limitation:
80//
81// The print primitives print the elements of an STL-style container
82// using the compiler-inferred type of *iter where iter is a
83// const_iterator of the container. When const_iterator is an input
84// iterator but not a forward iterator, this inferred type may not
85// match value_type, and the print output may be incorrect. In
86// practice, this is rarely a problem as for most containers
87// const_iterator is a forward iterator. We'll fix this if there's an
88// actual need for it. Note that this fix cannot rely on value_type
89// being defined as many user-defined container types don't have
90// value_type.
shiqiane35fdd92008-12-10 05:08:54 +000091
92#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_PRINTERS_H_
93#define GMOCK_INCLUDE_GMOCK_GMOCK_PRINTERS_H_
94
95#include <ostream> // NOLINT
zhanyong.wan4a5330d2009-02-19 00:36:44 +000096#include <sstream>
shiqiane35fdd92008-12-10 05:08:54 +000097#include <string>
98#include <utility>
zhanyong.wan4a5330d2009-02-19 00:36:44 +000099#include <vector>
shiqiane35fdd92008-12-10 05:08:54 +0000100
101#include <gmock/internal/gmock-internal-utils.h>
102#include <gmock/internal/gmock-port.h>
103#include <gtest/gtest.h>
104
shiqiane35fdd92008-12-10 05:08:54 +0000105namespace testing {
106
107// Definitions in the 'internal' and 'internal2' name spaces are
108// subject to change without notice. DO NOT USE THEM IN USER CODE!
109namespace internal2 {
110
111// Prints the given number of bytes in the given object to the given
112// ostream.
113void PrintBytesInObjectTo(const unsigned char* obj_bytes,
114 size_t count,
115 ::std::ostream* os);
116
117// TypeWithoutFormatter<T, kIsProto>::PrintValue(value, os) is called
118// by the universal printer to print a value of type T when neither
119// operator<< nor PrintTo() is defined for type T. When T is
120// ProtocolMessage, proto2::Message, or a subclass of those, kIsProto
121// will be true and the short debug string of the protocol message
122// value will be printed; otherwise kIsProto will be false and the
123// bytes in the value will be printed.
124template <typename T, bool kIsProto>
125class TypeWithoutFormatter {
126 public:
127 static void PrintValue(const T& value, ::std::ostream* os) {
128 PrintBytesInObjectTo(reinterpret_cast<const unsigned char*>(&value),
129 sizeof(value), os);
130 }
131};
zhanyong.wan0ea67f82009-08-14 04:50:02 +0000132
133// We print a protobuf using its ShortDebugString() when the string
134// doesn't exceed this many characters; otherwise we print it using
135// DebugString() for better readability.
136const size_t kProtobufOneLinerMaxLength = 50;
137
shiqiane35fdd92008-12-10 05:08:54 +0000138template <typename T>
139class TypeWithoutFormatter<T, true> {
140 public:
141 static void PrintValue(const T& value, ::std::ostream* os) {
zhanyong.wan0ea67f82009-08-14 04:50:02 +0000142 const ::testing::internal::string short_str = value.ShortDebugString();
143 const ::testing::internal::string pretty_str =
144 short_str.length() <= kProtobufOneLinerMaxLength ?
145 short_str : ("\n" + value.DebugString());
146 ::std::operator<<(*os, "<" + pretty_str + ">");
shiqiane35fdd92008-12-10 05:08:54 +0000147 }
148};
149
150// Prints the given value to the given ostream. If the value is a
151// protocol message, its short debug string is printed; otherwise the
152// bytes in the value are printed. This is what
153// UniversalPrinter<T>::Print() does when it knows nothing about type
154// T and T has no << operator.
155//
156// A user can override this behavior for a class type Foo by defining
157// a << operator in the namespace where Foo is defined.
158//
159// We put this operator in namespace 'internal2' instead of 'internal'
160// to simplify the implementation, as much code in 'internal' needs to
161// use << in STL, which would conflict with our own << were it defined
162// in 'internal'.
zhanyong.wan2f0849f2009-02-11 18:06:37 +0000163//
164// Note that this operator<< takes a generic std::basic_ostream<Char,
165// CharTraits> type instead of the more restricted std::ostream. If
166// we define it to take an std::ostream instead, we'll get an
167// "ambiguous overloads" compiler error when trying to print a type
168// Foo that supports streaming to std::basic_ostream<Char,
169// CharTraits>, as the compiler cannot tell whether
170// operator<<(std::ostream&, const T&) or
171// operator<<(std::basic_stream<Char, CharTraits>, const Foo&) is more
172// specific.
173template <typename Char, typename CharTraits, typename T>
174::std::basic_ostream<Char, CharTraits>& operator<<(
175 ::std::basic_ostream<Char, CharTraits>& os, const T& x) {
shiqiane35fdd92008-12-10 05:08:54 +0000176 TypeWithoutFormatter<T, ::testing::internal::IsAProtocolMessage<T>::value>::
177 PrintValue(x, &os);
178 return os;
179}
180
181} // namespace internal2
zhanyong.wan7e571ef2009-03-31 18:26:29 +0000182} // namespace testing
shiqiane35fdd92008-12-10 05:08:54 +0000183
zhanyong.wan7e571ef2009-03-31 18:26:29 +0000184// This namespace MUST NOT BE NESTED IN ::testing, or the name look-up
185// magic needed for implementing UniversalPrinter won't work.
186namespace testing_internal {
187
188// Used to print a value that is not an STL-style container when the
189// user doesn't define PrintTo() for it.
190template <typename T>
191void DefaultPrintNonContainerTo(const T& value, ::std::ostream* os) {
192 // With the following statement, during unqualified name lookup,
193 // testing::internal2::operator<< appears as if it was declared in
194 // the nearest enclosing namespace that contains both
195 // ::testing_internal and ::testing::internal2, i.e. the global
196 // namespace. For more details, refer to the C++ Standard section
197 // 7.3.4-1 [namespace.udir]. This allows us to fall back onto
198 // testing::internal2::operator<< in case T doesn't come with a <<
199 // operator.
200 //
201 // We cannot write 'using ::testing::internal2::operator<<;', which
202 // gcc 3.3 fails to compile due to a compiler bug.
203 using namespace ::testing::internal2; // NOLINT
204
205 // Assuming T is defined in namespace foo, in the next statement,
206 // the compiler will consider all of:
207 //
208 // 1. foo::operator<< (thanks to Koenig look-up),
209 // 2. ::operator<< (as the current namespace is enclosed in ::),
210 // 3. testing::internal2::operator<< (thanks to the using statement above).
211 //
212 // The operator<< whose type matches T best will be picked.
213 //
214 // We deliberately allow #2 to be a candidate, as sometimes it's
215 // impossible to define #1 (e.g. when foo is ::std, defining
216 // anything in it is undefined behavior unless you are a compiler
217 // vendor.).
218 *os << value;
219}
220
221} // namespace testing_internal
222
223namespace testing {
shiqiane35fdd92008-12-10 05:08:54 +0000224namespace internal {
225
226// UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
227// value to the given ostream. The caller must ensure that
228// 'ostream_ptr' is not NULL, or the behavior is undefined.
229//
230// We define UniversalPrinter as a class template (as opposed to a
231// function template), as we need to partially specialize it for
232// reference types, which cannot be done with function templates.
233template <typename T>
234class UniversalPrinter;
235
zhanyong.wanb8243162009-06-04 05:48:20 +0000236template <typename T>
237void UniversalPrint(const T& value, ::std::ostream* os);
238
shiqiane35fdd92008-12-10 05:08:54 +0000239// Used to print an STL-style container when the user doesn't define
240// a PrintTo() for it.
241template <typename C>
zhanyong.wanc6a41232009-05-13 23:38:40 +0000242void DefaultPrintTo(IsContainer /* dummy */,
243 false_type /* is not a pointer */,
244 const C& container, ::std::ostream* os) {
shiqiane35fdd92008-12-10 05:08:54 +0000245 const size_t kMaxCount = 32; // The maximum number of elements to print.
246 *os << '{';
247 size_t count = 0;
248 for (typename C::const_iterator it = container.begin();
249 it != container.end(); ++it, ++count) {
250 if (count > 0) {
251 *os << ',';
252 if (count == kMaxCount) { // Enough has been printed.
253 *os << " ...";
254 break;
255 }
256 }
257 *os << ' ';
zhanyong.wanb8243162009-06-04 05:48:20 +0000258 // We cannot call PrintTo(*it, os) here as PrintTo() doesn't
259 // handle *it being a native array.
260 internal::UniversalPrint(*it, os);
shiqiane35fdd92008-12-10 05:08:54 +0000261 }
262
263 if (count > 0) {
264 *os << ' ';
265 }
266 *os << '}';
267}
268
zhanyong.wanc6a41232009-05-13 23:38:40 +0000269// Used to print a pointer that is neither a char pointer nor a member
270// pointer, when the user doesn't define PrintTo() for it. (A member
271// variable pointer or member function pointer doesn't really point to
272// a location in the address space. Their representation is
273// implementation-defined. Therefore they will be printed as raw
274// bytes.)
shiqiane35fdd92008-12-10 05:08:54 +0000275template <typename T>
zhanyong.wanc6a41232009-05-13 23:38:40 +0000276void DefaultPrintTo(IsNotContainer /* dummy */,
277 true_type /* is a pointer */,
278 T* p, ::std::ostream* os) {
279 if (p == NULL) {
280 *os << "NULL";
281 } else {
zhanyong.wanf47a2df2009-09-24 16:39:30 +0000282 // We want to print p as a const void*. However, we cannot cast
283 // it to const void* directly, even using reinterpret_cast, as
284 // earlier versions of gcc (e.g. 3.4.5) cannot compile the cast
285 // when p is a function pointer. Casting to UInt64 first solves
286 // the problem.
287 *os << reinterpret_cast<const void*>(reinterpret_cast<internal::UInt64>(p));
zhanyong.wanc6a41232009-05-13 23:38:40 +0000288 }
289}
290
291// Used to print a non-container, non-pointer value when the user
292// doesn't define PrintTo() for it.
293template <typename T>
294void DefaultPrintTo(IsNotContainer /* dummy */,
295 false_type /* is not a pointer */,
296 const T& value, ::std::ostream* os) {
zhanyong.wan7e571ef2009-03-31 18:26:29 +0000297 ::testing_internal::DefaultPrintNonContainerTo(value, os);
shiqiane35fdd92008-12-10 05:08:54 +0000298}
299
300// Prints the given value using the << operator if it has one;
301// otherwise prints the bytes in it. This is what
302// UniversalPrinter<T>::Print() does when PrintTo() is not specialized
303// or overloaded for type T.
304//
305// A user can override this behavior for a class type Foo by defining
306// an overload of PrintTo() in the namespace where Foo is defined. We
307// give the user this option as sometimes defining a << operator for
308// Foo is not desirable (e.g. the coding style may prevent doing it,
309// or there is already a << operator but it doesn't do what the user
310// wants).
311template <typename T>
312void PrintTo(const T& value, ::std::ostream* os) {
zhanyong.wanc6a41232009-05-13 23:38:40 +0000313 // DefaultPrintTo() is overloaded. The type of its first two
314 // arguments determine which version will be picked. If T is an
315 // STL-style container, the version for container will be called; if
316 // T is a pointer, the pointer version will be called; otherwise the
317 // generic version will be called.
shiqiane35fdd92008-12-10 05:08:54 +0000318 //
319 // Note that we check for container types here, prior to we check
320 // for protocol message types in our operator<<. The rationale is:
321 //
322 // For protocol messages, we want to give people a chance to
323 // override Google Mock's format by defining a PrintTo() or
zhanyong.wan18490652009-05-11 18:54:08 +0000324 // operator<<. For STL containers, other formats can be
325 // incompatible with Google Mock's format for the container
326 // elements; therefore we check for container types here to ensure
327 // that our format is used.
zhanyong.wanc6a41232009-05-13 23:38:40 +0000328 //
329 // The second argument of DefaultPrintTo() is needed to bypass a bug
330 // in Symbian's C++ compiler that prevents it from picking the right
331 // overload between:
332 //
333 // PrintTo(const T& x, ...);
334 // PrintTo(T* x, ...);
335 DefaultPrintTo(IsContainerTest<T>(0), is_pointer<T>(), value, os);
shiqiane35fdd92008-12-10 05:08:54 +0000336}
337
338// The following list of PrintTo() overloads tells
339// UniversalPrinter<T>::Print() how to print standard types (built-in
340// types, strings, plain arrays, and pointers).
341
342// Overloads for various char types.
343void PrintCharTo(char c, int char_code, ::std::ostream* os);
344inline void PrintTo(unsigned char c, ::std::ostream* os) {
345 PrintCharTo(c, c, os);
346}
347inline void PrintTo(signed char c, ::std::ostream* os) {
348 PrintCharTo(c, c, os);
349}
350inline void PrintTo(char c, ::std::ostream* os) {
351 // When printing a plain char, we always treat it as unsigned. This
352 // way, the output won't be affected by whether the compiler thinks
353 // char is signed or not.
354 PrintTo(static_cast<unsigned char>(c), os);
355}
356
357// Overloads for other simple built-in types.
358inline void PrintTo(bool x, ::std::ostream* os) {
359 *os << (x ? "true" : "false");
360}
361
362// Overload for wchar_t type.
363// Prints a wchar_t as a symbol if it is printable or as its internal
364// code otherwise and also as its decimal code (except for L'\0').
365// The L'\0' char is printed as "L'\\0'". The decimal code is printed
366// as signed integer when wchar_t is implemented by the compiler
367// as a signed type and is printed as an unsigned integer when wchar_t
368// is implemented as an unsigned type.
369void PrintTo(wchar_t wc, ::std::ostream* os);
370
371// Overloads for C strings.
372void PrintTo(const char* s, ::std::ostream* os);
373inline void PrintTo(char* s, ::std::ostream* os) {
374 PrintTo(implicit_cast<const char*>(s), os);
375}
376
zhanyong.wan16cf4732009-05-14 20:55:30 +0000377// MSVC can be configured to define wchar_t as a typedef of unsigned
378// short. It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native
379// type. When wchar_t is a typedef, defining an overload for const
380// wchar_t* would cause unsigned short* be printed as a wide string,
381// possibly causing invalid memory accesses.
shiqiane35fdd92008-12-10 05:08:54 +0000382#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
383// Overloads for wide C strings
384void PrintTo(const wchar_t* s, ::std::ostream* os);
385inline void PrintTo(wchar_t* s, ::std::ostream* os) {
386 PrintTo(implicit_cast<const wchar_t*>(s), os);
387}
388#endif
389
shiqiane35fdd92008-12-10 05:08:54 +0000390// Overload for C arrays. Multi-dimensional arrays are printed
391// properly.
392
393// Prints the given number of elements in an array, without printing
394// the curly braces.
395template <typename T>
396void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
397 UniversalPrinter<T>::Print(a[0], os);
398 for (size_t i = 1; i != count; i++) {
399 *os << ", ";
400 UniversalPrinter<T>::Print(a[i], os);
401 }
402}
403
404// Overloads for ::string and ::std::string.
405#if GTEST_HAS_GLOBAL_STRING
406void PrintStringTo(const ::string&s, ::std::ostream* os);
407inline void PrintTo(const ::string& s, ::std::ostream* os) {
408 PrintStringTo(s, os);
409}
410#endif // GTEST_HAS_GLOBAL_STRING
411
412#if GTEST_HAS_STD_STRING
413void PrintStringTo(const ::std::string&s, ::std::ostream* os);
414inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
415 PrintStringTo(s, os);
416}
417#endif // GTEST_HAS_STD_STRING
418
419// Overloads for ::wstring and ::std::wstring.
420#if GTEST_HAS_GLOBAL_WSTRING
421void PrintWideStringTo(const ::wstring&s, ::std::ostream* os);
422inline void PrintTo(const ::wstring& s, ::std::ostream* os) {
423 PrintWideStringTo(s, os);
424}
425#endif // GTEST_HAS_GLOBAL_WSTRING
426
427#if GTEST_HAS_STD_WSTRING
428void PrintWideStringTo(const ::std::wstring&s, ::std::ostream* os);
429inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
430 PrintWideStringTo(s, os);
431}
432#endif // GTEST_HAS_STD_WSTRING
433
434// Overload for ::std::tr1::tuple. Needed for printing function
435// arguments, which are packed as tuples.
436
shiqianc97f2f52008-12-11 17:22:59 +0000437// Helper function for printing a tuple. T must be instantiated with
438// a tuple type.
439template <typename T>
zhanyong.wana95c6a52009-12-02 08:36:42 +0000440void PrintTupleTo(const T& t, ::std::ostream* os);
shiqianc97f2f52008-12-11 17:22:59 +0000441
442// Overloaded PrintTo() for tuples of various arities. We support
443// tuples of up-to 10 fields. The following implementation works
444// regardless of whether tr1::tuple is implemented using the
445// non-standard variadic template feature or not.
446
447inline void PrintTo(const ::std::tr1::tuple<>& t, ::std::ostream* os) {
448 PrintTupleTo(t, os);
449}
450
451template <typename T1>
452void PrintTo(const ::std::tr1::tuple<T1>& t, ::std::ostream* os) {
453 PrintTupleTo(t, os);
454}
455
456template <typename T1, typename T2>
457void PrintTo(const ::std::tr1::tuple<T1, T2>& t, ::std::ostream* os) {
458 PrintTupleTo(t, os);
459}
460
461template <typename T1, typename T2, typename T3>
462void PrintTo(const ::std::tr1::tuple<T1, T2, T3>& t, ::std::ostream* os) {
463 PrintTupleTo(t, os);
464}
465
466template <typename T1, typename T2, typename T3, typename T4>
467void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4>& t, ::std::ostream* os) {
468 PrintTupleTo(t, os);
469}
470
471template <typename T1, typename T2, typename T3, typename T4, typename T5>
472void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5>& t,
473 ::std::ostream* os) {
474 PrintTupleTo(t, os);
475}
476
477template <typename T1, typename T2, typename T3, typename T4, typename T5,
478 typename T6>
479void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6>& t,
480 ::std::ostream* os) {
481 PrintTupleTo(t, os);
482}
483
484template <typename T1, typename T2, typename T3, typename T4, typename T5,
485 typename T6, typename T7>
486void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7>& t,
487 ::std::ostream* os) {
488 PrintTupleTo(t, os);
489}
490
491template <typename T1, typename T2, typename T3, typename T4, typename T5,
492 typename T6, typename T7, typename T8>
493void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8>& t,
494 ::std::ostream* os) {
495 PrintTupleTo(t, os);
496}
497
498template <typename T1, typename T2, typename T3, typename T4, typename T5,
499 typename T6, typename T7, typename T8, typename T9>
500void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9>& t,
501 ::std::ostream* os) {
502 PrintTupleTo(t, os);
503}
504
shiqiane35fdd92008-12-10 05:08:54 +0000505template <typename T1, typename T2, typename T3, typename T4, typename T5,
506 typename T6, typename T7, typename T8, typename T9, typename T10>
507void PrintTo(
508 const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& t,
509 ::std::ostream* os) {
shiqianc97f2f52008-12-11 17:22:59 +0000510 PrintTupleTo(t, os);
shiqiane35fdd92008-12-10 05:08:54 +0000511}
512
513// Overload for std::pair.
514template <typename T1, typename T2>
515void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
516 *os << '(';
517 UniversalPrinter<T1>::Print(value.first, os);
518 *os << ", ";
519 UniversalPrinter<T2>::Print(value.second, os);
520 *os << ')';
521}
522
523// Implements printing a non-reference type T by letting the compiler
524// pick the right overload of PrintTo() for T.
525template <typename T>
526class UniversalPrinter {
527 public:
528 // MSVC warns about adding const to a function type, so we want to
529 // disable the warning.
530#ifdef _MSC_VER
531#pragma warning(push) // Saves the current warning state.
532#pragma warning(disable:4180) // Temporarily disables warning 4180.
533#endif // _MSC_VER
534
535 // Note: we deliberately don't call this PrintTo(), as that name
536 // conflicts with ::testing::internal::PrintTo in the body of the
537 // function.
538 static void Print(const T& value, ::std::ostream* os) {
539 // By default, ::testing::internal::PrintTo() is used for printing
540 // the value.
541 //
542 // Thanks to Koenig look-up, if T is a class and has its own
543 // PrintTo() function defined in its namespace, that function will
544 // be visible here. Since it is more specific than the generic ones
545 // in ::testing::internal, it will be picked by the compiler in the
546 // following statement - exactly what we want.
547 PrintTo(value, os);
548 }
549
550 // A convenient wrapper for Print() that returns the print-out as a
551 // string.
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000552 static string PrintToString(const T& value) {
shiqiane35fdd92008-12-10 05:08:54 +0000553 ::std::stringstream ss;
554 Print(value, &ss);
555 return ss.str();
556 }
557
558#ifdef _MSC_VER
559#pragma warning(pop) // Restores the warning state.
560#endif // _MSC_VER
561};
562
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000563// UniversalPrintArray(begin, len, os) prints an array of 'len'
564// elements, starting at address 'begin'.
565template <typename T>
566void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
567 if (len == 0) {
568 *os << "{}";
569 } else {
570 *os << "{ ";
571 const size_t kThreshold = 18;
572 const size_t kChunkSize = 8;
573 // If the array has more than kThreshold elements, we'll have to
574 // omit some details by printing only the first and the last
575 // kChunkSize elements.
576 // TODO(wan@google.com): let the user control the threshold using a flag.
577 if (len <= kThreshold) {
578 PrintRawArrayTo(begin, len, os);
579 } else {
580 PrintRawArrayTo(begin, kChunkSize, os);
581 *os << ", ..., ";
582 PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
583 }
584 *os << " }";
585 }
586}
587// This overload prints a (const) char array compactly.
588void UniversalPrintArray(const char* begin, size_t len, ::std::ostream* os);
589
590// Prints an array of 'len' elements, starting at address 'begin', to a string.
591template <typename T>
592string UniversalPrintArrayToString(const T* begin, size_t len) {
593 ::std::stringstream ss;
594 UniversalPrintArray(begin, len, &ss);
595 return ss.str();
596}
597
shiqiane35fdd92008-12-10 05:08:54 +0000598// Implements printing an array type T[N].
599template <typename T, size_t N>
600class UniversalPrinter<T[N]> {
601 public:
602 // Prints the given array, omitting some elements when there are too
603 // many.
604 static void Print(const T (&a)[N], ::std::ostream* os) {
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000605 UniversalPrintArray(a, N, os);
shiqiane35fdd92008-12-10 05:08:54 +0000606 }
607
608 // A convenient wrapper for Print() that returns the print-out as a
609 // string.
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000610 static string PrintToString(const T (&a)[N]) {
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000611 return UniversalPrintArrayToString(a, N);
shiqiane35fdd92008-12-10 05:08:54 +0000612 }
613};
614
615// Implements printing a reference type T&.
616template <typename T>
617class UniversalPrinter<T&> {
618 public:
619 // MSVC warns about adding const to a function type, so we want to
620 // disable the warning.
621#ifdef _MSC_VER
622#pragma warning(push) // Saves the current warning state.
623#pragma warning(disable:4180) // Temporarily disables warning 4180.
624#endif // _MSC_VER
625
626 static void Print(const T& value, ::std::ostream* os) {
627 // Prints the address of the value. We use reinterpret_cast here
628 // as static_cast doesn't compile when T is a function type.
629 *os << "@" << reinterpret_cast<const void*>(&value) << " ";
630
631 // Then prints the value itself.
632 UniversalPrinter<T>::Print(value, os);
633 }
634
635 // A convenient wrapper for Print() that returns the print-out as a
636 // string.
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000637 static string PrintToString(const T& value) {
shiqiane35fdd92008-12-10 05:08:54 +0000638 ::std::stringstream ss;
639 Print(value, &ss);
640 return ss.str();
641 }
642
643#ifdef _MSC_VER
644#pragma warning(pop) // Restores the warning state.
645#endif // _MSC_VER
646};
647
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000648// Prints a value tersely: for a reference type, the referenced value
649// (but not the address) is printed; for a (const) char pointer, the
650// NUL-terminated string (but not the pointer) is printed.
zhanyong.wance198ff2009-02-12 01:34:27 +0000651template <typename T>
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000652void UniversalTersePrint(const T& value, ::std::ostream* os) {
zhanyong.wance198ff2009-02-12 01:34:27 +0000653 UniversalPrinter<T>::Print(value, os);
654}
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000655inline void UniversalTersePrint(const char* str, ::std::ostream* os) {
656 if (str == NULL) {
657 *os << "NULL";
658 } else {
659 UniversalPrinter<string>::Print(string(str), os);
660 }
661}
662inline void UniversalTersePrint(char* str, ::std::ostream* os) {
663 UniversalTersePrint(static_cast<const char*>(str), os);
664}
665
zhanyong.wanb8243162009-06-04 05:48:20 +0000666// Prints a value using the type inferred by the compiler. The
667// difference between this and UniversalTersePrint() is that for a
668// (const) char pointer, this prints both the pointer and the
669// NUL-terminated string.
670template <typename T>
671void UniversalPrint(const T& value, ::std::ostream* os) {
672 UniversalPrinter<T>::Print(value, os);
673}
674
zhanyong.wana95c6a52009-12-02 08:36:42 +0000675typedef ::std::vector<string> Strings;
676
677// This helper template allows PrintTo() for tuples and
678// UniversalTersePrintTupleFieldsToStrings() to be defined by
679// induction on the number of tuple fields. The idea is that
680// TuplePrefixPrinter<N>::PrintPrefixTo(t, os) prints the first N
681// fields in tuple t, and can be defined in terms of
682// TuplePrefixPrinter<N - 1>.
683
684// The inductive case.
685template <size_t N>
686struct TuplePrefixPrinter {
687 // Prints the first N fields of a tuple.
688 template <typename Tuple>
689 static void PrintPrefixTo(const Tuple& t, ::std::ostream* os) {
690 TuplePrefixPrinter<N - 1>::PrintPrefixTo(t, os);
691 *os << ", ";
692 UniversalPrinter<typename ::std::tr1::tuple_element<N - 1, Tuple>::type>
693 ::Print(::std::tr1::get<N - 1>(t), os);
694 }
695
696 // Tersely prints the first N fields of a tuple to a string vector,
697 // one element for each field.
698 template <typename Tuple>
699 static void TersePrintPrefixToStrings(const Tuple& t, Strings* strings) {
700 TuplePrefixPrinter<N - 1>::TersePrintPrefixToStrings(t, strings);
701 ::std::stringstream ss;
702 UniversalTersePrint(::std::tr1::get<N - 1>(t), &ss);
703 strings->push_back(ss.str());
704 }
705};
706
707// Base cases.
708template <>
709struct TuplePrefixPrinter<0> {
710 template <typename Tuple>
711 static void PrintPrefixTo(const Tuple&, ::std::ostream*) {}
712
713 template <typename Tuple>
714 static void TersePrintPrefixToStrings(const Tuple&, Strings*) {}
715};
716template <>
717template <typename Tuple>
718void TuplePrefixPrinter<1>::PrintPrefixTo(const Tuple& t, ::std::ostream* os) {
719 UniversalPrinter<typename ::std::tr1::tuple_element<0, Tuple>::type>::
720 Print(::std::tr1::get<0>(t), os);
721}
722
723// Helper function for printing a tuple. T must be instantiated with
724// a tuple type.
725template <typename T>
726void PrintTupleTo(const T& t, ::std::ostream* os) {
727 *os << "(";
728 TuplePrefixPrinter< ::std::tr1::tuple_size<T>::value>::
729 PrintPrefixTo(t, os);
730 *os << ")";
731}
732
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000733// Prints the fields of a tuple tersely to a string vector, one
734// element for each field. See the comment before
735// UniversalTersePrint() for how we define "tersely".
736template <typename Tuple>
737Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
738 Strings result;
739 TuplePrefixPrinter< ::std::tr1::tuple_size<Tuple>::value>::
740 TersePrintPrefixToStrings(value, &result);
741 return result;
742}
zhanyong.wance198ff2009-02-12 01:34:27 +0000743
shiqiane35fdd92008-12-10 05:08:54 +0000744} // namespace internal
745} // namespace testing
746
747#endif // GMOCK_INCLUDE_GMOCK_GMOCK_PRINTERS_H_