1 // 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 Test - The Google C++ Testing Framework
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 //
39 // 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.
52 //
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
56 // printed.
57 //
58 // We also provide some convenient wrappers:
59 //
60 // // Prints a value to a string. For a (const or not) char
61 // // pointer, the NUL-terminated string (but not the pointer) is
62 // // printed.
63 // std::string ::testing::PrintToString(const T& value);
64 //
65 // // Prints a value tersely: for a reference type, the referenced
66 // // value (but not the address) is printed; for a (const or not) char
67 // // pointer, the NUL-terminated string (but not the pointer) is
68 // // printed.
69 // void ::testing::internal::UniversalTersePrint(const T& value, ostream*);
70 //
71 // // Prints value using the type inferred by the compiler. The difference
72 // // from UniversalTersePrint() is that this function prints both the
73 // // pointer and the NUL-terminated string for a (const or not) char pointer.
74 // void ::testing::internal::UniversalPrint(const T& value, ostream*);
75 //
76 // // Prints the fields of a tuple tersely to a string vector, one
77 // // element for each field. Tuple support must be enabled in
78 // // gtest-port.h.
79 // std::vector<string> UniversalTersePrintTupleFieldsToStrings(
80 // const Tuple& value);
81 //
82 // Known limitation:
83 //
84 // The print primitives print the elements of an STL-style container
85 // using the compiler-inferred type of *iter where iter is a
86 // const_iterator of the container. When const_iterator is an input
87 // iterator but not a forward iterator, this inferred type may not
88 // match value_type, and the print output may be incorrect. In
89 // practice, this is rarely a problem as for most containers
90 // const_iterator is a forward iterator. We'll fix this if there's an
91 // actual need for it. Note that this fix cannot rely on value_type
92 // being defined as many user-defined container types don't have
93 // value_type.
94
95 #ifndef GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
96 #define GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
97 #ifndef _MSC_VER
98 #pragma GCC system_header
99 #endif
100
101 #include <ostream> // NOLINT
102 #include <sstream>
103 #include <string>
104 #include <utility>
105 #include <vector>
106 #include "gtest/internal/gtest-port.h"
107 #include "gtest/internal/gtest-internal.h"
108
109 namespace testing {
110
111 // Definitions in the 'internal' and 'internal2' name spaces are
112 // subject to change without notice. DO NOT USE THEM IN USER CODE!
113 namespace internal2 {
114
115 // Prints the given number of bytes in the given object to the given
116 // ostream.
117 GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
118 size_t count,
119 ::std::ostream* os);
120
121 // For selecting which printer to use when a given type has neither <<
122 // nor PrintTo().
123 enum TypeKind {
124 kProtobuf, // a protobuf type
125 kConvertibleToInteger, // a type implicitly convertible to BiggestInt
126 // (e.g. a named or unnamed enum type)
127 kOtherType // anything else
128 };
129
130 // TypeWithoutFormatter<T, kTypeKind>::PrintValue(value, os) is called
131 // by the universal printer to print a value of type T when neither
132 // operator<< nor PrintTo() is defined for T, where kTypeKind is the
133 // "kind" of T as defined by enum TypeKind.
134 template <typename T, TypeKind kTypeKind>
135 class TypeWithoutFormatter {
136 public:
137 // This default version is called when kTypeKind is kOtherType.
PrintValue(const T & value,::std::ostream * os)138 static void PrintValue(const T& value, ::std::ostream* os) {
139 PrintBytesInObjectTo(reinterpret_cast<const unsigned char*>(&value),
140 sizeof(value), os);
141 }
142 };
143
144 // We print a protobuf using its ShortDebugString() when the string
145 // doesn't exceed this many characters; otherwise we print it using
146 // DebugString() for better readability.
147 const size_t kProtobufOneLinerMaxLength = 50;
148
149 template <typename T>
150 class TypeWithoutFormatter<T, kProtobuf> {
151 public:
PrintValue(const T & value,::std::ostream * os)152 static void PrintValue(const T& value, ::std::ostream* os) {
153 const ::testing::internal::string short_str = value.ShortDebugString();
154 const ::testing::internal::string pretty_str =
155 short_str.length() <= kProtobufOneLinerMaxLength ?
156 short_str : ("\n" + value.DebugString());
157 *os << ("<" + pretty_str + ">");
158 }
159 };
160
161 template <typename T>
162 class TypeWithoutFormatter<T, kConvertibleToInteger> {
163 public:
164 // Since T has no << operator or PrintTo() but can be implicitly
165 // converted to BiggestInt, we print it as a BiggestInt.
166 //
167 // Most likely T is an enum type (either named or unnamed), in which
168 // case printing it as an integer is the desired behavior. In case
169 // T is not an enum, printing it as an integer is the best we can do
170 // given that it has no user-defined printer.
PrintValue(const T & value,::std::ostream * os)171 static void PrintValue(const T& value, ::std::ostream* os) {
172 const internal::BiggestInt kBigInt = value;
173 *os << kBigInt;
174 }
175 };
176
177 // Prints the given value to the given ostream. If the value is a
178 // protocol message, its debug string is printed; if it's an enum or
179 // of a type implicitly convertible to BiggestInt, it's printed as an
180 // integer; otherwise the bytes in the value are printed. This is
181 // what UniversalPrinter<T>::Print() does when it knows nothing about
182 // type T and T has neither << operator nor PrintTo().
183 //
184 // A user can override this behavior for a class type Foo by defining
185 // a << operator in the namespace where Foo is defined.
186 //
187 // We put this operator in namespace 'internal2' instead of 'internal'
188 // to simplify the implementation, as much code in 'internal' needs to
189 // use << in STL, which would conflict with our own << were it defined
190 // in 'internal'.
191 //
192 // Note that this operator<< takes a generic std::basic_ostream<Char,
193 // CharTraits> type instead of the more restricted std::ostream. If
194 // we define it to take an std::ostream instead, we'll get an
195 // "ambiguous overloads" compiler error when trying to print a type
196 // Foo that supports streaming to std::basic_ostream<Char,
197 // CharTraits>, as the compiler cannot tell whether
198 // operator<<(std::ostream&, const T&) or
199 // operator<<(std::basic_stream<Char, CharTraits>, const Foo&) is more
200 // specific.
201 template <typename Char, typename CharTraits, typename T>
202 ::std::basic_ostream<Char, CharTraits>& operator<<(
203 ::std::basic_ostream<Char, CharTraits>& os, const T& x) {
204 TypeWithoutFormatter<T,
205 (internal::IsAProtocolMessage<T>::value ? kProtobuf :
206 internal::ImplicitlyConvertible<const T&, internal::BiggestInt>::value ?
207 kConvertibleToInteger : kOtherType)>::PrintValue(x, &os);
208 return os;
209 }
210
211 } // namespace internal2
212 } // namespace testing
213
214 // This namespace MUST NOT BE NESTED IN ::testing, or the name look-up
215 // magic needed for implementing UniversalPrinter won't work.
216 namespace testing_internal {
217
218 // Used to print a value that is not an STL-style container when the
219 // user doesn't define PrintTo() for it.
220 template <typename T>
DefaultPrintNonContainerTo(const T & value,::std::ostream * os)221 void DefaultPrintNonContainerTo(const T& value, ::std::ostream* os) {
222 // With the following statement, during unqualified name lookup,
223 // testing::internal2::operator<< appears as if it was declared in
224 // the nearest enclosing namespace that contains both
225 // ::testing_internal and ::testing::internal2, i.e. the global
226 // namespace. For more details, refer to the C++ Standard section
227 // 7.3.4-1 [namespace.udir]. This allows us to fall back onto
228 // testing::internal2::operator<< in case T doesn't come with a <<
229 // operator.
230 //
231 // We cannot write 'using ::testing::internal2::operator<<;', which
232 // gcc 3.3 fails to compile due to a compiler bug.
233 using namespace ::testing::internal2; // NOLINT
234
235 // Assuming T is defined in namespace foo, in the next statement,
236 // the compiler will consider all of:
237 //
238 // 1. foo::operator<< (thanks to Koenig look-up),
239 // 2. ::operator<< (as the current namespace is enclosed in ::),
240 // 3. testing::internal2::operator<< (thanks to the using statement above).
241 //
242 // The operator<< whose type matches T best will be picked.
243 //
244 // We deliberately allow #2 to be a candidate, as sometimes it's
245 // impossible to define #1 (e.g. when foo is ::std, defining
246 // anything in it is undefined behavior unless you are a compiler
247 // vendor.).
248 *os << value;
249 }
250
251 } // namespace testing_internal
252
253 namespace testing {
254 namespace internal {
255
256 // UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
257 // value to the given ostream. The caller must ensure that
258 // 'ostream_ptr' is not NULL, or the behavior is undefined.
259 //
260 // We define UniversalPrinter as a class template (as opposed to a
261 // function template), as we need to partially specialize it for
262 // reference types, which cannot be done with function templates.
263 template <typename T>
264 class UniversalPrinter;
265
266 template <typename T>
267 void UniversalPrint(const T& value, ::std::ostream* os);
268
269 // Used to print an STL-style container when the user doesn't define
270 // a PrintTo() for it.
271 template <typename C>
DefaultPrintTo(IsContainer,false_type,const C & container,::std::ostream * os)272 void DefaultPrintTo(IsContainer /* dummy */,
273 false_type /* is not a pointer */,
274 const C& container, ::std::ostream* os) {
275 const size_t kMaxCount = 32; // The maximum number of elements to print.
276 *os << '{';
277 size_t count = 0;
278 for (typename C::const_iterator it = container.begin();
279 it != container.end(); ++it, ++count) {
280 if (count > 0) {
281 *os << ',';
282 if (count == kMaxCount) { // Enough has been printed.
283 *os << " ...";
284 break;
285 }
286 }
287 *os << ' ';
288 // We cannot call PrintTo(*it, os) here as PrintTo() doesn't
289 // handle *it being a native array.
290 internal::UniversalPrint(*it, os);
291 }
292
293 if (count > 0) {
294 *os << ' ';
295 }
296 *os << '}';
297 }
298
299 // Used to print a pointer that is neither a char pointer nor a member
300 // pointer, when the user doesn't define PrintTo() for it. (A member
301 // variable pointer or member function pointer doesn't really point to
302 // a location in the address space. Their representation is
303 // implementation-defined. Therefore they will be printed as raw
304 // bytes.)
305 template <typename T>
DefaultPrintTo(IsNotContainer,true_type,T * p,::std::ostream * os)306 void DefaultPrintTo(IsNotContainer /* dummy */,
307 true_type /* is a pointer */,
308 T* p, ::std::ostream* os) {
309 if (p == NULL) {
310 *os << "NULL";
311 } else {
312 // C++ doesn't allow casting from a function pointer to any object
313 // pointer.
314 //
315 // IsTrue() silences warnings: "Condition is always true",
316 // "unreachable code".
317 if (IsTrue(ImplicitlyConvertible<T*, const void*>::value)) {
318 // T is not a function type. We just call << to print p,
319 // relying on ADL to pick up user-defined << for their pointer
320 // types, if any.
321 *os << p;
322 } else {
323 // T is a function type, so '*os << p' doesn't do what we want
324 // (it just prints p as bool). We want to print p as a const
325 // void*. However, we cannot cast it to const void* directly,
326 // even using reinterpret_cast, as earlier versions of gcc
327 // (e.g. 3.4.5) cannot compile the cast when p is a function
328 // pointer. Casting to UInt64 first solves the problem.
329 *os << reinterpret_cast<const void*>(
330 reinterpret_cast<internal::UInt64>(p));
331 }
332 }
333 }
334
335 // Used to print a non-container, non-pointer value when the user
336 // doesn't define PrintTo() for it.
337 template <typename T>
DefaultPrintTo(IsNotContainer,false_type,const T & value,::std::ostream * os)338 void DefaultPrintTo(IsNotContainer /* dummy */,
339 false_type /* is not a pointer */,
340 const T& value, ::std::ostream* os) {
341 ::testing_internal::DefaultPrintNonContainerTo(value, os);
342 }
343
344 // Prints the given value using the << operator if it has one;
345 // otherwise prints the bytes in it. This is what
346 // UniversalPrinter<T>::Print() does when PrintTo() is not specialized
347 // or overloaded for type T.
348 //
349 // A user can override this behavior for a class type Foo by defining
350 // an overload of PrintTo() in the namespace where Foo is defined. We
351 // give the user this option as sometimes defining a << operator for
352 // Foo is not desirable (e.g. the coding style may prevent doing it,
353 // or there is already a << operator but it doesn't do what the user
354 // wants).
355 template <typename T>
PrintTo(const T & value,::std::ostream * os)356 void PrintTo(const T& value, ::std::ostream* os) {
357 // DefaultPrintTo() is overloaded. The type of its first two
358 // arguments determine which version will be picked. If T is an
359 // STL-style container, the version for container will be called; if
360 // T is a pointer, the pointer version will be called; otherwise the
361 // generic version will be called.
362 //
363 // Note that we check for container types here, prior to we check
364 // for protocol message types in our operator<<. The rationale is:
365 //
366 // For protocol messages, we want to give people a chance to
367 // override Google Mock's format by defining a PrintTo() or
368 // operator<<. For STL containers, other formats can be
369 // incompatible with Google Mock's format for the container
370 // elements; therefore we check for container types here to ensure
371 // that our format is used.
372 //
373 // The second argument of DefaultPrintTo() is needed to bypass a bug
374 // in Symbian's C++ compiler that prevents it from picking the right
375 // overload between:
376 //
377 // PrintTo(const T& x, ...);
378 // PrintTo(T* x, ...);
379 DefaultPrintTo(IsContainerTest<T>(0), is_pointer<T>(), value, os);
380 }
381
382 // The following list of PrintTo() overloads tells
383 // UniversalPrinter<T>::Print() how to print standard types (built-in
384 // types, strings, plain arrays, and pointers).
385
386 // Overloads for various char types.
387 GTEST_API_ void PrintTo(unsigned char c, ::std::ostream* os);
388 GTEST_API_ void PrintTo(signed char c, ::std::ostream* os);
PrintTo(char c,::std::ostream * os)389 inline void PrintTo(char c, ::std::ostream* os) {
390 // When printing a plain char, we always treat it as unsigned. This
391 // way, the output won't be affected by whether the compiler thinks
392 // char is signed or not.
393 PrintTo(static_cast<unsigned char>(c), os);
394 }
395
396 // Overloads for other simple built-in types.
PrintTo(bool x,::std::ostream * os)397 inline void PrintTo(bool x, ::std::ostream* os) {
398 *os << (x ? "true" : "false");
399 }
400
401 // Overload for wchar_t type.
402 // Prints a wchar_t as a symbol if it is printable or as its internal
403 // code otherwise and also as its decimal code (except for L'\0').
404 // The L'\0' char is printed as "L'\\0'". The decimal code is printed
405 // as signed integer when wchar_t is implemented by the compiler
406 // as a signed type and is printed as an unsigned integer when wchar_t
407 // is implemented as an unsigned type.
408 GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os);
409
410 // Overloads for C strings.
411 GTEST_API_ void PrintTo(const char* s, ::std::ostream* os);
PrintTo(char * s,::std::ostream * os)412 inline void PrintTo(char* s, ::std::ostream* os) {
413 PrintTo(ImplicitCast_<const char*>(s), os);
414 }
415
416 // signed/unsigned char is often used for representing binary data, so
417 // we print pointers to it as void* to be safe.
PrintTo(const signed char * s,::std::ostream * os)418 inline void PrintTo(const signed char* s, ::std::ostream* os) {
419 PrintTo(ImplicitCast_<const void*>(s), os);
420 }
PrintTo(signed char * s,::std::ostream * os)421 inline void PrintTo(signed char* s, ::std::ostream* os) {
422 PrintTo(ImplicitCast_<const void*>(s), os);
423 }
PrintTo(const unsigned char * s,::std::ostream * os)424 inline void PrintTo(const unsigned char* s, ::std::ostream* os) {
425 PrintTo(ImplicitCast_<const void*>(s), os);
426 }
PrintTo(unsigned char * s,::std::ostream * os)427 inline void PrintTo(unsigned char* s, ::std::ostream* os) {
428 PrintTo(ImplicitCast_<const void*>(s), os);
429 }
430
431 // MSVC can be configured to define wchar_t as a typedef of unsigned
432 // short. It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native
433 // type. When wchar_t is a typedef, defining an overload for const
434 // wchar_t* would cause unsigned short* be printed as a wide string,
435 // possibly causing invalid memory accesses.
436 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
437 // Overloads for wide C strings
438 GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os);
PrintTo(wchar_t * s,::std::ostream * os)439 inline void PrintTo(wchar_t* s, ::std::ostream* os) {
440 PrintTo(ImplicitCast_<const wchar_t*>(s), os);
441 }
442 #endif
443
444 // Overload for C arrays. Multi-dimensional arrays are printed
445 // properly.
446
447 // Prints the given number of elements in an array, without printing
448 // the curly braces.
449 template <typename T>
PrintRawArrayTo(const T a[],size_t count,::std::ostream * os)450 void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
451 UniversalPrint(a[0], os);
452 for (size_t i = 1; i != count; i++) {
453 *os << ", ";
454 UniversalPrint(a[i], os);
455 }
456 }
457
458 // Overloads for ::string and ::std::string.
459 #if GTEST_HAS_GLOBAL_STRING
460 GTEST_API_ void PrintStringTo(const ::string&s, ::std::ostream* os);
PrintTo(const::string & s,::std::ostream * os)461 inline void PrintTo(const ::string& s, ::std::ostream* os) {
462 PrintStringTo(s, os);
463 }
464 #endif // GTEST_HAS_GLOBAL_STRING
465
466 GTEST_API_ void PrintStringTo(const ::std::string&s, ::std::ostream* os);
PrintTo(const::std::string & s,::std::ostream * os)467 inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
468 PrintStringTo(s, os);
469 }
470
471 // Overloads for ::wstring and ::std::wstring.
472 #if GTEST_HAS_GLOBAL_WSTRING
473 GTEST_API_ void PrintWideStringTo(const ::wstring&s, ::std::ostream* os);
PrintTo(const::wstring & s,::std::ostream * os)474 inline void PrintTo(const ::wstring& s, ::std::ostream* os) {
475 PrintWideStringTo(s, os);
476 }
477 #endif // GTEST_HAS_GLOBAL_WSTRING
478
479 #if GTEST_HAS_STD_WSTRING
480 GTEST_API_ void PrintWideStringTo(const ::std::wstring&s, ::std::ostream* os);
PrintTo(const::std::wstring & s,::std::ostream * os)481 inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
482 PrintWideStringTo(s, os);
483 }
484 #endif // GTEST_HAS_STD_WSTRING
485
486 #if GTEST_HAS_TR1_TUPLE
487 // Overload for ::std::tr1::tuple. Needed for printing function arguments,
488 // which are packed as tuples.
489
490 // Helper function for printing a tuple. T must be instantiated with
491 // a tuple type.
492 template <typename T>
493 void PrintTupleTo(const T& t, ::std::ostream* os);
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
PrintTo(const::std::tr1::tuple<> & t,::std::ostream * os)500 inline void PrintTo(const ::std::tr1::tuple<>& t, ::std::ostream* os) {
501 PrintTupleTo(t, os);
502 }
503
504 template <typename T1>
PrintTo(const::std::tr1::tuple<T1> & t,::std::ostream * os)505 void PrintTo(const ::std::tr1::tuple<T1>& t, ::std::ostream* os) {
506 PrintTupleTo(t, os);
507 }
508
509 template <typename T1, typename T2>
PrintTo(const::std::tr1::tuple<T1,T2> & t,::std::ostream * os)510 void PrintTo(const ::std::tr1::tuple<T1, T2>& t, ::std::ostream* os) {
511 PrintTupleTo(t, os);
512 }
513
514 template <typename T1, typename T2, typename T3>
PrintTo(const::std::tr1::tuple<T1,T2,T3> & t,::std::ostream * os)515 void PrintTo(const ::std::tr1::tuple<T1, T2, T3>& t, ::std::ostream* os) {
516 PrintTupleTo(t, os);
517 }
518
519 template <typename T1, typename T2, typename T3, typename T4>
PrintTo(const::std::tr1::tuple<T1,T2,T3,T4> & t,::std::ostream * os)520 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4>& t, ::std::ostream* os) {
521 PrintTupleTo(t, os);
522 }
523
524 template <typename T1, typename T2, typename T3, typename T4, typename T5>
PrintTo(const::std::tr1::tuple<T1,T2,T3,T4,T5> & t,::std::ostream * os)525 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5>& t,
526 ::std::ostream* os) {
527 PrintTupleTo(t, os);
528 }
529
530 template <typename T1, typename T2, typename T3, typename T4, typename T5,
531 typename T6>
PrintTo(const::std::tr1::tuple<T1,T2,T3,T4,T5,T6> & t,::std::ostream * os)532 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6>& t,
533 ::std::ostream* os) {
534 PrintTupleTo(t, os);
535 }
536
537 template <typename T1, typename T2, typename T3, typename T4, typename T5,
538 typename T6, typename T7>
PrintTo(const::std::tr1::tuple<T1,T2,T3,T4,T5,T6,T7> & t,::std::ostream * os)539 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7>& t,
540 ::std::ostream* os) {
541 PrintTupleTo(t, os);
542 }
543
544 template <typename T1, typename T2, typename T3, typename T4, typename T5,
545 typename T6, typename T7, typename T8>
PrintTo(const::std::tr1::tuple<T1,T2,T3,T4,T5,T6,T7,T8> & t,::std::ostream * os)546 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8>& t,
547 ::std::ostream* os) {
548 PrintTupleTo(t, os);
549 }
550
551 template <typename T1, typename T2, typename T3, typename T4, typename T5,
552 typename T6, typename T7, typename T8, typename T9>
PrintTo(const::std::tr1::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9> & t,::std::ostream * os)553 void 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
558 template <typename T1, typename T2, typename T3, typename T4, typename T5,
559 typename T6, typename T7, typename T8, typename T9, typename T10>
PrintTo(const::std::tr1::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10> & t,::std::ostream * os)560 void PrintTo(
561 const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& t,
562 ::std::ostream* os) {
563 PrintTupleTo(t, os);
564 }
565 #endif // GTEST_HAS_TR1_TUPLE
566
567 // Overload for std::pair.
568 template <typename T1, typename T2>
PrintTo(const::std::pair<T1,T2> & value,::std::ostream * os)569 void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
570 *os << '(';
571 // We cannot use UniversalPrint(value.first, os) here, as T1 may be
572 // a reference type. The same for printing value.second.
573 UniversalPrinter<T1>::Print(value.first, os);
574 *os << ", ";
575 UniversalPrinter<T2>::Print(value.second, os);
576 *os << ')';
577 }
578
579 // Implements printing a non-reference type T by letting the compiler
580 // pick the right overload of PrintTo() for T.
581 template <typename T>
582 class UniversalPrinter {
583 public:
584 // MSVC warns about adding const to a function type, so we want to
585 // disable the warning.
586 #ifdef _MSC_VER
587 # pragma warning(push) // Saves the current warning state.
588 # pragma warning(disable:4180) // Temporarily disables warning 4180.
589 #endif // _MSC_VER
590
591 // Note: we deliberately don't call this PrintTo(), as that name
592 // conflicts with ::testing::internal::PrintTo in the body of the
593 // function.
Print(const T & value,::std::ostream * os)594 static void Print(const T& value, ::std::ostream* os) {
595 // By default, ::testing::internal::PrintTo() is used for printing
596 // the value.
597 //
598 // Thanks to Koenig look-up, if T is a class and has its own
599 // PrintTo() function defined in its namespace, that function will
600 // be visible here. Since it is more specific than the generic ones
601 // in ::testing::internal, it will be picked by the compiler in the
602 // following statement - exactly what we want.
603 PrintTo(value, os);
604 }
605
606 #ifdef _MSC_VER
607 # pragma warning(pop) // Restores the warning state.
608 #endif // _MSC_VER
609 };
610
611 // UniversalPrintArray(begin, len, os) prints an array of 'len'
612 // elements, starting at address 'begin'.
613 template <typename T>
UniversalPrintArray(const T * begin,size_t len,::std::ostream * os)614 void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
615 if (len == 0) {
616 *os << "{}";
617 } else {
618 *os << "{ ";
619 const size_t kThreshold = 18;
620 const size_t kChunkSize = 8;
621 // If the array has more than kThreshold elements, we'll have to
622 // omit some details by printing only the first and the last
623 // kChunkSize elements.
624 // TODO(wan@google.com): let the user control the threshold using a flag.
625 if (len <= kThreshold) {
626 PrintRawArrayTo(begin, len, os);
627 } else {
628 PrintRawArrayTo(begin, kChunkSize, os);
629 *os << ", ..., ";
630 PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
631 }
632 *os << " }";
633 }
634 }
635 // This overload prints a (const) char array compactly.
636 GTEST_API_ void UniversalPrintArray(
637 const char* begin, size_t len, ::std::ostream* os);
638
639 // This overload prints a (const) wchar_t array compactly.
640 GTEST_API_ void UniversalPrintArray(
641 const wchar_t* begin, size_t len, ::std::ostream* os);
642
643 // Implements printing an array type T[N].
644 template <typename T, size_t N>
645 class UniversalPrinter<T[N]> {
646 public:
647 // Prints the given array, omitting some elements when there are too
648 // many.
Print(const T (& a)[N],::std::ostream * os)649 static void Print(const T (&a)[N], ::std::ostream* os) {
650 UniversalPrintArray(a, N, os);
651 }
652 };
653
654 // Implements printing a reference type T&.
655 template <typename T>
656 class UniversalPrinter<T&> {
657 public:
658 // MSVC warns about adding const to a function type, so we want to
659 // disable the warning.
660 #ifdef _MSC_VER
661 # pragma warning(push) // Saves the current warning state.
662 # pragma warning(disable:4180) // Temporarily disables warning 4180.
663 #endif // _MSC_VER
664
Print(const T & value,::std::ostream * os)665 static void Print(const T& value, ::std::ostream* os) {
666 // Prints the address of the value. We use reinterpret_cast here
667 // as static_cast doesn't compile when T is a function type.
668 *os << "@" << reinterpret_cast<const void*>(&value) << " ";
669
670 // Then prints the value itself.
671 UniversalPrint(value, os);
672 }
673
674 #ifdef _MSC_VER
675 # pragma warning(pop) // Restores the warning state.
676 #endif // _MSC_VER
677 };
678
679 // Prints a value tersely: for a reference type, the referenced value
680 // (but not the address) is printed; for a (const) char pointer, the
681 // NUL-terminated string (but not the pointer) is printed.
682
683 template <typename T>
684 class UniversalTersePrinter {
685 public:
Print(const T & value,::std::ostream * os)686 static void Print(const T& value, ::std::ostream* os) {
687 UniversalPrint(value, os);
688 }
689 };
690 template <typename T>
691 class UniversalTersePrinter<T&> {
692 public:
Print(const T & value,::std::ostream * os)693 static void Print(const T& value, ::std::ostream* os) {
694 UniversalPrint(value, os);
695 }
696 };
697 template <typename T, size_t N>
698 class UniversalTersePrinter<T[N]> {
699 public:
Print(const T (& value)[N],::std::ostream * os)700 static void Print(const T (&value)[N], ::std::ostream* os) {
701 UniversalPrinter<T[N]>::Print(value, os);
702 }
703 };
704 template <>
705 class UniversalTersePrinter<const char*> {
706 public:
Print(const char * str,::std::ostream * os)707 static void Print(const char* str, ::std::ostream* os) {
708 if (str == NULL) {
709 *os << "NULL";
710 } else {
711 UniversalPrint(string(str), os);
712 }
713 }
714 };
715 template <>
716 class UniversalTersePrinter<char*> {
717 public:
Print(char * str,::std::ostream * os)718 static void Print(char* str, ::std::ostream* os) {
719 UniversalTersePrinter<const char*>::Print(str, os);
720 }
721 };
722
723 #if GTEST_HAS_STD_WSTRING
724 template <>
725 class UniversalTersePrinter<const wchar_t*> {
726 public:
Print(const wchar_t * str,::std::ostream * os)727 static void Print(const wchar_t* str, ::std::ostream* os) {
728 if (str == NULL) {
729 *os << "NULL";
730 } else {
731 UniversalPrint(::std::wstring(str), os);
732 }
733 }
734 };
735 #endif
736
737 template <>
738 class UniversalTersePrinter<wchar_t*> {
739 public:
Print(wchar_t * str,::std::ostream * os)740 static void Print(wchar_t* str, ::std::ostream* os) {
741 UniversalTersePrinter<const wchar_t*>::Print(str, os);
742 }
743 };
744
745 template <typename T>
UniversalTersePrint(const T & value,::std::ostream * os)746 void UniversalTersePrint(const T& value, ::std::ostream* os) {
747 UniversalTersePrinter<T>::Print(value, os);
748 }
749
750 // Prints a value using the type inferred by the compiler. The
751 // difference between this and UniversalTersePrint() is that for a
752 // (const) char pointer, this prints both the pointer and the
753 // NUL-terminated string.
754 template <typename T>
UniversalPrint(const T & value,::std::ostream * os)755 void UniversalPrint(const T& value, ::std::ostream* os) {
756 // A workarond for the bug in VC++ 7.1 that prevents us from instantiating
757 // UniversalPrinter with T directly.
758 typedef T T1;
759 UniversalPrinter<T1>::Print(value, os);
760 }
761
762 #if GTEST_HAS_TR1_TUPLE
763 typedef ::std::vector<string> Strings;
764
765 // This helper template allows PrintTo() for tuples and
766 // UniversalTersePrintTupleFieldsToStrings() to be defined by
767 // induction on the number of tuple fields. The idea is that
768 // TuplePrefixPrinter<N>::PrintPrefixTo(t, os) prints the first N
769 // fields in tuple t, and can be defined in terms of
770 // TuplePrefixPrinter<N - 1>.
771
772 // The inductive case.
773 template <size_t N>
774 struct TuplePrefixPrinter {
775 // Prints the first N fields of a tuple.
776 template <typename Tuple>
PrintPrefixToTuplePrefixPrinter777 static void PrintPrefixTo(const Tuple& t, ::std::ostream* os) {
778 TuplePrefixPrinter<N - 1>::PrintPrefixTo(t, os);
779 *os << ", ";
780 UniversalPrinter<typename ::std::tr1::tuple_element<N - 1, Tuple>::type>
781 ::Print(::std::tr1::get<N - 1>(t), os);
782 }
783
784 // Tersely prints the first N fields of a tuple to a string vector,
785 // one element for each field.
786 template <typename Tuple>
TersePrintPrefixToStringsTuplePrefixPrinter787 static void TersePrintPrefixToStrings(const Tuple& t, Strings* strings) {
788 TuplePrefixPrinter<N - 1>::TersePrintPrefixToStrings(t, strings);
789 ::std::stringstream ss;
790 UniversalTersePrint(::std::tr1::get<N - 1>(t), &ss);
791 strings->push_back(ss.str());
792 }
793 };
794
795 // Base cases.
796 template <>
797 struct TuplePrefixPrinter<0> {
798 template <typename Tuple>
799 static void PrintPrefixTo(const Tuple&, ::std::ostream*) {}
800
801 template <typename Tuple>
802 static void TersePrintPrefixToStrings(const Tuple&, Strings*) {}
803 };
804 // We have to specialize the entire TuplePrefixPrinter<> class
805 // template here, even though the definition of
806 // TersePrintPrefixToStrings() is the same as the generic version, as
807 // Embarcadero (formerly CodeGear, formerly Borland) C++ doesn't
808 // support specializing a method template of a class template.
809 template <>
810 struct TuplePrefixPrinter<1> {
811 template <typename Tuple>
812 static void PrintPrefixTo(const Tuple& t, ::std::ostream* os) {
813 UniversalPrinter<typename ::std::tr1::tuple_element<0, Tuple>::type>::
814 Print(::std::tr1::get<0>(t), os);
815 }
816
817 template <typename Tuple>
818 static void TersePrintPrefixToStrings(const Tuple& t, Strings* strings) {
819 ::std::stringstream ss;
820 UniversalTersePrint(::std::tr1::get<0>(t), &ss);
821 strings->push_back(ss.str());
822 }
823 };
824
825 // Helper function for printing a tuple. T must be instantiated with
826 // a tuple type.
827 template <typename T>
828 void PrintTupleTo(const T& t, ::std::ostream* os) {
829 *os << "(";
830 TuplePrefixPrinter< ::std::tr1::tuple_size<T>::value>::
831 PrintPrefixTo(t, os);
832 *os << ")";
833 }
834
835 // Prints the fields of a tuple tersely to a string vector, one
836 // element for each field. See the comment before
837 // UniversalTersePrint() for how we define "tersely".
838 template <typename Tuple>
839 Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
840 Strings result;
841 TuplePrefixPrinter< ::std::tr1::tuple_size<Tuple>::value>::
842 TersePrintPrefixToStrings(value, &result);
843 return result;
844 }
845 #endif // GTEST_HAS_TR1_TUPLE
846
847 } // namespace internal
848
849 template <typename T>
850 ::std::string PrintToString(const T& value) {
851 ::std::stringstream ss;
852 internal::UniversalTersePrinter<T>::Print(value, &ss);
853 return ss.str();
854 }
855
856 } // namespace testing
857
858 #endif // GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
859