File indexing completed on 2025-09-15 09:00:24
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 #ifndef GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_
0040 #define GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_
0041
0042 #include <stdio.h>
0043
0044 #include <ostream> // NOLINT
0045 #include <string>
0046 #include <type_traits>
0047 #include <utility>
0048 #include <vector>
0049
0050 #include "gmock/internal/gmock-port.h"
0051 #include "gtest/gtest.h"
0052
0053 namespace testing {
0054
0055 template <typename>
0056 class Matcher;
0057
0058 namespace internal {
0059
0060
0061
0062 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100 4805)
0063
0064
0065
0066 GTEST_API_ std::string JoinAsKeyValueTuple(
0067 const std::vector<const char*>& names, const Strings& values);
0068
0069
0070
0071
0072
0073 GTEST_API_ std::string ConvertIdentifierNameToWords(const char* id_name);
0074
0075
0076
0077
0078 template <typename Pointer>
0079 inline const typename Pointer::element_type* GetRawPointer(const Pointer& p) {
0080 return p.get();
0081 }
0082
0083
0084 template <typename Element>
0085 inline const Element* GetRawPointer(const std::reference_wrapper<Element>& r) {
0086 return &r.get();
0087 }
0088
0089
0090 template <typename Element>
0091 inline Element* GetRawPointer(Element* p) {
0092 return p;
0093 }
0094
0095
0096
0097
0098
0099 #define GMOCK_INTERNAL_WARNING_PUSH()
0100 #define GMOCK_INTERNAL_WARNING_CLANG(Level, Name)
0101 #define GMOCK_INTERNAL_WARNING_POP()
0102
0103 #if defined(__clang__)
0104 #undef GMOCK_INTERNAL_WARNING_PUSH
0105 #define GMOCK_INTERNAL_WARNING_PUSH() _Pragma("clang diagnostic push")
0106 #undef GMOCK_INTERNAL_WARNING_CLANG
0107 #define GMOCK_INTERNAL_WARNING_CLANG(Level, Warning) \
0108 _Pragma(GMOCK_PP_INTERNAL_STRINGIZE(clang diagnostic Level Warning))
0109 #undef GMOCK_INTERNAL_WARNING_POP
0110 #define GMOCK_INTERNAL_WARNING_POP() _Pragma("clang diagnostic pop")
0111 #endif
0112
0113
0114
0115
0116
0117 #if defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED)
0118
0119 #else
0120 #define GMOCK_WCHAR_T_IS_NATIVE_ 1
0121 #endif
0122
0123
0124
0125
0126
0127
0128 enum TypeKind { kBool, kInteger, kFloatingPoint, kOther };
0129
0130
0131 template <typename T>
0132 struct KindOf {
0133 enum { value = kOther };
0134 };
0135
0136
0137 #define GMOCK_DECLARE_KIND_(type, kind) \
0138 template <> \
0139 struct KindOf<type> { \
0140 enum { value = kind }; \
0141 }
0142
0143 GMOCK_DECLARE_KIND_(bool, kBool);
0144
0145
0146 GMOCK_DECLARE_KIND_(char, kInteger);
0147 GMOCK_DECLARE_KIND_(signed char, kInteger);
0148 GMOCK_DECLARE_KIND_(unsigned char, kInteger);
0149 GMOCK_DECLARE_KIND_(short, kInteger);
0150 GMOCK_DECLARE_KIND_(unsigned short, kInteger);
0151 GMOCK_DECLARE_KIND_(int, kInteger);
0152 GMOCK_DECLARE_KIND_(unsigned int, kInteger);
0153 GMOCK_DECLARE_KIND_(long, kInteger);
0154 GMOCK_DECLARE_KIND_(unsigned long, kInteger);
0155 GMOCK_DECLARE_KIND_(long long, kInteger);
0156 GMOCK_DECLARE_KIND_(unsigned long long, kInteger);
0157
0158 #if GMOCK_WCHAR_T_IS_NATIVE_
0159 GMOCK_DECLARE_KIND_(wchar_t, kInteger);
0160 #endif
0161
0162
0163 GMOCK_DECLARE_KIND_(float, kFloatingPoint);
0164 GMOCK_DECLARE_KIND_(double, kFloatingPoint);
0165 GMOCK_DECLARE_KIND_(long double, kFloatingPoint);
0166
0167 #undef GMOCK_DECLARE_KIND_
0168
0169
0170 #define GMOCK_KIND_OF_(type) \
0171 static_cast< ::testing::internal::TypeKind>( \
0172 ::testing::internal::KindOf<type>::value)
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183 template <TypeKind kFromKind, typename From, TypeKind kToKind, typename To>
0184 using LosslessArithmeticConvertibleImpl = std::integral_constant<
0185 bool,
0186
0187
0188 (kFromKind == kBool) ? true
0189
0190
0191 : (kFromKind != kToKind) ? false
0192 : (kFromKind == kInteger &&
0193
0194
0195 (((sizeof(From) < sizeof(To)) &&
0196 !(std::is_signed<From>::value && !std::is_signed<To>::value)) ||
0197
0198
0199 ((sizeof(From) == sizeof(To)) &&
0200 (std::is_signed<From>::value == std::is_signed<To>::value)))
0201 ) ? true
0202
0203
0204 : (kFromKind == kFloatingPoint && (sizeof(From) <= sizeof(To))) ? true
0205 : false
0206
0207 >;
0208
0209
0210
0211
0212
0213
0214
0215
0216 template <typename From, typename To>
0217 using LosslessArithmeticConvertible =
0218 LosslessArithmeticConvertibleImpl<GMOCK_KIND_OF_(From), From,
0219 GMOCK_KIND_OF_(To), To>;
0220
0221
0222
0223 class FailureReporterInterface {
0224 public:
0225
0226 enum FailureType { kNonfatal, kFatal };
0227
0228 virtual ~FailureReporterInterface() = default;
0229
0230
0231 virtual void ReportFailure(FailureType type, const char* file, int line,
0232 const std::string& message) = 0;
0233 };
0234
0235
0236 GTEST_API_ FailureReporterInterface* GetFailureReporter();
0237
0238
0239
0240
0241
0242
0243 inline void Assert(bool condition, const char* file, int line,
0244 const std::string& msg) {
0245 if (!condition) {
0246 GetFailureReporter()->ReportFailure(FailureReporterInterface::kFatal, file,
0247 line, msg);
0248 }
0249 }
0250 inline void Assert(bool condition, const char* file, int line) {
0251 Assert(condition, file, line, "Assertion failed.");
0252 }
0253
0254
0255
0256 inline void Expect(bool condition, const char* file, int line,
0257 const std::string& msg) {
0258 if (!condition) {
0259 GetFailureReporter()->ReportFailure(FailureReporterInterface::kNonfatal,
0260 file, line, msg);
0261 }
0262 }
0263 inline void Expect(bool condition, const char* file, int line) {
0264 Expect(condition, file, line, "Expectation failed.");
0265 }
0266
0267
0268 enum LogSeverity { kInfo = 0, kWarning = 1 };
0269
0270
0271
0272
0273 const char kInfoVerbosity[] = "info";
0274
0275 const char kWarningVerbosity[] = "warning";
0276
0277 const char kErrorVerbosity[] = "error";
0278
0279
0280
0281 GTEST_API_ bool LogIsVisible(LogSeverity severity);
0282
0283
0284
0285
0286
0287
0288
0289
0290 GTEST_API_ void Log(LogSeverity severity, const std::string& message,
0291 int stack_frames_to_skip);
0292
0293
0294
0295
0296
0297
0298
0299 class WithoutMatchers {
0300 private:
0301 WithoutMatchers() {}
0302 friend GTEST_API_ WithoutMatchers GetWithoutMatchers();
0303 };
0304
0305
0306 GTEST_API_ WithoutMatchers GetWithoutMatchers();
0307
0308
0309
0310
0311
0312
0313 template <typename T>
0314 inline T Invalid() {
0315 Assert(false, "", -1,
0316 "Internal error: attempt to return invalid value");
0317 #if defined(__GNUC__) || defined(__clang__)
0318 __builtin_unreachable();
0319 #elif defined(_MSC_VER)
0320 __assume(0);
0321 #else
0322 return Invalid<T>();
0323 #endif
0324 }
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342 template <class RawContainer>
0343 class StlContainerView {
0344 public:
0345 typedef RawContainer type;
0346 typedef const type& const_reference;
0347
0348 static const_reference ConstReference(const RawContainer& container) {
0349 static_assert(!std::is_const<RawContainer>::value,
0350 "RawContainer type must not be const");
0351 return container;
0352 }
0353 static type Copy(const RawContainer& container) { return container; }
0354 };
0355
0356
0357 template <typename Element, size_t N>
0358 class StlContainerView<Element[N]> {
0359 public:
0360 typedef typename std::remove_const<Element>::type RawElement;
0361 typedef internal::NativeArray<RawElement> type;
0362
0363
0364
0365
0366
0367 typedef const type const_reference;
0368
0369 static const_reference ConstReference(const Element (&array)[N]) {
0370 static_assert(std::is_same<Element, RawElement>::value,
0371 "Element type must not be const");
0372 return type(array, N, RelationToSourceReference());
0373 }
0374 static type Copy(const Element (&array)[N]) {
0375 return type(array, N, RelationToSourceCopy());
0376 }
0377 };
0378
0379
0380
0381 template <typename ElementPointer, typename Size>
0382 class StlContainerView< ::std::tuple<ElementPointer, Size> > {
0383 public:
0384 typedef typename std::remove_const<
0385 typename std::pointer_traits<ElementPointer>::element_type>::type
0386 RawElement;
0387 typedef internal::NativeArray<RawElement> type;
0388 typedef const type const_reference;
0389
0390 static const_reference ConstReference(
0391 const ::std::tuple<ElementPointer, Size>& array) {
0392 return type(std::get<0>(array), std::get<1>(array),
0393 RelationToSourceReference());
0394 }
0395 static type Copy(const ::std::tuple<ElementPointer, Size>& array) {
0396 return type(std::get<0>(array), std::get<1>(array), RelationToSourceCopy());
0397 }
0398 };
0399
0400
0401
0402 template <typename T>
0403 class StlContainerView<T&>;
0404
0405
0406
0407
0408 template <typename T>
0409 struct RemoveConstFromKey {
0410 typedef T type;
0411 };
0412
0413
0414 template <typename K, typename V>
0415 struct RemoveConstFromKey<std::pair<const K, V> > {
0416 typedef std::pair<K, V> type;
0417 };
0418
0419
0420
0421 GTEST_API_ void IllegalDoDefault(const char* file, int line);
0422
0423 template <typename F, typename Tuple, size_t... Idx>
0424 auto ApplyImpl(F&& f, Tuple&& args, std::index_sequence<Idx...>)
0425 -> decltype(std::forward<F>(f)(
0426 std::get<Idx>(std::forward<Tuple>(args))...)) {
0427 return std::forward<F>(f)(std::get<Idx>(std::forward<Tuple>(args))...);
0428 }
0429
0430
0431 template <typename F, typename Tuple>
0432 auto Apply(F&& f, Tuple&& args)
0433 -> decltype(ApplyImpl(
0434 std::forward<F>(f), std::forward<Tuple>(args),
0435 std::make_index_sequence<std::tuple_size<
0436 typename std::remove_reference<Tuple>::type>::value>())) {
0437 return ApplyImpl(std::forward<F>(f), std::forward<Tuple>(args),
0438 std::make_index_sequence<std::tuple_size<
0439 typename std::remove_reference<Tuple>::type>::value>());
0440 }
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455 template <typename T>
0456 struct Function;
0457
0458 template <typename R, typename... Args>
0459 struct Function<R(Args...)> {
0460 using Result = R;
0461 static constexpr size_t ArgumentCount = sizeof...(Args);
0462 template <size_t I>
0463 using Arg = ElemFromList<I, Args...>;
0464 using ArgumentTuple = std::tuple<Args...>;
0465 using ArgumentMatcherTuple = std::tuple<Matcher<Args>...>;
0466 using MakeResultVoid = void(Args...);
0467 using MakeResultIgnoredValue = IgnoredValue(Args...);
0468 };
0469
0470
0471
0472
0473
0474 template <size_t I, typename T>
0475 using TupleElement = typename std::tuple_element<I, T>::type;
0476
0477 bool Base64Unescape(const std::string& encoded, std::string* decoded);
0478
0479 GTEST_DISABLE_MSC_WARNINGS_POP_()
0480
0481 }
0482 }
0483
0484 #endif