Home | History | Annotate | Download | only in gtest
      1 // Copyright 2005, Google Inc.
      2 // All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are
      6 // met:
      7 //
      8 //     * Redistributions of source code must retain the above copyright
      9 // notice, this list of conditions and the following disclaimer.
     10 //     * Redistributions in binary form must reproduce the above
     11 // copyright notice, this list of conditions and the following disclaimer
     12 // in the documentation and/or other materials provided with the
     13 // distribution.
     14 //     * Neither the name of Google Inc. nor the names of its
     15 // contributors may be used to endorse or promote products derived from
     16 // this software without specific prior written permission.
     17 //
     18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29 //
     30 // Author: wan (at) google.com (Zhanyong Wan)
     31 //
     32 // The Google C++ Testing Framework (Google Test)
     33 //
     34 // This header file defines the Message class.
     35 //
     36 // IMPORTANT NOTE: Due to limitation of the C++ language, we have to
     37 // leave some internal implementation details in this header file.
     38 // They are clearly marked by comments like this:
     39 //
     40 //   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
     41 //
     42 // Such code is NOT meant to be used by a user directly, and is subject
     43 // to CHANGE WITHOUT NOTICE.  Therefore DO NOT DEPEND ON IT in a user
     44 // program!
     45 
     46 #ifndef GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
     47 #define GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
     48 
     49 #include <limits>
     50 
     51 #include <gtest/internal/gtest-string.h>
     52 #include <gtest/internal/gtest-internal.h>
     53 
     54 namespace testing {
     55 
     56 // The Message class works like an ostream repeater.
     57 //
     58 // Typical usage:
     59 //
     60 //   1. You stream a bunch of values to a Message object.
     61 //      It will remember the text in a StrStream.
     62 //   2. Then you stream the Message object to an ostream.
     63 //      This causes the text in the Message to be streamed
     64 //      to the ostream.
     65 //
     66 // For example;
     67 //
     68 //   testing::Message foo;
     69 //   foo << 1 << " != " << 2;
     70 //   std::cout << foo;
     71 //
     72 // will print "1 != 2".
     73 //
     74 // Message is not intended to be inherited from.  In particular, its
     75 // destructor is not virtual.
     76 //
     77 // Note that StrStream behaves differently in gcc and in MSVC.  You
     78 // can stream a NULL char pointer to it in the former, but not in the
     79 // latter (it causes an access violation if you do).  The Message
     80 // class hides this difference by treating a NULL char pointer as
     81 // "(null)".
     82 class GTEST_API_ Message {
     83  private:
     84   // The type of basic IO manipulators (endl, ends, and flush) for
     85   // narrow streams.
     86   typedef std::ostream& (*BasicNarrowIoManip)(std::ostream&);
     87 
     88  public:
     89   // Constructs an empty Message.
     90   // We allocate the StrStream separately because it otherwise each use of
     91   // ASSERT/EXPECT in a procedure adds over 200 bytes to the procedure's
     92   // stack frame leading to huge stack frames in some cases; gcc does not reuse
     93   // the stack space.
     94   Message() : ss_(new internal::StrStream) {
     95     // By default, we want there to be enough precision when printing
     96     // a double to a Message.
     97     *ss_ << std::setprecision(std::numeric_limits<double>::digits10 + 2);
     98   }
     99 
    100   // Copy constructor.
    101   Message(const Message& msg) : ss_(new internal::StrStream) {  // NOLINT
    102     *ss_ << msg.GetString();
    103   }
    104 
    105   // Constructs a Message from a C-string.
    106   explicit Message(const char* str) : ss_(new internal::StrStream) {
    107     *ss_ << str;
    108   }
    109 
    110   ~Message() { delete ss_; }
    111 #if GTEST_OS_SYMBIAN
    112   // Streams a value (either a pointer or not) to this object.
    113   template <typename T>
    114   inline Message& operator <<(const T& value) {
    115     StreamHelper(typename internal::is_pointer<T>::type(), value);
    116     return *this;
    117   }
    118 #else
    119   // Streams a non-pointer value to this object.
    120   template <typename T>
    121   inline Message& operator <<(const T& val) {
    122     ::GTestStreamToHelper(ss_, val);
    123     return *this;
    124   }
    125 
    126   // Streams a pointer value to this object.
    127   //
    128   // This function is an overload of the previous one.  When you
    129   // stream a pointer to a Message, this definition will be used as it
    130   // is more specialized.  (The C++ Standard, section
    131   // [temp.func.order].)  If you stream a non-pointer, then the
    132   // previous definition will be used.
    133   //
    134   // The reason for this overload is that streaming a NULL pointer to
    135   // ostream is undefined behavior.  Depending on the compiler, you
    136   // may get "0", "(nil)", "(null)", or an access violation.  To
    137   // ensure consistent result across compilers, we always treat NULL
    138   // as "(null)".
    139   template <typename T>
    140   inline Message& operator <<(T* const& pointer) {  // NOLINT
    141     if (pointer == NULL) {
    142       *ss_ << "(null)";
    143     } else {
    144       ::GTestStreamToHelper(ss_, pointer);
    145     }
    146     return *this;
    147   }
    148 #endif  // GTEST_OS_SYMBIAN
    149 
    150   // Since the basic IO manipulators are overloaded for both narrow
    151   // and wide streams, we have to provide this specialized definition
    152   // of operator <<, even though its body is the same as the
    153   // templatized version above.  Without this definition, streaming
    154   // endl or other basic IO manipulators to Message will confuse the
    155   // compiler.
    156   Message& operator <<(BasicNarrowIoManip val) {
    157     *ss_ << val;
    158     return *this;
    159   }
    160 
    161   // Instead of 1/0, we want to see true/false for bool values.
    162   Message& operator <<(bool b) {
    163     return *this << (b ? "true" : "false");
    164   }
    165 
    166   // These two overloads allow streaming a wide C string to a Message
    167   // using the UTF-8 encoding.
    168   Message& operator <<(const wchar_t* wide_c_str) {
    169     return *this << internal::String::ShowWideCString(wide_c_str);
    170   }
    171   Message& operator <<(wchar_t* wide_c_str) {
    172     return *this << internal::String::ShowWideCString(wide_c_str);
    173   }
    174 
    175 #if GTEST_HAS_STD_WSTRING
    176   // Converts the given wide string to a narrow string using the UTF-8
    177   // encoding, and streams the result to this Message object.
    178   Message& operator <<(const ::std::wstring& wstr);
    179 #endif  // GTEST_HAS_STD_WSTRING
    180 
    181 #if GTEST_HAS_GLOBAL_WSTRING
    182   // Converts the given wide string to a narrow string using the UTF-8
    183   // encoding, and streams the result to this Message object.
    184   Message& operator <<(const ::wstring& wstr);
    185 #endif  // GTEST_HAS_GLOBAL_WSTRING
    186 
    187   // Gets the text streamed to this object so far as a String.
    188   // Each '\0' character in the buffer is replaced with "\\0".
    189   //
    190   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
    191   internal::String GetString() const {
    192     return internal::StrStreamToString(ss_);
    193   }
    194 
    195  private:
    196 #if GTEST_OS_SYMBIAN
    197   // These are needed as the Nokia Symbian Compiler cannot decide between
    198   // const T& and const T* in a function template. The Nokia compiler _can_
    199   // decide between class template specializations for T and T*, so a
    200   // tr1::type_traits-like is_pointer works, and we can overload on that.
    201   template <typename T>
    202   inline void StreamHelper(internal::true_type /*dummy*/, T* pointer) {
    203     if (pointer == NULL) {
    204       *ss_ << "(null)";
    205     } else {
    206       ::GTestStreamToHelper(ss_, pointer);
    207     }
    208   }
    209   template <typename T>
    210   inline void StreamHelper(internal::false_type /*dummy*/, const T& value) {
    211     ::GTestStreamToHelper(ss_, value);
    212   }
    213 #endif  // GTEST_OS_SYMBIAN
    214 
    215   // We'll hold the text streamed to this object here.
    216   internal::StrStream* const ss_;
    217 
    218   // We declare (but don't implement) this to prevent the compiler
    219   // from implementing the assignment operator.
    220   void operator=(const Message&);
    221 };
    222 
    223 // Streams a Message to an ostream.
    224 inline std::ostream& operator <<(std::ostream& os, const Message& sb) {
    225   return os << sb.GetString();
    226 }
    227 
    228 }  // namespace testing
    229 
    230 #endif  // GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
    231