gmock-internal-utils.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. // Copyright 2007, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // Google Mock - a framework for writing C++ mock classes.
  30. //
  31. // This file defines some utilities useful for implementing Google
  32. // Mock. They are subject to change without notice, so please DO NOT
  33. // USE THEM IN USER CODE.
  34. // GOOGLETEST_CM0002 DO NOT DELETE
  35. #ifndef GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_
  36. #define GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_
  37. #include <stdio.h>
  38. #include <ostream> // NOLINT
  39. #include <string>
  40. #include "gmock/internal/gmock-generated-internal-utils.h"
  41. #include "gmock/internal/gmock-port.h"
  42. #include "gtest/gtest.h"
  43. namespace testing {
  44. namespace internal {
  45. // Silence MSVC C4100 (unreferenced formal parameter) and
  46. // C4805('==': unsafe mix of type 'const int' and type 'const bool')
  47. #ifdef _MSC_VER
  48. # pragma warning(push)
  49. # pragma warning(disable:4100)
  50. # pragma warning(disable:4805)
  51. #endif
  52. // Joins a vector of strings as if they are fields of a tuple; returns
  53. // the joined string.
  54. GTEST_API_ std::string JoinAsTuple(const Strings& fields);
  55. // Converts an identifier name to a space-separated list of lower-case
  56. // words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is
  57. // treated as one word. For example, both "FooBar123" and
  58. // "foo_bar_123" are converted to "foo bar 123".
  59. GTEST_API_ std::string ConvertIdentifierNameToWords(const char* id_name);
  60. // PointeeOf<Pointer>::type is the type of a value pointed to by a
  61. // Pointer, which can be either a smart pointer or a raw pointer. The
  62. // following default implementation is for the case where Pointer is a
  63. // smart pointer.
  64. template <typename Pointer>
  65. struct PointeeOf {
  66. // Smart pointer classes define type element_type as the type of
  67. // their pointees.
  68. typedef typename Pointer::element_type type;
  69. };
  70. // This specialization is for the raw pointer case.
  71. template <typename T>
  72. struct PointeeOf<T*> { typedef T type; }; // NOLINT
  73. // GetRawPointer(p) returns the raw pointer underlying p when p is a
  74. // smart pointer, or returns p itself when p is already a raw pointer.
  75. // The following default implementation is for the smart pointer case.
  76. template <typename Pointer>
  77. inline const typename Pointer::element_type* GetRawPointer(const Pointer& p) {
  78. return p.get();
  79. }
  80. // This overloaded version is for the raw pointer case.
  81. template <typename Element>
  82. inline Element* GetRawPointer(Element* p) { return p; }
  83. // This comparator allows linked_ptr to be stored in sets.
  84. template <typename T>
  85. struct LinkedPtrLessThan {
  86. bool operator()(const ::testing::internal::linked_ptr<T>& lhs,
  87. const ::testing::internal::linked_ptr<T>& rhs) const {
  88. return lhs.get() < rhs.get();
  89. }
  90. };
  91. // Symbian compilation can be done with wchar_t being either a native
  92. // type or a typedef. Using Google Mock with OpenC without wchar_t
  93. // should require the definition of _STLP_NO_WCHAR_T.
  94. //
  95. // MSVC treats wchar_t as a native type usually, but treats it as the
  96. // same as unsigned short when the compiler option /Zc:wchar_t- is
  97. // specified. It defines _NATIVE_WCHAR_T_DEFINED symbol when wchar_t
  98. // is a native type.
  99. #if (GTEST_OS_SYMBIAN && defined(_STLP_NO_WCHAR_T)) || \
  100. (defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED))
  101. // wchar_t is a typedef.
  102. #else
  103. # define GMOCK_WCHAR_T_IS_NATIVE_ 1
  104. #endif
  105. // signed wchar_t and unsigned wchar_t are NOT in the C++ standard.
  106. // Using them is a bad practice and not portable. So DON'T use them.
  107. //
  108. // Still, Google Mock is designed to work even if the user uses signed
  109. // wchar_t or unsigned wchar_t (obviously, assuming the compiler
  110. // supports them).
  111. //
  112. // To gcc,
  113. // wchar_t == signed wchar_t != unsigned wchar_t == unsigned int
  114. #ifdef __GNUC__
  115. #if !defined(__WCHAR_UNSIGNED__)
  116. // signed/unsigned wchar_t are valid types.
  117. # define GMOCK_HAS_SIGNED_WCHAR_T_ 1
  118. #endif
  119. #endif
  120. // In what follows, we use the term "kind" to indicate whether a type
  121. // is bool, an integer type (excluding bool), a floating-point type,
  122. // or none of them. This categorization is useful for determining
  123. // when a matcher argument type can be safely converted to another
  124. // type in the implementation of SafeMatcherCast.
  125. enum TypeKind {
  126. kBool, kInteger, kFloatingPoint, kOther
  127. };
  128. // KindOf<T>::value is the kind of type T.
  129. template <typename T> struct KindOf {
  130. enum { value = kOther }; // The default kind.
  131. };
  132. // This macro declares that the kind of 'type' is 'kind'.
  133. #define GMOCK_DECLARE_KIND_(type, kind) \
  134. template <> struct KindOf<type> { enum { value = kind }; }
  135. GMOCK_DECLARE_KIND_(bool, kBool);
  136. // All standard integer types.
  137. GMOCK_DECLARE_KIND_(char, kInteger);
  138. GMOCK_DECLARE_KIND_(signed char, kInteger);
  139. GMOCK_DECLARE_KIND_(unsigned char, kInteger);
  140. GMOCK_DECLARE_KIND_(short, kInteger); // NOLINT
  141. GMOCK_DECLARE_KIND_(unsigned short, kInteger); // NOLINT
  142. GMOCK_DECLARE_KIND_(int, kInteger);
  143. GMOCK_DECLARE_KIND_(unsigned int, kInteger);
  144. GMOCK_DECLARE_KIND_(long, kInteger); // NOLINT
  145. GMOCK_DECLARE_KIND_(unsigned long, kInteger); // NOLINT
  146. #if GMOCK_WCHAR_T_IS_NATIVE_
  147. GMOCK_DECLARE_KIND_(wchar_t, kInteger);
  148. #endif
  149. // Non-standard integer types.
  150. GMOCK_DECLARE_KIND_(Int64, kInteger);
  151. GMOCK_DECLARE_KIND_(UInt64, kInteger);
  152. // All standard floating-point types.
  153. GMOCK_DECLARE_KIND_(float, kFloatingPoint);
  154. GMOCK_DECLARE_KIND_(double, kFloatingPoint);
  155. GMOCK_DECLARE_KIND_(long double, kFloatingPoint);
  156. #undef GMOCK_DECLARE_KIND_
  157. // Evaluates to the kind of 'type'.
  158. #define GMOCK_KIND_OF_(type) \
  159. static_cast< ::testing::internal::TypeKind>( \
  160. ::testing::internal::KindOf<type>::value)
  161. // Evaluates to true iff integer type T is signed.
  162. #define GMOCK_IS_SIGNED_(T) (static_cast<T>(-1) < 0)
  163. // LosslessArithmeticConvertibleImpl<kFromKind, From, kToKind, To>::value
  164. // is true iff arithmetic type From can be losslessly converted to
  165. // arithmetic type To.
  166. //
  167. // It's the user's responsibility to ensure that both From and To are
  168. // raw (i.e. has no CV modifier, is not a pointer, and is not a
  169. // reference) built-in arithmetic types, kFromKind is the kind of
  170. // From, and kToKind is the kind of To; the value is
  171. // implementation-defined when the above pre-condition is violated.
  172. template <TypeKind kFromKind, typename From, TypeKind kToKind, typename To>
  173. struct LosslessArithmeticConvertibleImpl : public false_type {};
  174. // Converting bool to bool is lossless.
  175. template <>
  176. struct LosslessArithmeticConvertibleImpl<kBool, bool, kBool, bool>
  177. : public true_type {}; // NOLINT
  178. // Converting bool to any integer type is lossless.
  179. template <typename To>
  180. struct LosslessArithmeticConvertibleImpl<kBool, bool, kInteger, To>
  181. : public true_type {}; // NOLINT
  182. // Converting bool to any floating-point type is lossless.
  183. template <typename To>
  184. struct LosslessArithmeticConvertibleImpl<kBool, bool, kFloatingPoint, To>
  185. : public true_type {}; // NOLINT
  186. // Converting an integer to bool is lossy.
  187. template <typename From>
  188. struct LosslessArithmeticConvertibleImpl<kInteger, From, kBool, bool>
  189. : public false_type {}; // NOLINT
  190. // Converting an integer to another non-bool integer is lossless iff
  191. // the target type's range encloses the source type's range.
  192. template <typename From, typename To>
  193. struct LosslessArithmeticConvertibleImpl<kInteger, From, kInteger, To>
  194. : public bool_constant<
  195. // When converting from a smaller size to a larger size, we are
  196. // fine as long as we are not converting from signed to unsigned.
  197. ((sizeof(From) < sizeof(To)) &&
  198. (!GMOCK_IS_SIGNED_(From) || GMOCK_IS_SIGNED_(To))) ||
  199. // When converting between the same size, the signedness must match.
  200. ((sizeof(From) == sizeof(To)) &&
  201. (GMOCK_IS_SIGNED_(From) == GMOCK_IS_SIGNED_(To)))> {}; // NOLINT
  202. #undef GMOCK_IS_SIGNED_
  203. // Converting an integer to a floating-point type may be lossy, since
  204. // the format of a floating-point number is implementation-defined.
  205. template <typename From, typename To>
  206. struct LosslessArithmeticConvertibleImpl<kInteger, From, kFloatingPoint, To>
  207. : public false_type {}; // NOLINT
  208. // Converting a floating-point to bool is lossy.
  209. template <typename From>
  210. struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kBool, bool>
  211. : public false_type {}; // NOLINT
  212. // Converting a floating-point to an integer is lossy.
  213. template <typename From, typename To>
  214. struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kInteger, To>
  215. : public false_type {}; // NOLINT
  216. // Converting a floating-point to another floating-point is lossless
  217. // iff the target type is at least as big as the source type.
  218. template <typename From, typename To>
  219. struct LosslessArithmeticConvertibleImpl<
  220. kFloatingPoint, From, kFloatingPoint, To>
  221. : public bool_constant<sizeof(From) <= sizeof(To)> {}; // NOLINT
  222. // LosslessArithmeticConvertible<From, To>::value is true iff arithmetic
  223. // type From can be losslessly converted to arithmetic type To.
  224. //
  225. // It's the user's responsibility to ensure that both From and To are
  226. // raw (i.e. has no CV modifier, is not a pointer, and is not a
  227. // reference) built-in arithmetic types; the value is
  228. // implementation-defined when the above pre-condition is violated.
  229. template <typename From, typename To>
  230. struct LosslessArithmeticConvertible
  231. : public LosslessArithmeticConvertibleImpl<
  232. GMOCK_KIND_OF_(From), From, GMOCK_KIND_OF_(To), To> {}; // NOLINT
  233. // This interface knows how to report a Google Mock failure (either
  234. // non-fatal or fatal).
  235. class FailureReporterInterface {
  236. public:
  237. // The type of a failure (either non-fatal or fatal).
  238. enum FailureType {
  239. kNonfatal, kFatal
  240. };
  241. virtual ~FailureReporterInterface() {}
  242. // Reports a failure that occurred at the given source file location.
  243. virtual void ReportFailure(FailureType type, const char* file, int line,
  244. const std::string& message) = 0;
  245. };
  246. // Returns the failure reporter used by Google Mock.
  247. GTEST_API_ FailureReporterInterface* GetFailureReporter();
  248. // Asserts that condition is true; aborts the process with the given
  249. // message if condition is false. We cannot use LOG(FATAL) or CHECK()
  250. // as Google Mock might be used to mock the log sink itself. We
  251. // inline this function to prevent it from showing up in the stack
  252. // trace.
  253. inline void Assert(bool condition, const char* file, int line,
  254. const std::string& msg) {
  255. if (!condition) {
  256. GetFailureReporter()->ReportFailure(FailureReporterInterface::kFatal,
  257. file, line, msg);
  258. }
  259. }
  260. inline void Assert(bool condition, const char* file, int line) {
  261. Assert(condition, file, line, "Assertion failed.");
  262. }
  263. // Verifies that condition is true; generates a non-fatal failure if
  264. // condition is false.
  265. inline void Expect(bool condition, const char* file, int line,
  266. const std::string& msg) {
  267. if (!condition) {
  268. GetFailureReporter()->ReportFailure(FailureReporterInterface::kNonfatal,
  269. file, line, msg);
  270. }
  271. }
  272. inline void Expect(bool condition, const char* file, int line) {
  273. Expect(condition, file, line, "Expectation failed.");
  274. }
  275. // Severity level of a log.
  276. enum LogSeverity {
  277. kInfo = 0,
  278. kWarning = 1
  279. };
  280. // Valid values for the --gmock_verbose flag.
  281. // All logs (informational and warnings) are printed.
  282. const char kInfoVerbosity[] = "info";
  283. // Only warnings are printed.
  284. const char kWarningVerbosity[] = "warning";
  285. // No logs are printed.
  286. const char kErrorVerbosity[] = "error";
  287. // Returns true iff a log with the given severity is visible according
  288. // to the --gmock_verbose flag.
  289. GTEST_API_ bool LogIsVisible(LogSeverity severity);
  290. // Prints the given message to stdout iff 'severity' >= the level
  291. // specified by the --gmock_verbose flag. If stack_frames_to_skip >=
  292. // 0, also prints the stack trace excluding the top
  293. // stack_frames_to_skip frames. In opt mode, any positive
  294. // stack_frames_to_skip is treated as 0, since we don't know which
  295. // function calls will be inlined by the compiler and need to be
  296. // conservative.
  297. GTEST_API_ void Log(LogSeverity severity, const std::string& message,
  298. int stack_frames_to_skip);
  299. // A marker class that is used to resolve parameterless expectations to the
  300. // correct overload. This must not be instantiable, to prevent client code from
  301. // accidentally resolving to the overload; for example:
  302. //
  303. // ON_CALL(mock, Method({}, nullptr))...
  304. //
  305. class WithoutMatchers {
  306. private:
  307. WithoutMatchers() {}
  308. friend GTEST_API_ WithoutMatchers GetWithoutMatchers();
  309. };
  310. // Internal use only: access the singleton instance of WithoutMatchers.
  311. GTEST_API_ WithoutMatchers GetWithoutMatchers();
  312. // FIXME: group all type utilities together.
  313. // Type traits.
  314. // is_reference<T>::value is non-zero iff T is a reference type.
  315. template <typename T> struct is_reference : public false_type {};
  316. template <typename T> struct is_reference<T&> : public true_type {};
  317. // type_equals<T1, T2>::value is non-zero iff T1 and T2 are the same type.
  318. template <typename T1, typename T2> struct type_equals : public false_type {};
  319. template <typename T> struct type_equals<T, T> : public true_type {};
  320. // remove_reference<T>::type removes the reference from type T, if any.
  321. template <typename T> struct remove_reference { typedef T type; }; // NOLINT
  322. template <typename T> struct remove_reference<T&> { typedef T type; }; // NOLINT
  323. // DecayArray<T>::type turns an array type U[N] to const U* and preserves
  324. // other types. Useful for saving a copy of a function argument.
  325. template <typename T> struct DecayArray { typedef T type; }; // NOLINT
  326. template <typename T, size_t N> struct DecayArray<T[N]> {
  327. typedef const T* type;
  328. };
  329. // Sometimes people use arrays whose size is not available at the use site
  330. // (e.g. extern const char kNamePrefix[]). This specialization covers that
  331. // case.
  332. template <typename T> struct DecayArray<T[]> {
  333. typedef const T* type;
  334. };
  335. // Disable MSVC warnings for infinite recursion, since in this case the
  336. // the recursion is unreachable.
  337. #ifdef _MSC_VER
  338. # pragma warning(push)
  339. # pragma warning(disable:4717)
  340. #endif
  341. // Invalid<T>() is usable as an expression of type T, but will terminate
  342. // the program with an assertion failure if actually run. This is useful
  343. // when a value of type T is needed for compilation, but the statement
  344. // will not really be executed (or we don't care if the statement
  345. // crashes).
  346. template <typename T>
  347. inline T Invalid() {
  348. Assert(false, "", -1, "Internal error: attempt to return invalid value");
  349. // This statement is unreachable, and would never terminate even if it
  350. // could be reached. It is provided only to placate compiler warnings
  351. // about missing return statements.
  352. return Invalid<T>();
  353. }
  354. #ifdef _MSC_VER
  355. # pragma warning(pop)
  356. #endif
  357. // Given a raw type (i.e. having no top-level reference or const
  358. // modifier) RawContainer that's either an STL-style container or a
  359. // native array, class StlContainerView<RawContainer> has the
  360. // following members:
  361. //
  362. // - type is a type that provides an STL-style container view to
  363. // (i.e. implements the STL container concept for) RawContainer;
  364. // - const_reference is a type that provides a reference to a const
  365. // RawContainer;
  366. // - ConstReference(raw_container) returns a const reference to an STL-style
  367. // container view to raw_container, which is a RawContainer.
  368. // - Copy(raw_container) returns an STL-style container view of a
  369. // copy of raw_container, which is a RawContainer.
  370. //
  371. // This generic version is used when RawContainer itself is already an
  372. // STL-style container.
  373. template <class RawContainer>
  374. class StlContainerView {
  375. public:
  376. typedef RawContainer type;
  377. typedef const type& const_reference;
  378. static const_reference ConstReference(const RawContainer& container) {
  379. // Ensures that RawContainer is not a const type.
  380. testing::StaticAssertTypeEq<RawContainer,
  381. GTEST_REMOVE_CONST_(RawContainer)>();
  382. return container;
  383. }
  384. static type Copy(const RawContainer& container) { return container; }
  385. };
  386. // This specialization is used when RawContainer is a native array type.
  387. template <typename Element, size_t N>
  388. class StlContainerView<Element[N]> {
  389. public:
  390. typedef GTEST_REMOVE_CONST_(Element) RawElement;
  391. typedef internal::NativeArray<RawElement> type;
  392. // NativeArray<T> can represent a native array either by value or by
  393. // reference (selected by a constructor argument), so 'const type'
  394. // can be used to reference a const native array. We cannot
  395. // 'typedef const type& const_reference' here, as that would mean
  396. // ConstReference() has to return a reference to a local variable.
  397. typedef const type const_reference;
  398. static const_reference ConstReference(const Element (&array)[N]) {
  399. // Ensures that Element is not a const type.
  400. testing::StaticAssertTypeEq<Element, RawElement>();
  401. #if GTEST_OS_SYMBIAN
  402. // The Nokia Symbian compiler confuses itself in template instantiation
  403. // for this call without the cast to Element*:
  404. // function call '[testing::internal::NativeArray<char *>].NativeArray(
  405. // {lval} const char *[4], long, testing::internal::RelationToSource)'
  406. // does not match
  407. // 'testing::internal::NativeArray<char *>::NativeArray(
  408. // char *const *, unsigned int, testing::internal::RelationToSource)'
  409. // (instantiating: 'testing::internal::ContainsMatcherImpl
  410. // <const char * (&)[4]>::Matches(const char * (&)[4]) const')
  411. // (instantiating: 'testing::internal::StlContainerView<char *[4]>::
  412. // ConstReference(const char * (&)[4])')
  413. // (and though the N parameter type is mismatched in the above explicit
  414. // conversion of it doesn't help - only the conversion of the array).
  415. return type(const_cast<Element*>(&array[0]), N,
  416. RelationToSourceReference());
  417. #else
  418. return type(array, N, RelationToSourceReference());
  419. #endif // GTEST_OS_SYMBIAN
  420. }
  421. static type Copy(const Element (&array)[N]) {
  422. #if GTEST_OS_SYMBIAN
  423. return type(const_cast<Element*>(&array[0]), N, RelationToSourceCopy());
  424. #else
  425. return type(array, N, RelationToSourceCopy());
  426. #endif // GTEST_OS_SYMBIAN
  427. }
  428. };
  429. // This specialization is used when RawContainer is a native array
  430. // represented as a (pointer, size) tuple.
  431. template <typename ElementPointer, typename Size>
  432. class StlContainerView< ::testing::tuple<ElementPointer, Size> > {
  433. public:
  434. typedef GTEST_REMOVE_CONST_(
  435. typename internal::PointeeOf<ElementPointer>::type) RawElement;
  436. typedef internal::NativeArray<RawElement> type;
  437. typedef const type const_reference;
  438. static const_reference ConstReference(
  439. const ::testing::tuple<ElementPointer, Size>& array) {
  440. return type(get<0>(array), get<1>(array), RelationToSourceReference());
  441. }
  442. static type Copy(const ::testing::tuple<ElementPointer, Size>& array) {
  443. return type(get<0>(array), get<1>(array), RelationToSourceCopy());
  444. }
  445. };
  446. // The following specialization prevents the user from instantiating
  447. // StlContainer with a reference type.
  448. template <typename T> class StlContainerView<T&>;
  449. // A type transform to remove constness from the first part of a pair.
  450. // Pairs like that are used as the value_type of associative containers,
  451. // and this transform produces a similar but assignable pair.
  452. template <typename T>
  453. struct RemoveConstFromKey {
  454. typedef T type;
  455. };
  456. // Partially specialized to remove constness from std::pair<const K, V>.
  457. template <typename K, typename V>
  458. struct RemoveConstFromKey<std::pair<const K, V> > {
  459. typedef std::pair<K, V> type;
  460. };
  461. // Mapping from booleans to types. Similar to boost::bool_<kValue> and
  462. // std::integral_constant<bool, kValue>.
  463. template <bool kValue>
  464. struct BooleanConstant {};
  465. // Emit an assertion failure due to incorrect DoDefault() usage. Out-of-lined to
  466. // reduce code size.
  467. GTEST_API_ void IllegalDoDefault(const char* file, int line);
  468. #if GTEST_LANG_CXX11
  469. // Helper types for Apply() below.
  470. template <size_t... Is> struct int_pack { typedef int_pack type; };
  471. template <class Pack, size_t I> struct append;
  472. template <size_t... Is, size_t I>
  473. struct append<int_pack<Is...>, I> : int_pack<Is..., I> {};
  474. template <size_t C>
  475. struct make_int_pack : append<typename make_int_pack<C - 1>::type, C - 1> {};
  476. template <> struct make_int_pack<0> : int_pack<> {};
  477. template <typename F, typename Tuple, size_t... Idx>
  478. auto ApplyImpl(F&& f, Tuple&& args, int_pack<Idx...>) -> decltype(
  479. std::forward<F>(f)(std::get<Idx>(std::forward<Tuple>(args))...)) {
  480. return std::forward<F>(f)(std::get<Idx>(std::forward<Tuple>(args))...);
  481. }
  482. // Apply the function to a tuple of arguments.
  483. template <typename F, typename Tuple>
  484. auto Apply(F&& f, Tuple&& args)
  485. -> decltype(ApplyImpl(std::forward<F>(f), std::forward<Tuple>(args),
  486. make_int_pack<std::tuple_size<Tuple>::value>())) {
  487. return ApplyImpl(std::forward<F>(f), std::forward<Tuple>(args),
  488. make_int_pack<std::tuple_size<Tuple>::value>());
  489. }
  490. #endif
  491. #ifdef _MSC_VER
  492. # pragma warning(pop)
  493. #endif
  494. } // namespace internal
  495. } // namespace testing
  496. #endif // GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_