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