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-string.h"
00052 #include "gtest/internal/gtest-internal.h"
00053 
00054 namespace testing {
00055 
00056 // The Message class works like an ostream repeater.
00057 //
00058 // Typical usage:
00059 //
00060 //   1. You stream a bunch of values to a Message object.
00061 //      It will remember the text in a stringstream.
00062 //   2. Then you stream the Message object to an ostream.
00063 //      This causes the text in the Message to be streamed
00064 //      to the ostream.
00065 //
00066 // For example;
00067 //
00068 //   testing::Message foo;
00069 //   foo << 1 << " != " << 2;
00070 //   std::cout << foo;
00071 //
00072 // will print "1 != 2".
00073 //
00074 // Message is not intended to be inherited from.  In particular, its
00075 // destructor is not virtual.
00076 //
00077 // Note that stringstream behaves differently in gcc and in MSVC.  You
00078 // can stream a NULL char pointer to it in the former, but not in the
00079 // latter (it causes an access violation if you do).  The Message
00080 // class hides this difference by treating a NULL char pointer as
00081 // "(null)".
00082 class GTEST_API_ Message {
00083  private:
00084   // The type of basic IO manipulators (endl, ends, and flush) for
00085   // narrow streams.
00086   typedef std::ostream& (*BasicNarrowIoManip)(std::ostream&);
00087 
00088  public:
00089   // Constructs an empty Message.
00090   // We allocate the stringstream separately because otherwise each use of
00091   // ASSERT/EXPECT in a procedure adds over 200 bytes to the procedure's
00092   // stack frame leading to huge stack frames in some cases; gcc does not reuse
00093   // the stack space.
00094   Message() : ss_(new ::std::stringstream) {
00095     // By default, we want there to be enough precision when printing
00096     // a double to a Message.
00097     *ss_ << std::setprecision(std::numeric_limits<double>::digits10 + 2);
00098   }
00099 
00100   // Copy constructor.
00101   Message(const Message& msg) : ss_(new ::std::stringstream) {  // NOLINT
00102     *ss_ << msg.GetString();
00103   }
00104 
00105   // Constructs a Message from a C-string.
00106   explicit Message(const char* str) : ss_(new ::std::stringstream) {
00107     *ss_ << str;
00108   }
00109 
00110 #if GTEST_OS_SYMBIAN
00111   // Streams a value (either a pointer or not) to this object.
00112   template <typename T>
00113   inline Message& operator <<(const T& value) {
00114     StreamHelper(typename internal::is_pointer<T>::type(), value);
00115     return *this;
00116   }
00117 #else
00118   // Streams a non-pointer value to this object.
00119   template <typename T>
00120   inline Message& operator <<(const T& val) {
00121     ::GTestStreamToHelper(ss_.get(), val);
00122     return *this;
00123   }
00124 
00125   // Streams a pointer value to this object.
00126   //
00127   // This function is an overload of the previous one.  When you
00128   // stream a pointer to a Message, this definition will be used as it
00129   // is more specialized.  (The C++ Standard, section
00130   // [temp.func.order].)  If you stream a non-pointer, then the
00131   // previous definition will be used.
00132   //
00133   // The reason for this overload is that streaming a NULL pointer to
00134   // ostream is undefined behavior.  Depending on the compiler, you
00135   // may get "0", "(nil)", "(null)", or an access violation.  To
00136   // ensure consistent result across compilers, we always treat NULL
00137   // as "(null)".
00138   template <typename T>
00139   inline Message& operator <<(T* const& pointer) {  // NOLINT
00140     if (pointer == NULL) {
00141       *ss_ << "(null)";
00142     } else {
00143       ::GTestStreamToHelper(ss_.get(), pointer);
00144     }
00145     return *this;
00146   }
00147 #endif  // GTEST_OS_SYMBIAN
00148 
00149   // Since the basic IO manipulators are overloaded for both narrow
00150   // and wide streams, we have to provide this specialized definition
00151   // of operator <<, even though its body is the same as the
00152   // templatized version above.  Without this definition, streaming
00153   // endl or other basic IO manipulators to Message will confuse the
00154   // compiler.
00155   Message& operator <<(BasicNarrowIoManip val) {
00156     *ss_ << val;
00157     return *this;
00158   }
00159 
00160   // Instead of 1/0, we want to see true/false for bool values.
00161   Message& operator <<(bool b) {
00162     return *this << (b ? "true" : "false");
00163   }
00164 
00165   // These two overloads allow streaming a wide C string to a Message
00166   // using the UTF-8 encoding.
00167   Message& operator <<(const wchar_t* wide_c_str) {
00168     return *this << internal::String::ShowWideCString(wide_c_str);
00169   }
00170   Message& operator <<(wchar_t* wide_c_str) {
00171     return *this << internal::String::ShowWideCString(wide_c_str);
00172   }
00173 
00174 #if GTEST_HAS_STD_WSTRING
00175   // Converts the given wide string to a narrow string using the UTF-8
00176   // encoding, and streams the result to this Message object.
00177   Message& operator <<(const ::std::wstring& wstr);
00178 #endif  // GTEST_HAS_STD_WSTRING
00179 
00180 #if GTEST_HAS_GLOBAL_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 ::wstring& wstr);
00184 #endif  // GTEST_HAS_GLOBAL_WSTRING
00185 
00186   // Gets the text streamed to this object so far as a String.
00187   // Each '\0' character in the buffer is replaced with "\\0".
00188   //
00189   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
00190   internal::String GetString() const {
00191     return internal::StringStreamToString(ss_.get());
00192   }
00193 
00194  private:
00195 
00196 #if GTEST_OS_SYMBIAN
00197   // These are needed as the Nokia Symbian Compiler cannot decide between
00198   // const T& and const T* in a function template. The Nokia compiler _can_
00199   // decide between class template specializations for T and T*, so a
00200   // tr1::type_traits-like is_pointer works, and we can overload on that.
00201   template <typename T>
00202   inline void StreamHelper(internal::true_type /*dummy*/, T* pointer) {
00203     if (pointer == NULL) {
00204       *ss_ << "(null)";
00205     } else {
00206       ::GTestStreamToHelper(ss_.get(), pointer);
00207     }
00208   }
00209   template <typename T>
00210   inline void StreamHelper(internal::false_type /*dummy*/, const T& value) {
00211     ::GTestStreamToHelper(ss_.get(), value);
00212   }
00213 #endif  // GTEST_OS_SYMBIAN
00214 
00215   // We'll hold the text streamed to this object here.
00216   const internal::scoped_ptr< ::std::stringstream> ss_;
00217 
00218   // We declare (but don't implement) this to prevent the compiler
00219   // from implementing the assignment operator.
00220   void operator=(const Message&);
00221 };
00222 
00223 // Streams a Message to an ostream.
00224 inline std::ostream& operator <<(std::ostream& os, const Message& sb) {
00225   return os << sb.GetString();
00226 }
00227 
00228 }  // namespace testing
00229 
00230 #endif  // GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:24:39