blob: de5020483fcd337ac3ed9d5b99da2c8eb8c784cb [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)
537 : value_(ImplicitCast_<Result>(*value)) {}
shiqiane35fdd92008-12-10 05:08:54 +0000538
539 virtual Result Perform(const ArgumentTuple&) { return value_; }
540
541 private:
kosak3d1c78b2014-11-17 00:56:52 +0000542 GTEST_COMPILE_ASSERT_(!is_reference<Result>::value,
vladloseva070cbd2009-11-18 00:09:28 +0000543 Result_cannot_be_a_reference_type);
544 Result value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000545
546 GTEST_DISALLOW_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000547 };
548
kosak3d1c78b2014-11-17 00:56:52 +0000549 // Partially specialize for ByMoveWrapper. This version of ReturnAction will
550 // move its contents instead.
551 template <typename R_, typename F>
552 class Impl<ByMoveWrapper<R_>, F> : public ActionInterface<F> {
553 public:
554 typedef typename Function<F>::Result Result;
555 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
556
557 explicit Impl(const linked_ptr<R>& wrapper)
558 : performed_(false), wrapper_(wrapper) {}
559
560 virtual Result Perform(const ArgumentTuple&) {
561 GTEST_CHECK_(!performed_)
562 << "A ByMove() action should only be performed once.";
563 performed_ = true;
kosakd370f852014-11-17 01:14:16 +0000564 return internal::move(wrapper_->payload);
kosak3d1c78b2014-11-17 00:56:52 +0000565 }
566
567 private:
568 bool performed_;
569 const linked_ptr<R> wrapper_;
570
571 GTEST_DISALLOW_ASSIGN_(Impl);
572 };
573
574 const linked_ptr<R> value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000575
576 GTEST_DISALLOW_ASSIGN_(ReturnAction);
shiqiane35fdd92008-12-10 05:08:54 +0000577};
578
579// Implements the ReturnNull() action.
580class ReturnNullAction {
581 public:
582 // Allows ReturnNull() to be used in any pointer-returning function.
583 template <typename Result, typename ArgumentTuple>
584 static Result Perform(const ArgumentTuple&) {
zhanyong.wan02f71062010-05-10 17:14:29 +0000585 GTEST_COMPILE_ASSERT_(internal::is_pointer<Result>::value,
zhanyong.wane0d051e2009-02-19 00:33:37 +0000586 ReturnNull_can_be_used_to_return_a_pointer_only);
shiqiane35fdd92008-12-10 05:08:54 +0000587 return NULL;
588 }
589};
590
591// Implements the Return() action.
592class ReturnVoidAction {
593 public:
594 // Allows Return() to be used in any void-returning function.
595 template <typename Result, typename ArgumentTuple>
596 static void Perform(const ArgumentTuple&) {
597 CompileAssertTypesEqual<void, Result>();
598 }
599};
600
601// Implements the polymorphic ReturnRef(x) action, which can be used
602// in any function that returns a reference to the type of x,
603// regardless of the argument types.
604template <typename T>
605class ReturnRefAction {
606 public:
607 // Constructs a ReturnRefAction object from the reference to be returned.
608 explicit ReturnRefAction(T& ref) : ref_(ref) {} // NOLINT
609
610 // This template type conversion operator allows ReturnRef(x) to be
611 // used in ANY function that returns a reference to x's type.
612 template <typename F>
613 operator Action<F>() const {
614 typedef typename Function<F>::Result Result;
615 // Asserts that the function return type is a reference. This
616 // catches the user error of using ReturnRef(x) when Return(x)
617 // should be used, and generates some helpful error message.
zhanyong.wan02f71062010-05-10 17:14:29 +0000618 GTEST_COMPILE_ASSERT_(internal::is_reference<Result>::value,
zhanyong.wane0d051e2009-02-19 00:33:37 +0000619 use_Return_instead_of_ReturnRef_to_return_a_value);
shiqiane35fdd92008-12-10 05:08:54 +0000620 return Action<F>(new Impl<F>(ref_));
621 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000622
shiqiane35fdd92008-12-10 05:08:54 +0000623 private:
624 // Implements the ReturnRef(x) action for a particular function type F.
625 template <typename F>
626 class Impl : public ActionInterface<F> {
627 public:
628 typedef typename Function<F>::Result Result;
629 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
630
631 explicit Impl(T& ref) : ref_(ref) {} // NOLINT
632
633 virtual Result Perform(const ArgumentTuple&) {
634 return ref_;
635 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000636
shiqiane35fdd92008-12-10 05:08:54 +0000637 private:
638 T& ref_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000639
640 GTEST_DISALLOW_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000641 };
642
643 T& ref_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000644
645 GTEST_DISALLOW_ASSIGN_(ReturnRefAction);
shiqiane35fdd92008-12-10 05:08:54 +0000646};
647
zhanyong.wane3bd0982010-07-03 00:16:42 +0000648// Implements the polymorphic ReturnRefOfCopy(x) action, which can be
649// used in any function that returns a reference to the type of x,
650// regardless of the argument types.
651template <typename T>
652class ReturnRefOfCopyAction {
653 public:
654 // Constructs a ReturnRefOfCopyAction object from the reference to
655 // be returned.
656 explicit ReturnRefOfCopyAction(const T& value) : value_(value) {} // NOLINT
657
658 // This template type conversion operator allows ReturnRefOfCopy(x) to be
659 // used in ANY function that returns a reference to x's type.
660 template <typename F>
661 operator Action<F>() const {
662 typedef typename Function<F>::Result Result;
663 // Asserts that the function return type is a reference. This
664 // catches the user error of using ReturnRefOfCopy(x) when Return(x)
665 // should be used, and generates some helpful error message.
666 GTEST_COMPILE_ASSERT_(
667 internal::is_reference<Result>::value,
668 use_Return_instead_of_ReturnRefOfCopy_to_return_a_value);
669 return Action<F>(new Impl<F>(value_));
670 }
671
672 private:
673 // Implements the ReturnRefOfCopy(x) action for a particular function type F.
674 template <typename F>
675 class Impl : public ActionInterface<F> {
676 public:
677 typedef typename Function<F>::Result Result;
678 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
679
680 explicit Impl(const T& value) : value_(value) {} // NOLINT
681
682 virtual Result Perform(const ArgumentTuple&) {
683 return value_;
684 }
685
686 private:
687 T value_;
688
689 GTEST_DISALLOW_ASSIGN_(Impl);
690 };
691
692 const T value_;
693
694 GTEST_DISALLOW_ASSIGN_(ReturnRefOfCopyAction);
695};
696
shiqiane35fdd92008-12-10 05:08:54 +0000697// Implements the polymorphic DoDefault() action.
698class DoDefaultAction {
699 public:
700 // This template type conversion operator allows DoDefault() to be
701 // used in any function.
702 template <typename F>
zhanyong.waned6c9272011-02-23 19:39:27 +0000703 operator Action<F>() const { return Action<F>(NULL); }
shiqiane35fdd92008-12-10 05:08:54 +0000704};
705
706// Implements the Assign action to set a given pointer referent to a
707// particular value.
708template <typename T1, typename T2>
709class AssignAction {
710 public:
711 AssignAction(T1* ptr, T2 value) : ptr_(ptr), value_(value) {}
712
713 template <typename Result, typename ArgumentTuple>
zhanyong.wan3fbd2dd2009-03-26 19:06:45 +0000714 void Perform(const ArgumentTuple& /* args */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000715 *ptr_ = value_;
716 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000717
shiqiane35fdd92008-12-10 05:08:54 +0000718 private:
719 T1* const ptr_;
720 const T2 value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000721
722 GTEST_DISALLOW_ASSIGN_(AssignAction);
shiqiane35fdd92008-12-10 05:08:54 +0000723};
724
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000725#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000726
shiqiane35fdd92008-12-10 05:08:54 +0000727// Implements the SetErrnoAndReturn action to simulate return from
728// various system calls and libc functions.
729template <typename T>
730class SetErrnoAndReturnAction {
731 public:
732 SetErrnoAndReturnAction(int errno_value, T result)
733 : errno_(errno_value),
734 result_(result) {}
735 template <typename Result, typename ArgumentTuple>
zhanyong.wan3fbd2dd2009-03-26 19:06:45 +0000736 Result Perform(const ArgumentTuple& /* args */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000737 errno = errno_;
738 return result_;
739 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000740
shiqiane35fdd92008-12-10 05:08:54 +0000741 private:
742 const int errno_;
743 const T result_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000744
745 GTEST_DISALLOW_ASSIGN_(SetErrnoAndReturnAction);
shiqiane35fdd92008-12-10 05:08:54 +0000746};
747
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000748#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000749
shiqiane35fdd92008-12-10 05:08:54 +0000750// Implements the SetArgumentPointee<N>(x) action for any function
751// whose N-th argument (0-based) is a pointer to x's type. The
752// template parameter kIsProto is true iff type A is ProtocolMessage,
753// proto2::Message, or a sub-class of those.
754template <size_t N, typename A, bool kIsProto>
755class SetArgumentPointeeAction {
756 public:
757 // Constructs an action that sets the variable pointed to by the
758 // N-th function argument to 'value'.
759 explicit SetArgumentPointeeAction(const A& value) : value_(value) {}
760
761 template <typename Result, typename ArgumentTuple>
762 void Perform(const ArgumentTuple& args) const {
763 CompileAssertTypesEqual<void, Result>();
kosakbd018832014-04-02 20:30:00 +0000764 *::testing::get<N>(args) = value_;
shiqiane35fdd92008-12-10 05:08:54 +0000765 }
766
767 private:
768 const A value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000769
770 GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
shiqiane35fdd92008-12-10 05:08:54 +0000771};
772
773template <size_t N, typename Proto>
774class SetArgumentPointeeAction<N, Proto, true> {
775 public:
776 // Constructs an action that sets the variable pointed to by the
777 // N-th function argument to 'proto'. Both ProtocolMessage and
778 // proto2::Message have the CopyFrom() method, so the same
779 // implementation works for both.
780 explicit SetArgumentPointeeAction(const Proto& proto) : proto_(new Proto) {
781 proto_->CopyFrom(proto);
782 }
783
784 template <typename Result, typename ArgumentTuple>
785 void Perform(const ArgumentTuple& args) const {
786 CompileAssertTypesEqual<void, Result>();
kosakbd018832014-04-02 20:30:00 +0000787 ::testing::get<N>(args)->CopyFrom(*proto_);
shiqiane35fdd92008-12-10 05:08:54 +0000788 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000789
shiqiane35fdd92008-12-10 05:08:54 +0000790 private:
791 const internal::linked_ptr<Proto> proto_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000792
793 GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
shiqiane35fdd92008-12-10 05:08:54 +0000794};
795
shiqiane35fdd92008-12-10 05:08:54 +0000796// Implements the InvokeWithoutArgs(f) action. The template argument
797// FunctionImpl is the implementation type of f, which can be either a
798// function pointer or a functor. InvokeWithoutArgs(f) can be used as an
799// Action<F> as long as f's type is compatible with F (i.e. f can be
800// assigned to a tr1::function<F>).
801template <typename FunctionImpl>
802class InvokeWithoutArgsAction {
803 public:
804 // The c'tor makes a copy of function_impl (either a function
805 // pointer or a functor).
806 explicit InvokeWithoutArgsAction(FunctionImpl function_impl)
807 : function_impl_(function_impl) {}
808
809 // Allows InvokeWithoutArgs(f) to be used as any action whose type is
810 // compatible with f.
811 template <typename Result, typename ArgumentTuple>
812 Result Perform(const ArgumentTuple&) { return function_impl_(); }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000813
shiqiane35fdd92008-12-10 05:08:54 +0000814 private:
815 FunctionImpl function_impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000816
817 GTEST_DISALLOW_ASSIGN_(InvokeWithoutArgsAction);
shiqiane35fdd92008-12-10 05:08:54 +0000818};
819
820// Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action.
821template <class Class, typename MethodPtr>
822class InvokeMethodWithoutArgsAction {
823 public:
824 InvokeMethodWithoutArgsAction(Class* obj_ptr, MethodPtr method_ptr)
825 : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
826
827 template <typename Result, typename ArgumentTuple>
828 Result Perform(const ArgumentTuple&) const {
829 return (obj_ptr_->*method_ptr_)();
830 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000831
shiqiane35fdd92008-12-10 05:08:54 +0000832 private:
833 Class* const obj_ptr_;
834 const MethodPtr method_ptr_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000835
836 GTEST_DISALLOW_ASSIGN_(InvokeMethodWithoutArgsAction);
shiqiane35fdd92008-12-10 05:08:54 +0000837};
838
839// Implements the IgnoreResult(action) action.
840template <typename A>
841class IgnoreResultAction {
842 public:
843 explicit IgnoreResultAction(const A& action) : action_(action) {}
844
845 template <typename F>
846 operator Action<F>() const {
847 // Assert statement belongs here because this is the best place to verify
848 // conditions on F. It produces the clearest error messages
849 // in most compilers.
850 // Impl really belongs in this scope as a local class but can't
851 // because MSVC produces duplicate symbols in different translation units
852 // in this case. Until MS fixes that bug we put Impl into the class scope
853 // and put the typedef both here (for use in assert statement) and
854 // in the Impl class. But both definitions must be the same.
855 typedef typename internal::Function<F>::Result Result;
856
857 // Asserts at compile time that F returns void.
858 CompileAssertTypesEqual<void, Result>();
859
860 return Action<F>(new Impl<F>(action_));
861 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000862
shiqiane35fdd92008-12-10 05:08:54 +0000863 private:
864 template <typename F>
865 class Impl : public ActionInterface<F> {
866 public:
867 typedef typename internal::Function<F>::Result Result;
868 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
869
870 explicit Impl(const A& action) : action_(action) {}
871
872 virtual void Perform(const ArgumentTuple& args) {
873 // Performs the action and ignores its result.
874 action_.Perform(args);
875 }
876
877 private:
878 // Type OriginalFunction is the same as F except that its return
879 // type is IgnoredValue.
880 typedef typename internal::Function<F>::MakeResultIgnoredValue
881 OriginalFunction;
882
883 const Action<OriginalFunction> action_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000884
885 GTEST_DISALLOW_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000886 };
887
888 const A action_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000889
890 GTEST_DISALLOW_ASSIGN_(IgnoreResultAction);
shiqiane35fdd92008-12-10 05:08:54 +0000891};
892
zhanyong.wana18423e2009-07-22 23:58:19 +0000893// A ReferenceWrapper<T> object represents a reference to type T,
894// which can be either const or not. It can be explicitly converted
895// from, and implicitly converted to, a T&. Unlike a reference,
896// ReferenceWrapper<T> can be copied and can survive template type
897// inference. This is used to support by-reference arguments in the
898// InvokeArgument<N>(...) action. The idea was from "reference
899// wrappers" in tr1, which we don't have in our source tree yet.
900template <typename T>
901class ReferenceWrapper {
902 public:
903 // Constructs a ReferenceWrapper<T> object from a T&.
904 explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {} // NOLINT
905
906 // Allows a ReferenceWrapper<T> object to be implicitly converted to
907 // a T&.
908 operator T&() const { return *pointer_; }
909 private:
910 T* pointer_;
911};
912
913// Allows the expression ByRef(x) to be printed as a reference to x.
914template <typename T>
915void PrintTo(const ReferenceWrapper<T>& ref, ::std::ostream* os) {
916 T& value = ref;
917 UniversalPrinter<T&>::Print(value, os);
918}
919
920// Does two actions sequentially. Used for implementing the DoAll(a1,
921// a2, ...) action.
922template <typename Action1, typename Action2>
923class DoBothAction {
924 public:
925 DoBothAction(Action1 action1, Action2 action2)
926 : action1_(action1), action2_(action2) {}
927
928 // This template type conversion operator allows DoAll(a1, ..., a_n)
929 // to be used in ANY function of compatible type.
930 template <typename F>
931 operator Action<F>() const {
932 return Action<F>(new Impl<F>(action1_, action2_));
933 }
934
935 private:
936 // Implements the DoAll(...) action for a particular function type F.
937 template <typename F>
938 class Impl : public ActionInterface<F> {
939 public:
940 typedef typename Function<F>::Result Result;
941 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
942 typedef typename Function<F>::MakeResultVoid VoidResult;
943
944 Impl(const Action<VoidResult>& action1, const Action<F>& action2)
945 : action1_(action1), action2_(action2) {}
946
947 virtual Result Perform(const ArgumentTuple& args) {
948 action1_.Perform(args);
949 return action2_.Perform(args);
950 }
951
952 private:
953 const Action<VoidResult> action1_;
954 const Action<F> action2_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000955
956 GTEST_DISALLOW_ASSIGN_(Impl);
zhanyong.wana18423e2009-07-22 23:58:19 +0000957 };
958
959 Action1 action1_;
960 Action2 action2_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000961
962 GTEST_DISALLOW_ASSIGN_(DoBothAction);
zhanyong.wana18423e2009-07-22 23:58:19 +0000963};
964
shiqiane35fdd92008-12-10 05:08:54 +0000965} // namespace internal
966
967// An Unused object can be implicitly constructed from ANY value.
968// This is handy when defining actions that ignore some or all of the
969// mock function arguments. For example, given
970//
971// MOCK_METHOD3(Foo, double(const string& label, double x, double y));
972// MOCK_METHOD3(Bar, double(int index, double x, double y));
973//
974// instead of
975//
976// double DistanceToOriginWithLabel(const string& label, double x, double y) {
977// return sqrt(x*x + y*y);
978// }
979// double DistanceToOriginWithIndex(int index, double x, double y) {
980// return sqrt(x*x + y*y);
981// }
982// ...
983// EXEPCT_CALL(mock, Foo("abc", _, _))
984// .WillOnce(Invoke(DistanceToOriginWithLabel));
985// EXEPCT_CALL(mock, Bar(5, _, _))
986// .WillOnce(Invoke(DistanceToOriginWithIndex));
987//
988// you could write
989//
990// // We can declare any uninteresting argument as Unused.
991// double DistanceToOrigin(Unused, double x, double y) {
992// return sqrt(x*x + y*y);
993// }
994// ...
995// EXEPCT_CALL(mock, Foo("abc", _, _)).WillOnce(Invoke(DistanceToOrigin));
996// EXEPCT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin));
997typedef internal::IgnoredValue Unused;
998
999// This constructor allows us to turn an Action<From> object into an
1000// Action<To>, as long as To's arguments can be implicitly converted
1001// to From's and From's return type cann be implicitly converted to
1002// To's.
1003template <typename To>
1004template <typename From>
1005Action<To>::Action(const Action<From>& from)
1006 : impl_(new internal::ActionAdaptor<To, From>(from)) {}
1007
1008// Creates an action that returns 'value'. 'value' is passed by value
1009// instead of const reference - otherwise Return("string literal")
1010// will trigger a compiler error about using array as initializer.
1011template <typename R>
1012internal::ReturnAction<R> Return(R value) {
kosak3d1c78b2014-11-17 00:56:52 +00001013 return internal::ReturnAction<R>(internal::move(value));
shiqiane35fdd92008-12-10 05:08:54 +00001014}
1015
1016// Creates an action that returns NULL.
1017inline PolymorphicAction<internal::ReturnNullAction> ReturnNull() {
1018 return MakePolymorphicAction(internal::ReturnNullAction());
1019}
1020
1021// Creates an action that returns from a void function.
1022inline PolymorphicAction<internal::ReturnVoidAction> Return() {
1023 return MakePolymorphicAction(internal::ReturnVoidAction());
1024}
1025
1026// Creates an action that returns the reference to a variable.
1027template <typename R>
1028inline internal::ReturnRefAction<R> ReturnRef(R& x) { // NOLINT
1029 return internal::ReturnRefAction<R>(x);
1030}
1031
zhanyong.wane3bd0982010-07-03 00:16:42 +00001032// Creates an action that returns the reference to a copy of the
1033// argument. The copy is created when the action is constructed and
1034// lives as long as the action.
1035template <typename R>
1036inline internal::ReturnRefOfCopyAction<R> ReturnRefOfCopy(const R& x) {
1037 return internal::ReturnRefOfCopyAction<R>(x);
1038}
1039
kosak3d1c78b2014-11-17 00:56:52 +00001040// Modifies the parent action (a Return() action) to perform a move of the
1041// argument instead of a copy.
1042// Return(ByMove()) actions can only be executed once and will assert this
1043// invariant.
1044template <typename R>
1045internal::ByMoveWrapper<R> ByMove(R x) {
1046 return internal::ByMoveWrapper<R>(internal::move(x));
1047}
1048
shiqiane35fdd92008-12-10 05:08:54 +00001049// Creates an action that does the default action for the give mock function.
1050inline internal::DoDefaultAction DoDefault() {
1051 return internal::DoDefaultAction();
1052}
1053
1054// Creates an action that sets the variable pointed by the N-th
1055// (0-based) function argument to 'value'.
1056template <size_t N, typename T>
1057PolymorphicAction<
1058 internal::SetArgumentPointeeAction<
1059 N, T, internal::IsAProtocolMessage<T>::value> >
zhanyong.wan59214832010-10-05 05:58:51 +00001060SetArgPointee(const T& x) {
1061 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1062 N, T, internal::IsAProtocolMessage<T>::value>(x));
1063}
zhanyong.wanfc8c6c42011-03-09 01:18:08 +00001064
1065#if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
zhanyong.wana684b5a2010-12-02 23:30:50 +00001066// This overload allows SetArgPointee() to accept a string literal.
zhanyong.wanfc8c6c42011-03-09 01:18:08 +00001067// GCC prior to the version 4.0 and Symbian C++ compiler cannot distinguish
1068// this overload from the templated version and emit a compile error.
zhanyong.wana684b5a2010-12-02 23:30:50 +00001069template <size_t N>
1070PolymorphicAction<
1071 internal::SetArgumentPointeeAction<N, const char*, false> >
1072SetArgPointee(const char* p) {
1073 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1074 N, const char*, false>(p));
1075}
zhanyong.wanfc8c6c42011-03-09 01:18:08 +00001076
1077template <size_t N>
1078PolymorphicAction<
1079 internal::SetArgumentPointeeAction<N, const wchar_t*, false> >
1080SetArgPointee(const wchar_t* p) {
1081 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1082 N, const wchar_t*, false>(p));
1083}
1084#endif
1085
zhanyong.wan59214832010-10-05 05:58:51 +00001086// The following version is DEPRECATED.
1087template <size_t N, typename T>
1088PolymorphicAction<
1089 internal::SetArgumentPointeeAction<
1090 N, T, internal::IsAProtocolMessage<T>::value> >
shiqiane35fdd92008-12-10 05:08:54 +00001091SetArgumentPointee(const T& x) {
1092 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1093 N, T, internal::IsAProtocolMessage<T>::value>(x));
1094}
1095
shiqiane35fdd92008-12-10 05:08:54 +00001096// Creates an action that sets a pointer referent to a given value.
1097template <typename T1, typename T2>
1098PolymorphicAction<internal::AssignAction<T1, T2> > Assign(T1* ptr, T2 val) {
1099 return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val));
1100}
1101
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001102#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001103
shiqiane35fdd92008-12-10 05:08:54 +00001104// Creates an action that sets errno and returns the appropriate error.
1105template <typename T>
1106PolymorphicAction<internal::SetErrnoAndReturnAction<T> >
1107SetErrnoAndReturn(int errval, T result) {
1108 return MakePolymorphicAction(
1109 internal::SetErrnoAndReturnAction<T>(errval, result));
1110}
1111
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001112#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001113
shiqiane35fdd92008-12-10 05:08:54 +00001114// Various overloads for InvokeWithoutArgs().
1115
1116// Creates an action that invokes 'function_impl' with no argument.
1117template <typename FunctionImpl>
1118PolymorphicAction<internal::InvokeWithoutArgsAction<FunctionImpl> >
1119InvokeWithoutArgs(FunctionImpl function_impl) {
1120 return MakePolymorphicAction(
1121 internal::InvokeWithoutArgsAction<FunctionImpl>(function_impl));
1122}
1123
1124// Creates an action that invokes the given method on the given object
1125// with no argument.
1126template <class Class, typename MethodPtr>
1127PolymorphicAction<internal::InvokeMethodWithoutArgsAction<Class, MethodPtr> >
1128InvokeWithoutArgs(Class* obj_ptr, MethodPtr method_ptr) {
1129 return MakePolymorphicAction(
1130 internal::InvokeMethodWithoutArgsAction<Class, MethodPtr>(
1131 obj_ptr, method_ptr));
1132}
1133
1134// Creates an action that performs an_action and throws away its
1135// result. In other words, it changes the return type of an_action to
1136// void. an_action MUST NOT return void, or the code won't compile.
1137template <typename A>
1138inline internal::IgnoreResultAction<A> IgnoreResult(const A& an_action) {
1139 return internal::IgnoreResultAction<A>(an_action);
1140}
1141
zhanyong.wana18423e2009-07-22 23:58:19 +00001142// Creates a reference wrapper for the given L-value. If necessary,
1143// you can explicitly specify the type of the reference. For example,
1144// suppose 'derived' is an object of type Derived, ByRef(derived)
1145// would wrap a Derived&. If you want to wrap a const Base& instead,
1146// where Base is a base class of Derived, just write:
1147//
1148// ByRef<const Base>(derived)
1149template <typename T>
1150inline internal::ReferenceWrapper<T> ByRef(T& l_value) { // NOLINT
1151 return internal::ReferenceWrapper<T>(l_value);
1152}
1153
shiqiane35fdd92008-12-10 05:08:54 +00001154} // namespace testing
1155
1156#endif // GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_