106f32e7eSjoerg // Copyright 2007, Google Inc.
206f32e7eSjoerg // All rights reserved.
306f32e7eSjoerg //
406f32e7eSjoerg // Redistribution and use in source and binary forms, with or without
506f32e7eSjoerg // modification, are permitted provided that the following conditions are
606f32e7eSjoerg // met:
706f32e7eSjoerg //
806f32e7eSjoerg //     * Redistributions of source code must retain the above copyright
906f32e7eSjoerg // notice, this list of conditions and the following disclaimer.
1006f32e7eSjoerg //     * Redistributions in binary form must reproduce the above
1106f32e7eSjoerg // copyright notice, this list of conditions and the following disclaimer
1206f32e7eSjoerg // in the documentation and/or other materials provided with the
1306f32e7eSjoerg // distribution.
1406f32e7eSjoerg //     * Neither the name of Google Inc. nor the names of its
1506f32e7eSjoerg // contributors may be used to endorse or promote products derived from
1606f32e7eSjoerg // this software without specific prior written permission.
1706f32e7eSjoerg //
1806f32e7eSjoerg // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1906f32e7eSjoerg // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2006f32e7eSjoerg // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2106f32e7eSjoerg // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2206f32e7eSjoerg // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2306f32e7eSjoerg // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2406f32e7eSjoerg // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2506f32e7eSjoerg // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2606f32e7eSjoerg // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2706f32e7eSjoerg // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2806f32e7eSjoerg // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*da58b97aSjoerg 
3006f32e7eSjoerg 
3106f32e7eSjoerg // Google Mock - a framework for writing C++ mock classes.
3206f32e7eSjoerg //
3306f32e7eSjoerg // This file defines some utilities useful for implementing Google
3406f32e7eSjoerg // Mock.  They are subject to change without notice, so please DO NOT
3506f32e7eSjoerg // USE THEM IN USER CODE.
3606f32e7eSjoerg 
37*da58b97aSjoerg // GOOGLETEST_CM0002 DO NOT DELETE
38*da58b97aSjoerg 
3906f32e7eSjoerg // IWYU pragma: private, include "gmock/gmock.h"
4006f32e7eSjoerg 
4106f32e7eSjoerg #ifndef GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_
4206f32e7eSjoerg #define GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_
4306f32e7eSjoerg 
4406f32e7eSjoerg #include <stdio.h>
4506f32e7eSjoerg #include <ostream>  // NOLINT
4606f32e7eSjoerg #include <string>
47*da58b97aSjoerg #include <type_traits>
4806f32e7eSjoerg #include "gmock/internal/gmock-port.h"
4906f32e7eSjoerg #include "gtest/gtest.h"
5006f32e7eSjoerg 
5106f32e7eSjoerg namespace testing {
52*da58b97aSjoerg 
53*da58b97aSjoerg template <typename>
54*da58b97aSjoerg class Matcher;
55*da58b97aSjoerg 
5606f32e7eSjoerg namespace internal {
5706f32e7eSjoerg 
58*da58b97aSjoerg // Silence MSVC C4100 (unreferenced formal parameter) and
59*da58b97aSjoerg // C4805('==': unsafe mix of type 'const int' and type 'const bool')
60*da58b97aSjoerg #ifdef _MSC_VER
61*da58b97aSjoerg # pragma warning(push)
62*da58b97aSjoerg # pragma warning(disable:4100)
63*da58b97aSjoerg # pragma warning(disable:4805)
64*da58b97aSjoerg #endif
65*da58b97aSjoerg 
66*da58b97aSjoerg // Joins a vector of strings as if they are fields of a tuple; returns
67*da58b97aSjoerg // the joined string.
68*da58b97aSjoerg GTEST_API_ std::string JoinAsTuple(const Strings& fields);
69*da58b97aSjoerg 
7006f32e7eSjoerg // Converts an identifier name to a space-separated list of lower-case
7106f32e7eSjoerg // words.  Each maximum substring of the form [A-Za-z][a-z]*|\d+ is
7206f32e7eSjoerg // treated as one word.  For example, both "FooBar123" and
7306f32e7eSjoerg // "foo_bar_123" are converted to "foo bar 123".
74*da58b97aSjoerg GTEST_API_ std::string ConvertIdentifierNameToWords(const char* id_name);
7506f32e7eSjoerg 
7606f32e7eSjoerg // PointeeOf<Pointer>::type is the type of a value pointed to by a
7706f32e7eSjoerg // Pointer, which can be either a smart pointer or a raw pointer.  The
7806f32e7eSjoerg // following default implementation is for the case where Pointer is a
7906f32e7eSjoerg // smart pointer.
8006f32e7eSjoerg template <typename Pointer>
8106f32e7eSjoerg struct PointeeOf {
8206f32e7eSjoerg   // Smart pointer classes define type element_type as the type of
8306f32e7eSjoerg   // their pointees.
8406f32e7eSjoerg   typedef typename Pointer::element_type type;
8506f32e7eSjoerg };
8606f32e7eSjoerg // This specialization is for the raw pointer case.
8706f32e7eSjoerg template <typename T>
8806f32e7eSjoerg struct PointeeOf<T*> { typedef T type; };  // NOLINT
8906f32e7eSjoerg 
9006f32e7eSjoerg // GetRawPointer(p) returns the raw pointer underlying p when p is a
9106f32e7eSjoerg // smart pointer, or returns p itself when p is already a raw pointer.
9206f32e7eSjoerg // The following default implementation is for the smart pointer case.
9306f32e7eSjoerg template <typename Pointer>
9406f32e7eSjoerg inline const typename Pointer::element_type* GetRawPointer(const Pointer& p) {
9506f32e7eSjoerg   return p.get();
9606f32e7eSjoerg }
9706f32e7eSjoerg // This overloaded version is for the raw pointer case.
9806f32e7eSjoerg template <typename Element>
9906f32e7eSjoerg inline Element* GetRawPointer(Element* p) { return p; }
10006f32e7eSjoerg 
10106f32e7eSjoerg // MSVC treats wchar_t as a native type usually, but treats it as the
10206f32e7eSjoerg // same as unsigned short when the compiler option /Zc:wchar_t- is
10306f32e7eSjoerg // specified.  It defines _NATIVE_WCHAR_T_DEFINED symbol when wchar_t
10406f32e7eSjoerg // is a native type.
105*da58b97aSjoerg #if defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED)
10606f32e7eSjoerg // wchar_t is a typedef.
10706f32e7eSjoerg #else
10806f32e7eSjoerg # define GMOCK_WCHAR_T_IS_NATIVE_ 1
10906f32e7eSjoerg #endif
11006f32e7eSjoerg 
11106f32e7eSjoerg // In what follows, we use the term "kind" to indicate whether a type
11206f32e7eSjoerg // is bool, an integer type (excluding bool), a floating-point type,
11306f32e7eSjoerg // or none of them.  This categorization is useful for determining
11406f32e7eSjoerg // when a matcher argument type can be safely converted to another
11506f32e7eSjoerg // type in the implementation of SafeMatcherCast.
11606f32e7eSjoerg enum TypeKind {
11706f32e7eSjoerg   kBool, kInteger, kFloatingPoint, kOther
11806f32e7eSjoerg };
11906f32e7eSjoerg 
12006f32e7eSjoerg // KindOf<T>::value is the kind of type T.
12106f32e7eSjoerg template <typename T> struct KindOf {
12206f32e7eSjoerg   enum { value = kOther };  // The default kind.
12306f32e7eSjoerg };
12406f32e7eSjoerg 
12506f32e7eSjoerg // This macro declares that the kind of 'type' is 'kind'.
12606f32e7eSjoerg #define GMOCK_DECLARE_KIND_(type, kind) \
12706f32e7eSjoerg   template <> struct KindOf<type> { enum { value = kind }; }
12806f32e7eSjoerg 
12906f32e7eSjoerg GMOCK_DECLARE_KIND_(bool, kBool);
13006f32e7eSjoerg 
13106f32e7eSjoerg // All standard integer types.
13206f32e7eSjoerg GMOCK_DECLARE_KIND_(char, kInteger);
13306f32e7eSjoerg GMOCK_DECLARE_KIND_(signed char, kInteger);
13406f32e7eSjoerg GMOCK_DECLARE_KIND_(unsigned char, kInteger);
13506f32e7eSjoerg GMOCK_DECLARE_KIND_(short, kInteger);  // NOLINT
13606f32e7eSjoerg GMOCK_DECLARE_KIND_(unsigned short, kInteger);  // NOLINT
13706f32e7eSjoerg GMOCK_DECLARE_KIND_(int, kInteger);
13806f32e7eSjoerg GMOCK_DECLARE_KIND_(unsigned int, kInteger);
13906f32e7eSjoerg GMOCK_DECLARE_KIND_(long, kInteger);  // NOLINT
14006f32e7eSjoerg GMOCK_DECLARE_KIND_(unsigned long, kInteger);  // NOLINT
14106f32e7eSjoerg 
14206f32e7eSjoerg #if GMOCK_WCHAR_T_IS_NATIVE_
14306f32e7eSjoerg GMOCK_DECLARE_KIND_(wchar_t, kInteger);
14406f32e7eSjoerg #endif
14506f32e7eSjoerg 
14606f32e7eSjoerg // Non-standard integer types.
14706f32e7eSjoerg GMOCK_DECLARE_KIND_(Int64, kInteger);
14806f32e7eSjoerg GMOCK_DECLARE_KIND_(UInt64, kInteger);
14906f32e7eSjoerg 
15006f32e7eSjoerg // All standard floating-point types.
15106f32e7eSjoerg GMOCK_DECLARE_KIND_(float, kFloatingPoint);
15206f32e7eSjoerg GMOCK_DECLARE_KIND_(double, kFloatingPoint);
15306f32e7eSjoerg GMOCK_DECLARE_KIND_(long double, kFloatingPoint);
15406f32e7eSjoerg 
15506f32e7eSjoerg #undef GMOCK_DECLARE_KIND_
15606f32e7eSjoerg 
15706f32e7eSjoerg // Evaluates to the kind of 'type'.
15806f32e7eSjoerg #define GMOCK_KIND_OF_(type) \
15906f32e7eSjoerg   static_cast< ::testing::internal::TypeKind>( \
16006f32e7eSjoerg       ::testing::internal::KindOf<type>::value)
16106f32e7eSjoerg 
162*da58b97aSjoerg // Evaluates to true if and only if integer type T is signed.
16306f32e7eSjoerg #define GMOCK_IS_SIGNED_(T) (static_cast<T>(-1) < 0)
16406f32e7eSjoerg 
16506f32e7eSjoerg // LosslessArithmeticConvertibleImpl<kFromKind, From, kToKind, To>::value
166*da58b97aSjoerg // is true if and only if arithmetic type From can be losslessly converted to
16706f32e7eSjoerg // arithmetic type To.
16806f32e7eSjoerg //
16906f32e7eSjoerg // It's the user's responsibility to ensure that both From and To are
17006f32e7eSjoerg // raw (i.e. has no CV modifier, is not a pointer, and is not a
17106f32e7eSjoerg // reference) built-in arithmetic types, kFromKind is the kind of
17206f32e7eSjoerg // From, and kToKind is the kind of To; the value is
17306f32e7eSjoerg // implementation-defined when the above pre-condition is violated.
17406f32e7eSjoerg template <TypeKind kFromKind, typename From, TypeKind kToKind, typename To>
175*da58b97aSjoerg struct LosslessArithmeticConvertibleImpl : public std::false_type {};
17606f32e7eSjoerg 
17706f32e7eSjoerg // Converting bool to bool is lossless.
17806f32e7eSjoerg template <>
17906f32e7eSjoerg struct LosslessArithmeticConvertibleImpl<kBool, bool, kBool, bool>
180*da58b97aSjoerg     : public std::true_type {};
18106f32e7eSjoerg 
18206f32e7eSjoerg // Converting bool to any integer type is lossless.
18306f32e7eSjoerg template <typename To>
18406f32e7eSjoerg struct LosslessArithmeticConvertibleImpl<kBool, bool, kInteger, To>
185*da58b97aSjoerg     : public std::true_type {};
18606f32e7eSjoerg 
18706f32e7eSjoerg // Converting bool to any floating-point type is lossless.
18806f32e7eSjoerg template <typename To>
18906f32e7eSjoerg struct LosslessArithmeticConvertibleImpl<kBool, bool, kFloatingPoint, To>
190*da58b97aSjoerg     : public std::true_type {};
19106f32e7eSjoerg 
19206f32e7eSjoerg // Converting an integer to bool is lossy.
19306f32e7eSjoerg template <typename From>
19406f32e7eSjoerg struct LosslessArithmeticConvertibleImpl<kInteger, From, kBool, bool>
195*da58b97aSjoerg     : public std::false_type {};
19606f32e7eSjoerg 
197*da58b97aSjoerg // Converting an integer to another non-bool integer is lossless
198*da58b97aSjoerg // if and only if the target type's range encloses the source type's range.
19906f32e7eSjoerg template <typename From, typename To>
20006f32e7eSjoerg struct LosslessArithmeticConvertibleImpl<kInteger, From, kInteger, To>
20106f32e7eSjoerg     : public bool_constant<
20206f32e7eSjoerg       // When converting from a smaller size to a larger size, we are
20306f32e7eSjoerg       // fine as long as we are not converting from signed to unsigned.
20406f32e7eSjoerg       ((sizeof(From) < sizeof(To)) &&
20506f32e7eSjoerg        (!GMOCK_IS_SIGNED_(From) || GMOCK_IS_SIGNED_(To))) ||
20606f32e7eSjoerg       // When converting between the same size, the signedness must match.
20706f32e7eSjoerg       ((sizeof(From) == sizeof(To)) &&
20806f32e7eSjoerg        (GMOCK_IS_SIGNED_(From) == GMOCK_IS_SIGNED_(To)))> {};  // NOLINT
20906f32e7eSjoerg 
21006f32e7eSjoerg #undef GMOCK_IS_SIGNED_
21106f32e7eSjoerg 
21206f32e7eSjoerg // Converting an integer to a floating-point type may be lossy, since
21306f32e7eSjoerg // the format of a floating-point number is implementation-defined.
21406f32e7eSjoerg template <typename From, typename To>
21506f32e7eSjoerg struct LosslessArithmeticConvertibleImpl<kInteger, From, kFloatingPoint, To>
216*da58b97aSjoerg     : public std::false_type {};
21706f32e7eSjoerg 
21806f32e7eSjoerg // Converting a floating-point to bool is lossy.
21906f32e7eSjoerg template <typename From>
22006f32e7eSjoerg struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kBool, bool>
221*da58b97aSjoerg     : public std::false_type {};
22206f32e7eSjoerg 
22306f32e7eSjoerg // Converting a floating-point to an integer is lossy.
22406f32e7eSjoerg template <typename From, typename To>
22506f32e7eSjoerg struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kInteger, To>
226*da58b97aSjoerg     : public std::false_type {};
22706f32e7eSjoerg 
22806f32e7eSjoerg // Converting a floating-point to another floating-point is lossless
229*da58b97aSjoerg // if and only if the target type is at least as big as the source type.
23006f32e7eSjoerg template <typename From, typename To>
23106f32e7eSjoerg struct LosslessArithmeticConvertibleImpl<
23206f32e7eSjoerg   kFloatingPoint, From, kFloatingPoint, To>
23306f32e7eSjoerg     : public bool_constant<sizeof(From) <= sizeof(To)> {};  // NOLINT
23406f32e7eSjoerg 
235*da58b97aSjoerg // LosslessArithmeticConvertible<From, To>::value is true if and only if
236*da58b97aSjoerg // arithmetic type From can be losslessly converted to arithmetic type To.
23706f32e7eSjoerg //
23806f32e7eSjoerg // It's the user's responsibility to ensure that both From and To are
23906f32e7eSjoerg // raw (i.e. has no CV modifier, is not a pointer, and is not a
24006f32e7eSjoerg // reference) built-in arithmetic types; the value is
24106f32e7eSjoerg // implementation-defined when the above pre-condition is violated.
24206f32e7eSjoerg template <typename From, typename To>
24306f32e7eSjoerg struct LosslessArithmeticConvertible
24406f32e7eSjoerg     : public LosslessArithmeticConvertibleImpl<
24506f32e7eSjoerg   GMOCK_KIND_OF_(From), From, GMOCK_KIND_OF_(To), To> {};  // NOLINT
24606f32e7eSjoerg 
24706f32e7eSjoerg // This interface knows how to report a Google Mock failure (either
24806f32e7eSjoerg // non-fatal or fatal).
24906f32e7eSjoerg class FailureReporterInterface {
25006f32e7eSjoerg  public:
25106f32e7eSjoerg   // The type of a failure (either non-fatal or fatal).
25206f32e7eSjoerg   enum FailureType {
25306f32e7eSjoerg     kNonfatal, kFatal
25406f32e7eSjoerg   };
25506f32e7eSjoerg 
25606f32e7eSjoerg   virtual ~FailureReporterInterface() {}
25706f32e7eSjoerg 
25806f32e7eSjoerg   // Reports a failure that occurred at the given source file location.
25906f32e7eSjoerg   virtual void ReportFailure(FailureType type, const char* file, int line,
260*da58b97aSjoerg                              const std::string& message) = 0;
26106f32e7eSjoerg };
26206f32e7eSjoerg 
26306f32e7eSjoerg // Returns the failure reporter used by Google Mock.
26406f32e7eSjoerg GTEST_API_ FailureReporterInterface* GetFailureReporter();
26506f32e7eSjoerg 
26606f32e7eSjoerg // Asserts that condition is true; aborts the process with the given
26706f32e7eSjoerg // message if condition is false.  We cannot use LOG(FATAL) or CHECK()
26806f32e7eSjoerg // as Google Mock might be used to mock the log sink itself.  We
26906f32e7eSjoerg // inline this function to prevent it from showing up in the stack
27006f32e7eSjoerg // trace.
27106f32e7eSjoerg inline void Assert(bool condition, const char* file, int line,
272*da58b97aSjoerg                    const std::string& msg) {
27306f32e7eSjoerg   if (!condition) {
27406f32e7eSjoerg     GetFailureReporter()->ReportFailure(FailureReporterInterface::kFatal,
27506f32e7eSjoerg                                         file, line, msg);
27606f32e7eSjoerg   }
27706f32e7eSjoerg }
27806f32e7eSjoerg inline void Assert(bool condition, const char* file, int line) {
27906f32e7eSjoerg   Assert(condition, file, line, "Assertion failed.");
28006f32e7eSjoerg }
28106f32e7eSjoerg 
28206f32e7eSjoerg // Verifies that condition is true; generates a non-fatal failure if
28306f32e7eSjoerg // condition is false.
28406f32e7eSjoerg inline void Expect(bool condition, const char* file, int line,
285*da58b97aSjoerg                    const std::string& msg) {
28606f32e7eSjoerg   if (!condition) {
28706f32e7eSjoerg     GetFailureReporter()->ReportFailure(FailureReporterInterface::kNonfatal,
28806f32e7eSjoerg                                         file, line, msg);
28906f32e7eSjoerg   }
29006f32e7eSjoerg }
29106f32e7eSjoerg inline void Expect(bool condition, const char* file, int line) {
29206f32e7eSjoerg   Expect(condition, file, line, "Expectation failed.");
29306f32e7eSjoerg }
29406f32e7eSjoerg 
29506f32e7eSjoerg // Severity level of a log.
29606f32e7eSjoerg enum LogSeverity {
29706f32e7eSjoerg   kInfo = 0,
29806f32e7eSjoerg   kWarning = 1
29906f32e7eSjoerg };
30006f32e7eSjoerg 
30106f32e7eSjoerg // Valid values for the --gmock_verbose flag.
30206f32e7eSjoerg 
30306f32e7eSjoerg // All logs (informational and warnings) are printed.
30406f32e7eSjoerg const char kInfoVerbosity[] = "info";
30506f32e7eSjoerg // Only warnings are printed.
30606f32e7eSjoerg const char kWarningVerbosity[] = "warning";
30706f32e7eSjoerg // No logs are printed.
30806f32e7eSjoerg const char kErrorVerbosity[] = "error";
30906f32e7eSjoerg 
310*da58b97aSjoerg // Returns true if and only if a log with the given severity is visible
311*da58b97aSjoerg // according to the --gmock_verbose flag.
31206f32e7eSjoerg GTEST_API_ bool LogIsVisible(LogSeverity severity);
31306f32e7eSjoerg 
314*da58b97aSjoerg // Prints the given message to stdout if and only if 'severity' >= the level
31506f32e7eSjoerg // specified by the --gmock_verbose flag.  If stack_frames_to_skip >=
31606f32e7eSjoerg // 0, also prints the stack trace excluding the top
31706f32e7eSjoerg // stack_frames_to_skip frames.  In opt mode, any positive
31806f32e7eSjoerg // stack_frames_to_skip is treated as 0, since we don't know which
31906f32e7eSjoerg // function calls will be inlined by the compiler and need to be
32006f32e7eSjoerg // conservative.
321*da58b97aSjoerg GTEST_API_ void Log(LogSeverity severity, const std::string& message,
32206f32e7eSjoerg                     int stack_frames_to_skip);
32306f32e7eSjoerg 
324*da58b97aSjoerg // A marker class that is used to resolve parameterless expectations to the
325*da58b97aSjoerg // correct overload. This must not be instantiable, to prevent client code from
326*da58b97aSjoerg // accidentally resolving to the overload; for example:
327*da58b97aSjoerg //
328*da58b97aSjoerg //    ON_CALL(mock, Method({}, nullptr))...
329*da58b97aSjoerg //
330*da58b97aSjoerg class WithoutMatchers {
331*da58b97aSjoerg  private:
332*da58b97aSjoerg   WithoutMatchers() {}
333*da58b97aSjoerg   friend GTEST_API_ WithoutMatchers GetWithoutMatchers();
334*da58b97aSjoerg };
335*da58b97aSjoerg 
336*da58b97aSjoerg // Internal use only: access the singleton instance of WithoutMatchers.
337*da58b97aSjoerg GTEST_API_ WithoutMatchers GetWithoutMatchers();
33806f32e7eSjoerg 
33906f32e7eSjoerg // Type traits.
34006f32e7eSjoerg 
34106f32e7eSjoerg // Disable MSVC warnings for infinite recursion, since in this case the
34206f32e7eSjoerg // the recursion is unreachable.
34306f32e7eSjoerg #ifdef _MSC_VER
34406f32e7eSjoerg # pragma warning(push)
34506f32e7eSjoerg # pragma warning(disable:4717)
34606f32e7eSjoerg #endif
34706f32e7eSjoerg 
34806f32e7eSjoerg // Invalid<T>() is usable as an expression of type T, but will terminate
34906f32e7eSjoerg // the program with an assertion failure if actually run.  This is useful
35006f32e7eSjoerg // when a value of type T is needed for compilation, but the statement
35106f32e7eSjoerg // will not really be executed (or we don't care if the statement
35206f32e7eSjoerg // crashes).
35306f32e7eSjoerg template <typename T>
35406f32e7eSjoerg inline T Invalid() {
35506f32e7eSjoerg   Assert(false, "", -1, "Internal error: attempt to return invalid value");
35606f32e7eSjoerg   // This statement is unreachable, and would never terminate even if it
35706f32e7eSjoerg   // could be reached. It is provided only to placate compiler warnings
35806f32e7eSjoerg   // about missing return statements.
35906f32e7eSjoerg   return Invalid<T>();
36006f32e7eSjoerg }
36106f32e7eSjoerg 
36206f32e7eSjoerg #ifdef _MSC_VER
36306f32e7eSjoerg # pragma warning(pop)
36406f32e7eSjoerg #endif
36506f32e7eSjoerg 
36606f32e7eSjoerg // Given a raw type (i.e. having no top-level reference or const
36706f32e7eSjoerg // modifier) RawContainer that's either an STL-style container or a
36806f32e7eSjoerg // native array, class StlContainerView<RawContainer> has the
36906f32e7eSjoerg // following members:
37006f32e7eSjoerg //
37106f32e7eSjoerg //   - type is a type that provides an STL-style container view to
37206f32e7eSjoerg //     (i.e. implements the STL container concept for) RawContainer;
37306f32e7eSjoerg //   - const_reference is a type that provides a reference to a const
37406f32e7eSjoerg //     RawContainer;
37506f32e7eSjoerg //   - ConstReference(raw_container) returns a const reference to an STL-style
37606f32e7eSjoerg //     container view to raw_container, which is a RawContainer.
37706f32e7eSjoerg //   - Copy(raw_container) returns an STL-style container view of a
37806f32e7eSjoerg //     copy of raw_container, which is a RawContainer.
37906f32e7eSjoerg //
38006f32e7eSjoerg // This generic version is used when RawContainer itself is already an
38106f32e7eSjoerg // STL-style container.
38206f32e7eSjoerg template <class RawContainer>
38306f32e7eSjoerg class StlContainerView {
38406f32e7eSjoerg  public:
38506f32e7eSjoerg   typedef RawContainer type;
38606f32e7eSjoerg   typedef const type& const_reference;
38706f32e7eSjoerg 
38806f32e7eSjoerg   static const_reference ConstReference(const RawContainer& container) {
389*da58b97aSjoerg     static_assert(!std::is_const<RawContainer>::value,
390*da58b97aSjoerg                   "RawContainer type must not be const");
39106f32e7eSjoerg     return container;
39206f32e7eSjoerg   }
39306f32e7eSjoerg   static type Copy(const RawContainer& container) { return container; }
39406f32e7eSjoerg };
39506f32e7eSjoerg 
39606f32e7eSjoerg // This specialization is used when RawContainer is a native array type.
39706f32e7eSjoerg template <typename Element, size_t N>
39806f32e7eSjoerg class StlContainerView<Element[N]> {
39906f32e7eSjoerg  public:
400*da58b97aSjoerg   typedef typename std::remove_const<Element>::type RawElement;
40106f32e7eSjoerg   typedef internal::NativeArray<RawElement> type;
40206f32e7eSjoerg   // NativeArray<T> can represent a native array either by value or by
40306f32e7eSjoerg   // reference (selected by a constructor argument), so 'const type'
40406f32e7eSjoerg   // can be used to reference a const native array.  We cannot
40506f32e7eSjoerg   // 'typedef const type& const_reference' here, as that would mean
40606f32e7eSjoerg   // ConstReference() has to return a reference to a local variable.
40706f32e7eSjoerg   typedef const type const_reference;
40806f32e7eSjoerg 
40906f32e7eSjoerg   static const_reference ConstReference(const Element (&array)[N]) {
410*da58b97aSjoerg     static_assert(std::is_same<Element, RawElement>::value,
411*da58b97aSjoerg                   "Element type must not be const");
41206f32e7eSjoerg     return type(array, N, RelationToSourceReference());
41306f32e7eSjoerg   }
41406f32e7eSjoerg   static type Copy(const Element (&array)[N]) {
41506f32e7eSjoerg     return type(array, N, RelationToSourceCopy());
41606f32e7eSjoerg   }
41706f32e7eSjoerg };
41806f32e7eSjoerg 
41906f32e7eSjoerg // This specialization is used when RawContainer is a native array
42006f32e7eSjoerg // represented as a (pointer, size) tuple.
42106f32e7eSjoerg template <typename ElementPointer, typename Size>
422*da58b97aSjoerg class StlContainerView< ::std::tuple<ElementPointer, Size> > {
42306f32e7eSjoerg  public:
424*da58b97aSjoerg   typedef typename std::remove_const<
425*da58b97aSjoerg       typename internal::PointeeOf<ElementPointer>::type>::type RawElement;
42606f32e7eSjoerg   typedef internal::NativeArray<RawElement> type;
42706f32e7eSjoerg   typedef const type const_reference;
42806f32e7eSjoerg 
42906f32e7eSjoerg   static const_reference ConstReference(
430*da58b97aSjoerg       const ::std::tuple<ElementPointer, Size>& array) {
431*da58b97aSjoerg     return type(std::get<0>(array), std::get<1>(array),
432*da58b97aSjoerg                 RelationToSourceReference());
43306f32e7eSjoerg   }
434*da58b97aSjoerg   static type Copy(const ::std::tuple<ElementPointer, Size>& array) {
435*da58b97aSjoerg     return type(std::get<0>(array), std::get<1>(array), RelationToSourceCopy());
43606f32e7eSjoerg   }
43706f32e7eSjoerg };
43806f32e7eSjoerg 
43906f32e7eSjoerg // The following specialization prevents the user from instantiating
44006f32e7eSjoerg // StlContainer with a reference type.
44106f32e7eSjoerg template <typename T> class StlContainerView<T&>;
44206f32e7eSjoerg 
44306f32e7eSjoerg // A type transform to remove constness from the first part of a pair.
44406f32e7eSjoerg // Pairs like that are used as the value_type of associative containers,
44506f32e7eSjoerg // and this transform produces a similar but assignable pair.
44606f32e7eSjoerg template <typename T>
44706f32e7eSjoerg struct RemoveConstFromKey {
44806f32e7eSjoerg   typedef T type;
44906f32e7eSjoerg };
45006f32e7eSjoerg 
45106f32e7eSjoerg // Partially specialized to remove constness from std::pair<const K, V>.
45206f32e7eSjoerg template <typename K, typename V>
45306f32e7eSjoerg struct RemoveConstFromKey<std::pair<const K, V> > {
45406f32e7eSjoerg   typedef std::pair<K, V> type;
45506f32e7eSjoerg };
45606f32e7eSjoerg 
457*da58b97aSjoerg // Emit an assertion failure due to incorrect DoDefault() usage. Out-of-lined to
458*da58b97aSjoerg // reduce code size.
459*da58b97aSjoerg GTEST_API_ void IllegalDoDefault(const char* file, int line);
460*da58b97aSjoerg 
461*da58b97aSjoerg template <typename F, typename Tuple, size_t... Idx>
462*da58b97aSjoerg auto ApplyImpl(F&& f, Tuple&& args, IndexSequence<Idx...>) -> decltype(
463*da58b97aSjoerg     std::forward<F>(f)(std::get<Idx>(std::forward<Tuple>(args))...)) {
464*da58b97aSjoerg   return std::forward<F>(f)(std::get<Idx>(std::forward<Tuple>(args))...);
465*da58b97aSjoerg }
466*da58b97aSjoerg 
467*da58b97aSjoerg // Apply the function to a tuple of arguments.
468*da58b97aSjoerg template <typename F, typename Tuple>
469*da58b97aSjoerg auto Apply(F&& f, Tuple&& args)
470*da58b97aSjoerg     -> decltype(ApplyImpl(std::forward<F>(f), std::forward<Tuple>(args),
471*da58b97aSjoerg                           MakeIndexSequence<std::tuple_size<Tuple>::value>())) {
472*da58b97aSjoerg   return ApplyImpl(std::forward<F>(f), std::forward<Tuple>(args),
473*da58b97aSjoerg                    MakeIndexSequence<std::tuple_size<Tuple>::value>());
474*da58b97aSjoerg }
475*da58b97aSjoerg 
476*da58b97aSjoerg // Template struct Function<F>, where F must be a function type, contains
477*da58b97aSjoerg // the following typedefs:
478*da58b97aSjoerg //
479*da58b97aSjoerg //   Result:               the function's return type.
480*da58b97aSjoerg //   Arg<N>:               the type of the N-th argument, where N starts with 0.
481*da58b97aSjoerg //   ArgumentTuple:        the tuple type consisting of all parameters of F.
482*da58b97aSjoerg //   ArgumentMatcherTuple: the tuple type consisting of Matchers for all
483*da58b97aSjoerg //                         parameters of F.
484*da58b97aSjoerg //   MakeResultVoid:       the function type obtained by substituting void
485*da58b97aSjoerg //                         for the return type of F.
486*da58b97aSjoerg //   MakeResultIgnoredValue:
487*da58b97aSjoerg //                         the function type obtained by substituting Something
488*da58b97aSjoerg //                         for the return type of F.
489*da58b97aSjoerg template <typename T>
490*da58b97aSjoerg struct Function;
491*da58b97aSjoerg 
492*da58b97aSjoerg template <typename R, typename... Args>
493*da58b97aSjoerg struct Function<R(Args...)> {
494*da58b97aSjoerg   using Result = R;
495*da58b97aSjoerg   static constexpr size_t ArgumentCount = sizeof...(Args);
496*da58b97aSjoerg   template <size_t I>
497*da58b97aSjoerg   using Arg = ElemFromList<I, typename MakeIndexSequence<sizeof...(Args)>::type,
498*da58b97aSjoerg                            Args...>;
499*da58b97aSjoerg   using ArgumentTuple = std::tuple<Args...>;
500*da58b97aSjoerg   using ArgumentMatcherTuple = std::tuple<Matcher<Args>...>;
501*da58b97aSjoerg   using MakeResultVoid = void(Args...);
502*da58b97aSjoerg   using MakeResultIgnoredValue = IgnoredValue(Args...);
503*da58b97aSjoerg };
504*da58b97aSjoerg 
505*da58b97aSjoerg template <typename R, typename... Args>
506*da58b97aSjoerg constexpr size_t Function<R(Args...)>::ArgumentCount;
507*da58b97aSjoerg 
508*da58b97aSjoerg #ifdef _MSC_VER
509*da58b97aSjoerg # pragma warning(pop)
510*da58b97aSjoerg #endif
51106f32e7eSjoerg 
51206f32e7eSjoerg }  // namespace internal
51306f32e7eSjoerg }  // namespace testing
51406f32e7eSjoerg 
51506f32e7eSjoerg #endif  // GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_
516