36#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
37#define GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
67template <
typename F1,
typename F2>
75template <
typename T,
bool kDefaultConstructible>
82 Assert(
false, __FILE__, __LINE__,
83 "Default action undefined for the function return type.");
103 return ::std::is_default_constructible<T>::value;
108 T, ::std::is_default_constructible<T>::value>
::Get();
139 static T*
Get() {
return NULL; }
144#define GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(type, value) \
146 class BuiltInDefaultValue<type> { \
148 static bool Exists() { return true; } \
149 static type Get() { return value; } \
153#if GTEST_HAS_GLOBAL_STRING
168#if GMOCK_WCHAR_T_IS_NATIVE_
183#undef GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_
207 producer_ =
new FixedValueProducer(x);
216 producer_ =
new FactoryValueProducer(factory);
226 static bool IsSet() {
return producer_ != NULL; }
238 return producer_ == NULL ?
243 class ValueProducer {
245 virtual ~ValueProducer() {}
246 virtual T Produce() = 0;
249 class FixedValueProducer :
public ValueProducer {
251 explicit FixedValueProducer(
T value) : value_(
value) {}
252 virtual T Produce() {
return value_; }
259 class FactoryValueProducer :
public ValueProducer {
262 : factory_(factory) {}
263 virtual T Produce() {
return factory_(); }
270 static ValueProducer* producer_;
289 static bool IsSet() {
return address_ != NULL; }
301 return address_ == NULL ?
320typename DefaultValue<T>::ValueProducer* DefaultValue<T>::producer_ = NULL;
324T* DefaultValue<T&>::address_ = NULL;
369 template <
typename G,
370 typename = typename ::std::enable_if<
371 ::std::is_constructible<::std::function<F>, G>
::value>::type>
372 Action(G&& fun) : fun_(::
std::forward<G>(fun)) {}
382 template <
typename Func>
388 return impl_ ==
nullptr && fun_ ==
nullptr;
390 return impl_ == NULL;
405 if (fun_ !=
nullptr) {
406 return internal::Apply(fun_, ::std::move(args));
409 return impl_->Perform(args);
413 template <
typename F1,
typename F2>
416 template <
typename G>
426 ::std::function<F> fun_;
452template <
typename Impl>
457 template <
typename F>
459 return Action<F>(
new MonomorphicImpl<F>(impl_));
463 template <
typename F>
469 explicit MonomorphicImpl(
const Impl& impl) : impl_(impl) {}
471 virtual Result Perform(
const ArgumentTuple& args) {
472 return impl_.template Perform<Result>(args);
500template <
typename Impl>
509template <
typename F1,
typename F2>
518 return impl_->Perform(args);
572 template <
typename F>
585 use_ReturnRef_instead_of_Return_to_return_a_reference);
586 return Action<F>(
new Impl<R, F>(value_));
591 template <
typename R_,
typename F>
605 : value_before_cast_(*
value),
608 virtual Result Perform(
const ArgumentTuple&) {
return value_; }
612 Result_cannot_be_a_reference_type);
615 R value_before_cast_;
623 template <
typename R_,
typename F>
624 class Impl<ByMoveWrapper<R_>, F> :
public ActionInterface<F> {
626 typedef typename Function<F>::Result Result;
627 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
629 explicit Impl(
const linked_ptr<R>& wrapper)
630 : performed_(false), wrapper_(wrapper) {}
632 virtual Result Perform(
const ArgumentTuple&) {
634 <<
"A ByMove() action should only be performed once.";
641 const linked_ptr<R> wrapper_;
646 const linked_ptr<R> value_;
657 template <
typename Result,
typename ArgumentTuple>
663 ReturnNull_can_be_used_to_return_a_pointer_only);
673 template <
typename Result,
typename ArgumentTuple>
690 template <
typename F>
697 use_Return_instead_of_ReturnRef_to_return_a_value);
703 template <
typename F>
709 explicit Impl(
T& ref) : ref_(ref) {}
711 virtual Result Perform(
const ArgumentTuple&) {
738 template <
typename F>
746 use_Return_instead_of_ReturnRefOfCopy_to_return_a_value);
752 template <
typename F>
760 virtual Result Perform(
const ArgumentTuple&) {
780 template <
typename F>
786template <
typename T1,
typename T2>
791 template <
typename Result,
typename ArgumentTuple>
803#if !GTEST_OS_WINDOWS_MOBILE
811 : errno_(errno_value),
813 template <
typename Result,
typename ArgumentTuple>
832template <
size_t N,
typename A,
bool kIsProto>
839 template <
typename Result,
typename ArgumentTuple>
840 void Perform(
const ArgumentTuple& args)
const {
842 *::testing::get<N>(args) = value_;
851template <
size_t N,
typename Proto>
859 proto_->CopyFrom(proto);
862 template <
typename Result,
typename ArgumentTuple>
863 void Perform(
const ArgumentTuple& args)
const {
865 ::testing::get<N>(args)->CopyFrom(*proto_);
879template <
typename FunctionImpl>
885 : function_impl_(function_impl) {}
889 template <
typename Result,
typename ArgumentTuple>
890 Result
Perform(
const ArgumentTuple&) {
return function_impl_(); }
893 FunctionImpl function_impl_;
899template <
class Class,
typename MethodPtr>
903 : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
905 template <
typename Result,
typename ArgumentTuple>
907 return (obj_ptr_->*method_ptr_)();
911 Class*
const obj_ptr_;
912 const MethodPtr method_ptr_;
918template <
typename CallbackType>
923 : callback_(callback) {
924 callback->CheckIsRepeatable();
930 template <
typename Result,
typename ArgumentTuple>
931 Result
Perform(
const ArgumentTuple&)
const {
return callback_->Run(); }
945 template <
typename F>
964 template <
typename F>
972 virtual void Perform(
const ArgumentTuple& args) {
980 typedef typename internal::Function<F>::MakeResultIgnoredValue
983 const Action<OriginalFunction> action_;
1000template <
typename T>
1008 operator T&()
const {
return *pointer_; }
1014template <
typename T>
1022template <
typename Action1,
typename Action2>
1026 : action1_(action1), action2_(action2) {}
1030 template <
typename F>
1032 return Action<F>(
new Impl<F>(action1_, action2_));
1037 template <
typename F>
1045 : action1_(action1), action2_(action2) {}
1047 virtual Result Perform(
const ArgumentTuple& args) {
1049 return action2_.Perform(args);
1053 const Action<VoidResult> action1_;
1054 const Action<F> action2_;
1103template <
typename To>
1104template <
typename From>
1110 impl_(from.impl_ == NULL ? NULL
1111 : new
internal::ActionAdaptor<To, From>(from)) {
1117template <
typename R>
1133template <
typename R>
1141template <
typename R>
1150template <
typename R>
1162template <
size_t N,
typename T>
1164 internal::SetArgumentPointeeAction<
1171#if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
1177 internal::SetArgumentPointeeAction<N, const char*, false> >
1180 N,
const char*,
false>(
p));
1185 internal::SetArgumentPointeeAction<N, const wchar_t*, false> >
1188 N,
const wchar_t*,
false>(
p));
1193template <
size_t N,
typename T>
1195 internal::SetArgumentPointeeAction<
1203template <
typename T1,
typename T2>
1208#if !GTEST_OS_WINDOWS_MOBILE
1211template <
typename T>
1212PolymorphicAction<internal::SetErrnoAndReturnAction<T> >
1223template <
typename FunctionImpl>
1224PolymorphicAction<internal::InvokeWithoutArgsAction<FunctionImpl> >
1232template <
class Class,
typename MethodPtr>
1233PolymorphicAction<internal::InvokeMethodWithoutArgsAction<Class, MethodPtr> >
1237 obj_ptr, method_ptr));
1243template <
typename A>
1255template <
typename T>
Result Perform(ArgumentTuple args) const
Action(ActionInterface< F > *impl)
Action(const Action< Func > &action)
internal::Function< F >::Result Result
internal::Function< F >::ArgumentTuple ArgumentTuple
internal::Function< F >::Result Result
virtual Result Perform(const ArgumentTuple &args)=0
internal::Function< F >::ArgumentTuple ArgumentTuple
virtual ~ActionInterface()
static void SetFactory(FactoryFunction factory)
PolymorphicAction(const Impl &impl)
internal::Function< F1 >::Result Result
ActionAdaptor(const Action< F2 > &from)
virtual Result Perform(const ArgumentTuple &args)
internal::Function< F1 >::ArgumentTuple ArgumentTuple
void Perform(const ArgumentTuple &) const
AssignAction(T1 *ptr, T2 value)
DoBothAction(Action1 action1, Action2 action2)
IgnoreResultAction(const A &action)
InvokeCallbackWithoutArgsAction(CallbackType *callback)
Result Perform(const ArgumentTuple &) const
Result Perform(const ArgumentTuple &) const
InvokeMethodWithoutArgsAction(Class *obj_ptr, MethodPtr method_ptr)
InvokeWithoutArgsAction(FunctionImpl function_impl)
Result Perform(const ArgumentTuple &)
ReferenceWrapper(T &l_value)
static Result Perform(const ArgumentTuple &)
ReturnRefOfCopyAction(const T &value)
static void Perform(const ArgumentTuple &)
void Perform(const ArgumentTuple &args) const
SetArgumentPointeeAction(const Proto &proto)
void Perform(const ArgumentTuple &args) const
SetArgumentPointeeAction(const A &value)
Result Perform(const ArgumentTuple &) const
SetErrnoAndReturnAction(int errno_value, T result)
static void Print(const T &value, ::std::ostream *os)
#define GTEST_CHECK_(condition)
#define GTEST_DISALLOW_ASSIGN_(type)
#define GTEST_COMPILE_ASSERT_(expr, msg)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
TypeWithSize< 8 >::Int Int64
void Assert(bool condition, const char *file, int line)
void PrintTo(const ReferenceWrapper< T > &ref, ::std::ostream *os)
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(void,)
const T & move(const T &t)
GTEST_API_ void IllegalDoDefault(const char *file, int line)
TypeWithSize< 8 >::UInt UInt64
internal::IgnoreResultAction< A > IgnoreResult(const A &an_action)
PolymorphicAction< internal::InvokeWithoutArgsAction< FunctionImpl > > InvokeWithoutArgs(FunctionImpl function_impl)
internal::ByMoveWrapper< R > ByMove(R x)
PolymorphicAction< Impl > MakePolymorphicAction(const Impl &impl)
PolymorphicAction< internal::ReturnVoidAction > Return()
internal::IgnoredValue Unused
PolymorphicAction< internal::AssignAction< T1, T2 > > Assign(T1 *ptr, T2 val)
PolymorphicAction< internal::SetErrnoAndReturnAction< T > > SetErrnoAndReturn(int errval, T result)
Action< F > MakeAction(ActionInterface< F > *impl)
PolymorphicAction< internal::SetArgumentPointeeAction< N, T, internal::IsAProtocolMessage< T >::value > > SetArgumentPointee(const T &x)
internal::ReturnRefOfCopyAction< R > ReturnRefOfCopy(const R &x)
internal::ReferenceWrapper< T > ByRef(T &l_value)
internal::ReturnRefAction< R > ReturnRef(R &x)
PolymorphicAction< internal::SetArgumentPointeeAction< N, T, internal::IsAProtocolMessage< T >::value > > SetArgPointee(const T &x)
internal::DoDefaultAction DoDefault()
PolymorphicAction< internal::ReturnNullAction > ReturnNull()
#define T(meth, val, expected)