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