gmock-internal-utils.h revision 95b12332c345cee508a8e2b68e007280392506e0
1e35fdd936d133bf8a48de140a3c666897588a05shiqian// Copyright 2007, Google Inc. 2e35fdd936d133bf8a48de140a3c666897588a05shiqian// All rights reserved. 3e35fdd936d133bf8a48de140a3c666897588a05shiqian// 4e35fdd936d133bf8a48de140a3c666897588a05shiqian// Redistribution and use in source and binary forms, with or without 5e35fdd936d133bf8a48de140a3c666897588a05shiqian// modification, are permitted provided that the following conditions are 6e35fdd936d133bf8a48de140a3c666897588a05shiqian// met: 7e35fdd936d133bf8a48de140a3c666897588a05shiqian// 8e35fdd936d133bf8a48de140a3c666897588a05shiqian// * Redistributions of source code must retain the above copyright 9e35fdd936d133bf8a48de140a3c666897588a05shiqian// notice, this list of conditions and the following disclaimer. 10e35fdd936d133bf8a48de140a3c666897588a05shiqian// * Redistributions in binary form must reproduce the above 11e35fdd936d133bf8a48de140a3c666897588a05shiqian// copyright notice, this list of conditions and the following disclaimer 12e35fdd936d133bf8a48de140a3c666897588a05shiqian// in the documentation and/or other materials provided with the 13e35fdd936d133bf8a48de140a3c666897588a05shiqian// distribution. 14e35fdd936d133bf8a48de140a3c666897588a05shiqian// * Neither the name of Google Inc. nor the names of its 15e35fdd936d133bf8a48de140a3c666897588a05shiqian// contributors may be used to endorse or promote products derived from 16e35fdd936d133bf8a48de140a3c666897588a05shiqian// this software without specific prior written permission. 17e35fdd936d133bf8a48de140a3c666897588a05shiqian// 18e35fdd936d133bf8a48de140a3c666897588a05shiqian// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19e35fdd936d133bf8a48de140a3c666897588a05shiqian// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20e35fdd936d133bf8a48de140a3c666897588a05shiqian// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21e35fdd936d133bf8a48de140a3c666897588a05shiqian// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22e35fdd936d133bf8a48de140a3c666897588a05shiqian// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23e35fdd936d133bf8a48de140a3c666897588a05shiqian// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24e35fdd936d133bf8a48de140a3c666897588a05shiqian// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25e35fdd936d133bf8a48de140a3c666897588a05shiqian// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26e35fdd936d133bf8a48de140a3c666897588a05shiqian// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27e35fdd936d133bf8a48de140a3c666897588a05shiqian// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28e35fdd936d133bf8a48de140a3c666897588a05shiqian// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29e35fdd936d133bf8a48de140a3c666897588a05shiqian// 30e35fdd936d133bf8a48de140a3c666897588a05shiqian// Author: wan@google.com (Zhanyong Wan) 31e35fdd936d133bf8a48de140a3c666897588a05shiqian 32e35fdd936d133bf8a48de140a3c666897588a05shiqian// Google Mock - a framework for writing C++ mock classes. 33e35fdd936d133bf8a48de140a3c666897588a05shiqian// 34e35fdd936d133bf8a48de140a3c666897588a05shiqian// This file defines some utilities useful for implementing Google 35e35fdd936d133bf8a48de140a3c666897588a05shiqian// Mock. They are subject to change without notice, so please DO NOT 36e35fdd936d133bf8a48de140a3c666897588a05shiqian// USE THEM IN USER CODE. 37e35fdd936d133bf8a48de140a3c666897588a05shiqian 38e35fdd936d133bf8a48de140a3c666897588a05shiqian#ifndef GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_ 39e35fdd936d133bf8a48de140a3c666897588a05shiqian#define GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_ 40e35fdd936d133bf8a48de140a3c666897588a05shiqian 41e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <stdio.h> 42e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <ostream> // NOLINT 43e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <string> 44e35fdd936d133bf8a48de140a3c666897588a05shiqian 45e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gmock/internal/gmock-generated-internal-utils.h> 46e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gmock/internal/gmock-port.h> 47e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gtest/gtest.h> 48e35fdd936d133bf8a48de140a3c666897588a05shiqian 49e35fdd936d133bf8a48de140a3c666897588a05shiqian// Concatenates two pre-processor symbols; works for concatenating 50e35fdd936d133bf8a48de140a3c666897588a05shiqian// built-in macros like __FILE__ and __LINE__. 51e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#define GMOCK_CONCAT_TOKEN_IMPL_(foo, bar) foo##bar 52e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#define GMOCK_CONCAT_TOKEN_(foo, bar) GMOCK_CONCAT_TOKEN_IMPL_(foo, bar) 53e35fdd936d133bf8a48de140a3c666897588a05shiqian 54e35fdd936d133bf8a48de140a3c666897588a05shiqian#ifdef __GNUC__ 55e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#define GMOCK_ATTRIBUTE_UNUSED_ __attribute__ ((unused)) 56e35fdd936d133bf8a48de140a3c666897588a05shiqian#else 57e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#define GMOCK_ATTRIBUTE_UNUSED_ 58e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // __GNUC__ 59e35fdd936d133bf8a48de140a3c666897588a05shiqian 60e35fdd936d133bf8a48de140a3c666897588a05shiqianclass ProtocolMessage; 61e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace proto2 { class Message; } 62e35fdd936d133bf8a48de140a3c666897588a05shiqian 63e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace testing { 64e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace internal { 65e35fdd936d133bf8a48de140a3c666897588a05shiqian 66ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan// Converts an identifier name to a space-separated list of lower-case 67ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan// words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is 68ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan// treated as one word. For example, both "FooBar123" and 69ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan// "foo_bar_123" are converted to "foo bar 123". 70ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wanstring ConvertIdentifierNameToWords(const char* id_name); 71ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan 72e35fdd936d133bf8a48de140a3c666897588a05shiqian// Defining a variable of type CompileAssertTypesEqual<T1, T2> will cause a 73e35fdd936d133bf8a48de140a3c666897588a05shiqian// compiler error iff T1 and T2 are different types. 74e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T1, typename T2> 75e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct CompileAssertTypesEqual; 76e35fdd936d133bf8a48de140a3c666897588a05shiqian 77e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 78e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct CompileAssertTypesEqual<T, T> { 79e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 80e35fdd936d133bf8a48de140a3c666897588a05shiqian 81e35fdd936d133bf8a48de140a3c666897588a05shiqian// Removes the reference from a type if it is a reference type, 82e35fdd936d133bf8a48de140a3c666897588a05shiqian// otherwise leaves it unchanged. This is the same as 83e35fdd936d133bf8a48de140a3c666897588a05shiqian// tr1::remove_reference, which is not widely available yet. 84e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 85e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct RemoveReference { typedef T type; }; // NOLINT 86e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 87e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct RemoveReference<T&> { typedef T type; }; // NOLINT 88e35fdd936d133bf8a48de140a3c666897588a05shiqian 89e35fdd936d133bf8a48de140a3c666897588a05shiqian// A handy wrapper around RemoveReference that works when the argument 90e35fdd936d133bf8a48de140a3c666897588a05shiqian// T depends on template parameters. 91e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#define GMOCK_REMOVE_REFERENCE_(T) \ 92e35fdd936d133bf8a48de140a3c666897588a05shiqian typename ::testing::internal::RemoveReference<T>::type 93e35fdd936d133bf8a48de140a3c666897588a05shiqian 94e35fdd936d133bf8a48de140a3c666897588a05shiqian// Removes const from a type if it is a const type, otherwise leaves 95e35fdd936d133bf8a48de140a3c666897588a05shiqian// it unchanged. This is the same as tr1::remove_const, which is not 96e35fdd936d133bf8a48de140a3c666897588a05shiqian// widely available yet. 97e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 98e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct RemoveConst { typedef T type; }; // NOLINT 99e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 100e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct RemoveConst<const T> { typedef T type; }; // NOLINT 101e35fdd936d133bf8a48de140a3c666897588a05shiqian 102b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// MSVC 8.0 has a bug which causes the above definition to fail to 103b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// remove the const in 'const int[3]'. The following specialization 104b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// works around the bug. However, it causes trouble with gcc and thus 105b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// needs to be conditionally compiled. 106b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan#ifdef _MSC_VER 107b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename T, size_t N> 108b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanstruct RemoveConst<T[N]> { 109b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef typename RemoveConst<T>::type type[N]; 110b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}; 111b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan#endif // _MSC_VER 112b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 113e35fdd936d133bf8a48de140a3c666897588a05shiqian// A handy wrapper around RemoveConst that works when the argument 114e35fdd936d133bf8a48de140a3c666897588a05shiqian// T depends on template parameters. 115e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#define GMOCK_REMOVE_CONST_(T) \ 116e35fdd936d133bf8a48de140a3c666897588a05shiqian typename ::testing::internal::RemoveConst<T>::type 117e35fdd936d133bf8a48de140a3c666897588a05shiqian 118e35fdd936d133bf8a48de140a3c666897588a05shiqian// Adds reference to a type if it is not a reference type, 119e35fdd936d133bf8a48de140a3c666897588a05shiqian// otherwise leaves it unchanged. This is the same as 120e35fdd936d133bf8a48de140a3c666897588a05shiqian// tr1::add_reference, which is not widely available yet. 121e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 122e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct AddReference { typedef T& type; }; // NOLINT 123e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 124e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct AddReference<T&> { typedef T& type; }; // NOLINT 125e35fdd936d133bf8a48de140a3c666897588a05shiqian 126e35fdd936d133bf8a48de140a3c666897588a05shiqian// A handy wrapper around AddReference that works when the argument T 127e35fdd936d133bf8a48de140a3c666897588a05shiqian// depends on template parameters. 128e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#define GMOCK_ADD_REFERENCE_(T) \ 129e35fdd936d133bf8a48de140a3c666897588a05shiqian typename ::testing::internal::AddReference<T>::type 130e35fdd936d133bf8a48de140a3c666897588a05shiqian 131e35fdd936d133bf8a48de140a3c666897588a05shiqian// Adds a reference to const on top of T as necessary. For example, 132e35fdd936d133bf8a48de140a3c666897588a05shiqian// it transforms 133e35fdd936d133bf8a48de140a3c666897588a05shiqian// 134e35fdd936d133bf8a48de140a3c666897588a05shiqian// char ==> const char& 135e35fdd936d133bf8a48de140a3c666897588a05shiqian// const char ==> const char& 136e35fdd936d133bf8a48de140a3c666897588a05shiqian// char& ==> const char& 137e35fdd936d133bf8a48de140a3c666897588a05shiqian// const char& ==> const char& 138e35fdd936d133bf8a48de140a3c666897588a05shiqian// 139e35fdd936d133bf8a48de140a3c666897588a05shiqian// The argument T must depend on some template parameters. 140e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#define GMOCK_REFERENCE_TO_CONST_(T) \ 141e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan GMOCK_ADD_REFERENCE_(const GMOCK_REMOVE_REFERENCE_(T)) 142e35fdd936d133bf8a48de140a3c666897588a05shiqian 143e35fdd936d133bf8a48de140a3c666897588a05shiqian// PointeeOf<Pointer>::type is the type of a value pointed to by a 144e35fdd936d133bf8a48de140a3c666897588a05shiqian// Pointer, which can be either a smart pointer or a raw pointer. The 145e35fdd936d133bf8a48de140a3c666897588a05shiqian// following default implementation is for the case where Pointer is a 146e35fdd936d133bf8a48de140a3c666897588a05shiqian// smart pointer. 147e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Pointer> 148e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct PointeeOf { 149e35fdd936d133bf8a48de140a3c666897588a05shiqian // Smart pointer classes define type element_type as the type of 150e35fdd936d133bf8a48de140a3c666897588a05shiqian // their pointees. 151e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename Pointer::element_type type; 152e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 153e35fdd936d133bf8a48de140a3c666897588a05shiqian// This specialization is for the raw pointer case. 154e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 155e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct PointeeOf<T*> { typedef T type; }; // NOLINT 156e35fdd936d133bf8a48de140a3c666897588a05shiqian 157e35fdd936d133bf8a48de140a3c666897588a05shiqian// GetRawPointer(p) returns the raw pointer underlying p when p is a 158e35fdd936d133bf8a48de140a3c666897588a05shiqian// smart pointer, or returns p itself when p is already a raw pointer. 159e35fdd936d133bf8a48de140a3c666897588a05shiqian// The following default implementation is for the smart pointer case. 160e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Pointer> 161e35fdd936d133bf8a48de140a3c666897588a05shiqianinline typename Pointer::element_type* GetRawPointer(const Pointer& p) { 162e35fdd936d133bf8a48de140a3c666897588a05shiqian return p.get(); 163e35fdd936d133bf8a48de140a3c666897588a05shiqian} 164e35fdd936d133bf8a48de140a3c666897588a05shiqian// This overloaded version is for the raw pointer case. 165e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Element> 166e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Element* GetRawPointer(Element* p) { return p; } 167e35fdd936d133bf8a48de140a3c666897588a05shiqian 168e35fdd936d133bf8a48de140a3c666897588a05shiqian// This comparator allows linked_ptr to be stored in sets. 169e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 170e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct LinkedPtrLessThan { 17116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan bool operator()(const ::testing::internal::linked_ptr<T>& lhs, 172e35fdd936d133bf8a48de140a3c666897588a05shiqian const ::testing::internal::linked_ptr<T>& rhs) const { 173e35fdd936d133bf8a48de140a3c666897588a05shiqian return lhs.get() < rhs.get(); 174e35fdd936d133bf8a48de140a3c666897588a05shiqian } 175e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 176e35fdd936d133bf8a48de140a3c666897588a05shiqian 177e35fdd936d133bf8a48de140a3c666897588a05shiqian// ImplicitlyConvertible<From, To>::value is a compile-time bool 178e35fdd936d133bf8a48de140a3c666897588a05shiqian// constant that's true iff type From can be implicitly converted to 179e35fdd936d133bf8a48de140a3c666897588a05shiqian// type To. 180e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename From, typename To> 181e35fdd936d133bf8a48de140a3c666897588a05shiqianclass ImplicitlyConvertible { 182e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 183e35fdd936d133bf8a48de140a3c666897588a05shiqian // We need the following helper functions only for their types. 184e35fdd936d133bf8a48de140a3c666897588a05shiqian // They have no implementations. 185e35fdd936d133bf8a48de140a3c666897588a05shiqian 186e35fdd936d133bf8a48de140a3c666897588a05shiqian // MakeFrom() is an expression whose type is From. We cannot simply 187e35fdd936d133bf8a48de140a3c666897588a05shiqian // use From(), as the type From may not have a public default 188e35fdd936d133bf8a48de140a3c666897588a05shiqian // constructor. 189e35fdd936d133bf8a48de140a3c666897588a05shiqian static From MakeFrom(); 190e35fdd936d133bf8a48de140a3c666897588a05shiqian 191e35fdd936d133bf8a48de140a3c666897588a05shiqian // These two functions are overloaded. Given an expression 192e35fdd936d133bf8a48de140a3c666897588a05shiqian // Helper(x), the compiler will pick the first version if x can be 193e35fdd936d133bf8a48de140a3c666897588a05shiqian // implicitly converted to type To; otherwise it will pick the 194e35fdd936d133bf8a48de140a3c666897588a05shiqian // second version. 195e35fdd936d133bf8a48de140a3c666897588a05shiqian // 196e35fdd936d133bf8a48de140a3c666897588a05shiqian // The first version returns a value of size 1, and the second 197e35fdd936d133bf8a48de140a3c666897588a05shiqian // version returns a value of size 2. Therefore, by checking the 198e35fdd936d133bf8a48de140a3c666897588a05shiqian // size of Helper(x), which can be done at compile time, we can tell 199e35fdd936d133bf8a48de140a3c666897588a05shiqian // which version of Helper() is used, and hence whether x can be 200e35fdd936d133bf8a48de140a3c666897588a05shiqian // implicitly converted to type To. 201e35fdd936d133bf8a48de140a3c666897588a05shiqian static char Helper(To); 202e35fdd936d133bf8a48de140a3c666897588a05shiqian static char (&Helper(...))[2]; // NOLINT 203e35fdd936d133bf8a48de140a3c666897588a05shiqian 204e35fdd936d133bf8a48de140a3c666897588a05shiqian // We have to put the 'public' section after the 'private' section, 205e35fdd936d133bf8a48de140a3c666897588a05shiqian // or MSVC refuses to compile the code. 206e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 207e35fdd936d133bf8a48de140a3c666897588a05shiqian // MSVC warns about implicitly converting from double to int for 208e35fdd936d133bf8a48de140a3c666897588a05shiqian // possible loss of data, so we need to temporarily disable the 209e35fdd936d133bf8a48de140a3c666897588a05shiqian // warning. 210e35fdd936d133bf8a48de140a3c666897588a05shiqian#ifdef _MSC_VER 211e35fdd936d133bf8a48de140a3c666897588a05shiqian#pragma warning(push) // Saves the current warning state. 212e35fdd936d133bf8a48de140a3c666897588a05shiqian#pragma warning(disable:4244) // Temporarily disables warning 4244. 213e35fdd936d133bf8a48de140a3c666897588a05shiqian static const bool value = 214e35fdd936d133bf8a48de140a3c666897588a05shiqian sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1; 215e35fdd936d133bf8a48de140a3c666897588a05shiqian#pragma warning(pop) // Restores the warning state. 216e35fdd936d133bf8a48de140a3c666897588a05shiqian#else 217e35fdd936d133bf8a48de140a3c666897588a05shiqian static const bool value = 218e35fdd936d133bf8a48de140a3c666897588a05shiqian sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1; 219e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // _MSV_VER 220e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 221e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename From, typename To> 222e35fdd936d133bf8a48de140a3c666897588a05shiqianconst bool ImplicitlyConvertible<From, To>::value; 223e35fdd936d133bf8a48de140a3c666897588a05shiqian 22495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// Symbian compilation can be done with wchar_t being either a native 22595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// type or a typedef. Using Google Mock with OpenC without wchar_t 22695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// should require the definition of _STLP_NO_WCHAR_T. 22795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// 22895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// MSVC treats wchar_t as a native type usually, but treats it as the 22995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// same as unsigned short when the compiler option /Zc:wchar_t- is 23095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// specified. It defines _NATIVE_WCHAR_T_DEFINED symbol when wchar_t 23195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// is a native type. 23295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#if (GTEST_OS_SYMBIAN && defined(_STLP_NO_WCHAR_T)) || \ 23395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan (defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED)) 23495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// wchar_t is a typedef. 23595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#else 23695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#define GMOCK_WCHAR_T_IS_NATIVE_ 1 23795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#endif 23895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan 23995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// signed wchar_t and unsigned wchar_t are NOT in the C++ standard. 24095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// Using them is a bad practice and not portable. So DON'T use them. 24195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// 24295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// Still, Google Mock is designed to work even if the user uses signed 24395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// wchar_t or unsigned wchar_t (obviously, assuming the compiler 24495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// supports them). 24595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// 24695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// To gcc, 24795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// wchar_t == signed wchar_t != unsigned wchar_t == unsigned int 24895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#ifdef __GNUC__ 24995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#define GMOCK_HAS_SIGNED_WCHAR_T_ 1 // signed/unsigned wchar_t are valid types. 25095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#endif 25195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan 25216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// In what follows, we use the term "kind" to indicate whether a type 25316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// is bool, an integer type (excluding bool), a floating-point type, 25416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// or none of them. This categorization is useful for determining 25516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// when a matcher argument type can be safely converted to another 25616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// type in the implementation of SafeMatcherCast. 25716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanenum TypeKind { 25816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan kBool, kInteger, kFloatingPoint, kOther 25916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan}; 26016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 26116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// KindOf<T>::value is the kind of type T. 26216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename T> struct KindOf { 26316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan enum { value = kOther }; // The default kind. 26416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan}; 26516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 26616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// This macro declares that the kind of 'type' is 'kind'. 26716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#define GMOCK_DECLARE_KIND_(type, kind) \ 26816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan template <> struct KindOf<type> { enum { value = kind }; } 26916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 27016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(bool, kBool); 27116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 27216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// All standard integer types. 27316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(char, kInteger); 27416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(signed char, kInteger); 27516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(unsigned char, kInteger); 27616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(short, kInteger); // NOLINT 27716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(unsigned short, kInteger); // NOLINT 27816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(int, kInteger); 27916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(unsigned int, kInteger); 28016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(long, kInteger); // NOLINT 28116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(unsigned long, kInteger); // NOLINT 28216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 28395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#if GMOCK_WCHAR_T_IS_NATIVE_ 28416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(wchar_t, kInteger); 28516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#endif 28616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 28716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Non-standard integer types. 28816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(Int64, kInteger); 28916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(UInt64, kInteger); 29016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 29116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// All standard floating-point types. 29216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(float, kFloatingPoint); 29316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(double, kFloatingPoint); 29416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(long double, kFloatingPoint); 29516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 29616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#undef GMOCK_DECLARE_KIND_ 29716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 29816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Evaluates to the kind of 'type'. 29916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#define GMOCK_KIND_OF_(type) \ 30016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan static_cast< ::testing::internal::TypeKind>( \ 30116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan ::testing::internal::KindOf<type>::value) 30216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 30316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Evaluates to true iff integer type T is signed. 30416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#define GMOCK_IS_SIGNED_(T) (static_cast<T>(-1) < 0) 30516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 30616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// LosslessArithmeticConvertibleImpl<kFromKind, From, kToKind, To>::value 30716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// is true iff arithmetic type From can be losslessly converted to 30816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// arithmetic type To. 30916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// 31016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// It's the user's responsibility to ensure that both From and To are 31116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// raw (i.e. has no CV modifier, is not a pointer, and is not a 31216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// reference) built-in arithmetic types, kFromKind is the kind of 31316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// From, and kToKind is the kind of To; the value is 31416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// implementation-defined when the above pre-condition is violated. 31516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <TypeKind kFromKind, typename From, TypeKind kToKind, typename To> 31616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl : public false_type {}; 31716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 31816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting bool to bool is lossless. 31916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <> 32016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kBool, bool, kBool, bool> 32116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public true_type {}; // NOLINT 32216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 32316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting bool to any integer type is lossless. 32416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename To> 32516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kBool, bool, kInteger, To> 32616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public true_type {}; // NOLINT 32716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 32816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting bool to any floating-point type is lossless. 32916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename To> 33016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kBool, bool, kFloatingPoint, To> 33116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public true_type {}; // NOLINT 33216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 33316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting an integer to bool is lossy. 33416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From> 33516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kInteger, From, kBool, bool> 33616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public false_type {}; // NOLINT 33716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 33816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting an integer to another non-bool integer is lossless iff 33916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// the target type's range encloses the source type's range. 34016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From, typename To> 34116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kInteger, From, kInteger, To> 34216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public bool_constant< 34316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // When converting from a smaller size to a larger size, we are 34416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // fine as long as we are not converting from signed to unsigned. 34516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan ((sizeof(From) < sizeof(To)) && 34616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan (!GMOCK_IS_SIGNED_(From) || GMOCK_IS_SIGNED_(To))) || 34716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // When converting between the same size, the signedness must match. 34816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan ((sizeof(From) == sizeof(To)) && 34916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan (GMOCK_IS_SIGNED_(From) == GMOCK_IS_SIGNED_(To)))> {}; // NOLINT 35016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 35116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#undef GMOCK_IS_SIGNED_ 35216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 35316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting an integer to a floating-point type may be lossy, since 35416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// the format of a floating-point number is implementation-defined. 35516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From, typename To> 35616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kInteger, From, kFloatingPoint, To> 35716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public false_type {}; // NOLINT 35816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 35916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting a floating-point to bool is lossy. 36016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From> 36116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kBool, bool> 36216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public false_type {}; // NOLINT 36316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 36416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting a floating-point to an integer is lossy. 36516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From, typename To> 36616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kInteger, To> 36716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public false_type {}; // NOLINT 36816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 36916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting a floating-point to another floating-point is lossless 37016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// iff the target type is at least as big as the source type. 37116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From, typename To> 37216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl< 37316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan kFloatingPoint, From, kFloatingPoint, To> 37416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public bool_constant<sizeof(From) <= sizeof(To)> {}; // NOLINT 37516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 37616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// LosslessArithmeticConvertible<From, To>::value is true iff arithmetic 37716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// type From can be losslessly converted to arithmetic type To. 37816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// 37916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// It's the user's responsibility to ensure that both From and To are 38016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// raw (i.e. has no CV modifier, is not a pointer, and is not a 38116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// reference) built-in arithmetic types; the value is 38216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// implementation-defined when the above pre-condition is violated. 38316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From, typename To> 38416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertible 38516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public LosslessArithmeticConvertibleImpl< 38616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan GMOCK_KIND_OF_(From), From, GMOCK_KIND_OF_(To), To> {}; // NOLINT 38716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 388e35fdd936d133bf8a48de140a3c666897588a05shiqian// IsAProtocolMessage<T>::value is a compile-time bool constant that's 389e35fdd936d133bf8a48de140a3c666897588a05shiqian// true iff T is type ProtocolMessage, proto2::Message, or a subclass 390e35fdd936d133bf8a48de140a3c666897588a05shiqian// of those. 391e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 39216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct IsAProtocolMessage 39316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public bool_constant< 39416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan ImplicitlyConvertible<const T*, const ::ProtocolMessage*>::value || 39516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan ImplicitlyConvertible<const T*, const ::proto2::Message*>::value> { 396e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 397e35fdd936d133bf8a48de140a3c666897588a05shiqian 398e35fdd936d133bf8a48de140a3c666897588a05shiqian// When the compiler sees expression IsContainerTest<C>(0), the first 399e35fdd936d133bf8a48de140a3c666897588a05shiqian// overload of IsContainerTest will be picked if C is an STL-style 400e35fdd936d133bf8a48de140a3c666897588a05shiqian// container class (since C::const_iterator* is a valid type and 0 can 401e35fdd936d133bf8a48de140a3c666897588a05shiqian// be converted to it), while the second overload will be picked 402e35fdd936d133bf8a48de140a3c666897588a05shiqian// otherwise (since C::const_iterator will be an invalid type in this 403e35fdd936d133bf8a48de140a3c666897588a05shiqian// case). Therefore, we can determine whether C is a container class 404e35fdd936d133bf8a48de140a3c666897588a05shiqian// by checking the type of IsContainerTest<C>(0). The value of the 405e35fdd936d133bf8a48de140a3c666897588a05shiqian// expression is insignificant. 406e35fdd936d133bf8a48de140a3c666897588a05shiqiantypedef int IsContainer; 407e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <class C> 408e35fdd936d133bf8a48de140a3c666897588a05shiqianIsContainer IsContainerTest(typename C::const_iterator*) { return 0; } 409e35fdd936d133bf8a48de140a3c666897588a05shiqian 410e35fdd936d133bf8a48de140a3c666897588a05shiqiantypedef char IsNotContainer; 411e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <class C> 412e35fdd936d133bf8a48de140a3c666897588a05shiqianIsNotContainer IsContainerTest(...) { return '\0'; } 413e35fdd936d133bf8a48de140a3c666897588a05shiqian 414e35fdd936d133bf8a48de140a3c666897588a05shiqian// This interface knows how to report a Google Mock failure (either 415e35fdd936d133bf8a48de140a3c666897588a05shiqian// non-fatal or fatal). 416e35fdd936d133bf8a48de140a3c666897588a05shiqianclass FailureReporterInterface { 417e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 418e35fdd936d133bf8a48de140a3c666897588a05shiqian // The type of a failure (either non-fatal or fatal). 419e35fdd936d133bf8a48de140a3c666897588a05shiqian enum FailureType { 420e35fdd936d133bf8a48de140a3c666897588a05shiqian NONFATAL, FATAL 421e35fdd936d133bf8a48de140a3c666897588a05shiqian }; 422e35fdd936d133bf8a48de140a3c666897588a05shiqian 423e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual ~FailureReporterInterface() {} 424e35fdd936d133bf8a48de140a3c666897588a05shiqian 425e35fdd936d133bf8a48de140a3c666897588a05shiqian // Reports a failure that occurred at the given source file location. 426e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void ReportFailure(FailureType type, const char* file, int line, 427e35fdd936d133bf8a48de140a3c666897588a05shiqian const string& message) = 0; 428e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 429e35fdd936d133bf8a48de140a3c666897588a05shiqian 430e35fdd936d133bf8a48de140a3c666897588a05shiqian// Returns the failure reporter used by Google Mock. 431e35fdd936d133bf8a48de140a3c666897588a05shiqianFailureReporterInterface* GetFailureReporter(); 432e35fdd936d133bf8a48de140a3c666897588a05shiqian 433e35fdd936d133bf8a48de140a3c666897588a05shiqian// Asserts that condition is true; aborts the process with the given 434e35fdd936d133bf8a48de140a3c666897588a05shiqian// message if condition is false. We cannot use LOG(FATAL) or CHECK() 435e35fdd936d133bf8a48de140a3c666897588a05shiqian// as Google Mock might be used to mock the log sink itself. We 436e35fdd936d133bf8a48de140a3c666897588a05shiqian// inline this function to prevent it from showing up in the stack 437e35fdd936d133bf8a48de140a3c666897588a05shiqian// trace. 438e35fdd936d133bf8a48de140a3c666897588a05shiqianinline void Assert(bool condition, const char* file, int line, 439e35fdd936d133bf8a48de140a3c666897588a05shiqian const string& msg) { 440e35fdd936d133bf8a48de140a3c666897588a05shiqian if (!condition) { 441e35fdd936d133bf8a48de140a3c666897588a05shiqian GetFailureReporter()->ReportFailure(FailureReporterInterface::FATAL, 442e35fdd936d133bf8a48de140a3c666897588a05shiqian file, line, msg); 443e35fdd936d133bf8a48de140a3c666897588a05shiqian } 444e35fdd936d133bf8a48de140a3c666897588a05shiqian} 445e35fdd936d133bf8a48de140a3c666897588a05shiqianinline void Assert(bool condition, const char* file, int line) { 446e35fdd936d133bf8a48de140a3c666897588a05shiqian Assert(condition, file, line, "Assertion failed."); 447e35fdd936d133bf8a48de140a3c666897588a05shiqian} 448e35fdd936d133bf8a48de140a3c666897588a05shiqian 449e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that condition is true; generates a non-fatal failure if 450e35fdd936d133bf8a48de140a3c666897588a05shiqian// condition is false. 451e35fdd936d133bf8a48de140a3c666897588a05shiqianinline void Expect(bool condition, const char* file, int line, 452e35fdd936d133bf8a48de140a3c666897588a05shiqian const string& msg) { 453e35fdd936d133bf8a48de140a3c666897588a05shiqian if (!condition) { 454e35fdd936d133bf8a48de140a3c666897588a05shiqian GetFailureReporter()->ReportFailure(FailureReporterInterface::NONFATAL, 455e35fdd936d133bf8a48de140a3c666897588a05shiqian file, line, msg); 456e35fdd936d133bf8a48de140a3c666897588a05shiqian } 457e35fdd936d133bf8a48de140a3c666897588a05shiqian} 458e35fdd936d133bf8a48de140a3c666897588a05shiqianinline void Expect(bool condition, const char* file, int line) { 459e35fdd936d133bf8a48de140a3c666897588a05shiqian Expect(condition, file, line, "Expectation failed."); 460e35fdd936d133bf8a48de140a3c666897588a05shiqian} 461e35fdd936d133bf8a48de140a3c666897588a05shiqian 462e35fdd936d133bf8a48de140a3c666897588a05shiqian// Severity level of a log. 463e35fdd936d133bf8a48de140a3c666897588a05shiqianenum LogSeverity { 464e35fdd936d133bf8a48de140a3c666897588a05shiqian INFO = 0, 465e35fdd936d133bf8a48de140a3c666897588a05shiqian WARNING = 1, 466e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 467e35fdd936d133bf8a48de140a3c666897588a05shiqian 468e35fdd936d133bf8a48de140a3c666897588a05shiqian// Valid values for the --gmock_verbose flag. 469e35fdd936d133bf8a48de140a3c666897588a05shiqian 470e35fdd936d133bf8a48de140a3c666897588a05shiqian// All logs (informational and warnings) are printed. 471e35fdd936d133bf8a48de140a3c666897588a05shiqianconst char kInfoVerbosity[] = "info"; 472e35fdd936d133bf8a48de140a3c666897588a05shiqian// Only warnings are printed. 473e35fdd936d133bf8a48de140a3c666897588a05shiqianconst char kWarningVerbosity[] = "warning"; 474e35fdd936d133bf8a48de140a3c666897588a05shiqian// No logs are printed. 475e35fdd936d133bf8a48de140a3c666897588a05shiqianconst char kErrorVerbosity[] = "error"; 476e35fdd936d133bf8a48de140a3c666897588a05shiqian 4779413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan// Returns true iff a log with the given severity is visible according 4789413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan// to the --gmock_verbose flag. 4799413f2ff615ae1b933580576183d316c4cb6376czhanyong.wanbool LogIsVisible(LogSeverity severity); 4809413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan 481e35fdd936d133bf8a48de140a3c666897588a05shiqian// Prints the given message to stdout iff 'severity' >= the level 482e35fdd936d133bf8a48de140a3c666897588a05shiqian// specified by the --gmock_verbose flag. If stack_frames_to_skip >= 483e35fdd936d133bf8a48de140a3c666897588a05shiqian// 0, also prints the stack trace excluding the top 484e35fdd936d133bf8a48de140a3c666897588a05shiqian// stack_frames_to_skip frames. In opt mode, any positive 485e35fdd936d133bf8a48de140a3c666897588a05shiqian// stack_frames_to_skip is treated as 0, since we don't know which 486e35fdd936d133bf8a48de140a3c666897588a05shiqian// function calls will be inlined by the compiler and need to be 487e35fdd936d133bf8a48de140a3c666897588a05shiqian// conservative. 488e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid Log(LogSeverity severity, const string& message, int stack_frames_to_skip); 489e35fdd936d133bf8a48de140a3c666897588a05shiqian 49016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// TODO(wan@google.com): group all type utilities together. 49116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 492e35fdd936d133bf8a48de140a3c666897588a05shiqian// Type traits. 493e35fdd936d133bf8a48de140a3c666897588a05shiqian 494e35fdd936d133bf8a48de140a3c666897588a05shiqian// is_reference<T>::value is non-zero iff T is a reference type. 495e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> struct is_reference : public false_type {}; 496e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> struct is_reference<T&> : public true_type {}; 497e35fdd936d133bf8a48de140a3c666897588a05shiqian 498e35fdd936d133bf8a48de140a3c666897588a05shiqian// type_equals<T1, T2>::value is non-zero iff T1 and T2 are the same type. 499e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T1, typename T2> struct type_equals : public false_type {}; 500e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> struct type_equals<T, T> : public true_type {}; 501e35fdd936d133bf8a48de140a3c666897588a05shiqian 502e35fdd936d133bf8a48de140a3c666897588a05shiqian// remove_reference<T>::type removes the reference from type T, if any. 50316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename T> struct remove_reference { typedef T type; }; // NOLINT 50416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename T> struct remove_reference<T&> { typedef T type; }; // NOLINT 505e35fdd936d133bf8a48de140a3c666897588a05shiqian 506e35fdd936d133bf8a48de140a3c666897588a05shiqian// Invalid<T>() returns an invalid value of type T. This is useful 507e35fdd936d133bf8a48de140a3c666897588a05shiqian// when a value of type T is needed for compilation, but the statement 508e35fdd936d133bf8a48de140a3c666897588a05shiqian// will not really be executed (or we don't care if the statement 509e35fdd936d133bf8a48de140a3c666897588a05shiqian// crashes). 510e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 511e35fdd936d133bf8a48de140a3c666897588a05shiqianinline T Invalid() { 512e35fdd936d133bf8a48de140a3c666897588a05shiqian return *static_cast<typename remove_reference<T>::type*>(NULL); 513e35fdd936d133bf8a48de140a3c666897588a05shiqian} 514e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <> 515e35fdd936d133bf8a48de140a3c666897588a05shiqianinline void Invalid<void>() {} 516e35fdd936d133bf8a48de140a3c666897588a05shiqian 517b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Utilities for native arrays. 518b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 519b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// ArrayEq() compares two k-dimensional native arrays using the 520b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// elements' operator==, where k can be any integer >= 0. When k is 521b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// 0, ArrayEq() degenerates into comparing a single pair of values. 522b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 523b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename T, typename U> 524b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanbool ArrayEq(const T* lhs, size_t size, const U* rhs); 525b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 526b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// This generic version is used when k is 0. 527b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename T, typename U> 528b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.waninline bool ArrayEq(const T& lhs, const U& rhs) { return lhs == rhs; } 529b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 530b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// This overload is used when k >= 1. 531b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename T, typename U, size_t N> 532b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.waninline bool ArrayEq(const T(&lhs)[N], const U(&rhs)[N]) { 533b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan return internal::ArrayEq(lhs, N, rhs); 534b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 535b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 536b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// This helper reduces code bloat. If we instead put its logic inside 537b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// the previous ArrayEq() function, arrays with different sizes would 538b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// lead to different copies of the template code. 539b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename T, typename U> 540b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanbool ArrayEq(const T* lhs, size_t size, const U* rhs) { 541b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan for (size_t i = 0; i != size; i++) { 542b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan if (!internal::ArrayEq(lhs[i], rhs[i])) 543b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan return false; 544b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 545b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan return true; 546b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 547b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 548b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Finds the first element in the iterator range [begin, end) that 549b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// equals elem. Element may be a native array type itself. 550b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename Iter, typename Element> 551b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanIter ArrayAwareFind(Iter begin, Iter end, const Element& elem) { 552b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan for (Iter it = begin; it != end; ++it) { 553b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan if (internal::ArrayEq(*it, elem)) 554b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan return it; 555b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 556b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan return end; 557b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 558b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 559b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// CopyArray() copies a k-dimensional native array using the elements' 560b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// operator=, where k can be any integer >= 0. When k is 0, 561b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// CopyArray() degenerates into copying a single value. 562b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 563b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename T, typename U> 564b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanvoid CopyArray(const T* from, size_t size, U* to); 565b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 566b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// This generic version is used when k is 0. 567b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename T, typename U> 568b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.waninline void CopyArray(const T& from, U* to) { *to = from; } 569b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 570b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// This overload is used when k >= 1. 571b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename T, typename U, size_t N> 572b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.waninline void CopyArray(const T(&from)[N], U(*to)[N]) { 573b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan internal::CopyArray(from, N, *to); 574b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 575b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 576b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// This helper reduces code bloat. If we instead put its logic inside 577b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// the previous CopyArray() function, arrays with different sizes 578b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// would lead to different copies of the template code. 579b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename T, typename U> 580b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanvoid CopyArray(const T* from, size_t size, U* to) { 581b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan for (size_t i = 0; i != size; i++) { 582b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan internal::CopyArray(from[i], to + i); 583b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 584b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 585b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 586b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// The relation between an NativeArray object (see below) and the 587b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// native array it represents. 588b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanenum RelationToSource { 589b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan kReference, // The NativeArray references the native array. 590b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan kCopy // The NativeArray makes a copy of the native array and 591b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // owns the copy. 592b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}; 593b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 594b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Adapts a native array to a read-only STL-style container. Instead 595b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// of the complete STL container concept, this adaptor only implements 596b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// members useful for Google Mock's container matchers. New members 597b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// should be added as needed. To simplify the implementation, we only 598b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// support Element being a raw type (i.e. having no top-level const or 599b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// reference modifier). It's the client's responsibility to satisfy 600b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// this requirement. Element can be an array type itself (hence 601b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// multi-dimensional arrays are supported). 602b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename Element> 603b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanclass NativeArray { 604b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan public: 605b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // STL-style container typedefs. 606b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef Element value_type; 607b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef const Element* const_iterator; 608b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 6094bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wan // Constructs from a native array. 6104bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wan NativeArray(const Element* array, size_t count, RelationToSource relation) { 6114bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wan Init(array, count, relation); 612b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 613b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 614b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // Copy constructor. 615b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan NativeArray(const NativeArray& rhs) { 616b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan Init(rhs.array_, rhs.size_, rhs.relation_to_source_); 617b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 618b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 619b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan ~NativeArray() { 620b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // Ensures that the user doesn't instantiate NativeArray with a 621b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // const or reference type. 622b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan testing::StaticAssertTypeEq<Element, 623b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Element))>(); 624b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan if (relation_to_source_ == kCopy) 625b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan delete[] array_; 626b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 627b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 628b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // STL-style container methods. 629b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan size_t size() const { return size_; } 630b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const_iterator begin() const { return array_; } 631b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const_iterator end() const { return array_ + size_; } 632b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan bool operator==(const NativeArray& rhs) const { 633b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan return size() == rhs.size() && 634b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan ArrayEq(begin(), size(), rhs.begin()); 635b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 636b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 637b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan private: 638b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // Not implemented as we don't want to support assignment. 639b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan void operator=(const NativeArray& rhs); 640b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 641b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // Initializes this object; makes a copy of the input array if 642b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // 'relation' is kCopy. 643b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan void Init(const Element* array, size_t size, RelationToSource relation) { 644b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan if (relation == kReference) { 645b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan array_ = array; 646b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } else { 647b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan Element* const copy = new Element[size]; 648b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan CopyArray(array, size, copy); 649b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan array_ = copy; 650b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 651b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan size_ = size; 652b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan relation_to_source_ = relation; 653b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 654b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 655b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const Element* array_; 656b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan size_t size_; 657b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan RelationToSource relation_to_source_; 658b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}; 659b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 660b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Given a raw type (i.e. having no top-level reference or const 661b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// modifier) RawContainer that's either an STL-style container or a 662b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// native array, class StlContainerView<RawContainer> has the 663b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// following members: 664b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// 665b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// - type is a type that provides an STL-style container view to 666b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// (i.e. implements the STL container concept for) RawContainer; 667b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// - const_reference is a type that provides a reference to a const 668b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// RawContainer; 669b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// - ConstReference(raw_container) returns a const reference to an STL-style 670b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// container view to raw_container, which is a RawContainer. 671b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// - Copy(raw_container) returns an STL-style container view of a 672b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// copy of raw_container, which is a RawContainer. 673b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// 674b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// This generic version is used when RawContainer itself is already an 675b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// STL-style container. 676b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <class RawContainer> 677b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanclass StlContainerView { 678b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan public: 679b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef RawContainer type; 680b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef const type& const_reference; 681b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 682b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan static const_reference ConstReference(const RawContainer& container) { 683b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // Ensures that RawContainer is not a const type. 684b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan testing::StaticAssertTypeEq<RawContainer, 685b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan GMOCK_REMOVE_CONST_(RawContainer)>(); 686b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan return container; 687b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 688b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan static type Copy(const RawContainer& container) { return container; } 689b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}; 690b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 691b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// This specialization is used when RawContainer is a native array type. 692b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename Element, size_t N> 693b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanclass StlContainerView<Element[N]> { 694b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan public: 695b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef GMOCK_REMOVE_CONST_(Element) RawElement; 696b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef internal::NativeArray<RawElement> type; 697b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // NativeArray<T> can represent a native array either by value or by 698b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // reference (selected by a constructor argument), so 'const type' 699b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // can be used to reference a const native array. We cannot 700b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // 'typedef const type& const_reference' here, as that would mean 701b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // ConstReference() has to return a reference to a local variable. 702b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef const type const_reference; 703b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 704b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan static const_reference ConstReference(const Element (&array)[N]) { 705b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // Ensures that Element is not a const type. 706b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan testing::StaticAssertTypeEq<Element, RawElement>(); 70795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#if GTEST_OS_SYMBIAN 70895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // The Nokia Symbian compiler confuses itself in template instantiation 70995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // for this call without the cast to Element*: 71095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // function call '[testing::internal::NativeArray<char *>].NativeArray( 71195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // {lval} const char *[4], long, testing::internal::RelationToSource)' 71295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // does not match 71395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // 'testing::internal::NativeArray<char *>::NativeArray( 71495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // char *const *, unsigned int, testing::internal::RelationToSource)' 71595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // (instantiating: 'testing::internal::ContainsMatcherImpl 71695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // <const char * (&)[4]>::Matches(const char * (&)[4]) const') 71795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // (instantiating: 'testing::internal::StlContainerView<char *[4]>:: 71895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // ConstReference(const char * (&)[4])') 71995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // (and though the N parameter type is mismatched in the above explicit 72095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // conversion of it doesn't help - only the conversion of the array). 72195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan return type(const_cast<Element*>(&array[0]), N, kReference); 72295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#else 7234bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wan return type(array, N, kReference); 72495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#endif // GTEST_OS_SYMBIAN 725b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 726b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan static type Copy(const Element (&array)[N]) { 72795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#if GTEST_OS_SYMBIAN 72895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan return type(const_cast<Element*>(&array[0]), N, kCopy); 72995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#else 7304bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wan return type(array, N, kCopy); 73195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#endif // GTEST_OS_SYMBIAN 732b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 733b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}; 734b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 735b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// This specialization is used when RawContainer is a native array 736b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// represented as a (pointer, size) tuple. 737b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename ElementPointer, typename Size> 738b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanclass StlContainerView< ::std::tr1::tuple<ElementPointer, Size> > { 739b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan public: 740b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef GMOCK_REMOVE_CONST_( 741b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typename internal::PointeeOf<ElementPointer>::type) RawElement; 742b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef internal::NativeArray<RawElement> type; 743b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef const type const_reference; 744b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 745b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan static const_reference ConstReference( 746b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const ::std::tr1::tuple<ElementPointer, Size>& array) { 7474bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wan using ::std::tr1::get; 7484bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wan return type(get<0>(array), get<1>(array), kReference); 749b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 750b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan static type Copy(const ::std::tr1::tuple<ElementPointer, Size>& array) { 7514bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wan using ::std::tr1::get; 7524bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wan return type(get<0>(array), get<1>(array), kCopy); 753b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 754b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}; 755b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 756b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// The following specialization prevents the user from instantiating 757b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// StlContainer with a reference type. 758b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename T> class StlContainerView<T&>; 759b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 760e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace internal 761e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace testing 762e35fdd936d133bf8a48de140a3c666897588a05shiqian 763e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_ 764