106f32e7eSjoerg // Copyright 2005, Google Inc.
206f32e7eSjoerg // All rights reserved.
306f32e7eSjoerg //
406f32e7eSjoerg // Redistribution and use in source and binary forms, with or without
506f32e7eSjoerg // modification, are permitted provided that the following conditions are
606f32e7eSjoerg // met:
706f32e7eSjoerg //
806f32e7eSjoerg //     * Redistributions of source code must retain the above copyright
906f32e7eSjoerg // notice, this list of conditions and the following disclaimer.
1006f32e7eSjoerg //     * Redistributions in binary form must reproduce the above
1106f32e7eSjoerg // copyright notice, this list of conditions and the following disclaimer
1206f32e7eSjoerg // in the documentation and/or other materials provided with the
1306f32e7eSjoerg // distribution.
1406f32e7eSjoerg //     * Neither the name of Google Inc. nor the names of its
1506f32e7eSjoerg // contributors may be used to endorse or promote products derived from
1606f32e7eSjoerg // this software without specific prior written permission.
1706f32e7eSjoerg //
1806f32e7eSjoerg // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1906f32e7eSjoerg // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2006f32e7eSjoerg // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2106f32e7eSjoerg // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2206f32e7eSjoerg // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2306f32e7eSjoerg // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2406f32e7eSjoerg // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2506f32e7eSjoerg // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2606f32e7eSjoerg // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2706f32e7eSjoerg // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2806f32e7eSjoerg // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*da58b97aSjoerg 
3006f32e7eSjoerg //
31*da58b97aSjoerg // The Google C++ Testing and Mocking Framework (Google Test)
3206f32e7eSjoerg //
3306f32e7eSjoerg // This header file defines the Message class.
3406f32e7eSjoerg //
3506f32e7eSjoerg // IMPORTANT NOTE: Due to limitation of the C++ language, we have to
3606f32e7eSjoerg // leave some internal implementation details in this header file.
3706f32e7eSjoerg // They are clearly marked by comments like this:
3806f32e7eSjoerg //
3906f32e7eSjoerg //   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
4006f32e7eSjoerg //
4106f32e7eSjoerg // Such code is NOT meant to be used by a user directly, and is subject
4206f32e7eSjoerg // to CHANGE WITHOUT NOTICE.  Therefore DO NOT DEPEND ON IT in a user
4306f32e7eSjoerg // program!
4406f32e7eSjoerg 
45*da58b97aSjoerg // GOOGLETEST_CM0001 DO NOT DELETE
46*da58b97aSjoerg 
4706f32e7eSjoerg #ifndef GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
4806f32e7eSjoerg #define GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
4906f32e7eSjoerg 
5006f32e7eSjoerg #include <limits>
51*da58b97aSjoerg #include <memory>
5206f32e7eSjoerg 
5306f32e7eSjoerg #include "gtest/internal/gtest-port.h"
5406f32e7eSjoerg #include "gtest/internal/custom/raw-ostream.h"
5506f32e7eSjoerg 
56*da58b97aSjoerg GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
57*da58b97aSjoerg /* class A needs to have dll-interface to be used by clients of class B */)
58*da58b97aSjoerg 
5906f32e7eSjoerg // Ensures that there is at least one operator<< in the global namespace.
6006f32e7eSjoerg // See Message& operator<<(...) below for why.
6106f32e7eSjoerg void operator<<(const testing::internal::Secret&, int);
6206f32e7eSjoerg 
6306f32e7eSjoerg namespace testing {
6406f32e7eSjoerg 
6506f32e7eSjoerg // The Message class works like an ostream repeater.
6606f32e7eSjoerg //
6706f32e7eSjoerg // Typical usage:
6806f32e7eSjoerg //
6906f32e7eSjoerg //   1. You stream a bunch of values to a Message object.
7006f32e7eSjoerg //      It will remember the text in a stringstream.
7106f32e7eSjoerg //   2. Then you stream the Message object to an ostream.
7206f32e7eSjoerg //      This causes the text in the Message to be streamed
7306f32e7eSjoerg //      to the ostream.
7406f32e7eSjoerg //
7506f32e7eSjoerg // For example;
7606f32e7eSjoerg //
7706f32e7eSjoerg //   testing::Message foo;
7806f32e7eSjoerg //   foo << 1 << " != " << 2;
7906f32e7eSjoerg //   std::cout << foo;
8006f32e7eSjoerg //
8106f32e7eSjoerg // will print "1 != 2".
8206f32e7eSjoerg //
8306f32e7eSjoerg // Message is not intended to be inherited from.  In particular, its
8406f32e7eSjoerg // destructor is not virtual.
8506f32e7eSjoerg //
8606f32e7eSjoerg // Note that stringstream behaves differently in gcc and in MSVC.  You
8706f32e7eSjoerg // can stream a NULL char pointer to it in the former, but not in the
8806f32e7eSjoerg // latter (it causes an access violation if you do).  The Message
8906f32e7eSjoerg // class hides this difference by treating a NULL char pointer as
9006f32e7eSjoerg // "(null)".
9106f32e7eSjoerg class GTEST_API_ Message {
9206f32e7eSjoerg  private:
9306f32e7eSjoerg   // The type of basic IO manipulators (endl, ends, and flush) for
9406f32e7eSjoerg   // narrow streams.
9506f32e7eSjoerg   typedef std::ostream& (*BasicNarrowIoManip)(std::ostream&);
9606f32e7eSjoerg 
9706f32e7eSjoerg  public:
9806f32e7eSjoerg   // Constructs an empty Message.
9906f32e7eSjoerg   Message();
10006f32e7eSjoerg 
10106f32e7eSjoerg   // Copy constructor.
Message(const Message & msg)10206f32e7eSjoerg   Message(const Message& msg) : ss_(new ::std::stringstream) {  // NOLINT
10306f32e7eSjoerg     *ss_ << msg.GetString();
10406f32e7eSjoerg   }
10506f32e7eSjoerg 
10606f32e7eSjoerg   // Constructs a Message from a C-string.
Message(const char * str)10706f32e7eSjoerg   explicit Message(const char* str) : ss_(new ::std::stringstream) {
10806f32e7eSjoerg     *ss_ << str;
10906f32e7eSjoerg   }
11006f32e7eSjoerg 
11106f32e7eSjoerg   // Streams a non-pointer value to this object.
11206f32e7eSjoerg   template <typename T>
11306f32e7eSjoerg   inline Message& operator <<(const T& val) {
11406f32e7eSjoerg     // Some libraries overload << for STL containers.  These
11506f32e7eSjoerg     // overloads are defined in the global namespace instead of ::std.
11606f32e7eSjoerg     //
11706f32e7eSjoerg     // C++'s symbol lookup rule (i.e. Koenig lookup) says that these
11806f32e7eSjoerg     // overloads are visible in either the std namespace or the global
11906f32e7eSjoerg     // namespace, but not other namespaces, including the testing
12006f32e7eSjoerg     // namespace which Google Test's Message class is in.
12106f32e7eSjoerg     //
12206f32e7eSjoerg     // To allow STL containers (and other types that has a << operator
12306f32e7eSjoerg     // defined in the global namespace) to be used in Google Test
12406f32e7eSjoerg     // assertions, testing::Message must access the custom << operator
12506f32e7eSjoerg     // from the global namespace.  With this using declaration,
12606f32e7eSjoerg     // overloads of << defined in the global namespace and those
12706f32e7eSjoerg     // visible via Koenig lookup are both exposed in this function.
12806f32e7eSjoerg     using ::operator <<;
12906f32e7eSjoerg     *ss_ << llvm_gtest::printable(val);
13006f32e7eSjoerg     return *this;
13106f32e7eSjoerg   }
13206f32e7eSjoerg 
13306f32e7eSjoerg   // Streams a pointer value to this object.
13406f32e7eSjoerg   //
13506f32e7eSjoerg   // This function is an overload of the previous one.  When you
13606f32e7eSjoerg   // stream a pointer to a Message, this definition will be used as it
13706f32e7eSjoerg   // is more specialized.  (The C++ Standard, section
13806f32e7eSjoerg   // [temp.func.order].)  If you stream a non-pointer, then the
13906f32e7eSjoerg   // previous definition will be used.
14006f32e7eSjoerg   //
14106f32e7eSjoerg   // The reason for this overload is that streaming a NULL pointer to
14206f32e7eSjoerg   // ostream is undefined behavior.  Depending on the compiler, you
14306f32e7eSjoerg   // may get "0", "(nil)", "(null)", or an access violation.  To
14406f32e7eSjoerg   // ensure consistent result across compilers, we always treat NULL
14506f32e7eSjoerg   // as "(null)".
14606f32e7eSjoerg   template <typename T>
14706f32e7eSjoerg   inline Message& operator <<(T* const& pointer) {  // NOLINT
148*da58b97aSjoerg     if (pointer == nullptr) {
14906f32e7eSjoerg       *ss_ << "(null)";
15006f32e7eSjoerg     } else {
15106f32e7eSjoerg       *ss_ << llvm_gtest::printable(pointer);
15206f32e7eSjoerg     }
15306f32e7eSjoerg     return *this;
15406f32e7eSjoerg   }
15506f32e7eSjoerg 
15606f32e7eSjoerg   // Since the basic IO manipulators are overloaded for both narrow
15706f32e7eSjoerg   // and wide streams, we have to provide this specialized definition
15806f32e7eSjoerg   // of operator <<, even though its body is the same as the
15906f32e7eSjoerg   // templatized version above.  Without this definition, streaming
16006f32e7eSjoerg   // endl or other basic IO manipulators to Message will confuse the
16106f32e7eSjoerg   // compiler.
16206f32e7eSjoerg   Message& operator <<(BasicNarrowIoManip val) {
16306f32e7eSjoerg     *ss_ << val;
16406f32e7eSjoerg     return *this;
16506f32e7eSjoerg   }
16606f32e7eSjoerg 
16706f32e7eSjoerg   // Instead of 1/0, we want to see true/false for bool values.
16806f32e7eSjoerg   Message& operator <<(bool b) {
16906f32e7eSjoerg     return *this << (b ? "true" : "false");
17006f32e7eSjoerg   }
17106f32e7eSjoerg 
17206f32e7eSjoerg   // These two overloads allow streaming a wide C string to a Message
17306f32e7eSjoerg   // using the UTF-8 encoding.
17406f32e7eSjoerg   Message& operator <<(const wchar_t* wide_c_str);
17506f32e7eSjoerg   Message& operator <<(wchar_t* wide_c_str);
17606f32e7eSjoerg 
17706f32e7eSjoerg #if GTEST_HAS_STD_WSTRING
17806f32e7eSjoerg   // Converts the given wide string to a narrow string using the UTF-8
17906f32e7eSjoerg   // encoding, and streams the result to this Message object.
18006f32e7eSjoerg   Message& operator <<(const ::std::wstring& wstr);
18106f32e7eSjoerg #endif  // GTEST_HAS_STD_WSTRING
18206f32e7eSjoerg 
18306f32e7eSjoerg   // Gets the text streamed to this object so far as an std::string.
18406f32e7eSjoerg   // Each '\0' character in the buffer is replaced with "\\0".
18506f32e7eSjoerg   //
18606f32e7eSjoerg   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
18706f32e7eSjoerg   std::string GetString() const;
18806f32e7eSjoerg 
18906f32e7eSjoerg  private:
19006f32e7eSjoerg   // We'll hold the text streamed to this object here.
191*da58b97aSjoerg   const std::unique_ptr< ::std::stringstream> ss_;
19206f32e7eSjoerg 
19306f32e7eSjoerg   // We declare (but don't implement) this to prevent the compiler
19406f32e7eSjoerg   // from implementing the assignment operator.
19506f32e7eSjoerg   void operator=(const Message&);
19606f32e7eSjoerg };
19706f32e7eSjoerg 
19806f32e7eSjoerg // Streams a Message to an ostream.
19906f32e7eSjoerg inline std::ostream& operator <<(std::ostream& os, const Message& sb) {
20006f32e7eSjoerg   return os << sb.GetString();
20106f32e7eSjoerg }
20206f32e7eSjoerg 
20306f32e7eSjoerg namespace internal {
20406f32e7eSjoerg 
20506f32e7eSjoerg // Converts a streamable value to an std::string.  A NULL pointer is
20606f32e7eSjoerg // converted to "(null)".  When the input value is a ::string,
20706f32e7eSjoerg // ::std::string, ::wstring, or ::std::wstring object, each NUL
20806f32e7eSjoerg // character in it is replaced with "\\0".
20906f32e7eSjoerg template <typename T>
StreamableToString(const T & streamable)21006f32e7eSjoerg std::string StreamableToString(const T& streamable) {
21106f32e7eSjoerg   return (Message() << streamable).GetString();
21206f32e7eSjoerg }
21306f32e7eSjoerg 
21406f32e7eSjoerg }  // namespace internal
21506f32e7eSjoerg }  // namespace testing
21606f32e7eSjoerg 
217*da58b97aSjoerg GTEST_DISABLE_MSC_WARNINGS_POP_()  //  4251
218*da58b97aSjoerg 
21906f32e7eSjoerg #endif  // GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
220