Blame googletest/include/gtest/gtest-message.h

Packit bd1cd8
// Copyright 2005, Google Inc.
Packit bd1cd8
// All rights reserved.
Packit bd1cd8
//
Packit bd1cd8
// Redistribution and use in source and binary forms, with or without
Packit bd1cd8
// modification, are permitted provided that the following conditions are
Packit bd1cd8
// met:
Packit bd1cd8
//
Packit bd1cd8
//     * Redistributions of source code must retain the above copyright
Packit bd1cd8
// notice, this list of conditions and the following disclaimer.
Packit bd1cd8
//     * Redistributions in binary form must reproduce the above
Packit bd1cd8
// copyright notice, this list of conditions and the following disclaimer
Packit bd1cd8
// in the documentation and/or other materials provided with the
Packit bd1cd8
// distribution.
Packit bd1cd8
//     * Neither the name of Google Inc. nor the names of its
Packit bd1cd8
// contributors may be used to endorse or promote products derived from
Packit bd1cd8
// this software without specific prior written permission.
Packit bd1cd8
//
Packit bd1cd8
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit bd1cd8
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit bd1cd8
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit bd1cd8
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit bd1cd8
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit bd1cd8
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit bd1cd8
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit bd1cd8
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit bd1cd8
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit bd1cd8
//
Packit bd1cd8
// Author: wan@google.com (Zhanyong Wan)
Packit bd1cd8
//
Packit bd1cd8
// The Google C++ Testing Framework (Google Test)
Packit bd1cd8
//
Packit bd1cd8
// This header file defines the Message class.
Packit bd1cd8
//
Packit bd1cd8
// IMPORTANT NOTE: Due to limitation of the C++ language, we have to
Packit bd1cd8
// leave some internal implementation details in this header file.
Packit bd1cd8
// They are clearly marked by comments like this:
Packit bd1cd8
//
Packit bd1cd8
//   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
Packit bd1cd8
//
Packit bd1cd8
// Such code is NOT meant to be used by a user directly, and is subject
Packit bd1cd8
// to CHANGE WITHOUT NOTICE.  Therefore DO NOT DEPEND ON IT in a user
Packit bd1cd8
// program!
Packit bd1cd8
Packit bd1cd8
#ifndef GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
Packit bd1cd8
#define GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
Packit bd1cd8
Packit bd1cd8
#include <limits>
Packit bd1cd8
Packit bd1cd8
#include "gtest/internal/gtest-port.h"
Packit bd1cd8
Packit bd1cd8
// Ensures that there is at least one operator<< in the global namespace.
Packit bd1cd8
// See Message& operator<<(...) below for why.
Packit bd1cd8
void operator<<(const testing::internal::Secret&, int);
Packit bd1cd8
Packit bd1cd8
namespace testing {
Packit bd1cd8
Packit bd1cd8
// The Message class works like an ostream repeater.
Packit bd1cd8
//
Packit bd1cd8
// Typical usage:
Packit bd1cd8
//
Packit bd1cd8
//   1. You stream a bunch of values to a Message object.
Packit bd1cd8
//      It will remember the text in a stringstream.
Packit bd1cd8
//   2. Then you stream the Message object to an ostream.
Packit bd1cd8
//      This causes the text in the Message to be streamed
Packit bd1cd8
//      to the ostream.
Packit bd1cd8
//
Packit bd1cd8
// For example;
Packit bd1cd8
//
Packit bd1cd8
//   testing::Message foo;
Packit bd1cd8
//   foo << 1 << " != " << 2;
Packit bd1cd8
//   std::cout << foo;
Packit bd1cd8
//
Packit bd1cd8
// will print "1 != 2".
Packit bd1cd8
//
Packit bd1cd8
// Message is not intended to be inherited from.  In particular, its
Packit bd1cd8
// destructor is not virtual.
Packit bd1cd8
//
Packit bd1cd8
// Note that stringstream behaves differently in gcc and in MSVC.  You
Packit bd1cd8
// can stream a NULL char pointer to it in the former, but not in the
Packit bd1cd8
// latter (it causes an access violation if you do).  The Message
Packit bd1cd8
// class hides this difference by treating a NULL char pointer as
Packit bd1cd8
// "(null)".
Packit bd1cd8
class GTEST_API_ Message {
Packit bd1cd8
 private:
Packit bd1cd8
  // The type of basic IO manipulators (endl, ends, and flush) for
Packit bd1cd8
  // narrow streams.
Packit bd1cd8
  typedef std::ostream& (*BasicNarrowIoManip)(std::ostream&);
Packit bd1cd8
Packit bd1cd8
 public:
Packit bd1cd8
  // Constructs an empty Message.
Packit bd1cd8
  Message();
Packit bd1cd8
Packit bd1cd8
  // Copy constructor.
Packit bd1cd8
  Message(const Message& msg) : ss_(new ::std::stringstream) {  // NOLINT
Packit bd1cd8
    *ss_ << msg.GetString();
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Constructs a Message from a C-string.
Packit bd1cd8
  explicit Message(const char* str) : ss_(new ::std::stringstream) {
Packit bd1cd8
    *ss_ << str;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
#if GTEST_OS_SYMBIAN
Packit bd1cd8
  // Streams a value (either a pointer or not) to this object.
Packit bd1cd8
  template <typename T>
Packit bd1cd8
  inline Message& operator <<(const T& value) {
Packit bd1cd8
    StreamHelper(typename internal::is_pointer<T>::type(), value);
Packit bd1cd8
    return *this;
Packit bd1cd8
  }
Packit bd1cd8
#else
Packit bd1cd8
  // Streams a non-pointer value to this object.
Packit bd1cd8
  template <typename T>
Packit bd1cd8
  inline Message& operator <<(const T& val) {
Packit bd1cd8
    // Some libraries overload << for STL containers.  These
Packit bd1cd8
    // overloads are defined in the global namespace instead of ::std.
Packit bd1cd8
    //
Packit bd1cd8
    // C++'s symbol lookup rule (i.e. Koenig lookup) says that these
Packit bd1cd8
    // overloads are visible in either the std namespace or the global
Packit bd1cd8
    // namespace, but not other namespaces, including the testing
Packit bd1cd8
    // namespace which Google Test's Message class is in.
Packit bd1cd8
    //
Packit bd1cd8
    // To allow STL containers (and other types that has a << operator
Packit bd1cd8
    // defined in the global namespace) to be used in Google Test
Packit bd1cd8
    // assertions, testing::Message must access the custom << operator
Packit bd1cd8
    // from the global namespace.  With this using declaration,
Packit bd1cd8
    // overloads of << defined in the global namespace and those
Packit bd1cd8
    // visible via Koenig lookup are both exposed in this function.
Packit bd1cd8
    using ::operator <<;
Packit bd1cd8
    *ss_ << val;
Packit bd1cd8
    return *this;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Streams a pointer value to this object.
Packit bd1cd8
  //
Packit bd1cd8
  // This function is an overload of the previous one.  When you
Packit bd1cd8
  // stream a pointer to a Message, this definition will be used as it
Packit bd1cd8
  // is more specialized.  (The C++ Standard, section
Packit bd1cd8
  // [temp.func.order].)  If you stream a non-pointer, then the
Packit bd1cd8
  // previous definition will be used.
Packit bd1cd8
  //
Packit bd1cd8
  // The reason for this overload is that streaming a NULL pointer to
Packit bd1cd8
  // ostream is undefined behavior.  Depending on the compiler, you
Packit bd1cd8
  // may get "0", "(nil)", "(null)", or an access violation.  To
Packit bd1cd8
  // ensure consistent result across compilers, we always treat NULL
Packit bd1cd8
  // as "(null)".
Packit bd1cd8
  template <typename T>
Packit bd1cd8
  inline Message& operator <<(T* const& pointer) {  // NOLINT
Packit bd1cd8
    if (pointer == NULL) {
Packit bd1cd8
      *ss_ << "(null)";
Packit bd1cd8
    } else {
Packit bd1cd8
      *ss_ << pointer;
Packit bd1cd8
    }
Packit bd1cd8
    return *this;
Packit bd1cd8
  }
Packit bd1cd8
#endif  // GTEST_OS_SYMBIAN
Packit bd1cd8
Packit bd1cd8
  // Since the basic IO manipulators are overloaded for both narrow
Packit bd1cd8
  // and wide streams, we have to provide this specialized definition
Packit bd1cd8
  // of operator <<, even though its body is the same as the
Packit bd1cd8
  // templatized version above.  Without this definition, streaming
Packit bd1cd8
  // endl or other basic IO manipulators to Message will confuse the
Packit bd1cd8
  // compiler.
Packit bd1cd8
  Message& operator <<(BasicNarrowIoManip val) {
Packit bd1cd8
    *ss_ << val;
Packit bd1cd8
    return *this;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Instead of 1/0, we want to see true/false for bool values.
Packit bd1cd8
  Message& operator <<(bool b) {
Packit bd1cd8
    return *this << (b ? "true" : "false");
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // These two overloads allow streaming a wide C string to a Message
Packit bd1cd8
  // using the UTF-8 encoding.
Packit bd1cd8
  Message& operator <<(const wchar_t* wide_c_str);
Packit bd1cd8
  Message& operator <<(wchar_t* wide_c_str);
Packit bd1cd8
Packit bd1cd8
#if GTEST_HAS_STD_WSTRING
Packit bd1cd8
  // Converts the given wide string to a narrow string using the UTF-8
Packit bd1cd8
  // encoding, and streams the result to this Message object.
Packit bd1cd8
  Message& operator <<(const ::std::wstring& wstr);
Packit bd1cd8
#endif  // GTEST_HAS_STD_WSTRING
Packit bd1cd8
Packit bd1cd8
#if GTEST_HAS_GLOBAL_WSTRING
Packit bd1cd8
  // Converts the given wide string to a narrow string using the UTF-8
Packit bd1cd8
  // encoding, and streams the result to this Message object.
Packit bd1cd8
  Message& operator <<(const ::wstring& wstr);
Packit bd1cd8
#endif  // GTEST_HAS_GLOBAL_WSTRING
Packit bd1cd8
Packit bd1cd8
  // Gets the text streamed to this object so far as an std::string.
Packit bd1cd8
  // Each '\0' character in the buffer is replaced with "\\0".
Packit bd1cd8
  //
Packit bd1cd8
  // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
Packit bd1cd8
  std::string GetString() const;
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
Packit bd1cd8
#if GTEST_OS_SYMBIAN
Packit bd1cd8
  // These are needed as the Nokia Symbian Compiler cannot decide between
Packit bd1cd8
  // const T& and const T* in a function template. The Nokia compiler _can_
Packit bd1cd8
  // decide between class template specializations for T and T*, so a
Packit bd1cd8
  // tr1::type_traits-like is_pointer works, and we can overload on that.
Packit bd1cd8
  template <typename T>
Packit bd1cd8
  inline void StreamHelper(internal::true_type /*is_pointer*/, T* pointer) {
Packit bd1cd8
    if (pointer == NULL) {
Packit bd1cd8
      *ss_ << "(null)";
Packit bd1cd8
    } else {
Packit bd1cd8
      *ss_ << pointer;
Packit bd1cd8
    }
Packit bd1cd8
  }
Packit bd1cd8
  template <typename T>
Packit bd1cd8
  inline void StreamHelper(internal::false_type /*is_pointer*/,
Packit bd1cd8
                           const T& value) {
Packit bd1cd8
    // See the comments in Message& operator <<(const T&) above for why
Packit bd1cd8
    // we need this using statement.
Packit bd1cd8
    using ::operator <<;
Packit bd1cd8
    *ss_ << value;
Packit bd1cd8
  }
Packit bd1cd8
#endif  // GTEST_OS_SYMBIAN
Packit bd1cd8
Packit bd1cd8
  // We'll hold the text streamed to this object here.
Packit bd1cd8
  const internal::scoped_ptr< ::std::stringstream> ss_;
Packit bd1cd8
Packit bd1cd8
  // We declare (but don't implement) this to prevent the compiler
Packit bd1cd8
  // from implementing the assignment operator.
Packit bd1cd8
  void operator=(const Message&);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Streams a Message to an ostream.
Packit bd1cd8
inline std::ostream& operator <<(std::ostream& os, const Message& sb) {
Packit bd1cd8
  return os << sb.GetString();
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
namespace internal {
Packit bd1cd8
Packit bd1cd8
// Converts a streamable value to an std::string.  A NULL pointer is
Packit bd1cd8
// converted to "(null)".  When the input value is a ::string,
Packit bd1cd8
// ::std::string, ::wstring, or ::std::wstring object, each NUL
Packit bd1cd8
// character in it is replaced with "\\0".
Packit bd1cd8
template <typename T>
Packit bd1cd8
std::string StreamableToString(const T& streamable) {
Packit bd1cd8
  return (Message() << streamable).GetString();
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
}  // namespace internal
Packit bd1cd8
}  // namespace testing
Packit bd1cd8
Packit bd1cd8
#endif  // GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_