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