blob: f92e479f4e36d7060ed93bc1a3c74b5c96e0e6eb [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:
586 // Allows ReturnNull() to be used in any pointer-returning function.
587 template <typename Result, typename ArgumentTuple>
588 static Result Perform(const ArgumentTuple&) {
zhanyong.wan02f71062010-05-10 17:14:29 +0000589 GTEST_COMPILE_ASSERT_(internal::is_pointer<Result>::value,
zhanyong.wane0d051e2009-02-19 00:33:37 +0000590 ReturnNull_can_be_used_to_return_a_pointer_only);
shiqiane35fdd92008-12-10 05:08:54 +0000591 return NULL;
592 }
593};
594
595// Implements the Return() action.
596class ReturnVoidAction {
597 public:
598 // Allows Return() to be used in any void-returning function.
599 template <typename Result, typename ArgumentTuple>
600 static void Perform(const ArgumentTuple&) {
601 CompileAssertTypesEqual<void, Result>();
602 }
603};
604
605// Implements the polymorphic ReturnRef(x) action, which can be used
606// in any function that returns a reference to the type of x,
607// regardless of the argument types.
608template <typename T>
609class ReturnRefAction {
610 public:
611 // Constructs a ReturnRefAction object from the reference to be returned.
612 explicit ReturnRefAction(T& ref) : ref_(ref) {} // NOLINT
613
614 // This template type conversion operator allows ReturnRef(x) to be
615 // used in ANY function that returns a reference to x's type.
616 template <typename F>
617 operator Action<F>() const {
618 typedef typename Function<F>::Result Result;
619 // Asserts that the function return type is a reference. This
620 // catches the user error of using ReturnRef(x) when Return(x)
621 // should be used, and generates some helpful error message.
zhanyong.wan02f71062010-05-10 17:14:29 +0000622 GTEST_COMPILE_ASSERT_(internal::is_reference<Result>::value,
zhanyong.wane0d051e2009-02-19 00:33:37 +0000623 use_Return_instead_of_ReturnRef_to_return_a_value);
shiqiane35fdd92008-12-10 05:08:54 +0000624 return Action<F>(new Impl<F>(ref_));
625 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000626
shiqiane35fdd92008-12-10 05:08:54 +0000627 private:
628 // Implements the ReturnRef(x) action for a particular function type F.
629 template <typename F>
630 class Impl : public ActionInterface<F> {
631 public:
632 typedef typename Function<F>::Result Result;
633 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
634
635 explicit Impl(T& ref) : ref_(ref) {} // NOLINT
636
637 virtual Result Perform(const ArgumentTuple&) {
638 return ref_;
639 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000640
shiqiane35fdd92008-12-10 05:08:54 +0000641 private:
642 T& ref_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000643
644 GTEST_DISALLOW_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000645 };
646
647 T& ref_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000648
649 GTEST_DISALLOW_ASSIGN_(ReturnRefAction);
shiqiane35fdd92008-12-10 05:08:54 +0000650};
651
zhanyong.wane3bd0982010-07-03 00:16:42 +0000652// Implements the polymorphic ReturnRefOfCopy(x) action, which can be
653// used in any function that returns a reference to the type of x,
654// regardless of the argument types.
655template <typename T>
656class ReturnRefOfCopyAction {
657 public:
658 // Constructs a ReturnRefOfCopyAction object from the reference to
659 // be returned.
660 explicit ReturnRefOfCopyAction(const T& value) : value_(value) {} // NOLINT
661
662 // This template type conversion operator allows ReturnRefOfCopy(x) to be
663 // used in ANY function that returns a reference to x's type.
664 template <typename F>
665 operator Action<F>() const {
666 typedef typename Function<F>::Result Result;
667 // Asserts that the function return type is a reference. This
668 // catches the user error of using ReturnRefOfCopy(x) when Return(x)
669 // should be used, and generates some helpful error message.
670 GTEST_COMPILE_ASSERT_(
671 internal::is_reference<Result>::value,
672 use_Return_instead_of_ReturnRefOfCopy_to_return_a_value);
673 return Action<F>(new Impl<F>(value_));
674 }
675
676 private:
677 // Implements the ReturnRefOfCopy(x) action for a particular function type F.
678 template <typename F>
679 class Impl : public ActionInterface<F> {
680 public:
681 typedef typename Function<F>::Result Result;
682 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
683
684 explicit Impl(const T& value) : value_(value) {} // NOLINT
685
686 virtual Result Perform(const ArgumentTuple&) {
687 return value_;
688 }
689
690 private:
691 T value_;
692
693 GTEST_DISALLOW_ASSIGN_(Impl);
694 };
695
696 const T value_;
697
698 GTEST_DISALLOW_ASSIGN_(ReturnRefOfCopyAction);
699};
700
shiqiane35fdd92008-12-10 05:08:54 +0000701// Implements the polymorphic DoDefault() action.
702class DoDefaultAction {
703 public:
704 // This template type conversion operator allows DoDefault() to be
705 // used in any function.
706 template <typename F>
zhanyong.waned6c9272011-02-23 19:39:27 +0000707 operator Action<F>() const { return Action<F>(NULL); }
shiqiane35fdd92008-12-10 05:08:54 +0000708};
709
710// Implements the Assign action to set a given pointer referent to a
711// particular value.
712template <typename T1, typename T2>
713class AssignAction {
714 public:
715 AssignAction(T1* ptr, T2 value) : ptr_(ptr), value_(value) {}
716
717 template <typename Result, typename ArgumentTuple>
zhanyong.wan3fbd2dd2009-03-26 19:06:45 +0000718 void Perform(const ArgumentTuple& /* args */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000719 *ptr_ = value_;
720 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000721
shiqiane35fdd92008-12-10 05:08:54 +0000722 private:
723 T1* const ptr_;
724 const T2 value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000725
726 GTEST_DISALLOW_ASSIGN_(AssignAction);
shiqiane35fdd92008-12-10 05:08:54 +0000727};
728
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000729#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000730
shiqiane35fdd92008-12-10 05:08:54 +0000731// Implements the SetErrnoAndReturn action to simulate return from
732// various system calls and libc functions.
733template <typename T>
734class SetErrnoAndReturnAction {
735 public:
736 SetErrnoAndReturnAction(int errno_value, T result)
737 : errno_(errno_value),
738 result_(result) {}
739 template <typename Result, typename ArgumentTuple>
zhanyong.wan3fbd2dd2009-03-26 19:06:45 +0000740 Result Perform(const ArgumentTuple& /* args */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000741 errno = errno_;
742 return result_;
743 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000744
shiqiane35fdd92008-12-10 05:08:54 +0000745 private:
746 const int errno_;
747 const T result_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000748
749 GTEST_DISALLOW_ASSIGN_(SetErrnoAndReturnAction);
shiqiane35fdd92008-12-10 05:08:54 +0000750};
751
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000752#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000753
shiqiane35fdd92008-12-10 05:08:54 +0000754// Implements the SetArgumentPointee<N>(x) action for any function
755// whose N-th argument (0-based) is a pointer to x's type. The
756// template parameter kIsProto is true iff type A is ProtocolMessage,
757// proto2::Message, or a sub-class of those.
758template <size_t N, typename A, bool kIsProto>
759class SetArgumentPointeeAction {
760 public:
761 // Constructs an action that sets the variable pointed to by the
762 // N-th function argument to 'value'.
763 explicit SetArgumentPointeeAction(const A& value) : value_(value) {}
764
765 template <typename Result, typename ArgumentTuple>
766 void Perform(const ArgumentTuple& args) const {
767 CompileAssertTypesEqual<void, Result>();
kosakbd018832014-04-02 20:30:00 +0000768 *::testing::get<N>(args) = value_;
shiqiane35fdd92008-12-10 05:08:54 +0000769 }
770
771 private:
772 const A value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000773
774 GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
shiqiane35fdd92008-12-10 05:08:54 +0000775};
776
777template <size_t N, typename Proto>
778class SetArgumentPointeeAction<N, Proto, true> {
779 public:
780 // Constructs an action that sets the variable pointed to by the
781 // N-th function argument to 'proto'. Both ProtocolMessage and
782 // proto2::Message have the CopyFrom() method, so the same
783 // implementation works for both.
784 explicit SetArgumentPointeeAction(const Proto& proto) : proto_(new Proto) {
785 proto_->CopyFrom(proto);
786 }
787
788 template <typename Result, typename ArgumentTuple>
789 void Perform(const ArgumentTuple& args) const {
790 CompileAssertTypesEqual<void, Result>();
kosakbd018832014-04-02 20:30:00 +0000791 ::testing::get<N>(args)->CopyFrom(*proto_);
shiqiane35fdd92008-12-10 05:08:54 +0000792 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000793
shiqiane35fdd92008-12-10 05:08:54 +0000794 private:
795 const internal::linked_ptr<Proto> proto_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000796
797 GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
shiqiane35fdd92008-12-10 05:08:54 +0000798};
799
shiqiane35fdd92008-12-10 05:08:54 +0000800// Implements the InvokeWithoutArgs(f) action. The template argument
801// FunctionImpl is the implementation type of f, which can be either a
802// function pointer or a functor. InvokeWithoutArgs(f) can be used as an
803// Action<F> as long as f's type is compatible with F (i.e. f can be
804// assigned to a tr1::function<F>).
805template <typename FunctionImpl>
806class InvokeWithoutArgsAction {
807 public:
808 // The c'tor makes a copy of function_impl (either a function
809 // pointer or a functor).
810 explicit InvokeWithoutArgsAction(FunctionImpl function_impl)
811 : function_impl_(function_impl) {}
812
813 // Allows InvokeWithoutArgs(f) to be used as any action whose type is
814 // compatible with f.
815 template <typename Result, typename ArgumentTuple>
816 Result Perform(const ArgumentTuple&) { return function_impl_(); }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000817
shiqiane35fdd92008-12-10 05:08:54 +0000818 private:
819 FunctionImpl function_impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000820
821 GTEST_DISALLOW_ASSIGN_(InvokeWithoutArgsAction);
shiqiane35fdd92008-12-10 05:08:54 +0000822};
823
824// Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action.
825template <class Class, typename MethodPtr>
826class InvokeMethodWithoutArgsAction {
827 public:
828 InvokeMethodWithoutArgsAction(Class* obj_ptr, MethodPtr method_ptr)
829 : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
830
831 template <typename Result, typename ArgumentTuple>
832 Result Perform(const ArgumentTuple&) const {
833 return (obj_ptr_->*method_ptr_)();
834 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000835
shiqiane35fdd92008-12-10 05:08:54 +0000836 private:
837 Class* const obj_ptr_;
838 const MethodPtr method_ptr_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000839
840 GTEST_DISALLOW_ASSIGN_(InvokeMethodWithoutArgsAction);
shiqiane35fdd92008-12-10 05:08:54 +0000841};
842
843// Implements the IgnoreResult(action) action.
844template <typename A>
845class IgnoreResultAction {
846 public:
847 explicit IgnoreResultAction(const A& action) : action_(action) {}
848
849 template <typename F>
850 operator Action<F>() const {
851 // Assert statement belongs here because this is the best place to verify
852 // conditions on F. It produces the clearest error messages
853 // in most compilers.
854 // Impl really belongs in this scope as a local class but can't
855 // because MSVC produces duplicate symbols in different translation units
856 // in this case. Until MS fixes that bug we put Impl into the class scope
857 // and put the typedef both here (for use in assert statement) and
858 // in the Impl class. But both definitions must be the same.
859 typedef typename internal::Function<F>::Result Result;
860
861 // Asserts at compile time that F returns void.
862 CompileAssertTypesEqual<void, Result>();
863
864 return Action<F>(new Impl<F>(action_));
865 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000866
shiqiane35fdd92008-12-10 05:08:54 +0000867 private:
868 template <typename F>
869 class Impl : public ActionInterface<F> {
870 public:
871 typedef typename internal::Function<F>::Result Result;
872 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
873
874 explicit Impl(const A& action) : action_(action) {}
875
876 virtual void Perform(const ArgumentTuple& args) {
877 // Performs the action and ignores its result.
878 action_.Perform(args);
879 }
880
881 private:
882 // Type OriginalFunction is the same as F except that its return
883 // type is IgnoredValue.
884 typedef typename internal::Function<F>::MakeResultIgnoredValue
885 OriginalFunction;
886
887 const Action<OriginalFunction> action_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000888
889 GTEST_DISALLOW_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000890 };
891
892 const A action_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000893
894 GTEST_DISALLOW_ASSIGN_(IgnoreResultAction);
shiqiane35fdd92008-12-10 05:08:54 +0000895};
896
zhanyong.wana18423e2009-07-22 23:58:19 +0000897// A ReferenceWrapper<T> object represents a reference to type T,
898// which can be either const or not. It can be explicitly converted
899// from, and implicitly converted to, a T&. Unlike a reference,
900// ReferenceWrapper<T> can be copied and can survive template type
901// inference. This is used to support by-reference arguments in the
902// InvokeArgument<N>(...) action. The idea was from "reference
903// wrappers" in tr1, which we don't have in our source tree yet.
904template <typename T>
905class ReferenceWrapper {
906 public:
907 // Constructs a ReferenceWrapper<T> object from a T&.
908 explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {} // NOLINT
909
910 // Allows a ReferenceWrapper<T> object to be implicitly converted to
911 // a T&.
912 operator T&() const { return *pointer_; }
913 private:
914 T* pointer_;
915};
916
917// Allows the expression ByRef(x) to be printed as a reference to x.
918template <typename T>
919void PrintTo(const ReferenceWrapper<T>& ref, ::std::ostream* os) {
920 T& value = ref;
921 UniversalPrinter<T&>::Print(value, os);
922}
923
924// Does two actions sequentially. Used for implementing the DoAll(a1,
925// a2, ...) action.
926template <typename Action1, typename Action2>
927class DoBothAction {
928 public:
929 DoBothAction(Action1 action1, Action2 action2)
930 : action1_(action1), action2_(action2) {}
931
932 // This template type conversion operator allows DoAll(a1, ..., a_n)
933 // to be used in ANY function of compatible type.
934 template <typename F>
935 operator Action<F>() const {
936 return Action<F>(new Impl<F>(action1_, action2_));
937 }
938
939 private:
940 // Implements the DoAll(...) action for a particular function type F.
941 template <typename F>
942 class Impl : public ActionInterface<F> {
943 public:
944 typedef typename Function<F>::Result Result;
945 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
946 typedef typename Function<F>::MakeResultVoid VoidResult;
947
948 Impl(const Action<VoidResult>& action1, const Action<F>& action2)
949 : action1_(action1), action2_(action2) {}
950
951 virtual Result Perform(const ArgumentTuple& args) {
952 action1_.Perform(args);
953 return action2_.Perform(args);
954 }
955
956 private:
957 const Action<VoidResult> action1_;
958 const Action<F> action2_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000959
960 GTEST_DISALLOW_ASSIGN_(Impl);
zhanyong.wana18423e2009-07-22 23:58:19 +0000961 };
962
963 Action1 action1_;
964 Action2 action2_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000965
966 GTEST_DISALLOW_ASSIGN_(DoBothAction);
zhanyong.wana18423e2009-07-22 23:58:19 +0000967};
968
shiqiane35fdd92008-12-10 05:08:54 +0000969} // namespace internal
970
971// An Unused object can be implicitly constructed from ANY value.
972// This is handy when defining actions that ignore some or all of the
973// mock function arguments. For example, given
974//
975// MOCK_METHOD3(Foo, double(const string& label, double x, double y));
976// MOCK_METHOD3(Bar, double(int index, double x, double y));
977//
978// instead of
979//
980// double DistanceToOriginWithLabel(const string& label, double x, double y) {
981// return sqrt(x*x + y*y);
982// }
983// double DistanceToOriginWithIndex(int index, double x, double y) {
984// return sqrt(x*x + y*y);
985// }
986// ...
987// EXEPCT_CALL(mock, Foo("abc", _, _))
988// .WillOnce(Invoke(DistanceToOriginWithLabel));
989// EXEPCT_CALL(mock, Bar(5, _, _))
990// .WillOnce(Invoke(DistanceToOriginWithIndex));
991//
992// you could write
993//
994// // We can declare any uninteresting argument as Unused.
995// double DistanceToOrigin(Unused, double x, double y) {
996// return sqrt(x*x + y*y);
997// }
998// ...
999// EXEPCT_CALL(mock, Foo("abc", _, _)).WillOnce(Invoke(DistanceToOrigin));
1000// EXEPCT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin));
1001typedef internal::IgnoredValue Unused;
1002
1003// This constructor allows us to turn an Action<From> object into an
1004// Action<To>, as long as To's arguments can be implicitly converted
1005// to From's and From's return type cann be implicitly converted to
1006// To's.
1007template <typename To>
1008template <typename From>
1009Action<To>::Action(const Action<From>& from)
1010 : impl_(new internal::ActionAdaptor<To, From>(from)) {}
1011
1012// Creates an action that returns 'value'. 'value' is passed by value
1013// instead of const reference - otherwise Return("string literal")
1014// will trigger a compiler error about using array as initializer.
1015template <typename R>
1016internal::ReturnAction<R> Return(R value) {
kosak3d1c78b2014-11-17 00:56:52 +00001017 return internal::ReturnAction<R>(internal::move(value));
shiqiane35fdd92008-12-10 05:08:54 +00001018}
1019
1020// Creates an action that returns NULL.
1021inline PolymorphicAction<internal::ReturnNullAction> ReturnNull() {
1022 return MakePolymorphicAction(internal::ReturnNullAction());
1023}
1024
1025// Creates an action that returns from a void function.
1026inline PolymorphicAction<internal::ReturnVoidAction> Return() {
1027 return MakePolymorphicAction(internal::ReturnVoidAction());
1028}
1029
1030// Creates an action that returns the reference to a variable.
1031template <typename R>
1032inline internal::ReturnRefAction<R> ReturnRef(R& x) { // NOLINT
1033 return internal::ReturnRefAction<R>(x);
1034}
1035
zhanyong.wane3bd0982010-07-03 00:16:42 +00001036// Creates an action that returns the reference to a copy of the
1037// argument. The copy is created when the action is constructed and
1038// lives as long as the action.
1039template <typename R>
1040inline internal::ReturnRefOfCopyAction<R> ReturnRefOfCopy(const R& x) {
1041 return internal::ReturnRefOfCopyAction<R>(x);
1042}
1043
kosak3d1c78b2014-11-17 00:56:52 +00001044// Modifies the parent action (a Return() action) to perform a move of the
1045// argument instead of a copy.
1046// Return(ByMove()) actions can only be executed once and will assert this
1047// invariant.
1048template <typename R>
1049internal::ByMoveWrapper<R> ByMove(R x) {
1050 return internal::ByMoveWrapper<R>(internal::move(x));
1051}
1052
shiqiane35fdd92008-12-10 05:08:54 +00001053// Creates an action that does the default action for the give mock function.
1054inline internal::DoDefaultAction DoDefault() {
1055 return internal::DoDefaultAction();
1056}
1057
1058// Creates an action that sets the variable pointed by the N-th
1059// (0-based) function argument to 'value'.
1060template <size_t N, typename T>
1061PolymorphicAction<
1062 internal::SetArgumentPointeeAction<
1063 N, T, internal::IsAProtocolMessage<T>::value> >
zhanyong.wan59214832010-10-05 05:58:51 +00001064SetArgPointee(const T& x) {
1065 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1066 N, T, internal::IsAProtocolMessage<T>::value>(x));
1067}
zhanyong.wanfc8c6c42011-03-09 01:18:08 +00001068
1069#if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
zhanyong.wana684b5a2010-12-02 23:30:50 +00001070// This overload allows SetArgPointee() to accept a string literal.
zhanyong.wanfc8c6c42011-03-09 01:18:08 +00001071// GCC prior to the version 4.0 and Symbian C++ compiler cannot distinguish
1072// this overload from the templated version and emit a compile error.
zhanyong.wana684b5a2010-12-02 23:30:50 +00001073template <size_t N>
1074PolymorphicAction<
1075 internal::SetArgumentPointeeAction<N, const char*, false> >
1076SetArgPointee(const char* p) {
1077 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1078 N, const char*, false>(p));
1079}
zhanyong.wanfc8c6c42011-03-09 01:18:08 +00001080
1081template <size_t N>
1082PolymorphicAction<
1083 internal::SetArgumentPointeeAction<N, const wchar_t*, false> >
1084SetArgPointee(const wchar_t* p) {
1085 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1086 N, const wchar_t*, false>(p));
1087}
1088#endif
1089
zhanyong.wan59214832010-10-05 05:58:51 +00001090// The following version is DEPRECATED.
1091template <size_t N, typename T>
1092PolymorphicAction<
1093 internal::SetArgumentPointeeAction<
1094 N, T, internal::IsAProtocolMessage<T>::value> >
shiqiane35fdd92008-12-10 05:08:54 +00001095SetArgumentPointee(const T& x) {
1096 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1097 N, T, internal::IsAProtocolMessage<T>::value>(x));
1098}
1099
shiqiane35fdd92008-12-10 05:08:54 +00001100// Creates an action that sets a pointer referent to a given value.
1101template <typename T1, typename T2>
1102PolymorphicAction<internal::AssignAction<T1, T2> > Assign(T1* ptr, T2 val) {
1103 return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val));
1104}
1105
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001106#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001107
shiqiane35fdd92008-12-10 05:08:54 +00001108// Creates an action that sets errno and returns the appropriate error.
1109template <typename T>
1110PolymorphicAction<internal::SetErrnoAndReturnAction<T> >
1111SetErrnoAndReturn(int errval, T result) {
1112 return MakePolymorphicAction(
1113 internal::SetErrnoAndReturnAction<T>(errval, result));
1114}
1115
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001116#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001117
shiqiane35fdd92008-12-10 05:08:54 +00001118// Various overloads for InvokeWithoutArgs().
1119
1120// Creates an action that invokes 'function_impl' with no argument.
1121template <typename FunctionImpl>
1122PolymorphicAction<internal::InvokeWithoutArgsAction<FunctionImpl> >
1123InvokeWithoutArgs(FunctionImpl function_impl) {
1124 return MakePolymorphicAction(
1125 internal::InvokeWithoutArgsAction<FunctionImpl>(function_impl));
1126}
1127
1128// Creates an action that invokes the given method on the given object
1129// with no argument.
1130template <class Class, typename MethodPtr>
1131PolymorphicAction<internal::InvokeMethodWithoutArgsAction<Class, MethodPtr> >
1132InvokeWithoutArgs(Class* obj_ptr, MethodPtr method_ptr) {
1133 return MakePolymorphicAction(
1134 internal::InvokeMethodWithoutArgsAction<Class, MethodPtr>(
1135 obj_ptr, method_ptr));
1136}
1137
1138// Creates an action that performs an_action and throws away its
1139// result. In other words, it changes the return type of an_action to
1140// void. an_action MUST NOT return void, or the code won't compile.
1141template <typename A>
1142inline internal::IgnoreResultAction<A> IgnoreResult(const A& an_action) {
1143 return internal::IgnoreResultAction<A>(an_action);
1144}
1145
zhanyong.wana18423e2009-07-22 23:58:19 +00001146// Creates a reference wrapper for the given L-value. If necessary,
1147// you can explicitly specify the type of the reference. For example,
1148// suppose 'derived' is an object of type Derived, ByRef(derived)
1149// would wrap a Derived&. If you want to wrap a const Base& instead,
1150// where Base is a base class of Derived, just write:
1151//
1152// ByRef<const Base>(derived)
1153template <typename T>
1154inline internal::ReferenceWrapper<T> ByRef(T& l_value) { // NOLINT
1155 return internal::ReferenceWrapper<T>(l_value);
1156}
1157
shiqiane35fdd92008-12-10 05:08:54 +00001158} // namespace testing
1159
1160#endif // GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_