Qucs-core  0.0.19
gtest-message.h
Go to the documentation of this file.
00001 // Copyright 2005, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 //
00032 // The Google C++ Testing Framework (Google Test)
00033 //
00034 // This header file defines the Message class.
00035 //
00036 // IMPORTANT NOTE: Due to limitation of the C++ language, we have to
00037 // leave some internal implementation details in this header file.
00038 // They are clearly marked by comments like this:
00039 //
00040 //   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
00041 //
00042 // Such code is NOT meant to be used by a user directly, and is subject
00043 // to CHANGE WITHOUT NOTICE.  Therefore DO NOT DEPEND ON IT in a user
00044 // program!
00045 
00046 #ifndef GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
00047 #define GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
00048 
00049 #include <limits>
00050 
00051 #include "gtest/internal/gtest-port.h"
00052 
00053 // Ensures that there is at least one operator<< in the global namespace.
00054 // See Message& operator<<(...) below for why.
00055 void operator<<(const testing::internal::Secret&, int);
00056 
00057 namespace testing {
00058 
00059 // The Message class works like an ostream repeater.
00060 //
00061 // Typical usage:
00062 //
00063 //   1. You stream a bunch of values to a Message object.
00064 //      It will remember the text in a stringstream.
00065 //   2. Then you stream the Message object to an ostream.
00066 //      This causes the text in the Message to be streamed
00067 //      to the ostream.
00068 //
00069 // For example;
00070 //
00071 //   testing::Message foo;
00072 //   foo << 1 << " != " << 2;
00073 //   std::cout << foo;
00074 //
00075 // will print "1 != 2".
00076 //
00077 // Message is not intended to be inherited from.  In particular, its
00078 // destructor is not virtual.
00079 //
00080 // Note that stringstream behaves differently in gcc and in MSVC.  You
00081 // can stream a NULL char pointer to it in the former, but not in the
00082 // latter (it causes an access violation if you do).  The Message
00083 // class hides this difference by treating a NULL char pointer as
00084 // "(null)".
00085 class GTEST_API_ Message {
00086  private:
00087   // The type of basic IO manipulators (endl, ends, and flush) for
00088   // narrow streams.
00089   typedef std::ostream& (*BasicNarrowIoManip)(std::ostream&);
00090 
00091  public:
00092   // Constructs an empty Message.
00093   Message();
00094 
00095   // Copy constructor.
00096   Message(const Message& msg) : ss_(new ::std::stringstream) {  // NOLINT
00097     *ss_ << msg.GetString();
00098   }
00099 
00100   // Constructs a Message from a C-string.
00101   explicit Message(const char* str) : ss_(new ::std::stringstream) {
00102     *ss_ << str;
00103   }
00104 
00105 #if GTEST_OS_SYMBIAN
00106   // Streams a value (either a pointer or not) to this object.
00107   template <typename T>
00108   inline Message& operator <<(const T& value) {
00109     StreamHelper(typename internal::is_pointer<T>::type(), value);
00110     return *this;
00111   }
00112 #else
00113   // Streams a non-pointer value to this object.
00114   template <typename T>
00115   inline Message& operator <<(const T& val) {
00116     // Some libraries overload << for STL containers.  These
00117     // overloads are defined in the global namespace instead of ::std.
00118     //
00119     // C++'s symbol lookup rule (i.e. Koenig lookup) says that these
00120     // overloads are visible in either the std namespace or the global
00121     // namespace, but not other namespaces, including the testing
00122     // namespace which Google Test's Message class is in.
00123     //
00124     // To allow STL containers (and other types that has a << operator
00125     // defined in the global namespace) to be used in Google Test
00126     // assertions, testing::Message must access the custom << operator
00127     // from the global namespace.  With this using declaration,
00128     // overloads of << defined in the global namespace and those
00129     // visible via Koenig lookup are both exposed in this function.
00130     using ::operator <<;
00131     *ss_ << val;
00132     return *this;
00133   }
00134 
00135   // Streams a pointer value to this object.
00136   //
00137   // This function is an overload of the previous one.  When you
00138   // stream a pointer to a Message, this definition will be used as it
00139   // is more specialized.  (The C++ Standard, section
00140   // [temp.func.order].)  If you stream a non-pointer, then the
00141   // previous definition will be used.
00142   //
00143   // The reason for this overload is that streaming a NULL pointer to
00144   // ostream is undefined behavior.  Depending on the compiler, you
00145   // may get "0", "(nil)", "(null)", or an access violation.  To
00146   // ensure consistent result across compilers, we always treat NULL
00147   // as "(null)".
00148   template <typename T>
00149   inline Message& operator <<(T* const& pointer) {  // NOLINT
00150     if (pointer == NULL) {
00151       *ss_ << "(null)";
00152     } else {
00153       *ss_ << pointer;
00154     }
00155     return *this;
00156   }
00157 #endif  // GTEST_OS_SYMBIAN
00158 
00159   // Since the basic IO manipulators are overloaded for both narrow
00160   // and wide streams, we have to provide this specialized definition
00161   // of operator <<, even though its body is the same as the
00162   // templatized version above.  Without this definition, streaming
00163   // endl or other basic IO manipulators to Message will confuse the
00164   // compiler.
00165   Message& operator <<(BasicNarrowIoManip val) {
00166     *ss_ << val;
00167     return *this;
00168   }
00169 
00170   // Instead of 1/0, we want to see true/false for bool values.
00171   Message& operator <<(bool b) {
00172     return *this << (b ? "true" : "false");
00173   }
00174 
00175   // These two overloads allow streaming a wide C string to a Message
00176   // using the UTF-8 encoding.
00177   Message& operator <<(const wchar_t* wide_c_str);
00178   Message& operator <<(wchar_t* wide_c_str);
00179 
00180 #if GTEST_HAS_STD_WSTRING
00181   // Converts the given wide string to a narrow string using the UTF-8
00182   // encoding, and streams the result to this Message object.
00183   Message& operator <<(const ::std::wstring& wstr);
00184 #endif  // GTEST_HAS_STD_WSTRING
00185 
00186 #if GTEST_HAS_GLOBAL_WSTRING
00187   // Converts the given wide string to a narrow string using the UTF-8
00188   // encoding, and streams the result to this Message object.
00189   Message& operator <<(const ::wstring& wstr);
00190 #endif  // GTEST_HAS_GLOBAL_WSTRING
00191 
00192   // Gets the text streamed to this object so far as an std::string.
00193   // Each '\0' character in the buffer is replaced with "\\0".
00194   //
00195   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
00196   std::string GetString() const;
00197 
00198  private:
00199 
00200 #if GTEST_OS_SYMBIAN
00201   // These are needed as the Nokia Symbian Compiler cannot decide between
00202   // const T& and const T* in a function template. The Nokia compiler _can_
00203   // decide between class template specializations for T and T*, so a
00204   // tr1::type_traits-like is_pointer works, and we can overload on that.
00205   template <typename T>
00206   inline void StreamHelper(internal::true_type /*is_pointer*/, T* pointer) {
00207     if (pointer == NULL) {
00208       *ss_ << "(null)";
00209     } else {
00210       *ss_ << pointer;
00211     }
00212   }
00213   template <typename T>
00214   inline void StreamHelper(internal::false_type /*is_pointer*/,
00215                            const T& value) {
00216     // See the comments in Message& operator <<(const T&) above for why
00217     // we need this using statement.
00218     using ::operator <<;
00219     *ss_ << value;
00220   }
00221 #endif  // GTEST_OS_SYMBIAN
00222 
00223   // We'll hold the text streamed to this object here.
00224   const internal::scoped_ptr< ::std::stringstream> ss_;
00225 
00226   // We declare (but don't implement) this to prevent the compiler
00227   // from implementing the assignment operator.
00228   void operator=(const Message&);
00229 };
00230 
00231 // Streams a Message to an ostream.
00232 inline std::ostream& operator <<(std::ostream& os, const Message& sb) {
00233   return os << sb.GetString();
00234 }
00235 
00236 namespace internal {
00237 
00238 // Converts a streamable value to an std::string.  A NULL pointer is
00239 // converted to "(null)".  When the input value is a ::string,
00240 // ::std::string, ::wstring, or ::std::wstring object, each NUL
00241 // character in it is replaced with "\\0".
00242 template <typename T>
00243 std::string StreamableToString(const T& streamable) {
00244   return (Message() << streamable).GetString();
00245 }
00246 
00247 }  // namespace internal
00248 }  // namespace testing
00249 
00250 #endif  // GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_