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 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
00037 #define GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
00038
00039 #ifndef _WIN32_WCE
00040 # include <errno.h>
00041 #endif
00042
00043 #include <algorithm>
00044 #include <string>
00045
00046 #include "gmock/internal/gmock-internal-utils.h"
00047 #include "gmock/internal/gmock-port.h"
00048
00049 #if GTEST_HAS_STD_TYPE_TRAITS_ // Defined by gtest-port.h via gmock-port.h.
00050 #include <type_traits>
00051 #endif
00052
00053 namespace testing {
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 namespace internal {
00065
00066 template <typename F1, typename F2>
00067 class ActionAdaptor;
00068
00069
00070
00071
00072
00073
00074 template <typename T, bool kDefaultConstructible>
00075 struct BuiltInDefaultValueGetter {
00076 static T Get() { return T(); }
00077 };
00078 template <typename T>
00079 struct BuiltInDefaultValueGetter<T, false> {
00080 static T Get() {
00081 Assert(false, __FILE__, __LINE__,
00082 "Default action undefined for the function return type.");
00083 return internal::Invalid<T>();
00084
00085
00086 }
00087 };
00088
00089
00090
00091
00092
00093
00094
00095
00096 template <typename T>
00097 class BuiltInDefaultValue {
00098 public:
00099 #if GTEST_HAS_STD_TYPE_TRAITS_
00100
00101 static bool Exists() {
00102 return ::std::is_default_constructible<T>::value;
00103 }
00104
00105 static T Get() {
00106 return BuiltInDefaultValueGetter<
00107 T, ::std::is_default_constructible<T>::value>::Get();
00108 }
00109
00110 #else // GTEST_HAS_STD_TYPE_TRAITS_
00111
00112 static bool Exists() {
00113 return false;
00114 }
00115
00116 static T Get() {
00117 return BuiltInDefaultValueGetter<T, false>::Get();
00118 }
00119
00120 #endif // GTEST_HAS_STD_TYPE_TRAITS_
00121 };
00122
00123
00124
00125 template <typename T>
00126 class BuiltInDefaultValue<const T> {
00127 public:
00128 static bool Exists() { return BuiltInDefaultValue<T>::Exists(); }
00129 static T Get() { return BuiltInDefaultValue<T>::Get(); }
00130 };
00131
00132
00133
00134 template <typename T>
00135 class BuiltInDefaultValue<T*> {
00136 public:
00137 static bool Exists() { return true; }
00138 static T* Get() { return NULL; }
00139 };
00140
00141
00142
00143 #define GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(type, value) \
00144 template <> \
00145 class BuiltInDefaultValue<type> { \
00146 public: \
00147 static bool Exists() { return true; } \
00148 static type Get() { return value; } \
00149 }
00150
00151 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(void, );
00152 #if GTEST_HAS_GLOBAL_STRING
00153 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::string, "");
00154 #endif // GTEST_HAS_GLOBAL_STRING
00155 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::std::string, "");
00156 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(bool, false);
00157 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned char, '\0');
00158 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed char, '\0');
00159 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(char, '\0');
00160
00161
00162
00163
00164
00165
00166
00167 #if GMOCK_WCHAR_T_IS_NATIVE_
00168 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(wchar_t, 0U);
00169 #endif
00170
00171 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned short, 0U);
00172 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed short, 0);
00173 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned int, 0U);
00174 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed int, 0);
00175 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned long, 0UL);
00176 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed long, 0L);
00177 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(UInt64, 0);
00178 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(Int64, 0);
00179 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(float, 0);
00180 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(double, 0);
00181
00182 #undef GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_
00183
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 template <typename T>
00200 class DefaultValue {
00201 public:
00202
00203
00204 static void Set(T x) {
00205 delete producer_;
00206 producer_ = new FixedValueProducer(x);
00207 }
00208
00209
00210
00211
00212 typedef T (*FactoryFunction)();
00213 static void SetFactory(FactoryFunction factory) {
00214 delete producer_;
00215 producer_ = new FactoryValueProducer(factory);
00216 }
00217
00218
00219 static void Clear() {
00220 delete producer_;
00221 producer_ = NULL;
00222 }
00223
00224
00225 static bool IsSet() { return producer_ != NULL; }
00226
00227
00228
00229 static bool Exists() {
00230 return IsSet() || internal::BuiltInDefaultValue<T>::Exists();
00231 }
00232
00233
00234
00235
00236 static T Get() {
00237 return producer_ == NULL ?
00238 internal::BuiltInDefaultValue<T>::Get() : producer_->Produce();
00239 }
00240
00241 private:
00242 class ValueProducer {
00243 public:
00244 virtual ~ValueProducer() {}
00245 virtual T Produce() = 0;
00246 };
00247
00248 class FixedValueProducer : public ValueProducer {
00249 public:
00250 explicit FixedValueProducer(T value) : value_(value) {}
00251 virtual T Produce() { return value_; }
00252
00253 private:
00254 const T value_;
00255 GTEST_DISALLOW_COPY_AND_ASSIGN_(FixedValueProducer);
00256 };
00257
00258 class FactoryValueProducer : public ValueProducer {
00259 public:
00260 explicit FactoryValueProducer(FactoryFunction factory)
00261 : factory_(factory) {}
00262 virtual T Produce() { return factory_(); }
00263
00264 private:
00265 const FactoryFunction factory_;
00266 GTEST_DISALLOW_COPY_AND_ASSIGN_(FactoryValueProducer);
00267 };
00268
00269 static ValueProducer* producer_;
00270 };
00271
00272
00273
00274 template <typename T>
00275 class DefaultValue<T&> {
00276 public:
00277
00278 static void Set(T& x) {
00279 address_ = &x;
00280 }
00281
00282
00283 static void Clear() {
00284 address_ = NULL;
00285 }
00286
00287
00288 static bool IsSet() { return address_ != NULL; }
00289
00290
00291
00292 static bool Exists() {
00293 return IsSet() || internal::BuiltInDefaultValue<T&>::Exists();
00294 }
00295
00296
00297
00298
00299 static T& Get() {
00300 return address_ == NULL ?
00301 internal::BuiltInDefaultValue<T&>::Get() : *address_;
00302 }
00303
00304 private:
00305 static T* address_;
00306 };
00307
00308
00309
00310 template <>
00311 class DefaultValue<void> {
00312 public:
00313 static bool Exists() { return true; }
00314 static void Get() {}
00315 };
00316
00317
00318 template <typename T>
00319 typename DefaultValue<T>::ValueProducer* DefaultValue<T>::producer_ = NULL;
00320
00321
00322 template <typename T>
00323 T* DefaultValue<T&>::address_ = NULL;
00324
00325
00326 template <typename F>
00327 class ActionInterface {
00328 public:
00329 typedef typename internal::Function<F>::Result Result;
00330 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
00331
00332 ActionInterface() {}
00333 virtual ~ActionInterface() {}
00334
00335
00336
00337
00338
00339 virtual Result Perform(const ArgumentTuple& args) = 0;
00340
00341 private:
00342 GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionInterface);
00343 };
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354 template <typename F>
00355 class Action {
00356 public:
00357 typedef typename internal::Function<F>::Result Result;
00358 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
00359
00360
00361
00362 Action() : impl_(NULL) {}
00363
00364
00365
00366 explicit Action(ActionInterface<F>* impl) : impl_(impl) {}
00367
00368
00369 Action(const Action& action) : impl_(action.impl_) {}
00370
00371
00372
00373
00374
00375 template <typename Func>
00376 explicit Action(const Action<Func>& action);
00377
00378
00379 bool IsDoDefault() const { return impl_.get() == NULL; }
00380
00381
00382
00383
00384
00385
00386
00387 Result Perform(const ArgumentTuple& args) const {
00388 internal::Assert(
00389 !IsDoDefault(), __FILE__, __LINE__,
00390 "You are using DoDefault() inside a composite action like "
00391 "DoAll() or WithArgs(). This is not supported for technical "
00392 "reasons. Please instead spell out the default action, or "
00393 "assign the default action to an Action variable and use "
00394 "the variable in various places.");
00395 return impl_->Perform(args);
00396 }
00397
00398 private:
00399 template <typename F1, typename F2>
00400 friend class internal::ActionAdaptor;
00401
00402 internal::linked_ptr<ActionInterface<F> > impl_;
00403 };
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426 template <typename Impl>
00427 class PolymorphicAction {
00428 public:
00429 explicit PolymorphicAction(const Impl& impl) : impl_(impl) {}
00430
00431 template <typename F>
00432 operator Action<F>() const {
00433 return Action<F>(new MonomorphicImpl<F>(impl_));
00434 }
00435
00436 private:
00437 template <typename F>
00438 class MonomorphicImpl : public ActionInterface<F> {
00439 public:
00440 typedef typename internal::Function<F>::Result Result;
00441 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
00442
00443 explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
00444
00445 virtual Result Perform(const ArgumentTuple& args) {
00446 return impl_.template Perform<Result>(args);
00447 }
00448
00449 private:
00450 Impl impl_;
00451
00452 GTEST_DISALLOW_ASSIGN_(MonomorphicImpl);
00453 };
00454
00455 Impl impl_;
00456
00457 GTEST_DISALLOW_ASSIGN_(PolymorphicAction);
00458 };
00459
00460
00461
00462 template <typename F>
00463 Action<F> MakeAction(ActionInterface<F>* impl) {
00464 return Action<F>(impl);
00465 }
00466
00467
00468
00469
00470
00471
00472
00473
00474 template <typename Impl>
00475 inline PolymorphicAction<Impl> MakePolymorphicAction(const Impl& impl) {
00476 return PolymorphicAction<Impl>(impl);
00477 }
00478
00479 namespace internal {
00480
00481
00482
00483 template <typename F1, typename F2>
00484 class ActionAdaptor : public ActionInterface<F1> {
00485 public:
00486 typedef typename internal::Function<F1>::Result Result;
00487 typedef typename internal::Function<F1>::ArgumentTuple ArgumentTuple;
00488
00489 explicit ActionAdaptor(const Action<F2>& from) : impl_(from.impl_) {}
00490
00491 virtual Result Perform(const ArgumentTuple& args) {
00492 return impl_->Perform(args);
00493 }
00494
00495 private:
00496 const internal::linked_ptr<ActionInterface<F2> > impl_;
00497
00498 GTEST_DISALLOW_ASSIGN_(ActionAdaptor);
00499 };
00500
00501
00502
00503 template <typename T>
00504 struct ByMoveWrapper {
00505 explicit ByMoveWrapper(T value) : payload(internal::move(value)) {}
00506 T payload;
00507 };
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533 template <typename R>
00534 class ReturnAction {
00535 public:
00536
00537
00538
00539 explicit ReturnAction(R value) : value_(new R(internal::move(value))) {}
00540
00541
00542
00543 template <typename F>
00544 operator Action<F>() const {
00545
00546
00547
00548
00549
00550
00551
00552
00553 typedef typename Function<F>::Result Result;
00554 GTEST_COMPILE_ASSERT_(
00555 !is_reference<Result>::value,
00556 use_ReturnRef_instead_of_Return_to_return_a_reference);
00557 return Action<F>(new Impl<R, F>(value_));
00558 }
00559
00560 private:
00561
00562 template <typename R_, typename F>
00563 class Impl : public ActionInterface<F> {
00564 public:
00565 typedef typename Function<F>::Result Result;
00566 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
00567
00568
00569
00570
00571
00572
00573
00574
00575 explicit Impl(const linked_ptr<R>& value)
00576 : value_before_cast_(*value),
00577 value_(ImplicitCast_<Result>(value_before_cast_)) {}
00578
00579 virtual Result Perform(const ArgumentTuple&) { return value_; }
00580
00581 private:
00582 GTEST_COMPILE_ASSERT_(!is_reference<Result>::value,
00583 Result_cannot_be_a_reference_type);
00584
00585
00586 R value_before_cast_;
00587 Result value_;
00588
00589 GTEST_DISALLOW_COPY_AND_ASSIGN_(Impl);
00590 };
00591
00592
00593
00594 template <typename R_, typename F>
00595 class Impl<ByMoveWrapper<R_>, F> : public ActionInterface<F> {
00596 public:
00597 typedef typename Function<F>::Result Result;
00598 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
00599
00600 explicit Impl(const linked_ptr<R>& wrapper)
00601 : performed_(false), wrapper_(wrapper) {}
00602
00603 virtual Result Perform(const ArgumentTuple&) {
00604 GTEST_CHECK_(!performed_)
00605 << "A ByMove() action should only be performed once.";
00606 performed_ = true;
00607 return internal::move(wrapper_->payload);
00608 }
00609
00610 private:
00611 bool performed_;
00612 const linked_ptr<R> wrapper_;
00613
00614 GTEST_DISALLOW_ASSIGN_(Impl);
00615 };
00616
00617 const linked_ptr<R> value_;
00618
00619 GTEST_DISALLOW_ASSIGN_(ReturnAction);
00620 };
00621
00622
00623 class ReturnNullAction {
00624 public:
00625
00626
00627
00628 template <typename Result, typename ArgumentTuple>
00629 static Result Perform(const ArgumentTuple&) {
00630 #if GTEST_LANG_CXX11
00631 return nullptr;
00632 #else
00633 GTEST_COMPILE_ASSERT_(internal::is_pointer<Result>::value,
00634 ReturnNull_can_be_used_to_return_a_pointer_only);
00635 return NULL;
00636 #endif // GTEST_LANG_CXX11
00637 }
00638 };
00639
00640
00641 class ReturnVoidAction {
00642 public:
00643
00644 template <typename Result, typename ArgumentTuple>
00645 static void Perform(const ArgumentTuple&) {
00646 CompileAssertTypesEqual<void, Result>();
00647 }
00648 };
00649
00650
00651
00652
00653 template <typename T>
00654 class ReturnRefAction {
00655 public:
00656
00657 explicit ReturnRefAction(T& ref) : ref_(ref) {}
00658
00659
00660
00661 template <typename F>
00662 operator Action<F>() const {
00663 typedef typename Function<F>::Result Result;
00664
00665
00666
00667 GTEST_COMPILE_ASSERT_(internal::is_reference<Result>::value,
00668 use_Return_instead_of_ReturnRef_to_return_a_value);
00669 return Action<F>(new Impl<F>(ref_));
00670 }
00671
00672 private:
00673
00674 template <typename F>
00675 class Impl : public ActionInterface<F> {
00676 public:
00677 typedef typename Function<F>::Result Result;
00678 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
00679
00680 explicit Impl(T& ref) : ref_(ref) {}
00681
00682 virtual Result Perform(const ArgumentTuple&) {
00683 return ref_;
00684 }
00685
00686 private:
00687 T& ref_;
00688
00689 GTEST_DISALLOW_ASSIGN_(Impl);
00690 };
00691
00692 T& ref_;
00693
00694 GTEST_DISALLOW_ASSIGN_(ReturnRefAction);
00695 };
00696
00697
00698
00699
00700 template <typename T>
00701 class ReturnRefOfCopyAction {
00702 public:
00703
00704
00705 explicit ReturnRefOfCopyAction(const T& value) : value_(value) {}
00706
00707
00708
00709 template <typename F>
00710 operator Action<F>() const {
00711 typedef typename Function<F>::Result Result;
00712
00713
00714
00715 GTEST_COMPILE_ASSERT_(
00716 internal::is_reference<Result>::value,
00717 use_Return_instead_of_ReturnRefOfCopy_to_return_a_value);
00718 return Action<F>(new Impl<F>(value_));
00719 }
00720
00721 private:
00722
00723 template <typename F>
00724 class Impl : public ActionInterface<F> {
00725 public:
00726 typedef typename Function<F>::Result Result;
00727 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
00728
00729 explicit Impl(const T& value) : value_(value) {}
00730
00731 virtual Result Perform(const ArgumentTuple&) {
00732 return value_;
00733 }
00734
00735 private:
00736 T value_;
00737
00738 GTEST_DISALLOW_ASSIGN_(Impl);
00739 };
00740
00741 const T value_;
00742
00743 GTEST_DISALLOW_ASSIGN_(ReturnRefOfCopyAction);
00744 };
00745
00746
00747 class DoDefaultAction {
00748 public:
00749
00750
00751 template <typename F>
00752 operator Action<F>() const { return Action<F>(NULL); }
00753 };
00754
00755
00756
00757 template <typename T1, typename T2>
00758 class AssignAction {
00759 public:
00760 AssignAction(T1* ptr, T2 value) : ptr_(ptr), value_(value) {}
00761
00762 template <typename Result, typename ArgumentTuple>
00763 void Perform(const ArgumentTuple& ) const {
00764 *ptr_ = value_;
00765 }
00766
00767 private:
00768 T1* const ptr_;
00769 const T2 value_;
00770
00771 GTEST_DISALLOW_ASSIGN_(AssignAction);
00772 };
00773
00774 #if !GTEST_OS_WINDOWS_MOBILE
00775
00776
00777
00778 template <typename T>
00779 class SetErrnoAndReturnAction {
00780 public:
00781 SetErrnoAndReturnAction(int errno_value, T result)
00782 : errno_(errno_value),
00783 result_(result) {}
00784 template <typename Result, typename ArgumentTuple>
00785 Result Perform(const ArgumentTuple& ) const {
00786 errno = errno_;
00787 return result_;
00788 }
00789
00790 private:
00791 const int errno_;
00792 const T result_;
00793
00794 GTEST_DISALLOW_ASSIGN_(SetErrnoAndReturnAction);
00795 };
00796
00797 #endif // !GTEST_OS_WINDOWS_MOBILE
00798
00799
00800
00801
00802
00803 template <size_t N, typename A, bool kIsProto>
00804 class SetArgumentPointeeAction {
00805 public:
00806
00807
00808 explicit SetArgumentPointeeAction(const A& value) : value_(value) {}
00809
00810 template <typename Result, typename ArgumentTuple>
00811 void Perform(const ArgumentTuple& args) const {
00812 CompileAssertTypesEqual<void, Result>();
00813 *::testing::get<N>(args) = value_;
00814 }
00815
00816 private:
00817 const A value_;
00818
00819 GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
00820 };
00821
00822 template <size_t N, typename Proto>
00823 class SetArgumentPointeeAction<N, Proto, true> {
00824 public:
00825
00826
00827
00828
00829 explicit SetArgumentPointeeAction(const Proto& proto) : proto_(new Proto) {
00830 proto_->CopyFrom(proto);
00831 }
00832
00833 template <typename Result, typename ArgumentTuple>
00834 void Perform(const ArgumentTuple& args) const {
00835 CompileAssertTypesEqual<void, Result>();
00836 ::testing::get<N>(args)->CopyFrom(*proto_);
00837 }
00838
00839 private:
00840 const internal::linked_ptr<Proto> proto_;
00841
00842 GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
00843 };
00844
00845
00846
00847
00848
00849
00850 template <typename FunctionImpl>
00851 class InvokeWithoutArgsAction {
00852 public:
00853
00854
00855 explicit InvokeWithoutArgsAction(FunctionImpl function_impl)
00856 : function_impl_(function_impl) {}
00857
00858
00859
00860 template <typename Result, typename ArgumentTuple>
00861 Result Perform(const ArgumentTuple&) { return function_impl_(); }
00862
00863 private:
00864 FunctionImpl function_impl_;
00865
00866 GTEST_DISALLOW_ASSIGN_(InvokeWithoutArgsAction);
00867 };
00868
00869
00870 template <class Class, typename MethodPtr>
00871 class InvokeMethodWithoutArgsAction {
00872 public:
00873 InvokeMethodWithoutArgsAction(Class* obj_ptr, MethodPtr method_ptr)
00874 : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
00875
00876 template <typename Result, typename ArgumentTuple>
00877 Result Perform(const ArgumentTuple&) const {
00878 return (obj_ptr_->*method_ptr_)();
00879 }
00880
00881 private:
00882 Class* const obj_ptr_;
00883 const MethodPtr method_ptr_;
00884
00885 GTEST_DISALLOW_ASSIGN_(InvokeMethodWithoutArgsAction);
00886 };
00887
00888
00889 template <typename A>
00890 class IgnoreResultAction {
00891 public:
00892 explicit IgnoreResultAction(const A& action) : action_(action) {}
00893
00894 template <typename F>
00895 operator Action<F>() const {
00896
00897
00898
00899
00900
00901
00902
00903
00904 typedef typename internal::Function<F>::Result Result;
00905
00906
00907 CompileAssertTypesEqual<void, Result>();
00908
00909 return Action<F>(new Impl<F>(action_));
00910 }
00911
00912 private:
00913 template <typename F>
00914 class Impl : public ActionInterface<F> {
00915 public:
00916 typedef typename internal::Function<F>::Result Result;
00917 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
00918
00919 explicit Impl(const A& action) : action_(action) {}
00920
00921 virtual void Perform(const ArgumentTuple& args) {
00922
00923 action_.Perform(args);
00924 }
00925
00926 private:
00927
00928
00929 typedef typename internal::Function<F>::MakeResultIgnoredValue
00930 OriginalFunction;
00931
00932 const Action<OriginalFunction> action_;
00933
00934 GTEST_DISALLOW_ASSIGN_(Impl);
00935 };
00936
00937 const A action_;
00938
00939 GTEST_DISALLOW_ASSIGN_(IgnoreResultAction);
00940 };
00941
00942
00943
00944
00945
00946
00947
00948
00949 template <typename T>
00950 class ReferenceWrapper {
00951 public:
00952
00953 explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {}
00954
00955
00956
00957 operator T&() const { return *pointer_; }
00958 private:
00959 T* pointer_;
00960 };
00961
00962
00963 template <typename T>
00964 void PrintTo(const ReferenceWrapper<T>& ref, ::std::ostream* os) {
00965 T& value = ref;
00966 UniversalPrinter<T&>::Print(value, os);
00967 }
00968
00969
00970
00971 template <typename Action1, typename Action2>
00972 class DoBothAction {
00973 public:
00974 DoBothAction(Action1 action1, Action2 action2)
00975 : action1_(action1), action2_(action2) {}
00976
00977
00978
00979 template <typename F>
00980 operator Action<F>() const {
00981 return Action<F>(new Impl<F>(action1_, action2_));
00982 }
00983
00984 private:
00985
00986 template <typename F>
00987 class Impl : public ActionInterface<F> {
00988 public:
00989 typedef typename Function<F>::Result Result;
00990 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
00991 typedef typename Function<F>::MakeResultVoid VoidResult;
00992
00993 Impl(const Action<VoidResult>& action1, const Action<F>& action2)
00994 : action1_(action1), action2_(action2) {}
00995
00996 virtual Result Perform(const ArgumentTuple& args) {
00997 action1_.Perform(args);
00998 return action2_.Perform(args);
00999 }
01000
01001 private:
01002 const Action<VoidResult> action1_;
01003 const Action<F> action2_;
01004
01005 GTEST_DISALLOW_ASSIGN_(Impl);
01006 };
01007
01008 Action1 action1_;
01009 Action2 action2_;
01010
01011 GTEST_DISALLOW_ASSIGN_(DoBothAction);
01012 };
01013
01014 }
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046 typedef internal::IgnoredValue Unused;
01047
01048
01049
01050
01051
01052 template <typename To>
01053 template <typename From>
01054 Action<To>::Action(const Action<From>& from)
01055 : impl_(new internal::ActionAdaptor<To, From>(from)) {}
01056
01057
01058
01059
01060 template <typename R>
01061 internal::ReturnAction<R> Return(R value) {
01062 return internal::ReturnAction<R>(internal::move(value));
01063 }
01064
01065
01066 inline PolymorphicAction<internal::ReturnNullAction> ReturnNull() {
01067 return MakePolymorphicAction(internal::ReturnNullAction());
01068 }
01069
01070
01071 inline PolymorphicAction<internal::ReturnVoidAction> Return() {
01072 return MakePolymorphicAction(internal::ReturnVoidAction());
01073 }
01074
01075
01076 template <typename R>
01077 inline internal::ReturnRefAction<R> ReturnRef(R& x) {
01078 return internal::ReturnRefAction<R>(x);
01079 }
01080
01081
01082
01083
01084 template <typename R>
01085 inline internal::ReturnRefOfCopyAction<R> ReturnRefOfCopy(const R& x) {
01086 return internal::ReturnRefOfCopyAction<R>(x);
01087 }
01088
01089
01090
01091
01092
01093 template <typename R>
01094 internal::ByMoveWrapper<R> ByMove(R x) {
01095 return internal::ByMoveWrapper<R>(internal::move(x));
01096 }
01097
01098
01099 inline internal::DoDefaultAction DoDefault() {
01100 return internal::DoDefaultAction();
01101 }
01102
01103
01104
01105 template <size_t N, typename T>
01106 PolymorphicAction<
01107 internal::SetArgumentPointeeAction<
01108 N, T, internal::IsAProtocolMessage<T>::value> >
01109 SetArgPointee(const T& x) {
01110 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
01111 N, T, internal::IsAProtocolMessage<T>::value>(x));
01112 }
01113
01114 #if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
01115
01116
01117
01118 template <size_t N>
01119 PolymorphicAction<
01120 internal::SetArgumentPointeeAction<N, const char*, false> >
01121 SetArgPointee(const char* p) {
01122 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
01123 N, const char*, false>(p));
01124 }
01125
01126 template <size_t N>
01127 PolymorphicAction<
01128 internal::SetArgumentPointeeAction<N, const wchar_t*, false> >
01129 SetArgPointee(const wchar_t* p) {
01130 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
01131 N, const wchar_t*, false>(p));
01132 }
01133 #endif
01134
01135
01136 template <size_t N, typename T>
01137 PolymorphicAction<
01138 internal::SetArgumentPointeeAction<
01139 N, T, internal::IsAProtocolMessage<T>::value> >
01140 SetArgumentPointee(const T& x) {
01141 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
01142 N, T, internal::IsAProtocolMessage<T>::value>(x));
01143 }
01144
01145
01146 template <typename T1, typename T2>
01147 PolymorphicAction<internal::AssignAction<T1, T2> > Assign(T1* ptr, T2 val) {
01148 return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val));
01149 }
01150
01151 #if !GTEST_OS_WINDOWS_MOBILE
01152
01153
01154 template <typename T>
01155 PolymorphicAction<internal::SetErrnoAndReturnAction<T> >
01156 SetErrnoAndReturn(int errval, T result) {
01157 return MakePolymorphicAction(
01158 internal::SetErrnoAndReturnAction<T>(errval, result));
01159 }
01160
01161 #endif // !GTEST_OS_WINDOWS_MOBILE
01162
01163
01164
01165
01166 template <typename FunctionImpl>
01167 PolymorphicAction<internal::InvokeWithoutArgsAction<FunctionImpl> >
01168 InvokeWithoutArgs(FunctionImpl function_impl) {
01169 return MakePolymorphicAction(
01170 internal::InvokeWithoutArgsAction<FunctionImpl>(function_impl));
01171 }
01172
01173
01174
01175 template <class Class, typename MethodPtr>
01176 PolymorphicAction<internal::InvokeMethodWithoutArgsAction<Class, MethodPtr> >
01177 InvokeWithoutArgs(Class* obj_ptr, MethodPtr method_ptr) {
01178 return MakePolymorphicAction(
01179 internal::InvokeMethodWithoutArgsAction<Class, MethodPtr>(
01180 obj_ptr, method_ptr));
01181 }
01182
01183
01184
01185
01186 template <typename A>
01187 inline internal::IgnoreResultAction<A> IgnoreResult(const A& an_action) {
01188 return internal::IgnoreResultAction<A>(an_action);
01189 }
01190
01191
01192
01193
01194
01195
01196
01197
01198 template <typename T>
01199 inline internal::ReferenceWrapper<T> ByRef(T& l_value) {
01200 return internal::ReferenceWrapper<T>(l_value);
01201 }
01202
01203 }
01204
01205 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_