blob: 69eee1200a84768b5e812f1cc8046425bb9dfc91 [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 {
282 // We cannot use implicit_cast or static_cast here, as they don't
283 // work when p is a function pointer.
284 *os << reinterpret_cast<const void*>(p);
285 }
286}
287
288// Used to print a non-container, non-pointer value when the user
289// doesn't define PrintTo() for it.
290template <typename T>
291void DefaultPrintTo(IsNotContainer /* dummy */,
292 false_type /* is not a pointer */,
293 const T& value, ::std::ostream* os) {
zhanyong.wan7e571ef2009-03-31 18:26:29 +0000294 ::testing_internal::DefaultPrintNonContainerTo(value, os);
shiqiane35fdd92008-12-10 05:08:54 +0000295}
296
297// Prints the given value using the << operator if it has one;
298// otherwise prints the bytes in it. This is what
299// UniversalPrinter<T>::Print() does when PrintTo() is not specialized
300// or overloaded for type T.
301//
302// A user can override this behavior for a class type Foo by defining
303// an overload of PrintTo() in the namespace where Foo is defined. We
304// give the user this option as sometimes defining a << operator for
305// Foo is not desirable (e.g. the coding style may prevent doing it,
306// or there is already a << operator but it doesn't do what the user
307// wants).
308template <typename T>
309void PrintTo(const T& value, ::std::ostream* os) {
zhanyong.wanc6a41232009-05-13 23:38:40 +0000310 // DefaultPrintTo() is overloaded. The type of its first two
311 // arguments determine which version will be picked. If T is an
312 // STL-style container, the version for container will be called; if
313 // T is a pointer, the pointer version will be called; otherwise the
314 // generic version will be called.
shiqiane35fdd92008-12-10 05:08:54 +0000315 //
316 // Note that we check for container types here, prior to we check
317 // for protocol message types in our operator<<. The rationale is:
318 //
319 // For protocol messages, we want to give people a chance to
320 // override Google Mock's format by defining a PrintTo() or
zhanyong.wan18490652009-05-11 18:54:08 +0000321 // operator<<. For STL containers, other formats can be
322 // incompatible with Google Mock's format for the container
323 // elements; therefore we check for container types here to ensure
324 // that our format is used.
zhanyong.wanc6a41232009-05-13 23:38:40 +0000325 //
326 // The second argument of DefaultPrintTo() is needed to bypass a bug
327 // in Symbian's C++ compiler that prevents it from picking the right
328 // overload between:
329 //
330 // PrintTo(const T& x, ...);
331 // PrintTo(T* x, ...);
332 DefaultPrintTo(IsContainerTest<T>(0), is_pointer<T>(), value, os);
shiqiane35fdd92008-12-10 05:08:54 +0000333}
334
335// The following list of PrintTo() overloads tells
336// UniversalPrinter<T>::Print() how to print standard types (built-in
337// types, strings, plain arrays, and pointers).
338
339// Overloads for various char types.
340void PrintCharTo(char c, int char_code, ::std::ostream* os);
341inline void PrintTo(unsigned char c, ::std::ostream* os) {
342 PrintCharTo(c, c, os);
343}
344inline void PrintTo(signed char c, ::std::ostream* os) {
345 PrintCharTo(c, c, os);
346}
347inline void PrintTo(char c, ::std::ostream* os) {
348 // When printing a plain char, we always treat it as unsigned. This
349 // way, the output won't be affected by whether the compiler thinks
350 // char is signed or not.
351 PrintTo(static_cast<unsigned char>(c), os);
352}
353
354// Overloads for other simple built-in types.
355inline void PrintTo(bool x, ::std::ostream* os) {
356 *os << (x ? "true" : "false");
357}
358
359// Overload for wchar_t type.
360// Prints a wchar_t as a symbol if it is printable or as its internal
361// code otherwise and also as its decimal code (except for L'\0').
362// The L'\0' char is printed as "L'\\0'". The decimal code is printed
363// as signed integer when wchar_t is implemented by the compiler
364// as a signed type and is printed as an unsigned integer when wchar_t
365// is implemented as an unsigned type.
366void PrintTo(wchar_t wc, ::std::ostream* os);
367
368// Overloads for C strings.
369void PrintTo(const char* s, ::std::ostream* os);
370inline void PrintTo(char* s, ::std::ostream* os) {
371 PrintTo(implicit_cast<const char*>(s), os);
372}
373
zhanyong.wan16cf4732009-05-14 20:55:30 +0000374// MSVC can be configured to define wchar_t as a typedef of unsigned
375// short. It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native
376// type. When wchar_t is a typedef, defining an overload for const
377// wchar_t* would cause unsigned short* be printed as a wide string,
378// possibly causing invalid memory accesses.
shiqiane35fdd92008-12-10 05:08:54 +0000379#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
380// Overloads for wide C strings
381void PrintTo(const wchar_t* s, ::std::ostream* os);
382inline void PrintTo(wchar_t* s, ::std::ostream* os) {
383 PrintTo(implicit_cast<const wchar_t*>(s), os);
384}
385#endif
386
shiqiane35fdd92008-12-10 05:08:54 +0000387// Overload for C arrays. Multi-dimensional arrays are printed
388// properly.
389
390// Prints the given number of elements in an array, without printing
391// the curly braces.
392template <typename T>
393void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
394 UniversalPrinter<T>::Print(a[0], os);
395 for (size_t i = 1; i != count; i++) {
396 *os << ", ";
397 UniversalPrinter<T>::Print(a[i], os);
398 }
399}
400
401// Overloads for ::string and ::std::string.
402#if GTEST_HAS_GLOBAL_STRING
403void PrintStringTo(const ::string&s, ::std::ostream* os);
404inline void PrintTo(const ::string& s, ::std::ostream* os) {
405 PrintStringTo(s, os);
406}
407#endif // GTEST_HAS_GLOBAL_STRING
408
409#if GTEST_HAS_STD_STRING
410void PrintStringTo(const ::std::string&s, ::std::ostream* os);
411inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
412 PrintStringTo(s, os);
413}
414#endif // GTEST_HAS_STD_STRING
415
416// Overloads for ::wstring and ::std::wstring.
417#if GTEST_HAS_GLOBAL_WSTRING
418void PrintWideStringTo(const ::wstring&s, ::std::ostream* os);
419inline void PrintTo(const ::wstring& s, ::std::ostream* os) {
420 PrintWideStringTo(s, os);
421}
422#endif // GTEST_HAS_GLOBAL_WSTRING
423
424#if GTEST_HAS_STD_WSTRING
425void PrintWideStringTo(const ::std::wstring&s, ::std::ostream* os);
426inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
427 PrintWideStringTo(s, os);
428}
429#endif // GTEST_HAS_STD_WSTRING
430
431// Overload for ::std::tr1::tuple. Needed for printing function
432// arguments, which are packed as tuples.
433
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000434typedef ::std::vector<string> Strings;
435
436// This helper template allows PrintTo() for tuples and
437// UniversalTersePrintTupleFieldsToStrings() to be defined by
shiqiane35fdd92008-12-10 05:08:54 +0000438// induction on the number of tuple fields. The idea is that
439// TuplePrefixPrinter<N>::PrintPrefixTo(t, os) prints the first N
440// fields in tuple t, and can be defined in terms of
441// TuplePrefixPrinter<N - 1>.
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000442
443// The inductive case.
shiqiane35fdd92008-12-10 05:08:54 +0000444template <size_t N>
445struct TuplePrefixPrinter {
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000446 // Prints the first N fields of a tuple.
shiqiane35fdd92008-12-10 05:08:54 +0000447 template <typename Tuple>
448 static void PrintPrefixTo(const Tuple& t, ::std::ostream* os) {
449 TuplePrefixPrinter<N - 1>::PrintPrefixTo(t, os);
450 *os << ", ";
451 UniversalPrinter<typename ::std::tr1::tuple_element<N - 1, Tuple>::type>
452 ::Print(::std::tr1::get<N - 1>(t), os);
453 }
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000454
455 // Tersely prints the first N fields of a tuple to a string vector,
456 // one element for each field.
457 template <typename Tuple>
458 static void TersePrintPrefixToStrings(const Tuple& t, Strings* strings) {
459 TuplePrefixPrinter<N - 1>::TersePrintPrefixToStrings(t, strings);
460 ::std::stringstream ss;
461 UniversalTersePrint(::std::tr1::get<N - 1>(t), &ss);
462 strings->push_back(ss.str());
463 }
shiqiane35fdd92008-12-10 05:08:54 +0000464};
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000465
466// Base cases.
shiqiane35fdd92008-12-10 05:08:54 +0000467template <>
468struct TuplePrefixPrinter<0> {
469 template <typename Tuple>
470 static void PrintPrefixTo(const Tuple&, ::std::ostream*) {}
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000471
472 template <typename Tuple>
473 static void TersePrintPrefixToStrings(const Tuple&, Strings*) {}
shiqiane35fdd92008-12-10 05:08:54 +0000474};
475template <>
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000476template <typename Tuple>
477void TuplePrefixPrinter<1>::PrintPrefixTo(const Tuple& t, ::std::ostream* os) {
478 UniversalPrinter<typename ::std::tr1::tuple_element<0, Tuple>::type>::
479 Print(::std::tr1::get<0>(t), os);
480}
shiqiane35fdd92008-12-10 05:08:54 +0000481
shiqianc97f2f52008-12-11 17:22:59 +0000482// Helper function for printing a tuple. T must be instantiated with
483// a tuple type.
484template <typename T>
485void PrintTupleTo(const T& t, ::std::ostream* os) {
486 *os << "(";
487 TuplePrefixPrinter< ::std::tr1::tuple_size<T>::value>::
488 PrintPrefixTo(t, os);
489 *os << ")";
490}
491
492// Overloaded PrintTo() for tuples of various arities. We support
493// tuples of up-to 10 fields. The following implementation works
494// regardless of whether tr1::tuple is implemented using the
495// non-standard variadic template feature or not.
496
497inline void PrintTo(const ::std::tr1::tuple<>& t, ::std::ostream* os) {
498 PrintTupleTo(t, os);
499}
500
501template <typename T1>
502void PrintTo(const ::std::tr1::tuple<T1>& t, ::std::ostream* os) {
503 PrintTupleTo(t, os);
504}
505
506template <typename T1, typename T2>
507void PrintTo(const ::std::tr1::tuple<T1, T2>& t, ::std::ostream* os) {
508 PrintTupleTo(t, os);
509}
510
511template <typename T1, typename T2, typename T3>
512void PrintTo(const ::std::tr1::tuple<T1, T2, T3>& t, ::std::ostream* os) {
513 PrintTupleTo(t, os);
514}
515
516template <typename T1, typename T2, typename T3, typename T4>
517void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4>& t, ::std::ostream* os) {
518 PrintTupleTo(t, os);
519}
520
521template <typename T1, typename T2, typename T3, typename T4, typename T5>
522void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5>& t,
523 ::std::ostream* os) {
524 PrintTupleTo(t, os);
525}
526
527template <typename T1, typename T2, typename T3, typename T4, typename T5,
528 typename T6>
529void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6>& t,
530 ::std::ostream* os) {
531 PrintTupleTo(t, os);
532}
533
534template <typename T1, typename T2, typename T3, typename T4, typename T5,
535 typename T6, typename T7>
536void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7>& t,
537 ::std::ostream* os) {
538 PrintTupleTo(t, os);
539}
540
541template <typename T1, typename T2, typename T3, typename T4, typename T5,
542 typename T6, typename T7, typename T8>
543void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8>& t,
544 ::std::ostream* os) {
545 PrintTupleTo(t, os);
546}
547
548template <typename T1, typename T2, typename T3, typename T4, typename T5,
549 typename T6, typename T7, typename T8, typename T9>
550void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9>& t,
551 ::std::ostream* os) {
552 PrintTupleTo(t, os);
553}
554
shiqiane35fdd92008-12-10 05:08:54 +0000555template <typename T1, typename T2, typename T3, typename T4, typename T5,
556 typename T6, typename T7, typename T8, typename T9, typename T10>
557void PrintTo(
558 const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& t,
559 ::std::ostream* os) {
shiqianc97f2f52008-12-11 17:22:59 +0000560 PrintTupleTo(t, os);
shiqiane35fdd92008-12-10 05:08:54 +0000561}
562
563// Overload for std::pair.
564template <typename T1, typename T2>
565void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
566 *os << '(';
567 UniversalPrinter<T1>::Print(value.first, os);
568 *os << ", ";
569 UniversalPrinter<T2>::Print(value.second, os);
570 *os << ')';
571}
572
573// Implements printing a non-reference type T by letting the compiler
574// pick the right overload of PrintTo() for T.
575template <typename T>
576class UniversalPrinter {
577 public:
578 // MSVC warns about adding const to a function type, so we want to
579 // disable the warning.
580#ifdef _MSC_VER
581#pragma warning(push) // Saves the current warning state.
582#pragma warning(disable:4180) // Temporarily disables warning 4180.
583#endif // _MSC_VER
584
585 // Note: we deliberately don't call this PrintTo(), as that name
586 // conflicts with ::testing::internal::PrintTo in the body of the
587 // function.
588 static void Print(const T& value, ::std::ostream* os) {
589 // By default, ::testing::internal::PrintTo() is used for printing
590 // the value.
591 //
592 // Thanks to Koenig look-up, if T is a class and has its own
593 // PrintTo() function defined in its namespace, that function will
594 // be visible here. Since it is more specific than the generic ones
595 // in ::testing::internal, it will be picked by the compiler in the
596 // following statement - exactly what we want.
597 PrintTo(value, os);
598 }
599
600 // A convenient wrapper for Print() that returns the print-out as a
601 // string.
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000602 static string PrintToString(const T& value) {
shiqiane35fdd92008-12-10 05:08:54 +0000603 ::std::stringstream ss;
604 Print(value, &ss);
605 return ss.str();
606 }
607
608#ifdef _MSC_VER
609#pragma warning(pop) // Restores the warning state.
610#endif // _MSC_VER
611};
612
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000613// UniversalPrintArray(begin, len, os) prints an array of 'len'
614// elements, starting at address 'begin'.
615template <typename T>
616void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
617 if (len == 0) {
618 *os << "{}";
619 } else {
620 *os << "{ ";
621 const size_t kThreshold = 18;
622 const size_t kChunkSize = 8;
623 // If the array has more than kThreshold elements, we'll have to
624 // omit some details by printing only the first and the last
625 // kChunkSize elements.
626 // TODO(wan@google.com): let the user control the threshold using a flag.
627 if (len <= kThreshold) {
628 PrintRawArrayTo(begin, len, os);
629 } else {
630 PrintRawArrayTo(begin, kChunkSize, os);
631 *os << ", ..., ";
632 PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
633 }
634 *os << " }";
635 }
636}
637// This overload prints a (const) char array compactly.
638void UniversalPrintArray(const char* begin, size_t len, ::std::ostream* os);
639
640// Prints an array of 'len' elements, starting at address 'begin', to a string.
641template <typename T>
642string UniversalPrintArrayToString(const T* begin, size_t len) {
643 ::std::stringstream ss;
644 UniversalPrintArray(begin, len, &ss);
645 return ss.str();
646}
647
shiqiane35fdd92008-12-10 05:08:54 +0000648// Implements printing an array type T[N].
649template <typename T, size_t N>
650class UniversalPrinter<T[N]> {
651 public:
652 // Prints the given array, omitting some elements when there are too
653 // many.
654 static void Print(const T (&a)[N], ::std::ostream* os) {
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000655 UniversalPrintArray(a, N, os);
shiqiane35fdd92008-12-10 05:08:54 +0000656 }
657
658 // A convenient wrapper for Print() that returns the print-out as a
659 // string.
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000660 static string PrintToString(const T (&a)[N]) {
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000661 return UniversalPrintArrayToString(a, N);
shiqiane35fdd92008-12-10 05:08:54 +0000662 }
663};
664
665// Implements printing a reference type T&.
666template <typename T>
667class UniversalPrinter<T&> {
668 public:
669 // MSVC warns about adding const to a function type, so we want to
670 // disable the warning.
671#ifdef _MSC_VER
672#pragma warning(push) // Saves the current warning state.
673#pragma warning(disable:4180) // Temporarily disables warning 4180.
674#endif // _MSC_VER
675
676 static void Print(const T& value, ::std::ostream* os) {
677 // Prints the address of the value. We use reinterpret_cast here
678 // as static_cast doesn't compile when T is a function type.
679 *os << "@" << reinterpret_cast<const void*>(&value) << " ";
680
681 // Then prints the value itself.
682 UniversalPrinter<T>::Print(value, os);
683 }
684
685 // A convenient wrapper for Print() that returns the print-out as a
686 // string.
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000687 static string PrintToString(const T& value) {
shiqiane35fdd92008-12-10 05:08:54 +0000688 ::std::stringstream ss;
689 Print(value, &ss);
690 return ss.str();
691 }
692
693#ifdef _MSC_VER
694#pragma warning(pop) // Restores the warning state.
695#endif // _MSC_VER
696};
697
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000698// Prints a value tersely: for a reference type, the referenced value
699// (but not the address) is printed; for a (const) char pointer, the
700// NUL-terminated string (but not the pointer) is printed.
zhanyong.wance198ff2009-02-12 01:34:27 +0000701template <typename T>
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000702void UniversalTersePrint(const T& value, ::std::ostream* os) {
zhanyong.wance198ff2009-02-12 01:34:27 +0000703 UniversalPrinter<T>::Print(value, os);
704}
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000705inline void UniversalTersePrint(const char* str, ::std::ostream* os) {
706 if (str == NULL) {
707 *os << "NULL";
708 } else {
709 UniversalPrinter<string>::Print(string(str), os);
710 }
711}
712inline void UniversalTersePrint(char* str, ::std::ostream* os) {
713 UniversalTersePrint(static_cast<const char*>(str), os);
714}
715
zhanyong.wanb8243162009-06-04 05:48:20 +0000716// Prints a value using the type inferred by the compiler. The
717// difference between this and UniversalTersePrint() is that for a
718// (const) char pointer, this prints both the pointer and the
719// NUL-terminated string.
720template <typename T>
721void UniversalPrint(const T& value, ::std::ostream* os) {
722 UniversalPrinter<T>::Print(value, os);
723}
724
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000725// Prints the fields of a tuple tersely to a string vector, one
726// element for each field. See the comment before
727// UniversalTersePrint() for how we define "tersely".
728template <typename Tuple>
729Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
730 Strings result;
731 TuplePrefixPrinter< ::std::tr1::tuple_size<Tuple>::value>::
732 TersePrintPrefixToStrings(value, &result);
733 return result;
734}
zhanyong.wance198ff2009-02-12 01:34:27 +0000735
shiqiane35fdd92008-12-10 05:08:54 +0000736} // namespace internal
737} // namespace testing
738
739#endif // GMOCK_INCLUDE_GMOCK_GMOCK_PRINTERS_H_