gtest-message.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright 2005, 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. // The Google C++ Testing Framework (Google Test)
  33. //
  34. // This header file defines the Message class.
  35. //
  36. // IMPORTANT NOTE: Due to limitation of the C++ language, we have to
  37. // leave some internal implementation details in this header file.
  38. // They are clearly marked by comments like this:
  39. //
  40. // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
  41. //
  42. // Such code is NOT meant to be used by a user directly, and is subject
  43. // to CHANGE WITHOUT NOTICE. Therefore DO NOT DEPEND ON IT in a user
  44. // program!
  45. #ifndef GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
  46. #define GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
  47. #include <gtest/internal/gtest-string.h>
  48. #include <gtest/internal/gtest-internal.h>
  49. namespace testing {
  50. // The Message class works like an ostream repeater.
  51. //
  52. // Typical usage:
  53. //
  54. // 1. You stream a bunch of values to a Message object.
  55. // It will remember the text in a StrStream.
  56. // 2. Then you stream the Message object to an ostream.
  57. // This causes the text in the Message to be streamed
  58. // to the ostream.
  59. //
  60. // For example;
  61. //
  62. // testing::Message foo;
  63. // foo << 1 << " != " << 2;
  64. // std::cout << foo;
  65. //
  66. // will print "1 != 2".
  67. //
  68. // Message is not intended to be inherited from. In particular, its
  69. // destructor is not virtual.
  70. //
  71. // Note that StrStream behaves differently in gcc and in MSVC. You
  72. // can stream a NULL char pointer to it in the former, but not in the
  73. // latter (it causes an access violation if you do). The Message
  74. // class hides this difference by treating a NULL char pointer as
  75. // "(null)".
  76. class Message {
  77. private:
  78. // The type of basic IO manipulators (endl, ends, and flush) for
  79. // narrow streams.
  80. typedef std::ostream& (*BasicNarrowIoManip)(std::ostream&);
  81. public:
  82. // Constructs an empty Message.
  83. // We allocate the StrStream separately because it otherwise each use of
  84. // ASSERT/EXPECT in a procedure adds over 200 bytes to the procedure's
  85. // stack frame leading to huge stack frames in some cases; gcc does not reuse
  86. // the stack space.
  87. Message() : ss_(new internal::StrStream) {}
  88. // Copy constructor.
  89. Message(const Message& msg) : ss_(new internal::StrStream) { // NOLINT
  90. *ss_ << msg.GetString();
  91. }
  92. // Constructs a Message from a C-string.
  93. explicit Message(const char* str) : ss_(new internal::StrStream) {
  94. *ss_ << str;
  95. }
  96. ~Message() { delete ss_; }
  97. #if GTEST_OS_SYMBIAN
  98. // Streams a value (either a pointer or not) to this object.
  99. template <typename T>
  100. inline Message& operator <<(const T& value) {
  101. StreamHelper(typename internal::is_pointer<T>::type(), value);
  102. return *this;
  103. }
  104. #else
  105. // Streams a non-pointer value to this object.
  106. template <typename T>
  107. inline Message& operator <<(const T& val) {
  108. ::GTestStreamToHelper(ss_, val);
  109. return *this;
  110. }
  111. // Streams a pointer value to this object.
  112. //
  113. // This function is an overload of the previous one. When you
  114. // stream a pointer to a Message, this definition will be used as it
  115. // is more specialized. (The C++ Standard, section
  116. // [temp.func.order].) If you stream a non-pointer, then the
  117. // previous definition will be used.
  118. //
  119. // The reason for this overload is that streaming a NULL pointer to
  120. // ostream is undefined behavior. Depending on the compiler, you
  121. // may get "0", "(nil)", "(null)", or an access violation. To
  122. // ensure consistent result across compilers, we always treat NULL
  123. // as "(null)".
  124. template <typename T>
  125. inline Message& operator <<(T* const& pointer) { // NOLINT
  126. if (pointer == NULL) {
  127. *ss_ << "(null)";
  128. } else {
  129. ::GTestStreamToHelper(ss_, pointer);
  130. }
  131. return *this;
  132. }
  133. #endif // GTEST_OS_SYMBIAN
  134. // Since the basic IO manipulators are overloaded for both narrow
  135. // and wide streams, we have to provide this specialized definition
  136. // of operator <<, even though its body is the same as the
  137. // templatized version above. Without this definition, streaming
  138. // endl or other basic IO manipulators to Message will confuse the
  139. // compiler.
  140. Message& operator <<(BasicNarrowIoManip val) {
  141. *ss_ << val;
  142. return *this;
  143. }
  144. // Instead of 1/0, we want to see true/false for bool values.
  145. Message& operator <<(bool b) {
  146. return *this << (b ? "true" : "false");
  147. }
  148. // These two overloads allow streaming a wide C string to a Message
  149. // using the UTF-8 encoding.
  150. Message& operator <<(const wchar_t* wide_c_str) {
  151. return *this << internal::String::ShowWideCString(wide_c_str);
  152. }
  153. Message& operator <<(wchar_t* wide_c_str) {
  154. return *this << internal::String::ShowWideCString(wide_c_str);
  155. }
  156. #if GTEST_HAS_STD_WSTRING
  157. // Converts the given wide string to a narrow string using the UTF-8
  158. // encoding, and streams the result to this Message object.
  159. Message& operator <<(const ::std::wstring& wstr);
  160. #endif // GTEST_HAS_STD_WSTRING
  161. #if GTEST_HAS_GLOBAL_WSTRING
  162. // Converts the given wide string to a narrow string using the UTF-8
  163. // encoding, and streams the result to this Message object.
  164. Message& operator <<(const ::wstring& wstr);
  165. #endif // GTEST_HAS_GLOBAL_WSTRING
  166. // Gets the text streamed to this object so far as a String.
  167. // Each '\0' character in the buffer is replaced with "\\0".
  168. //
  169. // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
  170. internal::String GetString() const {
  171. return internal::StrStreamToString(ss_);
  172. }
  173. private:
  174. #if GTEST_OS_SYMBIAN
  175. // These are needed as the Nokia Symbian Compiler cannot decide between
  176. // const T& and const T* in a function template. The Nokia compiler _can_
  177. // decide between class template specializations for T and T*, so a
  178. // tr1::type_traits-like is_pointer works, and we can overload on that.
  179. template <typename T>
  180. inline void StreamHelper(internal::true_type dummy, T* pointer) {
  181. if (pointer == NULL) {
  182. *ss_ << "(null)";
  183. } else {
  184. ::GTestStreamToHelper(ss_, pointer);
  185. }
  186. }
  187. template <typename T>
  188. inline void StreamHelper(internal::false_type dummy, const T& value) {
  189. ::GTestStreamToHelper(ss_, value);
  190. }
  191. #endif // GTEST_OS_SYMBIAN
  192. // We'll hold the text streamed to this object here.
  193. internal::StrStream* const ss_;
  194. // We declare (but don't implement) this to prevent the compiler
  195. // from implementing the assignment operator.
  196. void operator=(const Message&);
  197. };
  198. // Streams a Message to an ostream.
  199. inline std::ostream& operator <<(std::ostream& os, const Message& sb) {
  200. return os << sb.GetString();
  201. }
  202. } // namespace testing
  203. #endif // GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_