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


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:47