Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(548)

Side by Side Diff: testing/gmock/include/gmock/internal/gmock-internal-utils.h

Issue 113807: Checkin a version of gmock, modified to use our boost_tuple in VS2005. (Closed)
Patch Set: Fix grammar issue. Created 11 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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>
44
45 #include <gmock/internal/gmock-generated-internal-utils.h>
46 #include <gmock/internal/gmock-port.h>
47 #include <gtest/gtest.h>
48
49 // Concatenates two pre-processor symbols; works for concatenating
50 // built-in macros like __FILE__ and __LINE__.
51 #define GMOCK_CONCAT_TOKEN_IMPL_(foo, bar) foo##bar
52 #define GMOCK_CONCAT_TOKEN_(foo, bar) GMOCK_CONCAT_TOKEN_IMPL_(foo, bar)
53
54 #ifdef __GNUC__
55 #define GMOCK_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
56 #else
57 #define GMOCK_ATTRIBUTE_UNUSED_
58 #endif // __GNUC__
59
60 class ProtocolMessage;
61 namespace proto2 { class Message; }
62
63 namespace testing {
64 namespace internal {
65
66 // Converts an identifier name to a space-separated list of lower-case
67 // words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is
68 // treated as one word. For example, both "FooBar123" and
69 // "foo_bar_123" are converted to "foo bar 123".
70 string ConvertIdentifierNameToWords(const char* id_name);
71
72 // Defining a variable of type CompileAssertTypesEqual<T1, T2> will cause a
73 // compiler error iff T1 and T2 are different types.
74 template <typename T1, typename T2>
75 struct CompileAssertTypesEqual;
76
77 template <typename T>
78 struct CompileAssertTypesEqual<T, T> {
79 };
80
81 // Removes the reference from a type if it is a reference type,
82 // otherwise leaves it unchanged. This is the same as
83 // tr1::remove_reference, which is not widely available yet.
84 template <typename T>
85 struct RemoveReference { typedef T type; }; // NOLINT
86 template <typename T>
87 struct RemoveReference<T&> { typedef T type; }; // NOLINT
88
89 // A handy wrapper around RemoveReference that works when the argument
90 // T depends on template parameters.
91 #define GMOCK_REMOVE_REFERENCE_(T) \
92 typename ::testing::internal::RemoveReference<T>::type
93
94 // Removes const from a type if it is a const type, otherwise leaves
95 // it unchanged. This is the same as tr1::remove_const, which is not
96 // widely available yet.
97 template <typename T>
98 struct RemoveConst { typedef T type; }; // NOLINT
99 template <typename T>
100 struct RemoveConst<const T> { typedef T type; }; // NOLINT
101
102 // A handy wrapper around RemoveConst that works when the argument
103 // T depends on template parameters.
104 #define GMOCK_REMOVE_CONST_(T) \
105 typename ::testing::internal::RemoveConst<T>::type
106
107 // Adds reference to a type if it is not a reference type,
108 // otherwise leaves it unchanged. This is the same as
109 // tr1::add_reference, which is not widely available yet.
110 template <typename T>
111 struct AddReference { typedef T& type; }; // NOLINT
112 template <typename T>
113 struct AddReference<T&> { typedef T& type; }; // NOLINT
114
115 // A handy wrapper around AddReference that works when the argument T
116 // depends on template parameters.
117 #define GMOCK_ADD_REFERENCE_(T) \
118 typename ::testing::internal::AddReference<T>::type
119
120 // Adds a reference to const on top of T as necessary. For example,
121 // it transforms
122 //
123 // char ==> const char&
124 // const char ==> const char&
125 // char& ==> const char&
126 // const char& ==> const char&
127 //
128 // The argument T must depend on some template parameters.
129 #define GMOCK_REFERENCE_TO_CONST_(T) \
130 GMOCK_ADD_REFERENCE_(const GMOCK_REMOVE_REFERENCE_(T))
131
132 // PointeeOf<Pointer>::type is the type of a value pointed to by a
133 // Pointer, which can be either a smart pointer or a raw pointer. The
134 // following default implementation is for the case where Pointer is a
135 // smart pointer.
136 template <typename Pointer>
137 struct PointeeOf {
138 // Smart pointer classes define type element_type as the type of
139 // their pointees.
140 typedef typename Pointer::element_type type;
141 };
142 // This specialization is for the raw pointer case.
143 template <typename T>
144 struct PointeeOf<T*> { typedef T type; }; // NOLINT
145
146 // GetRawPointer(p) returns the raw pointer underlying p when p is a
147 // smart pointer, or returns p itself when p is already a raw pointer.
148 // The following default implementation is for the smart pointer case.
149 template <typename Pointer>
150 inline typename Pointer::element_type* GetRawPointer(const Pointer& p) {
151 return p.get();
152 }
153 // This overloaded version is for the raw pointer case.
154 template <typename Element>
155 inline Element* GetRawPointer(Element* p) { return p; }
156
157 // This comparator allows linked_ptr to be stored in sets.
158 template <typename T>
159 struct LinkedPtrLessThan {
160 bool operator()(const ::testing::internal::linked_ptr<T>& lhs,
161 const ::testing::internal::linked_ptr<T>& rhs) const {
162 return lhs.get() < rhs.get();
163 }
164 };
165
166 // ImplicitlyConvertible<From, To>::value is a compile-time bool
167 // constant that's true iff type From can be implicitly converted to
168 // type To.
169 template <typename From, typename To>
170 class ImplicitlyConvertible {
171 private:
172 // We need the following helper functions only for their types.
173 // They have no implementations.
174
175 // MakeFrom() is an expression whose type is From. We cannot simply
176 // use From(), as the type From may not have a public default
177 // constructor.
178 static From MakeFrom();
179
180 // These two functions are overloaded. Given an expression
181 // Helper(x), the compiler will pick the first version if x can be
182 // implicitly converted to type To; otherwise it will pick the
183 // second version.
184 //
185 // The first version returns a value of size 1, and the second
186 // version returns a value of size 2. Therefore, by checking the
187 // size of Helper(x), which can be done at compile time, we can tell
188 // which version of Helper() is used, and hence whether x can be
189 // implicitly converted to type To.
190 static char Helper(To);
191 static char (&Helper(...))[2]; // NOLINT
192
193 // We have to put the 'public' section after the 'private' section,
194 // or MSVC refuses to compile the code.
195 public:
196 // MSVC warns about implicitly converting from double to int for
197 // possible loss of data, so we need to temporarily disable the
198 // warning.
199 #ifdef _MSC_VER
200 #pragma warning(push) // Saves the current warning state.
201 #pragma warning(disable:4244) // Temporarily disables warning 4244.
202 static const bool value =
203 sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1;
204 #pragma warning(pop) // Restores the warning state.
205 #else
206 static const bool value =
207 sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1;
208 #endif // _MSV_VER
209 };
210 template <typename From, typename To>
211 const bool ImplicitlyConvertible<From, To>::value;
212
213 // In what follows, we use the term "kind" to indicate whether a type
214 // is bool, an integer type (excluding bool), a floating-point type,
215 // or none of them. This categorization is useful for determining
216 // when a matcher argument type can be safely converted to another
217 // type in the implementation of SafeMatcherCast.
218 enum TypeKind {
219 kBool, kInteger, kFloatingPoint, kOther
220 };
221
222 // KindOf<T>::value is the kind of type T.
223 template <typename T> struct KindOf {
224 enum { value = kOther }; // The default kind.
225 };
226
227 // This macro declares that the kind of 'type' is 'kind'.
228 #define GMOCK_DECLARE_KIND_(type, kind) \
229 template <> struct KindOf<type> { enum { value = kind }; }
230
231 GMOCK_DECLARE_KIND_(bool, kBool);
232
233 // All standard integer types.
234 GMOCK_DECLARE_KIND_(char, kInteger);
235 GMOCK_DECLARE_KIND_(signed char, kInteger);
236 GMOCK_DECLARE_KIND_(unsigned char, kInteger);
237 GMOCK_DECLARE_KIND_(short, kInteger); // NOLINT
238 GMOCK_DECLARE_KIND_(unsigned short, kInteger); // NOLINT
239 GMOCK_DECLARE_KIND_(int, kInteger);
240 GMOCK_DECLARE_KIND_(unsigned int, kInteger);
241 GMOCK_DECLARE_KIND_(long, kInteger); // NOLINT
242 GMOCK_DECLARE_KIND_(unsigned long, kInteger); // NOLINT
243
244 // MSVC can be configured to define wchar_t as a typedef of unsigned
245 // short. It defines _NATIVE_WCHAR_T_DEFINED symbol when wchar_t is a
246 // native type.
247 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
248 GMOCK_DECLARE_KIND_(wchar_t, kInteger);
249 #endif
250
251 // Non-standard integer types.
252 GMOCK_DECLARE_KIND_(Int64, kInteger);
253 GMOCK_DECLARE_KIND_(UInt64, kInteger);
254
255 // All standard floating-point types.
256 GMOCK_DECLARE_KIND_(float, kFloatingPoint);
257 GMOCK_DECLARE_KIND_(double, kFloatingPoint);
258 GMOCK_DECLARE_KIND_(long double, kFloatingPoint);
259
260 #undef GMOCK_DECLARE_KIND_
261
262 // Evaluates to the kind of 'type'.
263 #define GMOCK_KIND_OF_(type) \
264 static_cast< ::testing::internal::TypeKind>( \
265 ::testing::internal::KindOf<type>::value)
266
267 // Evaluates to true iff integer type T is signed.
268 #define GMOCK_IS_SIGNED_(T) (static_cast<T>(-1) < 0)
269
270 // LosslessArithmeticConvertibleImpl<kFromKind, From, kToKind, To>::value
271 // is true iff arithmetic type From can be losslessly converted to
272 // arithmetic type To.
273 //
274 // It's the user's responsibility to ensure that both From and To are
275 // raw (i.e. has no CV modifier, is not a pointer, and is not a
276 // reference) built-in arithmetic types, kFromKind is the kind of
277 // From, and kToKind is the kind of To; the value is
278 // implementation-defined when the above pre-condition is violated.
279 template <TypeKind kFromKind, typename From, TypeKind kToKind, typename To>
280 struct LosslessArithmeticConvertibleImpl : public false_type {};
281
282 // Converting bool to bool is lossless.
283 template <>
284 struct LosslessArithmeticConvertibleImpl<kBool, bool, kBool, bool>
285 : public true_type {}; // NOLINT
286
287 // Converting bool to any integer type is lossless.
288 template <typename To>
289 struct LosslessArithmeticConvertibleImpl<kBool, bool, kInteger, To>
290 : public true_type {}; // NOLINT
291
292 // Converting bool to any floating-point type is lossless.
293 template <typename To>
294 struct LosslessArithmeticConvertibleImpl<kBool, bool, kFloatingPoint, To>
295 : public true_type {}; // NOLINT
296
297 // Converting an integer to bool is lossy.
298 template <typename From>
299 struct LosslessArithmeticConvertibleImpl<kInteger, From, kBool, bool>
300 : public false_type {}; // NOLINT
301
302 // Converting an integer to another non-bool integer is lossless iff
303 // the target type's range encloses the source type's range.
304 template <typename From, typename To>
305 struct LosslessArithmeticConvertibleImpl<kInteger, From, kInteger, To>
306 : public bool_constant<
307 // When converting from a smaller size to a larger size, we are
308 // fine as long as we are not converting from signed to unsigned.
309 ((sizeof(From) < sizeof(To)) &&
310 (!GMOCK_IS_SIGNED_(From) || GMOCK_IS_SIGNED_(To))) ||
311 // When converting between the same size, the signedness must match.
312 ((sizeof(From) == sizeof(To)) &&
313 (GMOCK_IS_SIGNED_(From) == GMOCK_IS_SIGNED_(To)))> {}; // NOLINT
314
315 #undef GMOCK_IS_SIGNED_
316
317 // Converting an integer to a floating-point type may be lossy, since
318 // the format of a floating-point number is implementation-defined.
319 template <typename From, typename To>
320 struct LosslessArithmeticConvertibleImpl<kInteger, From, kFloatingPoint, To>
321 : public false_type {}; // NOLINT
322
323 // Converting a floating-point to bool is lossy.
324 template <typename From>
325 struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kBool, bool>
326 : public false_type {}; // NOLINT
327
328 // Converting a floating-point to an integer is lossy.
329 template <typename From, typename To>
330 struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kInteger, To>
331 : public false_type {}; // NOLINT
332
333 // Converting a floating-point to another floating-point is lossless
334 // iff the target type is at least as big as the source type.
335 template <typename From, typename To>
336 struct LosslessArithmeticConvertibleImpl<
337 kFloatingPoint, From, kFloatingPoint, To>
338 : public bool_constant<sizeof(From) <= sizeof(To)> {}; // NOLINT
339
340 // LosslessArithmeticConvertible<From, To>::value is true iff arithmetic
341 // type From can be losslessly converted to arithmetic type To.
342 //
343 // It's the user's responsibility to ensure that both From and To are
344 // raw (i.e. has no CV modifier, is not a pointer, and is not a
345 // reference) built-in arithmetic types; the value is
346 // implementation-defined when the above pre-condition is violated.
347 template <typename From, typename To>
348 struct LosslessArithmeticConvertible
349 : public LosslessArithmeticConvertibleImpl<
350 GMOCK_KIND_OF_(From), From, GMOCK_KIND_OF_(To), To> {}; // NOLINT
351
352 // IsAProtocolMessage<T>::value is a compile-time bool constant that's
353 // true iff T is type ProtocolMessage, proto2::Message, or a subclass
354 // of those.
355 template <typename T>
356 struct IsAProtocolMessage
357 : public bool_constant<
358 ImplicitlyConvertible<const T*, const ::ProtocolMessage*>::value ||
359 ImplicitlyConvertible<const T*, const ::proto2::Message*>::value> {
360 };
361
362 // When the compiler sees expression IsContainerTest<C>(0), the first
363 // overload of IsContainerTest will be picked if C is an STL-style
364 // container class (since C::const_iterator* is a valid type and 0 can
365 // be converted to it), while the second overload will be picked
366 // otherwise (since C::const_iterator will be an invalid type in this
367 // case). Therefore, we can determine whether C is a container class
368 // by checking the type of IsContainerTest<C>(0). The value of the
369 // expression is insignificant.
370 typedef int IsContainer;
371 template <class C>
372 IsContainer IsContainerTest(typename C::const_iterator*) { return 0; }
373
374 typedef char IsNotContainer;
375 template <class C>
376 IsNotContainer IsContainerTest(...) { return '\0'; }
377
378 // This interface knows how to report a Google Mock failure (either
379 // non-fatal or fatal).
380 class FailureReporterInterface {
381 public:
382 // The type of a failure (either non-fatal or fatal).
383 enum FailureType {
384 NONFATAL, FATAL
385 };
386
387 virtual ~FailureReporterInterface() {}
388
389 // Reports a failure that occurred at the given source file location.
390 virtual void ReportFailure(FailureType type, const char* file, int line,
391 const string& message) = 0;
392 };
393
394 // Returns the failure reporter used by Google Mock.
395 FailureReporterInterface* GetFailureReporter();
396
397 // Asserts that condition is true; aborts the process with the given
398 // message if condition is false. We cannot use LOG(FATAL) or CHECK()
399 // as Google Mock might be used to mock the log sink itself. We
400 // inline this function to prevent it from showing up in the stack
401 // trace.
402 inline void Assert(bool condition, const char* file, int line,
403 const string& msg) {
404 if (!condition) {
405 GetFailureReporter()->ReportFailure(FailureReporterInterface::FATAL,
406 file, line, msg);
407 }
408 }
409 inline void Assert(bool condition, const char* file, int line) {
410 Assert(condition, file, line, "Assertion failed.");
411 }
412
413 // Verifies that condition is true; generates a non-fatal failure if
414 // condition is false.
415 inline void Expect(bool condition, const char* file, int line,
416 const string& msg) {
417 if (!condition) {
418 GetFailureReporter()->ReportFailure(FailureReporterInterface::NONFATAL,
419 file, line, msg);
420 }
421 }
422 inline void Expect(bool condition, const char* file, int line) {
423 Expect(condition, file, line, "Expectation failed.");
424 }
425
426 // Severity level of a log.
427 enum LogSeverity {
428 INFO = 0,
429 WARNING = 1,
430 };
431
432 // Valid values for the --gmock_verbose flag.
433
434 // All logs (informational and warnings) are printed.
435 const char kInfoVerbosity[] = "info";
436 // Only warnings are printed.
437 const char kWarningVerbosity[] = "warning";
438 // No logs are printed.
439 const char kErrorVerbosity[] = "error";
440
441 // Prints the given message to stdout iff 'severity' >= the level
442 // specified by the --gmock_verbose flag. If stack_frames_to_skip >=
443 // 0, also prints the stack trace excluding the top
444 // stack_frames_to_skip frames. In opt mode, any positive
445 // stack_frames_to_skip is treated as 0, since we don't know which
446 // function calls will be inlined by the compiler and need to be
447 // conservative.
448 void Log(LogSeverity severity, const string& message, int stack_frames_to_skip);
449
450 // The universal value printer (public/gmock-printers.h) needs this
451 // to declare an unused << operator in the global namespace.
452 struct Unused {};
453
454 // TODO(wan@google.com): group all type utilities together.
455
456 // Type traits.
457
458 // is_reference<T>::value is non-zero iff T is a reference type.
459 template <typename T> struct is_reference : public false_type {};
460 template <typename T> struct is_reference<T&> : public true_type {};
461
462 // type_equals<T1, T2>::value is non-zero iff T1 and T2 are the same type.
463 template <typename T1, typename T2> struct type_equals : public false_type {};
464 template <typename T> struct type_equals<T, T> : public true_type {};
465
466 // remove_reference<T>::type removes the reference from type T, if any.
467 template <typename T> struct remove_reference { typedef T type; }; // NOLINT
468 template <typename T> struct remove_reference<T&> { typedef T type; }; // NOLINT
469
470 // Invalid<T>() returns an invalid value of type T. This is useful
471 // when a value of type T is needed for compilation, but the statement
472 // will not really be executed (or we don't care if the statement
473 // crashes).
474 template <typename T>
475 inline T Invalid() {
476 return *static_cast<typename remove_reference<T>::type*>(NULL);
477 }
478 template <>
479 inline void Invalid<void>() {}
480
481 } // namespace internal
482 } // namespace testing
483
484 #endif // GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698