00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
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>
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
00053
00054
00055
00056 GTEST_API_ string ConvertIdentifierNameToWords(const char* id_name);
00057
00058
00059
00060
00061
00062 template <typename Pointer>
00063 struct PointeeOf {
00064
00065
00066 typedef typename Pointer::element_type type;
00067 };
00068
00069 template <typename T>
00070 struct PointeeOf<T*> { typedef T type; };
00071
00072
00073
00074
00075 template <typename Pointer>
00076 inline const typename Pointer::element_type* GetRawPointer(const Pointer& p) {
00077 return p.get();
00078 }
00079
00080 template <typename Element>
00081 inline Element* GetRawPointer(Element* p) { return p; }
00082
00083
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
00093
00094
00095
00096
00097
00098
00099
00100 #if (GTEST_OS_SYMBIAN && defined(_STLP_NO_WCHAR_T)) || \
00101 (defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED))
00102
00103 #else
00104 # define GMOCK_WCHAR_T_IS_NATIVE_ 1
00105 #endif
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 #ifdef __GNUC__
00117
00118 # define GMOCK_HAS_SIGNED_WCHAR_T_ 1
00119 #endif
00120
00121
00122
00123
00124
00125
00126 enum TypeKind {
00127 kBool, kInteger, kFloatingPoint, kOther
00128 };
00129
00130
00131 template <typename T> struct KindOf {
00132 enum { value = kOther };
00133 };
00134
00135
00136 #define GMOCK_DECLARE_KIND_(type, kind) \
00137 template <> struct KindOf<type> { enum { value = kind }; }
00138
00139 GMOCK_DECLARE_KIND_(bool, kBool);
00140
00141
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);
00146 GMOCK_DECLARE_KIND_(unsigned short, kInteger);
00147 GMOCK_DECLARE_KIND_(int, kInteger);
00148 GMOCK_DECLARE_KIND_(unsigned int, kInteger);
00149 GMOCK_DECLARE_KIND_(long, kInteger);
00150 GMOCK_DECLARE_KIND_(unsigned long, kInteger);
00151
00152 #if GMOCK_WCHAR_T_IS_NATIVE_
00153 GMOCK_DECLARE_KIND_(wchar_t, kInteger);
00154 #endif
00155
00156
00157 GMOCK_DECLARE_KIND_(Int64, kInteger);
00158 GMOCK_DECLARE_KIND_(UInt64, kInteger);
00159
00160
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
00168 #define GMOCK_KIND_OF_(type) \
00169 static_cast< ::testing::internal::TypeKind>( \
00170 ::testing::internal::KindOf<type>::value)
00171
00172
00173 #define GMOCK_IS_SIGNED_(T) (static_cast<T>(-1) < 0)
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 template <TypeKind kFromKind, typename From, TypeKind kToKind, typename To>
00185 struct LosslessArithmeticConvertibleImpl : public false_type {};
00186
00187
00188 template <>
00189 struct LosslessArithmeticConvertibleImpl<kBool, bool, kBool, bool>
00190 : public true_type {};
00191
00192
00193 template <typename To>
00194 struct LosslessArithmeticConvertibleImpl<kBool, bool, kInteger, To>
00195 : public true_type {};
00196
00197
00198 template <typename To>
00199 struct LosslessArithmeticConvertibleImpl<kBool, bool, kFloatingPoint, To>
00200 : public true_type {};
00201
00202
00203 template <typename From>
00204 struct LosslessArithmeticConvertibleImpl<kInteger, From, kBool, bool>
00205 : public false_type {};
00206
00207
00208
00209 template <typename From, typename To>
00210 struct LosslessArithmeticConvertibleImpl<kInteger, From, kInteger, To>
00211 : public bool_constant<
00212
00213
00214 ((sizeof(From) < sizeof(To)) &&
00215 (!GMOCK_IS_SIGNED_(From) || GMOCK_IS_SIGNED_(To))) ||
00216
00217 ((sizeof(From) == sizeof(To)) &&
00218 (GMOCK_IS_SIGNED_(From) == GMOCK_IS_SIGNED_(To)))> {};
00219
00220 #undef GMOCK_IS_SIGNED_
00221
00222
00223
00224 template <typename From, typename To>
00225 struct LosslessArithmeticConvertibleImpl<kInteger, From, kFloatingPoint, To>
00226 : public false_type {};
00227
00228
00229 template <typename From>
00230 struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kBool, bool>
00231 : public false_type {};
00232
00233
00234 template <typename From, typename To>
00235 struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kInteger, To>
00236 : public false_type {};
00237
00238
00239
00240 template <typename From, typename To>
00241 struct LosslessArithmeticConvertibleImpl<
00242 kFloatingPoint, From, kFloatingPoint, To>
00243 : public bool_constant<sizeof(From) <= sizeof(To)> {};
00244
00245
00246
00247
00248
00249
00250
00251
00252 template <typename From, typename To>
00253 struct LosslessArithmeticConvertible
00254 : public LosslessArithmeticConvertibleImpl<
00255 GMOCK_KIND_OF_(From), From, GMOCK_KIND_OF_(To), To> {};
00256
00257
00258
00259 class FailureReporterInterface {
00260 public:
00261
00262 enum FailureType {
00263 kNonfatal, kFatal
00264 };
00265
00266 virtual ~FailureReporterInterface() {}
00267
00268
00269 virtual void ReportFailure(FailureType type, const char* file, int line,
00270 const string& message) = 0;
00271 };
00272
00273
00274 GTEST_API_ FailureReporterInterface* GetFailureReporter();
00275
00276
00277
00278
00279
00280
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
00293
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
00306 enum LogSeverity {
00307 kInfo = 0,
00308 kWarning = 1
00309 };
00310
00311
00312
00313
00314 const char kInfoVerbosity[] = "info";
00315
00316 const char kWarningVerbosity[] = "warning";
00317
00318 const char kErrorVerbosity[] = "error";
00319
00320
00321
00322 GTEST_API_ bool LogIsVisible(LogSeverity severity);
00323
00324
00325
00326
00327
00328
00329
00330
00331 GTEST_API_ void Log(LogSeverity severity,
00332 const string& message,
00333 int stack_frames_to_skip);
00334
00335
00336
00337
00338
00339
00340 template <typename T> struct is_reference : public false_type {};
00341 template <typename T> struct is_reference<T&> : public true_type {};
00342
00343
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
00348 template <typename T> struct remove_reference { typedef T type; };
00349 template <typename T> struct remove_reference<T&> { typedef T type; };
00350
00351
00352
00353 template <typename T> struct DecayArray { typedef T type; };
00354 template <typename T, size_t N> struct DecayArray<T[N]> {
00355 typedef const T* type;
00356 };
00357
00358
00359
00360 template <typename T> struct DecayArray<T[]> {
00361 typedef const T* type;
00362 };
00363
00364
00365
00366
00367
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
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
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
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
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
00414
00415
00416
00417
00418 typedef const type const_reference;
00419
00420 static const_reference ConstReference(const Element (&array)[N]) {
00421
00422 testing::StaticAssertTypeEq<Element, RawElement>();
00423 #if GTEST_OS_SYMBIAN
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
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
00452
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
00473
00474 template <typename T> class StlContainerView<T&>;
00475
00476
00477
00478
00479 template <typename T>
00480 struct RemoveConstFromKey {
00481 typedef T type;
00482 };
00483
00484
00485 template <typename K, typename V>
00486 struct RemoveConstFromKey<std::pair<const K, V> > {
00487 typedef std::pair<K, V> type;
00488 };
00489
00490
00491
00492 template <bool kValue>
00493 struct BooleanConstant {};
00494
00495 }
00496 }
00497
00498 #endif // GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_