1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Author: wan@google.com (Zhanyong Wan)
32 // Google Test - The Google C++ Testing Framework
34 // This file implements a universal value printer that can print a
35 // value of any type T:
37 // void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
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
45 // 1. foo::PrintTo(const T&, ostream*)
46 // 2. operator<<(ostream&, const T&) defined in either foo or the
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
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
58 // We also provide some convenient wrappers:
60 // // Prints a value to a string. For a (const or not) char
61 // // pointer, the NUL-terminated string (but not the pointer) is
63 // std::string ::testing::PrintToString(const T& value);
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
69 // void ::testing::internal::UniversalTersePrint(const T& value, ostream*);
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*);
76 // // Prints the fields of a tuple tersely to a string vector, one
77 // // element for each field. Tuple support must be enabled in
79 // std::vector<string> UniversalTersePrintTupleFieldsToStrings(
80 // const Tuple& value);
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
95 #ifndef GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
96 #define GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
98 #include <ostream> // NOLINT
103 #include "gtest/internal/gtest-port.h"
104 #include "gtest/internal/gtest-internal.h"
108 // Definitions in the 'internal' and 'internal2' name spaces are
109 // subject to change without notice. DO NOT USE THEM IN USER CODE!
110 namespace internal2
{
112 // Prints the given number of bytes in the given object to the given
114 GTEST_API_
void PrintBytesInObjectTo(const unsigned char* obj_bytes
,
118 // For selecting which printer to use when a given type has neither <<
121 kProtobuf
, // a protobuf type
122 kConvertibleToInteger
, // a type implicitly convertible to BiggestInt
123 // (e.g. a named or unnamed enum type)
124 kOtherType
// anything else
127 // TypeWithoutFormatter<T, kTypeKind>::PrintValue(value, os) is called
128 // by the universal printer to print a value of type T when neither
129 // operator<< nor PrintTo() is defined for T, where kTypeKind is the
130 // "kind" of T as defined by enum TypeKind.
131 template <typename T
, TypeKind kTypeKind
>
132 class TypeWithoutFormatter
{
134 // This default version is called when kTypeKind is kOtherType.
135 static void PrintValue(const T
& value
, ::std::ostream
* os
) {
136 PrintBytesInObjectTo(reinterpret_cast<const unsigned char*>(&value
),
141 // We print a protobuf using its ShortDebugString() when the string
142 // doesn't exceed this many characters; otherwise we print it using
143 // DebugString() for better readability.
144 const size_t kProtobufOneLinerMaxLength
= 50;
146 template <typename T
>
147 class TypeWithoutFormatter
<T
, kProtobuf
> {
149 static void PrintValue(const T
& value
, ::std::ostream
* os
) {
150 const ::testing::internal::string short_str
= value
.ShortDebugString();
151 const ::testing::internal::string pretty_str
=
152 short_str
.length() <= kProtobufOneLinerMaxLength
?
153 short_str
: ("\n" + value
.DebugString());
154 *os
<< ("<" + pretty_str
+ ">");
158 template <typename T
>
159 class TypeWithoutFormatter
<T
, kConvertibleToInteger
> {
161 // Since T has no << operator or PrintTo() but can be implicitly
162 // converted to BiggestInt, we print it as a BiggestInt.
164 // Most likely T is an enum type (either named or unnamed), in which
165 // case printing it as an integer is the desired behavior. In case
166 // T is not an enum, printing it as an integer is the best we can do
167 // given that it has no user-defined printer.
168 static void PrintValue(const T
& value
, ::std::ostream
* os
) {
169 const internal::BiggestInt kBigInt
= value
;
174 // Prints the given value to the given ostream. If the value is a
175 // protocol message, its debug string is printed; if it's an enum or
176 // of a type implicitly convertible to BiggestInt, it's printed as an
177 // integer; otherwise the bytes in the value are printed. This is
178 // what UniversalPrinter<T>::Print() does when it knows nothing about
179 // type T and T has neither << operator nor PrintTo().
181 // A user can override this behavior for a class type Foo by defining
182 // a << operator in the namespace where Foo is defined.
184 // We put this operator in namespace 'internal2' instead of 'internal'
185 // to simplify the implementation, as much code in 'internal' needs to
186 // use << in STL, which would conflict with our own << were it defined
189 // Note that this operator<< takes a generic std::basic_ostream<Char,
190 // CharTraits> type instead of the more restricted std::ostream. If
191 // we define it to take an std::ostream instead, we'll get an
192 // "ambiguous overloads" compiler error when trying to print a type
193 // Foo that supports streaming to std::basic_ostream<Char,
194 // CharTraits>, as the compiler cannot tell whether
195 // operator<<(std::ostream&, const T&) or
196 // operator<<(std::basic_stream<Char, CharTraits>, const Foo&) is more
198 template <typename Char
, typename CharTraits
, typename T
>
199 ::std::basic_ostream
<Char
, CharTraits
>& operator<<(
200 ::std::basic_ostream
<Char
, CharTraits
>& os
, const T
& x
) {
201 TypeWithoutFormatter
<T
,
202 (internal::IsAProtocolMessage
<T
>::value
? kProtobuf
:
203 internal::ImplicitlyConvertible
<const T
&, internal::BiggestInt
>::value
?
204 kConvertibleToInteger
: kOtherType
)>::PrintValue(x
, &os
);
208 } // namespace internal2
209 } // namespace testing
211 // This namespace MUST NOT BE NESTED IN ::testing, or the name look-up
212 // magic needed for implementing UniversalPrinter won't work.
213 namespace testing_internal
{
215 // Used to print a value that is not an STL-style container when the
216 // user doesn't define PrintTo() for it.
217 template <typename T
>
218 void DefaultPrintNonContainerTo(const T
& value
, ::std::ostream
* os
) {
219 // With the following statement, during unqualified name lookup,
220 // testing::internal2::operator<< appears as if it was declared in
221 // the nearest enclosing namespace that contains both
222 // ::testing_internal and ::testing::internal2, i.e. the global
223 // namespace. For more details, refer to the C++ Standard section
224 // 7.3.4-1 [namespace.udir]. This allows us to fall back onto
225 // testing::internal2::operator<< in case T doesn't come with a <<
228 // We cannot write 'using ::testing::internal2::operator<<;', which
229 // gcc 3.3 fails to compile due to a compiler bug.
230 using namespace ::testing::internal2
; // NOLINT
232 // Assuming T is defined in namespace foo, in the next statement,
233 // the compiler will consider all of:
235 // 1. foo::operator<< (thanks to Koenig look-up),
236 // 2. ::operator<< (as the current namespace is enclosed in ::),
237 // 3. testing::internal2::operator<< (thanks to the using statement above).
239 // The operator<< whose type matches T best will be picked.
241 // We deliberately allow #2 to be a candidate, as sometimes it's
242 // impossible to define #1 (e.g. when foo is ::std, defining
243 // anything in it is undefined behavior unless you are a compiler
248 } // namespace testing_internal
253 // UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
254 // value to the given ostream. The caller must ensure that
255 // 'ostream_ptr' is not NULL, or the behavior is undefined.
257 // We define UniversalPrinter as a class template (as opposed to a
258 // function template), as we need to partially specialize it for
259 // reference types, which cannot be done with function templates.
260 template <typename T
>
261 class UniversalPrinter
;
263 template <typename T
>
264 void UniversalPrint(const T
& value
, ::std::ostream
* os
);
266 // Used to print an STL-style container when the user doesn't define
267 // a PrintTo() for it.
268 template <typename C
>
269 void DefaultPrintTo(IsContainer
/* dummy */,
270 false_type
/* is not a pointer */,
271 const C
& container
, ::std::ostream
* os
) {
272 const size_t kMaxCount
= 32; // The maximum number of elements to print.
275 for (typename
C::const_iterator it
= container
.begin();
276 it
!= container
.end(); ++it
, ++count
) {
279 if (count
== kMaxCount
) { // Enough has been printed.
285 // We cannot call PrintTo(*it, os) here as PrintTo() doesn't
286 // handle *it being a native array.
287 internal::UniversalPrint(*it
, os
);
296 // Used to print a pointer that is neither a char pointer nor a member
297 // pointer, when the user doesn't define PrintTo() for it. (A member
298 // variable pointer or member function pointer doesn't really point to
299 // a location in the address space. Their representation is
300 // implementation-defined. Therefore they will be printed as raw
302 template <typename T
>
303 void DefaultPrintTo(IsNotContainer
/* dummy */,
304 true_type
/* is a pointer */,
305 T
* p
, ::std::ostream
* os
) {
309 // C++ doesn't allow casting from a function pointer to any object
312 // IsTrue() silences warnings: "Condition is always true",
313 // "unreachable code".
314 if (IsTrue(ImplicitlyConvertible
<T
*, const void*>::value
)) {
315 // T is not a function type. We just call << to print p,
316 // relying on ADL to pick up user-defined << for their pointer
320 // T is a function type, so '*os << p' doesn't do what we want
321 // (it just prints p as bool). We want to print p as a const
322 // void*. However, we cannot cast it to const void* directly,
323 // even using reinterpret_cast, as earlier versions of gcc
324 // (e.g. 3.4.5) cannot compile the cast when p is a function
325 // pointer. Casting to UInt64 first solves the problem.
326 *os
<< reinterpret_cast<const void*>(
327 reinterpret_cast<internal::UInt64
>(p
));
332 // Used to print a non-container, non-pointer value when the user
333 // doesn't define PrintTo() for it.
334 template <typename T
>
335 void DefaultPrintTo(IsNotContainer
/* dummy */,
336 false_type
/* is not a pointer */,
337 const T
& value
, ::std::ostream
* os
) {
338 ::testing_internal::DefaultPrintNonContainerTo(value
, os
);
341 // Prints the given value using the << operator if it has one;
342 // otherwise prints the bytes in it. This is what
343 // UniversalPrinter<T>::Print() does when PrintTo() is not specialized
344 // or overloaded for type T.
346 // A user can override this behavior for a class type Foo by defining
347 // an overload of PrintTo() in the namespace where Foo is defined. We
348 // give the user this option as sometimes defining a << operator for
349 // Foo is not desirable (e.g. the coding style may prevent doing it,
350 // or there is already a << operator but it doesn't do what the user
352 template <typename T
>
353 void PrintTo(const T
& value
, ::std::ostream
* os
) {
354 // DefaultPrintTo() is overloaded. The type of its first two
355 // arguments determine which version will be picked. If T is an
356 // STL-style container, the version for container will be called; if
357 // T is a pointer, the pointer version will be called; otherwise the
358 // generic version will be called.
360 // Note that we check for container types here, prior to we check
361 // for protocol message types in our operator<<. The rationale is:
363 // For protocol messages, we want to give people a chance to
364 // override Google Mock's format by defining a PrintTo() or
365 // operator<<. For STL containers, other formats can be
366 // incompatible with Google Mock's format for the container
367 // elements; therefore we check for container types here to ensure
368 // that our format is used.
370 // The second argument of DefaultPrintTo() is needed to bypass a bug
371 // in Symbian's C++ compiler that prevents it from picking the right
374 // PrintTo(const T& x, ...);
375 // PrintTo(T* x, ...);
376 DefaultPrintTo(IsContainerTest
<T
>(0), is_pointer
<T
>(), value
, os
);
379 // The following list of PrintTo() overloads tells
380 // UniversalPrinter<T>::Print() how to print standard types (built-in
381 // types, strings, plain arrays, and pointers).
383 // Overloads for various char types.
384 GTEST_API_
void PrintTo(unsigned char c
, ::std::ostream
* os
);
385 GTEST_API_
void PrintTo(signed char c
, ::std::ostream
* os
);
386 inline void PrintTo(char c
, ::std::ostream
* os
) {
387 // When printing a plain char, we always treat it as unsigned. This
388 // way, the output won't be affected by whether the compiler thinks
389 // char is signed or not.
390 PrintTo(static_cast<unsigned char>(c
), os
);
393 // Overloads for other simple built-in types.
394 inline void PrintTo(bool x
, ::std::ostream
* os
) {
395 *os
<< (x
? "true" : "false");
398 // Overload for wchar_t type.
399 // Prints a wchar_t as a symbol if it is printable or as its internal
400 // code otherwise and also as its decimal code (except for L'\0').
401 // The L'\0' char is printed as "L'\\0'". The decimal code is printed
402 // as signed integer when wchar_t is implemented by the compiler
403 // as a signed type and is printed as an unsigned integer when wchar_t
404 // is implemented as an unsigned type.
405 GTEST_API_
void PrintTo(wchar_t wc
, ::std::ostream
* os
);
407 // Overloads for C strings.
408 GTEST_API_
void PrintTo(const char* s
, ::std::ostream
* os
);
409 inline void PrintTo(char* s
, ::std::ostream
* os
) {
410 PrintTo(ImplicitCast_
<const char*>(s
), os
);
413 // signed/unsigned char is often used for representing binary data, so
414 // we print pointers to it as void* to be safe.
415 inline void PrintTo(const signed char* s
, ::std::ostream
* os
) {
416 PrintTo(ImplicitCast_
<const void*>(s
), os
);
418 inline void PrintTo(signed char* s
, ::std::ostream
* os
) {
419 PrintTo(ImplicitCast_
<const void*>(s
), os
);
421 inline void PrintTo(const unsigned char* s
, ::std::ostream
* os
) {
422 PrintTo(ImplicitCast_
<const void*>(s
), os
);
424 inline void PrintTo(unsigned char* s
, ::std::ostream
* os
) {
425 PrintTo(ImplicitCast_
<const void*>(s
), os
);
428 // MSVC can be configured to define wchar_t as a typedef of unsigned
429 // short. It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native
430 // type. When wchar_t is a typedef, defining an overload for const
431 // wchar_t* would cause unsigned short* be printed as a wide string,
432 // possibly causing invalid memory accesses.
433 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
434 // Overloads for wide C strings
435 GTEST_API_
void PrintTo(const wchar_t* s
, ::std::ostream
* os
);
436 inline void PrintTo(wchar_t* s
, ::std::ostream
* os
) {
437 PrintTo(ImplicitCast_
<const wchar_t*>(s
), os
);
441 // Overload for C arrays. Multi-dimensional arrays are printed
444 // Prints the given number of elements in an array, without printing
446 template <typename T
>
447 void PrintRawArrayTo(const T a
[], size_t count
, ::std::ostream
* os
) {
448 UniversalPrint(a
[0], os
);
449 for (size_t i
= 1; i
!= count
; i
++) {
451 UniversalPrint(a
[i
], os
);
455 // Overloads for ::string and ::std::string.
456 #if GTEST_HAS_GLOBAL_STRING
457 GTEST_API_
void PrintStringTo(const ::string
&s
, ::std::ostream
* os
);
458 inline void PrintTo(const ::string
& s
, ::std::ostream
* os
) {
459 PrintStringTo(s
, os
);
461 #endif // GTEST_HAS_GLOBAL_STRING
463 GTEST_API_
void PrintStringTo(const ::std::string
&s
, ::std::ostream
* os
);
464 inline void PrintTo(const ::std::string
& s
, ::std::ostream
* os
) {
465 PrintStringTo(s
, os
);
468 // Overloads for ::wstring and ::std::wstring.
469 #if GTEST_HAS_GLOBAL_WSTRING
470 GTEST_API_
void PrintWideStringTo(const ::wstring
&s
, ::std::ostream
* os
);
471 inline void PrintTo(const ::wstring
& s
, ::std::ostream
* os
) {
472 PrintWideStringTo(s
, os
);
474 #endif // GTEST_HAS_GLOBAL_WSTRING
476 #if GTEST_HAS_STD_WSTRING
477 GTEST_API_
void PrintWideStringTo(const ::std::wstring
&s
, ::std::ostream
* os
);
478 inline void PrintTo(const ::std::wstring
& s
, ::std::ostream
* os
) {
479 PrintWideStringTo(s
, os
);
481 #endif // GTEST_HAS_STD_WSTRING
483 #if GTEST_HAS_TR1_TUPLE
484 // Overload for ::std::tr1::tuple. Needed for printing function arguments,
485 // which are packed as tuples.
487 // Helper function for printing a tuple. T must be instantiated with
489 template <typename T
>
490 void PrintTupleTo(const T
& t
, ::std::ostream
* os
);
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.
497 inline void PrintTo(const ::std::tr1::tuple
<>& t
, ::std::ostream
* os
) {
501 template <typename T1
>
502 void PrintTo(const ::std::tr1::tuple
<T1
>& t
, ::std::ostream
* os
) {
506 template <typename T1
, typename T2
>
507 void PrintTo(const ::std::tr1::tuple
<T1
, T2
>& t
, ::std::ostream
* os
) {
511 template <typename T1
, typename T2
, typename T3
>
512 void PrintTo(const ::std::tr1::tuple
<T1
, T2
, T3
>& t
, ::std::ostream
* os
) {
516 template <typename T1
, typename T2
, typename T3
, typename T4
>
517 void PrintTo(const ::std::tr1::tuple
<T1
, T2
, T3
, T4
>& t
, ::std::ostream
* os
) {
521 template <typename T1
, typename T2
, typename T3
, typename T4
, typename T5
>
522 void PrintTo(const ::std::tr1::tuple
<T1
, T2
, T3
, T4
, T5
>& t
,
523 ::std::ostream
* os
) {
527 template <typename T1
, typename T2
, typename T3
, typename T4
, typename T5
,
529 void PrintTo(const ::std::tr1::tuple
<T1
, T2
, T3
, T4
, T5
, T6
>& t
,
530 ::std::ostream
* os
) {
534 template <typename T1
, typename T2
, typename T3
, typename T4
, typename T5
,
535 typename T6
, typename T7
>
536 void PrintTo(const ::std::tr1::tuple
<T1
, T2
, T3
, T4
, T5
, T6
, T7
>& t
,
537 ::std::ostream
* os
) {
541 template <typename T1
, typename T2
, typename T3
, typename T4
, typename T5
,
542 typename T6
, typename T7
, typename T8
>
543 void PrintTo(const ::std::tr1::tuple
<T1
, T2
, T3
, T4
, T5
, T6
, T7
, T8
>& t
,
544 ::std::ostream
* os
) {
548 template <typename T1
, typename T2
, typename T3
, typename T4
, typename T5
,
549 typename T6
, typename T7
, typename T8
, typename T9
>
550 void PrintTo(const ::std::tr1::tuple
<T1
, T2
, T3
, T4
, T5
, T6
, T7
, T8
, T9
>& t
,
551 ::std::ostream
* os
) {
555 template <typename T1
, typename T2
, typename T3
, typename T4
, typename T5
,
556 typename T6
, typename T7
, typename T8
, typename T9
, typename T10
>
558 const ::std::tr1::tuple
<T1
, T2
, T3
, T4
, T5
, T6
, T7
, T8
, T9
, T10
>& t
,
559 ::std::ostream
* os
) {
562 #endif // GTEST_HAS_TR1_TUPLE
564 // Overload for std::pair.
565 template <typename T1
, typename T2
>
566 void PrintTo(const ::std::pair
<T1
, T2
>& value
, ::std::ostream
* os
) {
568 // We cannot use UniversalPrint(value.first, os) here, as T1 may be
569 // a reference type. The same for printing value.second.
570 UniversalPrinter
<T1
>::Print(value
.first
, os
);
572 UniversalPrinter
<T2
>::Print(value
.second
, os
);
576 // Implements printing a non-reference type T by letting the compiler
577 // pick the right overload of PrintTo() for T.
578 template <typename T
>
579 class UniversalPrinter
{
581 // MSVC warns about adding const to a function type, so we want to
582 // disable the warning.
584 # pragma warning(push) // Saves the current warning state.
585 # pragma warning(disable:4180) // Temporarily disables warning 4180.
588 // Note: we deliberately don't call this PrintTo(), as that name
589 // conflicts with ::testing::internal::PrintTo in the body of the
591 static void Print(const T
& value
, ::std::ostream
* os
) {
592 // By default, ::testing::internal::PrintTo() is used for printing
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.
604 # pragma warning(pop) // Restores the warning state.
608 // UniversalPrintArray(begin, len, os) prints an array of 'len'
609 // elements, starting at address 'begin'.
610 template <typename T
>
611 void UniversalPrintArray(const T
* begin
, size_t len
, ::std::ostream
* os
) {
616 const size_t kThreshold
= 18;
617 const size_t kChunkSize
= 8;
618 // If the array has more than kThreshold elements, we'll have to
619 // omit some details by printing only the first and the last
620 // kChunkSize elements.
621 // TODO(wan@google.com): let the user control the threshold using a flag.
622 if (len
<= kThreshold
) {
623 PrintRawArrayTo(begin
, len
, os
);
625 PrintRawArrayTo(begin
, kChunkSize
, os
);
627 PrintRawArrayTo(begin
+ len
- kChunkSize
, kChunkSize
, os
);
632 // This overload prints a (const) char array compactly.
633 GTEST_API_
void UniversalPrintArray(const char* begin
,
637 // Implements printing an array type T[N].
638 template <typename T
, size_t N
>
639 class UniversalPrinter
<T
[N
]> {
641 // Prints the given array, omitting some elements when there are too
643 static void Print(const T (&a
)[N
], ::std::ostream
* os
) {
644 UniversalPrintArray(a
, N
, os
);
648 // Implements printing a reference type T&.
649 template <typename T
>
650 class UniversalPrinter
<T
&> {
652 // MSVC warns about adding const to a function type, so we want to
653 // disable the warning.
655 # pragma warning(push) // Saves the current warning state.
656 # pragma warning(disable:4180) // Temporarily disables warning 4180.
659 static void Print(const T
& value
, ::std::ostream
* os
) {
660 // Prints the address of the value. We use reinterpret_cast here
661 // as static_cast doesn't compile when T is a function type.
662 *os
<< "@" << reinterpret_cast<const void*>(&value
) << " ";
664 // Then prints the value itself.
665 UniversalPrint(value
, os
);
669 # pragma warning(pop) // Restores the warning state.
673 // Prints a value tersely: for a reference type, the referenced value
674 // (but not the address) is printed; for a (const) char pointer, the
675 // NUL-terminated string (but not the pointer) is printed.
676 template <typename T
>
677 void UniversalTersePrint(const T
& value
, ::std::ostream
* os
) {
678 UniversalPrint(value
, os
);
680 inline void UniversalTersePrint(const char* str
, ::std::ostream
* os
) {
684 UniversalPrint(string(str
), os
);
687 inline void UniversalTersePrint(char* str
, ::std::ostream
* os
) {
688 UniversalTersePrint(static_cast<const char*>(str
), os
);
691 // Prints a value using the type inferred by the compiler. The
692 // difference between this and UniversalTersePrint() is that for a
693 // (const) char pointer, this prints both the pointer and the
694 // NUL-terminated string.
695 template <typename T
>
696 void UniversalPrint(const T
& value
, ::std::ostream
* os
) {
697 UniversalPrinter
<T
>::Print(value
, os
);
700 #if GTEST_HAS_TR1_TUPLE
701 typedef ::std::vector
<string
> Strings
;
703 // This helper template allows PrintTo() for tuples and
704 // UniversalTersePrintTupleFieldsToStrings() to be defined by
705 // induction on the number of tuple fields. The idea is that
706 // TuplePrefixPrinter<N>::PrintPrefixTo(t, os) prints the first N
707 // fields in tuple t, and can be defined in terms of
708 // TuplePrefixPrinter<N - 1>.
710 // The inductive case.
712 struct TuplePrefixPrinter
{
713 // Prints the first N fields of a tuple.
714 template <typename Tuple
>
715 static void PrintPrefixTo(const Tuple
& t
, ::std::ostream
* os
) {
716 TuplePrefixPrinter
<N
- 1>::PrintPrefixTo(t
, os
);
718 UniversalPrinter
<typename ::std::tr1::tuple_element
<N
- 1, Tuple
>::type
>
719 ::Print(::std::tr1::get
<N
- 1>(t
), os
);
722 // Tersely prints the first N fields of a tuple to a string vector,
723 // one element for each field.
724 template <typename Tuple
>
725 static void TersePrintPrefixToStrings(const Tuple
& t
, Strings
* strings
) {
726 TuplePrefixPrinter
<N
- 1>::TersePrintPrefixToStrings(t
, strings
);
727 ::std::stringstream ss
;
728 UniversalTersePrint(::std::tr1::get
<N
- 1>(t
), &ss
);
729 strings
->push_back(ss
.str());
735 struct TuplePrefixPrinter
<0> {
736 template <typename Tuple
>
737 static void PrintPrefixTo(const Tuple
&, ::std::ostream
*) {}
739 template <typename Tuple
>
740 static void TersePrintPrefixToStrings(const Tuple
&, Strings
*) {}
742 // We have to specialize the entire TuplePrefixPrinter<> class
743 // template here, even though the definition of
744 // TersePrintPrefixToStrings() is the same as the generic version, as
745 // Embarcadero (formerly CodeGear, formerly Borland) C++ doesn't
746 // support specializing a method template of a class template.
748 struct TuplePrefixPrinter
<1> {
749 template <typename Tuple
>
750 static void PrintPrefixTo(const Tuple
& t
, ::std::ostream
* os
) {
751 UniversalPrinter
<typename ::std::tr1::tuple_element
<0, Tuple
>::type
>::
752 Print(::std::tr1::get
<0>(t
), os
);
755 template <typename Tuple
>
756 static void TersePrintPrefixToStrings(const Tuple
& t
, Strings
* strings
) {
757 ::std::stringstream ss
;
758 UniversalTersePrint(::std::tr1::get
<0>(t
), &ss
);
759 strings
->push_back(ss
.str());
763 // Helper function for printing a tuple. T must be instantiated with
765 template <typename T
>
766 void PrintTupleTo(const T
& t
, ::std::ostream
* os
) {
768 TuplePrefixPrinter
< ::std::tr1::tuple_size
<T
>::value
>::
769 PrintPrefixTo(t
, os
);
773 // Prints the fields of a tuple tersely to a string vector, one
774 // element for each field. See the comment before
775 // UniversalTersePrint() for how we define "tersely".
776 template <typename Tuple
>
777 Strings
UniversalTersePrintTupleFieldsToStrings(const Tuple
& value
) {
779 TuplePrefixPrinter
< ::std::tr1::tuple_size
<Tuple
>::value
>::
780 TersePrintPrefixToStrings(value
, &result
);
783 #endif // GTEST_HAS_TR1_TUPLE
785 } // namespace internal
787 template <typename T
>
788 ::std::string
PrintToString(const T
& value
) {
789 ::std::stringstream ss
;
790 internal::UniversalTersePrint(value
, &ss
);
794 } // namespace testing
796 #endif // GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_