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