/thirdparty/breakpad/third_party/protobuf/protobuf/gtest/include/gtest/gtest-message.h

http://github.com/tomahawk-player/tomahawk · C++ Header · 230 lines · 87 code · 22 blank · 121 comment · 6 complexity · b17f9122b6494746b1b0ab04a4eb9d11 MD5 · raw file

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