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