gmock-internal-utils.h
Go to the documentation of this file.
00001 // Copyright 2007, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 
00032 // Google Mock - a framework for writing C++ mock classes.
00033 //
00034 // This file defines some utilities useful for implementing Google
00035 // Mock.  They are subject to change without notice, so please DO NOT
00036 // USE THEM IN USER CODE.
00037 
00038 #ifndef GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_
00039 #define GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_
00040 
00041 #include <stdio.h>
00042 #include <ostream>  // NOLINT
00043 #include <string>
00044 
00045 #include "gmock/internal/gmock-generated-internal-utils.h"
00046 #include "gmock/internal/gmock-port.h"
00047 #include "gtest/gtest.h"
00048 
00049 namespace testing {
00050 namespace internal {
00051 
00052 // Converts an identifier name to a space-separated list of lower-case
00053 // words.  Each maximum substring of the form [A-Za-z][a-z]*|\d+ is
00054 // treated as one word.  For example, both "FooBar123" and
00055 // "foo_bar_123" are converted to "foo bar 123".
00056 GTEST_API_ string ConvertIdentifierNameToWords(const char* id_name);
00057 
00058 // PointeeOf<Pointer>::type is the type of a value pointed to by a
00059 // Pointer, which can be either a smart pointer or a raw pointer.  The
00060 // following default implementation is for the case where Pointer is a
00061 // smart pointer.
00062 template <typename Pointer>
00063 struct PointeeOf {
00064   // Smart pointer classes define type element_type as the type of
00065   // their pointees.
00066   typedef typename Pointer::element_type type;
00067 };
00068 // This specialization is for the raw pointer case.
00069 template <typename T>
00070 struct PointeeOf<T*> { typedef T type; };  // NOLINT
00071 
00072 // GetRawPointer(p) returns the raw pointer underlying p when p is a
00073 // smart pointer, or returns p itself when p is already a raw pointer.
00074 // The following default implementation is for the smart pointer case.
00075 template <typename Pointer>
00076 inline const typename Pointer::element_type* GetRawPointer(const Pointer& p) {
00077   return p.get();
00078 }
00079 // This overloaded version is for the raw pointer case.
00080 template <typename Element>
00081 inline Element* GetRawPointer(Element* p) { return p; }
00082 
00083 // This comparator allows linked_ptr to be stored in sets.
00084 template <typename T>
00085 struct LinkedPtrLessThan {
00086   bool operator()(const ::testing::internal::linked_ptr<T>& lhs,
00087                   const ::testing::internal::linked_ptr<T>& rhs) const {
00088     return lhs.get() < rhs.get();
00089   }
00090 };
00091 
00092 // Symbian compilation can be done with wchar_t being either a native
00093 // type or a typedef.  Using Google Mock with OpenC without wchar_t
00094 // should require the definition of _STLP_NO_WCHAR_T.
00095 //
00096 // MSVC treats wchar_t as a native type usually, but treats it as the
00097 // same as unsigned short when the compiler option /Zc:wchar_t- is
00098 // specified.  It defines _NATIVE_WCHAR_T_DEFINED symbol when wchar_t
00099 // is a native type.
00100 #if (GTEST_OS_SYMBIAN && defined(_STLP_NO_WCHAR_T)) || \
00101     (defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED))
00102 // wchar_t is a typedef.
00103 #else
00104 # define GMOCK_WCHAR_T_IS_NATIVE_ 1
00105 #endif
00106 
00107 // signed wchar_t and unsigned wchar_t are NOT in the C++ standard.
00108 // Using them is a bad practice and not portable.  So DON'T use them.
00109 //
00110 // Still, Google Mock is designed to work even if the user uses signed
00111 // wchar_t or unsigned wchar_t (obviously, assuming the compiler
00112 // supports them).
00113 //
00114 // To gcc,
00115 //   wchar_t == signed wchar_t != unsigned wchar_t == unsigned int
00116 #ifdef __GNUC__
00117 // signed/unsigned wchar_t are valid types.
00118 # define GMOCK_HAS_SIGNED_WCHAR_T_ 1
00119 #endif
00120 
00121 // In what follows, we use the term "kind" to indicate whether a type
00122 // is bool, an integer type (excluding bool), a floating-point type,
00123 // or none of them.  This categorization is useful for determining
00124 // when a matcher argument type can be safely converted to another
00125 // type in the implementation of SafeMatcherCast.
00126 enum TypeKind {
00127   kBool, kInteger, kFloatingPoint, kOther
00128 };
00129 
00130 // KindOf<T>::value is the kind of type T.
00131 template <typename T> struct KindOf {
00132   enum { value = kOther };  // The default kind.
00133 };
00134 
00135 // This macro declares that the kind of 'type' is 'kind'.
00136 #define GMOCK_DECLARE_KIND_(type, kind) \
00137   template <> struct KindOf<type> { enum { value = kind }; }
00138 
00139 GMOCK_DECLARE_KIND_(bool, kBool);
00140 
00141 // All standard integer types.
00142 GMOCK_DECLARE_KIND_(char, kInteger);
00143 GMOCK_DECLARE_KIND_(signed char, kInteger);
00144 GMOCK_DECLARE_KIND_(unsigned char, kInteger);
00145 GMOCK_DECLARE_KIND_(short, kInteger);  // NOLINT
00146 GMOCK_DECLARE_KIND_(unsigned short, kInteger);  // NOLINT
00147 GMOCK_DECLARE_KIND_(int, kInteger);
00148 GMOCK_DECLARE_KIND_(unsigned int, kInteger);
00149 GMOCK_DECLARE_KIND_(long, kInteger);  // NOLINT
00150 GMOCK_DECLARE_KIND_(unsigned long, kInteger);  // NOLINT
00151 
00152 #if GMOCK_WCHAR_T_IS_NATIVE_
00153 GMOCK_DECLARE_KIND_(wchar_t, kInteger);
00154 #endif
00155 
00156 // Non-standard integer types.
00157 GMOCK_DECLARE_KIND_(Int64, kInteger);
00158 GMOCK_DECLARE_KIND_(UInt64, kInteger);
00159 
00160 // All standard floating-point types.
00161 GMOCK_DECLARE_KIND_(float, kFloatingPoint);
00162 GMOCK_DECLARE_KIND_(double, kFloatingPoint);
00163 GMOCK_DECLARE_KIND_(long double, kFloatingPoint);
00164 
00165 #undef GMOCK_DECLARE_KIND_
00166 
00167 // Evaluates to the kind of 'type'.
00168 #define GMOCK_KIND_OF_(type) \
00169   static_cast< ::testing::internal::TypeKind>( \
00170       ::testing::internal::KindOf<type>::value)
00171 
00172 // Evaluates to true iff integer type T is signed.
00173 #define GMOCK_IS_SIGNED_(T) (static_cast<T>(-1) < 0)
00174 
00175 // LosslessArithmeticConvertibleImpl<kFromKind, From, kToKind, To>::value
00176 // is true iff arithmetic type From can be losslessly converted to
00177 // arithmetic type To.
00178 //
00179 // It's the user's responsibility to ensure that both From and To are
00180 // raw (i.e. has no CV modifier, is not a pointer, and is not a
00181 // reference) built-in arithmetic types, kFromKind is the kind of
00182 // From, and kToKind is the kind of To; the value is
00183 // implementation-defined when the above pre-condition is violated.
00184 template <TypeKind kFromKind, typename From, TypeKind kToKind, typename To>
00185 struct LosslessArithmeticConvertibleImpl : public false_type {};
00186 
00187 // Converting bool to bool is lossless.
00188 template <>
00189 struct LosslessArithmeticConvertibleImpl<kBool, bool, kBool, bool>
00190     : public true_type {};  // NOLINT
00191 
00192 // Converting bool to any integer type is lossless.
00193 template <typename To>
00194 struct LosslessArithmeticConvertibleImpl<kBool, bool, kInteger, To>
00195     : public true_type {};  // NOLINT
00196 
00197 // Converting bool to any floating-point type is lossless.
00198 template <typename To>
00199 struct LosslessArithmeticConvertibleImpl<kBool, bool, kFloatingPoint, To>
00200     : public true_type {};  // NOLINT
00201 
00202 // Converting an integer to bool is lossy.
00203 template <typename From>
00204 struct LosslessArithmeticConvertibleImpl<kInteger, From, kBool, bool>
00205     : public false_type {};  // NOLINT
00206 
00207 // Converting an integer to another non-bool integer is lossless iff
00208 // the target type's range encloses the source type's range.
00209 template <typename From, typename To>
00210 struct LosslessArithmeticConvertibleImpl<kInteger, From, kInteger, To>
00211     : public bool_constant<
00212       // When converting from a smaller size to a larger size, we are
00213       // fine as long as we are not converting from signed to unsigned.
00214       ((sizeof(From) < sizeof(To)) &&
00215        (!GMOCK_IS_SIGNED_(From) || GMOCK_IS_SIGNED_(To))) ||
00216       // When converting between the same size, the signedness must match.
00217       ((sizeof(From) == sizeof(To)) &&
00218        (GMOCK_IS_SIGNED_(From) == GMOCK_IS_SIGNED_(To)))> {};  // NOLINT
00219 
00220 #undef GMOCK_IS_SIGNED_
00221 
00222 // Converting an integer to a floating-point type may be lossy, since
00223 // the format of a floating-point number is implementation-defined.
00224 template <typename From, typename To>
00225 struct LosslessArithmeticConvertibleImpl<kInteger, From, kFloatingPoint, To>
00226     : public false_type {};  // NOLINT
00227 
00228 // Converting a floating-point to bool is lossy.
00229 template <typename From>
00230 struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kBool, bool>
00231     : public false_type {};  // NOLINT
00232 
00233 // Converting a floating-point to an integer is lossy.
00234 template <typename From, typename To>
00235 struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kInteger, To>
00236     : public false_type {};  // NOLINT
00237 
00238 // Converting a floating-point to another floating-point is lossless
00239 // iff the target type is at least as big as the source type.
00240 template <typename From, typename To>
00241 struct LosslessArithmeticConvertibleImpl<
00242   kFloatingPoint, From, kFloatingPoint, To>
00243     : public bool_constant<sizeof(From) <= sizeof(To)> {};  // NOLINT
00244 
00245 // LosslessArithmeticConvertible<From, To>::value is true iff arithmetic
00246 // type From can be losslessly converted to arithmetic type To.
00247 //
00248 // It's the user's responsibility to ensure that both From and To are
00249 // raw (i.e. has no CV modifier, is not a pointer, and is not a
00250 // reference) built-in arithmetic types; the value is
00251 // implementation-defined when the above pre-condition is violated.
00252 template <typename From, typename To>
00253 struct LosslessArithmeticConvertible
00254     : public LosslessArithmeticConvertibleImpl<
00255   GMOCK_KIND_OF_(From), From, GMOCK_KIND_OF_(To), To> {};  // NOLINT
00256 
00257 // This interface knows how to report a Google Mock failure (either
00258 // non-fatal or fatal).
00259 class FailureReporterInterface {
00260  public:
00261   // The type of a failure (either non-fatal or fatal).
00262   enum FailureType {
00263     kNonfatal, kFatal
00264   };
00265 
00266   virtual ~FailureReporterInterface() {}
00267 
00268   // Reports a failure that occurred at the given source file location.
00269   virtual void ReportFailure(FailureType type, const char* file, int line,
00270                              const string& message) = 0;
00271 };
00272 
00273 // Returns the failure reporter used by Google Mock.
00274 GTEST_API_ FailureReporterInterface* GetFailureReporter();
00275 
00276 // Asserts that condition is true; aborts the process with the given
00277 // message if condition is false.  We cannot use LOG(FATAL) or CHECK()
00278 // as Google Mock might be used to mock the log sink itself.  We
00279 // inline this function to prevent it from showing up in the stack
00280 // trace.
00281 inline void Assert(bool condition, const char* file, int line,
00282                    const string& msg) {
00283   if (!condition) {
00284     GetFailureReporter()->ReportFailure(FailureReporterInterface::kFatal,
00285                                         file, line, msg);
00286   }
00287 }
00288 inline void Assert(bool condition, const char* file, int line) {
00289   Assert(condition, file, line, "Assertion failed.");
00290 }
00291 
00292 // Verifies that condition is true; generates a non-fatal failure if
00293 // condition is false.
00294 inline void Expect(bool condition, const char* file, int line,
00295                    const string& msg) {
00296   if (!condition) {
00297     GetFailureReporter()->ReportFailure(FailureReporterInterface::kNonfatal,
00298                                         file, line, msg);
00299   }
00300 }
00301 inline void Expect(bool condition, const char* file, int line) {
00302   Expect(condition, file, line, "Expectation failed.");
00303 }
00304 
00305 // Severity level of a log.
00306 enum LogSeverity {
00307   kInfo = 0,
00308   kWarning = 1
00309 };
00310 
00311 // Valid values for the --gmock_verbose flag.
00312 
00313 // All logs (informational and warnings) are printed.
00314 const char kInfoVerbosity[] = "info";
00315 // Only warnings are printed.
00316 const char kWarningVerbosity[] = "warning";
00317 // No logs are printed.
00318 const char kErrorVerbosity[] = "error";
00319 
00320 // Returns true iff a log with the given severity is visible according
00321 // to the --gmock_verbose flag.
00322 GTEST_API_ bool LogIsVisible(LogSeverity severity);
00323 
00324 // Prints the given message to stdout iff 'severity' >= the level
00325 // specified by the --gmock_verbose flag.  If stack_frames_to_skip >=
00326 // 0, also prints the stack trace excluding the top
00327 // stack_frames_to_skip frames.  In opt mode, any positive
00328 // stack_frames_to_skip is treated as 0, since we don't know which
00329 // function calls will be inlined by the compiler and need to be
00330 // conservative.
00331 GTEST_API_ void Log(LogSeverity severity,
00332                     const string& message,
00333                     int stack_frames_to_skip);
00334 
00335 // TODO(wan@google.com): group all type utilities together.
00336 
00337 // Type traits.
00338 
00339 // is_reference<T>::value is non-zero iff T is a reference type.
00340 template <typename T> struct is_reference : public false_type {};
00341 template <typename T> struct is_reference<T&> : public true_type {};
00342 
00343 // type_equals<T1, T2>::value is non-zero iff T1 and T2 are the same type.
00344 template <typename T1, typename T2> struct type_equals : public false_type {};
00345 template <typename T> struct type_equals<T, T> : public true_type {};
00346 
00347 // remove_reference<T>::type removes the reference from type T, if any.
00348 template <typename T> struct remove_reference { typedef T type; };  // NOLINT
00349 template <typename T> struct remove_reference<T&> { typedef T type; }; // NOLINT
00350 
00351 // DecayArray<T>::type turns an array type U[N] to const U* and preserves
00352 // other types.  Useful for saving a copy of a function argument.
00353 template <typename T> struct DecayArray { typedef T type; };  // NOLINT
00354 template <typename T, size_t N> struct DecayArray<T[N]> {
00355   typedef const T* type;
00356 };
00357 // Sometimes people use arrays whose size is not available at the use site
00358 // (e.g. extern const char kNamePrefix[]).  This specialization covers that
00359 // case.
00360 template <typename T> struct DecayArray<T[]> {
00361   typedef const T* type;
00362 };
00363 
00364 // Invalid<T>() returns an invalid value of type T.  This is useful
00365 // when a value of type T is needed for compilation, but the statement
00366 // will not really be executed (or we don't care if the statement
00367 // crashes).
00368 template <typename T>
00369 inline T Invalid() {
00370   return const_cast<typename remove_reference<T>::type&>(
00371       *static_cast<volatile typename remove_reference<T>::type*>(NULL));
00372 }
00373 template <>
00374 inline void Invalid<void>() {}
00375 
00376 // Given a raw type (i.e. having no top-level reference or const
00377 // modifier) RawContainer that's either an STL-style container or a
00378 // native array, class StlContainerView<RawContainer> has the
00379 // following members:
00380 //
00381 //   - type is a type that provides an STL-style container view to
00382 //     (i.e. implements the STL container concept for) RawContainer;
00383 //   - const_reference is a type that provides a reference to a const
00384 //     RawContainer;
00385 //   - ConstReference(raw_container) returns a const reference to an STL-style
00386 //     container view to raw_container, which is a RawContainer.
00387 //   - Copy(raw_container) returns an STL-style container view of a
00388 //     copy of raw_container, which is a RawContainer.
00389 //
00390 // This generic version is used when RawContainer itself is already an
00391 // STL-style container.
00392 template <class RawContainer>
00393 class StlContainerView {
00394  public:
00395   typedef RawContainer type;
00396   typedef const type& const_reference;
00397 
00398   static const_reference ConstReference(const RawContainer& container) {
00399     // Ensures that RawContainer is not a const type.
00400     testing::StaticAssertTypeEq<RawContainer,
00401         GTEST_REMOVE_CONST_(RawContainer)>();
00402     return container;
00403   }
00404   static type Copy(const RawContainer& container) { return container; }
00405 };
00406 
00407 // This specialization is used when RawContainer is a native array type.
00408 template <typename Element, size_t N>
00409 class StlContainerView<Element[N]> {
00410  public:
00411   typedef GTEST_REMOVE_CONST_(Element) RawElement;
00412   typedef internal::NativeArray<RawElement> type;
00413   // NativeArray<T> can represent a native array either by value or by
00414   // reference (selected by a constructor argument), so 'const type'
00415   // can be used to reference a const native array.  We cannot
00416   // 'typedef const type& const_reference' here, as that would mean
00417   // ConstReference() has to return a reference to a local variable.
00418   typedef const type const_reference;
00419 
00420   static const_reference ConstReference(const Element (&array)[N]) {
00421     // Ensures that Element is not a const type.
00422     testing::StaticAssertTypeEq<Element, RawElement>();
00423 #if GTEST_OS_SYMBIAN
00424     // The Nokia Symbian compiler confuses itself in template instantiation
00425     // for this call without the cast to Element*:
00426     // function call '[testing::internal::NativeArray<char *>].NativeArray(
00427     //     {lval} const char *[4], long, testing::internal::RelationToSource)'
00428     //     does not match
00429     // 'testing::internal::NativeArray<char *>::NativeArray(
00430     //     char *const *, unsigned int, testing::internal::RelationToSource)'
00431     // (instantiating: 'testing::internal::ContainsMatcherImpl
00432     //     <const char * (&)[4]>::Matches(const char * (&)[4]) const')
00433     // (instantiating: 'testing::internal::StlContainerView<char *[4]>::
00434     //     ConstReference(const char * (&)[4])')
00435     // (and though the N parameter type is mismatched in the above explicit
00436     // conversion of it doesn't help - only the conversion of the array).
00437     return type(const_cast<Element*>(&array[0]), N, kReference);
00438 #else
00439     return type(array, N, kReference);
00440 #endif  // GTEST_OS_SYMBIAN
00441   }
00442   static type Copy(const Element (&array)[N]) {
00443 #if GTEST_OS_SYMBIAN
00444     return type(const_cast<Element*>(&array[0]), N, kCopy);
00445 #else
00446     return type(array, N, kCopy);
00447 #endif  // GTEST_OS_SYMBIAN
00448   }
00449 };
00450 
00451 // This specialization is used when RawContainer is a native array
00452 // represented as a (pointer, size) tuple.
00453 template <typename ElementPointer, typename Size>
00454 class StlContainerView< ::std::tr1::tuple<ElementPointer, Size> > {
00455  public:
00456   typedef GTEST_REMOVE_CONST_(
00457       typename internal::PointeeOf<ElementPointer>::type) RawElement;
00458   typedef internal::NativeArray<RawElement> type;
00459   typedef const type const_reference;
00460 
00461   static const_reference ConstReference(
00462       const ::std::tr1::tuple<ElementPointer, Size>& array) {
00463     using ::std::tr1::get;
00464     return type(get<0>(array), get<1>(array), kReference);
00465   }
00466   static type Copy(const ::std::tr1::tuple<ElementPointer, Size>& array) {
00467     using ::std::tr1::get;
00468     return type(get<0>(array), get<1>(array), kCopy);
00469   }
00470 };
00471 
00472 // The following specialization prevents the user from instantiating
00473 // StlContainer with a reference type.
00474 template <typename T> class StlContainerView<T&>;
00475 
00476 // A type transform to remove constness from the first part of a pair.
00477 // Pairs like that are used as the value_type of associative containers,
00478 // and this transform produces a similar but assignable pair.
00479 template <typename T>
00480 struct RemoveConstFromKey {
00481   typedef T type;
00482 };
00483 
00484 // Partially specialized to remove constness from std::pair<const K, V>.
00485 template <typename K, typename V>
00486 struct RemoveConstFromKey<std::pair<const K, V> > {
00487   typedef std::pair<K, V> type;
00488 };
00489 
00490 // Mapping from booleans to types. Similar to boost::bool_<kValue> and
00491 // std::integral_constant<bool, kValue>.
00492 template <bool kValue>
00493 struct BooleanConstant {};
00494 
00495 }  // namespace internal
00496 }  // namespace testing
00497 
00498 #endif  // GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:41