gmock-internal-utils.h
Go to the documentation of this file.
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 
47 #include "gtest/gtest.h"
48 
49 namespace testing
50 {
51 namespace internal
52 {
53 
54 // Converts an identifier name to a space-separated list of lower-case
55 // words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is
56 // treated as one word. For example, both "FooBar123" and
57 // "foo_bar_123" are converted to "foo bar 123".
58 GTEST_API_ string ConvertIdentifierNameToWords(const char * id_name);
59 
60 // PointeeOf<Pointer>::type is the type of a value pointed to by a
61 // Pointer, which can be either a smart pointer or a raw pointer. The
62 // following default implementation is for the case where Pointer is a
63 // smart pointer.
64 template <typename Pointer>
65 struct PointeeOf
66 {
67  // Smart pointer classes define type element_type as the type of
68  // their pointees.
69  typedef typename Pointer::element_type type;
70 };
71 // This specialization is for the raw pointer case.
72 template <typename T>
73 struct PointeeOf<T *> { typedef T type; }; // NOLINT
74 
75 // GetRawPointer(p) returns the raw pointer underlying p when p is a
76 // smart pointer, or returns p itself when p is already a raw pointer.
77 // The following default implementation is for the smart pointer case.
78 template <typename Pointer>
79 inline const typename Pointer::element_type * GetRawPointer(const Pointer & p)
80 {
81  return p.get();
82 }
83 // This overloaded version is for the raw pointer case.
84 template <typename Element>
85 inline Element * GetRawPointer(Element * p) { return p; }
86 
87 // This comparator allows linked_ptr to be stored in sets.
88 template <typename T>
89 struct LinkedPtrLessThan
90 {
91  bool operator()(const ::testing::internal::linked_ptr<T> & lhs,
92  const ::testing::internal::linked_ptr<T> & rhs) const
93  {
94  return lhs.get() < rhs.get();
95  }
96 };
97 
98 // Symbian compilation can be done with wchar_t being either a native
99 // type or a typedef. Using Google Mock with OpenC without wchar_t
100 // should require the definition of _STLP_NO_WCHAR_T.
101 //
102 // MSVC treats wchar_t as a native type usually, but treats it as the
103 // same as unsigned short when the compiler option /Zc:wchar_t- is
104 // specified. It defines _NATIVE_WCHAR_T_DEFINED symbol when wchar_t
105 // is a native type.
106 #if (GTEST_OS_SYMBIAN && defined(_STLP_NO_WCHAR_T)) || \
107  (defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED))
108 // wchar_t is a typedef.
109 #else
110 # define GMOCK_WCHAR_T_IS_NATIVE_ 1
111 #endif
112 
113 // signed wchar_t and unsigned wchar_t are NOT in the C++ standard.
114 // Using them is a bad practice and not portable. So DON'T use them.
115 //
116 // Still, Google Mock is designed to work even if the user uses signed
117 // wchar_t or unsigned wchar_t (obviously, assuming the compiler
118 // supports them).
119 //
120 // To gcc,
121 // wchar_t == signed wchar_t != unsigned wchar_t == unsigned int
122 #ifdef __GNUC__
123 // signed/unsigned wchar_t are valid types.
124 # define GMOCK_HAS_SIGNED_WCHAR_T_ 1
125 #endif
126 
127 // In what follows, we use the term "kind" to indicate whether a type
128 // is bool, an integer type (excluding bool), a floating-point type,
129 // or none of them. This categorization is useful for determining
130 // when a matcher argument type can be safely converted to another
131 // type in the implementation of SafeMatcherCast.
133 {
135 };
136 
137 // KindOf<T>::value is the kind of type T.
138 template <typename T> struct KindOf
139 {
140  enum { value = kOther }; // The default kind.
141 };
142 
143 // This macro declares that the kind of 'type' is 'kind'.
144 #define GMOCK_DECLARE_KIND_(type, kind) \
145  template <> struct KindOf<type> { enum { value = kind }; }
146 
148 
149 // All standard integer types.
151 GMOCK_DECLARE_KIND_(signed char, kInteger);
152 GMOCK_DECLARE_KIND_(unsigned char, kInteger);
153 GMOCK_DECLARE_KIND_(short, kInteger); // NOLINT
154 GMOCK_DECLARE_KIND_(unsigned short, kInteger); // NOLINT
156 GMOCK_DECLARE_KIND_(unsigned int, kInteger);
157 GMOCK_DECLARE_KIND_(long, kInteger); // NOLINT
158 GMOCK_DECLARE_KIND_(unsigned long, kInteger); // NOLINT
159 
160 #if GMOCK_WCHAR_T_IS_NATIVE_
161 GMOCK_DECLARE_KIND_(wchar_t, kInteger);
162 #endif
163 
164 // Non-standard integer types.
167 
168 // All standard floating-point types.
171 GMOCK_DECLARE_KIND_(long double, kFloatingPoint);
172 
173 #undef GMOCK_DECLARE_KIND_
174 
175 // Evaluates to the kind of 'type'.
176 #define GMOCK_KIND_OF_(type) \
177  static_cast< ::testing::internal::TypeKind>( \
178  ::testing::internal::KindOf<type>::value)
179 
180 // Evaluates to true iff integer type T is signed.
181 #define GMOCK_IS_SIGNED_(T) (static_cast<T>(-1) < 0)
182 
183 // LosslessArithmeticConvertibleImpl<kFromKind, From, kToKind, To>::value
184 // is true iff arithmetic type From can be losslessly converted to
185 // arithmetic type To.
186 //
187 // It's the user's responsibility to ensure that both From and To are
188 // raw (i.e. has no CV modifier, is not a pointer, and is not a
189 // reference) built-in arithmetic types, kFromKind is the kind of
190 // From, and kToKind is the kind of To; the value is
191 // implementation-defined when the above pre-condition is violated.
192 template <TypeKind kFromKind, typename From, TypeKind kToKind, typename To>
193 struct LosslessArithmeticConvertibleImpl : public false_type {};
194 
195 // Converting bool to bool is lossless.
196 template <>
197 struct LosslessArithmeticConvertibleImpl<kBool, bool, kBool, bool>
198  : public true_type {}; // NOLINT
199 
200 // Converting bool to any integer type is lossless.
201 template <typename To>
202 struct LosslessArithmeticConvertibleImpl<kBool, bool, kInteger, To>
203  : public true_type {}; // NOLINT
204 
205 // Converting bool to any floating-point type is lossless.
206 template <typename To>
207 struct LosslessArithmeticConvertibleImpl<kBool, bool, kFloatingPoint, To>
208  : public true_type {}; // NOLINT
209 
210 // Converting an integer to bool is lossy.
211 template <typename From>
212 struct LosslessArithmeticConvertibleImpl<kInteger, From, kBool, bool>
213  : public false_type {}; // NOLINT
214 
215 // Converting an integer to another non-bool integer is lossless iff
216 // the target type's range encloses the source type's range.
217 template <typename From, typename To>
218 struct LosslessArithmeticConvertibleImpl<kInteger, From, kInteger, To>
219  : public bool_constant <
220 // When converting from a smaller size to a larger size, we are
221 // fine as long as we are not converting from signed to unsigned.
222  ((sizeof(From) < sizeof(To)) &&
223  (!GMOCK_IS_SIGNED_(From) || GMOCK_IS_SIGNED_(To))) ||
224  // When converting between the same size, the signedness must match.
225  ((sizeof(From) == sizeof(To)) &&
226  (GMOCK_IS_SIGNED_(From) == GMOCK_IS_SIGNED_(To))) > {}; // NOLINT
227 
228 #undef GMOCK_IS_SIGNED_
229 
230 // Converting an integer to a floating-point type may be lossy, since
231 // the format of a floating-point number is implementation-defined.
232 template <typename From, typename To>
233 struct LosslessArithmeticConvertibleImpl<kInteger, From, kFloatingPoint, To>
234  : public false_type {}; // NOLINT
235 
236 // Converting a floating-point to bool is lossy.
237 template <typename From>
238 struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kBool, bool>
239  : public false_type {}; // NOLINT
240 
241 // Converting a floating-point to an integer is lossy.
242 template <typename From, typename To>
243 struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kInteger, To>
244  : public false_type {}; // NOLINT
245 
246 // Converting a floating-point to another floating-point is lossless
247 // iff the target type is at least as big as the source type.
248 template <typename From, typename To>
249 struct LosslessArithmeticConvertibleImpl <
250  kFloatingPoint, From, kFloatingPoint, To >
251 : public bool_constant < sizeof(From) <= sizeof(To) > {}; // NOLINT
252 
253 // LosslessArithmeticConvertible<From, To>::value is true iff arithmetic
254 // type From can be losslessly converted to arithmetic type To.
255 //
256 // It's the user's responsibility to ensure that both From and To are
257 // raw (i.e. has no CV modifier, is not a pointer, and is not a
258 // reference) built-in arithmetic types; the value is
259 // implementation-defined when the above pre-condition is violated.
260 template <typename From, typename To>
261 struct LosslessArithmeticConvertible
262  : public LosslessArithmeticConvertibleImpl <
263  GMOCK_KIND_OF_(From), From, GMOCK_KIND_OF_(To), To > {}; // NOLINT
264 
265 // This interface knows how to report a Google Mock failure (either
266 // non-fatal or fatal).
267 class FailureReporterInterface
268 {
269 public:
270  // The type of a failure (either non-fatal or fatal).
271  enum FailureType
272  {
273  kNonfatal, kFatal
274  };
275 
276  virtual ~FailureReporterInterface() {}
277 
278  // Reports a failure that occurred at the given source file location.
279  virtual void ReportFailure(FailureType type, const char * file, int line,
280  const string & message) = 0;
281 };
282 
283 // Returns the failure reporter used by Google Mock.
284 GTEST_API_ FailureReporterInterface * GetFailureReporter();
285 
286 // Asserts that condition is true; aborts the process with the given
287 // message if condition is false. We cannot use LOG(FATAL) or CHECK()
288 // as Google Mock might be used to mock the log sink itself. We
289 // inline this function to prevent it from showing up in the stack
290 // trace.
291 inline void Assert(bool condition, const char * file, int line,
292  const string & msg)
293 {
294  if (!condition)
295  {
296  GetFailureReporter()->ReportFailure(FailureReporterInterface::kFatal,
297  file, line, msg);
298  }
299 }
300 inline void Assert(bool condition, const char * file, int line)
301 {
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,
308  const string & msg)
309 {
310  if (!condition)
311  {
312  GetFailureReporter()->ReportFailure(FailureReporterInterface::kNonfatal,
313  file, line, msg);
314  }
315 }
316 inline void Expect(bool condition, const char * file, int line)
317 {
318  Expect(condition, file, line, "Expectation failed.");
319 }
320 
321 // Severity level of a log.
323 {
324  kInfo = 0,
325  kWarning = 1
326 };
327 
328 // Valid values for the --gmock_verbose flag.
329 
330 // All logs (informational and warnings) are printed.
331 const char kInfoVerbosity[] = "info";
332 // Only warnings are printed.
333 const char kWarningVerbosity[] = "warning";
334 // No logs are printed.
335 const char kErrorVerbosity[] = "error";
336 
337 // Returns true iff a log with the given severity is visible according
338 // to the --gmock_verbose flag.
339 GTEST_API_ bool LogIsVisible(LogSeverity severity);
340 
341 // Prints the given message to stdout iff 'severity' >= the level
342 // specified by the --gmock_verbose flag. If stack_frames_to_skip >=
343 // 0, also prints the stack trace excluding the top
344 // stack_frames_to_skip frames. In opt mode, any positive
345 // stack_frames_to_skip is treated as 0, since we don't know which
346 // function calls will be inlined by the compiler and need to be
347 // conservative.
348 GTEST_API_ void Log(LogSeverity severity,
349  const string & message,
350  int stack_frames_to_skip);
351 
352 // TODO(wan@google.com): group all type utilities together.
353 
354 // Type traits.
355 
356 // is_reference<T>::value is non-zero iff T is a reference type.
357 template <typename T> struct is_reference : public false_type {};
358 template <typename T> struct is_reference<T &> : public true_type {};
359 
360 // type_equals<T1, T2>::value is non-zero iff T1 and T2 are the same type.
361 template <typename T1, typename T2> struct type_equals : public false_type {};
362 template <typename T> struct type_equals<T, T> : public true_type {};
363 
364 // remove_reference<T>::type removes the reference from type T, if any.
365 template <typename T> struct remove_reference { typedef T type; }; // NOLINT
366 template <typename T> struct remove_reference<T &> { typedef T type; }; // NOLINT
367 
368 // DecayArray<T>::type turns an array type U[N] to const U* and preserves
369 // other types. Useful for saving a copy of a function argument.
370 template <typename T> struct DecayArray { typedef T type; }; // NOLINT
371 template <typename T, size_t N> struct DecayArray<T[N]>
372 {
373  typedef const T * type;
374 };
375 // Sometimes people use arrays whose size is not available at the use site
376 // (e.g. extern const char kNamePrefix[]). This specialization covers that
377 // case.
378 template <typename T> struct DecayArray<T[]>
379 {
380  typedef const T * type;
381 };
382 
383 // Invalid<T>() returns an invalid value of type T. This is useful
384 // when a value of type T is needed for compilation, but the statement
385 // will not really be executed (or we don't care if the statement
386 // crashes).
387 template <typename T>
388 inline T Invalid()
389 {
390  return const_cast<typename remove_reference<T>::type &>(
391  *static_cast<volatile typename remove_reference<T>::type *>(NULL));
392 }
393 template <>
394 inline void Invalid<void>() {}
395 
396 // Given a raw type (i.e. having no top-level reference or const
397 // modifier) RawContainer that's either an STL-style container or a
398 // native array, class StlContainerView<RawContainer> has the
399 // following members:
400 //
401 // - type is a type that provides an STL-style container view to
402 // (i.e. implements the STL container concept for) RawContainer;
403 // - const_reference is a type that provides a reference to a const
404 // RawContainer;
405 // - ConstReference(raw_container) returns a const reference to an STL-style
406 // container view to raw_container, which is a RawContainer.
407 // - Copy(raw_container) returns an STL-style container view of a
408 // copy of raw_container, which is a RawContainer.
409 //
410 // This generic version is used when RawContainer itself is already an
411 // STL-style container.
412 template <class RawContainer>
413 class StlContainerView
414 {
415 public:
416  typedef RawContainer type;
417  typedef const type & const_reference;
418 
419  static const_reference ConstReference(const RawContainer & container)
420  {
421  // Ensures that RawContainer is not a const type.
422  testing::StaticAssertTypeEq<RawContainer,
423  GTEST_REMOVE_CONST_(RawContainer)>();
424  return container;
425  }
426  static type Copy(const RawContainer & container) { return container; }
427 };
428 
429 // This specialization is used when RawContainer is a native array type.
430 template <typename Element, size_t N>
431 class StlContainerView<Element[N]>
432 {
433 public:
434  typedef GTEST_REMOVE_CONST_(Element) RawElement;
436  // NativeArray<T> can represent a native array either by value or by
437  // reference (selected by a constructor argument), so 'const type'
438  // can be used to reference a const native array. We cannot
439  // 'typedef const type& const_reference' here, as that would mean
440  // ConstReference() has to return a reference to a local variable.
441  typedef const type const_reference;
442 
443  static const_reference ConstReference(const Element(&array)[N])
444  {
445  // Ensures that Element is not a const type.
446  testing::StaticAssertTypeEq<Element, RawElement>();
447 #if GTEST_OS_SYMBIAN
448  // The Nokia Symbian compiler confuses itself in template instantiation
449  // for this call without the cast to Element*:
450  // function call '[testing::internal::NativeArray<char *>].NativeArray(
451  // {lval} const char *[4], long, testing::internal::RelationToSource)'
452  // does not match
453  // 'testing::internal::NativeArray<char *>::NativeArray(
454  // char *const *, unsigned int, testing::internal::RelationToSource)'
455  // (instantiating: 'testing::internal::ContainsMatcherImpl
456  // <const char * (&)[4]>::Matches(const char * (&)[4]) const')
457  // (instantiating: 'testing::internal::StlContainerView<char *[4]>::
458  // ConstReference(const char * (&)[4])')
459  // (and though the N parameter type is mismatched in the above explicit
460  // conversion of it doesn't help - only the conversion of the array).
461  return type(const_cast<Element *>(&array[0]), N, kReference);
462 #else
463  return type(array, N, kReference);
464 #endif // GTEST_OS_SYMBIAN
465  }
466  static type Copy(const Element(&array)[N])
467  {
468 #if GTEST_OS_SYMBIAN
469  return type(const_cast<Element *>(&array[0]), N, kCopy);
470 #else
471  return type(array, N, kCopy);
472 #endif // GTEST_OS_SYMBIAN
473  }
474 };
475 
476 // This specialization is used when RawContainer is a native array
477 // represented as a (pointer, size) tuple.
478 template <typename ElementPointer, typename Size>
479 class StlContainerView< ::std::tr1::tuple<ElementPointer, Size> >
480 {
481 public:
482  typedef GTEST_REMOVE_CONST_(
483  typename internal::PointeeOf<ElementPointer>::type) RawElement;
485  typedef const type const_reference;
486 
487  static const_reference ConstReference(
488  const ::std::tr1::tuple<ElementPointer, Size> & array)
489  {
491  return type(get<0>(array), get<1>(array), kReference);
492  }
493  static type Copy(const ::std::tr1::tuple<ElementPointer, Size> & array)
494  {
496  return type(get<0>(array), get<1>(array), kCopy);
497  }
498 };
499 
500 // The following specialization prevents the user from instantiating
501 // StlContainer with a reference type.
502 template <typename T> class StlContainerView<T &>;
503 
504 // A type transform to remove constness from the first part of a pair.
505 // Pairs like that are used as the value_type of associative containers,
506 // and this transform produces a similar but assignable pair.
507 template <typename T>
508 struct RemoveConstFromKey
509 {
510  typedef T type;
511 };
512 
513 // Partially specialized to remove constness from std::pair<const K, V>.
514 template <typename K, typename V>
515 struct RemoveConstFromKey<std::pair<const K, V> >
516 {
517  typedef std::pair<K, V> type;
518 };
519 
520 // Mapping from booleans to types. Similar to boost::bool_<kValue> and
521 // std::integral_constant<bool, kValue>.
522 template <bool kValue>
523 struct BooleanConstant {};
524 
525 } // namespace internal
526 } // namespace testing
527 
528 #endif // GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_
TypeWithSize< 8 >::Int Int64
GTEST_API_ string ConvertIdentifierNameToWords(const char *id_name)
static const_reference ConstReference(const ::std::tr1::tuple< ElementPointer, Size > &array)
static type Copy(const ::std::tr1::tuple< ElementPointer, Size > &array)
#define GTEST_API_
static const_reference ConstReference(const RawContainer &container)
TypeWithSize< 8 >::UInt UInt64
static const_reference ConstReference(const Element(&array)[N])
bool_constant< true > true_type
GMOCK_DECLARE_KIND_(bool, kBool)
void Expect(bool condition, const char *file, int line, const string &msg)
const char kWarningVerbosity[]
message
Definition: server.py:50
GTEST_API_ void Log(LogSeverity severity, const string &message, int stack_frames_to_skip)
bool operator()(const ::testing::internal::linked_ptr< T > &lhs, const ::testing::internal::linked_ptr< T > &rhs) const
GTEST_API_ bool LogIsVisible(LogSeverity severity)
static type Copy(const RawContainer &container)
#define GTEST_REMOVE_CONST_(T)
const Pointer::element_type * GetRawPointer(const Pointer &p)
bool_constant< false > false_type
static type Copy(const Element(&array)[N])


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:06