Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gmock-actions.h
Go to the documentation of this file.
1 // Copyright 2007, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 
31 // Google Mock - a framework for writing C++ mock classes.
32 //
33 // The ACTION* family of macros can be used in a namespace scope to
34 // define custom actions easily. The syntax:
35 //
36 // ACTION(name) { statements; }
37 //
38 // will define an action with the given name that executes the
39 // statements. The value returned by the statements will be used as
40 // the return value of the action. Inside the statements, you can
41 // refer to the K-th (0-based) argument of the mock function by
42 // 'argK', and refer to its type by 'argK_type'. For example:
43 //
44 // ACTION(IncrementArg1) {
45 // arg1_type temp = arg1;
46 // return ++(*temp);
47 // }
48 //
49 // allows you to write
50 //
51 // ...WillOnce(IncrementArg1());
52 //
53 // You can also refer to the entire argument tuple and its type by
54 // 'args' and 'args_type', and refer to the mock function type and its
55 // return type by 'function_type' and 'return_type'.
56 //
57 // Note that you don't need to specify the types of the mock function
58 // arguments. However rest assured that your code is still type-safe:
59 // you'll get a compiler error if *arg1 doesn't support the ++
60 // operator, or if the type of ++(*arg1) isn't compatible with the
61 // mock function's return type, for example.
62 //
63 // Sometimes you'll want to parameterize the action. For that you can use
64 // another macro:
65 //
66 // ACTION_P(name, param_name) { statements; }
67 //
68 // For example:
69 //
70 // ACTION_P(Add, n) { return arg0 + n; }
71 //
72 // will allow you to write:
73 //
74 // ...WillOnce(Add(5));
75 //
76 // Note that you don't need to provide the type of the parameter
77 // either. If you need to reference the type of a parameter named
78 // 'foo', you can write 'foo_type'. For example, in the body of
79 // ACTION_P(Add, n) above, you can write 'n_type' to refer to the type
80 // of 'n'.
81 //
82 // We also provide ACTION_P2, ACTION_P3, ..., up to ACTION_P10 to support
83 // multi-parameter actions.
84 //
85 // For the purpose of typing, you can view
86 //
87 // ACTION_Pk(Foo, p1, ..., pk) { ... }
88 //
89 // as shorthand for
90 //
91 // template <typename p1_type, ..., typename pk_type>
92 // FooActionPk<p1_type, ..., pk_type> Foo(p1_type p1, ..., pk_type pk) { ... }
93 //
94 // In particular, you can provide the template type arguments
95 // explicitly when invoking Foo(), as in Foo<long, bool>(5, false);
96 // although usually you can rely on the compiler to infer the types
97 // for you automatically. You can assign the result of expression
98 // Foo(p1, ..., pk) to a variable of type FooActionPk<p1_type, ...,
99 // pk_type>. This can be useful when composing actions.
100 //
101 // You can also overload actions with different numbers of parameters:
102 //
103 // ACTION_P(Plus, a) { ... }
104 // ACTION_P2(Plus, a, b) { ... }
105 //
106 // While it's tempting to always use the ACTION* macros when defining
107 // a new action, you should also consider implementing ActionInterface
108 // or using MakePolymorphicAction() instead, especially if you need to
109 // use the action a lot. While these approaches require more work,
110 // they give you more control on the types of the mock function
111 // arguments and the action parameters, which in general leads to
112 // better compiler error messages that pay off in the long run. They
113 // also allow overloading actions based on parameter types (as opposed
114 // to just based on the number of parameters).
115 //
116 // CAVEAT:
117 //
118 // ACTION*() can only be used in a namespace scope as templates cannot be
119 // declared inside of a local class.
120 // Users can, however, define any local functors (e.g. a lambda) that
121 // can be used as actions.
122 //
123 // MORE INFORMATION:
124 //
125 // To learn more about using these macros, please search for 'ACTION' on
126 // https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md
127 
128 // GOOGLETEST_CM0002 DO NOT DELETE
129 
130 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
131 #define GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
132 
133 #ifndef _WIN32_WCE
134 # include <errno.h>
135 #endif
136 
137 #include <algorithm>
138 #include <functional>
139 #include <memory>
140 #include <string>
141 #include <tuple>
142 #include <type_traits>
143 #include <utility>
144 
147 #include "gmock/internal/gmock-pp.h"
148 
149 #ifdef _MSC_VER
150 # pragma warning(push)
151 # pragma warning(disable:4100)
152 #endif
153 
154 namespace testing {
155 
156 // To implement an action Foo, define:
157 // 1. a class FooAction that implements the ActionInterface interface, and
158 // 2. a factory function that creates an Action object from a
159 // const FooAction*.
160 //
161 // The two-level delegation design follows that of Matcher, providing
162 // consistency for extension developers. It also eases ownership
163 // management as Action objects can now be copied like plain values.
164 
165 namespace internal {
166 
167 // BuiltInDefaultValueGetter<T, true>::Get() returns a
168 // default-constructed T value. BuiltInDefaultValueGetter<T,
169 // false>::Get() crashes with an error.
170 //
171 // This primary template is used when kDefaultConstructible is true.
172 template <typename T, bool kDefaultConstructible>
174  static T Get() { return T(); }
175 };
176 template <typename T>
178  static T Get() {
179  Assert(false, __FILE__, __LINE__,
180  "Default action undefined for the function return type.");
181  return internal::Invalid<T>();
182  // The above statement will never be reached, but is required in
183  // order for this function to compile.
184  }
185 };
186 
187 // BuiltInDefaultValue<T>::Get() returns the "built-in" default value
188 // for type T, which is NULL when T is a raw pointer type, 0 when T is
189 // a numeric type, false when T is bool, or "" when T is string or
190 // std::string. In addition, in C++11 and above, it turns a
191 // default-constructed T value if T is default constructible. For any
192 // other type T, the built-in default T value is undefined, and the
193 // function will abort the process.
194 template <typename T>
196  public:
197  // This function returns true if and only if type T has a built-in default
198  // value.
199  static bool Exists() {
201  }
202 
203  static T Get() {
206  }
207 };
208 
209 // This partial specialization says that we use the same built-in
210 // default value for T and const T.
211 template <typename T>
212 class BuiltInDefaultValue<const T> {
213  public:
214  static bool Exists() { return BuiltInDefaultValue<T>::Exists(); }
215  static T Get() { return BuiltInDefaultValue<T>::Get(); }
216 };
217 
218 // This partial specialization defines the default values for pointer
219 // types.
220 template <typename T>
222  public:
223  static bool Exists() { return true; }
224  static T* Get() { return nullptr; }
225 };
226 
227 // The following specializations define the default values for
228 // specific types we care about.
229 #define GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(type, value) \
230  template <> \
231  class BuiltInDefaultValue<type> { \
232  public: \
233  static bool Exists() { return true; } \
234  static type Get() { return value; } \
235  }
236 
243 
244 // There's no need for a default action for signed wchar_t, as that
245 // type is the same as wchar_t for gcc, and invalid for MSVC.
246 //
247 // There's also no need for a default action for unsigned wchar_t, as
248 // that type is the same as unsigned int for gcc, and invalid for
249 // MSVC.
250 #if GMOCK_WCHAR_T_IS_NATIVE_
251 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(wchar_t, 0U); // NOLINT
252 #endif
253 
254 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned short, 0U); // NOLINT
255 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed short, 0); // NOLINT
258 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned long, 0UL); // NOLINT
259 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed long, 0L); // NOLINT
260 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned long long, 0); // NOLINT
261 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed long long, 0); // NOLINT
264 
265 #undef GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_
266 
267 // Simple two-arg form of std::disjunction.
268 template <typename P, typename Q>
269 using disjunction = typename ::std::conditional<P::value, P, Q>::type;
270 
271 } // namespace internal
272 
273 // When an unexpected function call is encountered, Google Mock will
274 // let it return a default value if the user has specified one for its
275 // return type, or if the return type has a built-in default value;
276 // otherwise Google Mock won't know what value to return and will have
277 // to abort the process.
278 //
279 // The DefaultValue<T> class allows a user to specify the
280 // default value for a type T that is both copyable and publicly
281 // destructible (i.e. anything that can be used as a function return
282 // type). The usage is:
283 //
284 // // Sets the default value for type T to be foo.
285 // DefaultValue<T>::Set(foo);
286 template <typename T>
288  public:
289  // Sets the default value for type T; requires T to be
290  // copy-constructable and have a public destructor.
291  static void Set(T x) {
292  delete producer_;
293  producer_ = new FixedValueProducer(x);
294  }
295 
296  // Provides a factory function to be called to generate the default value.
297  // This method can be used even if T is only move-constructible, but it is not
298  // limited to that case.
299  typedef T (*FactoryFunction)();
300  static void SetFactory(FactoryFunction factory) {
301  delete producer_;
302  producer_ = new FactoryValueProducer(factory);
303  }
304 
305  // Unsets the default value for type T.
306  static void Clear() {
307  delete producer_;
308  producer_ = nullptr;
309  }
310 
311  // Returns true if and only if the user has set the default value for type T.
312  static bool IsSet() { return producer_ != nullptr; }
313 
314  // Returns true if T has a default return value set by the user or there
315  // exists a built-in default value.
316  static bool Exists() {
318  }
319 
320  // Returns the default value for type T if the user has set one;
321  // otherwise returns the built-in default value. Requires that Exists()
322  // is true, which ensures that the return value is well-defined.
323  static T Get() {
325  : producer_->Produce();
326  }
327 
328  private:
330  public:
331  virtual ~ValueProducer() {}
332  virtual T Produce() = 0;
333  };
334 
336  public:
337  explicit FixedValueProducer(T value) : value_(value) {}
338  T Produce() override { return value_; }
339 
340  private:
341  const T value_;
343  };
344 
346  public:
348  : factory_(factory) {}
349  T Produce() override { return factory_(); }
350 
351  private:
354  };
355 
357 };
358 
359 // This partial specialization allows a user to set default values for
360 // reference types.
361 template <typename T>
362 class DefaultValue<T&> {
363  public:
364  // Sets the default value for type T&.
365  static void Set(T& x) { // NOLINT
366  address_ = &x;
367  }
368 
369  // Unsets the default value for type T&.
370  static void Clear() { address_ = nullptr; }
371 
372  // Returns true if and only if the user has set the default value for type T&.
373  static bool IsSet() { return address_ != nullptr; }
374 
375  // Returns true if T has a default return value set by the user or there
376  // exists a built-in default value.
377  static bool Exists() {
378  return IsSet() || internal::BuiltInDefaultValue<T&>::Exists();
379  }
380 
381  // Returns the default value for type T& if the user has set one;
382  // otherwise returns the built-in default value if there is one;
383  // otherwise aborts the process.
384  static T& Get() {
385  return address_ == nullptr ? internal::BuiltInDefaultValue<T&>::Get()
386  : *address_;
387  }
388 
389  private:
390  static T* address_;
391 };
392 
393 // This specialization allows DefaultValue<void>::Get() to
394 // compile.
395 template <>
397  public:
398  static bool Exists() { return true; }
399  static void Get() {}
400 };
401 
402 // Points to the user-set default value for type T.
403 template <typename T>
404 typename DefaultValue<T>::ValueProducer* DefaultValue<T>::producer_ = nullptr;
405 
406 // Points to the user-set default value for type T&.
407 template <typename T>
408 T* DefaultValue<T&>::address_ = nullptr;
409 
410 // Implement this interface to define an action for function type F.
411 template <typename F>
413  public:
416 
418  virtual ~ActionInterface() {}
419 
420  // Performs the action. This method is not const, as in general an
421  // action can have side effects and be stateful. For example, a
422  // get-the-next-element-from-the-collection action will need to
423  // remember the current element.
424  virtual Result Perform(const ArgumentTuple& args) = 0;
425 
426  private:
428 };
429 
430 // An Action<F> is a copyable and IMMUTABLE (except by assignment)
431 // object that represents an action to be taken when a mock function
432 // of type F is called. The implementation of Action<T> is just a
433 // std::shared_ptr to const ActionInterface<T>. Don't inherit from Action!
434 // You can view an object implementing ActionInterface<F> as a
435 // concrete action (including its current state), and an Action<F>
436 // object as a handle to it.
437 template <typename F>
438 class Action {
439  // Adapter class to allow constructing Action from a legacy ActionInterface.
440  // New code should create Actions from functors instead.
441  struct ActionAdapter {
442  // Adapter must be copyable to satisfy std::function requirements.
443  ::std::shared_ptr<ActionInterface<F>> impl_;
444 
445  template <typename... Args>
446  typename internal::Function<F>::Result operator()(Args&&... args) {
447  return impl_->Perform(
448  ::std::forward_as_tuple(::std::forward<Args>(args)...));
449  }
450  };
451 
452  public:
455 
456  // Constructs a null Action. Needed for storing Action objects in
457  // STL containers.
458  Action() {}
459 
460  // Construct an Action from a specified callable.
461  // This cannot take std::function directly, because then Action would not be
462  // directly constructible from lambda (it would require two conversions).
463  template <typename G,
464  typename IsCompatibleFunctor =
465  ::std::is_constructible<::std::function<F>, G>,
466  typename IsNoArgsFunctor =
467  ::std::is_constructible<::std::function<Result()>, G>,
468  typename = typename ::std::enable_if<internal::disjunction<
469  IsCompatibleFunctor, IsNoArgsFunctor>::value>::type>
470  Action(G&& fun) { // NOLINT
471  Init(::std::forward<G>(fun), IsCompatibleFunctor());
472  }
473 
474  // Constructs an Action from its implementation.
475  explicit Action(ActionInterface<F>* impl)
476  : fun_(ActionAdapter{::std::shared_ptr<ActionInterface<F>>(impl)}) {}
477 
478  // This constructor allows us to turn an Action<Func> object into an
479  // Action<F>, as long as F's arguments can be implicitly converted
480  // to Func's and Func's return type can be implicitly converted to F's.
481  template <typename Func>
482  explicit Action(const Action<Func>& action) : fun_(action.fun_) {}
483 
484  // Returns true if and only if this is the DoDefault() action.
485  bool IsDoDefault() const { return fun_ == nullptr; }
486 
487  // Performs the action. Note that this method is const even though
488  // the corresponding method in ActionInterface is not. The reason
489  // is that a const Action<F> means that it cannot be re-bound to
490  // another concrete action, not that the concrete action it binds to
491  // cannot change state. (Think of the difference between a const
492  // pointer and a pointer to const.)
494  if (IsDoDefault()) {
495  internal::IllegalDoDefault(__FILE__, __LINE__);
496  }
497  return internal::Apply(fun_, ::std::move(args));
498  }
499 
500  private:
501  template <typename G>
502  friend class Action;
503 
504  template <typename G>
505  void Init(G&& g, ::std::true_type) {
506  fun_ = ::std::forward<G>(g);
507  }
508 
509  template <typename G>
510  void Init(G&& g, ::std::false_type) {
511  fun_ = IgnoreArgs<typename ::std::decay<G>::type>{::std::forward<G>(g)};
512  }
513 
514  template <typename FunctionImpl>
515  struct IgnoreArgs {
516  template <typename... Args>
517  Result operator()(const Args&...) const {
518  return function_impl();
519  }
520 
521  FunctionImpl function_impl;
522  };
523 
524  // fun_ is an empty function if and only if this is the DoDefault() action.
525  ::std::function<F> fun_;
526 };
527 
528 // The PolymorphicAction class template makes it easy to implement a
529 // polymorphic action (i.e. an action that can be used in mock
530 // functions of than one type, e.g. Return()).
531 //
532 // To define a polymorphic action, a user first provides a COPYABLE
533 // implementation class that has a Perform() method template:
534 //
535 // class FooAction {
536 // public:
537 // template <typename Result, typename ArgumentTuple>
538 // Result Perform(const ArgumentTuple& args) const {
539 // // Processes the arguments and returns a result, using
540 // // std::get<N>(args) to get the N-th (0-based) argument in the tuple.
541 // }
542 // ...
543 // };
544 //
545 // Then the user creates the polymorphic action using
546 // MakePolymorphicAction(object) where object has type FooAction. See
547 // the definition of Return(void) and SetArgumentPointee<N>(value) for
548 // complete examples.
549 template <typename Impl>
551  public:
552  explicit PolymorphicAction(const Impl& impl) : impl_(impl) {}
553 
554  template <typename F>
555  operator Action<F>() const {
556  return Action<F>(new MonomorphicImpl<F>(impl_));
557  }
558 
559  private:
560  template <typename F>
561  class MonomorphicImpl : public ActionInterface<F> {
562  public:
565 
566  explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
567 
568  Result Perform(const ArgumentTuple& args) override {
569  return impl_.template Perform<Result>(args);
570  }
571 
572  private:
573  Impl impl_;
574  };
575 
576  Impl impl_;
577 };
578 
579 // Creates an Action from its implementation and returns it. The
580 // created Action object owns the implementation.
581 template <typename F>
583  return Action<F>(impl);
584 }
585 
586 // Creates a polymorphic action from its implementation. This is
587 // easier to use than the PolymorphicAction<Impl> constructor as it
588 // doesn't require you to explicitly write the template argument, e.g.
589 //
590 // MakePolymorphicAction(foo);
591 // vs
592 // PolymorphicAction<TypeOfFoo>(foo);
593 template <typename Impl>
595  return PolymorphicAction<Impl>(impl);
596 }
597 
598 namespace internal {
599 
600 // Helper struct to specialize ReturnAction to execute a move instead of a copy
601 // on return. Useful for move-only types, but could be used on any type.
602 template <typename T>
604  explicit ByMoveWrapper(T value) : payload(std::move(value)) {}
606 };
607 
608 // Implements the polymorphic Return(x) action, which can be used in
609 // any function that returns the type of x, regardless of the argument
610 // types.
611 //
612 // Note: The value passed into Return must be converted into
613 // Function<F>::Result when this action is cast to Action<F> rather than
614 // when that action is performed. This is important in scenarios like
615 //
616 // MOCK_METHOD1(Method, T(U));
617 // ...
618 // {
619 // Foo foo;
620 // X x(&foo);
621 // EXPECT_CALL(mock, Method(_)).WillOnce(Return(x));
622 // }
623 //
624 // In the example above the variable x holds reference to foo which leaves
625 // scope and gets destroyed. If copying X just copies a reference to foo,
626 // that copy will be left with a hanging reference. If conversion to T
627 // makes a copy of foo, the above code is safe. To support that scenario, we
628 // need to make sure that the type conversion happens inside the EXPECT_CALL
629 // statement, and conversion of the result of Return to Action<T(U)> is a
630 // good place for that.
631 //
632 // The real life example of the above scenario happens when an invocation
633 // of gtl::Container() is passed into Return.
634 //
635 template <typename R>
637  public:
638  // Constructs a ReturnAction object from the value to be returned.
639  // 'value' is passed by value instead of by const reference in order
640  // to allow Return("string literal") to compile.
641  explicit ReturnAction(R value) : value_(new R(std::move(value))) {}
642 
643  // This template type conversion operator allows Return(x) to be
644  // used in ANY function that returns x's type.
645  template <typename F>
646  operator Action<F>() const { // NOLINT
647  // Assert statement belongs here because this is the best place to verify
648  // conditions on F. It produces the clearest error messages
649  // in most compilers.
650  // Impl really belongs in this scope as a local class but can't
651  // because MSVC produces duplicate symbols in different translation units
652  // in this case. Until MS fixes that bug we put Impl into the class scope
653  // and put the typedef both here (for use in assert statement) and
654  // in the Impl class. But both definitions must be the same.
655  typedef typename Function<F>::Result Result;
658  use_ReturnRef_instead_of_Return_to_return_a_reference);
659  static_assert(!std::is_void<Result>::value,
660  "Can't use Return() on an action expected to return `void`.");
661  return Action<F>(new Impl<R, F>(value_));
662  }
663 
664  private:
665  // Implements the Return(x) action for a particular function type F.
666  template <typename R_, typename F>
667  class Impl : public ActionInterface<F> {
668  public:
669  typedef typename Function<F>::Result Result;
671 
672  // The implicit cast is necessary when Result has more than one
673  // single-argument constructor (e.g. Result is std::vector<int>) and R
674  // has a type conversion operator template. In that case, value_(value)
675  // won't compile as the compiler doesn't known which constructor of
676  // Result to call. ImplicitCast_ forces the compiler to convert R to
677  // Result without considering explicit constructors, thus resolving the
678  // ambiguity. value_ is then initialized using its copy constructor.
679  explicit Impl(const std::shared_ptr<R>& value)
680  : value_before_cast_(*value),
681  value_(ImplicitCast_<Result>(value_before_cast_)) {}
682 
683  Result Perform(const ArgumentTuple&) override { return value_; }
684 
685  private:
687  Result_cannot_be_a_reference_type);
688  // We save the value before casting just in case it is being cast to a
689  // wrapper type.
692 
694  };
695 
696  // Partially specialize for ByMoveWrapper. This version of ReturnAction will
697  // move its contents instead.
698  template <typename R_, typename F>
699  class Impl<ByMoveWrapper<R_>, F> : public ActionInterface<F> {
700  public:
701  typedef typename Function<F>::Result Result;
703 
704  explicit Impl(const std::shared_ptr<R>& wrapper)
705  : performed_(false), wrapper_(wrapper) {}
706 
707  Result Perform(const ArgumentTuple&) override {
708  GTEST_CHECK_(!performed_)
709  << "A ByMove() action should only be performed once.";
710  performed_ = true;
711  return std::move(wrapper_->payload);
712  }
713 
714  private:
716  const std::shared_ptr<R> wrapper_;
717  };
718 
719  const std::shared_ptr<R> value_;
720 };
721 
722 // Implements the ReturnNull() action.
724  public:
725  // Allows ReturnNull() to be used in any pointer-returning function. In C++11
726  // this is enforced by returning nullptr, and in non-C++11 by asserting a
727  // pointer type on compile time.
728  template <typename Result, typename ArgumentTuple>
729  static Result Perform(const ArgumentTuple&) {
730  return nullptr;
731  }
732 };
733 
734 // Implements the Return() action.
736  public:
737  // Allows Return() to be used in any void-returning function.
738  template <typename Result, typename ArgumentTuple>
739  static void Perform(const ArgumentTuple&) {
740  static_assert(std::is_void<Result>::value, "Result should be void.");
741  }
742 };
743 
744 // Implements the polymorphic ReturnRef(x) action, which can be used
745 // in any function that returns a reference to the type of x,
746 // regardless of the argument types.
747 template <typename T>
749  public:
750  // Constructs a ReturnRefAction object from the reference to be returned.
751  explicit ReturnRefAction(T& ref) : ref_(ref) {} // NOLINT
752 
753  // This template type conversion operator allows ReturnRef(x) to be
754  // used in ANY function that returns a reference to x's type.
755  template <typename F>
756  operator Action<F>() const {
757  typedef typename Function<F>::Result Result;
758  // Asserts that the function return type is a reference. This
759  // catches the user error of using ReturnRef(x) when Return(x)
760  // should be used, and generates some helpful error message.
762  use_Return_instead_of_ReturnRef_to_return_a_value);
763  return Action<F>(new Impl<F>(ref_));
764  }
765 
766  private:
767  // Implements the ReturnRef(x) action for a particular function type F.
768  template <typename F>
769  class Impl : public ActionInterface<F> {
770  public:
771  typedef typename Function<F>::Result Result;
773 
774  explicit Impl(T& ref) : ref_(ref) {} // NOLINT
775 
776  Result Perform(const ArgumentTuple&) override { return ref_; }
777 
778  private:
779  T& ref_;
780  };
781 
782  T& ref_;
783 };
784 
785 // Implements the polymorphic ReturnRefOfCopy(x) action, which can be
786 // used in any function that returns a reference to the type of x,
787 // regardless of the argument types.
788 template <typename T>
790  public:
791  // Constructs a ReturnRefOfCopyAction object from the reference to
792  // be returned.
793  explicit ReturnRefOfCopyAction(const T& value) : value_(value) {} // NOLINT
794 
795  // This template type conversion operator allows ReturnRefOfCopy(x) to be
796  // used in ANY function that returns a reference to x's type.
797  template <typename F>
798  operator Action<F>() const {
799  typedef typename Function<F>::Result Result;
800  // Asserts that the function return type is a reference. This
801  // catches the user error of using ReturnRefOfCopy(x) when Return(x)
802  // should be used, and generates some helpful error message.
805  use_Return_instead_of_ReturnRefOfCopy_to_return_a_value);
806  return Action<F>(new Impl<F>(value_));
807  }
808 
809  private:
810  // Implements the ReturnRefOfCopy(x) action for a particular function type F.
811  template <typename F>
812  class Impl : public ActionInterface<F> {
813  public:
814  typedef typename Function<F>::Result Result;
816 
817  explicit Impl(const T& value) : value_(value) {} // NOLINT
818 
819  Result Perform(const ArgumentTuple&) override { return value_; }
820 
821  private:
823  };
824 
825  const T value_;
826 };
827 
828 // Implements the polymorphic ReturnRoundRobin(v) action, which can be
829 // used in any function that returns the element_type of v.
830 template <typename T>
832  public:
833  explicit ReturnRoundRobinAction(std::vector<T> values) {
834  GTEST_CHECK_(!values.empty())
835  << "ReturnRoundRobin requires at least one element.";
836  state_->values = std::move(values);
837  }
838 
839  template <typename... Args>
840  T operator()(Args&&...) const {
841  return state_->Next();
842  }
843 
844  private:
845  struct State {
846  T Next() {
847  T ret_val = values[i++];
848  if (i == values.size()) i = 0;
849  return ret_val;
850  }
851 
852  std::vector<T> values;
853  size_t i = 0;
854  };
855  std::shared_ptr<State> state_ = std::make_shared<State>();
856 };
857 
858 // Implements the polymorphic DoDefault() action.
860  public:
861  // This template type conversion operator allows DoDefault() to be
862  // used in any function.
863  template <typename F>
864  operator Action<F>() const { return Action<F>(); } // NOLINT
865 };
866 
867 // Implements the Assign action to set a given pointer referent to a
868 // particular value.
869 template <typename T1, typename T2>
871  public:
872  AssignAction(T1* ptr, T2 value) : ptr_(ptr), value_(value) {}
873 
874  template <typename Result, typename ArgumentTuple>
875  void Perform(const ArgumentTuple& /* args */) const {
876  *ptr_ = value_;
877  }
878 
879  private:
880  T1* const ptr_;
881  const T2 value_;
882 };
883 
884 #if !GTEST_OS_WINDOWS_MOBILE
885 
886 // Implements the SetErrnoAndReturn action to simulate return from
887 // various system calls and libc functions.
888 template <typename T>
890  public:
891  SetErrnoAndReturnAction(int errno_value, T result)
892  : errno_(errno_value),
893  result_(result) {}
894  template <typename Result, typename ArgumentTuple>
895  Result Perform(const ArgumentTuple& /* args */) const {
896  errno = errno_;
897  return result_;
898  }
899 
900  private:
901  const int errno_;
902  const T result_;
903 };
904 
905 #endif // !GTEST_OS_WINDOWS_MOBILE
906 
907 // Implements the SetArgumentPointee<N>(x) action for any function
908 // whose N-th argument (0-based) is a pointer to x's type.
909 template <size_t N, typename A, typename = void>
912 
913  template <typename... Args>
914  void operator()(const Args&... args) const {
915  *::std::get<N>(std::tie(args...)) = value;
916  }
917 };
918 
919 // Implements the Invoke(object_ptr, &Class::Method) action.
920 template <class Class, typename MethodPtr>
922  Class* const obj_ptr;
923  const MethodPtr method_ptr;
924 
925  template <typename... Args>
926  auto operator()(Args&&... args) const
927  -> decltype((obj_ptr->*method_ptr)(std::forward<Args>(args)...)) {
928  return (obj_ptr->*method_ptr)(std::forward<Args>(args)...);
929  }
930 };
931 
932 // Implements the InvokeWithoutArgs(f) action. The template argument
933 // FunctionImpl is the implementation type of f, which can be either a
934 // function pointer or a functor. InvokeWithoutArgs(f) can be used as an
935 // Action<F> as long as f's type is compatible with F.
936 template <typename FunctionImpl>
938  FunctionImpl function_impl;
939 
940  // Allows InvokeWithoutArgs(f) to be used as any action whose type is
941  // compatible with f.
942  template <typename... Args>
943  auto operator()(const Args&...) -> decltype(function_impl()) {
944  return function_impl();
945  }
946 };
947 
948 // Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action.
949 template <class Class, typename MethodPtr>
951  Class* const obj_ptr;
952  const MethodPtr method_ptr;
953 
954  using ReturnType =
955  decltype((std::declval<Class*>()->*std::declval<MethodPtr>())());
956 
957  template <typename... Args>
958  ReturnType operator()(const Args&...) const {
959  return (obj_ptr->*method_ptr)();
960  }
961 };
962 
963 // Implements the IgnoreResult(action) action.
964 template <typename A>
966  public:
967  explicit IgnoreResultAction(const A& action) : action_(action) {}
968 
969  template <typename F>
970  operator Action<F>() const {
971  // Assert statement belongs here because this is the best place to verify
972  // conditions on F. It produces the clearest error messages
973  // in most compilers.
974  // Impl really belongs in this scope as a local class but can't
975  // because MSVC produces duplicate symbols in different translation units
976  // in this case. Until MS fixes that bug we put Impl into the class scope
977  // and put the typedef both here (for use in assert statement) and
978  // in the Impl class. But both definitions must be the same.
979  typedef typename internal::Function<F>::Result Result;
980 
981  // Asserts at compile time that F returns void.
982  static_assert(std::is_void<Result>::value, "Result type should be void.");
983 
984  return Action<F>(new Impl<F>(action_));
985  }
986 
987  private:
988  template <typename F>
989  class Impl : public ActionInterface<F> {
990  public:
993 
994  explicit Impl(const A& action) : action_(action) {}
995 
996  void Perform(const ArgumentTuple& args) override {
997  // Performs the action and ignores its result.
998  action_.Perform(args);
999  }
1000 
1001  private:
1002  // Type OriginalFunction is the same as F except that its return
1003  // type is IgnoredValue.
1006 
1008  };
1009 
1010  const A action_;
1011 };
1012 
1013 template <typename InnerAction, size_t... I>
1015  InnerAction action;
1016 
1017  // The inner action could be anything convertible to Action<X>.
1018  // We use the conversion operator to detect the signature of the inner Action.
1019  template <typename R, typename... Args>
1020  operator Action<R(Args...)>() const { // NOLINT
1021  using TupleType = std::tuple<Args...>;
1023  converted(action);
1024 
1025  return [converted](Args... args) -> R {
1026  return converted.Perform(std::forward_as_tuple(
1027  std::get<I>(std::forward_as_tuple(std::forward<Args>(args)...))...));
1028  };
1029  }
1030 };
1031 
1032 template <typename... Actions>
1033 struct DoAllAction {
1034  private:
1035  template <typename... Args, size_t... I>
1036  std::vector<Action<void(Args...)>> Convert(IndexSequence<I...>) const {
1037  return {std::get<I>(actions)...};
1038  }
1039 
1040  public:
1041  std::tuple<Actions...> actions;
1042 
1043  template <typename R, typename... Args>
1044  operator Action<R(Args...)>() const { // NOLINT
1045  struct Op {
1046  std::vector<Action<void(Args...)>> converted;
1047  Action<R(Args...)> last;
1048  R operator()(Args... args) const {
1049  auto tuple_args = std::forward_as_tuple(std::forward<Args>(args)...);
1050  for (auto& a : converted) {
1051  a.Perform(tuple_args);
1052  }
1053  return last.Perform(tuple_args);
1054  }
1055  };
1056  return Op{Convert<Args...>(MakeIndexSequence<sizeof...(Actions) - 1>()),
1057  std::get<sizeof...(Actions) - 1>(actions)};
1058  }
1059 };
1060 
1061 } // namespace internal
1062 
1063 // An Unused object can be implicitly constructed from ANY value.
1064 // This is handy when defining actions that ignore some or all of the
1065 // mock function arguments. For example, given
1066 //
1067 // MOCK_METHOD3(Foo, double(const string& label, double x, double y));
1068 // MOCK_METHOD3(Bar, double(int index, double x, double y));
1069 //
1070 // instead of
1071 //
1072 // double DistanceToOriginWithLabel(const string& label, double x, double y) {
1073 // return sqrt(x*x + y*y);
1074 // }
1075 // double DistanceToOriginWithIndex(int index, double x, double y) {
1076 // return sqrt(x*x + y*y);
1077 // }
1078 // ...
1079 // EXPECT_CALL(mock, Foo("abc", _, _))
1080 // .WillOnce(Invoke(DistanceToOriginWithLabel));
1081 // EXPECT_CALL(mock, Bar(5, _, _))
1082 // .WillOnce(Invoke(DistanceToOriginWithIndex));
1083 //
1084 // you could write
1085 //
1086 // // We can declare any uninteresting argument as Unused.
1087 // double DistanceToOrigin(Unused, double x, double y) {
1088 // return sqrt(x*x + y*y);
1089 // }
1090 // ...
1091 // EXPECT_CALL(mock, Foo("abc", _, _)).WillOnce(Invoke(DistanceToOrigin));
1092 // EXPECT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin));
1094 
1095 // Creates an action that does actions a1, a2, ..., sequentially in
1096 // each invocation.
1097 template <typename... Action>
1099  Action&&... action) {
1100  return {std::forward_as_tuple(std::forward<Action>(action)...)};
1101 }
1102 
1103 // WithArg<k>(an_action) creates an action that passes the k-th
1104 // (0-based) argument of the mock function to an_action and performs
1105 // it. It adapts an action accepting one argument to one that accepts
1106 // multiple arguments. For convenience, we also provide
1107 // WithArgs<k>(an_action) (defined below) as a synonym.
1108 template <size_t k, typename InnerAction>
1109 internal::WithArgsAction<typename std::decay<InnerAction>::type, k>
1110 WithArg(InnerAction&& action) {
1111  return {std::forward<InnerAction>(action)};
1112 }
1113 
1114 // WithArgs<N1, N2, ..., Nk>(an_action) creates an action that passes
1115 // the selected arguments of the mock function to an_action and
1116 // performs it. It serves as an adaptor between actions with
1117 // different argument lists.
1118 template <size_t k, size_t... ks, typename InnerAction>
1120 WithArgs(InnerAction&& action) {
1121  return {std::forward<InnerAction>(action)};
1122 }
1123 
1124 // WithoutArgs(inner_action) can be used in a mock function with a
1125 // non-empty argument list to perform inner_action, which takes no
1126 // argument. In other words, it adapts an action accepting no
1127 // argument to one that accepts (and ignores) arguments.
1128 template <typename InnerAction>
1129 internal::WithArgsAction<typename std::decay<InnerAction>::type>
1130 WithoutArgs(InnerAction&& action) {
1131  return {std::forward<InnerAction>(action)};
1132 }
1133 
1134 // Creates an action that returns 'value'. 'value' is passed by value
1135 // instead of const reference - otherwise Return("string literal")
1136 // will trigger a compiler error about using array as initializer.
1137 template <typename R>
1139  return internal::ReturnAction<R>(std::move(value));
1140 }
1141 
1142 // Creates an action that returns NULL.
1145 }
1146 
1147 // Creates an action that returns from a void function.
1150 }
1151 
1152 // Creates an action that returns the reference to a variable.
1153 template <typename R>
1156 }
1157 
1158 // Prevent using ReturnRef on reference to temporary.
1159 template <typename R, R* = nullptr>
1160 internal::ReturnRefAction<R> ReturnRef(R&&) = delete;
1161 
1162 // Creates an action that returns the reference to a copy of the
1163 // argument. The copy is created when the action is constructed and
1164 // lives as long as the action.
1165 template <typename R>
1168 }
1169 
1170 // Modifies the parent action (a Return() action) to perform a move of the
1171 // argument instead of a copy.
1172 // Return(ByMove()) actions can only be executed once and will assert this
1173 // invariant.
1174 template <typename R>
1176  return internal::ByMoveWrapper<R>(std::move(x));
1177 }
1178 
1179 // Creates an action that returns an element of `vals`. Calling this action will
1180 // repeatedly return the next value from `vals` until it reaches the end and
1181 // will restart from the beginning.
1182 template <typename T>
1184  return internal::ReturnRoundRobinAction<T>(std::move(vals));
1185 }
1186 
1187 // Creates an action that returns an element of `vals`. Calling this action will
1188 // repeatedly return the next value from `vals` until it reaches the end and
1189 // will restart from the beginning.
1190 template <typename T>
1192  std::initializer_list<T> vals) {
1193  return internal::ReturnRoundRobinAction<T>(std::vector<T>(vals));
1194 }
1195 
1196 // Creates an action that does the default action for the give mock function.
1198  return internal::DoDefaultAction();
1199 }
1200 
1201 // Creates an action that sets the variable pointed by the N-th
1202 // (0-based) function argument to 'value'.
1203 template <size_t N, typename T>
1205  return {std::move(value)};
1206 }
1207 
1208 // The following version is DEPRECATED.
1209 template <size_t N, typename T>
1211  return {std::move(value)};
1212 }
1213 
1214 // Creates an action that sets a pointer referent to a given value.
1215 template <typename T1, typename T2>
1218 }
1219 
1220 #if !GTEST_OS_WINDOWS_MOBILE
1221 
1222 // Creates an action that sets errno and returns the appropriate error.
1223 template <typename T>
1224 PolymorphicAction<internal::SetErrnoAndReturnAction<T> >
1225 SetErrnoAndReturn(int errval, T result) {
1226  return MakePolymorphicAction(
1227  internal::SetErrnoAndReturnAction<T>(errval, result));
1228 }
1229 
1230 #endif // !GTEST_OS_WINDOWS_MOBILE
1231 
1232 // Various overloads for Invoke().
1233 
1234 // Legacy function.
1235 // Actions can now be implicitly constructed from callables. No need to create
1236 // wrapper objects.
1237 // This function exists for backwards compatibility.
1238 template <typename FunctionImpl>
1239 typename std::decay<FunctionImpl>::type Invoke(FunctionImpl&& function_impl) {
1240  return std::forward<FunctionImpl>(function_impl);
1241 }
1242 
1243 // Creates an action that invokes the given method on the given object
1244 // with the mock function's arguments.
1245 template <class Class, typename MethodPtr>
1247  MethodPtr method_ptr) {
1248  return {obj_ptr, method_ptr};
1249 }
1250 
1251 // Creates an action that invokes 'function_impl' with no argument.
1252 template <typename FunctionImpl>
1253 internal::InvokeWithoutArgsAction<typename std::decay<FunctionImpl>::type>
1254 InvokeWithoutArgs(FunctionImpl function_impl) {
1255  return {std::move(function_impl)};
1256 }
1257 
1258 // Creates an action that invokes the given method on the given object
1259 // with no argument.
1260 template <class Class, typename MethodPtr>
1262  Class* obj_ptr, MethodPtr method_ptr) {
1263  return {obj_ptr, method_ptr};
1264 }
1265 
1266 // Creates an action that performs an_action and throws away its
1267 // result. In other words, it changes the return type of an_action to
1268 // void. an_action MUST NOT return void, or the code won't compile.
1269 template <typename A>
1271  return internal::IgnoreResultAction<A>(an_action);
1272 }
1273 
1274 // Creates a reference wrapper for the given L-value. If necessary,
1275 // you can explicitly specify the type of the reference. For example,
1276 // suppose 'derived' is an object of type Derived, ByRef(derived)
1277 // would wrap a Derived&. If you want to wrap a const Base& instead,
1278 // where Base is a base class of Derived, just write:
1279 //
1280 // ByRef<const Base>(derived)
1281 //
1282 // N.B. ByRef is redundant with std::ref, std::cref and std::reference_wrapper.
1283 // However, it may still be used for consistency with ByMove().
1284 template <typename T>
1285 inline ::std::reference_wrapper<T> ByRef(T& l_value) { // NOLINT
1286  return ::std::reference_wrapper<T>(l_value);
1287 }
1288 
1289 namespace internal {
1290 
1291 template <typename T, typename... Params>
1293  T* operator()() const {
1294  return internal::Apply(
1295  [](const Params&... unpacked_params) {
1296  return new T(unpacked_params...);
1297  },
1298  params);
1299  }
1300  std::tuple<Params...> params;
1301 };
1302 
1303 } // namespace internal
1304 
1305 // The ReturnNew<T>(a1, a2, ..., a_k) action returns a pointer to a new
1306 // instance of type T, constructed on the heap with constructor arguments
1307 // a1, a2, ..., and a_k. The caller assumes ownership of the returned value.
1308 template <typename T, typename... Params>
1310  Params&&... params) {
1311  return {std::forward_as_tuple(std::forward<Params>(params)...)};
1312 }
1313 
1314 namespace internal {
1315 
1316 // A macro from the ACTION* family (defined later in gmock-generated-actions.h)
1317 // defines an action that can be used in a mock function. Typically,
1318 // these actions only care about a subset of the arguments of the mock
1319 // function. For example, if such an action only uses the second
1320 // argument, it can be used in any mock function that takes >= 2
1321 // arguments where the type of the second argument is compatible.
1322 //
1323 // Therefore, the action implementation must be prepared to take more
1324 // arguments than it needs. The ExcessiveArg type is used to
1325 // represent those excessive arguments. In order to keep the compiler
1326 // error messages tractable, we define it in the testing namespace
1327 // instead of testing::internal. However, this is an INTERNAL TYPE
1328 // and subject to change without notice, so a user MUST NOT USE THIS
1329 // TYPE DIRECTLY.
1330 struct ExcessiveArg {};
1331 
1332 // A helper class needed for implementing the ACTION* macros.
1333 template <typename Result, class Impl>
1335  public:
1336  template <typename... Ts>
1337  static Result Perform(Impl* impl, const std::tuple<Ts...>& args) {
1338  static constexpr size_t kMaxArgs = sizeof...(Ts) <= 10 ? sizeof...(Ts) : 10;
1339  return Apply(impl, args, MakeIndexSequence<kMaxArgs>{},
1340  MakeIndexSequence<10 - kMaxArgs>{});
1341  }
1342 
1343  private:
1344  template <typename... Ts, std::size_t... tuple_ids, std::size_t... rest_ids>
1345  static Result Apply(Impl* impl, const std::tuple<Ts...>& args,
1347  return impl->template gmock_PerformImpl<
1348  typename std::tuple_element<tuple_ids, std::tuple<Ts...>>::type...>(
1349  args, std::get<tuple_ids>(args)...,
1350  ((void)rest_ids, ExcessiveArg())...);
1351  }
1352 };
1353 
1354 // A helper base class needed for implementing the ACTION* macros.
1355 // Implements constructor and conversion operator for Action.
1356 //
1357 // Template specialization for parameterless Action.
1358 template <typename Derived>
1359 class ActionImpl {
1360  public:
1361  ActionImpl() = default;
1362 
1363  template <typename F>
1364  operator ::testing::Action<F>() const { // NOLINT(runtime/explicit)
1365  return ::testing::Action<F>(new typename Derived::template gmock_Impl<F>());
1366  }
1367 };
1368 
1369 // Template specialization for parameterized Action.
1370 template <template <typename...> class Derived, typename... Ts>
1371 class ActionImpl<Derived<Ts...>> {
1372  public:
1373  explicit ActionImpl(Ts... params) : params_(std::forward<Ts>(params)...) {}
1374 
1375  template <typename F>
1376  operator ::testing::Action<F>() const { // NOLINT(runtime/explicit)
1377  return Apply<F>(MakeIndexSequence<sizeof...(Ts)>{});
1378  }
1379 
1380  private:
1381  template <typename F, std::size_t... tuple_ids>
1383  return ::testing::Action<F>(new
1384  typename Derived<Ts...>::template gmock_Impl<F>(
1385  std::get<tuple_ids>(params_)...));
1386  }
1387 
1388  std::tuple<Ts...> params_;
1389 };
1390 
1391 namespace invoke_argument {
1392 
1393 // Appears in InvokeArgumentAdl's argument list to help avoid
1394 // accidental calls to user functions of the same name.
1395 struct AdlTag {};
1396 
1397 // InvokeArgumentAdl - a helper for InvokeArgument.
1398 // The basic overloads are provided here for generic functors.
1399 // Overloads for other custom-callables are provided in the
1400 // internal/custom/gmock-generated-actions.h header.
1401 template <typename F, typename... Args>
1402 auto InvokeArgumentAdl(AdlTag, F f, Args... args) -> decltype(f(args...)) {
1403  return f(args...);
1404 }
1405 
1406 } // namespace invoke_argument
1407 
1408 #define GMOCK_INTERNAL_ARG_UNUSED(i, data, el) \
1409  , const arg##i##_type& arg##i GTEST_ATTRIBUTE_UNUSED_
1410 #define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_ \
1411  const args_type& args GTEST_ATTRIBUTE_UNUSED_ GMOCK_PP_REPEAT( \
1412  GMOCK_INTERNAL_ARG_UNUSED, , 10)
1413 
1414 #define GMOCK_INTERNAL_ARG(i, data, el) , const arg##i##_type& arg##i
1415 #define GMOCK_ACTION_ARG_TYPES_AND_NAMES_ \
1416  const args_type& args GMOCK_PP_REPEAT(GMOCK_INTERNAL_ARG, , 10)
1417 
1418 #define GMOCK_INTERNAL_TEMPLATE_ARG(i, data, el) , typename arg##i##_type
1419 #define GMOCK_ACTION_TEMPLATE_ARGS_NAMES_ \
1420  GMOCK_PP_TAIL(GMOCK_PP_REPEAT(GMOCK_INTERNAL_TEMPLATE_ARG, , 10))
1421 
1422 #define GMOCK_INTERNAL_TYPENAME_PARAM(i, data, param) , typename param##_type
1423 #define GMOCK_ACTION_TYPENAME_PARAMS_(params) \
1424  GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_TYPENAME_PARAM, , params))
1425 
1426 #define GMOCK_INTERNAL_TYPE_PARAM(i, data, param) , param##_type
1427 #define GMOCK_ACTION_TYPE_PARAMS_(params) \
1428  GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_TYPE_PARAM, , params))
1429 
1430 #define GMOCK_INTERNAL_TYPE_GVALUE_PARAM(i, data, param) \
1431  , param##_type gmock_p##i
1432 #define GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params) \
1433  GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_TYPE_GVALUE_PARAM, , params))
1434 
1435 #define GMOCK_INTERNAL_GVALUE_PARAM(i, data, param) \
1436  , std::forward<param##_type>(gmock_p##i)
1437 #define GMOCK_ACTION_GVALUE_PARAMS_(params) \
1438  GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_GVALUE_PARAM, , params))
1439 
1440 #define GMOCK_INTERNAL_INIT_PARAM(i, data, param) \
1441  , param(::std::forward<param##_type>(gmock_p##i))
1442 #define GMOCK_ACTION_INIT_PARAMS_(params) \
1443  GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_INIT_PARAM, , params))
1444 
1445 #define GMOCK_INTERNAL_FIELD_PARAM(i, data, param) param##_type param;
1446 #define GMOCK_ACTION_FIELD_PARAMS_(params) \
1447  GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_FIELD_PARAM, , params)
1448 
1449 #define GMOCK_INTERNAL_ACTION(name, full_name, params) \
1450  template <GMOCK_ACTION_TYPENAME_PARAMS_(params)> \
1451  class full_name : public ::testing::internal::ActionImpl< \
1452  full_name<GMOCK_ACTION_TYPE_PARAMS_(params)>> { \
1453  using base_type = ::testing::internal::ActionImpl<full_name>; \
1454  \
1455  public: \
1456  using base_type::base_type; \
1457  template <typename F> \
1458  class gmock_Impl : public ::testing::ActionInterface<F> { \
1459  public: \
1460  typedef F function_type; \
1461  typedef typename ::testing::internal::Function<F>::Result return_type; \
1462  typedef \
1463  typename ::testing::internal::Function<F>::ArgumentTuple args_type; \
1464  explicit gmock_Impl(GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params)) \
1465  : GMOCK_ACTION_INIT_PARAMS_(params) {} \
1466  return_type Perform(const args_type& args) override { \
1467  return ::testing::internal::ActionHelper<return_type, \
1468  gmock_Impl>::Perform(this, \
1469  args); \
1470  } \
1471  template <GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
1472  return_type gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_) const; \
1473  GMOCK_ACTION_FIELD_PARAMS_(params) \
1474  }; \
1475  }; \
1476  template <GMOCK_ACTION_TYPENAME_PARAMS_(params)> \
1477  inline full_name<GMOCK_ACTION_TYPE_PARAMS_(params)> name( \
1478  GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params)) { \
1479  return full_name<GMOCK_ACTION_TYPE_PARAMS_(params)>( \
1480  GMOCK_ACTION_GVALUE_PARAMS_(params)); \
1481  } \
1482  template <GMOCK_ACTION_TYPENAME_PARAMS_(params)> \
1483  template <typename F> \
1484  template <GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
1485  typename ::testing::internal::Function<F>::Result \
1486  full_name<GMOCK_ACTION_TYPE_PARAMS_(params)>::gmock_Impl< \
1487  F>::gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) \
1488  const
1489 
1490 } // namespace internal
1491 
1492 #define ACTION(name) \
1493  class name##Action : public ::testing::internal::ActionImpl<name##Action> { \
1494  using base_type = ::testing::internal::ActionImpl<name##Action>; \
1495  \
1496  public: \
1497  using base_type::base_type; \
1498  template <typename F> \
1499  class gmock_Impl : public ::testing::ActionInterface<F> { \
1500  public: \
1501  typedef F function_type; \
1502  typedef typename ::testing::internal::Function<F>::Result return_type; \
1503  typedef \
1504  typename ::testing::internal::Function<F>::ArgumentTuple args_type; \
1505  gmock_Impl() {} \
1506  return_type Perform(const args_type& args) override { \
1507  return ::testing::internal::ActionHelper<return_type, \
1508  gmock_Impl>::Perform(this, \
1509  args); \
1510  } \
1511  template <GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
1512  return_type gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_) const; \
1513  }; \
1514  }; \
1515  inline name##Action name() { return name##Action(); } \
1516  template <typename F> \
1517  template <GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
1518  typename ::testing::internal::Function<F>::Result \
1519  name##Action::gmock_Impl<F>::gmock_PerformImpl( \
1520  GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
1521 
1522 #define ACTION_P(name, ...) \
1523  GMOCK_INTERNAL_ACTION(name, name##ActionP, (__VA_ARGS__))
1524 
1525 #define ACTION_P2(name, ...) \
1526  GMOCK_INTERNAL_ACTION(name, name##ActionP2, (__VA_ARGS__))
1527 
1528 #define ACTION_P3(name, ...) \
1529  GMOCK_INTERNAL_ACTION(name, name##ActionP3, (__VA_ARGS__))
1530 
1531 #define ACTION_P4(name, ...) \
1532  GMOCK_INTERNAL_ACTION(name, name##ActionP4, (__VA_ARGS__))
1533 
1534 #define ACTION_P5(name, ...) \
1535  GMOCK_INTERNAL_ACTION(name, name##ActionP5, (__VA_ARGS__))
1536 
1537 #define ACTION_P6(name, ...) \
1538  GMOCK_INTERNAL_ACTION(name, name##ActionP6, (__VA_ARGS__))
1539 
1540 #define ACTION_P7(name, ...) \
1541  GMOCK_INTERNAL_ACTION(name, name##ActionP7, (__VA_ARGS__))
1542 
1543 #define ACTION_P8(name, ...) \
1544  GMOCK_INTERNAL_ACTION(name, name##ActionP8, (__VA_ARGS__))
1545 
1546 #define ACTION_P9(name, ...) \
1547  GMOCK_INTERNAL_ACTION(name, name##ActionP9, (__VA_ARGS__))
1548 
1549 #define ACTION_P10(name, ...) \
1550  GMOCK_INTERNAL_ACTION(name, name##ActionP10, (__VA_ARGS__))
1551 
1552 } // namespace testing
1553 
1554 #ifdef _MSC_VER
1555 # pragma warning(pop)
1556 #endif
1557 
1558 
1559 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
Sacado::Fad::DFad< double > F
Definition: ad_example.cpp:20
To ImplicitCast_(To x)
Definition: gtest-port.h:1063
internal::Function< F >::Result Result
static Result Perform(const ArgumentTuple &)
PolymorphicAction< internal::ReturnNullAction > ReturnNull()
void f()
internal::Function< F >::ArgumentTuple ArgumentTuple
internal::IgnoredValue Unused
GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionInterface)
void Init(G &&g,::std::true_type)
internal::ReturnRoundRobinAction< T > ReturnRoundRobin(std::vector< T > vals)
GTEST_API_ void IllegalDoDefault(const char *file, int line)
static void SetFactory(FactoryFunction factory)
PolymorphicAction< internal::AssignAction< T1, T2 > > Assign(T1 *ptr, T2 val)
auto Apply(F &&f, Tuple &&args) -> decltype(ApplyImpl(std::forward< F >(f), std::forward< Tuple >(args), MakeIndexSequence< std::tuple_size< typename std::remove_reference< Tuple >::type >::value >()))
static Result Apply(Impl *impl, const std::tuple< Ts...> &args, IndexSequence< tuple_ids...>, IndexSequence< rest_ids...>)
static void Set(T x)
::std::shared_ptr< ActionInterface< F > > impl_
AssignAction(T1 *ptr, T2 value)
Action(const Action< Func > &action)
PolymorphicAction(const Impl &impl)
internal::DoAllAction< typename std::decay< Action >::type...> DoAll(Action &&...action)
Impl(const std::shared_ptr< R > &value)
ReturnType operator()(const Args &...) const
std::tuple< Actions...> actions
GTEST_DISALLOW_COPY_AND_ASSIGN_(FactoryValueProducer)
internal::InvokeWithoutArgsAction< typename std::decay< FunctionImpl >::type > InvokeWithoutArgs(FunctionImpl function_impl)
auto operator()(Args &&...args) const -> decltype((obj_ptr-> *method_ptr)(std::forward< Args >(args)...))
internal::WithArgsAction< typename std::decay< InnerAction >::type, k, ks...> WithArgs(InnerAction &&action)
Function< F >::ArgumentTuple ArgumentTuple
static void Perform(const ArgumentTuple &)
static Result Perform(Impl *impl, const std::tuple< Ts...> &args)
PolymorphicAction< Impl > MakePolymorphicAction(const Impl &impl)
Function< F >::ArgumentTuple ArgumentTuple
Sacado::Rad::ADvar< double > R
Definition: ad_example.cpp:22
Result Perform(const ArgumentTuple &) override
internal::Function< F >::MakeResultIgnoredValue OriginalFunction
inline::std::reference_wrapper< T > ByRef(T &l_value)
PolymorphicAction< internal::SetErrnoAndReturnAction< T > > SetErrnoAndReturn(int errval, T result)
internal::Function< F >::ArgumentTuple ArgumentTuple
auto InvokeArgumentAdl(AdlTag, F f, Args...args) -> decltype(f(args...))
internal::ReturnRefAction< R > ReturnRef(R &x)
expr val()
std::decay< FunctionImpl >::type Invoke(FunctionImpl &&function_impl)
#define T
Definition: Sacado_rad.hpp:553
void operator()(const Args &...args) const
int value_
Result Perform(const ArgumentTuple &) override
Action(ActionInterface< F > *impl)
#define GTEST_COMPILE_ASSERT_(expr, msg)
Definition: gtest-port.h:875
static bool Exists()
#define T2(r, f)
Definition: Sacado_rad.hpp:558
#define GTEST_CHECK_(condition)
Definition: gtest-port.h:1004
FactoryValueProducer(FactoryFunction factory)
GTEST_DISALLOW_COPY_AND_ASSIGN_(FixedValueProducer)
internal::WithArgsAction< typename std::decay< InnerAction >::type > WithoutArgs(InnerAction &&action)
internal::ReturnRefOfCopyAction< R > ReturnRefOfCopy(const R &x)
internal::Function< F >::Result operator()(Args &&...args)
SetErrnoAndReturnAction(int errno_value, T result)
#define T1(r, f)
Definition: Sacado_rad.hpp:583
void g()
internal::Function< F >::Result Result
internal::Function< F >::ArgumentTuple ArgumentTuple
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(void,)
internal::SetArgumentPointeeAction< N, T > SetArgPointee(T value)
std::vector< Action< void(Args...)> > Convert(IndexSequence< I...>) const
internal::Function< F >::Result Result
void
Definition: uninit.c:105
bool IsDoDefault() const
ReturnRoundRobinAction(std::vector< T > values)
int value
internal::Function< F >::ArgumentTuple ArgumentTuple
Result Perform(const ArgumentTuple &) override
auto operator()(const Args &...) -> decltype(function_impl())
internal::DoDefaultAction DoDefault()
internal::ReturnAction< R > Return(R value)
::testing::Action< F > Apply(IndexSequence< tuple_ids...>) const
Function< F >::ArgumentTuple ArgumentTuple
void Assert(bool condition, const char *file, int line, const std::string &msg)
const Action< OriginalFunction > action_
Result Perform(const ArgumentTuple &) const
internal::SetArgumentPointeeAction< N, T > SetArgumentPointee(T value)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
Definition: gtest-port.h:693
internal::ByMoveWrapper< R > ByMove(R x)
virtual Result Perform(const ArgumentTuple &args)=0
internal::WithArgsAction< typename std::decay< InnerAction >::type, k > WithArg(InnerAction &&action)
::std::function< F > fun_
Result Perform(const ArgumentTuple &args) override
static ValueProducer * producer_
internal::Function< F >::Result Result
internal::ReturnNewAction< T, typename std::decay< Params >::type...> ReturnNew(Params &&...params)
void Perform(const ArgumentTuple &args) override
void Perform(const ArgumentTuple &) const
expr expr expr bar false
const std::shared_ptr< R > value_
internal::IgnoreResultAction< A > IgnoreResult(const A &an_action)
Result operator()(const Args &...) const
void Init(G &&g,::std::false_type)
Result Perform(ArgumentTuple args) const
decltype((std::declval< Class * >() -> *std::declval< MethodPtr >())()) ReturnType
Action< F > MakeAction(ActionInterface< F > *impl)
typename::std::conditional< P::value, P, Q >::type disjunction