Blame googlemock/include/gmock/internal/gmock-internal-utils.h

Packit bd1cd8
// Copyright 2007, Google Inc.
Packit bd1cd8
// All rights reserved.
Packit bd1cd8
//
Packit bd1cd8
// Redistribution and use in source and binary forms, with or without
Packit bd1cd8
// modification, are permitted provided that the following conditions are
Packit bd1cd8
// met:
Packit bd1cd8
//
Packit bd1cd8
//     * Redistributions of source code must retain the above copyright
Packit bd1cd8
// notice, this list of conditions and the following disclaimer.
Packit bd1cd8
//     * Redistributions in binary form must reproduce the above
Packit bd1cd8
// copyright notice, this list of conditions and the following disclaimer
Packit bd1cd8
// in the documentation and/or other materials provided with the
Packit bd1cd8
// distribution.
Packit bd1cd8
//     * Neither the name of Google Inc. nor the names of its
Packit bd1cd8
// contributors may be used to endorse or promote products derived from
Packit bd1cd8
// this software without specific prior written permission.
Packit bd1cd8
//
Packit bd1cd8
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit bd1cd8
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit bd1cd8
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit bd1cd8
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit bd1cd8
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit bd1cd8
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit bd1cd8
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit bd1cd8
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit bd1cd8
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit bd1cd8
//
Packit bd1cd8
// Author: wan@google.com (Zhanyong Wan)
Packit bd1cd8
Packit bd1cd8
// Google Mock - a framework for writing C++ mock classes.
Packit bd1cd8
//
Packit bd1cd8
// This file defines some utilities useful for implementing Google
Packit bd1cd8
// Mock.  They are subject to change without notice, so please DO NOT
Packit bd1cd8
// USE THEM IN USER CODE.
Packit bd1cd8
Packit bd1cd8
#ifndef GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_
Packit bd1cd8
#define GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_
Packit bd1cd8
Packit bd1cd8
#include <stdio.h>
Packit bd1cd8
#include <ostream>  // NOLINT
Packit bd1cd8
#include <string>
Packit bd1cd8
Packit bd1cd8
#include "gmock/internal/gmock-generated-internal-utils.h"
Packit bd1cd8
#include "gmock/internal/gmock-port.h"
Packit bd1cd8
#include "gtest/gtest.h"
Packit bd1cd8
Packit bd1cd8
namespace testing {
Packit bd1cd8
namespace internal {
Packit bd1cd8
Packit bd1cd8
// Converts an identifier name to a space-separated list of lower-case
Packit bd1cd8
// words.  Each maximum substring of the form [A-Za-z][a-z]*|\d+ is
Packit bd1cd8
// treated as one word.  For example, both "FooBar123" and
Packit bd1cd8
// "foo_bar_123" are converted to "foo bar 123".
Packit bd1cd8
GTEST_API_ string ConvertIdentifierNameToWords(const char* id_name);
Packit bd1cd8
Packit bd1cd8
// PointeeOf<Pointer>::type is the type of a value pointed to by a
Packit bd1cd8
// Pointer, which can be either a smart pointer or a raw pointer.  The
Packit bd1cd8
// following default implementation is for the case where Pointer is a
Packit bd1cd8
// smart pointer.
Packit bd1cd8
template <typename Pointer>
Packit bd1cd8
struct PointeeOf {
Packit bd1cd8
  // Smart pointer classes define type element_type as the type of
Packit bd1cd8
  // their pointees.
Packit bd1cd8
  typedef typename Pointer::element_type type;
Packit bd1cd8
};
Packit bd1cd8
// This specialization is for the raw pointer case.
Packit bd1cd8
template <typename T>
Packit bd1cd8
struct PointeeOf<T*> { typedef T type; };  // NOLINT
Packit bd1cd8
Packit bd1cd8
// GetRawPointer(p) returns the raw pointer underlying p when p is a
Packit bd1cd8
// smart pointer, or returns p itself when p is already a raw pointer.
Packit bd1cd8
// The following default implementation is for the smart pointer case.
Packit bd1cd8
template <typename Pointer>
Packit bd1cd8
inline const typename Pointer::element_type* GetRawPointer(const Pointer& p) {
Packit bd1cd8
  return p.get();
Packit bd1cd8
}
Packit bd1cd8
// This overloaded version is for the raw pointer case.
Packit bd1cd8
template <typename Element>
Packit bd1cd8
inline Element* GetRawPointer(Element* p) { return p; }
Packit bd1cd8
Packit bd1cd8
// This comparator allows linked_ptr to be stored in sets.
Packit bd1cd8
template <typename T>
Packit bd1cd8
struct LinkedPtrLessThan {
Packit bd1cd8
  bool operator()(const ::testing::internal::linked_ptr<T>& lhs,
Packit bd1cd8
                  const ::testing::internal::linked_ptr<T>& rhs) const {
Packit bd1cd8
    return lhs.get() < rhs.get();
Packit bd1cd8
  }
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Symbian compilation can be done with wchar_t being either a native
Packit bd1cd8
// type or a typedef.  Using Google Mock with OpenC without wchar_t
Packit bd1cd8
// should require the definition of _STLP_NO_WCHAR_T.
Packit bd1cd8
//
Packit bd1cd8
// MSVC treats wchar_t as a native type usually, but treats it as the
Packit bd1cd8
// same as unsigned short when the compiler option /Zc:wchar_t- is
Packit bd1cd8
// specified.  It defines _NATIVE_WCHAR_T_DEFINED symbol when wchar_t
Packit bd1cd8
// is a native type.
Packit bd1cd8
#if (GTEST_OS_SYMBIAN && defined(_STLP_NO_WCHAR_T)) || \
Packit bd1cd8
    (defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED))
Packit bd1cd8
// wchar_t is a typedef.
Packit bd1cd8
#else
Packit bd1cd8
# define GMOCK_WCHAR_T_IS_NATIVE_ 1
Packit bd1cd8
#endif
Packit bd1cd8
Packit bd1cd8
// signed wchar_t and unsigned wchar_t are NOT in the C++ standard.
Packit bd1cd8
// Using them is a bad practice and not portable.  So DON'T use them.
Packit bd1cd8
//
Packit bd1cd8
// Still, Google Mock is designed to work even if the user uses signed
Packit bd1cd8
// wchar_t or unsigned wchar_t (obviously, assuming the compiler
Packit bd1cd8
// supports them).
Packit bd1cd8
//
Packit bd1cd8
// To gcc,
Packit bd1cd8
//   wchar_t == signed wchar_t != unsigned wchar_t == unsigned int
Packit bd1cd8
#ifdef __GNUC__
Packit bd1cd8
// signed/unsigned wchar_t are valid types.
Packit bd1cd8
# define GMOCK_HAS_SIGNED_WCHAR_T_ 1
Packit bd1cd8
#endif
Packit bd1cd8
Packit bd1cd8
// In what follows, we use the term "kind" to indicate whether a type
Packit bd1cd8
// is bool, an integer type (excluding bool), a floating-point type,
Packit bd1cd8
// or none of them.  This categorization is useful for determining
Packit bd1cd8
// when a matcher argument type can be safely converted to another
Packit bd1cd8
// type in the implementation of SafeMatcherCast.
Packit bd1cd8
enum TypeKind {
Packit bd1cd8
  kBool, kInteger, kFloatingPoint, kOther
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// KindOf<T>::value is the kind of type T.
Packit bd1cd8
template <typename T> struct KindOf {
Packit bd1cd8
  enum { value = kOther };  // The default kind.
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// This macro declares that the kind of 'type' is 'kind'.
Packit bd1cd8
#define GMOCK_DECLARE_KIND_(type, kind) \
Packit bd1cd8
  template <> struct KindOf<type> { enum { value = kind }; }
Packit bd1cd8
Packit bd1cd8
GMOCK_DECLARE_KIND_(bool, kBool);
Packit bd1cd8
Packit bd1cd8
// All standard integer types.
Packit bd1cd8
GMOCK_DECLARE_KIND_(char, kInteger);
Packit bd1cd8
GMOCK_DECLARE_KIND_(signed char, kInteger);
Packit bd1cd8
GMOCK_DECLARE_KIND_(unsigned char, kInteger);
Packit bd1cd8
GMOCK_DECLARE_KIND_(short, kInteger);  // NOLINT
Packit bd1cd8
GMOCK_DECLARE_KIND_(unsigned short, kInteger);  // NOLINT
Packit bd1cd8
GMOCK_DECLARE_KIND_(int, kInteger);
Packit bd1cd8
GMOCK_DECLARE_KIND_(unsigned int, kInteger);
Packit bd1cd8
GMOCK_DECLARE_KIND_(long, kInteger);  // NOLINT
Packit bd1cd8
GMOCK_DECLARE_KIND_(unsigned long, kInteger);  // NOLINT
Packit bd1cd8
Packit bd1cd8
#if GMOCK_WCHAR_T_IS_NATIVE_
Packit bd1cd8
GMOCK_DECLARE_KIND_(wchar_t, kInteger);
Packit bd1cd8
#endif
Packit bd1cd8
Packit bd1cd8
// Non-standard integer types.
Packit bd1cd8
GMOCK_DECLARE_KIND_(Int64, kInteger);
Packit bd1cd8
GMOCK_DECLARE_KIND_(UInt64, kInteger);
Packit bd1cd8
Packit bd1cd8
// All standard floating-point types.
Packit bd1cd8
GMOCK_DECLARE_KIND_(float, kFloatingPoint);
Packit bd1cd8
GMOCK_DECLARE_KIND_(double, kFloatingPoint);
Packit bd1cd8
GMOCK_DECLARE_KIND_(long double, kFloatingPoint);
Packit bd1cd8
Packit bd1cd8
#undef GMOCK_DECLARE_KIND_
Packit bd1cd8
Packit bd1cd8
// Evaluates to the kind of 'type'.
Packit bd1cd8
#define GMOCK_KIND_OF_(type) \
Packit bd1cd8
  static_cast< ::testing::internal::TypeKind>( \
Packit bd1cd8
      ::testing::internal::KindOf<type>::value)
Packit bd1cd8
Packit bd1cd8
// Evaluates to true iff integer type T is signed.
Packit bd1cd8
#define GMOCK_IS_SIGNED_(T) (static_cast<T>(-1) < 0)
Packit bd1cd8
Packit bd1cd8
// LosslessArithmeticConvertibleImpl<kFromKind, From, kToKind, To>::value
Packit bd1cd8
// is true iff arithmetic type From can be losslessly converted to
Packit bd1cd8
// arithmetic type To.
Packit bd1cd8
//
Packit bd1cd8
// It's the user's responsibility to ensure that both From and To are
Packit bd1cd8
// raw (i.e. has no CV modifier, is not a pointer, and is not a
Packit bd1cd8
// reference) built-in arithmetic types, kFromKind is the kind of
Packit bd1cd8
// From, and kToKind is the kind of To; the value is
Packit bd1cd8
// implementation-defined when the above pre-condition is violated.
Packit bd1cd8
template <TypeKind kFromKind, typename From, TypeKind kToKind, typename To>
Packit bd1cd8
struct LosslessArithmeticConvertibleImpl : public false_type {};
Packit bd1cd8
Packit bd1cd8
// Converting bool to bool is lossless.
Packit bd1cd8
template <>
Packit bd1cd8
struct LosslessArithmeticConvertibleImpl<kBool, bool, kBool, bool>
Packit bd1cd8
    : public true_type {};  // NOLINT
Packit bd1cd8
Packit bd1cd8
// Converting bool to any integer type is lossless.
Packit bd1cd8
template <typename To>
Packit bd1cd8
struct LosslessArithmeticConvertibleImpl<kBool, bool, kInteger, To>
Packit bd1cd8
    : public true_type {};  // NOLINT
Packit bd1cd8
Packit bd1cd8
// Converting bool to any floating-point type is lossless.
Packit bd1cd8
template <typename To>
Packit bd1cd8
struct LosslessArithmeticConvertibleImpl<kBool, bool, kFloatingPoint, To>
Packit bd1cd8
    : public true_type {};  // NOLINT
Packit bd1cd8
Packit bd1cd8
// Converting an integer to bool is lossy.
Packit bd1cd8
template <typename From>
Packit bd1cd8
struct LosslessArithmeticConvertibleImpl<kInteger, From, kBool, bool>
Packit bd1cd8
    : public false_type {};  // NOLINT
Packit bd1cd8
Packit bd1cd8
// Converting an integer to another non-bool integer is lossless iff
Packit bd1cd8
// the target type's range encloses the source type's range.
Packit bd1cd8
template <typename From, typename To>
Packit bd1cd8
struct LosslessArithmeticConvertibleImpl<kInteger, From, kInteger, To>
Packit bd1cd8
    : public bool_constant<
Packit bd1cd8
      // When converting from a smaller size to a larger size, we are
Packit bd1cd8
      // fine as long as we are not converting from signed to unsigned.
Packit bd1cd8
      ((sizeof(From) < sizeof(To)) &&
Packit bd1cd8
       (!GMOCK_IS_SIGNED_(From) || GMOCK_IS_SIGNED_(To))) ||
Packit bd1cd8
      // When converting between the same size, the signedness must match.
Packit bd1cd8
      ((sizeof(From) == sizeof(To)) &&
Packit bd1cd8
       (GMOCK_IS_SIGNED_(From) == GMOCK_IS_SIGNED_(To)))> {};  // NOLINT
Packit bd1cd8
Packit bd1cd8
#undef GMOCK_IS_SIGNED_
Packit bd1cd8
Packit bd1cd8
// Converting an integer to a floating-point type may be lossy, since
Packit bd1cd8
// the format of a floating-point number is implementation-defined.
Packit bd1cd8
template <typename From, typename To>
Packit bd1cd8
struct LosslessArithmeticConvertibleImpl<kInteger, From, kFloatingPoint, To>
Packit bd1cd8
    : public false_type {};  // NOLINT
Packit bd1cd8
Packit bd1cd8
// Converting a floating-point to bool is lossy.
Packit bd1cd8
template <typename From>
Packit bd1cd8
struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kBool, bool>
Packit bd1cd8
    : public false_type {};  // NOLINT
Packit bd1cd8
Packit bd1cd8
// Converting a floating-point to an integer is lossy.
Packit bd1cd8
template <typename From, typename To>
Packit bd1cd8
struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kInteger, To>
Packit bd1cd8
    : public false_type {};  // NOLINT
Packit bd1cd8
Packit bd1cd8
// Converting a floating-point to another floating-point is lossless
Packit bd1cd8
// iff the target type is at least as big as the source type.
Packit bd1cd8
template <typename From, typename To>
Packit bd1cd8
struct LosslessArithmeticConvertibleImpl<
Packit bd1cd8
  kFloatingPoint, From, kFloatingPoint, To>
Packit bd1cd8
    : public bool_constant<sizeof(From) <= sizeof(To)> {};  // NOLINT
Packit bd1cd8
Packit bd1cd8
// LosslessArithmeticConvertible<From, To>::value is true iff arithmetic
Packit bd1cd8
// type From can be losslessly converted to arithmetic type To.
Packit bd1cd8
//
Packit bd1cd8
// It's the user's responsibility to ensure that both From and To are
Packit bd1cd8
// raw (i.e. has no CV modifier, is not a pointer, and is not a
Packit bd1cd8
// reference) built-in arithmetic types; the value is
Packit bd1cd8
// implementation-defined when the above pre-condition is violated.
Packit bd1cd8
template <typename From, typename To>
Packit bd1cd8
struct LosslessArithmeticConvertible
Packit bd1cd8
    : public LosslessArithmeticConvertibleImpl<
Packit bd1cd8
  GMOCK_KIND_OF_(From), From, GMOCK_KIND_OF_(To), To> {};  // NOLINT
Packit bd1cd8
Packit bd1cd8
// This interface knows how to report a Google Mock failure (either
Packit bd1cd8
// non-fatal or fatal).
Packit bd1cd8
class FailureReporterInterface {
Packit bd1cd8
 public:
Packit bd1cd8
  // The type of a failure (either non-fatal or fatal).
Packit bd1cd8
  enum FailureType {
Packit bd1cd8
    kNonfatal, kFatal
Packit bd1cd8
  };
Packit bd1cd8
Packit bd1cd8
  virtual ~FailureReporterInterface() {}
Packit bd1cd8
Packit bd1cd8
  // Reports a failure that occurred at the given source file location.
Packit bd1cd8
  virtual void ReportFailure(FailureType type, const char* file, int line,
Packit bd1cd8
                             const string& message) = 0;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Returns the failure reporter used by Google Mock.
Packit bd1cd8
GTEST_API_ FailureReporterInterface* GetFailureReporter();
Packit bd1cd8
Packit bd1cd8
// Asserts that condition is true; aborts the process with the given
Packit bd1cd8
// message if condition is false.  We cannot use LOG(FATAL) or CHECK()
Packit bd1cd8
// as Google Mock might be used to mock the log sink itself.  We
Packit bd1cd8
// inline this function to prevent it from showing up in the stack
Packit bd1cd8
// trace.
Packit bd1cd8
inline void Assert(bool condition, const char* file, int line,
Packit bd1cd8
                   const string& msg) {
Packit bd1cd8
  if (!condition) {
Packit bd1cd8
    GetFailureReporter()->ReportFailure(FailureReporterInterface::kFatal,
Packit bd1cd8
                                        file, line, msg);
Packit bd1cd8
  }
Packit bd1cd8
}
Packit bd1cd8
inline void Assert(bool condition, const char* file, int line) {
Packit bd1cd8
  Assert(condition, file, line, "Assertion failed.");
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Verifies that condition is true; generates a non-fatal failure if
Packit bd1cd8
// condition is false.
Packit bd1cd8
inline void Expect(bool condition, const char* file, int line,
Packit bd1cd8
                   const string& msg) {
Packit bd1cd8
  if (!condition) {
Packit bd1cd8
    GetFailureReporter()->ReportFailure(FailureReporterInterface::kNonfatal,
Packit bd1cd8
                                        file, line, msg);
Packit bd1cd8
  }
Packit bd1cd8
}
Packit bd1cd8
inline void Expect(bool condition, const char* file, int line) {
Packit bd1cd8
  Expect(condition, file, line, "Expectation failed.");
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Severity level of a log.
Packit bd1cd8
enum LogSeverity {
Packit bd1cd8
  kInfo = 0,
Packit bd1cd8
  kWarning = 1
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Valid values for the --gmock_verbose flag.
Packit bd1cd8
Packit bd1cd8
// All logs (informational and warnings) are printed.
Packit bd1cd8
const char kInfoVerbosity[] = "info";
Packit bd1cd8
// Only warnings are printed.
Packit bd1cd8
const char kWarningVerbosity[] = "warning";
Packit bd1cd8
// No logs are printed.
Packit bd1cd8
const char kErrorVerbosity[] = "error";
Packit bd1cd8
Packit bd1cd8
// Returns true iff a log with the given severity is visible according
Packit bd1cd8
// to the --gmock_verbose flag.
Packit bd1cd8
GTEST_API_ bool LogIsVisible(LogSeverity severity);
Packit bd1cd8
Packit bd1cd8
// Prints the given message to stdout iff 'severity' >= the level
Packit bd1cd8
// specified by the --gmock_verbose flag.  If stack_frames_to_skip >=
Packit bd1cd8
// 0, also prints the stack trace excluding the top
Packit bd1cd8
// stack_frames_to_skip frames.  In opt mode, any positive
Packit bd1cd8
// stack_frames_to_skip is treated as 0, since we don't know which
Packit bd1cd8
// function calls will be inlined by the compiler and need to be
Packit bd1cd8
// conservative.
Packit bd1cd8
GTEST_API_ void Log(LogSeverity severity,
Packit bd1cd8
                    const string& message,
Packit bd1cd8
                    int stack_frames_to_skip);
Packit bd1cd8
Packit bd1cd8
// TODO(wan@google.com): group all type utilities together.
Packit bd1cd8
Packit bd1cd8
// Type traits.
Packit bd1cd8
Packit bd1cd8
// is_reference<T>::value is non-zero iff T is a reference type.
Packit bd1cd8
template <typename T> struct is_reference : public false_type {};
Packit bd1cd8
template <typename T> struct is_reference<T&> : public true_type {};
Packit bd1cd8
Packit bd1cd8
// type_equals<T1, T2>::value is non-zero iff T1 and T2 are the same type.
Packit bd1cd8
template <typename T1, typename T2> struct type_equals : public false_type {};
Packit bd1cd8
template <typename T> struct type_equals<T, T> : public true_type {};
Packit bd1cd8
Packit bd1cd8
// remove_reference<T>::type removes the reference from type T, if any.
Packit bd1cd8
template <typename T> struct remove_reference { typedef T type; };  // NOLINT
Packit bd1cd8
template <typename T> struct remove_reference<T&> { typedef T type; }; // NOLINT
Packit bd1cd8
Packit bd1cd8
// DecayArray<T>::type turns an array type U[N] to const U* and preserves
Packit bd1cd8
// other types.  Useful for saving a copy of a function argument.
Packit bd1cd8
template <typename T> struct DecayArray { typedef T type; };  // NOLINT
Packit bd1cd8
template <typename T, size_t N> struct DecayArray<T[N]> {
Packit bd1cd8
  typedef const T* type;
Packit bd1cd8
};
Packit bd1cd8
// Sometimes people use arrays whose size is not available at the use site
Packit bd1cd8
// (e.g. extern const char kNamePrefix[]).  This specialization covers that
Packit bd1cd8
// case.
Packit bd1cd8
template <typename T> struct DecayArray<T[]> {
Packit bd1cd8
  typedef const T* type;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Disable MSVC warnings for infinite recursion, since in this case the
Packit bd1cd8
// the recursion is unreachable.
Packit bd1cd8
#ifdef _MSC_VER
Packit bd1cd8
# pragma warning(push)
Packit bd1cd8
# pragma warning(disable:4717)
Packit bd1cd8
#endif
Packit bd1cd8
Packit bd1cd8
// Invalid<T>() is usable as an expression of type T, but will terminate
Packit bd1cd8
// the program with an assertion failure if actually run.  This is useful
Packit bd1cd8
// when a value of type T is needed for compilation, but the statement
Packit bd1cd8
// will not really be executed (or we don't care if the statement
Packit bd1cd8
// crashes).
Packit bd1cd8
template <typename T>
Packit bd1cd8
inline T Invalid() {
Packit bd1cd8
  Assert(false, "", -1, "Internal error: attempt to return invalid value");
Packit bd1cd8
  // This statement is unreachable, and would never terminate even if it
Packit bd1cd8
  // could be reached. It is provided only to placate compiler warnings
Packit bd1cd8
  // about missing return statements.
Packit bd1cd8
  return Invalid<T>();
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
#ifdef _MSC_VER
Packit bd1cd8
# pragma warning(pop)
Packit bd1cd8
#endif
Packit bd1cd8
Packit bd1cd8
// Given a raw type (i.e. having no top-level reference or const
Packit bd1cd8
// modifier) RawContainer that's either an STL-style container or a
Packit bd1cd8
// native array, class StlContainerView<RawContainer> has the
Packit bd1cd8
// following members:
Packit bd1cd8
//
Packit bd1cd8
//   - type is a type that provides an STL-style container view to
Packit bd1cd8
//     (i.e. implements the STL container concept for) RawContainer;
Packit bd1cd8
//   - const_reference is a type that provides a reference to a const
Packit bd1cd8
//     RawContainer;
Packit bd1cd8
//   - ConstReference(raw_container) returns a const reference to an STL-style
Packit bd1cd8
//     container view to raw_container, which is a RawContainer.
Packit bd1cd8
//   - Copy(raw_container) returns an STL-style container view of a
Packit bd1cd8
//     copy of raw_container, which is a RawContainer.
Packit bd1cd8
//
Packit bd1cd8
// This generic version is used when RawContainer itself is already an
Packit bd1cd8
// STL-style container.
Packit bd1cd8
template <class RawContainer>
Packit bd1cd8
class StlContainerView {
Packit bd1cd8
 public:
Packit bd1cd8
  typedef RawContainer type;
Packit bd1cd8
  typedef const type& const_reference;
Packit bd1cd8
Packit bd1cd8
  static const_reference ConstReference(const RawContainer& container) {
Packit bd1cd8
    // Ensures that RawContainer is not a const type.
Packit bd1cd8
    testing::StaticAssertTypeEq
Packit bd1cd8
        GTEST_REMOVE_CONST_(RawContainer)>();
Packit bd1cd8
    return container;
Packit bd1cd8
  }
Packit bd1cd8
  static type Copy(const RawContainer& container) { return container; }
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// This specialization is used when RawContainer is a native array type.
Packit bd1cd8
template <typename Element, size_t N>
Packit bd1cd8
class StlContainerView<Element[N]> {
Packit bd1cd8
 public:
Packit bd1cd8
  typedef GTEST_REMOVE_CONST_(Element) RawElement;
Packit bd1cd8
  typedef internal::NativeArray<RawElement> type;
Packit bd1cd8
  // NativeArray<T> can represent a native array either by value or by
Packit bd1cd8
  // reference (selected by a constructor argument), so 'const type'
Packit bd1cd8
  // can be used to reference a const native array.  We cannot
Packit bd1cd8
  // 'typedef const type& const_reference' here, as that would mean
Packit bd1cd8
  // ConstReference() has to return a reference to a local variable.
Packit bd1cd8
  typedef const type const_reference;
Packit bd1cd8
Packit bd1cd8
  static const_reference ConstReference(const Element (&array)[N]) {
Packit bd1cd8
    // Ensures that Element is not a const type.
Packit bd1cd8
    testing::StaticAssertTypeEq<Element, RawElement>();
Packit bd1cd8
#if GTEST_OS_SYMBIAN
Packit bd1cd8
    // The Nokia Symbian compiler confuses itself in template instantiation
Packit bd1cd8
    // for this call without the cast to Element*:
Packit bd1cd8
    // function call '[testing::internal::NativeArray<char *>].NativeArray(
Packit bd1cd8
    //     {lval} const char *[4], long, testing::internal::RelationToSource)'
Packit bd1cd8
    //     does not match
Packit bd1cd8
    // 'testing::internal::NativeArray<char *>::NativeArray(
Packit bd1cd8
    //     char *const *, unsigned int, testing::internal::RelationToSource)'
Packit bd1cd8
    // (instantiating: 'testing::internal::ContainsMatcherImpl
Packit bd1cd8
    //     <const char * (&)[4]>::Matches(const char * (&)[4]) const')
Packit bd1cd8
    // (instantiating: 'testing::internal::StlContainerView<char *[4]>::
Packit bd1cd8
    //     ConstReference(const char * (&)[4])')
Packit bd1cd8
    // (and though the N parameter type is mismatched in the above explicit
Packit bd1cd8
    // conversion of it doesn't help - only the conversion of the array).
Packit bd1cd8
    return type(const_cast<Element*>(&array[0]), N,
Packit bd1cd8
                RelationToSourceReference());
Packit bd1cd8
#else
Packit bd1cd8
    return type(array, N, RelationToSourceReference());
Packit bd1cd8
#endif  // GTEST_OS_SYMBIAN
Packit bd1cd8
  }
Packit bd1cd8
  static type Copy(const Element (&array)[N]) {
Packit bd1cd8
#if GTEST_OS_SYMBIAN
Packit bd1cd8
    return type(const_cast<Element*>(&array[0]), N, RelationToSourceCopy());
Packit bd1cd8
#else
Packit bd1cd8
    return type(array, N, RelationToSourceCopy());
Packit bd1cd8
#endif  // GTEST_OS_SYMBIAN
Packit bd1cd8
  }
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// This specialization is used when RawContainer is a native array
Packit bd1cd8
// represented as a (pointer, size) tuple.
Packit bd1cd8
template <typename ElementPointer, typename Size>
Packit bd1cd8
class StlContainerView< ::testing::tuple<ElementPointer, Size> > {
Packit bd1cd8
 public:
Packit bd1cd8
  typedef GTEST_REMOVE_CONST_(
Packit bd1cd8
      typename internal::PointeeOf<ElementPointer>::type) RawElement;
Packit bd1cd8
  typedef internal::NativeArray<RawElement> type;
Packit bd1cd8
  typedef const type const_reference;
Packit bd1cd8
Packit bd1cd8
  static const_reference ConstReference(
Packit bd1cd8
      const ::testing::tuple<ElementPointer, Size>& array) {
Packit bd1cd8
    return type(get<0>(array), get<1>(array), RelationToSourceReference());
Packit bd1cd8
  }
Packit bd1cd8
  static type Copy(const ::testing::tuple<ElementPointer, Size>& array) {
Packit bd1cd8
    return type(get<0>(array), get<1>(array), RelationToSourceCopy());
Packit bd1cd8
  }
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// The following specialization prevents the user from instantiating
Packit bd1cd8
// StlContainer with a reference type.
Packit bd1cd8
template <typename T> class StlContainerView<T&>;
Packit bd1cd8
Packit bd1cd8
// A type transform to remove constness from the first part of a pair.
Packit bd1cd8
// Pairs like that are used as the value_type of associative containers,
Packit bd1cd8
// and this transform produces a similar but assignable pair.
Packit bd1cd8
template <typename T>
Packit bd1cd8
struct RemoveConstFromKey {
Packit bd1cd8
  typedef T type;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Partially specialized to remove constness from std::pair<const K, V>.
Packit bd1cd8
template <typename K, typename V>
Packit bd1cd8
struct RemoveConstFromKey<std::pair<const K, V> > {
Packit bd1cd8
  typedef std::pair<K, V> type;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Mapping from booleans to types. Similar to boost::bool_<kValue> and
Packit bd1cd8
// std::integral_constant<bool, kValue>.
Packit bd1cd8
template <bool kValue>
Packit bd1cd8
struct BooleanConstant {};
Packit bd1cd8
Packit bd1cd8
}  // namespace internal
Packit bd1cd8
}  // namespace testing
Packit bd1cd8
Packit bd1cd8
#endif  // GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_
Packit bd1cd8