gtest/include/gtest/gtest-printers.h
Go to the documentation of this file.
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 Test - The Google C++ Testing Framework
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 //
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.
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
56 // printed.
57 //
58 // We also provide some convenient wrappers:
59 //
60 // // Prints a value to a string. For a (const or not) char
61 // // pointer, the NUL-terminated string (but not the pointer) is
62 // // printed.
63 // std::string ::testing::PrintToString(const T& value);
64 //
65 // // Prints a value tersely: for a reference type, the referenced
66 // // value (but not the address) is printed; for a (const or not) char
67 // // pointer, the NUL-terminated string (but not the pointer) is
68 // // printed.
69 // void ::testing::internal::UniversalTersePrint(const T& value, ostream*);
70 //
71 // // Prints value using the type inferred by the compiler. The difference
72 // // from UniversalTersePrint() is that this function prints both the
73 // // pointer and the NUL-terminated string for a (const or not) char pointer.
74 // void ::testing::internal::UniversalPrint(const T& value, ostream*);
75 //
76 // // Prints the fields of a tuple tersely to a string vector, one
77 // // element for each field. Tuple support must be enabled in
78 // // gtest-port.h.
79 // std::vector<string> UniversalTersePrintTupleFieldsToStrings(
80 // const Tuple& value);
81 //
82 // Known limitation:
83 //
84 // The print primitives print the elements of an STL-style container
85 // using the compiler-inferred type of *iter where iter is a
86 // const_iterator of the container. When const_iterator is an input
87 // iterator but not a forward iterator, this inferred type may not
88 // match value_type, and the print output may be incorrect. In
89 // practice, this is rarely a problem as for most containers
90 // const_iterator is a forward iterator. We'll fix this if there's an
91 // actual need for it. Note that this fix cannot rely on value_type
92 // being defined as many user-defined container types don't have
93 // value_type.
94 
95 #ifndef GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
96 #define GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
97 
98 #include <ostream> // NOLINT
99 #include <sstream>
100 #include <string>
101 #include <utility>
102 #include <vector>
103 #include "gtest/internal/gtest-port.h"
104 #include "gtest/internal/gtest-internal.h"
105 
106 namespace testing
107 {
108 
109 // Definitions in the 'internal' and 'internal2' name spaces are
110 // subject to change without notice. DO NOT USE THEM IN USER CODE!
111 namespace internal2
112 {
113 
114 // Prints the given number of bytes in the given object to the given
115 // ostream.
116 GTEST_API_ void PrintBytesInObjectTo(const unsigned char * obj_bytes,
117  size_t count,
118  ::std::ostream * os);
119 
120 // For selecting which printer to use when a given type has neither <<
121 // nor PrintTo().
123 {
124  kProtobuf, // a protobuf type
125  kConvertibleToInteger, // a type implicitly convertible to BiggestInt
126  // (e.g. a named or unnamed enum type)
127  kOtherType // anything else
128 };
129 
130 // TypeWithoutFormatter<T, kTypeKind>::PrintValue(value, os) is called
131 // by the universal printer to print a value of type T when neither
132 // operator<< nor PrintTo() is defined for T, where kTypeKind is the
133 // "kind" of T as defined by enum TypeKind.
134 template <typename T, TypeKind kTypeKind>
136 {
137 public:
138  // This default version is called when kTypeKind is kOtherType.
139  static void PrintValue(const T & value, ::std::ostream * os)
140  {
141  PrintBytesInObjectTo(reinterpret_cast<const unsigned char *>(&value),
142  sizeof(value), os);
143  }
144 };
145 
146 // We print a protobuf using its ShortDebugString() when the string
147 // doesn't exceed this many characters; otherwise we print it using
148 // DebugString() for better readability.
149 const size_t kProtobufOneLinerMaxLength = 50;
150 
151 template <typename T>
152 class TypeWithoutFormatter<T, kProtobuf>
153 {
154 public:
155  static void PrintValue(const T & value, ::std::ostream * os)
156  {
157  const ::testing::internal::string short_str = value.ShortDebugString();
159  short_str.length() <= kProtobufOneLinerMaxLength ?
160  short_str : ("\n" + value.DebugString());
161  *os << ("<" + pretty_str + ">");
162  }
163 };
164 
165 template <typename T>
166 class TypeWithoutFormatter<T, kConvertibleToInteger>
167 {
168 public:
169  // Since T has no << operator or PrintTo() but can be implicitly
170  // converted to BiggestInt, we print it as a BiggestInt.
171  //
172  // Most likely T is an enum type (either named or unnamed), in which
173  // case printing it as an integer is the desired behavior. In case
174  // T is not an enum, printing it as an integer is the best we can do
175  // given that it has no user-defined printer.
176  static void PrintValue(const T & value, ::std::ostream * os)
177  {
178  const internal::BiggestInt kBigInt = value;
179  *os << kBigInt;
180  }
181 };
182 
183 // Prints the given value to the given ostream. If the value is a
184 // protocol message, its debug string is printed; if it's an enum or
185 // of a type implicitly convertible to BiggestInt, it's printed as an
186 // integer; otherwise the bytes in the value are printed. This is
187 // what UniversalPrinter<T>::Print() does when it knows nothing about
188 // type T and T has neither << operator nor PrintTo().
189 //
190 // A user can override this behavior for a class type Foo by defining
191 // a << operator in the namespace where Foo is defined.
192 //
193 // We put this operator in namespace 'internal2' instead of 'internal'
194 // to simplify the implementation, as much code in 'internal' needs to
195 // use << in STL, which would conflict with our own << were it defined
196 // in 'internal'.
197 //
198 // Note that this operator<< takes a generic std::basic_ostream<Char,
199 // CharTraits> type instead of the more restricted std::ostream. If
200 // we define it to take an std::ostream instead, we'll get an
201 // "ambiguous overloads" compiler error when trying to print a type
202 // Foo that supports streaming to std::basic_ostream<Char,
203 // CharTraits>, as the compiler cannot tell whether
204 // operator<<(std::ostream&, const T&) or
205 // operator<<(std::basic_stream<Char, CharTraits>, const Foo&) is more
206 // specific.
207 template <typename Char, typename CharTraits, typename T>
208 ::std::basic_ostream<Char, CharTraits> & operator<<(
209  ::std::basic_ostream<Char, CharTraits> & os, const T & x)
210 {
214  kConvertibleToInteger : kOtherType) >::PrintValue(x, &os);
215  return os;
216 }
217 
218 } // namespace internal2
219 } // namespace testing
220 
221 // This namespace MUST NOT BE NESTED IN ::testing, or the name look-up
222 // magic needed for implementing UniversalPrinter won't work.
223 namespace testing_internal
224 {
225 
226 // Used to print a value that is not an STL-style container when the
227 // user doesn't define PrintTo() for it.
228 template <typename T>
229 void DefaultPrintNonContainerTo(const T & value, ::std::ostream * os)
230 {
231  // With the following statement, during unqualified name lookup,
232  // testing::internal2::operator<< appears as if it was declared in
233  // the nearest enclosing namespace that contains both
234  // ::testing_internal and ::testing::internal2, i.e. the global
235  // namespace. For more details, refer to the C++ Standard section
236  // 7.3.4-1 [namespace.udir]. This allows us to fall back onto
237  // testing::internal2::operator<< in case T doesn't come with a <<
238  // operator.
239  //
240  // We cannot write 'using ::testing::internal2::operator<<;', which
241  // gcc 3.3 fails to compile due to a compiler bug.
242  using namespace ::testing::internal2; // NOLINT
243 
244  // Assuming T is defined in namespace foo, in the next statement,
245  // the compiler will consider all of:
246  //
247  // 1. foo::operator<< (thanks to Koenig look-up),
248  // 2. ::operator<< (as the current namespace is enclosed in ::),
249  // 3. testing::internal2::operator<< (thanks to the using statement above).
250  //
251  // The operator<< whose type matches T best will be picked.
252  //
253  // We deliberately allow #2 to be a candidate, as sometimes it's
254  // impossible to define #1 (e.g. when foo is ::std, defining
255  // anything in it is undefined behavior unless you are a compiler
256  // vendor.).
257  *os << value;
258 }
259 
260 } // namespace testing_internal
261 
262 namespace testing
263 {
264 namespace internal
265 {
266 
267 // UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
268 // value to the given ostream. The caller must ensure that
269 // 'ostream_ptr' is not NULL, or the behavior is undefined.
270 //
271 // We define UniversalPrinter as a class template (as opposed to a
272 // function template), as we need to partially specialize it for
273 // reference types, which cannot be done with function templates.
274 template <typename T>
275 class UniversalPrinter;
276 
277 template <typename T>
278 void UniversalPrint(const T & value, ::std::ostream * os);
279 
280 // Used to print an STL-style container when the user doesn't define
281 // a PrintTo() for it.
282 template <typename C>
283 void DefaultPrintTo(IsContainer /* dummy */,
284  false_type /* is not a pointer */,
285  const C & container, ::std::ostream * os)
286 {
287  const size_t kMaxCount = 32; // The maximum number of elements to print.
288  *os << '{';
289  size_t count = 0;
290 
291  for (typename C::const_iterator it = container.begin();
292  it != container.end(); ++it, ++count)
293  {
294  if (count > 0)
295  {
296  *os << ',';
297 
298  if (count == kMaxCount) // Enough has been printed.
299  {
300  *os << " ...";
301  break;
302  }
303  }
304 
305  *os << ' ';
306  // We cannot call PrintTo(*it, os) here as PrintTo() doesn't
307  // handle *it being a native array.
308  internal::UniversalPrint(*it, os);
309  }
310 
311  if (count > 0)
312  {
313  *os << ' ';
314  }
315 
316  *os << '}';
317 }
318 
319 // Used to print a pointer that is neither a char pointer nor a member
320 // pointer, when the user doesn't define PrintTo() for it. (A member
321 // variable pointer or member function pointer doesn't really point to
322 // a location in the address space. Their representation is
323 // implementation-defined. Therefore they will be printed as raw
324 // bytes.)
325 template <typename T>
326 void DefaultPrintTo(IsNotContainer /* dummy */,
327  true_type /* is a pointer */,
328  T * p, ::std::ostream * os)
329 {
330  if (p == NULL)
331  {
332  *os << "NULL";
333  }
334 
335  else
336  {
337  // C++ doesn't allow casting from a function pointer to any object
338  // pointer.
339  //
340  // IsTrue() silences warnings: "Condition is always true",
341  // "unreachable code".
342  if (IsTrue(ImplicitlyConvertible<T *, const void *>::value))
343  {
344  // T is not a function type. We just call << to print p,
345  // relying on ADL to pick up user-defined << for their pointer
346  // types, if any.
347  *os << p;
348  }
349 
350  else
351  {
352  // T is a function type, so '*os << p' doesn't do what we want
353  // (it just prints p as bool). We want to print p as a const
354  // void*. However, we cannot cast it to const void* directly,
355  // even using reinterpret_cast, as earlier versions of gcc
356  // (e.g. 3.4.5) cannot compile the cast when p is a function
357  // pointer. Casting to UInt64 first solves the problem.
358  *os << reinterpret_cast<const void *>(
359  reinterpret_cast<internal::UInt64>(p));
360  }
361  }
362 }
363 
364 // Used to print a non-container, non-pointer value when the user
365 // doesn't define PrintTo() for it.
366 template <typename T>
367 void DefaultPrintTo(IsNotContainer /* dummy */,
368  false_type /* is not a pointer */,
369  const T & value, ::std::ostream * os)
370 {
372 }
373 
374 // Prints the given value using the << operator if it has one;
375 // otherwise prints the bytes in it. This is what
376 // UniversalPrinter<T>::Print() does when PrintTo() is not specialized
377 // or overloaded for type T.
378 //
379 // A user can override this behavior for a class type Foo by defining
380 // an overload of PrintTo() in the namespace where Foo is defined. We
381 // give the user this option as sometimes defining a << operator for
382 // Foo is not desirable (e.g. the coding style may prevent doing it,
383 // or there is already a << operator but it doesn't do what the user
384 // wants).
385 template <typename T>
386 void PrintTo(const T & value, ::std::ostream * os)
387 {
388  // DefaultPrintTo() is overloaded. The type of its first two
389  // arguments determine which version will be picked. If T is an
390  // STL-style container, the version for container will be called; if
391  // T is a pointer, the pointer version will be called; otherwise the
392  // generic version will be called.
393  //
394  // Note that we check for container types here, prior to we check
395  // for protocol message types in our operator<<. The rationale is:
396  //
397  // For protocol messages, we want to give people a chance to
398  // override Google Mock's format by defining a PrintTo() or
399  // operator<<. For STL containers, other formats can be
400  // incompatible with Google Mock's format for the container
401  // elements; therefore we check for container types here to ensure
402  // that our format is used.
403  //
404  // The second argument of DefaultPrintTo() is needed to bypass a bug
405  // in Symbian's C++ compiler that prevents it from picking the right
406  // overload between:
407  //
408  // PrintTo(const T& x, ...);
409  // PrintTo(T* x, ...);
410  DefaultPrintTo(IsContainerTest<T>(0), is_pointer<T>(), value, os);
411 }
412 
413 // The following list of PrintTo() overloads tells
414 // UniversalPrinter<T>::Print() how to print standard types (built-in
415 // types, strings, plain arrays, and pointers).
416 
417 // Overloads for various char types.
418 GTEST_API_ void PrintTo(unsigned char c, ::std::ostream * os);
419 GTEST_API_ void PrintTo(signed char c, ::std::ostream * os);
420 inline void PrintTo(char c, ::std::ostream * os)
421 {
422  // When printing a plain char, we always treat it as unsigned. This
423  // way, the output won't be affected by whether the compiler thinks
424  // char is signed or not.
425  PrintTo(static_cast<unsigned char>(c), os);
426 }
427 
428 // Overloads for other simple built-in types.
429 inline void PrintTo(bool x, ::std::ostream * os)
430 {
431  *os << (x ? "true" : "false");
432 }
433 
434 // Overload for wchar_t type.
435 // Prints a wchar_t as a symbol if it is printable or as its internal
436 // code otherwise and also as its decimal code (except for L'\0').
437 // The L'\0' char is printed as "L'\\0'". The decimal code is printed
438 // as signed integer when wchar_t is implemented by the compiler
439 // as a signed type and is printed as an unsigned integer when wchar_t
440 // is implemented as an unsigned type.
441 GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream * os);
442 
443 // Overloads for C strings.
444 GTEST_API_ void PrintTo(const char * s, ::std::ostream * os);
445 inline void PrintTo(char * s, ::std::ostream * os)
446 {
447  PrintTo(ImplicitCast_<const char *>(s), os);
448 }
449 
450 // signed/unsigned char is often used for representing binary data, so
451 // we print pointers to it as void* to be safe.
452 inline void PrintTo(const signed char * s, ::std::ostream * os)
453 {
454  PrintTo(ImplicitCast_<const void *>(s), os);
455 }
456 inline void PrintTo(signed char * s, ::std::ostream * os)
457 {
458  PrintTo(ImplicitCast_<const void *>(s), os);
459 }
460 inline void PrintTo(const unsigned char * s, ::std::ostream * os)
461 {
462  PrintTo(ImplicitCast_<const void *>(s), os);
463 }
464 inline void PrintTo(unsigned char * s, ::std::ostream * os)
465 {
466  PrintTo(ImplicitCast_<const void *>(s), os);
467 }
468 
469 // MSVC can be configured to define wchar_t as a typedef of unsigned
470 // short. It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native
471 // type. When wchar_t is a typedef, defining an overload for const
472 // wchar_t* would cause unsigned short* be printed as a wide string,
473 // possibly causing invalid memory accesses.
474 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
475 // Overloads for wide C strings
476 GTEST_API_ void PrintTo(const wchar_t * s, ::std::ostream * os);
477 inline void PrintTo(wchar_t * s, ::std::ostream * os)
478 {
479  PrintTo(ImplicitCast_<const wchar_t *>(s), os);
480 }
481 #endif
482 
483 // Overload for C arrays. Multi-dimensional arrays are printed
484 // properly.
485 
486 // Prints the given number of elements in an array, without printing
487 // the curly braces.
488 template <typename T>
489 void PrintRawArrayTo(const T a[], size_t count, ::std::ostream * os)
490 {
491  UniversalPrint(a[0], os);
492 
493  for (size_t i = 1; i != count; i++)
494  {
495  *os << ", ";
496  UniversalPrint(a[i], os);
497  }
498 }
499 
500 // Overloads for ::string and ::std::string.
501 #if GTEST_HAS_GLOBAL_STRING
502 GTEST_API_ void PrintStringTo(const ::string & s, ::std::ostream * os);
503 inline void PrintTo(const ::string & s, ::std::ostream * os)
504 {
505  PrintStringTo(s, os);
506 }
507 #endif // GTEST_HAS_GLOBAL_STRING
508 
509 GTEST_API_ void PrintStringTo(const ::std::string & s, ::std::ostream * os);
510 inline void PrintTo(const ::std::string & s, ::std::ostream * os)
511 {
512  PrintStringTo(s, os);
513 }
514 
515 // Overloads for ::wstring and ::std::wstring.
516 #if GTEST_HAS_GLOBAL_WSTRING
517 GTEST_API_ void PrintWideStringTo(const ::wstring & s, ::std::ostream * os);
518 inline void PrintTo(const ::wstring & s, ::std::ostream * os)
519 {
520  PrintWideStringTo(s, os);
521 }
522 #endif // GTEST_HAS_GLOBAL_WSTRING
523 
524 #if GTEST_HAS_STD_WSTRING
525 GTEST_API_ void PrintWideStringTo(const ::std::wstring & s, ::std::ostream * os);
526 inline void PrintTo(const ::std::wstring & s, ::std::ostream * os)
527 {
528  PrintWideStringTo(s, os);
529 }
530 #endif // GTEST_HAS_STD_WSTRING
531 
532 #if GTEST_HAS_TR1_TUPLE
533 // Overload for ::std::tr1::tuple. Needed for printing function arguments,
534 // which are packed as tuples.
535 
536 // Helper function for printing a tuple. T must be instantiated with
537 // a tuple type.
538 template <typename T>
539 void PrintTupleTo(const T & t, ::std::ostream * os);
540 
541 // Overloaded PrintTo() for tuples of various arities. We support
542 // tuples of up-to 10 fields. The following implementation works
543 // regardless of whether tr1::tuple is implemented using the
544 // non-standard variadic template feature or not.
545 
546 inline void PrintTo(const ::std::tr1::tuple<> & t, ::std::ostream * os)
547 {
548  PrintTupleTo(t, os);
549 }
550 
551 template <typename T1>
552 void PrintTo(const ::std::tr1::tuple<T1> & t, ::std::ostream * os)
553 {
554  PrintTupleTo(t, os);
555 }
556 
557 template <typename T1, typename T2>
558 void PrintTo(const ::std::tr1::tuple<T1, T2> & t, ::std::ostream * os)
559 {
560  PrintTupleTo(t, os);
561 }
562 
563 template <typename T1, typename T2, typename T3>
564 void PrintTo(const ::std::tr1::tuple<T1, T2, T3> & t, ::std::ostream * os)
565 {
566  PrintTupleTo(t, os);
567 }
568 
569 template <typename T1, typename T2, typename T3, typename T4>
570 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4> & t, ::std::ostream * os)
571 {
572  PrintTupleTo(t, os);
573 }
574 
575 template <typename T1, typename T2, typename T3, typename T4, typename T5>
576 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5> & t,
577  ::std::ostream * os)
578 {
579  PrintTupleTo(t, os);
580 }
581 
582 template <typename T1, typename T2, typename T3, typename T4, typename T5,
583  typename T6>
584 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6> & t,
585  ::std::ostream * os)
586 {
587  PrintTupleTo(t, os);
588 }
589 
590 template <typename T1, typename T2, typename T3, typename T4, typename T5,
591  typename T6, typename T7>
592 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7> & t,
593  ::std::ostream * os)
594 {
595  PrintTupleTo(t, os);
596 }
597 
598 template <typename T1, typename T2, typename T3, typename T4, typename T5,
599  typename T6, typename T7, typename T8>
600 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8> & t,
601  ::std::ostream * os)
602 {
603  PrintTupleTo(t, os);
604 }
605 
606 template <typename T1, typename T2, typename T3, typename T4, typename T5,
607  typename T6, typename T7, typename T8, typename T9>
608 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9> & t,
609  ::std::ostream * os)
610 {
611  PrintTupleTo(t, os);
612 }
613 
614 template <typename T1, typename T2, typename T3, typename T4, typename T5,
615  typename T6, typename T7, typename T8, typename T9, typename T10>
616 void PrintTo(
617  const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> & t,
618  ::std::ostream * os)
619 {
620  PrintTupleTo(t, os);
621 }
622 #endif // GTEST_HAS_TR1_TUPLE
623 
624 // Overload for std::pair.
625 template <typename T1, typename T2>
626 void PrintTo(const ::std::pair<T1, T2> & value, ::std::ostream * os)
627 {
628  *os << '(';
629  // We cannot use UniversalPrint(value.first, os) here, as T1 may be
630  // a reference type. The same for printing value.second.
631  UniversalPrinter<T1>::Print(value.first, os);
632  *os << ", ";
633  UniversalPrinter<T2>::Print(value.second, os);
634  *os << ')';
635 }
636 
637 // Implements printing a non-reference type T by letting the compiler
638 // pick the right overload of PrintTo() for T.
639 template <typename T>
640 class UniversalPrinter
641 {
642 public:
643  // MSVC warns about adding const to a function type, so we want to
644  // disable the warning.
645 #ifdef _MSC_VER
646 # pragma warning(push) // Saves the current warning state.
647 # pragma warning(disable:4180) // Temporarily disables warning 4180.
648 #endif // _MSC_VER
649 
650  // Note: we deliberately don't call this PrintTo(), as that name
651  // conflicts with ::testing::internal::PrintTo in the body of the
652  // function.
653  static void Print(const T & value, ::std::ostream * os)
654  {
655  // By default, ::testing::internal::PrintTo() is used for printing
656  // the value.
657  //
658  // Thanks to Koenig look-up, if T is a class and has its own
659  // PrintTo() function defined in its namespace, that function will
660  // be visible here. Since it is more specific than the generic ones
661  // in ::testing::internal, it will be picked by the compiler in the
662  // following statement - exactly what we want.
663  PrintTo(value, os);
664  }
665 
666 #ifdef _MSC_VER
667 # pragma warning(pop) // Restores the warning state.
668 #endif // _MSC_VER
669 };
670 
671 // UniversalPrintArray(begin, len, os) prints an array of 'len'
672 // elements, starting at address 'begin'.
673 template <typename T>
674 void UniversalPrintArray(const T * begin, size_t len, ::std::ostream * os)
675 {
676  if (len == 0)
677  {
678  *os << "{}";
679  }
680 
681  else
682  {
683  *os << "{ ";
684  const size_t kThreshold = 18;
685  const size_t kChunkSize = 8;
686 
687  // If the array has more than kThreshold elements, we'll have to
688  // omit some details by printing only the first and the last
689  // kChunkSize elements.
690  // TODO(wan@google.com): let the user control the threshold using a flag.
691  if (len <= kThreshold)
692  {
693  PrintRawArrayTo(begin, len, os);
694  }
695 
696  else
697  {
698  PrintRawArrayTo(begin, kChunkSize, os);
699  *os << ", ..., ";
700  PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
701  }
702 
703  *os << " }";
704  }
705 }
706 // This overload prints a (const) char array compactly.
708  const char * begin, size_t len, ::std::ostream * os);
709 
710 // This overload prints a (const) wchar_t array compactly.
712  const wchar_t * begin, size_t len, ::std::ostream * os);
713 
714 // Implements printing an array type T[N].
715 template <typename T, size_t N>
716 class UniversalPrinter<T[N]>
717 {
718 public:
719  // Prints the given array, omitting some elements when there are too
720  // many.
721  static void Print(const T(&a)[N], ::std::ostream * os)
722  {
723  UniversalPrintArray(a, N, os);
724  }
725 };
726 
727 // Implements printing a reference type T&.
728 template <typename T>
729 class UniversalPrinter<T &>
730 {
731 public:
732  // MSVC warns about adding const to a function type, so we want to
733  // disable the warning.
734 #ifdef _MSC_VER
735 # pragma warning(push) // Saves the current warning state.
736 # pragma warning(disable:4180) // Temporarily disables warning 4180.
737 #endif // _MSC_VER
738 
739  static void Print(const T & value, ::std::ostream * os)
740  {
741  // Prints the address of the value. We use reinterpret_cast here
742  // as static_cast doesn't compile when T is a function type.
743  *os << "@" << reinterpret_cast<const void *>(&value) << " ";
744 
745  // Then prints the value itself.
746  UniversalPrint(value, os);
747  }
748 
749 #ifdef _MSC_VER
750 # pragma warning(pop) // Restores the warning state.
751 #endif // _MSC_VER
752 };
753 
754 // Prints a value tersely: for a reference type, the referenced value
755 // (but not the address) is printed; for a (const) char pointer, the
756 // NUL-terminated string (but not the pointer) is printed.
757 
758 template <typename T>
760 {
761 public:
762  static void Print(const T & value, ::std::ostream * os)
763  {
764  UniversalPrint(value, os);
765  }
766 };
767 template <typename T>
768 class UniversalTersePrinter<T &>
769 {
770 public:
771  static void Print(const T & value, ::std::ostream * os)
772  {
773  UniversalPrint(value, os);
774  }
775 };
776 template <typename T, size_t N>
777 class UniversalTersePrinter<T[N]>
778 {
779 public:
780  static void Print(const T(&value)[N], ::std::ostream * os)
781  {
783  }
784 };
785 template <>
786 class UniversalTersePrinter<const char *>
787 {
788 public:
789  static void Print(const char * str, ::std::ostream * os)
790  {
791  if (str == NULL)
792  {
793  *os << "NULL";
794  }
795 
796  else
797  {
798  UniversalPrint(string(str), os);
799  }
800  }
801 };
802 template <>
803 class UniversalTersePrinter<char *>
804 {
805 public:
806  static void Print(char * str, ::std::ostream * os)
807  {
809  }
810 };
811 
812 #if GTEST_HAS_STD_WSTRING
813 template <>
814 class UniversalTersePrinter<const wchar_t *>
815 {
816 public:
817  static void Print(const wchar_t * str, ::std::ostream * os)
818  {
819  if (str == NULL)
820  {
821  *os << "NULL";
822  }
823 
824  else
825  {
826  UniversalPrint(::std::wstring(str), os);
827  }
828  }
829 };
830 #endif
831 
832 template <>
833 class UniversalTersePrinter<wchar_t *>
834 {
835 public:
836  static void Print(wchar_t * str, ::std::ostream * os)
837  {
839  }
840 };
841 
842 template <typename T>
843 void UniversalTersePrint(const T & value, ::std::ostream * os)
844 {
846 }
847 
848 // Prints a value using the type inferred by the compiler. The
849 // difference between this and UniversalTersePrint() is that for a
850 // (const) char pointer, this prints both the pointer and the
851 // NUL-terminated string.
852 template <typename T>
853 void UniversalPrint(const T & value, ::std::ostream * os)
854 {
855  // A workarond for the bug in VC++ 7.1 that prevents us from instantiating
856  // UniversalPrinter with T directly.
857  typedef T T1;
858  UniversalPrinter<T1>::Print(value, os);
859 }
860 
861 #if GTEST_HAS_TR1_TUPLE
862 typedef ::std::vector<string> Strings;
863 
864 // This helper template allows PrintTo() for tuples and
865 // UniversalTersePrintTupleFieldsToStrings() to be defined by
866 // induction on the number of tuple fields. The idea is that
867 // TuplePrefixPrinter<N>::PrintPrefixTo(t, os) prints the first N
868 // fields in tuple t, and can be defined in terms of
869 // TuplePrefixPrinter<N - 1>.
870 
871 // The inductive case.
872 template <size_t N>
873 struct TuplePrefixPrinter
874 {
875  // Prints the first N fields of a tuple.
876  template <typename Tuple>
877  static void PrintPrefixTo(const Tuple & t, ::std::ostream * os)
878  {
879  TuplePrefixPrinter < N - 1 >::PrintPrefixTo(t, os);
880  *os << ", ";
881  UniversalPrinter < typename ::std::tr1::tuple_element < N - 1, Tuple >::type >
882  ::Print(::std::tr1::get < N - 1 > (t), os);
883  }
884 
885  // Tersely prints the first N fields of a tuple to a string vector,
886  // one element for each field.
887  template <typename Tuple>
888  static void TersePrintPrefixToStrings(const Tuple & t, Strings * strings)
889  {
890  TuplePrefixPrinter < N - 1 >::TersePrintPrefixToStrings(t, strings);
891  ::std::stringstream ss;
892  UniversalTersePrint(::std::tr1::get < N - 1 > (t), &ss);
893  strings->push_back(ss.str());
894  }
895 };
896 
897 // Base cases.
898 template <>
899 struct TuplePrefixPrinter<0>
900 {
901  template <typename Tuple>
902  static void PrintPrefixTo(const Tuple &, ::std::ostream *) {}
903 
904  template <typename Tuple>
905  static void TersePrintPrefixToStrings(const Tuple &, Strings *) {}
906 };
907 // We have to specialize the entire TuplePrefixPrinter<> class
908 // template here, even though the definition of
909 // TersePrintPrefixToStrings() is the same as the generic version, as
910 // Embarcadero (formerly CodeGear, formerly Borland) C++ doesn't
911 // support specializing a method template of a class template.
912 template <>
913 struct TuplePrefixPrinter<1>
914 {
915  template <typename Tuple>
916  static void PrintPrefixTo(const Tuple & t, ::std::ostream * os)
917  {
919  Print(::std::tr1::get<0>(t), os);
920  }
921 
922  template <typename Tuple>
923  static void TersePrintPrefixToStrings(const Tuple & t, Strings * strings)
924  {
925  ::std::stringstream ss;
926  UniversalTersePrint(::std::tr1::get<0>(t), &ss);
927  strings->push_back(ss.str());
928  }
929 };
930 
931 // Helper function for printing a tuple. T must be instantiated with
932 // a tuple type.
933 template <typename T>
934 void PrintTupleTo(const T & t, ::std::ostream * os)
935 {
936  *os << "(";
938  PrintPrefixTo(t, os);
939  *os << ")";
940 }
941 
942 // Prints the fields of a tuple tersely to a string vector, one
943 // element for each field. See the comment before
944 // UniversalTersePrint() for how we define "tersely".
945 template <typename Tuple>
946 Strings UniversalTersePrintTupleFieldsToStrings(const Tuple & value)
947 {
948  Strings result;
950  TersePrintPrefixToStrings(value, &result);
951  return result;
952 }
953 #endif // GTEST_HAS_TR1_TUPLE
954 
955 } // namespace internal
956 
957 template <typename T>
958 ::std::string PrintToString(const T & value)
959 {
960  ::std::stringstream ss;
962  return ss.str();
963 }
964 
965 } // namespace testing
966 
967 #endif // GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
static void Print(const T &value,::std::ostream *os)
bool IsTrue(bool condition)
int * count
void PrintRawArrayTo(const T a[], size_t count,::std::ostream *os)
#define GTEST_API_
static void Print(const T &value,::std::ostream *os)
::std::string PrintToString(const T &value)
static void Print(const T(&value)[N],::std::ostream *os)
void UniversalTersePrint(const T &value,::std::ostream *os)
XmlRpcServer s
void DefaultPrintNonContainerTo(const T &value,::std::ostream *os)
static void Print(const T(&a)[N],::std::ostream *os)
TypeWithSize< 8 >::UInt UInt64
::std::basic_ostream< Char, CharTraits > & operator<<(::std::basic_ostream< Char, CharTraits > &os, const T &x)
static void Print(const T &value,::std::ostream *os)
void PrintStringTo(const ::std::string &s, ostream *os)
bool_constant< true > true_type
static void PrintValue(const T &value,::std::ostream *os)
void PrintBytesInObjectTo(const unsigned char *obj_bytes, size_t count, ostream *os)
void UniversalPrint(const T &value,::std::ostream *os)
Strings UniversalTersePrintTupleFieldsToStrings(const Tuple &value)
void UniversalPrintArray(const char *begin, size_t len, ostream *os)
::std::vector< string > Strings
bool_constant< false > false_type
void PrintTupleTo(const T &t,::std::ostream *os)
void PrintWideStringTo(const ::std::wstring &s, ostream *os)
static void Print(const T &value,::std::ostream *os)
void PrintTo(const ReferenceWrapper< T > &ref,::std::ostream *os)
void DefaultPrintTo(IsContainer, false_type, const C &container,::std::ostream *os)


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:20