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 4553e08c44dd34857ba57581d7c5774d6c96a8d0e1zhanyong.wan#include "gmock/internal/gmock-generated-internal-utils.h" 4653e08c44dd34857ba57581d7c5774d6c96a8d0e1zhanyong.wan#include "gmock/internal/gmock-port.h" 4753e08c44dd34857ba57581d7c5774d6c96a8d0e1zhanyong.wan#include "gtest/gtest.h" 48e35fdd936d133bf8a48de140a3c666897588a05shiqian 49e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace testing { 50e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace internal { 51e35fdd936d133bf8a48de140a3c666897588a05shiqian 52ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan// Converts an identifier name to a space-separated list of lower-case 53ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan// words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is 54ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan// treated as one word. For example, both "FooBar123" and 55ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan// "foo_bar_123" are converted to "foo bar 123". 56587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosevGTEST_API_ string ConvertIdentifierNameToWords(const char* id_name); 57ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan 58e35fdd936d133bf8a48de140a3c666897588a05shiqian// PointeeOf<Pointer>::type is the type of a value pointed to by a 59e35fdd936d133bf8a48de140a3c666897588a05shiqian// Pointer, which can be either a smart pointer or a raw pointer. The 60e35fdd936d133bf8a48de140a3c666897588a05shiqian// following default implementation is for the case where Pointer is a 61e35fdd936d133bf8a48de140a3c666897588a05shiqian// smart pointer. 62e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Pointer> 63e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct PointeeOf { 64e35fdd936d133bf8a48de140a3c666897588a05shiqian // Smart pointer classes define type element_type as the type of 65e35fdd936d133bf8a48de140a3c666897588a05shiqian // their pointees. 66e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename Pointer::element_type type; 67e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 68e35fdd936d133bf8a48de140a3c666897588a05shiqian// This specialization is for the raw pointer case. 69e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 70e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct PointeeOf<T*> { typedef T type; }; // NOLINT 71e35fdd936d133bf8a48de140a3c666897588a05shiqian 72e35fdd936d133bf8a48de140a3c666897588a05shiqian// GetRawPointer(p) returns the raw pointer underlying p when p is a 73e35fdd936d133bf8a48de140a3c666897588a05shiqian// smart pointer, or returns p itself when p is already a raw pointer. 74e35fdd936d133bf8a48de140a3c666897588a05shiqian// The following default implementation is for the smart pointer case. 75e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Pointer> 76ada23475e27babd85fb9c13250243f6acfd3ffd8vladlosevinline const typename Pointer::element_type* GetRawPointer(const Pointer& p) { 77e35fdd936d133bf8a48de140a3c666897588a05shiqian return p.get(); 78e35fdd936d133bf8a48de140a3c666897588a05shiqian} 79e35fdd936d133bf8a48de140a3c666897588a05shiqian// This overloaded version is for the raw pointer case. 80e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Element> 81e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Element* GetRawPointer(Element* p) { return p; } 82e35fdd936d133bf8a48de140a3c666897588a05shiqian 83e35fdd936d133bf8a48de140a3c666897588a05shiqian// This comparator allows linked_ptr to be stored in sets. 84e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 85e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct LinkedPtrLessThan { 8616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan bool operator()(const ::testing::internal::linked_ptr<T>& lhs, 87e35fdd936d133bf8a48de140a3c666897588a05shiqian const ::testing::internal::linked_ptr<T>& rhs) const { 88e35fdd936d133bf8a48de140a3c666897588a05shiqian return lhs.get() < rhs.get(); 89e35fdd936d133bf8a48de140a3c666897588a05shiqian } 90e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 91e35fdd936d133bf8a48de140a3c666897588a05shiqian 9295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// Symbian compilation can be done with wchar_t being either a native 9395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// type or a typedef. Using Google Mock with OpenC without wchar_t 9495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// should require the definition of _STLP_NO_WCHAR_T. 9595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// 9695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// MSVC treats wchar_t as a native type usually, but treats it as the 9795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// same as unsigned short when the compiler option /Zc:wchar_t- is 9895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// specified. It defines _NATIVE_WCHAR_T_DEFINED symbol when wchar_t 9995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// is a native type. 10095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#if (GTEST_OS_SYMBIAN && defined(_STLP_NO_WCHAR_T)) || \ 10195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan (defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED)) 10295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// wchar_t is a typedef. 10395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#else 104658ac0b71a350cc833ee4520536b6c4964c7b944zhanyong.wan# define GMOCK_WCHAR_T_IS_NATIVE_ 1 10595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#endif 10695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan 10795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// signed wchar_t and unsigned wchar_t are NOT in the C++ standard. 10895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// Using them is a bad practice and not portable. So DON'T use them. 10995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// 11095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// Still, Google Mock is designed to work even if the user uses signed 11195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// wchar_t or unsigned wchar_t (obviously, assuming the compiler 11295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// supports them). 11395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// 11495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// To gcc, 11595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// wchar_t == signed wchar_t != unsigned wchar_t == unsigned int 11695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#ifdef __GNUC__ 117658ac0b71a350cc833ee4520536b6c4964c7b944zhanyong.wan// signed/unsigned wchar_t are valid types. 118658ac0b71a350cc833ee4520536b6c4964c7b944zhanyong.wan# define GMOCK_HAS_SIGNED_WCHAR_T_ 1 11995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#endif 12095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan 12116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// In what follows, we use the term "kind" to indicate whether a type 12216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// is bool, an integer type (excluding bool), a floating-point type, 12316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// or none of them. This categorization is useful for determining 12416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// when a matcher argument type can be safely converted to another 12516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// type in the implementation of SafeMatcherCast. 12616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanenum TypeKind { 12716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan kBool, kInteger, kFloatingPoint, kOther 12816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan}; 12916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 13016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// KindOf<T>::value is the kind of type T. 13116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename T> struct KindOf { 13216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan enum { value = kOther }; // The default kind. 13316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan}; 13416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 13516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// This macro declares that the kind of 'type' is 'kind'. 13616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#define GMOCK_DECLARE_KIND_(type, kind) \ 13716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan template <> struct KindOf<type> { enum { value = kind }; } 13816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 13916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(bool, kBool); 14016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 14116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// All standard integer types. 14216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(char, kInteger); 14316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(signed char, kInteger); 14416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(unsigned char, kInteger); 14516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(short, kInteger); // NOLINT 14616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(unsigned short, kInteger); // NOLINT 14716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(int, kInteger); 14816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(unsigned int, kInteger); 14916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(long, kInteger); // NOLINT 15016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(unsigned long, kInteger); // NOLINT 15116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 15295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#if GMOCK_WCHAR_T_IS_NATIVE_ 15316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(wchar_t, kInteger); 15416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#endif 15516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 15616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Non-standard integer types. 15716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(Int64, kInteger); 15816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(UInt64, kInteger); 15916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 16016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// All standard floating-point types. 16116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(float, kFloatingPoint); 16216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(double, kFloatingPoint); 16316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanGMOCK_DECLARE_KIND_(long double, kFloatingPoint); 16416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 16516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#undef GMOCK_DECLARE_KIND_ 16616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 16716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Evaluates to the kind of 'type'. 16816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#define GMOCK_KIND_OF_(type) \ 16916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan static_cast< ::testing::internal::TypeKind>( \ 17016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan ::testing::internal::KindOf<type>::value) 17116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 17216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Evaluates to true iff integer type T is signed. 17316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#define GMOCK_IS_SIGNED_(T) (static_cast<T>(-1) < 0) 17416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 17516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// LosslessArithmeticConvertibleImpl<kFromKind, From, kToKind, To>::value 17616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// is true iff arithmetic type From can be losslessly converted to 17716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// arithmetic type To. 17816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// 17916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// It's the user's responsibility to ensure that both From and To are 18016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// raw (i.e. has no CV modifier, is not a pointer, and is not a 18116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// reference) built-in arithmetic types, kFromKind is the kind of 18216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// From, and kToKind is the kind of To; the value is 18316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// implementation-defined when the above pre-condition is violated. 18416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <TypeKind kFromKind, typename From, TypeKind kToKind, typename To> 18516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl : public false_type {}; 18616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 18716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting bool to bool is lossless. 18816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <> 18916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kBool, bool, kBool, bool> 19016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public true_type {}; // NOLINT 19116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 19216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting bool to any integer type is lossless. 19316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename To> 19416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kBool, bool, kInteger, To> 19516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public true_type {}; // NOLINT 19616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 19716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting bool to any floating-point type is lossless. 19816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename To> 19916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kBool, bool, kFloatingPoint, To> 20016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public true_type {}; // NOLINT 20116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 20216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting an integer to bool is lossy. 20316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From> 20416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kInteger, From, kBool, bool> 20516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public false_type {}; // NOLINT 20616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 20716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting an integer to another non-bool integer is lossless iff 20816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// the target type's range encloses the source type's range. 20916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From, typename To> 21016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kInteger, From, kInteger, To> 21116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public bool_constant< 21216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // When converting from a smaller size to a larger size, we are 21316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // fine as long as we are not converting from signed to unsigned. 21416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan ((sizeof(From) < sizeof(To)) && 21516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan (!GMOCK_IS_SIGNED_(From) || GMOCK_IS_SIGNED_(To))) || 21616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // When converting between the same size, the signedness must match. 21716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan ((sizeof(From) == sizeof(To)) && 21816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan (GMOCK_IS_SIGNED_(From) == GMOCK_IS_SIGNED_(To)))> {}; // NOLINT 21916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 22016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#undef GMOCK_IS_SIGNED_ 22116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 22216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting an integer to a floating-point type may be lossy, since 22316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// the format of a floating-point number is implementation-defined. 22416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From, typename To> 22516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kInteger, From, kFloatingPoint, To> 22616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public false_type {}; // NOLINT 22716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 22816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting a floating-point to bool is lossy. 22916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From> 23016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kBool, bool> 23116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public false_type {}; // NOLINT 23216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 23316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting a floating-point to an integer is lossy. 23416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From, typename To> 23516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kInteger, To> 23616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public false_type {}; // NOLINT 23716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 23816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Converting a floating-point to another floating-point is lossless 23916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// iff the target type is at least as big as the source type. 24016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From, typename To> 24116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertibleImpl< 24216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan kFloatingPoint, From, kFloatingPoint, To> 24316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public bool_constant<sizeof(From) <= sizeof(To)> {}; // NOLINT 24416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 24516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// LosslessArithmeticConvertible<From, To>::value is true iff arithmetic 24616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// type From can be losslessly converted to arithmetic type To. 24716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// 24816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// It's the user's responsibility to ensure that both From and To are 24916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// raw (i.e. has no CV modifier, is not a pointer, and is not a 25016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// reference) built-in arithmetic types; the value is 25116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// implementation-defined when the above pre-condition is violated. 25216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename From, typename To> 25316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanstruct LosslessArithmeticConvertible 25416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan : public LosslessArithmeticConvertibleImpl< 25516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan GMOCK_KIND_OF_(From), From, GMOCK_KIND_OF_(To), To> {}; // NOLINT 25616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 257e35fdd936d133bf8a48de140a3c666897588a05shiqian// This interface knows how to report a Google Mock failure (either 258e35fdd936d133bf8a48de140a3c666897588a05shiqian// non-fatal or fatal). 259e35fdd936d133bf8a48de140a3c666897588a05shiqianclass FailureReporterInterface { 260e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 261e35fdd936d133bf8a48de140a3c666897588a05shiqian // The type of a failure (either non-fatal or fatal). 262e35fdd936d133bf8a48de140a3c666897588a05shiqian enum FailureType { 2632fd619edd3d1ec053f6276debdb513f1122ebcf3zhanyong.wan kNonfatal, kFatal 264e35fdd936d133bf8a48de140a3c666897588a05shiqian }; 265e35fdd936d133bf8a48de140a3c666897588a05shiqian 266e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual ~FailureReporterInterface() {} 267e35fdd936d133bf8a48de140a3c666897588a05shiqian 268e35fdd936d133bf8a48de140a3c666897588a05shiqian // Reports a failure that occurred at the given source file location. 269e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void ReportFailure(FailureType type, const char* file, int line, 270e35fdd936d133bf8a48de140a3c666897588a05shiqian const string& message) = 0; 271e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 272e35fdd936d133bf8a48de140a3c666897588a05shiqian 273e35fdd936d133bf8a48de140a3c666897588a05shiqian// Returns the failure reporter used by Google Mock. 274587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosevGTEST_API_ FailureReporterInterface* GetFailureReporter(); 275e35fdd936d133bf8a48de140a3c666897588a05shiqian 276e35fdd936d133bf8a48de140a3c666897588a05shiqian// Asserts that condition is true; aborts the process with the given 277e35fdd936d133bf8a48de140a3c666897588a05shiqian// message if condition is false. We cannot use LOG(FATAL) or CHECK() 278e35fdd936d133bf8a48de140a3c666897588a05shiqian// as Google Mock might be used to mock the log sink itself. We 279e35fdd936d133bf8a48de140a3c666897588a05shiqian// inline this function to prevent it from showing up in the stack 280e35fdd936d133bf8a48de140a3c666897588a05shiqian// trace. 281e35fdd936d133bf8a48de140a3c666897588a05shiqianinline void Assert(bool condition, const char* file, int line, 282e35fdd936d133bf8a48de140a3c666897588a05shiqian const string& msg) { 283e35fdd936d133bf8a48de140a3c666897588a05shiqian if (!condition) { 2842fd619edd3d1ec053f6276debdb513f1122ebcf3zhanyong.wan GetFailureReporter()->ReportFailure(FailureReporterInterface::kFatal, 285e35fdd936d133bf8a48de140a3c666897588a05shiqian file, line, msg); 286e35fdd936d133bf8a48de140a3c666897588a05shiqian } 287e35fdd936d133bf8a48de140a3c666897588a05shiqian} 288e35fdd936d133bf8a48de140a3c666897588a05shiqianinline void Assert(bool condition, const char* file, int line) { 289e35fdd936d133bf8a48de140a3c666897588a05shiqian Assert(condition, file, line, "Assertion failed."); 290e35fdd936d133bf8a48de140a3c666897588a05shiqian} 291e35fdd936d133bf8a48de140a3c666897588a05shiqian 292e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that condition is true; generates a non-fatal failure if 293e35fdd936d133bf8a48de140a3c666897588a05shiqian// condition is false. 294e35fdd936d133bf8a48de140a3c666897588a05shiqianinline void Expect(bool condition, const char* file, int line, 295e35fdd936d133bf8a48de140a3c666897588a05shiqian const string& msg) { 296e35fdd936d133bf8a48de140a3c666897588a05shiqian if (!condition) { 2972fd619edd3d1ec053f6276debdb513f1122ebcf3zhanyong.wan GetFailureReporter()->ReportFailure(FailureReporterInterface::kNonfatal, 298e35fdd936d133bf8a48de140a3c666897588a05shiqian file, line, msg); 299e35fdd936d133bf8a48de140a3c666897588a05shiqian } 300e35fdd936d133bf8a48de140a3c666897588a05shiqian} 301e35fdd936d133bf8a48de140a3c666897588a05shiqianinline void Expect(bool condition, const char* file, int line) { 302e35fdd936d133bf8a48de140a3c666897588a05shiqian Expect(condition, file, line, "Expectation failed."); 303e35fdd936d133bf8a48de140a3c666897588a05shiqian} 304e35fdd936d133bf8a48de140a3c666897588a05shiqian 305e35fdd936d133bf8a48de140a3c666897588a05shiqian// Severity level of a log. 306e35fdd936d133bf8a48de140a3c666897588a05shiqianenum LogSeverity { 3072fd619edd3d1ec053f6276debdb513f1122ebcf3zhanyong.wan kInfo = 0, 3082fd619edd3d1ec053f6276debdb513f1122ebcf3zhanyong.wan kWarning = 1 309e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 310e35fdd936d133bf8a48de140a3c666897588a05shiqian 311e35fdd936d133bf8a48de140a3c666897588a05shiqian// Valid values for the --gmock_verbose flag. 312e35fdd936d133bf8a48de140a3c666897588a05shiqian 313e35fdd936d133bf8a48de140a3c666897588a05shiqian// All logs (informational and warnings) are printed. 314e35fdd936d133bf8a48de140a3c666897588a05shiqianconst char kInfoVerbosity[] = "info"; 315e35fdd936d133bf8a48de140a3c666897588a05shiqian// Only warnings are printed. 316e35fdd936d133bf8a48de140a3c666897588a05shiqianconst char kWarningVerbosity[] = "warning"; 317e35fdd936d133bf8a48de140a3c666897588a05shiqian// No logs are printed. 318e35fdd936d133bf8a48de140a3c666897588a05shiqianconst char kErrorVerbosity[] = "error"; 319e35fdd936d133bf8a48de140a3c666897588a05shiqian 3209413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan// Returns true iff a log with the given severity is visible according 3219413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan// to the --gmock_verbose flag. 322587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosevGTEST_API_ bool LogIsVisible(LogSeverity severity); 3239413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan 324e35fdd936d133bf8a48de140a3c666897588a05shiqian// Prints the given message to stdout iff 'severity' >= the level 325e35fdd936d133bf8a48de140a3c666897588a05shiqian// specified by the --gmock_verbose flag. If stack_frames_to_skip >= 326e35fdd936d133bf8a48de140a3c666897588a05shiqian// 0, also prints the stack trace excluding the top 327e35fdd936d133bf8a48de140a3c666897588a05shiqian// stack_frames_to_skip frames. In opt mode, any positive 328e35fdd936d133bf8a48de140a3c666897588a05shiqian// stack_frames_to_skip is treated as 0, since we don't know which 329e35fdd936d133bf8a48de140a3c666897588a05shiqian// function calls will be inlined by the compiler and need to be 330e35fdd936d133bf8a48de140a3c666897588a05shiqian// conservative. 331587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosevGTEST_API_ void Log(LogSeverity severity, 332587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosev const string& message, 333587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosev int stack_frames_to_skip); 334e35fdd936d133bf8a48de140a3c666897588a05shiqian 33516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// TODO(wan@google.com): group all type utilities together. 33616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 337e35fdd936d133bf8a48de140a3c666897588a05shiqian// Type traits. 338e35fdd936d133bf8a48de140a3c666897588a05shiqian 339e35fdd936d133bf8a48de140a3c666897588a05shiqian// is_reference<T>::value is non-zero iff T is a reference type. 340e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> struct is_reference : public false_type {}; 341e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> struct is_reference<T&> : public true_type {}; 342e35fdd936d133bf8a48de140a3c666897588a05shiqian 343e35fdd936d133bf8a48de140a3c666897588a05shiqian// type_equals<T1, T2>::value is non-zero iff T1 and T2 are the same type. 344e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T1, typename T2> struct type_equals : public false_type {}; 345e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> struct type_equals<T, T> : public true_type {}; 346e35fdd936d133bf8a48de140a3c666897588a05shiqian 347e35fdd936d133bf8a48de140a3c666897588a05shiqian// remove_reference<T>::type removes the reference from type T, if any. 34816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename T> struct remove_reference { typedef T type; }; // NOLINT 34916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wantemplate <typename T> struct remove_reference<T&> { typedef T type; }; // NOLINT 350e35fdd936d133bf8a48de140a3c666897588a05shiqian 35138513a8bb154f0b6d0a4088814fe92552696d465jgm// DecayArray<T>::type turns an array type U[N] to const U* and preserves 35238513a8bb154f0b6d0a4088814fe92552696d465jgm// other types. Useful for saving a copy of a function argument. 35338513a8bb154f0b6d0a4088814fe92552696d465jgmtemplate <typename T> struct DecayArray { typedef T type; }; // NOLINT 35438513a8bb154f0b6d0a4088814fe92552696d465jgmtemplate <typename T, size_t N> struct DecayArray<T[N]> { 35538513a8bb154f0b6d0a4088814fe92552696d465jgm typedef const T* type; 35638513a8bb154f0b6d0a4088814fe92552696d465jgm}; 35738513a8bb154f0b6d0a4088814fe92552696d465jgm// Sometimes people use arrays whose size is not available at the use site 35838513a8bb154f0b6d0a4088814fe92552696d465jgm// (e.g. extern const char kNamePrefix[]). This specialization covers that 35938513a8bb154f0b6d0a4088814fe92552696d465jgm// case. 36038513a8bb154f0b6d0a4088814fe92552696d465jgmtemplate <typename T> struct DecayArray<T[]> { 36138513a8bb154f0b6d0a4088814fe92552696d465jgm typedef const T* type; 36238513a8bb154f0b6d0a4088814fe92552696d465jgm}; 36338513a8bb154f0b6d0a4088814fe92552696d465jgm 364b5c81098a8ccc25e313ffca56c911200b3591ea0kosak// Disable MSVC warnings for infinite recursion, since in this case the 365b5c81098a8ccc25e313ffca56c911200b3591ea0kosak// the recursion is unreachable. 366b5c81098a8ccc25e313ffca56c911200b3591ea0kosak#ifdef _MSC_VER 367b5c81098a8ccc25e313ffca56c911200b3591ea0kosak# pragma warning(push) 368b5c81098a8ccc25e313ffca56c911200b3591ea0kosak# pragma warning(disable:4717) 369b5c81098a8ccc25e313ffca56c911200b3591ea0kosak#endif 370b5c81098a8ccc25e313ffca56c911200b3591ea0kosak 371b5c81098a8ccc25e313ffca56c911200b3591ea0kosak// Invalid<T>() is usable as an expression of type T, but will terminate 372b5c81098a8ccc25e313ffca56c911200b3591ea0kosak// the program with an assertion failure if actually run. This is useful 373e35fdd936d133bf8a48de140a3c666897588a05shiqian// when a value of type T is needed for compilation, but the statement 374e35fdd936d133bf8a48de140a3c666897588a05shiqian// will not really be executed (or we don't care if the statement 375e35fdd936d133bf8a48de140a3c666897588a05shiqian// crashes). 376e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 377e35fdd936d133bf8a48de140a3c666897588a05shiqianinline T Invalid() { 378b5c81098a8ccc25e313ffca56c911200b3591ea0kosak Assert(false, "", -1, "Internal error: attempt to return invalid value"); 379b5c81098a8ccc25e313ffca56c911200b3591ea0kosak // This statement is unreachable, and would never terminate even if it 380b5c81098a8ccc25e313ffca56c911200b3591ea0kosak // could be reached. It is provided only to placate compiler warnings 381b5c81098a8ccc25e313ffca56c911200b3591ea0kosak // about missing return statements. 382b5c81098a8ccc25e313ffca56c911200b3591ea0kosak return Invalid<T>(); 383e35fdd936d133bf8a48de140a3c666897588a05shiqian} 384b5c81098a8ccc25e313ffca56c911200b3591ea0kosak 385b5c81098a8ccc25e313ffca56c911200b3591ea0kosak#ifdef _MSC_VER 386b5c81098a8ccc25e313ffca56c911200b3591ea0kosak# pragma warning(pop) 387b5c81098a8ccc25e313ffca56c911200b3591ea0kosak#endif 388e35fdd936d133bf8a48de140a3c666897588a05shiqian 389b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Given a raw type (i.e. having no top-level reference or const 390b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// modifier) RawContainer that's either an STL-style container or a 391b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// native array, class StlContainerView<RawContainer> has the 392b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// following members: 393b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// 394b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// - type is a type that provides an STL-style container view to 395b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// (i.e. implements the STL container concept for) RawContainer; 396b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// - const_reference is a type that provides a reference to a const 397b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// RawContainer; 398b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// - ConstReference(raw_container) returns a const reference to an STL-style 399b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// container view to raw_container, which is a RawContainer. 400b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// - Copy(raw_container) returns an STL-style container view of a 401b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// copy of raw_container, which is a RawContainer. 402b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// 403b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// This generic version is used when RawContainer itself is already an 404b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// STL-style container. 405b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <class RawContainer> 406b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanclass StlContainerView { 407b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan public: 408b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef RawContainer type; 409b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef const type& const_reference; 410b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 411b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan static const_reference ConstReference(const RawContainer& container) { 412b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // Ensures that RawContainer is not a const type. 413b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan testing::StaticAssertTypeEq<RawContainer, 41402f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan GTEST_REMOVE_CONST_(RawContainer)>(); 415b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan return container; 416b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 417b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan static type Copy(const RawContainer& container) { return container; } 418b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}; 419b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 420b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// This specialization is used when RawContainer is a native array type. 421b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename Element, size_t N> 422b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanclass StlContainerView<Element[N]> { 423b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan public: 42402f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan typedef GTEST_REMOVE_CONST_(Element) RawElement; 425b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef internal::NativeArray<RawElement> type; 426b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // NativeArray<T> can represent a native array either by value or by 427b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // reference (selected by a constructor argument), so 'const type' 428b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // can be used to reference a const native array. We cannot 429b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // 'typedef const type& const_reference' here, as that would mean 430b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // ConstReference() has to return a reference to a local variable. 431b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef const type const_reference; 432b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 433b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan static const_reference ConstReference(const Element (&array)[N]) { 434b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // Ensures that Element is not a const type. 435b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan testing::StaticAssertTypeEq<Element, RawElement>(); 43695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#if GTEST_OS_SYMBIAN 43795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // The Nokia Symbian compiler confuses itself in template instantiation 43895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // for this call without the cast to Element*: 43995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // function call '[testing::internal::NativeArray<char *>].NativeArray( 44095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // {lval} const char *[4], long, testing::internal::RelationToSource)' 44195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // does not match 44295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // 'testing::internal::NativeArray<char *>::NativeArray( 44395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // char *const *, unsigned int, testing::internal::RelationToSource)' 44495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // (instantiating: 'testing::internal::ContainsMatcherImpl 44595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // <const char * (&)[4]>::Matches(const char * (&)[4]) const') 44695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // (instantiating: 'testing::internal::StlContainerView<char *[4]>:: 44795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // ConstReference(const char * (&)[4])') 44895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // (and though the N parameter type is mismatched in the above explicit 44995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // conversion of it doesn't help - only the conversion of the array). 4501f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue return type(const_cast<Element*>(&array[0]), N, 4511f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue RelationToSourceReference()); 45295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#else 4531f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue return type(array, N, RelationToSourceReference()); 45495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#endif // GTEST_OS_SYMBIAN 455b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 456b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan static type Copy(const Element (&array)[N]) { 45795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#if GTEST_OS_SYMBIAN 4581f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue return type(const_cast<Element*>(&array[0]), N, RelationToSourceCopy()); 45995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#else 4601f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue return type(array, N, RelationToSourceCopy()); 46195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan#endif // GTEST_OS_SYMBIAN 462b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 463b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}; 464b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 465b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// This specialization is used when RawContainer is a native array 466b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// represented as a (pointer, size) tuple. 467b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename ElementPointer, typename Size> 468bd0188320de5aab1b09718e2c466387099d43cfbkosakclass StlContainerView< ::testing::tuple<ElementPointer, Size> > { 469b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan public: 47002f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan typedef GTEST_REMOVE_CONST_( 471b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typename internal::PointeeOf<ElementPointer>::type) RawElement; 472b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef internal::NativeArray<RawElement> type; 473b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef const type const_reference; 474b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 475b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan static const_reference ConstReference( 476bd0188320de5aab1b09718e2c466387099d43cfbkosak const ::testing::tuple<ElementPointer, Size>& array) { 4771f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue return type(get<0>(array), get<1>(array), RelationToSourceReference()); 478b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 479bd0188320de5aab1b09718e2c466387099d43cfbkosak static type Copy(const ::testing::tuple<ElementPointer, Size>& array) { 4801f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue return type(get<0>(array), get<1>(array), RelationToSourceCopy()); 481b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 482b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}; 483b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 484b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// The following specialization prevents the user from instantiating 485b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// StlContainer with a reference type. 486b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename T> class StlContainerView<T&>; 487b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 488a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan// A type transform to remove constness from the first part of a pair. 489a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan// Pairs like that are used as the value_type of associative containers, 490a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan// and this transform produces a similar but assignable pair. 491a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wantemplate <typename T> 492a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wanstruct RemoveConstFromKey { 493a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan typedef T type; 494a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan}; 495a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan 496a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan// Partially specialized to remove constness from std::pair<const K, V>. 497a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wantemplate <typename K, typename V> 498a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wanstruct RemoveConstFromKey<std::pair<const K, V> > { 499a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan typedef std::pair<K, V> type; 500a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan}; 501a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan 50279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// Mapping from booleans to types. Similar to boost::bool_<kValue> and 50379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// std::integral_constant<bool, kValue>. 50479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmtemplate <bool kValue> 50579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmstruct BooleanConstant {}; 50679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm 507e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace internal 508e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace testing 509e35fdd936d133bf8a48de140a3c666897588a05shiqian 510e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_ 5111f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue 512