File indexing completed on 2025-08-07 08:19:49
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 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
0037 #define GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
0038
0039 #ifndef _WIN32_WCE
0040 # include <errno.h>
0041 #endif
0042
0043 #include <algorithm>
0044 #include <string>
0045
0046 #include "gmock/internal/gmock-internal-utils.h"
0047 #include "gmock/internal/gmock-port.h"
0048
0049 #if GTEST_HAS_STD_TYPE_TRAITS_
0050 #include <type_traits>
0051 #endif
0052
0053 namespace testing {
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064 namespace internal {
0065
0066 template <typename F1, typename F2>
0067 class ActionAdaptor;
0068
0069
0070
0071
0072
0073
0074 template <typename T, bool kDefaultConstructible>
0075 struct BuiltInDefaultValueGetter {
0076 static T Get() { return T(); }
0077 };
0078 template <typename T>
0079 struct BuiltInDefaultValueGetter<T, false> {
0080 static T Get() {
0081 Assert(false, __FILE__, __LINE__,
0082 "Default action undefined for the function return type.");
0083 return internal::Invalid<T>();
0084
0085
0086 }
0087 };
0088
0089
0090
0091
0092
0093
0094
0095
0096 template <typename T>
0097 class BuiltInDefaultValue {
0098 public:
0099 #if GTEST_HAS_STD_TYPE_TRAITS_
0100
0101 static bool Exists() {
0102 return ::std::is_default_constructible<T>::value;
0103 }
0104
0105 static T Get() {
0106 return BuiltInDefaultValueGetter<
0107 T, ::std::is_default_constructible<T>::value>::Get();
0108 }
0109
0110 #else
0111
0112 static bool Exists() {
0113 return false;
0114 }
0115
0116 static T Get() {
0117 return BuiltInDefaultValueGetter<T, false>::Get();
0118 }
0119
0120 #endif
0121 };
0122
0123
0124
0125 template <typename T>
0126 class BuiltInDefaultValue<const T> {
0127 public:
0128 static bool Exists() { return BuiltInDefaultValue<T>::Exists(); }
0129 static T Get() { return BuiltInDefaultValue<T>::Get(); }
0130 };
0131
0132
0133
0134 template <typename T>
0135 class BuiltInDefaultValue<T*> {
0136 public:
0137 static bool Exists() { return true; }
0138 static T* Get() { return NULL; }
0139 };
0140
0141
0142
0143 #define GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(type, value) \
0144 template <> \
0145 class BuiltInDefaultValue<type> { \
0146 public: \
0147 static bool Exists() { return true; } \
0148 static type Get() { return value; } \
0149 }
0150
0151 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(void, );
0152 #if GTEST_HAS_GLOBAL_STRING
0153 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::string, "");
0154 #endif
0155 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::std::string, "");
0156 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(bool, false);
0157 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned char, '\0');
0158 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed char, '\0');
0159 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(char, '\0');
0160
0161
0162
0163
0164
0165
0166
0167 #if GMOCK_WCHAR_T_IS_NATIVE_
0168 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(wchar_t, 0U);
0169 #endif
0170
0171 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned short, 0U);
0172 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed short, 0);
0173 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned int, 0U);
0174 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed int, 0);
0175 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned long, 0UL);
0176 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed long, 0L);
0177 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(UInt64, 0);
0178 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(Int64, 0);
0179 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(float, 0);
0180 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(double, 0);
0181
0182 #undef GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_
0183
0184 }
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199 template <typename T>
0200 class DefaultValue {
0201 public:
0202
0203
0204 static void Set(T x) {
0205 delete producer_;
0206 producer_ = new FixedValueProducer(x);
0207 }
0208
0209
0210
0211
0212 typedef T (*FactoryFunction)();
0213 static void SetFactory(FactoryFunction factory) {
0214 delete producer_;
0215 producer_ = new FactoryValueProducer(factory);
0216 }
0217
0218
0219 static void Clear() {
0220 delete producer_;
0221 producer_ = NULL;
0222 }
0223
0224
0225 static bool IsSet() { return producer_ != NULL; }
0226
0227
0228
0229 static bool Exists() {
0230 return IsSet() || internal::BuiltInDefaultValue<T>::Exists();
0231 }
0232
0233
0234
0235
0236 static T Get() {
0237 return producer_ == NULL ?
0238 internal::BuiltInDefaultValue<T>::Get() : producer_->Produce();
0239 }
0240
0241 private:
0242 class ValueProducer {
0243 public:
0244 virtual ~ValueProducer() {}
0245 virtual T Produce() = 0;
0246 };
0247
0248 class FixedValueProducer : public ValueProducer {
0249 public:
0250 explicit FixedValueProducer(T value) : value_(value) {}
0251 virtual T Produce() { return value_; }
0252
0253 private:
0254 const T value_;
0255 GTEST_DISALLOW_COPY_AND_ASSIGN_(FixedValueProducer);
0256 };
0257
0258 class FactoryValueProducer : public ValueProducer {
0259 public:
0260 explicit FactoryValueProducer(FactoryFunction factory)
0261 : factory_(factory) {}
0262 virtual T Produce() { return factory_(); }
0263
0264 private:
0265 const FactoryFunction factory_;
0266 GTEST_DISALLOW_COPY_AND_ASSIGN_(FactoryValueProducer);
0267 };
0268
0269 static ValueProducer* producer_;
0270 };
0271
0272
0273
0274 template <typename T>
0275 class DefaultValue<T&> {
0276 public:
0277
0278 static void Set(T& x) {
0279 address_ = &x;
0280 }
0281
0282
0283 static void Clear() {
0284 address_ = NULL;
0285 }
0286
0287
0288 static bool IsSet() { return address_ != NULL; }
0289
0290
0291
0292 static bool Exists() {
0293 return IsSet() || internal::BuiltInDefaultValue<T&>::Exists();
0294 }
0295
0296
0297
0298
0299 static T& Get() {
0300 return address_ == NULL ?
0301 internal::BuiltInDefaultValue<T&>::Get() : *address_;
0302 }
0303
0304 private:
0305 static T* address_;
0306 };
0307
0308
0309
0310 template <>
0311 class DefaultValue<void> {
0312 public:
0313 static bool Exists() { return true; }
0314 static void Get() {}
0315 };
0316
0317
0318 template <typename T>
0319 typename DefaultValue<T>::ValueProducer* DefaultValue<T>::producer_ = NULL;
0320
0321
0322 template <typename T>
0323 T* DefaultValue<T&>::address_ = NULL;
0324
0325
0326 template <typename F>
0327 class ActionInterface {
0328 public:
0329 typedef typename internal::Function<F>::Result Result;
0330 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
0331
0332 ActionInterface() {}
0333 virtual ~ActionInterface() {}
0334
0335
0336
0337
0338
0339 virtual Result Perform(const ArgumentTuple& args) = 0;
0340
0341 private:
0342 GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionInterface);
0343 };
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354 template <typename F>
0355 class Action {
0356 public:
0357 typedef typename internal::Function<F>::Result Result;
0358 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
0359
0360
0361
0362 Action() : impl_(NULL) {}
0363
0364
0365
0366 explicit Action(ActionInterface<F>* impl) : impl_(impl) {}
0367
0368
0369 Action(const Action& action) : impl_(action.impl_) {}
0370
0371
0372
0373
0374
0375 template <typename Func>
0376 explicit Action(const Action<Func>& action);
0377
0378
0379 bool IsDoDefault() const { return impl_.get() == NULL; }
0380
0381
0382
0383
0384
0385
0386
0387 Result Perform(const ArgumentTuple& args) const {
0388 internal::Assert(
0389 !IsDoDefault(), __FILE__, __LINE__,
0390 "You are using DoDefault() inside a composite action like "
0391 "DoAll() or WithArgs(). This is not supported for technical "
0392 "reasons. Please instead spell out the default action, or "
0393 "assign the default action to an Action variable and use "
0394 "the variable in various places.");
0395 return impl_->Perform(args);
0396 }
0397
0398 private:
0399 template <typename F1, typename F2>
0400 friend class internal::ActionAdaptor;
0401
0402 internal::linked_ptr<ActionInterface<F> > impl_;
0403 };
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426 template <typename Impl>
0427 class PolymorphicAction {
0428 public:
0429 explicit PolymorphicAction(const Impl& impl) : impl_(impl) {}
0430
0431 template <typename F>
0432 operator Action<F>() const {
0433 return Action<F>(new MonomorphicImpl<F>(impl_));
0434 }
0435
0436 private:
0437 template <typename F>
0438 class MonomorphicImpl : public ActionInterface<F> {
0439 public:
0440 typedef typename internal::Function<F>::Result Result;
0441 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
0442
0443 explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
0444
0445 virtual Result Perform(const ArgumentTuple& args) {
0446 return impl_.template Perform<Result>(args);
0447 }
0448
0449 private:
0450 Impl impl_;
0451
0452 GTEST_DISALLOW_ASSIGN_(MonomorphicImpl);
0453 };
0454
0455 Impl impl_;
0456
0457 GTEST_DISALLOW_ASSIGN_(PolymorphicAction);
0458 };
0459
0460
0461
0462 template <typename F>
0463 Action<F> MakeAction(ActionInterface<F>* impl) {
0464 return Action<F>(impl);
0465 }
0466
0467
0468
0469
0470
0471
0472
0473
0474 template <typename Impl>
0475 inline PolymorphicAction<Impl> MakePolymorphicAction(const Impl& impl) {
0476 return PolymorphicAction<Impl>(impl);
0477 }
0478
0479 namespace internal {
0480
0481
0482
0483 template <typename F1, typename F2>
0484 class ActionAdaptor : public ActionInterface<F1> {
0485 public:
0486 typedef typename internal::Function<F1>::Result Result;
0487 typedef typename internal::Function<F1>::ArgumentTuple ArgumentTuple;
0488
0489 explicit ActionAdaptor(const Action<F2>& from) : impl_(from.impl_) {}
0490
0491 virtual Result Perform(const ArgumentTuple& args) {
0492 return impl_->Perform(args);
0493 }
0494
0495 private:
0496 const internal::linked_ptr<ActionInterface<F2> > impl_;
0497
0498 GTEST_DISALLOW_ASSIGN_(ActionAdaptor);
0499 };
0500
0501
0502
0503 template <typename T>
0504 struct ByMoveWrapper {
0505 explicit ByMoveWrapper(T value) : payload(internal::move(value)) {}
0506 T payload;
0507 };
0508
0509
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533 template <typename R>
0534 class ReturnAction {
0535 public:
0536
0537
0538
0539 explicit ReturnAction(R value) : value_(new R(internal::move(value))) {}
0540
0541
0542
0543 template <typename F>
0544 operator Action<F>() const {
0545
0546
0547
0548
0549
0550
0551
0552
0553 typedef typename Function<F>::Result Result;
0554 GTEST_COMPILE_ASSERT_(
0555 !is_reference<Result>::value,
0556 use_ReturnRef_instead_of_Return_to_return_a_reference);
0557 return Action<F>(new Impl<R, F>(value_));
0558 }
0559
0560 private:
0561
0562 template <typename R_, typename F>
0563 class Impl : public ActionInterface<F> {
0564 public:
0565 typedef typename Function<F>::Result Result;
0566 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
0567
0568
0569
0570
0571
0572
0573
0574
0575 explicit Impl(const linked_ptr<R>& value)
0576 : value_before_cast_(*value),
0577 value_(ImplicitCast_<Result>(value_before_cast_)) {}
0578
0579 virtual Result Perform(const ArgumentTuple&) { return value_; }
0580
0581 private:
0582 GTEST_COMPILE_ASSERT_(!is_reference<Result>::value,
0583 Result_cannot_be_a_reference_type);
0584
0585
0586 R value_before_cast_;
0587 Result value_;
0588
0589 GTEST_DISALLOW_COPY_AND_ASSIGN_(Impl);
0590 };
0591
0592
0593
0594 template <typename R_, typename F>
0595 class Impl<ByMoveWrapper<R_>, F> : public ActionInterface<F> {
0596 public:
0597 typedef typename Function<F>::Result Result;
0598 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
0599
0600 explicit Impl(const linked_ptr<R>& wrapper)
0601 : performed_(false), wrapper_(wrapper) {}
0602
0603 virtual Result Perform(const ArgumentTuple&) {
0604 GTEST_CHECK_(!performed_)
0605 << "A ByMove() action should only be performed once.";
0606 performed_ = true;
0607 return internal::move(wrapper_->payload);
0608 }
0609
0610 private:
0611 bool performed_;
0612 const linked_ptr<R> wrapper_;
0613
0614 GTEST_DISALLOW_ASSIGN_(Impl);
0615 };
0616
0617 const linked_ptr<R> value_;
0618
0619 GTEST_DISALLOW_ASSIGN_(ReturnAction);
0620 };
0621
0622
0623 class ReturnNullAction {
0624 public:
0625
0626
0627
0628 template <typename Result, typename ArgumentTuple>
0629 static Result Perform(const ArgumentTuple&) {
0630 #if GTEST_LANG_CXX11
0631 return nullptr;
0632 #else
0633 GTEST_COMPILE_ASSERT_(internal::is_pointer<Result>::value,
0634 ReturnNull_can_be_used_to_return_a_pointer_only);
0635 return NULL;
0636 #endif
0637 }
0638 };
0639
0640
0641 class ReturnVoidAction {
0642 public:
0643
0644 template <typename Result, typename ArgumentTuple>
0645 static void Perform(const ArgumentTuple&) {
0646 CompileAssertTypesEqual<void, Result>();
0647 }
0648 };
0649
0650
0651
0652
0653 template <typename T>
0654 class ReturnRefAction {
0655 public:
0656
0657 explicit ReturnRefAction(T& ref) : ref_(ref) {}
0658
0659
0660
0661 template <typename F>
0662 operator Action<F>() const {
0663 typedef typename Function<F>::Result Result;
0664
0665
0666
0667 GTEST_COMPILE_ASSERT_(internal::is_reference<Result>::value,
0668 use_Return_instead_of_ReturnRef_to_return_a_value);
0669 return Action<F>(new Impl<F>(ref_));
0670 }
0671
0672 private:
0673
0674 template <typename F>
0675 class Impl : public ActionInterface<F> {
0676 public:
0677 typedef typename Function<F>::Result Result;
0678 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
0679
0680 explicit Impl(T& ref) : ref_(ref) {}
0681
0682 virtual Result Perform(const ArgumentTuple&) {
0683 return ref_;
0684 }
0685
0686 private:
0687 T& ref_;
0688
0689 GTEST_DISALLOW_ASSIGN_(Impl);
0690 };
0691
0692 T& ref_;
0693
0694 GTEST_DISALLOW_ASSIGN_(ReturnRefAction);
0695 };
0696
0697
0698
0699
0700 template <typename T>
0701 class ReturnRefOfCopyAction {
0702 public:
0703
0704
0705 explicit ReturnRefOfCopyAction(const T& value) : value_(value) {}
0706
0707
0708
0709 template <typename F>
0710 operator Action<F>() const {
0711 typedef typename Function<F>::Result Result;
0712
0713
0714
0715 GTEST_COMPILE_ASSERT_(
0716 internal::is_reference<Result>::value,
0717 use_Return_instead_of_ReturnRefOfCopy_to_return_a_value);
0718 return Action<F>(new Impl<F>(value_));
0719 }
0720
0721 private:
0722
0723 template <typename F>
0724 class Impl : public ActionInterface<F> {
0725 public:
0726 typedef typename Function<F>::Result Result;
0727 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
0728
0729 explicit Impl(const T& value) : value_(value) {}
0730
0731 virtual Result Perform(const ArgumentTuple&) {
0732 return value_;
0733 }
0734
0735 private:
0736 T value_;
0737
0738 GTEST_DISALLOW_ASSIGN_(Impl);
0739 };
0740
0741 const T value_;
0742
0743 GTEST_DISALLOW_ASSIGN_(ReturnRefOfCopyAction);
0744 };
0745
0746
0747 class DoDefaultAction {
0748 public:
0749
0750
0751 template <typename F>
0752 operator Action<F>() const { return Action<F>(NULL); }
0753 };
0754
0755
0756
0757 template <typename T1, typename T2>
0758 class AssignAction {
0759 public:
0760 AssignAction(T1* ptr, T2 value) : ptr_(ptr), value_(value) {}
0761
0762 template <typename Result, typename ArgumentTuple>
0763 void Perform(const ArgumentTuple& ) const {
0764 *ptr_ = value_;
0765 }
0766
0767 private:
0768 T1* const ptr_;
0769 const T2 value_;
0770
0771 GTEST_DISALLOW_ASSIGN_(AssignAction);
0772 };
0773
0774 #if !GTEST_OS_WINDOWS_MOBILE
0775
0776
0777
0778 template <typename T>
0779 class SetErrnoAndReturnAction {
0780 public:
0781 SetErrnoAndReturnAction(int errno_value, T result)
0782 : errno_(errno_value),
0783 result_(result) {}
0784 template <typename Result, typename ArgumentTuple>
0785 Result Perform(const ArgumentTuple& ) const {
0786 errno = errno_;
0787 return result_;
0788 }
0789
0790 private:
0791 const int errno_;
0792 const T result_;
0793
0794 GTEST_DISALLOW_ASSIGN_(SetErrnoAndReturnAction);
0795 };
0796
0797 #endif
0798
0799
0800
0801
0802
0803 template <size_t N, typename A, bool kIsProto>
0804 class SetArgumentPointeeAction {
0805 public:
0806
0807
0808 explicit SetArgumentPointeeAction(const A& value) : value_(value) {}
0809
0810 template <typename Result, typename ArgumentTuple>
0811 void Perform(const ArgumentTuple& args) const {
0812 CompileAssertTypesEqual<void, Result>();
0813 *::testing::get<N>(args) = value_;
0814 }
0815
0816 private:
0817 const A value_;
0818
0819 GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
0820 };
0821
0822 template <size_t N, typename Proto>
0823 class SetArgumentPointeeAction<N, Proto, true> {
0824 public:
0825
0826
0827
0828
0829 explicit SetArgumentPointeeAction(const Proto& proto) : proto_(new Proto) {
0830 proto_->CopyFrom(proto);
0831 }
0832
0833 template <typename Result, typename ArgumentTuple>
0834 void Perform(const ArgumentTuple& args) const {
0835 CompileAssertTypesEqual<void, Result>();
0836 ::testing::get<N>(args)->CopyFrom(*proto_);
0837 }
0838
0839 private:
0840 const internal::linked_ptr<Proto> proto_;
0841
0842 GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
0843 };
0844
0845
0846
0847
0848
0849
0850 template <typename FunctionImpl>
0851 class InvokeWithoutArgsAction {
0852 public:
0853
0854
0855 explicit InvokeWithoutArgsAction(FunctionImpl function_impl)
0856 : function_impl_(function_impl) {}
0857
0858
0859
0860 template <typename Result, typename ArgumentTuple>
0861 Result Perform(const ArgumentTuple&) { return function_impl_(); }
0862
0863 private:
0864 FunctionImpl function_impl_;
0865
0866 GTEST_DISALLOW_ASSIGN_(InvokeWithoutArgsAction);
0867 };
0868
0869
0870 template <class Class, typename MethodPtr>
0871 class InvokeMethodWithoutArgsAction {
0872 public:
0873 InvokeMethodWithoutArgsAction(Class* obj_ptr, MethodPtr method_ptr)
0874 : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
0875
0876 template <typename Result, typename ArgumentTuple>
0877 Result Perform(const ArgumentTuple&) const {
0878 return (obj_ptr_->*method_ptr_)();
0879 }
0880
0881 private:
0882 Class* const obj_ptr_;
0883 const MethodPtr method_ptr_;
0884
0885 GTEST_DISALLOW_ASSIGN_(InvokeMethodWithoutArgsAction);
0886 };
0887
0888
0889 template <typename A>
0890 class IgnoreResultAction {
0891 public:
0892 explicit IgnoreResultAction(const A& action) : action_(action) {}
0893
0894 template <typename F>
0895 operator Action<F>() const {
0896
0897
0898
0899
0900
0901
0902
0903
0904 typedef typename internal::Function<F>::Result Result;
0905
0906
0907 CompileAssertTypesEqual<void, Result>();
0908
0909 return Action<F>(new Impl<F>(action_));
0910 }
0911
0912 private:
0913 template <typename F>
0914 class Impl : public ActionInterface<F> {
0915 public:
0916 typedef typename internal::Function<F>::Result Result;
0917 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
0918
0919 explicit Impl(const A& action) : action_(action) {}
0920
0921 virtual void Perform(const ArgumentTuple& args) {
0922
0923 action_.Perform(args);
0924 }
0925
0926 private:
0927
0928
0929 typedef typename internal::Function<F>::MakeResultIgnoredValue
0930 OriginalFunction;
0931
0932 const Action<OriginalFunction> action_;
0933
0934 GTEST_DISALLOW_ASSIGN_(Impl);
0935 };
0936
0937 const A action_;
0938
0939 GTEST_DISALLOW_ASSIGN_(IgnoreResultAction);
0940 };
0941
0942
0943
0944
0945
0946
0947
0948
0949 template <typename T>
0950 class ReferenceWrapper {
0951 public:
0952
0953 explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {}
0954
0955
0956
0957 operator T&() const { return *pointer_; }
0958 private:
0959 T* pointer_;
0960 };
0961
0962
0963 template <typename T>
0964 void PrintTo(const ReferenceWrapper<T>& ref, ::std::ostream* os) {
0965 T& value = ref;
0966 UniversalPrinter<T&>::Print(value, os);
0967 }
0968
0969
0970
0971 template <typename Action1, typename Action2>
0972 class DoBothAction {
0973 public:
0974 DoBothAction(Action1 action1, Action2 action2)
0975 : action1_(action1), action2_(action2) {}
0976
0977
0978
0979 template <typename F>
0980 operator Action<F>() const {
0981 return Action<F>(new Impl<F>(action1_, action2_));
0982 }
0983
0984 private:
0985
0986 template <typename F>
0987 class Impl : public ActionInterface<F> {
0988 public:
0989 typedef typename Function<F>::Result Result;
0990 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
0991 typedef typename Function<F>::MakeResultVoid VoidResult;
0992
0993 Impl(const Action<VoidResult>& action1, const Action<F>& action2)
0994 : action1_(action1), action2_(action2) {}
0995
0996 virtual Result Perform(const ArgumentTuple& args) {
0997 action1_.Perform(args);
0998 return action2_.Perform(args);
0999 }
1000
1001 private:
1002 const Action<VoidResult> action1_;
1003 const Action<F> action2_;
1004
1005 GTEST_DISALLOW_ASSIGN_(Impl);
1006 };
1007
1008 Action1 action1_;
1009 Action2 action2_;
1010
1011 GTEST_DISALLOW_ASSIGN_(DoBothAction);
1012 };
1013
1014 }
1015
1016
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 typedef internal::IgnoredValue Unused;
1047
1048
1049
1050
1051
1052 template <typename To>
1053 template <typename From>
1054 Action<To>::Action(const Action<From>& from)
1055 : impl_(new internal::ActionAdaptor<To, From>(from)) {}
1056
1057
1058
1059
1060 template <typename R>
1061 internal::ReturnAction<R> Return(R value) {
1062 return internal::ReturnAction<R>(internal::move(value));
1063 }
1064
1065
1066 inline PolymorphicAction<internal::ReturnNullAction> ReturnNull() {
1067 return MakePolymorphicAction(internal::ReturnNullAction());
1068 }
1069
1070
1071 inline PolymorphicAction<internal::ReturnVoidAction> Return() {
1072 return MakePolymorphicAction(internal::ReturnVoidAction());
1073 }
1074
1075
1076 template <typename R>
1077 inline internal::ReturnRefAction<R> ReturnRef(R& x) {
1078 return internal::ReturnRefAction<R>(x);
1079 }
1080
1081
1082
1083
1084 template <typename R>
1085 inline internal::ReturnRefOfCopyAction<R> ReturnRefOfCopy(const R& x) {
1086 return internal::ReturnRefOfCopyAction<R>(x);
1087 }
1088
1089
1090
1091
1092
1093 template <typename R>
1094 internal::ByMoveWrapper<R> ByMove(R x) {
1095 return internal::ByMoveWrapper<R>(internal::move(x));
1096 }
1097
1098
1099 inline internal::DoDefaultAction DoDefault() {
1100 return internal::DoDefaultAction();
1101 }
1102
1103
1104
1105 template <size_t N, typename T>
1106 PolymorphicAction<
1107 internal::SetArgumentPointeeAction<
1108 N, T, internal::IsAProtocolMessage<T>::value> >
1109 SetArgPointee(const T& x) {
1110 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1111 N, T, internal::IsAProtocolMessage<T>::value>(x));
1112 }
1113
1114 #if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
1115
1116
1117
1118 template <size_t N>
1119 PolymorphicAction<
1120 internal::SetArgumentPointeeAction<N, const char*, false> >
1121 SetArgPointee(const char* p) {
1122 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1123 N, const char*, false>(p));
1124 }
1125
1126 template <size_t N>
1127 PolymorphicAction<
1128 internal::SetArgumentPointeeAction<N, const wchar_t*, false> >
1129 SetArgPointee(const wchar_t* p) {
1130 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1131 N, const wchar_t*, false>(p));
1132 }
1133 #endif
1134
1135
1136 template <size_t N, typename T>
1137 PolymorphicAction<
1138 internal::SetArgumentPointeeAction<
1139 N, T, internal::IsAProtocolMessage<T>::value> >
1140 SetArgumentPointee(const T& x) {
1141 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1142 N, T, internal::IsAProtocolMessage<T>::value>(x));
1143 }
1144
1145
1146 template <typename T1, typename T2>
1147 PolymorphicAction<internal::AssignAction<T1, T2> > Assign(T1* ptr, T2 val) {
1148 return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val));
1149 }
1150
1151 #if !GTEST_OS_WINDOWS_MOBILE
1152
1153
1154 template <typename T>
1155 PolymorphicAction<internal::SetErrnoAndReturnAction<T> >
1156 SetErrnoAndReturn(int errval, T result) {
1157 return MakePolymorphicAction(
1158 internal::SetErrnoAndReturnAction<T>(errval, result));
1159 }
1160
1161 #endif
1162
1163
1164
1165
1166 template <typename FunctionImpl>
1167 PolymorphicAction<internal::InvokeWithoutArgsAction<FunctionImpl> >
1168 InvokeWithoutArgs(FunctionImpl function_impl) {
1169 return MakePolymorphicAction(
1170 internal::InvokeWithoutArgsAction<FunctionImpl>(function_impl));
1171 }
1172
1173
1174
1175 template <class Class, typename MethodPtr>
1176 PolymorphicAction<internal::InvokeMethodWithoutArgsAction<Class, MethodPtr> >
1177 InvokeWithoutArgs(Class* obj_ptr, MethodPtr method_ptr) {
1178 return MakePolymorphicAction(
1179 internal::InvokeMethodWithoutArgsAction<Class, MethodPtr>(
1180 obj_ptr, method_ptr));
1181 }
1182
1183
1184
1185
1186 template <typename A>
1187 inline internal::IgnoreResultAction<A> IgnoreResult(const A& an_action) {
1188 return internal::IgnoreResultAction<A>(an_action);
1189 }
1190
1191
1192
1193
1194
1195
1196
1197
1198 template <typename T>
1199 inline internal::ReferenceWrapper<T> ByRef(T& l_value) {
1200 return internal::ReferenceWrapper<T>(l_value);
1201 }
1202
1203 }
1204
1205 #endif