gmock-internal-utils.h revision 16cf473930c01cd7a1a51dff65f22c541fbad5b8
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 102e35fdd936d133bf8a48de140a3c666897588a05shiqian// A handy wrapper around RemoveConst that works when the argument 103e35fdd936d133bf8a48de140a3c666897588a05shiqian// T depends on template parameters. 104e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#define GMOCK_REMOVE_CONST_(T) \ 105e35fdd936d133bf8a48de140a3c666897588a05shiqian typename ::testing::internal::RemoveConst<T>::type 106e35fdd936d133bf8a48de140a3c666897588a05shiqian 107e35fdd936d133bf8a48de140a3c666897588a05shiqian// Adds reference to a type if it is not a reference type, 108e35fdd936d133bf8a48de140a3c666897588a05shiqian// otherwise leaves it unchanged. This is the same as 109e35fdd936d133bf8a48de140a3c666897588a05shiqian// tr1::add_reference, which is not widely available yet. 110e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 111e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct AddReference { typedef T& type; }; // NOLINT 112e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 113e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct AddReference<T&> { typedef T& type; }; // NOLINT 114e35fdd936d133bf8a48de140a3c666897588a05shiqian 115e35fdd936d133bf8a48de140a3c666897588a05shiqian// A handy wrapper around AddReference that works when the argument T 116e35fdd936d133bf8a48de140a3c666897588a05shiqian// depends on template parameters. 117e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#define GMOCK_ADD_REFERENCE_(T) \ 118e35fdd936d133bf8a48de140a3c666897588a05shiqian typename ::testing::internal::AddReference<T>::type 119e35fdd936d133bf8a48de140a3c666897588a05shiqian 120e35fdd936d133bf8a48de140a3c666897588a05shiqian// Adds a reference to const on top of T as necessary. For example, 121e35fdd936d133bf8a48de140a3c666897588a05shiqian// it transforms 122e35fdd936d133bf8a48de140a3c666897588a05shiqian// 123e35fdd936d133bf8a48de140a3c666897588a05shiqian// char ==> const char& 124e35fdd936d133bf8a48de140a3c666897588a05shiqian// const char ==> const char& 125e35fdd936d133bf8a48de140a3c666897588a05shiqian// char& ==> const char& 126e35fdd936d133bf8a48de140a3c666897588a05shiqian// const char& ==> const char& 127e35fdd936d133bf8a48de140a3c666897588a05shiqian// 128e35fdd936d133bf8a48de140a3c666897588a05shiqian// The argument T must depend on some template parameters. 129e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#define GMOCK_REFERENCE_TO_CONST_(T) \ 130e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan GMOCK_ADD_REFERENCE_(const GMOCK_REMOVE_REFERENCE_(T)) 131e35fdd936d133bf8a48de140a3c666897588a05shiqian 132e35fdd936d133bf8a48de140a3c666897588a05shiqian// PointeeOf<Pointer>::type is the type of a value pointed to by a 133e35fdd936d133bf8a48de140a3c666897588a05shiqian// Pointer, which can be either a smart pointer or a raw pointer. The 134e35fdd936d133bf8a48de140a3c666897588a05shiqian// following default implementation is for the case where Pointer is a 135e35fdd936d133bf8a48de140a3c666897588a05shiqian// smart pointer. 136e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Pointer> 137e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct PointeeOf { 138e35fdd936d133bf8a48de140a3c666897588a05shiqian // Smart pointer classes define type element_type as the type of 139e35fdd936d133bf8a48de140a3c666897588a05shiqian // their pointees. 140e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename Pointer::element_type type; 141e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 142e35fdd936d133bf8a48de140a3c666897588a05shiqian// This specialization is for the raw pointer case. 143e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 144e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct PointeeOf<T*> { typedef T type; }; // NOLINT 145e35fdd936d133bf8a48de140a3c666897588a05shiqian 146e35fdd936d133bf8a48de140a3c666897588a05shiqian// GetRawPointer(p) returns the raw pointer underlying p when p is a 147e35fdd936d133bf8a48de140a3c666897588a05shiqian// smart pointer, or returns p itself when p is already a raw pointer. 148e35fdd936d133bf8a48de140a3c666897588a05shiqian// The following default implementation is for the smart pointer case. 149e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Pointer> 150e35fdd936d133bf8a48de140a3c666897588a05shiqianinline typename Pointer::element_type* GetRawPointer(const Pointer& p) { 151e35fdd936d133bf8a48de140a3c666897588a05shiqian return p.get(); 152e35fdd936d133bf8a48de140a3c666897588a05shiqian} 153e35fdd936d133bf8a48de140a3c666897588a05shiqian// This overloaded version is for the raw pointer case. 154e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Element> 155e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Element* GetRawPointer(Element* p) { return p; } 156e35fdd936d133bf8a48de140a3c666897588a05shiqian 157e35fdd936d133bf8a48de140a3c666897588a05shiqian// This comparator allows linked_ptr to be stored in sets. 158e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 159e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct LinkedPtrLessThan { 16016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan bool operator()(const ::testing::internal::linked_ptr<T>& lhs, 161e35fdd936d133bf8a48de140a3c666897588a05shiqian const ::testing::internal::linked_ptr<T>& rhs) const { 162e35fdd936d133bf8a48de140a3c666897588a05shiqian return lhs.get() < rhs.get(); 163e35fdd936d133bf8a48de140a3c666897588a05shiqian } 164e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 165e35fdd936d133bf8a48de140a3c666897588a05shiqian 166e35fdd936d133bf8a48de140a3c666897588a05shiqian// ImplicitlyConvertible<From, To>::value is a compile-time bool 167e35fdd936d133bf8a48de140a3c666897588a05shiqian// constant that's true iff type From can be implicitly converted to 168e35fdd936d133bf8a48de140a3c666897588a05shiqian// type To. 169e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename From, typename To> 170e35fdd936d133bf8a48de140a3c666897588a05shiqianclass ImplicitlyConvertible { 171e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 172e35fdd936d133bf8a48de140a3c666897588a05shiqian // We need the following helper functions only for their types. 173e35fdd936d133bf8a48de140a3c666897588a05shiqian // They have no implementations. 174e35fdd936d133bf8a48de140a3c666897588a05shiqian 175e35fdd936d133bf8a48de140a3c666897588a05shiqian // MakeFrom() is an expression whose type is From. We cannot simply 176e35fdd936d133bf8a48de140a3c666897588a05shiqian // use From(), as the type From may not have a public default 177e35fdd936d133bf8a48de140a3c666897588a05shiqian // constructor. 178e35fdd936d133bf8a48de140a3c666897588a05shiqian static From MakeFrom(); 179e35fdd936d133bf8a48de140a3c666897588a05shiqian 180e35fdd936d133bf8a48de140a3c666897588a05shiqian // These two functions are overloaded. Given an expression 181e35fdd936d133bf8a48de140a3c666897588a05shiqian // Helper(x), the compiler will pick the first version if x can be 182e35fdd936d133bf8a48de140a3c666897588a05shiqian // implicitly converted to type To; otherwise it will pick the 183e35fdd936d133bf8a48de140a3c666897588a05shiqian // second version. 184e35fdd936d133bf8a48de140a3c666897588a05shiqian // 185e35fdd936d133bf8a48de140a3c666897588a05shiqian // The first version returns a value of size 1, and the second 186e35fdd936d133bf8a48de140a3c666897588a05shiqian // version returns a value of size 2. Therefore, by checking the 187e35fdd936d133bf8a48de140a3c666897588a05shiqian // size of Helper(x), which can be done at compile time, we can tell 188e35fdd936d133bf8a48de140a3c666897588a05shiqian // which version of Helper() is used, and hence whether x can be 189e35fdd936d133bf8a48de140a3c666897588a05shiqian // implicitly converted to type To. 190e35fdd936d133bf8a48de140a3c666897588a05shiqian static char Helper(To); 191e35fdd936d133bf8a48de140a3c666897588a05shiqian static char (&Helper(...))[2]; // NOLINT 192e35fdd936d133bf8a48de140a3c666897588a05shiqian 193e35fdd936d133bf8a48de140a3c666897588a05shiqian // We have to put the 'public' section after the 'private' section, 194e35fdd936d133bf8a48de140a3c666897588a05shiqian // or MSVC refuses to compile the code. 195e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 196e35fdd936d133bf8a48de140a3c666897588a05shiqian // MSVC warns about implicitly converting from double to int for 197e35fdd936d133bf8a48de140a3c666897588a05shiqian // possible loss of data, so we need to temporarily disable the 198e35fdd936d133bf8a48de140a3c666897588a05shiqian // warning. 199e35fdd936d133bf8a48de140a3c666897588a05shiqian#ifdef _MSC_VER 200e35fdd936d133bf8a48de140a3c666897588a05shiqian#pragma warning(push) // Saves the current warning state. 201e35fdd936d133bf8a48de140a3c666897588a05shiqian#pragma warning(disable:4244) // Temporarily disables warning 4244. 202e35fdd936d133bf8a48de140a3c666897588a05shiqian static const bool value = 203e35fdd936d133bf8a48de140a3c666897588a05shiqian sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1; 204e35fdd936d133bf8a48de140a3c666897588a05shiqian#pragma warning(pop) // Restores the warning state. 205e35fdd936d133bf8a48de140a3c666897588a05shiqian#else 206e35fdd936d133bf8a48de140a3c666897588a05shiqian static const bool value = 207e35fdd936d133bf8a48de140a3c666897588a05shiqian sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1; 208e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // _MSV_VER 209e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 210e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename From, typename To> 211e35fdd936d133bf8a48de140a3c666897588a05shiqianconst bool ImplicitlyConvertible<From, To>::value; 212e35fdd936d133bf8a48de140a3c666897588a05shiqian 21316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// In what follows, we use the term "kind" to indicate whether a type 21416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// is bool, an integer type (excluding bool), a floating-point type, 21516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// or none of them. This categorization is useful for determining 21616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// when a matcher argument type can be safely converted to another 21716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// type in the implementation of SafeMatcherCast. 21816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanenum TypeKind { 21916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan kBool, kInteger, kFloatingPoint, kOther 22016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan}; 22116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 22216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// KindOf<T>::value is the kind of type T. 22316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename T> struct KindOf { 22416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan enum { value = kOther }; // The default kind. 22516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan}; 22616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 22716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// This macro declares that the kind of 'type' is 'kind'. 22816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#define GMOCK_DECLARE_KIND_(type, kind) \ 22916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan template <> struct KindOf<type> { enum { value = kind }; } 23016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 23116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(bool, kBool); 23216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 23316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// All standard integer types. 23416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(char, kInteger); 23516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(signed char, kInteger); 23616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(unsigned char, kInteger); 23716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(short, kInteger); // NOLINT 23816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(unsigned short, kInteger); // NOLINT 23916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(int, kInteger); 24016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(unsigned int, kInteger); 24116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(long, kInteger); // NOLINT 24216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(unsigned long, kInteger); // NOLINT 24316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 24416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// MSVC can be configured to define wchar_t as a typedef of unsigned 24516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// short. It defines _NATIVE_WCHAR_T_DEFINED symbol when wchar_t is a 24616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// native type. 24716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) 24816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(wchar_t, kInteger); 24916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#endif 25016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 25116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Non-standard integer types. 25216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(Int64, kInteger); 25316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(UInt64, kInteger); 25416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 25516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// All standard floating-point types. 25616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(float, kFloatingPoint); 25716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(double, kFloatingPoint); 25816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(long double, kFloatingPoint); 25916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 26016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#undef GMOCK_DECLARE_KIND_ 26116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 26216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Evaluates to the kind of 'type'. 26316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#define GMOCK_KIND_OF_(type) \ 26416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan static_cast< ::testing::internal::TypeKind>( \ 26516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan ::testing::internal::KindOf<type>::value) 26616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 26716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Evaluates to true iff integer type T is signed. 26816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#define GMOCK_IS_SIGNED_(T) (static_cast<T>(-1) < 0) 26916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 27016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// LosslessArithmeticConvertibleImpl<kFromKind, From, kToKind, To>::value 27116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// is true iff arithmetic type From can be losslessly converted to 27216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// arithmetic type To. 27316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// 27416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// It's the user's responsibility to ensure that both From and To are 27516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// raw (i.e. has no CV modifier, is not a pointer, and is not a 27616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// reference) built-in arithmetic types, kFromKind is the kind of 27716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// From, and kToKind is the kind of To; the value is 27816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// implementation-defined when the above pre-condition is violated. 27916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <TypeKind kFromKind, typename From, TypeKind kToKind, typename To> 28016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl : public false_type {}; 28116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 28216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting bool to bool is lossless. 28316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <> 28416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kBool, bool, kBool, bool> 28516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public true_type {}; // NOLINT 28616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 28716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting bool to any integer type is lossless. 28816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename To> 28916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kBool, bool, kInteger, To> 29016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public true_type {}; // NOLINT 29116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 29216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting bool to any floating-point type is lossless. 29316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename To> 29416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kBool, bool, kFloatingPoint, To> 29516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public true_type {}; // NOLINT 29616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 29716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting an integer to bool is lossy. 29816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From> 29916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kInteger, From, kBool, bool> 30016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public false_type {}; // NOLINT 30116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 30216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting an integer to another non-bool integer is lossless iff 30316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// the target type's range encloses the source type's range. 30416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From, typename To> 30516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kInteger, From, kInteger, To> 30616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public bool_constant< 30716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // When converting from a smaller size to a larger size, we are 30816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // fine as long as we are not converting from signed to unsigned. 30916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan ((sizeof(From) < sizeof(To)) && 31016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan (!GMOCK_IS_SIGNED_(From) || GMOCK_IS_SIGNED_(To))) || 31116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // When converting between the same size, the signedness must match. 31216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan ((sizeof(From) == sizeof(To)) && 31316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan (GMOCK_IS_SIGNED_(From) == GMOCK_IS_SIGNED_(To)))> {}; // NOLINT 31416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 31516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#undef GMOCK_IS_SIGNED_ 31616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 31716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting an integer to a floating-point type may be lossy, since 31816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// the format of a floating-point number is implementation-defined. 31916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From, typename To> 32016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kInteger, From, kFloatingPoint, To> 32116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public false_type {}; // NOLINT 32216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 32316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting a floating-point to bool is lossy. 32416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From> 32516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kBool, bool> 32616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public false_type {}; // NOLINT 32716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 32816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting a floating-point to an integer is lossy. 32916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From, typename To> 33016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kInteger, To> 33116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public false_type {}; // NOLINT 33216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 33316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting a floating-point to another floating-point is lossless 33416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// iff the target type is at least as big as the source type. 33516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From, typename To> 33616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl< 33716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan kFloatingPoint, From, kFloatingPoint, To> 33816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public bool_constant<sizeof(From) <= sizeof(To)> {}; // NOLINT 33916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 34016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// LosslessArithmeticConvertible<From, To>::value is true iff arithmetic 34116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// type From can be losslessly converted to arithmetic type To. 34216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// 34316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// It's the user's responsibility to ensure that both From and To are 34416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// raw (i.e. has no CV modifier, is not a pointer, and is not a 34516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// reference) built-in arithmetic types; the value is 34616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// implementation-defined when the above pre-condition is violated. 34716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From, typename To> 34816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertible 34916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public LosslessArithmeticConvertibleImpl< 35016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan GMOCK_KIND_OF_(From), From, GMOCK_KIND_OF_(To), To> {}; // NOLINT 35116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 352e35fdd936d133bf8a48de140a3c666897588a05shiqian// IsAProtocolMessage<T>::value is a compile-time bool constant that's 353e35fdd936d133bf8a48de140a3c666897588a05shiqian// true iff T is type ProtocolMessage, proto2::Message, or a subclass 354e35fdd936d133bf8a48de140a3c666897588a05shiqian// of those. 355e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 35616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct IsAProtocolMessage 35716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public bool_constant< 35816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan ImplicitlyConvertible<const T*, const ::ProtocolMessage*>::value || 35916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan ImplicitlyConvertible<const T*, const ::proto2::Message*>::value> { 360e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 361e35fdd936d133bf8a48de140a3c666897588a05shiqian 362e35fdd936d133bf8a48de140a3c666897588a05shiqian// When the compiler sees expression IsContainerTest<C>(0), the first 363e35fdd936d133bf8a48de140a3c666897588a05shiqian// overload of IsContainerTest will be picked if C is an STL-style 364e35fdd936d133bf8a48de140a3c666897588a05shiqian// container class (since C::const_iterator* is a valid type and 0 can 365e35fdd936d133bf8a48de140a3c666897588a05shiqian// be converted to it), while the second overload will be picked 366e35fdd936d133bf8a48de140a3c666897588a05shiqian// otherwise (since C::const_iterator will be an invalid type in this 367e35fdd936d133bf8a48de140a3c666897588a05shiqian// case). Therefore, we can determine whether C is a container class 368e35fdd936d133bf8a48de140a3c666897588a05shiqian// by checking the type of IsContainerTest<C>(0). The value of the 369e35fdd936d133bf8a48de140a3c666897588a05shiqian// expression is insignificant. 370e35fdd936d133bf8a48de140a3c666897588a05shiqiantypedef int IsContainer; 371e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <class C> 372e35fdd936d133bf8a48de140a3c666897588a05shiqianIsContainer IsContainerTest(typename C::const_iterator*) { return 0; } 373e35fdd936d133bf8a48de140a3c666897588a05shiqian 374e35fdd936d133bf8a48de140a3c666897588a05shiqiantypedef char IsNotContainer; 375e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <class C> 376e35fdd936d133bf8a48de140a3c666897588a05shiqianIsNotContainer IsContainerTest(...) { return '\0'; } 377e35fdd936d133bf8a48de140a3c666897588a05shiqian 378e35fdd936d133bf8a48de140a3c666897588a05shiqian// This interface knows how to report a Google Mock failure (either 379e35fdd936d133bf8a48de140a3c666897588a05shiqian// non-fatal or fatal). 380e35fdd936d133bf8a48de140a3c666897588a05shiqianclass FailureReporterInterface { 381e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 382e35fdd936d133bf8a48de140a3c666897588a05shiqian // The type of a failure (either non-fatal or fatal). 383e35fdd936d133bf8a48de140a3c666897588a05shiqian enum FailureType { 384e35fdd936d133bf8a48de140a3c666897588a05shiqian NONFATAL, FATAL 385e35fdd936d133bf8a48de140a3c666897588a05shiqian }; 386e35fdd936d133bf8a48de140a3c666897588a05shiqian 387e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual ~FailureReporterInterface() {} 388e35fdd936d133bf8a48de140a3c666897588a05shiqian 389e35fdd936d133bf8a48de140a3c666897588a05shiqian // Reports a failure that occurred at the given source file location. 390e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void ReportFailure(FailureType type, const char* file, int line, 391e35fdd936d133bf8a48de140a3c666897588a05shiqian const string& message) = 0; 392e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 393e35fdd936d133bf8a48de140a3c666897588a05shiqian 394e35fdd936d133bf8a48de140a3c666897588a05shiqian// Returns the failure reporter used by Google Mock. 395e35fdd936d133bf8a48de140a3c666897588a05shiqianFailureReporterInterface* GetFailureReporter(); 396e35fdd936d133bf8a48de140a3c666897588a05shiqian 397e35fdd936d133bf8a48de140a3c666897588a05shiqian// Asserts that condition is true; aborts the process with the given 398e35fdd936d133bf8a48de140a3c666897588a05shiqian// message if condition is false. We cannot use LOG(FATAL) or CHECK() 399e35fdd936d133bf8a48de140a3c666897588a05shiqian// as Google Mock might be used to mock the log sink itself. We 400e35fdd936d133bf8a48de140a3c666897588a05shiqian// inline this function to prevent it from showing up in the stack 401e35fdd936d133bf8a48de140a3c666897588a05shiqian// trace. 402e35fdd936d133bf8a48de140a3c666897588a05shiqianinline void Assert(bool condition, const char* file, int line, 403e35fdd936d133bf8a48de140a3c666897588a05shiqian const string& msg) { 404e35fdd936d133bf8a48de140a3c666897588a05shiqian if (!condition) { 405e35fdd936d133bf8a48de140a3c666897588a05shiqian GetFailureReporter()->ReportFailure(FailureReporterInterface::FATAL, 406e35fdd936d133bf8a48de140a3c666897588a05shiqian file, line, msg); 407e35fdd936d133bf8a48de140a3c666897588a05shiqian } 408e35fdd936d133bf8a48de140a3c666897588a05shiqian} 409e35fdd936d133bf8a48de140a3c666897588a05shiqianinline void Assert(bool condition, const char* file, int line) { 410e35fdd936d133bf8a48de140a3c666897588a05shiqian Assert(condition, file, line, "Assertion failed."); 411e35fdd936d133bf8a48de140a3c666897588a05shiqian} 412e35fdd936d133bf8a48de140a3c666897588a05shiqian 413e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that condition is true; generates a non-fatal failure if 414e35fdd936d133bf8a48de140a3c666897588a05shiqian// condition is false. 415e35fdd936d133bf8a48de140a3c666897588a05shiqianinline void Expect(bool condition, const char* file, int line, 416e35fdd936d133bf8a48de140a3c666897588a05shiqian const string& msg) { 417e35fdd936d133bf8a48de140a3c666897588a05shiqian if (!condition) { 418e35fdd936d133bf8a48de140a3c666897588a05shiqian GetFailureReporter()->ReportFailure(FailureReporterInterface::NONFATAL, 419e35fdd936d133bf8a48de140a3c666897588a05shiqian file, line, msg); 420e35fdd936d133bf8a48de140a3c666897588a05shiqian } 421e35fdd936d133bf8a48de140a3c666897588a05shiqian} 422e35fdd936d133bf8a48de140a3c666897588a05shiqianinline void Expect(bool condition, const char* file, int line) { 423e35fdd936d133bf8a48de140a3c666897588a05shiqian Expect(condition, file, line, "Expectation failed."); 424e35fdd936d133bf8a48de140a3c666897588a05shiqian} 425e35fdd936d133bf8a48de140a3c666897588a05shiqian 426e35fdd936d133bf8a48de140a3c666897588a05shiqian// Severity level of a log. 427e35fdd936d133bf8a48de140a3c666897588a05shiqianenum LogSeverity { 428e35fdd936d133bf8a48de140a3c666897588a05shiqian INFO = 0, 429e35fdd936d133bf8a48de140a3c666897588a05shiqian WARNING = 1, 430e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 431e35fdd936d133bf8a48de140a3c666897588a05shiqian 432e35fdd936d133bf8a48de140a3c666897588a05shiqian// Valid values for the --gmock_verbose flag. 433e35fdd936d133bf8a48de140a3c666897588a05shiqian 434e35fdd936d133bf8a48de140a3c666897588a05shiqian// All logs (informational and warnings) are printed. 435e35fdd936d133bf8a48de140a3c666897588a05shiqianconst char kInfoVerbosity[] = "info"; 436e35fdd936d133bf8a48de140a3c666897588a05shiqian// Only warnings are printed. 437e35fdd936d133bf8a48de140a3c666897588a05shiqianconst char kWarningVerbosity[] = "warning"; 438e35fdd936d133bf8a48de140a3c666897588a05shiqian// No logs are printed. 439e35fdd936d133bf8a48de140a3c666897588a05shiqianconst char kErrorVerbosity[] = "error"; 440e35fdd936d133bf8a48de140a3c666897588a05shiqian 441e35fdd936d133bf8a48de140a3c666897588a05shiqian// Prints the given message to stdout iff 'severity' >= the level 442e35fdd936d133bf8a48de140a3c666897588a05shiqian// specified by the --gmock_verbose flag. If stack_frames_to_skip >= 443e35fdd936d133bf8a48de140a3c666897588a05shiqian// 0, also prints the stack trace excluding the top 444e35fdd936d133bf8a48de140a3c666897588a05shiqian// stack_frames_to_skip frames. In opt mode, any positive 445e35fdd936d133bf8a48de140a3c666897588a05shiqian// stack_frames_to_skip is treated as 0, since we don't know which 446e35fdd936d133bf8a48de140a3c666897588a05shiqian// function calls will be inlined by the compiler and need to be 447e35fdd936d133bf8a48de140a3c666897588a05shiqian// conservative. 448e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid Log(LogSeverity severity, const string& message, int stack_frames_to_skip); 449e35fdd936d133bf8a48de140a3c666897588a05shiqian 450e35fdd936d133bf8a48de140a3c666897588a05shiqian// The universal value printer (public/gmock-printers.h) needs this 451e35fdd936d133bf8a48de140a3c666897588a05shiqian// to declare an unused << operator in the global namespace. 452e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct Unused {}; 453e35fdd936d133bf8a48de140a3c666897588a05shiqian 45416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// TODO(wan@google.com): group all type utilities together. 45516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 456e35fdd936d133bf8a48de140a3c666897588a05shiqian// Type traits. 457e35fdd936d133bf8a48de140a3c666897588a05shiqian 458e35fdd936d133bf8a48de140a3c666897588a05shiqian// is_reference<T>::value is non-zero iff T is a reference type. 459e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> struct is_reference : public false_type {}; 460e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> struct is_reference<T&> : public true_type {}; 461e35fdd936d133bf8a48de140a3c666897588a05shiqian 462e35fdd936d133bf8a48de140a3c666897588a05shiqian// type_equals<T1, T2>::value is non-zero iff T1 and T2 are the same type. 463e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T1, typename T2> struct type_equals : public false_type {}; 464e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> struct type_equals<T, T> : public true_type {}; 465e35fdd936d133bf8a48de140a3c666897588a05shiqian 466e35fdd936d133bf8a48de140a3c666897588a05shiqian// remove_reference<T>::type removes the reference from type T, if any. 46716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename T> struct remove_reference { typedef T type; }; // NOLINT 46816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename T> struct remove_reference<T&> { typedef T type; }; // NOLINT 469e35fdd936d133bf8a48de140a3c666897588a05shiqian 470e35fdd936d133bf8a48de140a3c666897588a05shiqian// Invalid<T>() returns an invalid value of type T. This is useful 471e35fdd936d133bf8a48de140a3c666897588a05shiqian// when a value of type T is needed for compilation, but the statement 472e35fdd936d133bf8a48de140a3c666897588a05shiqian// will not really be executed (or we don't care if the statement 473e35fdd936d133bf8a48de140a3c666897588a05shiqian// crashes). 474e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 475e35fdd936d133bf8a48de140a3c666897588a05shiqianinline T Invalid() { 476e35fdd936d133bf8a48de140a3c666897588a05shiqian return *static_cast<typename remove_reference<T>::type*>(NULL); 477e35fdd936d133bf8a48de140a3c666897588a05shiqian} 478e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <> 479e35fdd936d133bf8a48de140a3c666897588a05shiqianinline void Invalid<void>() {} 480e35fdd936d133bf8a48de140a3c666897588a05shiqian 481e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace internal 482e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace testing 483e35fdd936d133bf8a48de140a3c666897588a05shiqian 484e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_ 485