blob: e07d92af7943f9a61fb971f1cc7121b0c4f34636 [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
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000437typedef ::std::vector<string> Strings;
438
439// This helper template allows PrintTo() for tuples and
440// UniversalTersePrintTupleFieldsToStrings() to be defined by
shiqiane35fdd92008-12-10 05:08:54 +0000441// induction on the number of tuple fields. The idea is that
442// TuplePrefixPrinter<N>::PrintPrefixTo(t, os) prints the first N
443// fields in tuple t, and can be defined in terms of
444// TuplePrefixPrinter<N - 1>.
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000445
446// The inductive case.
shiqiane35fdd92008-12-10 05:08:54 +0000447template <size_t N>
448struct TuplePrefixPrinter {
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000449 // Prints the first N fields of a tuple.
shiqiane35fdd92008-12-10 05:08:54 +0000450 template <typename Tuple>
451 static void PrintPrefixTo(const Tuple& t, ::std::ostream* os) {
452 TuplePrefixPrinter<N - 1>::PrintPrefixTo(t, os);
453 *os << ", ";
454 UniversalPrinter<typename ::std::tr1::tuple_element<N - 1, Tuple>::type>
455 ::Print(::std::tr1::get<N - 1>(t), os);
456 }
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000457
458 // Tersely prints the first N fields of a tuple to a string vector,
459 // one element for each field.
460 template <typename Tuple>
461 static void TersePrintPrefixToStrings(const Tuple& t, Strings* strings) {
462 TuplePrefixPrinter<N - 1>::TersePrintPrefixToStrings(t, strings);
463 ::std::stringstream ss;
464 UniversalTersePrint(::std::tr1::get<N - 1>(t), &ss);
465 strings->push_back(ss.str());
466 }
shiqiane35fdd92008-12-10 05:08:54 +0000467};
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000468
469// Base cases.
shiqiane35fdd92008-12-10 05:08:54 +0000470template <>
471struct TuplePrefixPrinter<0> {
472 template <typename Tuple>
473 static void PrintPrefixTo(const Tuple&, ::std::ostream*) {}
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000474
475 template <typename Tuple>
476 static void TersePrintPrefixToStrings(const Tuple&, Strings*) {}
shiqiane35fdd92008-12-10 05:08:54 +0000477};
478template <>
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000479template <typename Tuple>
480void TuplePrefixPrinter<1>::PrintPrefixTo(const Tuple& t, ::std::ostream* os) {
481 UniversalPrinter<typename ::std::tr1::tuple_element<0, Tuple>::type>::
482 Print(::std::tr1::get<0>(t), os);
483}
shiqiane35fdd92008-12-10 05:08:54 +0000484
shiqianc97f2f52008-12-11 17:22:59 +0000485// Helper function for printing a tuple. T must be instantiated with
486// a tuple type.
487template <typename T>
488void PrintTupleTo(const T& t, ::std::ostream* os) {
489 *os << "(";
490 TuplePrefixPrinter< ::std::tr1::tuple_size<T>::value>::
491 PrintPrefixTo(t, os);
492 *os << ")";
493}
494
495// Overloaded PrintTo() for tuples of various arities. We support
496// tuples of up-to 10 fields. The following implementation works
497// regardless of whether tr1::tuple is implemented using the
498// non-standard variadic template feature or not.
499
500inline void PrintTo(const ::std::tr1::tuple<>& t, ::std::ostream* os) {
501 PrintTupleTo(t, os);
502}
503
504template <typename T1>
505void PrintTo(const ::std::tr1::tuple<T1>& t, ::std::ostream* os) {
506 PrintTupleTo(t, os);
507}
508
509template <typename T1, typename T2>
510void PrintTo(const ::std::tr1::tuple<T1, T2>& t, ::std::ostream* os) {
511 PrintTupleTo(t, os);
512}
513
514template <typename T1, typename T2, typename T3>
515void PrintTo(const ::std::tr1::tuple<T1, T2, T3>& t, ::std::ostream* os) {
516 PrintTupleTo(t, os);
517}
518
519template <typename T1, typename T2, typename T3, typename T4>
520void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4>& t, ::std::ostream* os) {
521 PrintTupleTo(t, os);
522}
523
524template <typename T1, typename T2, typename T3, typename T4, typename T5>
525void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5>& t,
526 ::std::ostream* os) {
527 PrintTupleTo(t, os);
528}
529
530template <typename T1, typename T2, typename T3, typename T4, typename T5,
531 typename T6>
532void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6>& t,
533 ::std::ostream* os) {
534 PrintTupleTo(t, os);
535}
536
537template <typename T1, typename T2, typename T3, typename T4, typename T5,
538 typename T6, typename T7>
539void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7>& t,
540 ::std::ostream* os) {
541 PrintTupleTo(t, os);
542}
543
544template <typename T1, typename T2, typename T3, typename T4, typename T5,
545 typename T6, typename T7, typename T8>
546void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8>& t,
547 ::std::ostream* os) {
548 PrintTupleTo(t, os);
549}
550
551template <typename T1, typename T2, typename T3, typename T4, typename T5,
552 typename T6, typename T7, typename T8, typename T9>
553void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9>& t,
554 ::std::ostream* os) {
555 PrintTupleTo(t, os);
556}
557
shiqiane35fdd92008-12-10 05:08:54 +0000558template <typename T1, typename T2, typename T3, typename T4, typename T5,
559 typename T6, typename T7, typename T8, typename T9, typename T10>
560void PrintTo(
561 const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& t,
562 ::std::ostream* os) {
shiqianc97f2f52008-12-11 17:22:59 +0000563 PrintTupleTo(t, os);
shiqiane35fdd92008-12-10 05:08:54 +0000564}
565
566// Overload for std::pair.
567template <typename T1, typename T2>
568void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
569 *os << '(';
570 UniversalPrinter<T1>::Print(value.first, os);
571 *os << ", ";
572 UniversalPrinter<T2>::Print(value.second, os);
573 *os << ')';
574}
575
576// Implements printing a non-reference type T by letting the compiler
577// pick the right overload of PrintTo() for T.
578template <typename T>
579class UniversalPrinter {
580 public:
581 // MSVC warns about adding const to a function type, so we want to
582 // disable the warning.
583#ifdef _MSC_VER
584#pragma warning(push) // Saves the current warning state.
585#pragma warning(disable:4180) // Temporarily disables warning 4180.
586#endif // _MSC_VER
587
588 // Note: we deliberately don't call this PrintTo(), as that name
589 // conflicts with ::testing::internal::PrintTo in the body of the
590 // function.
591 static void Print(const T& value, ::std::ostream* os) {
592 // By default, ::testing::internal::PrintTo() is used for printing
593 // the value.
594 //
595 // Thanks to Koenig look-up, if T is a class and has its own
596 // PrintTo() function defined in its namespace, that function will
597 // be visible here. Since it is more specific than the generic ones
598 // in ::testing::internal, it will be picked by the compiler in the
599 // following statement - exactly what we want.
600 PrintTo(value, os);
601 }
602
603 // A convenient wrapper for Print() that returns the print-out as a
604 // string.
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000605 static string PrintToString(const T& value) {
shiqiane35fdd92008-12-10 05:08:54 +0000606 ::std::stringstream ss;
607 Print(value, &ss);
608 return ss.str();
609 }
610
611#ifdef _MSC_VER
612#pragma warning(pop) // Restores the warning state.
613#endif // _MSC_VER
614};
615
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000616// UniversalPrintArray(begin, len, os) prints an array of 'len'
617// elements, starting at address 'begin'.
618template <typename T>
619void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
620 if (len == 0) {
621 *os << "{}";
622 } else {
623 *os << "{ ";
624 const size_t kThreshold = 18;
625 const size_t kChunkSize = 8;
626 // If the array has more than kThreshold elements, we'll have to
627 // omit some details by printing only the first and the last
628 // kChunkSize elements.
629 // TODO(wan@google.com): let the user control the threshold using a flag.
630 if (len <= kThreshold) {
631 PrintRawArrayTo(begin, len, os);
632 } else {
633 PrintRawArrayTo(begin, kChunkSize, os);
634 *os << ", ..., ";
635 PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
636 }
637 *os << " }";
638 }
639}
640// This overload prints a (const) char array compactly.
641void UniversalPrintArray(const char* begin, size_t len, ::std::ostream* os);
642
643// Prints an array of 'len' elements, starting at address 'begin', to a string.
644template <typename T>
645string UniversalPrintArrayToString(const T* begin, size_t len) {
646 ::std::stringstream ss;
647 UniversalPrintArray(begin, len, &ss);
648 return ss.str();
649}
650
shiqiane35fdd92008-12-10 05:08:54 +0000651// Implements printing an array type T[N].
652template <typename T, size_t N>
653class UniversalPrinter<T[N]> {
654 public:
655 // Prints the given array, omitting some elements when there are too
656 // many.
657 static void Print(const T (&a)[N], ::std::ostream* os) {
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000658 UniversalPrintArray(a, N, os);
shiqiane35fdd92008-12-10 05:08:54 +0000659 }
660
661 // A convenient wrapper for Print() that returns the print-out as a
662 // string.
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000663 static string PrintToString(const T (&a)[N]) {
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000664 return UniversalPrintArrayToString(a, N);
shiqiane35fdd92008-12-10 05:08:54 +0000665 }
666};
667
668// Implements printing a reference type T&.
669template <typename T>
670class UniversalPrinter<T&> {
671 public:
672 // MSVC warns about adding const to a function type, so we want to
673 // disable the warning.
674#ifdef _MSC_VER
675#pragma warning(push) // Saves the current warning state.
676#pragma warning(disable:4180) // Temporarily disables warning 4180.
677#endif // _MSC_VER
678
679 static void Print(const T& value, ::std::ostream* os) {
680 // Prints the address of the value. We use reinterpret_cast here
681 // as static_cast doesn't compile when T is a function type.
682 *os << "@" << reinterpret_cast<const void*>(&value) << " ";
683
684 // Then prints the value itself.
685 UniversalPrinter<T>::Print(value, os);
686 }
687
688 // A convenient wrapper for Print() that returns the print-out as a
689 // string.
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000690 static string PrintToString(const T& value) {
shiqiane35fdd92008-12-10 05:08:54 +0000691 ::std::stringstream ss;
692 Print(value, &ss);
693 return ss.str();
694 }
695
696#ifdef _MSC_VER
697#pragma warning(pop) // Restores the warning state.
698#endif // _MSC_VER
699};
700
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000701// Prints a value tersely: for a reference type, the referenced value
702// (but not the address) is printed; for a (const) char pointer, the
703// NUL-terminated string (but not the pointer) is printed.
zhanyong.wance198ff2009-02-12 01:34:27 +0000704template <typename T>
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000705void UniversalTersePrint(const T& value, ::std::ostream* os) {
zhanyong.wance198ff2009-02-12 01:34:27 +0000706 UniversalPrinter<T>::Print(value, os);
707}
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000708inline void UniversalTersePrint(const char* str, ::std::ostream* os) {
709 if (str == NULL) {
710 *os << "NULL";
711 } else {
712 UniversalPrinter<string>::Print(string(str), os);
713 }
714}
715inline void UniversalTersePrint(char* str, ::std::ostream* os) {
716 UniversalTersePrint(static_cast<const char*>(str), os);
717}
718
zhanyong.wanb8243162009-06-04 05:48:20 +0000719// Prints a value using the type inferred by the compiler. The
720// difference between this and UniversalTersePrint() is that for a
721// (const) char pointer, this prints both the pointer and the
722// NUL-terminated string.
723template <typename T>
724void UniversalPrint(const T& value, ::std::ostream* os) {
725 UniversalPrinter<T>::Print(value, os);
726}
727
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000728// Prints the fields of a tuple tersely to a string vector, one
729// element for each field. See the comment before
730// UniversalTersePrint() for how we define "tersely".
731template <typename Tuple>
732Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
733 Strings result;
734 TuplePrefixPrinter< ::std::tr1::tuple_size<Tuple>::value>::
735 TersePrintPrefixToStrings(value, &result);
736 return result;
737}
zhanyong.wance198ff2009-02-12 01:34:27 +0000738
shiqiane35fdd92008-12-10 05:08:54 +0000739} // namespace internal
740} // namespace testing
741
742#endif // GMOCK_INCLUDE_GMOCK_GMOCK_PRINTERS_H_