gtest-printers.h
Go to the documentation of this file.
00001 // Copyright 2007, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 
00032 // Google Test - The Google C++ Testing Framework
00033 //
00034 // This file implements a universal value printer that can print a
00035 // value of any type T:
00036 //
00037 //   void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
00038 //
00039 // A user can teach this function how to print a class type T by
00040 // defining either operator<<() or PrintTo() in the namespace that
00041 // defines T.  More specifically, the FIRST defined function in the
00042 // following list will be used (assuming T is defined in namespace
00043 // foo):
00044 //
00045 //   1. foo::PrintTo(const T&, ostream*)
00046 //   2. operator<<(ostream&, const T&) defined in either foo or the
00047 //      global namespace.
00048 //
00049 // If none of the above is defined, it will print the debug string of
00050 // the value if it is a protocol buffer, or print the raw bytes in the
00051 // value otherwise.
00052 //
00053 // To aid debugging: when T is a reference type, the address of the
00054 // value is also printed; when T is a (const) char pointer, both the
00055 // pointer value and the NUL-terminated string it points to are
00056 // printed.
00057 //
00058 // We also provide some convenient wrappers:
00059 //
00060 //   // Prints a value to a string.  For a (const or not) char
00061 //   // pointer, the NUL-terminated string (but not the pointer) is
00062 //   // printed.
00063 //   std::string ::testing::PrintToString(const T& value);
00064 //
00065 //   // Prints a value tersely: for a reference type, the referenced
00066 //   // value (but not the address) is printed; for a (const or not) char
00067 //   // pointer, the NUL-terminated string (but not the pointer) is
00068 //   // printed.
00069 //   void ::testing::internal::UniversalTersePrint(const T& value, ostream*);
00070 //
00071 //   // Prints value using the type inferred by the compiler.  The difference
00072 //   // from UniversalTersePrint() is that this function prints both the
00073 //   // pointer and the NUL-terminated string for a (const or not) char pointer.
00074 //   void ::testing::internal::UniversalPrint(const T& value, ostream*);
00075 //
00076 //   // Prints the fields of a tuple tersely to a string vector, one
00077 //   // element for each field. Tuple support must be enabled in
00078 //   // gtest-port.h.
00079 //   std::vector<string> UniversalTersePrintTupleFieldsToStrings(
00080 //       const Tuple& value);
00081 //
00082 // Known limitation:
00083 //
00084 // The print primitives print the elements of an STL-style container
00085 // using the compiler-inferred type of *iter where iter is a
00086 // const_iterator of the container.  When const_iterator is an input
00087 // iterator but not a forward iterator, this inferred type may not
00088 // match value_type, and the print output may be incorrect.  In
00089 // practice, this is rarely a problem as for most containers
00090 // const_iterator is a forward iterator.  We'll fix this if there's an
00091 // actual need for it.  Note that this fix cannot rely on value_type
00092 // being defined as many user-defined container types don't have
00093 // value_type.
00094 
00095 #ifndef GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
00096 #define GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
00097 
00098 #include <ostream>  // NOLINT
00099 #include <sstream>
00100 #include <string>
00101 #include <utility>
00102 #include <vector>
00103 #include "gtest/internal/gtest-port.h"
00104 #include "gtest/internal/gtest-internal.h"
00105 
00106 #if GTEST_HAS_STD_TUPLE_
00107 # include <tuple>
00108 #endif
00109 
00110 namespace testing {
00111 
00112 // Definitions in the 'internal' and 'internal2' name spaces are
00113 // subject to change without notice.  DO NOT USE THEM IN USER CODE!
00114 namespace internal2 {
00115 
00116 // Prints the given number of bytes in the given object to the given
00117 // ostream.
00118 GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
00119                                      size_t count,
00120                                      ::std::ostream* os);
00121 
00122 // For selecting which printer to use when a given type has neither <<
00123 // nor PrintTo().
00124 enum TypeKind {
00125   kProtobuf,              // a protobuf type
00126   kConvertibleToInteger,  // a type implicitly convertible to BiggestInt
00127                           // (e.g. a named or unnamed enum type)
00128   kOtherType              // anything else
00129 };
00130 
00131 // TypeWithoutFormatter<T, kTypeKind>::PrintValue(value, os) is called
00132 // by the universal printer to print a value of type T when neither
00133 // operator<< nor PrintTo() is defined for T, where kTypeKind is the
00134 // "kind" of T as defined by enum TypeKind.
00135 template <typename T, TypeKind kTypeKind>
00136 class TypeWithoutFormatter {
00137  public:
00138   // This default version is called when kTypeKind is kOtherType.
00139   static void PrintValue(const T& value, ::std::ostream* os) {
00140     PrintBytesInObjectTo(reinterpret_cast<const unsigned char*>(&value),
00141                          sizeof(value), os);
00142   }
00143 };
00144 
00145 // We print a protobuf using its ShortDebugString() when the string
00146 // doesn't exceed this many characters; otherwise we print it using
00147 // DebugString() for better readability.
00148 const size_t kProtobufOneLinerMaxLength = 50;
00149 
00150 template <typename T>
00151 class TypeWithoutFormatter<T, kProtobuf> {
00152  public:
00153   static void PrintValue(const T& value, ::std::ostream* os) {
00154     const ::testing::internal::string short_str = value.ShortDebugString();
00155     const ::testing::internal::string pretty_str =
00156         short_str.length() <= kProtobufOneLinerMaxLength ?
00157         short_str : ("\n" + value.DebugString());
00158     *os << ("<" + pretty_str + ">");
00159   }
00160 };
00161 
00162 template <typename T>
00163 class TypeWithoutFormatter<T, kConvertibleToInteger> {
00164  public:
00165   // Since T has no << operator or PrintTo() but can be implicitly
00166   // converted to BiggestInt, we print it as a BiggestInt.
00167   //
00168   // Most likely T is an enum type (either named or unnamed), in which
00169   // case printing it as an integer is the desired behavior.  In case
00170   // T is not an enum, printing it as an integer is the best we can do
00171   // given that it has no user-defined printer.
00172   static void PrintValue(const T& value, ::std::ostream* os) {
00173     const internal::BiggestInt kBigInt = value;
00174     *os << kBigInt;
00175   }
00176 };
00177 
00178 // Prints the given value to the given ostream.  If the value is a
00179 // protocol message, its debug string is printed; if it's an enum or
00180 // of a type implicitly convertible to BiggestInt, it's printed as an
00181 // integer; otherwise the bytes in the value are printed.  This is
00182 // what UniversalPrinter<T>::Print() does when it knows nothing about
00183 // type T and T has neither << operator nor PrintTo().
00184 //
00185 // A user can override this behavior for a class type Foo by defining
00186 // a << operator in the namespace where Foo is defined.
00187 //
00188 // We put this operator in namespace 'internal2' instead of 'internal'
00189 // to simplify the implementation, as much code in 'internal' needs to
00190 // use << in STL, which would conflict with our own << were it defined
00191 // in 'internal'.
00192 //
00193 // Note that this operator<< takes a generic std::basic_ostream<Char,
00194 // CharTraits> type instead of the more restricted std::ostream.  If
00195 // we define it to take an std::ostream instead, we'll get an
00196 // "ambiguous overloads" compiler error when trying to print a type
00197 // Foo that supports streaming to std::basic_ostream<Char,
00198 // CharTraits>, as the compiler cannot tell whether
00199 // operator<<(std::ostream&, const T&) or
00200 // operator<<(std::basic_stream<Char, CharTraits>, const Foo&) is more
00201 // specific.
00202 template <typename Char, typename CharTraits, typename T>
00203 ::std::basic_ostream<Char, CharTraits>& operator<<(
00204     ::std::basic_ostream<Char, CharTraits>& os, const T& x) {
00205   TypeWithoutFormatter<T,
00206       (internal::IsAProtocolMessage<T>::value ? kProtobuf :
00207        internal::ImplicitlyConvertible<const T&, internal::BiggestInt>::value ?
00208        kConvertibleToInteger : kOtherType)>::PrintValue(x, &os);
00209   return os;
00210 }
00211 
00212 }  // namespace internal2
00213 }  // namespace testing
00214 
00215 // This namespace MUST NOT BE NESTED IN ::testing, or the name look-up
00216 // magic needed for implementing UniversalPrinter won't work.
00217 namespace testing_internal {
00218 
00219 // Used to print a value that is not an STL-style container when the
00220 // user doesn't define PrintTo() for it.
00221 template <typename T>
00222 void DefaultPrintNonContainerTo(const T& value, ::std::ostream* os) {
00223   // With the following statement, during unqualified name lookup,
00224   // testing::internal2::operator<< appears as if it was declared in
00225   // the nearest enclosing namespace that contains both
00226   // ::testing_internal and ::testing::internal2, i.e. the global
00227   // namespace.  For more details, refer to the C++ Standard section
00228   // 7.3.4-1 [namespace.udir].  This allows us to fall back onto
00229   // testing::internal2::operator<< in case T doesn't come with a <<
00230   // operator.
00231   //
00232   // We cannot write 'using ::testing::internal2::operator<<;', which
00233   // gcc 3.3 fails to compile due to a compiler bug.
00234   using namespace ::testing::internal2;  // NOLINT
00235 
00236   // Assuming T is defined in namespace foo, in the next statement,
00237   // the compiler will consider all of:
00238   //
00239   //   1. foo::operator<< (thanks to Koenig look-up),
00240   //   2. ::operator<< (as the current namespace is enclosed in ::),
00241   //   3. testing::internal2::operator<< (thanks to the using statement above).
00242   //
00243   // The operator<< whose type matches T best will be picked.
00244   //
00245   // We deliberately allow #2 to be a candidate, as sometimes it's
00246   // impossible to define #1 (e.g. when foo is ::std, defining
00247   // anything in it is undefined behavior unless you are a compiler
00248   // vendor.).
00249   *os << value;
00250 }
00251 
00252 }  // namespace testing_internal
00253 
00254 namespace testing {
00255 namespace internal {
00256 
00257 // UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
00258 // value to the given ostream.  The caller must ensure that
00259 // 'ostream_ptr' is not NULL, or the behavior is undefined.
00260 //
00261 // We define UniversalPrinter as a class template (as opposed to a
00262 // function template), as we need to partially specialize it for
00263 // reference types, which cannot be done with function templates.
00264 template <typename T>
00265 class UniversalPrinter;
00266 
00267 template <typename T>
00268 void UniversalPrint(const T& value, ::std::ostream* os);
00269 
00270 // Used to print an STL-style container when the user doesn't define
00271 // a PrintTo() for it.
00272 template <typename C>
00273 void DefaultPrintTo(IsContainer /* dummy */,
00274                     false_type /* is not a pointer */,
00275                     const C& container, ::std::ostream* os) {
00276   const size_t kMaxCount = 32;  // The maximum number of elements to print.
00277   *os << '{';
00278   size_t count = 0;
00279   for (typename C::const_iterator it = container.begin();
00280        it != container.end(); ++it, ++count) {
00281     if (count > 0) {
00282       *os << ',';
00283       if (count == kMaxCount) {  // Enough has been printed.
00284         *os << " ...";
00285         break;
00286       }
00287     }
00288     *os << ' ';
00289     // We cannot call PrintTo(*it, os) here as PrintTo() doesn't
00290     // handle *it being a native array.
00291     internal::UniversalPrint(*it, os);
00292   }
00293 
00294   if (count > 0) {
00295     *os << ' ';
00296   }
00297   *os << '}';
00298 }
00299 
00300 // Used to print a pointer that is neither a char pointer nor a member
00301 // pointer, when the user doesn't define PrintTo() for it.  (A member
00302 // variable pointer or member function pointer doesn't really point to
00303 // a location in the address space.  Their representation is
00304 // implementation-defined.  Therefore they will be printed as raw
00305 // bytes.)
00306 template <typename T>
00307 void DefaultPrintTo(IsNotContainer /* dummy */,
00308                     true_type /* is a pointer */,
00309                     T* p, ::std::ostream* os) {
00310   if (p == NULL) {
00311     *os << "NULL";
00312   } else {
00313     // C++ doesn't allow casting from a function pointer to any object
00314     // pointer.
00315     //
00316     // IsTrue() silences warnings: "Condition is always true",
00317     // "unreachable code".
00318     if (IsTrue(ImplicitlyConvertible<T*, const void*>::value)) {
00319       // T is not a function type.  We just call << to print p,
00320       // relying on ADL to pick up user-defined << for their pointer
00321       // types, if any.
00322       *os << p;
00323     } else {
00324       // T is a function type, so '*os << p' doesn't do what we want
00325       // (it just prints p as bool).  We want to print p as a const
00326       // void*.  However, we cannot cast it to const void* directly,
00327       // even using reinterpret_cast, as earlier versions of gcc
00328       // (e.g. 3.4.5) cannot compile the cast when p is a function
00329       // pointer.  Casting to UInt64 first solves the problem.
00330       *os << reinterpret_cast<const void*>(
00331           reinterpret_cast<internal::UInt64>(p));
00332     }
00333   }
00334 }
00335 
00336 // Used to print a non-container, non-pointer value when the user
00337 // doesn't define PrintTo() for it.
00338 template <typename T>
00339 void DefaultPrintTo(IsNotContainer /* dummy */,
00340                     false_type /* is not a pointer */,
00341                     const T& value, ::std::ostream* os) {
00342   ::testing_internal::DefaultPrintNonContainerTo(value, os);
00343 }
00344 
00345 // Prints the given value using the << operator if it has one;
00346 // otherwise prints the bytes in it.  This is what
00347 // UniversalPrinter<T>::Print() does when PrintTo() is not specialized
00348 // or overloaded for type T.
00349 //
00350 // A user can override this behavior for a class type Foo by defining
00351 // an overload of PrintTo() in the namespace where Foo is defined.  We
00352 // give the user this option as sometimes defining a << operator for
00353 // Foo is not desirable (e.g. the coding style may prevent doing it,
00354 // or there is already a << operator but it doesn't do what the user
00355 // wants).
00356 template <typename T>
00357 void PrintTo(const T& value, ::std::ostream* os) {
00358   // DefaultPrintTo() is overloaded.  The type of its first two
00359   // arguments determine which version will be picked.  If T is an
00360   // STL-style container, the version for container will be called; if
00361   // T is a pointer, the pointer version will be called; otherwise the
00362   // generic version will be called.
00363   //
00364   // Note that we check for container types here, prior to we check
00365   // for protocol message types in our operator<<.  The rationale is:
00366   //
00367   // For protocol messages, we want to give people a chance to
00368   // override Google Mock's format by defining a PrintTo() or
00369   // operator<<.  For STL containers, other formats can be
00370   // incompatible with Google Mock's format for the container
00371   // elements; therefore we check for container types here to ensure
00372   // that our format is used.
00373   //
00374   // The second argument of DefaultPrintTo() is needed to bypass a bug
00375   // in Symbian's C++ compiler that prevents it from picking the right
00376   // overload between:
00377   //
00378   //   PrintTo(const T& x, ...);
00379   //   PrintTo(T* x, ...);
00380   DefaultPrintTo(IsContainerTest<T>(0), is_pointer<T>(), value, os);
00381 }
00382 
00383 // The following list of PrintTo() overloads tells
00384 // UniversalPrinter<T>::Print() how to print standard types (built-in
00385 // types, strings, plain arrays, and pointers).
00386 
00387 // Overloads for various char types.
00388 GTEST_API_ void PrintTo(unsigned char c, ::std::ostream* os);
00389 GTEST_API_ void PrintTo(signed char c, ::std::ostream* os);
00390 inline void PrintTo(char c, ::std::ostream* os) {
00391   // When printing a plain char, we always treat it as unsigned.  This
00392   // way, the output won't be affected by whether the compiler thinks
00393   // char is signed or not.
00394   PrintTo(static_cast<unsigned char>(c), os);
00395 }
00396 
00397 // Overloads for other simple built-in types.
00398 inline void PrintTo(bool x, ::std::ostream* os) {
00399   *os << (x ? "true" : "false");
00400 }
00401 
00402 // Overload for wchar_t type.
00403 // Prints a wchar_t as a symbol if it is printable or as its internal
00404 // code otherwise and also as its decimal code (except for L'\0').
00405 // The L'\0' char is printed as "L'\\0'". The decimal code is printed
00406 // as signed integer when wchar_t is implemented by the compiler
00407 // as a signed type and is printed as an unsigned integer when wchar_t
00408 // is implemented as an unsigned type.
00409 GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os);
00410 
00411 // Overloads for C strings.
00412 GTEST_API_ void PrintTo(const char* s, ::std::ostream* os);
00413 inline void PrintTo(char* s, ::std::ostream* os) {
00414   PrintTo(ImplicitCast_<const char*>(s), os);
00415 }
00416 
00417 // signed/unsigned char is often used for representing binary data, so
00418 // we print pointers to it as void* to be safe.
00419 inline void PrintTo(const signed char* s, ::std::ostream* os) {
00420   PrintTo(ImplicitCast_<const void*>(s), os);
00421 }
00422 inline void PrintTo(signed char* s, ::std::ostream* os) {
00423   PrintTo(ImplicitCast_<const void*>(s), os);
00424 }
00425 inline void PrintTo(const unsigned char* s, ::std::ostream* os) {
00426   PrintTo(ImplicitCast_<const void*>(s), os);
00427 }
00428 inline void PrintTo(unsigned char* s, ::std::ostream* os) {
00429   PrintTo(ImplicitCast_<const void*>(s), os);
00430 }
00431 
00432 // MSVC can be configured to define wchar_t as a typedef of unsigned
00433 // short.  It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native
00434 // type.  When wchar_t is a typedef, defining an overload for const
00435 // wchar_t* would cause unsigned short* be printed as a wide string,
00436 // possibly causing invalid memory accesses.
00437 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
00438 // Overloads for wide C strings
00439 GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os);
00440 inline void PrintTo(wchar_t* s, ::std::ostream* os) {
00441   PrintTo(ImplicitCast_<const wchar_t*>(s), os);
00442 }
00443 #endif
00444 
00445 // Overload for C arrays.  Multi-dimensional arrays are printed
00446 // properly.
00447 
00448 // Prints the given number of elements in an array, without printing
00449 // the curly braces.
00450 template <typename T>
00451 void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
00452   UniversalPrint(a[0], os);
00453   for (size_t i = 1; i != count; i++) {
00454     *os << ", ";
00455     UniversalPrint(a[i], os);
00456   }
00457 }
00458 
00459 // Overloads for ::string and ::std::string.
00460 #if GTEST_HAS_GLOBAL_STRING
00461 GTEST_API_ void PrintStringTo(const ::string&s, ::std::ostream* os);
00462 inline void PrintTo(const ::string& s, ::std::ostream* os) {
00463   PrintStringTo(s, os);
00464 }
00465 #endif  // GTEST_HAS_GLOBAL_STRING
00466 
00467 GTEST_API_ void PrintStringTo(const ::std::string&s, ::std::ostream* os);
00468 inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
00469   PrintStringTo(s, os);
00470 }
00471 
00472 // Overloads for ::wstring and ::std::wstring.
00473 #if GTEST_HAS_GLOBAL_WSTRING
00474 GTEST_API_ void PrintWideStringTo(const ::wstring&s, ::std::ostream* os);
00475 inline void PrintTo(const ::wstring& s, ::std::ostream* os) {
00476   PrintWideStringTo(s, os);
00477 }
00478 #endif  // GTEST_HAS_GLOBAL_WSTRING
00479 
00480 #if GTEST_HAS_STD_WSTRING
00481 GTEST_API_ void PrintWideStringTo(const ::std::wstring&s, ::std::ostream* os);
00482 inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
00483   PrintWideStringTo(s, os);
00484 }
00485 #endif  // GTEST_HAS_STD_WSTRING
00486 
00487 #if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
00488 // Helper function for printing a tuple.  T must be instantiated with
00489 // a tuple type.
00490 template <typename T>
00491 void PrintTupleTo(const T& t, ::std::ostream* os);
00492 #endif  // GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
00493 
00494 #if GTEST_HAS_TR1_TUPLE
00495 // Overload for ::std::tr1::tuple.  Needed for printing function arguments,
00496 // which are packed as tuples.
00497 
00498 // Overloaded PrintTo() for tuples of various arities.  We support
00499 // tuples of up-to 10 fields.  The following implementation works
00500 // regardless of whether tr1::tuple is implemented using the
00501 // non-standard variadic template feature or not.
00502 
00503 inline void PrintTo(const ::std::tr1::tuple<>& t, ::std::ostream* os) {
00504   PrintTupleTo(t, os);
00505 }
00506 
00507 template <typename T1>
00508 void PrintTo(const ::std::tr1::tuple<T1>& t, ::std::ostream* os) {
00509   PrintTupleTo(t, os);
00510 }
00511 
00512 template <typename T1, typename T2>
00513 void PrintTo(const ::std::tr1::tuple<T1, T2>& t, ::std::ostream* os) {
00514   PrintTupleTo(t, os);
00515 }
00516 
00517 template <typename T1, typename T2, typename T3>
00518 void PrintTo(const ::std::tr1::tuple<T1, T2, T3>& t, ::std::ostream* os) {
00519   PrintTupleTo(t, os);
00520 }
00521 
00522 template <typename T1, typename T2, typename T3, typename T4>
00523 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4>& t, ::std::ostream* os) {
00524   PrintTupleTo(t, os);
00525 }
00526 
00527 template <typename T1, typename T2, typename T3, typename T4, typename T5>
00528 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5>& t,
00529              ::std::ostream* os) {
00530   PrintTupleTo(t, os);
00531 }
00532 
00533 template <typename T1, typename T2, typename T3, typename T4, typename T5,
00534           typename T6>
00535 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6>& t,
00536              ::std::ostream* os) {
00537   PrintTupleTo(t, os);
00538 }
00539 
00540 template <typename T1, typename T2, typename T3, typename T4, typename T5,
00541           typename T6, typename T7>
00542 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7>& t,
00543              ::std::ostream* os) {
00544   PrintTupleTo(t, os);
00545 }
00546 
00547 template <typename T1, typename T2, typename T3, typename T4, typename T5,
00548           typename T6, typename T7, typename T8>
00549 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8>& t,
00550              ::std::ostream* os) {
00551   PrintTupleTo(t, os);
00552 }
00553 
00554 template <typename T1, typename T2, typename T3, typename T4, typename T5,
00555           typename T6, typename T7, typename T8, typename T9>
00556 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9>& t,
00557              ::std::ostream* os) {
00558   PrintTupleTo(t, os);
00559 }
00560 
00561 template <typename T1, typename T2, typename T3, typename T4, typename T5,
00562           typename T6, typename T7, typename T8, typename T9, typename T10>
00563 void PrintTo(
00564     const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& t,
00565     ::std::ostream* os) {
00566   PrintTupleTo(t, os);
00567 }
00568 #endif  // GTEST_HAS_TR1_TUPLE
00569 
00570 #if GTEST_HAS_STD_TUPLE_
00571 template <typename... Types>
00572 void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
00573   PrintTupleTo(t, os);
00574 }
00575 #endif  // GTEST_HAS_STD_TUPLE_
00576 
00577 // Overload for std::pair.
00578 template <typename T1, typename T2>
00579 void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
00580   *os << '(';
00581   // We cannot use UniversalPrint(value.first, os) here, as T1 may be
00582   // a reference type.  The same for printing value.second.
00583   UniversalPrinter<T1>::Print(value.first, os);
00584   *os << ", ";
00585   UniversalPrinter<T2>::Print(value.second, os);
00586   *os << ')';
00587 }
00588 
00589 // Implements printing a non-reference type T by letting the compiler
00590 // pick the right overload of PrintTo() for T.
00591 template <typename T>
00592 class UniversalPrinter {
00593  public:
00594   // MSVC warns about adding const to a function type, so we want to
00595   // disable the warning.
00596   GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
00597 
00598   // Note: we deliberately don't call this PrintTo(), as that name
00599   // conflicts with ::testing::internal::PrintTo in the body of the
00600   // function.
00601   static void Print(const T& value, ::std::ostream* os) {
00602     // By default, ::testing::internal::PrintTo() is used for printing
00603     // the value.
00604     //
00605     // Thanks to Koenig look-up, if T is a class and has its own
00606     // PrintTo() function defined in its namespace, that function will
00607     // be visible here.  Since it is more specific than the generic ones
00608     // in ::testing::internal, it will be picked by the compiler in the
00609     // following statement - exactly what we want.
00610     PrintTo(value, os);
00611   }
00612 
00613   GTEST_DISABLE_MSC_WARNINGS_POP_()
00614 };
00615 
00616 // UniversalPrintArray(begin, len, os) prints an array of 'len'
00617 // elements, starting at address 'begin'.
00618 template <typename T>
00619 void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
00620   if (len == 0) {
00621     *os << "{}";
00622   } else {
00623     *os << "{ ";
00624     const size_t kThreshold = 18;
00625     const size_t kChunkSize = 8;
00626     // If the array has more than kThreshold elements, we'll have to
00627     // omit some details by printing only the first and the last
00628     // kChunkSize elements.
00629     // TODO(wan@google.com): let the user control the threshold using a flag.
00630     if (len <= kThreshold) {
00631       PrintRawArrayTo(begin, len, os);
00632     } else {
00633       PrintRawArrayTo(begin, kChunkSize, os);
00634       *os << ", ..., ";
00635       PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
00636     }
00637     *os << " }";
00638   }
00639 }
00640 // This overload prints a (const) char array compactly.
00641 GTEST_API_ void UniversalPrintArray(
00642     const char* begin, size_t len, ::std::ostream* os);
00643 
00644 // This overload prints a (const) wchar_t array compactly.
00645 GTEST_API_ void UniversalPrintArray(
00646     const wchar_t* begin, size_t len, ::std::ostream* os);
00647 
00648 // Implements printing an array type T[N].
00649 template <typename T, size_t N>
00650 class UniversalPrinter<T[N]> {
00651  public:
00652   // Prints the given array, omitting some elements when there are too
00653   // many.
00654   static void Print(const T (&a)[N], ::std::ostream* os) {
00655     UniversalPrintArray(a, N, os);
00656   }
00657 };
00658 
00659 // Implements printing a reference type T&.
00660 template <typename T>
00661 class UniversalPrinter<T&> {
00662  public:
00663   // MSVC warns about adding const to a function type, so we want to
00664   // disable the warning.
00665   GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
00666 
00667   static void Print(const T& value, ::std::ostream* os) {
00668     // Prints the address of the value.  We use reinterpret_cast here
00669     // as static_cast doesn't compile when T is a function type.
00670     *os << "@" << reinterpret_cast<const void*>(&value) << " ";
00671 
00672     // Then prints the value itself.
00673     UniversalPrint(value, os);
00674   }
00675 
00676   GTEST_DISABLE_MSC_WARNINGS_POP_()
00677 };
00678 
00679 // Prints a value tersely: for a reference type, the referenced value
00680 // (but not the address) is printed; for a (const) char pointer, the
00681 // NUL-terminated string (but not the pointer) is printed.
00682 
00683 template <typename T>
00684 class UniversalTersePrinter {
00685  public:
00686   static void Print(const T& value, ::std::ostream* os) {
00687     UniversalPrint(value, os);
00688   }
00689 };
00690 template <typename T>
00691 class UniversalTersePrinter<T&> {
00692  public:
00693   static void Print(const T& value, ::std::ostream* os) {
00694     UniversalPrint(value, os);
00695   }
00696 };
00697 template <typename T, size_t N>
00698 class UniversalTersePrinter<T[N]> {
00699  public:
00700   static void Print(const T (&value)[N], ::std::ostream* os) {
00701     UniversalPrinter<T[N]>::Print(value, os);
00702   }
00703 };
00704 template <>
00705 class UniversalTersePrinter<const char*> {
00706  public:
00707   static void Print(const char* str, ::std::ostream* os) {
00708     if (str == NULL) {
00709       *os << "NULL";
00710     } else {
00711       UniversalPrint(string(str), os);
00712     }
00713   }
00714 };
00715 template <>
00716 class UniversalTersePrinter<char*> {
00717  public:
00718   static void Print(char* str, ::std::ostream* os) {
00719     UniversalTersePrinter<const char*>::Print(str, os);
00720   }
00721 };
00722 
00723 #if GTEST_HAS_STD_WSTRING
00724 template <>
00725 class UniversalTersePrinter<const wchar_t*> {
00726  public:
00727   static void Print(const wchar_t* str, ::std::ostream* os) {
00728     if (str == NULL) {
00729       *os << "NULL";
00730     } else {
00731       UniversalPrint(::std::wstring(str), os);
00732     }
00733   }
00734 };
00735 #endif
00736 
00737 template <>
00738 class UniversalTersePrinter<wchar_t*> {
00739  public:
00740   static void Print(wchar_t* str, ::std::ostream* os) {
00741     UniversalTersePrinter<const wchar_t*>::Print(str, os);
00742   }
00743 };
00744 
00745 template <typename T>
00746 void UniversalTersePrint(const T& value, ::std::ostream* os) {
00747   UniversalTersePrinter<T>::Print(value, os);
00748 }
00749 
00750 // Prints a value using the type inferred by the compiler.  The
00751 // difference between this and UniversalTersePrint() is that for a
00752 // (const) char pointer, this prints both the pointer and the
00753 // NUL-terminated string.
00754 template <typename T>
00755 void UniversalPrint(const T& value, ::std::ostream* os) {
00756   // A workarond for the bug in VC++ 7.1 that prevents us from instantiating
00757   // UniversalPrinter with T directly.
00758   typedef T T1;
00759   UniversalPrinter<T1>::Print(value, os);
00760 }
00761 
00762 typedef ::std::vector<string> Strings;
00763 
00764 // TuplePolicy<TupleT> must provide:
00765 // - tuple_size
00766 //     size of tuple TupleT.
00767 // - get<size_t I>(const TupleT& t)
00768 //     static function extracting element I of tuple TupleT.
00769 // - tuple_element<size_t I>::type
00770 //     type of element I of tuple TupleT.
00771 template <typename TupleT>
00772 struct TuplePolicy;
00773 
00774 #if GTEST_HAS_TR1_TUPLE
00775 template <typename TupleT>
00776 struct TuplePolicy {
00777   typedef TupleT Tuple;
00778   static const size_t tuple_size = ::std::tr1::tuple_size<Tuple>::value;
00779 
00780   template <size_t I>
00781   struct tuple_element : ::std::tr1::tuple_element<I, Tuple> {};
00782 
00783   template <size_t I>
00784   static typename AddReference<
00785       const typename ::std::tr1::tuple_element<I, Tuple>::type>::type get(
00786       const Tuple& tuple) {
00787     return ::std::tr1::get<I>(tuple);
00788   }
00789 };
00790 template <typename TupleT>
00791 const size_t TuplePolicy<TupleT>::tuple_size;
00792 #endif  // GTEST_HAS_TR1_TUPLE
00793 
00794 #if GTEST_HAS_STD_TUPLE_
00795 template <typename... Types>
00796 struct TuplePolicy< ::std::tuple<Types...> > {
00797   typedef ::std::tuple<Types...> Tuple;
00798   static const size_t tuple_size = ::std::tuple_size<Tuple>::value;
00799 
00800   template <size_t I>
00801   struct tuple_element : ::std::tuple_element<I, Tuple> {};
00802 
00803   template <size_t I>
00804   static const typename ::std::tuple_element<I, Tuple>::type& get(
00805       const Tuple& tuple) {
00806     return ::std::get<I>(tuple);
00807   }
00808 };
00809 template <typename... Types>
00810 const size_t TuplePolicy< ::std::tuple<Types...> >::tuple_size;
00811 #endif  // GTEST_HAS_STD_TUPLE_
00812 
00813 #if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
00814 // This helper template allows PrintTo() for tuples and
00815 // UniversalTersePrintTupleFieldsToStrings() to be defined by
00816 // induction on the number of tuple fields.  The idea is that
00817 // TuplePrefixPrinter<N>::PrintPrefixTo(t, os) prints the first N
00818 // fields in tuple t, and can be defined in terms of
00819 // TuplePrefixPrinter<N - 1>.
00820 //
00821 // The inductive case.
00822 template <size_t N>
00823 struct TuplePrefixPrinter {
00824   // Prints the first N fields of a tuple.
00825   template <typename Tuple>
00826   static void PrintPrefixTo(const Tuple& t, ::std::ostream* os) {
00827     TuplePrefixPrinter<N - 1>::PrintPrefixTo(t, os);
00828     GTEST_INTENTIONAL_CONST_COND_PUSH_()
00829     if (N > 1) {
00830     GTEST_INTENTIONAL_CONST_COND_POP_()
00831       *os << ", ";
00832     }
00833     UniversalPrinter<
00834         typename TuplePolicy<Tuple>::template tuple_element<N - 1>::type>
00835         ::Print(TuplePolicy<Tuple>::template get<N - 1>(t), os);
00836   }
00837 
00838   // Tersely prints the first N fields of a tuple to a string vector,
00839   // one element for each field.
00840   template <typename Tuple>
00841   static void TersePrintPrefixToStrings(const Tuple& t, Strings* strings) {
00842     TuplePrefixPrinter<N - 1>::TersePrintPrefixToStrings(t, strings);
00843     ::std::stringstream ss;
00844     UniversalTersePrint(TuplePolicy<Tuple>::template get<N - 1>(t), &ss);
00845     strings->push_back(ss.str());
00846   }
00847 };
00848 
00849 // Base case.
00850 template <>
00851 struct TuplePrefixPrinter<0> {
00852   template <typename Tuple>
00853   static void PrintPrefixTo(const Tuple&, ::std::ostream*) {}
00854 
00855   template <typename Tuple>
00856   static void TersePrintPrefixToStrings(const Tuple&, Strings*) {}
00857 };
00858 
00859 // Helper function for printing a tuple.
00860 // Tuple must be either std::tr1::tuple or std::tuple type.
00861 template <typename Tuple>
00862 void PrintTupleTo(const Tuple& t, ::std::ostream* os) {
00863   *os << "(";
00864   TuplePrefixPrinter<TuplePolicy<Tuple>::tuple_size>::PrintPrefixTo(t, os);
00865   *os << ")";
00866 }
00867 
00868 // Prints the fields of a tuple tersely to a string vector, one
00869 // element for each field.  See the comment before
00870 // UniversalTersePrint() for how we define "tersely".
00871 template <typename Tuple>
00872 Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
00873   Strings result;
00874   TuplePrefixPrinter<TuplePolicy<Tuple>::tuple_size>::
00875       TersePrintPrefixToStrings(value, &result);
00876   return result;
00877 }
00878 #endif  // GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
00879 
00880 }  // namespace internal
00881 
00882 template <typename T>
00883 ::std::string PrintToString(const T& value) {
00884   ::std::stringstream ss;
00885   internal::UniversalTersePrinter<T>::Print(value, &ss);
00886   return ss.str();
00887 }
00888 
00889 }  // namespace testing
00890 
00891 #endif  // GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:03