File indexing completed on 2025-09-17 09:00:40
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
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130 #ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
0131 #define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
0132
0133 #ifndef _WIN32_WCE
0134 #include <errno.h>
0135 #endif
0136
0137 #include <algorithm>
0138 #include <exception>
0139 #include <functional>
0140 #include <memory>
0141 #include <string>
0142 #include <tuple>
0143 #include <type_traits>
0144 #include <utility>
0145
0146 #include "gmock/internal/gmock-internal-utils.h"
0147 #include "gmock/internal/gmock-port.h"
0148 #include "gmock/internal/gmock-pp.h"
0149
0150 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
0151
0152 namespace testing {
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163 namespace internal {
0164
0165
0166
0167
0168
0169
0170 template <typename T, bool kDefaultConstructible>
0171 struct BuiltInDefaultValueGetter {
0172 static T Get() { return T(); }
0173 };
0174 template <typename T>
0175 struct BuiltInDefaultValueGetter<T, false> {
0176 static T Get() {
0177 Assert(false, __FILE__, __LINE__,
0178 "Default action undefined for the function return type.");
0179 #if defined(__GNUC__) || defined(__clang__)
0180 __builtin_unreachable();
0181 #elif defined(_MSC_VER)
0182 __assume(0);
0183 #else
0184 return Invalid<T>();
0185
0186
0187 #endif
0188 }
0189 };
0190
0191
0192
0193
0194
0195
0196
0197
0198 template <typename T>
0199 class BuiltInDefaultValue {
0200 public:
0201
0202
0203 static bool Exists() { return ::std::is_default_constructible<T>::value; }
0204
0205 static T Get() {
0206 return BuiltInDefaultValueGetter<
0207 T, ::std::is_default_constructible<T>::value>::Get();
0208 }
0209 };
0210
0211
0212
0213 template <typename T>
0214 class BuiltInDefaultValue<const T> {
0215 public:
0216 static bool Exists() { return BuiltInDefaultValue<T>::Exists(); }
0217 static T Get() { return BuiltInDefaultValue<T>::Get(); }
0218 };
0219
0220
0221
0222 template <typename T>
0223 class BuiltInDefaultValue<T*> {
0224 public:
0225 static bool Exists() { return true; }
0226 static T* Get() { return nullptr; }
0227 };
0228
0229
0230
0231 #define GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(type, value) \
0232 template <> \
0233 class BuiltInDefaultValue<type> { \
0234 public: \
0235 static bool Exists() { return true; } \
0236 static type Get() { return value; } \
0237 }
0238
0239 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(void, );
0240 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::std::string, "");
0241 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(bool, false);
0242 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned char, '\0');
0243 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed char, '\0');
0244 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(char, '\0');
0245
0246
0247
0248
0249
0250
0251
0252 #if GMOCK_WCHAR_T_IS_NATIVE_
0253 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(wchar_t, 0U);
0254 #endif
0255
0256 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned short, 0U);
0257 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed short, 0);
0258 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned int, 0U);
0259 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed int, 0);
0260 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned long, 0UL);
0261 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed long, 0L);
0262 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned long long, 0);
0263 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed long long, 0);
0264 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(float, 0);
0265 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(double, 0);
0266
0267 #undef GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_
0268
0269
0270
0271
0272 template <typename P>
0273 struct negation
0274
0275 : std::integral_constant<bool, bool(!P::value)> {};
0276
0277
0278 template <typename...>
0279 struct conjunction : std::true_type {};
0280
0281
0282 template <typename P1>
0283 struct conjunction<P1> : P1 {};
0284
0285
0286
0287 template <typename P1, typename... Ps>
0288 struct conjunction<P1, Ps...>
0289 : std::conditional<bool(P1::value), conjunction<Ps...>, P1>::type {};
0290
0291 template <typename...>
0292 struct disjunction : std::false_type {};
0293
0294 template <typename P1>
0295 struct disjunction<P1> : P1 {};
0296
0297 template <typename P1, typename... Ps>
0298 struct disjunction<P1, Ps...>
0299
0300 : std::conditional<!bool(P1::value), disjunction<Ps...>, P1>::type {};
0301
0302 template <typename...>
0303 using void_t = void;
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327 template <typename From, typename To>
0328 struct is_implicitly_convertible {
0329 private:
0330
0331
0332 template <typename T>
0333 static void Accept(T);
0334
0335
0336 template <typename T>
0337 static T Make();
0338
0339
0340 template <typename T, typename = decltype(Accept<To>(Make<T>()))>
0341 static std::true_type TestImplicitConversion(int);
0342
0343
0344 template <typename T>
0345 static std::false_type TestImplicitConversion(...);
0346
0347 public:
0348 using type = decltype(TestImplicitConversion<From>(0));
0349 static constexpr bool value = type::value;
0350 };
0351
0352
0353
0354
0355 template <typename F, typename... Args>
0356 using call_result_t = decltype(std::declval<F>()(std::declval<Args>()...));
0357
0358 template <typename Void, typename R, typename F, typename... Args>
0359 struct is_callable_r_impl : std::false_type {};
0360
0361
0362
0363
0364 template <typename R, typename F, typename... Args>
0365 struct is_callable_r_impl<void_t<call_result_t<F, Args...>>, R, F, Args...>
0366 : std::conditional<
0367 std::is_void<R>::value,
0368 std::true_type,
0369 is_implicitly_convertible<call_result_t<F, Args...>, R>>::type {};
0370
0371
0372
0373 template <typename R, typename F, typename... Args>
0374 using is_callable_r = is_callable_r_impl<void, R, F, Args...>;
0375
0376
0377 template <typename T>
0378 typename std::add_const<T>::type& as_const(T& t) {
0379 return t;
0380 }
0381
0382 }
0383
0384
0385 template <typename F>
0386 class OnceAction;
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423 template <typename Result, typename... Args>
0424 class OnceAction<Result(Args...)> final {
0425 private:
0426
0427
0428 template <typename Callable>
0429 using IsDirectlyCompatible = internal::conjunction<
0430
0431 std::is_constructible<typename std::decay<Callable>::type, Callable>,
0432
0433 internal::is_callable_r<Result, typename std::decay<Callable>::type,
0434 Args...>>;
0435
0436
0437
0438 template <typename Callable>
0439 using IsCompatibleAfterIgnoringArguments = internal::conjunction<
0440
0441 std::is_constructible<typename std::decay<Callable>::type, Callable>,
0442
0443
0444 internal::is_callable_r<Result, typename std::decay<Callable>::type>>;
0445
0446 public:
0447
0448
0449
0450 template <typename Callable,
0451 typename std::enable_if<
0452 internal::conjunction<
0453
0454
0455
0456
0457 internal::negation<std::is_same<
0458 OnceAction, typename std::decay<Callable>::type>>,
0459 IsDirectlyCompatible<Callable>>
0460 ::value,
0461 int>::type = 0>
0462 OnceAction(Callable&& callable)
0463 : function_(StdFunctionAdaptor<typename std::decay<Callable>::type>(
0464 {}, std::forward<Callable>(callable))) {}
0465
0466
0467 template <typename Callable,
0468 typename std::enable_if<
0469 internal::conjunction<
0470
0471
0472
0473
0474 internal::negation<std::is_same<
0475 OnceAction, typename std::decay<Callable>::type>>,
0476
0477
0478 internal::negation<IsDirectlyCompatible<Callable>>,
0479 IsCompatibleAfterIgnoringArguments<Callable>>::value,
0480 int>::type = 0>
0481 OnceAction(Callable&& callable)
0482
0483
0484 : OnceAction(IgnoreIncomingArguments<typename std::decay<Callable>::type>{
0485 std::forward<Callable>(callable)}) {}
0486
0487
0488
0489 OnceAction(const OnceAction&) = delete;
0490 OnceAction& operator=(const OnceAction&) = delete;
0491 OnceAction(OnceAction&&) = default;
0492
0493
0494
0495 Result Call(Args... args) && {
0496 return function_(std::forward<Args>(args)...);
0497 }
0498
0499 private:
0500
0501
0502
0503
0504
0505
0506
0507 template <typename Callable>
0508 class StdFunctionAdaptor final {
0509 public:
0510
0511
0512
0513 struct CallableTag final {};
0514
0515 template <typename F>
0516 explicit StdFunctionAdaptor(CallableTag, F&& callable)
0517 : callable_(std::make_shared<Callable>(std::forward<F>(callable))) {}
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537 template <typename... ArgRefs>
0538 internal::call_result_t<Callable, ArgRefs...> operator()(
0539 ArgRefs&&... args) const {
0540 return std::move(*callable_)(std::forward<ArgRefs>(args)...);
0541 }
0542
0543 private:
0544
0545
0546 std::shared_ptr<Callable> callable_;
0547 };
0548
0549
0550
0551 template <typename Callable>
0552 struct IgnoreIncomingArguments {
0553 internal::call_result_t<Callable> operator()(Args&&...) {
0554 return std::move(callable)();
0555 }
0556
0557 Callable callable;
0558 };
0559
0560 std::function<Result(Args...)> function_;
0561 };
0562
0563
0564
0565
0566
0567
0568
0569
0570
0571
0572
0573
0574
0575
0576 template <typename T>
0577 class DefaultValue {
0578 public:
0579
0580
0581 static void Set(T x) {
0582 delete producer_;
0583 producer_ = new FixedValueProducer(x);
0584 }
0585
0586
0587
0588
0589 typedef T (*FactoryFunction)();
0590 static void SetFactory(FactoryFunction factory) {
0591 delete producer_;
0592 producer_ = new FactoryValueProducer(factory);
0593 }
0594
0595
0596 static void Clear() {
0597 delete producer_;
0598 producer_ = nullptr;
0599 }
0600
0601
0602 static bool IsSet() { return producer_ != nullptr; }
0603
0604
0605
0606 static bool Exists() {
0607 return IsSet() || internal::BuiltInDefaultValue<T>::Exists();
0608 }
0609
0610
0611
0612
0613 static T Get() {
0614 return producer_ == nullptr ? internal::BuiltInDefaultValue<T>::Get()
0615 : producer_->Produce();
0616 }
0617
0618 private:
0619 class ValueProducer {
0620 public:
0621 virtual ~ValueProducer() = default;
0622 virtual T Produce() = 0;
0623 };
0624
0625 class FixedValueProducer : public ValueProducer {
0626 public:
0627 explicit FixedValueProducer(T value) : value_(value) {}
0628 T Produce() override { return value_; }
0629
0630 private:
0631 const T value_;
0632 FixedValueProducer(const FixedValueProducer&) = delete;
0633 FixedValueProducer& operator=(const FixedValueProducer&) = delete;
0634 };
0635
0636 class FactoryValueProducer : public ValueProducer {
0637 public:
0638 explicit FactoryValueProducer(FactoryFunction factory)
0639 : factory_(factory) {}
0640 T Produce() override { return factory_(); }
0641
0642 private:
0643 const FactoryFunction factory_;
0644 FactoryValueProducer(const FactoryValueProducer&) = delete;
0645 FactoryValueProducer& operator=(const FactoryValueProducer&) = delete;
0646 };
0647
0648 static ValueProducer* producer_;
0649 };
0650
0651
0652
0653 template <typename T>
0654 class DefaultValue<T&> {
0655 public:
0656
0657 static void Set(T& x) {
0658 address_ = &x;
0659 }
0660
0661
0662 static void Clear() { address_ = nullptr; }
0663
0664
0665 static bool IsSet() { return address_ != nullptr; }
0666
0667
0668
0669 static bool Exists() {
0670 return IsSet() || internal::BuiltInDefaultValue<T&>::Exists();
0671 }
0672
0673
0674
0675
0676 static T& Get() {
0677 return address_ == nullptr ? internal::BuiltInDefaultValue<T&>::Get()
0678 : *address_;
0679 }
0680
0681 private:
0682 static T* address_;
0683 };
0684
0685
0686
0687 template <>
0688 class DefaultValue<void> {
0689 public:
0690 static bool Exists() { return true; }
0691 static void Get() {}
0692 };
0693
0694
0695 template <typename T>
0696 typename DefaultValue<T>::ValueProducer* DefaultValue<T>::producer_ = nullptr;
0697
0698
0699 template <typename T>
0700 T* DefaultValue<T&>::address_ = nullptr;
0701
0702
0703 template <typename F>
0704 class ActionInterface {
0705 public:
0706 typedef typename internal::Function<F>::Result Result;
0707 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
0708
0709 ActionInterface() = default;
0710 virtual ~ActionInterface() = default;
0711
0712
0713
0714
0715
0716 virtual Result Perform(const ArgumentTuple& args) = 0;
0717
0718 private:
0719 ActionInterface(const ActionInterface&) = delete;
0720 ActionInterface& operator=(const ActionInterface&) = delete;
0721 };
0722
0723 template <typename F>
0724 class Action;
0725
0726
0727
0728
0729
0730
0731
0732 template <typename R, typename... Args>
0733 class Action<R(Args...)> {
0734 private:
0735 using F = R(Args...);
0736
0737
0738
0739 struct ActionAdapter {
0740
0741 ::std::shared_ptr<ActionInterface<F>> impl_;
0742
0743 template <typename... InArgs>
0744 typename internal::Function<F>::Result operator()(InArgs&&... args) {
0745 return impl_->Perform(
0746 ::std::forward_as_tuple(::std::forward<InArgs>(args)...));
0747 }
0748 };
0749
0750 template <typename G>
0751 using IsCompatibleFunctor = std::is_constructible<std::function<F>, G>;
0752
0753 public:
0754 typedef typename internal::Function<F>::Result Result;
0755 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
0756
0757
0758
0759 Action() = default;
0760
0761
0762
0763
0764 template <
0765 typename G,
0766 typename = typename std::enable_if<internal::disjunction<
0767 IsCompatibleFunctor<G>, std::is_constructible<std::function<Result()>,
0768 G>>::value>::type>
0769 Action(G&& fun) {
0770 Init(::std::forward<G>(fun), IsCompatibleFunctor<G>());
0771 }
0772
0773
0774 explicit Action(ActionInterface<F>* impl)
0775 : fun_(ActionAdapter{::std::shared_ptr<ActionInterface<F>>(impl)}) {}
0776
0777
0778
0779
0780 template <typename Func>
0781 Action(const Action<Func>& action)
0782 : fun_(action.fun_) {}
0783
0784
0785 bool IsDoDefault() const { return fun_ == nullptr; }
0786
0787
0788
0789
0790
0791
0792
0793 Result Perform(ArgumentTuple args) const {
0794 if (IsDoDefault()) {
0795 internal::IllegalDoDefault(__FILE__, __LINE__);
0796 }
0797 return internal::Apply(fun_, ::std::move(args));
0798 }
0799
0800
0801
0802 operator OnceAction<F>() const {
0803
0804
0805
0806 struct OA {
0807 Action<F> action;
0808
0809 R operator()(Args... args) && {
0810 return action.Perform(
0811 std::forward_as_tuple(std::forward<Args>(args)...));
0812 }
0813 };
0814
0815 return OA{*this};
0816 }
0817
0818 private:
0819 template <typename G>
0820 friend class Action;
0821
0822 template <typename G>
0823 void Init(G&& g, ::std::true_type) {
0824 fun_ = ::std::forward<G>(g);
0825 }
0826
0827 template <typename G>
0828 void Init(G&& g, ::std::false_type) {
0829 fun_ = IgnoreArgs<typename ::std::decay<G>::type>{::std::forward<G>(g)};
0830 }
0831
0832 template <typename FunctionImpl>
0833 struct IgnoreArgs {
0834 template <typename... InArgs>
0835 Result operator()(const InArgs&...) const {
0836 return function_impl();
0837 }
0838 template <typename... InArgs>
0839 Result operator()(const InArgs&...) {
0840 return function_impl();
0841 }
0842
0843 FunctionImpl function_impl;
0844 };
0845
0846
0847 ::std::function<F> fun_;
0848 };
0849
0850
0851
0852
0853
0854
0855
0856
0857
0858
0859
0860
0861
0862
0863
0864
0865
0866
0867
0868
0869
0870
0871 template <typename Impl>
0872 class PolymorphicAction {
0873 public:
0874 explicit PolymorphicAction(const Impl& impl) : impl_(impl) {}
0875
0876 template <typename F>
0877 operator Action<F>() const {
0878 return Action<F>(new MonomorphicImpl<F>(impl_));
0879 }
0880
0881 private:
0882 template <typename F>
0883 class MonomorphicImpl : public ActionInterface<F> {
0884 public:
0885 typedef typename internal::Function<F>::Result Result;
0886 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
0887
0888 explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
0889
0890 Result Perform(const ArgumentTuple& args) override {
0891 return impl_.template Perform<Result>(args);
0892 }
0893
0894 private:
0895 Impl impl_;
0896 };
0897
0898 Impl impl_;
0899 };
0900
0901
0902
0903 template <typename F>
0904 Action<F> MakeAction(ActionInterface<F>* impl) {
0905 return Action<F>(impl);
0906 }
0907
0908
0909
0910
0911
0912
0913
0914
0915 template <typename Impl>
0916 inline PolymorphicAction<Impl> MakePolymorphicAction(const Impl& impl) {
0917 return PolymorphicAction<Impl>(impl);
0918 }
0919
0920 namespace internal {
0921
0922
0923
0924 template <typename T>
0925 struct ByMoveWrapper {
0926 explicit ByMoveWrapper(T value) : payload(std::move(value)) {}
0927 T payload;
0928 };
0929
0930
0931 template <typename R>
0932 class ReturnAction final {
0933 public:
0934 explicit ReturnAction(R value) : value_(std::move(value)) {}
0935
0936 template <typename U, typename... Args,
0937 typename = typename std::enable_if<conjunction<
0938
0939 negation<std::is_same<void, U>>,
0940 negation<std::is_reference<U>>,
0941 std::is_convertible<R, U>,
0942 std::is_move_constructible<U>>::value>::type>
0943 operator OnceAction<U(Args...)>() && {
0944 return Impl<U>(std::move(value_));
0945 }
0946
0947 template <typename U, typename... Args,
0948 typename = typename std::enable_if<conjunction<
0949
0950 negation<std::is_same<void, U>>,
0951 negation<std::is_reference<U>>,
0952 std::is_convertible<const R&, U>,
0953 std::is_copy_constructible<U>>::value>::type>
0954 operator Action<U(Args...)>() const {
0955 return Impl<U>(value_);
0956 }
0957
0958 private:
0959
0960 template <typename U>
0961 class Impl final {
0962 public:
0963
0964
0965 explicit Impl(R&& input_value)
0966 : state_(new State(std::move(input_value))) {}
0967
0968
0969
0970 explicit Impl(const R& input_value) : state_(new State(input_value)) {}
0971
0972 U operator()() && { return std::move(state_->value); }
0973 U operator()() const& { return state_->value; }
0974
0975 private:
0976
0977
0978
0979
0980
0981
0982
0983
0984
0985
0986
0987 struct State {
0988 explicit State(const R& input_value_in)
0989 : input_value(input_value_in),
0990
0991
0992
0993
0994
0995
0996
0997
0998 value(ImplicitCast_<U>(internal::as_const(input_value))) {}
0999
1000
1001
1002 explicit State(R&& input_value_in)
1003 : input_value(std::move(input_value_in)),
1004
1005
1006
1007
1008
1009
1010 value(ImplicitCast_<U>(std::move(input_value))) {}
1011
1012
1013
1014
1015
1016 R input_value;
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083 U value;
1084 };
1085
1086 const std::shared_ptr<State> state_;
1087 };
1088
1089 R value_;
1090 };
1091
1092
1093
1094
1095
1096
1097 template <typename T>
1098 class ReturnAction<ByMoveWrapper<T>> final {
1099 public:
1100 explicit ReturnAction(ByMoveWrapper<T> wrapper)
1101 : state_(new State(std::move(wrapper.payload))) {}
1102
1103 T operator()() const {
1104 GTEST_CHECK_(!state_->called)
1105 << "A ByMove() action must be performed at most once.";
1106
1107 state_->called = true;
1108 return std::move(state_->value);
1109 }
1110
1111 private:
1112
1113
1114 struct State {
1115 explicit State(T&& value_in) : value(std::move(value_in)) {}
1116
1117 T value;
1118 bool called = false;
1119 };
1120
1121 const std::shared_ptr<State> state_;
1122 };
1123
1124
1125 class ReturnNullAction {
1126 public:
1127
1128
1129
1130 template <typename Result, typename ArgumentTuple>
1131 static Result Perform(const ArgumentTuple&) {
1132 return nullptr;
1133 }
1134 };
1135
1136
1137 class ReturnVoidAction {
1138 public:
1139
1140 template <typename Result, typename ArgumentTuple>
1141 static void Perform(const ArgumentTuple&) {
1142 static_assert(std::is_void<Result>::value, "Result should be void.");
1143 }
1144 };
1145
1146
1147
1148
1149 template <typename T>
1150 class ReturnRefAction {
1151 public:
1152
1153 explicit ReturnRefAction(T& ref) : ref_(ref) {}
1154
1155
1156
1157 template <typename F>
1158 operator Action<F>() const {
1159 typedef typename Function<F>::Result Result;
1160
1161
1162
1163 static_assert(std::is_reference<Result>::value,
1164 "use Return instead of ReturnRef to return a value");
1165 return Action<F>(new Impl<F>(ref_));
1166 }
1167
1168 private:
1169
1170 template <typename F>
1171 class Impl : public ActionInterface<F> {
1172 public:
1173 typedef typename Function<F>::Result Result;
1174 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
1175
1176 explicit Impl(T& ref) : ref_(ref) {}
1177
1178 Result Perform(const ArgumentTuple&) override { return ref_; }
1179
1180 private:
1181 T& ref_;
1182 };
1183
1184 T& ref_;
1185 };
1186
1187
1188
1189
1190 template <typename T>
1191 class ReturnRefOfCopyAction {
1192 public:
1193
1194
1195 explicit ReturnRefOfCopyAction(const T& value) : value_(value) {}
1196
1197
1198
1199 template <typename F>
1200 operator Action<F>() const {
1201 typedef typename Function<F>::Result Result;
1202
1203
1204
1205 static_assert(std::is_reference<Result>::value,
1206 "use Return instead of ReturnRefOfCopy to return a value");
1207 return Action<F>(new Impl<F>(value_));
1208 }
1209
1210 private:
1211
1212 template <typename F>
1213 class Impl : public ActionInterface<F> {
1214 public:
1215 typedef typename Function<F>::Result Result;
1216 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
1217
1218 explicit Impl(const T& value) : value_(value) {}
1219
1220 Result Perform(const ArgumentTuple&) override { return value_; }
1221
1222 private:
1223 T value_;
1224 };
1225
1226 const T value_;
1227 };
1228
1229
1230
1231 template <typename T>
1232 class ReturnRoundRobinAction {
1233 public:
1234 explicit ReturnRoundRobinAction(std::vector<T> values) {
1235 GTEST_CHECK_(!values.empty())
1236 << "ReturnRoundRobin requires at least one element.";
1237 state_->values = std::move(values);
1238 }
1239
1240 template <typename... Args>
1241 T operator()(Args&&...) const {
1242 return state_->Next();
1243 }
1244
1245 private:
1246 struct State {
1247 T Next() {
1248 T ret_val = values[i++];
1249 if (i == values.size()) i = 0;
1250 return ret_val;
1251 }
1252
1253 std::vector<T> values;
1254 size_t i = 0;
1255 };
1256 std::shared_ptr<State> state_ = std::make_shared<State>();
1257 };
1258
1259
1260 class DoDefaultAction {
1261 public:
1262
1263
1264 template <typename F>
1265 operator Action<F>() const {
1266 return Action<F>();
1267 }
1268 };
1269
1270
1271
1272 template <typename T1, typename T2>
1273 class AssignAction {
1274 public:
1275 AssignAction(T1* ptr, T2 value) : ptr_(ptr), value_(value) {}
1276
1277 template <typename Result, typename ArgumentTuple>
1278 void Perform(const ArgumentTuple& ) const {
1279 *ptr_ = value_;
1280 }
1281
1282 private:
1283 T1* const ptr_;
1284 const T2 value_;
1285 };
1286
1287 #ifndef GTEST_OS_WINDOWS_MOBILE
1288
1289
1290
1291 template <typename T>
1292 class SetErrnoAndReturnAction {
1293 public:
1294 SetErrnoAndReturnAction(int errno_value, T result)
1295 : errno_(errno_value), result_(result) {}
1296 template <typename Result, typename ArgumentTuple>
1297 Result Perform(const ArgumentTuple& ) const {
1298 errno = errno_;
1299 return result_;
1300 }
1301
1302 private:
1303 const int errno_;
1304 const T result_;
1305 };
1306
1307 #endif
1308
1309
1310
1311 template <size_t N, typename A, typename = void>
1312 struct SetArgumentPointeeAction {
1313 A value;
1314
1315 template <typename... Args>
1316 void operator()(const Args&... args) const {
1317 *::std::get<N>(std::tie(args...)) = value;
1318 }
1319 };
1320
1321
1322 template <class Class, typename MethodPtr>
1323 struct InvokeMethodAction {
1324 Class* const obj_ptr;
1325 const MethodPtr method_ptr;
1326
1327 template <typename... Args>
1328 auto operator()(Args&&... args) const
1329 -> decltype((obj_ptr->*method_ptr)(std::forward<Args>(args)...)) {
1330 return (obj_ptr->*method_ptr)(std::forward<Args>(args)...);
1331 }
1332 };
1333
1334
1335
1336
1337
1338 template <typename FunctionImpl>
1339 struct InvokeWithoutArgsAction {
1340 FunctionImpl function_impl;
1341
1342
1343
1344 template <typename... Args>
1345 auto operator()(const Args&...) -> decltype(function_impl()) {
1346 return function_impl();
1347 }
1348 };
1349
1350
1351 template <class Class, typename MethodPtr>
1352 struct InvokeMethodWithoutArgsAction {
1353 Class* const obj_ptr;
1354 const MethodPtr method_ptr;
1355
1356 using ReturnType =
1357 decltype((std::declval<Class*>()->*std::declval<MethodPtr>())());
1358
1359 template <typename... Args>
1360 ReturnType operator()(const Args&...) const {
1361 return (obj_ptr->*method_ptr)();
1362 }
1363 };
1364
1365
1366 template <typename A>
1367 class IgnoreResultAction {
1368 public:
1369 explicit IgnoreResultAction(const A& action) : action_(action) {}
1370
1371 template <typename F>
1372 operator Action<F>() const {
1373
1374
1375
1376
1377
1378
1379
1380
1381 typedef typename internal::Function<F>::Result Result;
1382
1383
1384 static_assert(std::is_void<Result>::value, "Result type should be void.");
1385
1386 return Action<F>(new Impl<F>(action_));
1387 }
1388
1389 private:
1390 template <typename F>
1391 class Impl : public ActionInterface<F> {
1392 public:
1393 typedef typename internal::Function<F>::Result Result;
1394 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
1395
1396 explicit Impl(const A& action) : action_(action) {}
1397
1398 void Perform(const ArgumentTuple& args) override {
1399
1400 action_.Perform(args);
1401 }
1402
1403 private:
1404
1405
1406 typedef
1407 typename internal::Function<F>::MakeResultIgnoredValue OriginalFunction;
1408
1409 const Action<OriginalFunction> action_;
1410 };
1411
1412 const A action_;
1413 };
1414
1415 template <typename InnerAction, size_t... I>
1416 struct WithArgsAction {
1417 InnerAction inner_action;
1418
1419
1420
1421 template <typename R, typename... Args>
1422 using InnerSignature =
1423 R(typename std::tuple_element<I, std::tuple<Args...>>::type...);
1424
1425
1426
1427
1428
1429
1430
1431 template <
1432 typename R, typename... Args,
1433 typename std::enable_if<
1434 std::is_convertible<InnerAction,
1435
1436
1437
1438
1439
1440
1441 OnceAction<R(internal::TupleElement<
1442 I, std::tuple<Args...>>...)>>::value,
1443 int>::type = 0>
1444 operator OnceAction<R(Args...)>() && {
1445 struct OA {
1446 OnceAction<InnerSignature<R, Args...>> inner_action;
1447
1448 R operator()(Args&&... args) && {
1449 return std::move(inner_action)
1450 .Call(std::get<I>(
1451 std::forward_as_tuple(std::forward<Args>(args)...))...);
1452 }
1453 };
1454
1455 return OA{std::move(inner_action)};
1456 }
1457
1458
1459
1460
1461 template <
1462 typename R, typename... Args,
1463 typename std::enable_if<
1464 std::is_convertible<const InnerAction&,
1465 OnceAction<R(internal::TupleElement<
1466 I, std::tuple<Args...>>...)>>::value,
1467 int>::type = 0>
1468 operator OnceAction<R(Args...)>() const& {
1469 struct OA {
1470 OnceAction<InnerSignature<R, Args...>> inner_action;
1471
1472 R operator()(Args&&... args) && {
1473 return std::move(inner_action)
1474 .Call(std::get<I>(
1475 std::forward_as_tuple(std::forward<Args>(args)...))...);
1476 }
1477 };
1478
1479 return OA{inner_action};
1480 }
1481
1482 template <
1483 typename R, typename... Args,
1484 typename std::enable_if<
1485 std::is_convertible<const InnerAction&,
1486
1487
1488
1489
1490
1491
1492 Action<R(internal::TupleElement<
1493 I, std::tuple<Args...>>...)>>::value,
1494 int>::type = 0>
1495 operator Action<R(Args...)>() const {
1496 Action<InnerSignature<R, Args...>> converted(inner_action);
1497
1498 return [converted](Args&&... args) -> R {
1499 return converted.Perform(std::forward_as_tuple(
1500 std::get<I>(std::forward_as_tuple(std::forward<Args>(args)...))...));
1501 };
1502 }
1503 };
1504
1505 template <typename... Actions>
1506 class DoAllAction;
1507
1508
1509 template <typename FinalAction>
1510 class DoAllAction<FinalAction> {
1511 public:
1512 struct UserConstructorTag {};
1513
1514 template <typename T>
1515 explicit DoAllAction(UserConstructorTag, T&& action)
1516 : final_action_(std::forward<T>(action)) {}
1517
1518
1519
1520
1521
1522
1523
1524
1525 template <typename R, typename... Args,
1526 typename std::enable_if<
1527 std::is_convertible<FinalAction, OnceAction<R(Args...)>>::value,
1528 int>::type = 0>
1529 operator OnceAction<R(Args...)>() && {
1530 return std::move(final_action_);
1531 }
1532
1533
1534
1535 template <
1536 typename R, typename... Args,
1537 typename std::enable_if<
1538 conjunction<
1539 negation<
1540 std::is_convertible<FinalAction, OnceAction<R(Args...)>>>,
1541 std::is_convertible<FinalAction, Action<R(Args...)>>>::value,
1542 int>::type = 0>
1543 operator OnceAction<R(Args...)>() && {
1544 return Action<R(Args...)>(std::move(final_action_));
1545 }
1546
1547
1548 template <
1549 typename R, typename... Args,
1550 typename std::enable_if<
1551 std::is_convertible<const FinalAction&, Action<R(Args...)>>::value,
1552 int>::type = 0>
1553 operator Action<R(Args...)>() const {
1554 return final_action_;
1555 }
1556
1557 private:
1558 FinalAction final_action_;
1559 };
1560
1561
1562
1563 template <typename InitialAction, typename... OtherActions>
1564 class DoAllAction<InitialAction, OtherActions...>
1565 : private DoAllAction<OtherActions...> {
1566 private:
1567 using Base = DoAllAction<OtherActions...>;
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614 template <typename T>
1615 using InitialActionArgType =
1616 typename std::conditional<std::is_scalar<T>::value, T, const T&>::type;
1617
1618 public:
1619 struct UserConstructorTag {};
1620
1621 template <typename T, typename... U>
1622 explicit DoAllAction(UserConstructorTag, T&& initial_action,
1623 U&&... other_actions)
1624 : Base({}, std::forward<U>(other_actions)...),
1625 initial_action_(std::forward<T>(initial_action)) {}
1626
1627
1628
1629 template <
1630 typename R, typename... Args,
1631 typename std::enable_if<
1632 conjunction<std::is_convertible<
1633 InitialAction,
1634 OnceAction<void(InitialActionArgType<Args>...)>>,
1635 std::is_convertible<Base, OnceAction<R(Args...)>>>::value,
1636 int>::type = 0>
1637 operator OnceAction<R(Args...)>() && {
1638
1639
1640
1641 struct OA {
1642 OnceAction<void(InitialActionArgType<Args>...)> initial_action;
1643 OnceAction<R(Args...)> remaining_actions;
1644
1645 R operator()(Args... args) && {
1646 std::move(initial_action)
1647 .Call(static_cast<InitialActionArgType<Args>>(args)...);
1648
1649 return std::move(remaining_actions).Call(std::forward<Args>(args)...);
1650 }
1651 };
1652
1653 return OA{
1654 std::move(initial_action_),
1655 std::move(static_cast<Base&>(*this)),
1656 };
1657 }
1658
1659
1660
1661
1662
1663
1664 template <
1665 typename R, typename... Args,
1666 typename std::enable_if<
1667 conjunction<
1668 negation<std::is_convertible<
1669 InitialAction,
1670 OnceAction<void(InitialActionArgType<Args>...)>>>,
1671 std::is_convertible<InitialAction,
1672 Action<void(InitialActionArgType<Args>...)>>,
1673 std::is_convertible<Base, OnceAction<R(Args...)>>>::value,
1674 int>::type = 0>
1675 operator OnceAction<R(Args...)>() && {
1676 return DoAll(
1677 Action<void(InitialActionArgType<Args>...)>(std::move(initial_action_)),
1678 std::move(static_cast<Base&>(*this)));
1679 }
1680
1681
1682
1683 template <
1684 typename R, typename... Args,
1685 typename std::enable_if<
1686 conjunction<
1687 std::is_convertible<const InitialAction&,
1688 Action<void(InitialActionArgType<Args>...)>>,
1689 std::is_convertible<const Base&, Action<R(Args...)>>>::value,
1690 int>::type = 0>
1691 operator Action<R(Args...)>() const {
1692
1693
1694
1695 struct OA {
1696 Action<void(InitialActionArgType<Args>...)> initial_action;
1697 Action<R(Args...)> remaining_actions;
1698
1699 R operator()(Args... args) const {
1700 initial_action.Perform(std::forward_as_tuple(
1701 static_cast<InitialActionArgType<Args>>(args)...));
1702
1703 return remaining_actions.Perform(
1704 std::forward_as_tuple(std::forward<Args>(args)...));
1705 }
1706 };
1707
1708 return OA{
1709 initial_action_,
1710 static_cast<const Base&>(*this),
1711 };
1712 }
1713
1714 private:
1715 InitialAction initial_action_;
1716 };
1717
1718 template <typename T, typename... Params>
1719 struct ReturnNewAction {
1720 T* operator()() const {
1721 return internal::Apply(
1722 [](const Params&... unpacked_params) {
1723 return new T(unpacked_params...);
1724 },
1725 params);
1726 }
1727 std::tuple<Params...> params;
1728 };
1729
1730 template <size_t k>
1731 struct ReturnArgAction {
1732 template <typename... Args,
1733 typename = typename std::enable_if<(k < sizeof...(Args))>::type>
1734 auto operator()(Args&&... args) const -> decltype(std::get<k>(
1735 std::forward_as_tuple(std::forward<Args>(args)...))) {
1736 return std::get<k>(std::forward_as_tuple(std::forward<Args>(args)...));
1737 }
1738 };
1739
1740 template <size_t k, typename Ptr>
1741 struct SaveArgAction {
1742 Ptr pointer;
1743
1744 template <typename... Args>
1745 void operator()(const Args&... args) const {
1746 *pointer = std::get<k>(std::tie(args...));
1747 }
1748 };
1749
1750 template <size_t k, typename Ptr>
1751 struct SaveArgByMoveAction {
1752 Ptr pointer;
1753
1754 template <typename... Args>
1755 void operator()(Args&&... args) const {
1756 *pointer = std::move(std::get<k>(std::tie(args...)));
1757 }
1758 };
1759
1760 template <size_t k, typename Ptr>
1761 struct SaveArgPointeeAction {
1762 Ptr pointer;
1763
1764 template <typename... Args>
1765 void operator()(const Args&... args) const {
1766 *pointer = *std::get<k>(std::tie(args...));
1767 }
1768 };
1769
1770 template <size_t k, typename T>
1771 struct SetArgRefereeAction {
1772 T value;
1773
1774 template <typename... Args>
1775 void operator()(Args&&... args) const {
1776 using argk_type =
1777 typename ::std::tuple_element<k, std::tuple<Args...>>::type;
1778 static_assert(std::is_lvalue_reference<argk_type>::value,
1779 "Argument must be a reference type.");
1780 std::get<k>(std::tie(args...)) = value;
1781 }
1782 };
1783
1784 template <size_t k, typename I1, typename I2>
1785 struct SetArrayArgumentAction {
1786 I1 first;
1787 I2 last;
1788
1789 template <typename... Args>
1790 void operator()(const Args&... args) const {
1791 auto value = std::get<k>(std::tie(args...));
1792 for (auto it = first; it != last; ++it, (void)++value) {
1793 *value = *it;
1794 }
1795 }
1796 };
1797
1798 template <size_t k>
1799 struct DeleteArgAction {
1800 template <typename... Args>
1801 void operator()(const Args&... args) const {
1802 delete std::get<k>(std::tie(args...));
1803 }
1804 };
1805
1806 template <typename Ptr>
1807 struct ReturnPointeeAction {
1808 Ptr pointer;
1809 template <typename... Args>
1810 auto operator()(const Args&...) const -> decltype(*pointer) {
1811 return *pointer;
1812 }
1813 };
1814
1815 #if GTEST_HAS_EXCEPTIONS
1816 template <typename T>
1817 struct ThrowAction {
1818 T exception;
1819
1820 template <typename R, typename... Args>
1821 operator Action<R(Args...)>() const {
1822 T copy = exception;
1823 return [copy](Args...) -> R { throw copy; };
1824 }
1825 };
1826 struct RethrowAction {
1827 std::exception_ptr exception;
1828 template <typename R, typename... Args>
1829 operator Action<R(Args...)>() const {
1830 return [ex = exception](Args...) -> R { std::rethrow_exception(ex); };
1831 }
1832 };
1833 #endif
1834
1835 }
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867 typedef internal::IgnoredValue Unused;
1868
1869
1870
1871
1872 template <typename... Action>
1873 internal::DoAllAction<typename std::decay<Action>::type...> DoAll(
1874 Action&&... action) {
1875 return internal::DoAllAction<typename std::decay<Action>::type...>(
1876 {}, std::forward<Action>(action)...);
1877 }
1878
1879
1880
1881
1882
1883
1884 template <size_t k, typename InnerAction>
1885 internal::WithArgsAction<typename std::decay<InnerAction>::type, k> WithArg(
1886 InnerAction&& action) {
1887 return {std::forward<InnerAction>(action)};
1888 }
1889
1890
1891
1892
1893
1894 template <size_t k, size_t... ks, typename InnerAction>
1895 internal::WithArgsAction<typename std::decay<InnerAction>::type, k, ks...>
1896 WithArgs(InnerAction&& action) {
1897 return {std::forward<InnerAction>(action)};
1898 }
1899
1900
1901
1902
1903
1904 template <typename InnerAction>
1905 internal::WithArgsAction<typename std::decay<InnerAction>::type> WithoutArgs(
1906 InnerAction&& action) {
1907 return {std::forward<InnerAction>(action)};
1908 }
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934 template <typename R>
1935 internal::ReturnAction<R> Return(R value) {
1936 return internal::ReturnAction<R>(std::move(value));
1937 }
1938
1939
1940 inline PolymorphicAction<internal::ReturnNullAction> ReturnNull() {
1941 return MakePolymorphicAction(internal::ReturnNullAction());
1942 }
1943
1944
1945 inline PolymorphicAction<internal::ReturnVoidAction> Return() {
1946 return MakePolymorphicAction(internal::ReturnVoidAction());
1947 }
1948
1949
1950 template <typename R>
1951 inline internal::ReturnRefAction<R> ReturnRef(R& x) {
1952 return internal::ReturnRefAction<R>(x);
1953 }
1954
1955
1956 template <typename R, R* = nullptr>
1957 internal::ReturnRefAction<R> ReturnRef(R&&) = delete;
1958
1959
1960
1961
1962 template <typename R>
1963 inline internal::ReturnRefOfCopyAction<R> ReturnRefOfCopy(const R& x) {
1964 return internal::ReturnRefOfCopyAction<R>(x);
1965 }
1966
1967
1968
1969
1970
1971
1972
1973 template <typename R>
1974 internal::ByMoveWrapper<R> ByMove(R x) {
1975 return internal::ByMoveWrapper<R>(std::move(x));
1976 }
1977
1978
1979
1980
1981 template <typename T>
1982 internal::ReturnRoundRobinAction<T> ReturnRoundRobin(std::vector<T> vals) {
1983 return internal::ReturnRoundRobinAction<T>(std::move(vals));
1984 }
1985
1986
1987
1988
1989 template <typename T>
1990 internal::ReturnRoundRobinAction<T> ReturnRoundRobin(
1991 std::initializer_list<T> vals) {
1992 return internal::ReturnRoundRobinAction<T>(std::vector<T>(vals));
1993 }
1994
1995
1996 inline internal::DoDefaultAction DoDefault() {
1997 return internal::DoDefaultAction();
1998 }
1999
2000
2001
2002 template <size_t N, typename T>
2003 internal::SetArgumentPointeeAction<N, T> SetArgPointee(T value) {
2004 return {std::move(value)};
2005 }
2006
2007
2008 template <size_t N, typename T>
2009 internal::SetArgumentPointeeAction<N, T> SetArgumentPointee(T value) {
2010 return {std::move(value)};
2011 }
2012
2013
2014 template <typename T1, typename T2>
2015 PolymorphicAction<internal::AssignAction<T1, T2>> Assign(T1* ptr, T2 val) {
2016 return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val));
2017 }
2018
2019 #ifndef GTEST_OS_WINDOWS_MOBILE
2020
2021
2022 template <typename T>
2023 PolymorphicAction<internal::SetErrnoAndReturnAction<T>> SetErrnoAndReturn(
2024 int errval, T result) {
2025 return MakePolymorphicAction(
2026 internal::SetErrnoAndReturnAction<T>(errval, result));
2027 }
2028
2029 #endif
2030
2031
2032
2033
2034
2035
2036
2037 template <typename FunctionImpl>
2038 typename std::decay<FunctionImpl>::type Invoke(FunctionImpl&& function_impl) {
2039 return std::forward<FunctionImpl>(function_impl);
2040 }
2041
2042
2043
2044 template <class Class, typename MethodPtr>
2045 internal::InvokeMethodAction<Class, MethodPtr> Invoke(Class* obj_ptr,
2046 MethodPtr method_ptr) {
2047 return {obj_ptr, method_ptr};
2048 }
2049
2050
2051 template <typename FunctionImpl>
2052 internal::InvokeWithoutArgsAction<typename std::decay<FunctionImpl>::type>
2053 InvokeWithoutArgs(FunctionImpl function_impl) {
2054 return {std::move(function_impl)};
2055 }
2056
2057
2058
2059 template <class Class, typename MethodPtr>
2060 internal::InvokeMethodWithoutArgsAction<Class, MethodPtr> InvokeWithoutArgs(
2061 Class* obj_ptr, MethodPtr method_ptr) {
2062 return {obj_ptr, method_ptr};
2063 }
2064
2065
2066
2067
2068 template <typename A>
2069 inline internal::IgnoreResultAction<A> IgnoreResult(const A& an_action) {
2070 return internal::IgnoreResultAction<A>(an_action);
2071 }
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083 template <typename T>
2084 inline ::std::reference_wrapper<T> ByRef(T& l_value) {
2085 return ::std::reference_wrapper<T>(l_value);
2086 }
2087
2088
2089
2090
2091 template <typename T, typename... Params>
2092 internal::ReturnNewAction<T, typename std::decay<Params>::type...> ReturnNew(
2093 Params&&... params) {
2094 return {std::forward_as_tuple(std::forward<Params>(params)...)};
2095 }
2096
2097
2098 template <size_t k>
2099 internal::ReturnArgAction<k> ReturnArg() {
2100 return {};
2101 }
2102
2103
2104
2105 template <size_t k, typename Ptr>
2106 internal::SaveArgAction<k, Ptr> SaveArg(Ptr pointer) {
2107 return {pointer};
2108 }
2109
2110
2111
2112 template <size_t k, typename Ptr>
2113 internal::SaveArgByMoveAction<k, Ptr> SaveArgByMove(Ptr pointer) {
2114 return {pointer};
2115 }
2116
2117
2118
2119 template <size_t k, typename Ptr>
2120 internal::SaveArgPointeeAction<k, Ptr> SaveArgPointee(Ptr pointer) {
2121 return {pointer};
2122 }
2123
2124
2125
2126 template <size_t k, typename T>
2127 internal::SetArgRefereeAction<k, typename std::decay<T>::type> SetArgReferee(
2128 T&& value) {
2129 return {std::forward<T>(value)};
2130 }
2131
2132
2133
2134
2135
2136
2137 template <size_t k, typename I1, typename I2>
2138 internal::SetArrayArgumentAction<k, I1, I2> SetArrayArgument(I1 first,
2139 I2 last) {
2140 return {first, last};
2141 }
2142
2143
2144
2145 template <size_t k>
2146 internal::DeleteArgAction<k> DeleteArg() {
2147 return {};
2148 }
2149
2150
2151 template <typename Ptr>
2152 internal::ReturnPointeeAction<Ptr> ReturnPointee(Ptr pointer) {
2153 return {pointer};
2154 }
2155
2156 #if GTEST_HAS_EXCEPTIONS
2157
2158
2159
2160
2161 template <typename T>
2162 typename std::enable_if<
2163 !std::is_base_of<std::exception_ptr, typename std::decay<T>::type>::value,
2164 internal::ThrowAction<typename std::decay<T>::type>>::type
2165 Throw(T&& exception) {
2166 return {std::forward<T>(exception)};
2167 }
2168
2169
2170 inline internal::RethrowAction Rethrow(std::exception_ptr exception) {
2171 return {std::move(exception)};
2172 }
2173 #endif
2174
2175 namespace internal {
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191 struct ExcessiveArg {};
2192
2193
2194
2195 template <typename F, typename Impl>
2196 struct ActionImpl;
2197
2198 template <typename Impl>
2199 struct ImplBase {
2200 struct Holder {
2201
2202 explicit operator const Impl&() const { return *ptr; }
2203 std::shared_ptr<Impl> ptr;
2204 };
2205 using type = typename std::conditional<std::is_constructible<Impl>::value,
2206 Impl, Holder>::type;
2207 };
2208
2209 template <typename R, typename... Args, typename Impl>
2210 struct ActionImpl<R(Args...), Impl> : ImplBase<Impl>::type {
2211 using Base = typename ImplBase<Impl>::type;
2212 using function_type = R(Args...);
2213 using args_type = std::tuple<Args...>;
2214
2215 ActionImpl() = default;
2216 explicit ActionImpl(std::shared_ptr<Impl> impl) : Base{std::move(impl)} {}
2217
2218 R operator()(Args&&... arg) const {
2219 static constexpr size_t kMaxArgs =
2220 sizeof...(Args) <= 10 ? sizeof...(Args) : 10;
2221 return Apply(std::make_index_sequence<kMaxArgs>{},
2222 std::make_index_sequence<10 - kMaxArgs>{},
2223 args_type{std::forward<Args>(arg)...});
2224 }
2225
2226 template <std::size_t... arg_id, std::size_t... excess_id>
2227 R Apply(std::index_sequence<arg_id...>, std::index_sequence<excess_id...>,
2228 const args_type& args) const {
2229
2230
2231
2232
2233
2234 static constexpr ExcessiveArg kExcessArg{};
2235 return static_cast<const Impl&>(*this)
2236 .template gmock_PerformImpl<
2237 function_type, R,
2238 args_type,
2239
2240 typename std::tuple_element<arg_id, args_type>::type...>(
2241 args, std::get<arg_id>(args)...,
2242 ((void)excess_id, kExcessArg)...);
2243 }
2244 };
2245
2246
2247
2248 template <typename F, typename Impl>
2249 ::testing::Action<F> MakeAction() {
2250 return ::testing::Action<F>(ActionImpl<F, Impl>());
2251 }
2252
2253
2254 template <typename F, typename Impl>
2255 ::testing::Action<F> MakeAction(std::shared_ptr<Impl> impl) {
2256 return ::testing::Action<F>(ActionImpl<F, Impl>(std::move(impl)));
2257 }
2258
2259 #define GMOCK_INTERNAL_ARG_UNUSED(i, data, el) \
2260 , [[maybe_unused]] const arg##i##_type& arg##i
2261 #define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_ \
2262 [[maybe_unused]] const args_type& args GMOCK_PP_REPEAT( \
2263 GMOCK_INTERNAL_ARG_UNUSED, , 10)
2264
2265 #define GMOCK_INTERNAL_ARG(i, data, el) , const arg##i##_type& arg##i
2266 #define GMOCK_ACTION_ARG_TYPES_AND_NAMES_ \
2267 const args_type& args GMOCK_PP_REPEAT(GMOCK_INTERNAL_ARG, , 10)
2268
2269 #define GMOCK_INTERNAL_TEMPLATE_ARG(i, data, el) , typename arg##i##_type
2270 #define GMOCK_ACTION_TEMPLATE_ARGS_NAMES_ \
2271 GMOCK_PP_TAIL(GMOCK_PP_REPEAT(GMOCK_INTERNAL_TEMPLATE_ARG, , 10))
2272
2273 #define GMOCK_INTERNAL_TYPENAME_PARAM(i, data, param) , typename param##_type
2274 #define GMOCK_ACTION_TYPENAME_PARAMS_(params) \
2275 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_TYPENAME_PARAM, , params))
2276
2277 #define GMOCK_INTERNAL_TYPE_PARAM(i, data, param) , param##_type
2278 #define GMOCK_ACTION_TYPE_PARAMS_(params) \
2279 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_TYPE_PARAM, , params))
2280
2281 #define GMOCK_INTERNAL_TYPE_GVALUE_PARAM(i, data, param) \
2282 , param##_type gmock_p##i
2283 #define GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params) \
2284 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_TYPE_GVALUE_PARAM, , params))
2285
2286 #define GMOCK_INTERNAL_GVALUE_PARAM(i, data, param) \
2287 , std::forward<param##_type>(gmock_p##i)
2288 #define GMOCK_ACTION_GVALUE_PARAMS_(params) \
2289 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_GVALUE_PARAM, , params))
2290
2291 #define GMOCK_INTERNAL_INIT_PARAM(i, data, param) \
2292 , param(::std::forward<param##_type>(gmock_p##i))
2293 #define GMOCK_ACTION_INIT_PARAMS_(params) \
2294 GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_INIT_PARAM, , params))
2295
2296 #define GMOCK_INTERNAL_FIELD_PARAM(i, data, param) param##_type param;
2297 #define GMOCK_ACTION_FIELD_PARAMS_(params) \
2298 GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_FIELD_PARAM, , params)
2299
2300 #define GMOCK_INTERNAL_ACTION(name, full_name, params) \
2301 template <GMOCK_ACTION_TYPENAME_PARAMS_(params)> \
2302 class full_name { \
2303 public: \
2304 explicit full_name(GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params)) \
2305 : impl_(std::make_shared<gmock_Impl>( \
2306 GMOCK_ACTION_GVALUE_PARAMS_(params))) {} \
2307 full_name(const full_name&) = default; \
2308 full_name(full_name&&) noexcept = default; \
2309 template <typename F> \
2310 operator ::testing::Action<F>() const { \
2311 return ::testing::internal::MakeAction<F>(impl_); \
2312 } \
2313 \
2314 private: \
2315 class gmock_Impl { \
2316 public: \
2317 explicit gmock_Impl(GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params)) \
2318 : GMOCK_ACTION_INIT_PARAMS_(params) {} \
2319 template <typename function_type, typename return_type, \
2320 typename args_type, GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
2321 return_type gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_) const; \
2322 GMOCK_ACTION_FIELD_PARAMS_(params) \
2323 }; \
2324 std::shared_ptr<const gmock_Impl> impl_; \
2325 }; \
2326 template <GMOCK_ACTION_TYPENAME_PARAMS_(params)> \
2327 [[nodiscard]] inline full_name<GMOCK_ACTION_TYPE_PARAMS_(params)> name( \
2328 GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params)); \
2329 template <GMOCK_ACTION_TYPENAME_PARAMS_(params)> \
2330 inline full_name<GMOCK_ACTION_TYPE_PARAMS_(params)> name( \
2331 GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params)) { \
2332 return full_name<GMOCK_ACTION_TYPE_PARAMS_(params)>( \
2333 GMOCK_ACTION_GVALUE_PARAMS_(params)); \
2334 } \
2335 template <GMOCK_ACTION_TYPENAME_PARAMS_(params)> \
2336 template <typename function_type, typename return_type, typename args_type, \
2337 GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
2338 return_type \
2339 full_name<GMOCK_ACTION_TYPE_PARAMS_(params)>::gmock_Impl::gmock_PerformImpl( \
2340 GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
2341
2342 }
2343
2344
2345 #define ACTION(name) \
2346 class name##Action { \
2347 public: \
2348 explicit name##Action() noexcept {} \
2349 name##Action(const name##Action&) noexcept {} \
2350 template <typename F> \
2351 operator ::testing::Action<F>() const { \
2352 return ::testing::internal::MakeAction<F, gmock_Impl>(); \
2353 } \
2354 \
2355 private: \
2356 class gmock_Impl { \
2357 public: \
2358 template <typename function_type, typename return_type, \
2359 typename args_type, GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
2360 return_type gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_) const; \
2361 }; \
2362 }; \
2363 [[nodiscard]] inline name##Action name(); \
2364 inline name##Action name() { return name##Action(); } \
2365 template <typename function_type, typename return_type, typename args_type, \
2366 GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
2367 return_type name##Action::gmock_Impl::gmock_PerformImpl( \
2368 GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
2369
2370 #define ACTION_P(name, ...) \
2371 GMOCK_INTERNAL_ACTION(name, name##ActionP, (__VA_ARGS__))
2372
2373 #define ACTION_P2(name, ...) \
2374 GMOCK_INTERNAL_ACTION(name, name##ActionP2, (__VA_ARGS__))
2375
2376 #define ACTION_P3(name, ...) \
2377 GMOCK_INTERNAL_ACTION(name, name##ActionP3, (__VA_ARGS__))
2378
2379 #define ACTION_P4(name, ...) \
2380 GMOCK_INTERNAL_ACTION(name, name##ActionP4, (__VA_ARGS__))
2381
2382 #define ACTION_P5(name, ...) \
2383 GMOCK_INTERNAL_ACTION(name, name##ActionP5, (__VA_ARGS__))
2384
2385 #define ACTION_P6(name, ...) \
2386 GMOCK_INTERNAL_ACTION(name, name##ActionP6, (__VA_ARGS__))
2387
2388 #define ACTION_P7(name, ...) \
2389 GMOCK_INTERNAL_ACTION(name, name##ActionP7, (__VA_ARGS__))
2390
2391 #define ACTION_P8(name, ...) \
2392 GMOCK_INTERNAL_ACTION(name, name##ActionP8, (__VA_ARGS__))
2393
2394 #define ACTION_P9(name, ...) \
2395 GMOCK_INTERNAL_ACTION(name, name##ActionP9, (__VA_ARGS__))
2396
2397 #define ACTION_P10(name, ...) \
2398 GMOCK_INTERNAL_ACTION(name, name##ActionP10, (__VA_ARGS__))
2399
2400 }
2401
2402 GTEST_DISABLE_MSC_WARNINGS_POP_()
2403
2404 #endif