![]() |
|
|||
File indexing completed on 2025-08-09 08:19:03
0001 // Copyright 2007, Google Inc. 0002 // All rights reserved. 0003 // 0004 // Redistribution and use in source and binary forms, with or without 0005 // modification, are permitted provided that the following conditions are 0006 // met: 0007 // 0008 // * Redistributions of source code must retain the above copyright 0009 // notice, this list of conditions and the following disclaimer. 0010 // * Redistributions in binary form must reproduce the above 0011 // copyright notice, this list of conditions and the following disclaimer 0012 // in the documentation and/or other materials provided with the 0013 // distribution. 0014 // * Neither the name of Google Inc. nor the names of its 0015 // contributors may be used to endorse or promote products derived from 0016 // this software without specific prior written permission. 0017 // 0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0029 // 0030 // Author: wan@google.com (Zhanyong Wan) 0031 0032 // Google Mock - a framework for writing C++ mock classes. 0033 // 0034 // This file defines some utilities useful for implementing Google 0035 // Mock. They are subject to change without notice, so please DO NOT 0036 // USE THEM IN USER CODE. 0037 0038 #ifndef GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_ 0039 #define GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_ 0040 0041 #include <stdio.h> 0042 #include <ostream> // NOLINT 0043 #include <string> 0044 0045 #include "gmock/internal/gmock-generated-internal-utils.h" 0046 #include "gmock/internal/gmock-port.h" 0047 #include "gtest/gtest.h" 0048 0049 namespace testing { 0050 namespace internal { 0051 0052 // Converts an identifier name to a space-separated list of lower-case 0053 // words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is 0054 // treated as one word. For example, both "FooBar123" and 0055 // "foo_bar_123" are converted to "foo bar 123". 0056 GTEST_API_ string ConvertIdentifierNameToWords(const char* id_name); 0057 0058 // PointeeOf<Pointer>::type is the type of a value pointed to by a 0059 // Pointer, which can be either a smart pointer or a raw pointer. The 0060 // following default implementation is for the case where Pointer is a 0061 // smart pointer. 0062 template <typename Pointer> 0063 struct PointeeOf { 0064 // Smart pointer classes define type element_type as the type of 0065 // their pointees. 0066 typedef typename Pointer::element_type type; 0067 }; 0068 // This specialization is for the raw pointer case. 0069 template <typename T> 0070 struct PointeeOf<T*> { typedef T type; }; // NOLINT 0071 0072 // GetRawPointer(p) returns the raw pointer underlying p when p is a 0073 // smart pointer, or returns p itself when p is already a raw pointer. 0074 // The following default implementation is for the smart pointer case. 0075 template <typename Pointer> 0076 inline const typename Pointer::element_type* GetRawPointer(const Pointer& p) { 0077 return p.get(); 0078 } 0079 // This overloaded version is for the raw pointer case. 0080 template <typename Element> 0081 inline Element* GetRawPointer(Element* p) { return p; } 0082 0083 // This comparator allows linked_ptr to be stored in sets. 0084 template <typename T> 0085 struct LinkedPtrLessThan { 0086 bool operator()(const ::testing::internal::linked_ptr<T>& lhs, 0087 const ::testing::internal::linked_ptr<T>& rhs) const { 0088 return lhs.get() < rhs.get(); 0089 } 0090 }; 0091 0092 // Symbian compilation can be done with wchar_t being either a native 0093 // type or a typedef. Using Google Mock with OpenC without wchar_t 0094 // should require the definition of _STLP_NO_WCHAR_T. 0095 // 0096 // MSVC treats wchar_t as a native type usually, but treats it as the 0097 // same as unsigned short when the compiler option /Zc:wchar_t- is 0098 // specified. It defines _NATIVE_WCHAR_T_DEFINED symbol when wchar_t 0099 // is a native type. 0100 #if (GTEST_OS_SYMBIAN && defined(_STLP_NO_WCHAR_T)) || \ 0101 (defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED)) 0102 // wchar_t is a typedef. 0103 #else 0104 # define GMOCK_WCHAR_T_IS_NATIVE_ 1 0105 #endif 0106 0107 // signed wchar_t and unsigned wchar_t are NOT in the C++ standard. 0108 // Using them is a bad practice and not portable. So DON'T use them. 0109 // 0110 // Still, Google Mock is designed to work even if the user uses signed 0111 // wchar_t or unsigned wchar_t (obviously, assuming the compiler 0112 // supports them). 0113 // 0114 // To gcc, 0115 // wchar_t == signed wchar_t != unsigned wchar_t == unsigned int 0116 #ifdef __GNUC__ 0117 // signed/unsigned wchar_t are valid types. 0118 # define GMOCK_HAS_SIGNED_WCHAR_T_ 1 0119 #endif 0120 0121 // In what follows, we use the term "kind" to indicate whether a type 0122 // is bool, an integer type (excluding bool), a floating-point type, 0123 // or none of them. This categorization is useful for determining 0124 // when a matcher argument type can be safely converted to another 0125 // type in the implementation of SafeMatcherCast. 0126 enum TypeKind { 0127 kBool, kInteger, kFloatingPoint, kOther 0128 }; 0129 0130 // KindOf<T>::value is the kind of type T. 0131 template <typename T> struct KindOf { 0132 enum { value = kOther }; // The default kind. 0133 }; 0134 0135 // This macro declares that the kind of 'type' is 'kind'. 0136 #define GMOCK_DECLARE_KIND_(type, kind) \ 0137 template <> struct KindOf<type> { enum { value = kind }; } 0138 0139 GMOCK_DECLARE_KIND_(bool, kBool); 0140 0141 // All standard integer types. 0142 GMOCK_DECLARE_KIND_(char, kInteger); 0143 GMOCK_DECLARE_KIND_(signed char, kInteger); 0144 GMOCK_DECLARE_KIND_(unsigned char, kInteger); 0145 GMOCK_DECLARE_KIND_(short, kInteger); // NOLINT 0146 GMOCK_DECLARE_KIND_(unsigned short, kInteger); // NOLINT 0147 GMOCK_DECLARE_KIND_(int, kInteger); 0148 GMOCK_DECLARE_KIND_(unsigned int, kInteger); 0149 GMOCK_DECLARE_KIND_(long, kInteger); // NOLINT 0150 GMOCK_DECLARE_KIND_(unsigned long, kInteger); // NOLINT 0151 0152 #if GMOCK_WCHAR_T_IS_NATIVE_ 0153 GMOCK_DECLARE_KIND_(wchar_t, kInteger); 0154 #endif 0155 0156 // Non-standard integer types. 0157 GMOCK_DECLARE_KIND_(Int64, kInteger); 0158 GMOCK_DECLARE_KIND_(UInt64, kInteger); 0159 0160 // All standard floating-point types. 0161 GMOCK_DECLARE_KIND_(float, kFloatingPoint); 0162 GMOCK_DECLARE_KIND_(double, kFloatingPoint); 0163 GMOCK_DECLARE_KIND_(long double, kFloatingPoint); 0164 0165 #undef GMOCK_DECLARE_KIND_ 0166 0167 // Evaluates to the kind of 'type'. 0168 #define GMOCK_KIND_OF_(type) \ 0169 static_cast< ::testing::internal::TypeKind>( \ 0170 ::testing::internal::KindOf<type>::value) 0171 0172 // Evaluates to true iff integer type T is signed. 0173 #define GMOCK_IS_SIGNED_(T) (static_cast<T>(-1) < 0) 0174 0175 // LosslessArithmeticConvertibleImpl<kFromKind, From, kToKind, To>::value 0176 // is true iff arithmetic type From can be losslessly converted to 0177 // arithmetic type To. 0178 // 0179 // It's the user's responsibility to ensure that both From and To are 0180 // raw (i.e. has no CV modifier, is not a pointer, and is not a 0181 // reference) built-in arithmetic types, kFromKind is the kind of 0182 // From, and kToKind is the kind of To; the value is 0183 // implementation-defined when the above pre-condition is violated. 0184 template <TypeKind kFromKind, typename From, TypeKind kToKind, typename To> 0185 struct LosslessArithmeticConvertibleImpl : public false_type {}; 0186 0187 // Converting bool to bool is lossless. 0188 template <> 0189 struct LosslessArithmeticConvertibleImpl<kBool, bool, kBool, bool> 0190 : public true_type {}; // NOLINT 0191 0192 // Converting bool to any integer type is lossless. 0193 template <typename To> 0194 struct LosslessArithmeticConvertibleImpl<kBool, bool, kInteger, To> 0195 : public true_type {}; // NOLINT 0196 0197 // Converting bool to any floating-point type is lossless. 0198 template <typename To> 0199 struct LosslessArithmeticConvertibleImpl<kBool, bool, kFloatingPoint, To> 0200 : public true_type {}; // NOLINT 0201 0202 // Converting an integer to bool is lossy. 0203 template <typename From> 0204 struct LosslessArithmeticConvertibleImpl<kInteger, From, kBool, bool> 0205 : public false_type {}; // NOLINT 0206 0207 // Converting an integer to another non-bool integer is lossless iff 0208 // the target type's range encloses the source type's range. 0209 template <typename From, typename To> 0210 struct LosslessArithmeticConvertibleImpl<kInteger, From, kInteger, To> 0211 : public bool_constant< 0212 // When converting from a smaller size to a larger size, we are 0213 // fine as long as we are not converting from signed to unsigned. 0214 ((sizeof(From) < sizeof(To)) && 0215 (!GMOCK_IS_SIGNED_(From) || GMOCK_IS_SIGNED_(To))) || 0216 // When converting between the same size, the signedness must match. 0217 ((sizeof(From) == sizeof(To)) && 0218 (GMOCK_IS_SIGNED_(From) == GMOCK_IS_SIGNED_(To)))> {}; // NOLINT 0219 0220 #undef GMOCK_IS_SIGNED_ 0221 0222 // Converting an integer to a floating-point type may be lossy, since 0223 // the format of a floating-point number is implementation-defined. 0224 template <typename From, typename To> 0225 struct LosslessArithmeticConvertibleImpl<kInteger, From, kFloatingPoint, To> 0226 : public false_type {}; // NOLINT 0227 0228 // Converting a floating-point to bool is lossy. 0229 template <typename From> 0230 struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kBool, bool> 0231 : public false_type {}; // NOLINT 0232 0233 // Converting a floating-point to an integer is lossy. 0234 template <typename From, typename To> 0235 struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kInteger, To> 0236 : public false_type {}; // NOLINT 0237 0238 // Converting a floating-point to another floating-point is lossless 0239 // iff the target type is at least as big as the source type. 0240 template <typename From, typename To> 0241 struct LosslessArithmeticConvertibleImpl< 0242 kFloatingPoint, From, kFloatingPoint, To> 0243 : public bool_constant<sizeof(From) <= sizeof(To)> {}; // NOLINT 0244 0245 // LosslessArithmeticConvertible<From, To>::value is true iff arithmetic 0246 // type From can be losslessly converted to arithmetic type To. 0247 // 0248 // It's the user's responsibility to ensure that both From and To are 0249 // raw (i.e. has no CV modifier, is not a pointer, and is not a 0250 // reference) built-in arithmetic types; the value is 0251 // implementation-defined when the above pre-condition is violated. 0252 template <typename From, typename To> 0253 struct LosslessArithmeticConvertible 0254 : public LosslessArithmeticConvertibleImpl< 0255 GMOCK_KIND_OF_(From), From, GMOCK_KIND_OF_(To), To> {}; // NOLINT 0256 0257 // This interface knows how to report a Google Mock failure (either 0258 // non-fatal or fatal). 0259 class FailureReporterInterface { 0260 public: 0261 // The type of a failure (either non-fatal or fatal). 0262 enum FailureType { 0263 kNonfatal, kFatal 0264 }; 0265 0266 virtual ~FailureReporterInterface() {} 0267 0268 // Reports a failure that occurred at the given source file location. 0269 virtual void ReportFailure(FailureType type, const char* file, int line, 0270 const string& message) = 0; 0271 }; 0272 0273 // Returns the failure reporter used by Google Mock. 0274 GTEST_API_ FailureReporterInterface* GetFailureReporter(); 0275 0276 // Asserts that condition is true; aborts the process with the given 0277 // message if condition is false. We cannot use LOG(FATAL) or CHECK() 0278 // as Google Mock might be used to mock the log sink itself. We 0279 // inline this function to prevent it from showing up in the stack 0280 // trace. 0281 inline void Assert(bool condition, const char* file, int line, 0282 const string& msg) { 0283 if (!condition) { 0284 GetFailureReporter()->ReportFailure(FailureReporterInterface::kFatal, 0285 file, line, msg); 0286 } 0287 } 0288 inline void Assert(bool condition, const char* file, int line) { 0289 Assert(condition, file, line, "Assertion failed."); 0290 } 0291 0292 // Verifies that condition is true; generates a non-fatal failure if 0293 // condition is false. 0294 inline void Expect(bool condition, const char* file, int line, 0295 const string& msg) { 0296 if (!condition) { 0297 GetFailureReporter()->ReportFailure(FailureReporterInterface::kNonfatal, 0298 file, line, msg); 0299 } 0300 } 0301 inline void Expect(bool condition, const char* file, int line) { 0302 Expect(condition, file, line, "Expectation failed."); 0303 } 0304 0305 // Severity level of a log. 0306 enum LogSeverity { 0307 kInfo = 0, 0308 kWarning = 1 0309 }; 0310 0311 // Valid values for the --gmock_verbose flag. 0312 0313 // All logs (informational and warnings) are printed. 0314 const char kInfoVerbosity[] = "info"; 0315 // Only warnings are printed. 0316 const char kWarningVerbosity[] = "warning"; 0317 // No logs are printed. 0318 const char kErrorVerbosity[] = "error"; 0319 0320 // Returns true iff a log with the given severity is visible according 0321 // to the --gmock_verbose flag. 0322 GTEST_API_ bool LogIsVisible(LogSeverity severity); 0323 0324 // Prints the given message to stdout iff 'severity' >= the level 0325 // specified by the --gmock_verbose flag. If stack_frames_to_skip >= 0326 // 0, also prints the stack trace excluding the top 0327 // stack_frames_to_skip frames. In opt mode, any positive 0328 // stack_frames_to_skip is treated as 0, since we don't know which 0329 // function calls will be inlined by the compiler and need to be 0330 // conservative. 0331 GTEST_API_ void Log(LogSeverity severity, 0332 const string& message, 0333 int stack_frames_to_skip); 0334 0335 // TODO(wan@google.com): group all type utilities together. 0336 0337 // Type traits. 0338 0339 // is_reference<T>::value is non-zero iff T is a reference type. 0340 template <typename T> struct is_reference : public false_type {}; 0341 template <typename T> struct is_reference<T&> : public true_type {}; 0342 0343 // type_equals<T1, T2>::value is non-zero iff T1 and T2 are the same type. 0344 template <typename T1, typename T2> struct type_equals : public false_type {}; 0345 template <typename T> struct type_equals<T, T> : public true_type {}; 0346 0347 // remove_reference<T>::type removes the reference from type T, if any. 0348 template <typename T> struct remove_reference { typedef T type; }; // NOLINT 0349 template <typename T> struct remove_reference<T&> { typedef T type; }; // NOLINT 0350 0351 // DecayArray<T>::type turns an array type U[N] to const U* and preserves 0352 // other types. Useful for saving a copy of a function argument. 0353 template <typename T> struct DecayArray { typedef T type; }; // NOLINT 0354 template <typename T, size_t N> struct DecayArray<T[N]> { 0355 typedef const T* type; 0356 }; 0357 // Sometimes people use arrays whose size is not available at the use site 0358 // (e.g. extern const char kNamePrefix[]). This specialization covers that 0359 // case. 0360 template <typename T> struct DecayArray<T[]> { 0361 typedef const T* type; 0362 }; 0363 0364 // Disable MSVC warnings for infinite recursion, since in this case the 0365 // the recursion is unreachable. 0366 #ifdef _MSC_VER 0367 # pragma warning(push) 0368 # pragma warning(disable:4717) 0369 #endif 0370 0371 // Invalid<T>() is usable as an expression of type T, but will terminate 0372 // the program with an assertion failure if actually run. This is useful 0373 // when a value of type T is needed for compilation, but the statement 0374 // will not really be executed (or we don't care if the statement 0375 // crashes). 0376 template <typename T> 0377 inline T Invalid() { 0378 Assert(false, "", -1, "Internal error: attempt to return invalid value"); 0379 // This statement is unreachable, and would never terminate even if it 0380 // could be reached. It is provided only to placate compiler warnings 0381 // about missing return statements. 0382 return Invalid<T>(); 0383 } 0384 0385 #ifdef _MSC_VER 0386 # pragma warning(pop) 0387 #endif 0388 0389 // Given a raw type (i.e. having no top-level reference or const 0390 // modifier) RawContainer that's either an STL-style container or a 0391 // native array, class StlContainerView<RawContainer> has the 0392 // following members: 0393 // 0394 // - type is a type that provides an STL-style container view to 0395 // (i.e. implements the STL container concept for) RawContainer; 0396 // - const_reference is a type that provides a reference to a const 0397 // RawContainer; 0398 // - ConstReference(raw_container) returns a const reference to an STL-style 0399 // container view to raw_container, which is a RawContainer. 0400 // - Copy(raw_container) returns an STL-style container view of a 0401 // copy of raw_container, which is a RawContainer. 0402 // 0403 // This generic version is used when RawContainer itself is already an 0404 // STL-style container. 0405 template <class RawContainer> 0406 class StlContainerView { 0407 public: 0408 typedef RawContainer type; 0409 typedef const type& const_reference; 0410 0411 static const_reference ConstReference(const RawContainer& container) { 0412 // Ensures that RawContainer is not a const type. 0413 testing::StaticAssertTypeEq<RawContainer, 0414 GTEST_REMOVE_CONST_(RawContainer)>(); 0415 return container; 0416 } 0417 static type Copy(const RawContainer& container) { return container; } 0418 }; 0419 0420 // This specialization is used when RawContainer is a native array type. 0421 template <typename Element, size_t N> 0422 class StlContainerView<Element[N]> { 0423 public: 0424 typedef GTEST_REMOVE_CONST_(Element) RawElement; 0425 typedef internal::NativeArray<RawElement> type; 0426 // NativeArray<T> can represent a native array either by value or by 0427 // reference (selected by a constructor argument), so 'const type' 0428 // can be used to reference a const native array. We cannot 0429 // 'typedef const type& const_reference' here, as that would mean 0430 // ConstReference() has to return a reference to a local variable. 0431 typedef const type const_reference; 0432 0433 static const_reference ConstReference(const Element (&array)[N]) { 0434 // Ensures that Element is not a const type. 0435 testing::StaticAssertTypeEq<Element, RawElement>(); 0436 #if GTEST_OS_SYMBIAN 0437 // The Nokia Symbian compiler confuses itself in template instantiation 0438 // for this call without the cast to Element*: 0439 // function call '[testing::internal::NativeArray<char *>].NativeArray( 0440 // {lval} const char *[4], long, testing::internal::RelationToSource)' 0441 // does not match 0442 // 'testing::internal::NativeArray<char *>::NativeArray( 0443 // char *const *, unsigned int, testing::internal::RelationToSource)' 0444 // (instantiating: 'testing::internal::ContainsMatcherImpl 0445 // <const char * (&)[4]>::Matches(const char * (&)[4]) const') 0446 // (instantiating: 'testing::internal::StlContainerView<char *[4]>:: 0447 // ConstReference(const char * (&)[4])') 0448 // (and though the N parameter type is mismatched in the above explicit 0449 // conversion of it doesn't help - only the conversion of the array). 0450 return type(const_cast<Element*>(&array[0]), N, 0451 RelationToSourceReference()); 0452 #else 0453 return type(array, N, RelationToSourceReference()); 0454 #endif // GTEST_OS_SYMBIAN 0455 } 0456 static type Copy(const Element (&array)[N]) { 0457 #if GTEST_OS_SYMBIAN 0458 return type(const_cast<Element*>(&array[0]), N, RelationToSourceCopy()); 0459 #else 0460 return type(array, N, RelationToSourceCopy()); 0461 #endif // GTEST_OS_SYMBIAN 0462 } 0463 }; 0464 0465 // This specialization is used when RawContainer is a native array 0466 // represented as a (pointer, size) tuple. 0467 template <typename ElementPointer, typename Size> 0468 class StlContainerView< ::testing::tuple<ElementPointer, Size> > { 0469 public: 0470 typedef GTEST_REMOVE_CONST_( 0471 typename internal::PointeeOf<ElementPointer>::type) RawElement; 0472 typedef internal::NativeArray<RawElement> type; 0473 typedef const type const_reference; 0474 0475 static const_reference ConstReference( 0476 const ::testing::tuple<ElementPointer, Size>& array) { 0477 return type(get<0>(array), get<1>(array), RelationToSourceReference()); 0478 } 0479 static type Copy(const ::testing::tuple<ElementPointer, Size>& array) { 0480 return type(get<0>(array), get<1>(array), RelationToSourceCopy()); 0481 } 0482 }; 0483 0484 // The following specialization prevents the user from instantiating 0485 // StlContainer with a reference type. 0486 template <typename T> class StlContainerView<T&>; 0487 0488 // A type transform to remove constness from the first part of a pair. 0489 // Pairs like that are used as the value_type of associative containers, 0490 // and this transform produces a similar but assignable pair. 0491 template <typename T> 0492 struct RemoveConstFromKey { 0493 typedef T type; 0494 }; 0495 0496 // Partially specialized to remove constness from std::pair<const K, V>. 0497 template <typename K, typename V> 0498 struct RemoveConstFromKey<std::pair<const K, V> > { 0499 typedef std::pair<K, V> type; 0500 }; 0501 0502 // Mapping from booleans to types. Similar to boost::bool_<kValue> and 0503 // std::integral_constant<bool, kValue>. 0504 template <bool kValue> 0505 struct BooleanConstant {}; 0506 0507 } // namespace internal 0508 } // namespace testing 0509 0510 #endif // GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_ 0511
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |