blob: 46b7bf9d4a17db53089c9741e2c84835758d7677 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// 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// Author: wan@google.com (Zhanyong Wan)
31
32// Google Mock - a framework for writing C++ mock classes.
33//
34// This file implements some commonly used actions.
35
36#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
37#define GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
38
zhanyong.wan5b5d62f2009-03-11 23:37:56 +000039#ifndef _WIN32_WCE
zhanyong.wan658ac0b2011-02-24 07:29:13 +000040# include <errno.h>
zhanyong.wan5b5d62f2009-03-11 23:37:56 +000041#endif
42
jgm79a367e2012-04-10 16:02:11 +000043#include <algorithm>
44#include <string>
45
zhanyong.wan53e08c42010-09-14 05:38:21 +000046#include "gmock/internal/gmock-internal-utils.h"
47#include "gmock/internal/gmock-port.h"
shiqiane35fdd92008-12-10 05:08:54 +000048
49namespace testing {
50
51// To implement an action Foo, define:
52// 1. a class FooAction that implements the ActionInterface interface, and
53// 2. a factory function that creates an Action object from a
54// const FooAction*.
55//
56// The two-level delegation design follows that of Matcher, providing
57// consistency for extension developers. It also eases ownership
58// management as Action objects can now be copied like plain values.
59
60namespace internal {
61
shiqiane35fdd92008-12-10 05:08:54 +000062template <typename F1, typename F2>
63class ActionAdaptor;
64
65// BuiltInDefaultValue<T>::Get() returns the "built-in" default
66// value for type T, which is NULL when T is a pointer type, 0 when T
67// is a numeric type, false when T is bool, or "" when T is string or
68// std::string. For any other type T, this value is undefined and the
69// function will abort the process.
70template <typename T>
71class BuiltInDefaultValue {
72 public:
zhanyong.wan5b95fa72009-01-27 22:28:45 +000073 // This function returns true iff type T has a built-in default value.
74 static bool Exists() { return false; }
shiqiane35fdd92008-12-10 05:08:54 +000075 static T Get() {
76 Assert(false, __FILE__, __LINE__,
77 "Default action undefined for the function return type.");
78 return internal::Invalid<T>();
79 // The above statement will never be reached, but is required in
80 // order for this function to compile.
81 }
82};
83
84// This partial specialization says that we use the same built-in
85// default value for T and const T.
86template <typename T>
87class BuiltInDefaultValue<const T> {
88 public:
zhanyong.wan5b95fa72009-01-27 22:28:45 +000089 static bool Exists() { return BuiltInDefaultValue<T>::Exists(); }
shiqiane35fdd92008-12-10 05:08:54 +000090 static T Get() { return BuiltInDefaultValue<T>::Get(); }
91};
92
93// This partial specialization defines the default values for pointer
94// types.
95template <typename T>
96class BuiltInDefaultValue<T*> {
97 public:
zhanyong.wan5b95fa72009-01-27 22:28:45 +000098 static bool Exists() { return true; }
shiqiane35fdd92008-12-10 05:08:54 +000099 static T* Get() { return NULL; }
100};
101
102// The following specializations define the default values for
103// specific types we care about.
zhanyong.wane0d051e2009-02-19 00:33:37 +0000104#define GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(type, value) \
shiqiane35fdd92008-12-10 05:08:54 +0000105 template <> \
106 class BuiltInDefaultValue<type> { \
107 public: \
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000108 static bool Exists() { return true; } \
shiqiane35fdd92008-12-10 05:08:54 +0000109 static type Get() { return value; } \
110 }
111
zhanyong.wane0d051e2009-02-19 00:33:37 +0000112GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(void, ); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000113#if GTEST_HAS_GLOBAL_STRING
zhanyong.wane0d051e2009-02-19 00:33:37 +0000114GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::string, "");
shiqiane35fdd92008-12-10 05:08:54 +0000115#endif // GTEST_HAS_GLOBAL_STRING
zhanyong.wane0d051e2009-02-19 00:33:37 +0000116GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::std::string, "");
zhanyong.wane0d051e2009-02-19 00:33:37 +0000117GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(bool, false);
118GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned char, '\0');
119GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed char, '\0');
120GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(char, '\0');
shiqiane35fdd92008-12-10 05:08:54 +0000121
shiqiane35fdd92008-12-10 05:08:54 +0000122// There's no need for a default action for signed wchar_t, as that
123// type is the same as wchar_t for gcc, and invalid for MSVC.
124//
125// There's also no need for a default action for unsigned wchar_t, as
126// that type is the same as unsigned int for gcc, and invalid for
127// MSVC.
zhanyong.wan95b12332009-09-25 18:55:50 +0000128#if GMOCK_WCHAR_T_IS_NATIVE_
zhanyong.wane0d051e2009-02-19 00:33:37 +0000129GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(wchar_t, 0U); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000130#endif
131
zhanyong.wane0d051e2009-02-19 00:33:37 +0000132GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned short, 0U); // NOLINT
133GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed short, 0); // NOLINT
134GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned int, 0U);
135GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed int, 0);
136GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned long, 0UL); // NOLINT
137GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed long, 0L); // NOLINT
138GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(UInt64, 0);
139GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(Int64, 0);
140GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(float, 0);
141GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(double, 0);
shiqiane35fdd92008-12-10 05:08:54 +0000142
zhanyong.wane0d051e2009-02-19 00:33:37 +0000143#undef GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_
shiqiane35fdd92008-12-10 05:08:54 +0000144
145} // namespace internal
146
147// When an unexpected function call is encountered, Google Mock will
148// let it return a default value if the user has specified one for its
149// return type, or if the return type has a built-in default value;
150// otherwise Google Mock won't know what value to return and will have
151// to abort the process.
152//
153// The DefaultValue<T> class allows a user to specify the
154// default value for a type T that is both copyable and publicly
155// destructible (i.e. anything that can be used as a function return
156// type). The usage is:
157//
158// // Sets the default value for type T to be foo.
159// DefaultValue<T>::Set(foo);
160template <typename T>
161class DefaultValue {
162 public:
163 // Sets the default value for type T; requires T to be
164 // copy-constructable and have a public destructor.
165 static void Set(T x) {
kosakb5c81092014-01-29 06:41:44 +0000166 delete producer_;
167 producer_ = new FixedValueProducer(x);
168 }
169
170 // Provides a factory function to be called to generate the default value.
171 // This method can be used even if T is only move-constructible, but it is not
172 // limited to that case.
173 typedef T (*FactoryFunction)();
174 static void SetFactory(FactoryFunction factory) {
175 delete producer_;
176 producer_ = new FactoryValueProducer(factory);
shiqiane35fdd92008-12-10 05:08:54 +0000177 }
178
179 // Unsets the default value for type T.
180 static void Clear() {
kosakb5c81092014-01-29 06:41:44 +0000181 delete producer_;
182 producer_ = NULL;
shiqiane35fdd92008-12-10 05:08:54 +0000183 }
184
185 // Returns true iff the user has set the default value for type T.
kosakb5c81092014-01-29 06:41:44 +0000186 static bool IsSet() { return producer_ != NULL; }
shiqiane35fdd92008-12-10 05:08:54 +0000187
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000188 // Returns true if T has a default return value set by the user or there
189 // exists a built-in default value.
190 static bool Exists() {
191 return IsSet() || internal::BuiltInDefaultValue<T>::Exists();
192 }
193
shiqiane35fdd92008-12-10 05:08:54 +0000194 // Returns the default value for type T if the user has set one;
kosakb5c81092014-01-29 06:41:44 +0000195 // otherwise returns the built-in default value. Requires that Exists()
196 // is true, which ensures that the return value is well-defined.
shiqiane35fdd92008-12-10 05:08:54 +0000197 static T Get() {
kosakb5c81092014-01-29 06:41:44 +0000198 return producer_ == NULL ?
199 internal::BuiltInDefaultValue<T>::Get() : producer_->Produce();
shiqiane35fdd92008-12-10 05:08:54 +0000200 }
jgm79a367e2012-04-10 16:02:11 +0000201
shiqiane35fdd92008-12-10 05:08:54 +0000202 private:
kosakb5c81092014-01-29 06:41:44 +0000203 class ValueProducer {
204 public:
205 virtual ~ValueProducer() {}
206 virtual T Produce() = 0;
207 };
208
209 class FixedValueProducer : public ValueProducer {
210 public:
211 explicit FixedValueProducer(T value) : value_(value) {}
212 virtual T Produce() { return value_; }
213
214 private:
215 const T value_;
216 GTEST_DISALLOW_COPY_AND_ASSIGN_(FixedValueProducer);
217 };
218
219 class FactoryValueProducer : public ValueProducer {
220 public:
221 explicit FactoryValueProducer(FactoryFunction factory)
222 : factory_(factory) {}
223 virtual T Produce() { return factory_(); }
224
225 private:
226 const FactoryFunction factory_;
227 GTEST_DISALLOW_COPY_AND_ASSIGN_(FactoryValueProducer);
228 };
229
230 static ValueProducer* producer_;
shiqiane35fdd92008-12-10 05:08:54 +0000231};
232
233// This partial specialization allows a user to set default values for
234// reference types.
235template <typename T>
236class DefaultValue<T&> {
237 public:
238 // Sets the default value for type T&.
239 static void Set(T& x) { // NOLINT
240 address_ = &x;
241 }
242
243 // Unsets the default value for type T&.
244 static void Clear() {
245 address_ = NULL;
246 }
247
248 // Returns true iff the user has set the default value for type T&.
249 static bool IsSet() { return address_ != NULL; }
250
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000251 // Returns true if T has a default return value set by the user or there
252 // exists a built-in default value.
253 static bool Exists() {
254 return IsSet() || internal::BuiltInDefaultValue<T&>::Exists();
255 }
256
shiqiane35fdd92008-12-10 05:08:54 +0000257 // Returns the default value for type T& if the user has set one;
258 // otherwise returns the built-in default value if there is one;
259 // otherwise aborts the process.
260 static T& Get() {
261 return address_ == NULL ?
262 internal::BuiltInDefaultValue<T&>::Get() : *address_;
263 }
jgm79a367e2012-04-10 16:02:11 +0000264
shiqiane35fdd92008-12-10 05:08:54 +0000265 private:
266 static T* address_;
267};
268
269// This specialization allows DefaultValue<void>::Get() to
270// compile.
271template <>
272class DefaultValue<void> {
273 public:
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000274 static bool Exists() { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000275 static void Get() {}
276};
277
278// Points to the user-set default value for type T.
279template <typename T>
kosakb5c81092014-01-29 06:41:44 +0000280typename DefaultValue<T>::ValueProducer* DefaultValue<T>::producer_ = NULL;
shiqiane35fdd92008-12-10 05:08:54 +0000281
282// Points to the user-set default value for type T&.
283template <typename T>
284T* DefaultValue<T&>::address_ = NULL;
285
286// Implement this interface to define an action for function type F.
287template <typename F>
288class ActionInterface {
289 public:
290 typedef typename internal::Function<F>::Result Result;
291 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
292
zhanyong.waned6c9272011-02-23 19:39:27 +0000293 ActionInterface() {}
shiqiane35fdd92008-12-10 05:08:54 +0000294 virtual ~ActionInterface() {}
295
296 // Performs the action. This method is not const, as in general an
297 // action can have side effects and be stateful. For example, a
298 // get-the-next-element-from-the-collection action will need to
299 // remember the current element.
300 virtual Result Perform(const ArgumentTuple& args) = 0;
301
shiqiane35fdd92008-12-10 05:08:54 +0000302 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000303 GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionInterface);
shiqiane35fdd92008-12-10 05:08:54 +0000304};
305
306// An Action<F> is a copyable and IMMUTABLE (except by assignment)
307// object that represents an action to be taken when a mock function
308// of type F is called. The implementation of Action<T> is just a
309// linked_ptr to const ActionInterface<T>, so copying is fairly cheap.
310// Don't inherit from Action!
311//
312// You can view an object implementing ActionInterface<F> as a
313// concrete action (including its current state), and an Action<F>
314// object as a handle to it.
315template <typename F>
316class Action {
317 public:
318 typedef typename internal::Function<F>::Result Result;
319 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
320
321 // Constructs a null Action. Needed for storing Action objects in
322 // STL containers.
323 Action() : impl_(NULL) {}
324
zhanyong.waned6c9272011-02-23 19:39:27 +0000325 // Constructs an Action from its implementation. A NULL impl is
326 // used to represent the "do-default" action.
shiqiane35fdd92008-12-10 05:08:54 +0000327 explicit Action(ActionInterface<F>* impl) : impl_(impl) {}
328
329 // Copy constructor.
330 Action(const Action& action) : impl_(action.impl_) {}
331
332 // This constructor allows us to turn an Action<Func> object into an
333 // Action<F>, as long as F's arguments can be implicitly converted
vladloseva070cbd2009-11-18 00:09:28 +0000334 // to Func's and Func's return type can be implicitly converted to
shiqiane35fdd92008-12-10 05:08:54 +0000335 // F's.
336 template <typename Func>
337 explicit Action(const Action<Func>& action);
338
339 // Returns true iff this is the DoDefault() action.
zhanyong.waned6c9272011-02-23 19:39:27 +0000340 bool IsDoDefault() const { return impl_.get() == NULL; }
shiqiane35fdd92008-12-10 05:08:54 +0000341
342 // Performs the action. Note that this method is const even though
343 // the corresponding method in ActionInterface is not. The reason
344 // is that a const Action<F> means that it cannot be re-bound to
345 // another concrete action, not that the concrete action it binds to
346 // cannot change state. (Think of the difference between a const
347 // pointer and a pointer to const.)
348 Result Perform(const ArgumentTuple& args) const {
zhanyong.waned6c9272011-02-23 19:39:27 +0000349 internal::Assert(
350 !IsDoDefault(), __FILE__, __LINE__,
351 "You are using DoDefault() inside a composite action like "
352 "DoAll() or WithArgs(). This is not supported for technical "
353 "reasons. Please instead spell out the default action, or "
354 "assign the default action to an Action variable and use "
355 "the variable in various places.");
shiqiane35fdd92008-12-10 05:08:54 +0000356 return impl_->Perform(args);
357 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000358
shiqiane35fdd92008-12-10 05:08:54 +0000359 private:
360 template <typename F1, typename F2>
361 friend class internal::ActionAdaptor;
362
363 internal::linked_ptr<ActionInterface<F> > impl_;
364};
365
366// The PolymorphicAction class template makes it easy to implement a
367// polymorphic action (i.e. an action that can be used in mock
368// functions of than one type, e.g. Return()).
369//
370// To define a polymorphic action, a user first provides a COPYABLE
371// implementation class that has a Perform() method template:
372//
373// class FooAction {
374// public:
375// template <typename Result, typename ArgumentTuple>
376// Result Perform(const ArgumentTuple& args) const {
377// // Processes the arguments and returns a result, using
378// // tr1::get<N>(args) to get the N-th (0-based) argument in the tuple.
379// }
380// ...
381// };
382//
383// Then the user creates the polymorphic action using
384// MakePolymorphicAction(object) where object has type FooAction. See
385// the definition of Return(void) and SetArgumentPointee<N>(value) for
386// complete examples.
387template <typename Impl>
388class PolymorphicAction {
389 public:
390 explicit PolymorphicAction(const Impl& impl) : impl_(impl) {}
391
392 template <typename F>
393 operator Action<F>() const {
394 return Action<F>(new MonomorphicImpl<F>(impl_));
395 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000396
shiqiane35fdd92008-12-10 05:08:54 +0000397 private:
398 template <typename F>
399 class MonomorphicImpl : public ActionInterface<F> {
400 public:
401 typedef typename internal::Function<F>::Result Result;
402 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
403
404 explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
405
406 virtual Result Perform(const ArgumentTuple& args) {
407 return impl_.template Perform<Result>(args);
408 }
409
410 private:
411 Impl impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000412
413 GTEST_DISALLOW_ASSIGN_(MonomorphicImpl);
shiqiane35fdd92008-12-10 05:08:54 +0000414 };
415
416 Impl impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000417
418 GTEST_DISALLOW_ASSIGN_(PolymorphicAction);
shiqiane35fdd92008-12-10 05:08:54 +0000419};
420
421// Creates an Action from its implementation and returns it. The
422// created Action object owns the implementation.
423template <typename F>
424Action<F> MakeAction(ActionInterface<F>* impl) {
425 return Action<F>(impl);
426}
427
428// Creates a polymorphic action from its implementation. This is
429// easier to use than the PolymorphicAction<Impl> constructor as it
430// doesn't require you to explicitly write the template argument, e.g.
431//
432// MakePolymorphicAction(foo);
433// vs
434// PolymorphicAction<TypeOfFoo>(foo);
435template <typename Impl>
436inline PolymorphicAction<Impl> MakePolymorphicAction(const Impl& impl) {
437 return PolymorphicAction<Impl>(impl);
438}
439
440namespace internal {
441
442// Allows an Action<F2> object to pose as an Action<F1>, as long as F2
443// and F1 are compatible.
444template <typename F1, typename F2>
445class ActionAdaptor : public ActionInterface<F1> {
446 public:
447 typedef typename internal::Function<F1>::Result Result;
448 typedef typename internal::Function<F1>::ArgumentTuple ArgumentTuple;
449
450 explicit ActionAdaptor(const Action<F2>& from) : impl_(from.impl_) {}
451
452 virtual Result Perform(const ArgumentTuple& args) {
453 return impl_->Perform(args);
454 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000455
shiqiane35fdd92008-12-10 05:08:54 +0000456 private:
457 const internal::linked_ptr<ActionInterface<F2> > impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000458
459 GTEST_DISALLOW_ASSIGN_(ActionAdaptor);
shiqiane35fdd92008-12-10 05:08:54 +0000460};
461
kosak3d1c78b2014-11-17 00:56:52 +0000462// Helper struct to specialize ReturnAction to execute a move instead of a copy
463// on return. Useful for move-only types, but could be used on any type.
464template <typename T>
465struct ByMoveWrapper {
kosakd370f852014-11-17 01:14:16 +0000466 explicit ByMoveWrapper(T value) : payload(internal::move(value)) {}
kosak3d1c78b2014-11-17 00:56:52 +0000467 T payload;
468};
469
shiqiane35fdd92008-12-10 05:08:54 +0000470// Implements the polymorphic Return(x) action, which can be used in
471// any function that returns the type of x, regardless of the argument
472// types.
vladloseva070cbd2009-11-18 00:09:28 +0000473//
474// Note: The value passed into Return must be converted into
475// Function<F>::Result when this action is cast to Action<F> rather than
476// when that action is performed. This is important in scenarios like
477//
478// MOCK_METHOD1(Method, T(U));
479// ...
480// {
481// Foo foo;
482// X x(&foo);
483// EXPECT_CALL(mock, Method(_)).WillOnce(Return(x));
484// }
485//
486// In the example above the variable x holds reference to foo which leaves
487// scope and gets destroyed. If copying X just copies a reference to foo,
488// that copy will be left with a hanging reference. If conversion to T
489// makes a copy of foo, the above code is safe. To support that scenario, we
490// need to make sure that the type conversion happens inside the EXPECT_CALL
491// statement, and conversion of the result of Return to Action<T(U)> is a
492// good place for that.
493//
shiqiane35fdd92008-12-10 05:08:54 +0000494template <typename R>
495class ReturnAction {
496 public:
497 // Constructs a ReturnAction object from the value to be returned.
498 // 'value' is passed by value instead of by const reference in order
499 // to allow Return("string literal") to compile.
kosakd370f852014-11-17 01:14:16 +0000500 explicit ReturnAction(R value) : value_(new R(internal::move(value))) {}
shiqiane35fdd92008-12-10 05:08:54 +0000501
502 // This template type conversion operator allows Return(x) to be
503 // used in ANY function that returns x's type.
504 template <typename F>
505 operator Action<F>() const {
506 // Assert statement belongs here because this is the best place to verify
507 // conditions on F. It produces the clearest error messages
508 // in most compilers.
509 // Impl really belongs in this scope as a local class but can't
510 // because MSVC produces duplicate symbols in different translation units
511 // in this case. Until MS fixes that bug we put Impl into the class scope
512 // and put the typedef both here (for use in assert statement) and
513 // in the Impl class. But both definitions must be the same.
514 typedef typename Function<F>::Result Result;
zhanyong.wan02f71062010-05-10 17:14:29 +0000515 GTEST_COMPILE_ASSERT_(
kosak3d1c78b2014-11-17 00:56:52 +0000516 !is_reference<Result>::value,
zhanyong.wane0d051e2009-02-19 00:33:37 +0000517 use_ReturnRef_instead_of_Return_to_return_a_reference);
kosak3d1c78b2014-11-17 00:56:52 +0000518 return Action<F>(new Impl<R, F>(value_));
shiqiane35fdd92008-12-10 05:08:54 +0000519 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000520
shiqiane35fdd92008-12-10 05:08:54 +0000521 private:
522 // Implements the Return(x) action for a particular function type F.
kosak3d1c78b2014-11-17 00:56:52 +0000523 template <typename R_, typename F>
shiqiane35fdd92008-12-10 05:08:54 +0000524 class Impl : public ActionInterface<F> {
525 public:
526 typedef typename Function<F>::Result Result;
527 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
528
vladloseva070cbd2009-11-18 00:09:28 +0000529 // The implicit cast is necessary when Result has more than one
530 // single-argument constructor (e.g. Result is std::vector<int>) and R
531 // has a type conversion operator template. In that case, value_(value)
532 // won't compile as the compiler doesn't known which constructor of
zhanyong.wan5b61ce32011-02-01 00:00:03 +0000533 // Result to call. ImplicitCast_ forces the compiler to convert R to
vladloseva070cbd2009-11-18 00:09:28 +0000534 // Result without considering explicit constructors, thus resolving the
535 // ambiguity. value_ is then initialized using its copy constructor.
kosak3d1c78b2014-11-17 00:56:52 +0000536 explicit Impl(const linked_ptr<R>& value)
kosak7123d832014-11-17 02:04:46 +0000537 : value_before_cast_(*value),
538 value_(ImplicitCast_<Result>(value_before_cast_)) {}
shiqiane35fdd92008-12-10 05:08:54 +0000539
540 virtual Result Perform(const ArgumentTuple&) { return value_; }
541
542 private:
kosak3d1c78b2014-11-17 00:56:52 +0000543 GTEST_COMPILE_ASSERT_(!is_reference<Result>::value,
vladloseva070cbd2009-11-18 00:09:28 +0000544 Result_cannot_be_a_reference_type);
kosak7123d832014-11-17 02:04:46 +0000545 // We save the value before casting just in case it is being cast to a
546 // wrapper type.
547 R value_before_cast_;
vladloseva070cbd2009-11-18 00:09:28 +0000548 Result value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000549
kosak7123d832014-11-17 02:04:46 +0000550 GTEST_DISALLOW_COPY_AND_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000551 };
552
kosak3d1c78b2014-11-17 00:56:52 +0000553 // Partially specialize for ByMoveWrapper. This version of ReturnAction will
554 // move its contents instead.
555 template <typename R_, typename F>
556 class Impl<ByMoveWrapper<R_>, F> : public ActionInterface<F> {
557 public:
558 typedef typename Function<F>::Result Result;
559 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
560
561 explicit Impl(const linked_ptr<R>& wrapper)
562 : performed_(false), wrapper_(wrapper) {}
563
564 virtual Result Perform(const ArgumentTuple&) {
565 GTEST_CHECK_(!performed_)
566 << "A ByMove() action should only be performed once.";
567 performed_ = true;
kosakd370f852014-11-17 01:14:16 +0000568 return internal::move(wrapper_->payload);
kosak3d1c78b2014-11-17 00:56:52 +0000569 }
570
571 private:
572 bool performed_;
573 const linked_ptr<R> wrapper_;
574
575 GTEST_DISALLOW_ASSIGN_(Impl);
576 };
577
578 const linked_ptr<R> value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000579
580 GTEST_DISALLOW_ASSIGN_(ReturnAction);
shiqiane35fdd92008-12-10 05:08:54 +0000581};
582
583// Implements the ReturnNull() action.
584class ReturnNullAction {
585 public:
kosak53d49dc2015-01-08 03:03:09 +0000586 // Allows ReturnNull() to be used in any pointer-returning function. In C++11
587 // this is enforced by returning nullptr, and in non-C++11 by asserting a
588 // pointer type on compile time.
shiqiane35fdd92008-12-10 05:08:54 +0000589 template <typename Result, typename ArgumentTuple>
590 static Result Perform(const ArgumentTuple&) {
kosak53d49dc2015-01-08 03:03:09 +0000591#if GTEST_LANG_CXX11
592 return nullptr;
593#else
zhanyong.wan02f71062010-05-10 17:14:29 +0000594 GTEST_COMPILE_ASSERT_(internal::is_pointer<Result>::value,
zhanyong.wane0d051e2009-02-19 00:33:37 +0000595 ReturnNull_can_be_used_to_return_a_pointer_only);
shiqiane35fdd92008-12-10 05:08:54 +0000596 return NULL;
kosak53d49dc2015-01-08 03:03:09 +0000597#endif // GTEST_LANG_CXX11
shiqiane35fdd92008-12-10 05:08:54 +0000598 }
599};
600
601// Implements the Return() action.
602class ReturnVoidAction {
603 public:
604 // Allows Return() to be used in any void-returning function.
605 template <typename Result, typename ArgumentTuple>
606 static void Perform(const ArgumentTuple&) {
607 CompileAssertTypesEqual<void, Result>();
608 }
609};
610
611// Implements the polymorphic ReturnRef(x) action, which can be used
612// in any function that returns a reference to the type of x,
613// regardless of the argument types.
614template <typename T>
615class ReturnRefAction {
616 public:
617 // Constructs a ReturnRefAction object from the reference to be returned.
618 explicit ReturnRefAction(T& ref) : ref_(ref) {} // NOLINT
619
620 // This template type conversion operator allows ReturnRef(x) to be
621 // used in ANY function that returns a reference to x's type.
622 template <typename F>
623 operator Action<F>() const {
624 typedef typename Function<F>::Result Result;
625 // Asserts that the function return type is a reference. This
626 // catches the user error of using ReturnRef(x) when Return(x)
627 // should be used, and generates some helpful error message.
zhanyong.wan02f71062010-05-10 17:14:29 +0000628 GTEST_COMPILE_ASSERT_(internal::is_reference<Result>::value,
zhanyong.wane0d051e2009-02-19 00:33:37 +0000629 use_Return_instead_of_ReturnRef_to_return_a_value);
shiqiane35fdd92008-12-10 05:08:54 +0000630 return Action<F>(new Impl<F>(ref_));
631 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000632
shiqiane35fdd92008-12-10 05:08:54 +0000633 private:
634 // Implements the ReturnRef(x) action for a particular function type F.
635 template <typename F>
636 class Impl : public ActionInterface<F> {
637 public:
638 typedef typename Function<F>::Result Result;
639 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
640
641 explicit Impl(T& ref) : ref_(ref) {} // NOLINT
642
643 virtual Result Perform(const ArgumentTuple&) {
644 return ref_;
645 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000646
shiqiane35fdd92008-12-10 05:08:54 +0000647 private:
648 T& ref_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000649
650 GTEST_DISALLOW_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000651 };
652
653 T& ref_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000654
655 GTEST_DISALLOW_ASSIGN_(ReturnRefAction);
shiqiane35fdd92008-12-10 05:08:54 +0000656};
657
zhanyong.wane3bd0982010-07-03 00:16:42 +0000658// Implements the polymorphic ReturnRefOfCopy(x) action, which can be
659// used in any function that returns a reference to the type of x,
660// regardless of the argument types.
661template <typename T>
662class ReturnRefOfCopyAction {
663 public:
664 // Constructs a ReturnRefOfCopyAction object from the reference to
665 // be returned.
666 explicit ReturnRefOfCopyAction(const T& value) : value_(value) {} // NOLINT
667
668 // This template type conversion operator allows ReturnRefOfCopy(x) to be
669 // used in ANY function that returns a reference to x's type.
670 template <typename F>
671 operator Action<F>() const {
672 typedef typename Function<F>::Result Result;
673 // Asserts that the function return type is a reference. This
674 // catches the user error of using ReturnRefOfCopy(x) when Return(x)
675 // should be used, and generates some helpful error message.
676 GTEST_COMPILE_ASSERT_(
677 internal::is_reference<Result>::value,
678 use_Return_instead_of_ReturnRefOfCopy_to_return_a_value);
679 return Action<F>(new Impl<F>(value_));
680 }
681
682 private:
683 // Implements the ReturnRefOfCopy(x) action for a particular function type F.
684 template <typename F>
685 class Impl : public ActionInterface<F> {
686 public:
687 typedef typename Function<F>::Result Result;
688 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
689
690 explicit Impl(const T& value) : value_(value) {} // NOLINT
691
692 virtual Result Perform(const ArgumentTuple&) {
693 return value_;
694 }
695
696 private:
697 T value_;
698
699 GTEST_DISALLOW_ASSIGN_(Impl);
700 };
701
702 const T value_;
703
704 GTEST_DISALLOW_ASSIGN_(ReturnRefOfCopyAction);
705};
706
shiqiane35fdd92008-12-10 05:08:54 +0000707// Implements the polymorphic DoDefault() action.
708class DoDefaultAction {
709 public:
710 // This template type conversion operator allows DoDefault() to be
711 // used in any function.
712 template <typename F>
zhanyong.waned6c9272011-02-23 19:39:27 +0000713 operator Action<F>() const { return Action<F>(NULL); }
shiqiane35fdd92008-12-10 05:08:54 +0000714};
715
716// Implements the Assign action to set a given pointer referent to a
717// particular value.
718template <typename T1, typename T2>
719class AssignAction {
720 public:
721 AssignAction(T1* ptr, T2 value) : ptr_(ptr), value_(value) {}
722
723 template <typename Result, typename ArgumentTuple>
zhanyong.wan3fbd2dd2009-03-26 19:06:45 +0000724 void Perform(const ArgumentTuple& /* args */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000725 *ptr_ = value_;
726 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000727
shiqiane35fdd92008-12-10 05:08:54 +0000728 private:
729 T1* const ptr_;
730 const T2 value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000731
732 GTEST_DISALLOW_ASSIGN_(AssignAction);
shiqiane35fdd92008-12-10 05:08:54 +0000733};
734
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000735#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000736
shiqiane35fdd92008-12-10 05:08:54 +0000737// Implements the SetErrnoAndReturn action to simulate return from
738// various system calls and libc functions.
739template <typename T>
740class SetErrnoAndReturnAction {
741 public:
742 SetErrnoAndReturnAction(int errno_value, T result)
743 : errno_(errno_value),
744 result_(result) {}
745 template <typename Result, typename ArgumentTuple>
zhanyong.wan3fbd2dd2009-03-26 19:06:45 +0000746 Result Perform(const ArgumentTuple& /* args */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000747 errno = errno_;
748 return result_;
749 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000750
shiqiane35fdd92008-12-10 05:08:54 +0000751 private:
752 const int errno_;
753 const T result_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000754
755 GTEST_DISALLOW_ASSIGN_(SetErrnoAndReturnAction);
shiqiane35fdd92008-12-10 05:08:54 +0000756};
757
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000758#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000759
shiqiane35fdd92008-12-10 05:08:54 +0000760// Implements the SetArgumentPointee<N>(x) action for any function
761// whose N-th argument (0-based) is a pointer to x's type. The
762// template parameter kIsProto is true iff type A is ProtocolMessage,
763// proto2::Message, or a sub-class of those.
764template <size_t N, typename A, bool kIsProto>
765class SetArgumentPointeeAction {
766 public:
767 // Constructs an action that sets the variable pointed to by the
768 // N-th function argument to 'value'.
769 explicit SetArgumentPointeeAction(const A& value) : value_(value) {}
770
771 template <typename Result, typename ArgumentTuple>
772 void Perform(const ArgumentTuple& args) const {
773 CompileAssertTypesEqual<void, Result>();
kosakbd018832014-04-02 20:30:00 +0000774 *::testing::get<N>(args) = value_;
shiqiane35fdd92008-12-10 05:08:54 +0000775 }
776
777 private:
778 const A value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000779
780 GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
shiqiane35fdd92008-12-10 05:08:54 +0000781};
782
783template <size_t N, typename Proto>
784class SetArgumentPointeeAction<N, Proto, true> {
785 public:
786 // Constructs an action that sets the variable pointed to by the
787 // N-th function argument to 'proto'. Both ProtocolMessage and
788 // proto2::Message have the CopyFrom() method, so the same
789 // implementation works for both.
790 explicit SetArgumentPointeeAction(const Proto& proto) : proto_(new Proto) {
791 proto_->CopyFrom(proto);
792 }
793
794 template <typename Result, typename ArgumentTuple>
795 void Perform(const ArgumentTuple& args) const {
796 CompileAssertTypesEqual<void, Result>();
kosakbd018832014-04-02 20:30:00 +0000797 ::testing::get<N>(args)->CopyFrom(*proto_);
shiqiane35fdd92008-12-10 05:08:54 +0000798 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000799
shiqiane35fdd92008-12-10 05:08:54 +0000800 private:
801 const internal::linked_ptr<Proto> proto_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000802
803 GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
shiqiane35fdd92008-12-10 05:08:54 +0000804};
805
shiqiane35fdd92008-12-10 05:08:54 +0000806// Implements the InvokeWithoutArgs(f) action. The template argument
807// FunctionImpl is the implementation type of f, which can be either a
808// function pointer or a functor. InvokeWithoutArgs(f) can be used as an
809// Action<F> as long as f's type is compatible with F (i.e. f can be
810// assigned to a tr1::function<F>).
811template <typename FunctionImpl>
812class InvokeWithoutArgsAction {
813 public:
814 // The c'tor makes a copy of function_impl (either a function
815 // pointer or a functor).
816 explicit InvokeWithoutArgsAction(FunctionImpl function_impl)
817 : function_impl_(function_impl) {}
818
819 // Allows InvokeWithoutArgs(f) to be used as any action whose type is
820 // compatible with f.
821 template <typename Result, typename ArgumentTuple>
822 Result Perform(const ArgumentTuple&) { return function_impl_(); }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000823
shiqiane35fdd92008-12-10 05:08:54 +0000824 private:
825 FunctionImpl function_impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000826
827 GTEST_DISALLOW_ASSIGN_(InvokeWithoutArgsAction);
shiqiane35fdd92008-12-10 05:08:54 +0000828};
829
830// Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action.
831template <class Class, typename MethodPtr>
832class InvokeMethodWithoutArgsAction {
833 public:
834 InvokeMethodWithoutArgsAction(Class* obj_ptr, MethodPtr method_ptr)
835 : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
836
837 template <typename Result, typename ArgumentTuple>
838 Result Perform(const ArgumentTuple&) const {
839 return (obj_ptr_->*method_ptr_)();
840 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000841
shiqiane35fdd92008-12-10 05:08:54 +0000842 private:
843 Class* const obj_ptr_;
844 const MethodPtr method_ptr_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000845
846 GTEST_DISALLOW_ASSIGN_(InvokeMethodWithoutArgsAction);
shiqiane35fdd92008-12-10 05:08:54 +0000847};
848
849// Implements the IgnoreResult(action) action.
850template <typename A>
851class IgnoreResultAction {
852 public:
853 explicit IgnoreResultAction(const A& action) : action_(action) {}
854
855 template <typename F>
856 operator Action<F>() const {
857 // Assert statement belongs here because this is the best place to verify
858 // conditions on F. It produces the clearest error messages
859 // in most compilers.
860 // Impl really belongs in this scope as a local class but can't
861 // because MSVC produces duplicate symbols in different translation units
862 // in this case. Until MS fixes that bug we put Impl into the class scope
863 // and put the typedef both here (for use in assert statement) and
864 // in the Impl class. But both definitions must be the same.
865 typedef typename internal::Function<F>::Result Result;
866
867 // Asserts at compile time that F returns void.
868 CompileAssertTypesEqual<void, Result>();
869
870 return Action<F>(new Impl<F>(action_));
871 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000872
shiqiane35fdd92008-12-10 05:08:54 +0000873 private:
874 template <typename F>
875 class Impl : public ActionInterface<F> {
876 public:
877 typedef typename internal::Function<F>::Result Result;
878 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
879
880 explicit Impl(const A& action) : action_(action) {}
881
882 virtual void Perform(const ArgumentTuple& args) {
883 // Performs the action and ignores its result.
884 action_.Perform(args);
885 }
886
887 private:
888 // Type OriginalFunction is the same as F except that its return
889 // type is IgnoredValue.
890 typedef typename internal::Function<F>::MakeResultIgnoredValue
891 OriginalFunction;
892
893 const Action<OriginalFunction> action_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000894
895 GTEST_DISALLOW_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000896 };
897
898 const A action_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000899
900 GTEST_DISALLOW_ASSIGN_(IgnoreResultAction);
shiqiane35fdd92008-12-10 05:08:54 +0000901};
902
zhanyong.wana18423e2009-07-22 23:58:19 +0000903// A ReferenceWrapper<T> object represents a reference to type T,
904// which can be either const or not. It can be explicitly converted
905// from, and implicitly converted to, a T&. Unlike a reference,
906// ReferenceWrapper<T> can be copied and can survive template type
907// inference. This is used to support by-reference arguments in the
908// InvokeArgument<N>(...) action. The idea was from "reference
909// wrappers" in tr1, which we don't have in our source tree yet.
910template <typename T>
911class ReferenceWrapper {
912 public:
913 // Constructs a ReferenceWrapper<T> object from a T&.
914 explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {} // NOLINT
915
916 // Allows a ReferenceWrapper<T> object to be implicitly converted to
917 // a T&.
918 operator T&() const { return *pointer_; }
919 private:
920 T* pointer_;
921};
922
923// Allows the expression ByRef(x) to be printed as a reference to x.
924template <typename T>
925void PrintTo(const ReferenceWrapper<T>& ref, ::std::ostream* os) {
926 T& value = ref;
927 UniversalPrinter<T&>::Print(value, os);
928}
929
930// Does two actions sequentially. Used for implementing the DoAll(a1,
931// a2, ...) action.
932template <typename Action1, typename Action2>
933class DoBothAction {
934 public:
935 DoBothAction(Action1 action1, Action2 action2)
936 : action1_(action1), action2_(action2) {}
937
938 // This template type conversion operator allows DoAll(a1, ..., a_n)
939 // to be used in ANY function of compatible type.
940 template <typename F>
941 operator Action<F>() const {
942 return Action<F>(new Impl<F>(action1_, action2_));
943 }
944
945 private:
946 // Implements the DoAll(...) action for a particular function type F.
947 template <typename F>
948 class Impl : public ActionInterface<F> {
949 public:
950 typedef typename Function<F>::Result Result;
951 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
952 typedef typename Function<F>::MakeResultVoid VoidResult;
953
954 Impl(const Action<VoidResult>& action1, const Action<F>& action2)
955 : action1_(action1), action2_(action2) {}
956
957 virtual Result Perform(const ArgumentTuple& args) {
958 action1_.Perform(args);
959 return action2_.Perform(args);
960 }
961
962 private:
963 const Action<VoidResult> action1_;
964 const Action<F> action2_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000965
966 GTEST_DISALLOW_ASSIGN_(Impl);
zhanyong.wana18423e2009-07-22 23:58:19 +0000967 };
968
969 Action1 action1_;
970 Action2 action2_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000971
972 GTEST_DISALLOW_ASSIGN_(DoBothAction);
zhanyong.wana18423e2009-07-22 23:58:19 +0000973};
974
shiqiane35fdd92008-12-10 05:08:54 +0000975} // namespace internal
976
977// An Unused object can be implicitly constructed from ANY value.
978// This is handy when defining actions that ignore some or all of the
979// mock function arguments. For example, given
980//
981// MOCK_METHOD3(Foo, double(const string& label, double x, double y));
982// MOCK_METHOD3(Bar, double(int index, double x, double y));
983//
984// instead of
985//
986// double DistanceToOriginWithLabel(const string& label, double x, double y) {
987// return sqrt(x*x + y*y);
988// }
989// double DistanceToOriginWithIndex(int index, double x, double y) {
990// return sqrt(x*x + y*y);
991// }
992// ...
993// EXEPCT_CALL(mock, Foo("abc", _, _))
994// .WillOnce(Invoke(DistanceToOriginWithLabel));
995// EXEPCT_CALL(mock, Bar(5, _, _))
996// .WillOnce(Invoke(DistanceToOriginWithIndex));
997//
998// you could write
999//
1000// // We can declare any uninteresting argument as Unused.
1001// double DistanceToOrigin(Unused, double x, double y) {
1002// return sqrt(x*x + y*y);
1003// }
1004// ...
1005// EXEPCT_CALL(mock, Foo("abc", _, _)).WillOnce(Invoke(DistanceToOrigin));
1006// EXEPCT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin));
1007typedef internal::IgnoredValue Unused;
1008
1009// This constructor allows us to turn an Action<From> object into an
1010// Action<To>, as long as To's arguments can be implicitly converted
1011// to From's and From's return type cann be implicitly converted to
1012// To's.
1013template <typename To>
1014template <typename From>
1015Action<To>::Action(const Action<From>& from)
1016 : impl_(new internal::ActionAdaptor<To, From>(from)) {}
1017
1018// Creates an action that returns 'value'. 'value' is passed by value
1019// instead of const reference - otherwise Return("string literal")
1020// will trigger a compiler error about using array as initializer.
1021template <typename R>
1022internal::ReturnAction<R> Return(R value) {
kosak3d1c78b2014-11-17 00:56:52 +00001023 return internal::ReturnAction<R>(internal::move(value));
shiqiane35fdd92008-12-10 05:08:54 +00001024}
1025
1026// Creates an action that returns NULL.
1027inline PolymorphicAction<internal::ReturnNullAction> ReturnNull() {
1028 return MakePolymorphicAction(internal::ReturnNullAction());
1029}
1030
1031// Creates an action that returns from a void function.
1032inline PolymorphicAction<internal::ReturnVoidAction> Return() {
1033 return MakePolymorphicAction(internal::ReturnVoidAction());
1034}
1035
1036// Creates an action that returns the reference to a variable.
1037template <typename R>
1038inline internal::ReturnRefAction<R> ReturnRef(R& x) { // NOLINT
1039 return internal::ReturnRefAction<R>(x);
1040}
1041
zhanyong.wane3bd0982010-07-03 00:16:42 +00001042// Creates an action that returns the reference to a copy of the
1043// argument. The copy is created when the action is constructed and
1044// lives as long as the action.
1045template <typename R>
1046inline internal::ReturnRefOfCopyAction<R> ReturnRefOfCopy(const R& x) {
1047 return internal::ReturnRefOfCopyAction<R>(x);
1048}
1049
kosak3d1c78b2014-11-17 00:56:52 +00001050// Modifies the parent action (a Return() action) to perform a move of the
1051// argument instead of a copy.
1052// Return(ByMove()) actions can only be executed once and will assert this
1053// invariant.
1054template <typename R>
1055internal::ByMoveWrapper<R> ByMove(R x) {
1056 return internal::ByMoveWrapper<R>(internal::move(x));
1057}
1058
shiqiane35fdd92008-12-10 05:08:54 +00001059// Creates an action that does the default action for the give mock function.
1060inline internal::DoDefaultAction DoDefault() {
1061 return internal::DoDefaultAction();
1062}
1063
1064// Creates an action that sets the variable pointed by the N-th
1065// (0-based) function argument to 'value'.
1066template <size_t N, typename T>
1067PolymorphicAction<
1068 internal::SetArgumentPointeeAction<
1069 N, T, internal::IsAProtocolMessage<T>::value> >
zhanyong.wan59214832010-10-05 05:58:51 +00001070SetArgPointee(const T& x) {
1071 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1072 N, T, internal::IsAProtocolMessage<T>::value>(x));
1073}
zhanyong.wanfc8c6c42011-03-09 01:18:08 +00001074
1075#if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
zhanyong.wana684b5a2010-12-02 23:30:50 +00001076// This overload allows SetArgPointee() to accept a string literal.
zhanyong.wanfc8c6c42011-03-09 01:18:08 +00001077// GCC prior to the version 4.0 and Symbian C++ compiler cannot distinguish
1078// this overload from the templated version and emit a compile error.
zhanyong.wana684b5a2010-12-02 23:30:50 +00001079template <size_t N>
1080PolymorphicAction<
1081 internal::SetArgumentPointeeAction<N, const char*, false> >
1082SetArgPointee(const char* p) {
1083 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1084 N, const char*, false>(p));
1085}
zhanyong.wanfc8c6c42011-03-09 01:18:08 +00001086
1087template <size_t N>
1088PolymorphicAction<
1089 internal::SetArgumentPointeeAction<N, const wchar_t*, false> >
1090SetArgPointee(const wchar_t* p) {
1091 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1092 N, const wchar_t*, false>(p));
1093}
1094#endif
1095
zhanyong.wan59214832010-10-05 05:58:51 +00001096// The following version is DEPRECATED.
1097template <size_t N, typename T>
1098PolymorphicAction<
1099 internal::SetArgumentPointeeAction<
1100 N, T, internal::IsAProtocolMessage<T>::value> >
shiqiane35fdd92008-12-10 05:08:54 +00001101SetArgumentPointee(const T& x) {
1102 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1103 N, T, internal::IsAProtocolMessage<T>::value>(x));
1104}
1105
shiqiane35fdd92008-12-10 05:08:54 +00001106// Creates an action that sets a pointer referent to a given value.
1107template <typename T1, typename T2>
1108PolymorphicAction<internal::AssignAction<T1, T2> > Assign(T1* ptr, T2 val) {
1109 return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val));
1110}
1111
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001112#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001113
shiqiane35fdd92008-12-10 05:08:54 +00001114// Creates an action that sets errno and returns the appropriate error.
1115template <typename T>
1116PolymorphicAction<internal::SetErrnoAndReturnAction<T> >
1117SetErrnoAndReturn(int errval, T result) {
1118 return MakePolymorphicAction(
1119 internal::SetErrnoAndReturnAction<T>(errval, result));
1120}
1121
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001122#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001123
shiqiane35fdd92008-12-10 05:08:54 +00001124// Various overloads for InvokeWithoutArgs().
1125
1126// Creates an action that invokes 'function_impl' with no argument.
1127template <typename FunctionImpl>
1128PolymorphicAction<internal::InvokeWithoutArgsAction<FunctionImpl> >
1129InvokeWithoutArgs(FunctionImpl function_impl) {
1130 return MakePolymorphicAction(
1131 internal::InvokeWithoutArgsAction<FunctionImpl>(function_impl));
1132}
1133
1134// Creates an action that invokes the given method on the given object
1135// with no argument.
1136template <class Class, typename MethodPtr>
1137PolymorphicAction<internal::InvokeMethodWithoutArgsAction<Class, MethodPtr> >
1138InvokeWithoutArgs(Class* obj_ptr, MethodPtr method_ptr) {
1139 return MakePolymorphicAction(
1140 internal::InvokeMethodWithoutArgsAction<Class, MethodPtr>(
1141 obj_ptr, method_ptr));
1142}
1143
1144// Creates an action that performs an_action and throws away its
1145// result. In other words, it changes the return type of an_action to
1146// void. an_action MUST NOT return void, or the code won't compile.
1147template <typename A>
1148inline internal::IgnoreResultAction<A> IgnoreResult(const A& an_action) {
1149 return internal::IgnoreResultAction<A>(an_action);
1150}
1151
zhanyong.wana18423e2009-07-22 23:58:19 +00001152// Creates a reference wrapper for the given L-value. If necessary,
1153// you can explicitly specify the type of the reference. For example,
1154// suppose 'derived' is an object of type Derived, ByRef(derived)
1155// would wrap a Derived&. If you want to wrap a const Base& instead,
1156// where Base is a base class of Derived, just write:
1157//
1158// ByRef<const Base>(derived)
1159template <typename T>
1160inline internal::ReferenceWrapper<T> ByRef(T& l_value) { // NOLINT
1161 return internal::ReferenceWrapper<T>(l_value);
1162}
1163
shiqiane35fdd92008-12-10 05:08:54 +00001164} // namespace testing
1165
1166#endif // GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_