blob: d08152a8fd588b05544c0a3e8bfa764e76cb62a6 [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
462// Implements the polymorphic Return(x) action, which can be used in
463// any function that returns the type of x, regardless of the argument
464// types.
vladloseva070cbd2009-11-18 00:09:28 +0000465//
466// Note: The value passed into Return must be converted into
467// Function<F>::Result when this action is cast to Action<F> rather than
468// when that action is performed. This is important in scenarios like
469//
470// MOCK_METHOD1(Method, T(U));
471// ...
472// {
473// Foo foo;
474// X x(&foo);
475// EXPECT_CALL(mock, Method(_)).WillOnce(Return(x));
476// }
477//
478// In the example above the variable x holds reference to foo which leaves
479// scope and gets destroyed. If copying X just copies a reference to foo,
480// that copy will be left with a hanging reference. If conversion to T
481// makes a copy of foo, the above code is safe. To support that scenario, we
482// need to make sure that the type conversion happens inside the EXPECT_CALL
483// statement, and conversion of the result of Return to Action<T(U)> is a
484// good place for that.
485//
shiqiane35fdd92008-12-10 05:08:54 +0000486template <typename R>
487class ReturnAction {
488 public:
489 // Constructs a ReturnAction object from the value to be returned.
490 // 'value' is passed by value instead of by const reference in order
491 // to allow Return("string literal") to compile.
492 explicit ReturnAction(R value) : value_(value) {}
493
494 // This template type conversion operator allows Return(x) to be
495 // used in ANY function that returns x's type.
496 template <typename F>
497 operator Action<F>() const {
498 // Assert statement belongs here because this is the best place to verify
499 // conditions on F. It produces the clearest error messages
500 // in most compilers.
501 // Impl really belongs in this scope as a local class but can't
502 // because MSVC produces duplicate symbols in different translation units
503 // in this case. Until MS fixes that bug we put Impl into the class scope
504 // and put the typedef both here (for use in assert statement) and
505 // in the Impl class. But both definitions must be the same.
506 typedef typename Function<F>::Result Result;
zhanyong.wan02f71062010-05-10 17:14:29 +0000507 GTEST_COMPILE_ASSERT_(
zhanyong.wane0d051e2009-02-19 00:33:37 +0000508 !internal::is_reference<Result>::value,
509 use_ReturnRef_instead_of_Return_to_return_a_reference);
shiqiane35fdd92008-12-10 05:08:54 +0000510 return Action<F>(new Impl<F>(value_));
511 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000512
shiqiane35fdd92008-12-10 05:08:54 +0000513 private:
514 // Implements the Return(x) action for a particular function type F.
515 template <typename F>
516 class Impl : public ActionInterface<F> {
517 public:
518 typedef typename Function<F>::Result Result;
519 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
520
vladloseva070cbd2009-11-18 00:09:28 +0000521 // The implicit cast is necessary when Result has more than one
522 // single-argument constructor (e.g. Result is std::vector<int>) and R
523 // has a type conversion operator template. In that case, value_(value)
524 // won't compile as the compiler doesn't known which constructor of
zhanyong.wan5b61ce32011-02-01 00:00:03 +0000525 // Result to call. ImplicitCast_ forces the compiler to convert R to
vladloseva070cbd2009-11-18 00:09:28 +0000526 // Result without considering explicit constructors, thus resolving the
527 // ambiguity. value_ is then initialized using its copy constructor.
528 explicit Impl(R value)
zhanyong.wan5b61ce32011-02-01 00:00:03 +0000529 : value_(::testing::internal::ImplicitCast_<Result>(value)) {}
shiqiane35fdd92008-12-10 05:08:54 +0000530
531 virtual Result Perform(const ArgumentTuple&) { return value_; }
532
533 private:
zhanyong.wan02f71062010-05-10 17:14:29 +0000534 GTEST_COMPILE_ASSERT_(!internal::is_reference<Result>::value,
vladloseva070cbd2009-11-18 00:09:28 +0000535 Result_cannot_be_a_reference_type);
536 Result value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000537
538 GTEST_DISALLOW_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000539 };
540
541 R value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000542
543 GTEST_DISALLOW_ASSIGN_(ReturnAction);
shiqiane35fdd92008-12-10 05:08:54 +0000544};
545
546// Implements the ReturnNull() action.
547class ReturnNullAction {
548 public:
549 // Allows ReturnNull() to be used in any pointer-returning function.
550 template <typename Result, typename ArgumentTuple>
551 static Result Perform(const ArgumentTuple&) {
zhanyong.wan02f71062010-05-10 17:14:29 +0000552 GTEST_COMPILE_ASSERT_(internal::is_pointer<Result>::value,
zhanyong.wane0d051e2009-02-19 00:33:37 +0000553 ReturnNull_can_be_used_to_return_a_pointer_only);
shiqiane35fdd92008-12-10 05:08:54 +0000554 return NULL;
555 }
556};
557
558// Implements the Return() action.
559class ReturnVoidAction {
560 public:
561 // Allows Return() to be used in any void-returning function.
562 template <typename Result, typename ArgumentTuple>
563 static void Perform(const ArgumentTuple&) {
564 CompileAssertTypesEqual<void, Result>();
565 }
566};
567
568// Implements the polymorphic ReturnRef(x) action, which can be used
569// in any function that returns a reference to the type of x,
570// regardless of the argument types.
571template <typename T>
572class ReturnRefAction {
573 public:
574 // Constructs a ReturnRefAction object from the reference to be returned.
575 explicit ReturnRefAction(T& ref) : ref_(ref) {} // NOLINT
576
577 // This template type conversion operator allows ReturnRef(x) to be
578 // used in ANY function that returns a reference to x's type.
579 template <typename F>
580 operator Action<F>() const {
581 typedef typename Function<F>::Result Result;
582 // Asserts that the function return type is a reference. This
583 // catches the user error of using ReturnRef(x) when Return(x)
584 // should be used, and generates some helpful error message.
zhanyong.wan02f71062010-05-10 17:14:29 +0000585 GTEST_COMPILE_ASSERT_(internal::is_reference<Result>::value,
zhanyong.wane0d051e2009-02-19 00:33:37 +0000586 use_Return_instead_of_ReturnRef_to_return_a_value);
shiqiane35fdd92008-12-10 05:08:54 +0000587 return Action<F>(new Impl<F>(ref_));
588 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000589
shiqiane35fdd92008-12-10 05:08:54 +0000590 private:
591 // Implements the ReturnRef(x) action for a particular function type F.
592 template <typename F>
593 class Impl : public ActionInterface<F> {
594 public:
595 typedef typename Function<F>::Result Result;
596 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
597
598 explicit Impl(T& ref) : ref_(ref) {} // NOLINT
599
600 virtual Result Perform(const ArgumentTuple&) {
601 return ref_;
602 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000603
shiqiane35fdd92008-12-10 05:08:54 +0000604 private:
605 T& ref_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000606
607 GTEST_DISALLOW_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000608 };
609
610 T& ref_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000611
612 GTEST_DISALLOW_ASSIGN_(ReturnRefAction);
shiqiane35fdd92008-12-10 05:08:54 +0000613};
614
zhanyong.wane3bd0982010-07-03 00:16:42 +0000615// Implements the polymorphic ReturnRefOfCopy(x) action, which can be
616// used in any function that returns a reference to the type of x,
617// regardless of the argument types.
618template <typename T>
619class ReturnRefOfCopyAction {
620 public:
621 // Constructs a ReturnRefOfCopyAction object from the reference to
622 // be returned.
623 explicit ReturnRefOfCopyAction(const T& value) : value_(value) {} // NOLINT
624
625 // This template type conversion operator allows ReturnRefOfCopy(x) to be
626 // used in ANY function that returns a reference to x's type.
627 template <typename F>
628 operator Action<F>() const {
629 typedef typename Function<F>::Result Result;
630 // Asserts that the function return type is a reference. This
631 // catches the user error of using ReturnRefOfCopy(x) when Return(x)
632 // should be used, and generates some helpful error message.
633 GTEST_COMPILE_ASSERT_(
634 internal::is_reference<Result>::value,
635 use_Return_instead_of_ReturnRefOfCopy_to_return_a_value);
636 return Action<F>(new Impl<F>(value_));
637 }
638
639 private:
640 // Implements the ReturnRefOfCopy(x) action for a particular function type F.
641 template <typename F>
642 class Impl : public ActionInterface<F> {
643 public:
644 typedef typename Function<F>::Result Result;
645 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
646
647 explicit Impl(const T& value) : value_(value) {} // NOLINT
648
649 virtual Result Perform(const ArgumentTuple&) {
650 return value_;
651 }
652
653 private:
654 T value_;
655
656 GTEST_DISALLOW_ASSIGN_(Impl);
657 };
658
659 const T value_;
660
661 GTEST_DISALLOW_ASSIGN_(ReturnRefOfCopyAction);
662};
663
shiqiane35fdd92008-12-10 05:08:54 +0000664// Implements the polymorphic DoDefault() action.
665class DoDefaultAction {
666 public:
667 // This template type conversion operator allows DoDefault() to be
668 // used in any function.
669 template <typename F>
zhanyong.waned6c9272011-02-23 19:39:27 +0000670 operator Action<F>() const { return Action<F>(NULL); }
shiqiane35fdd92008-12-10 05:08:54 +0000671};
672
673// Implements the Assign action to set a given pointer referent to a
674// particular value.
675template <typename T1, typename T2>
676class AssignAction {
677 public:
678 AssignAction(T1* ptr, T2 value) : ptr_(ptr), value_(value) {}
679
680 template <typename Result, typename ArgumentTuple>
zhanyong.wan3fbd2dd2009-03-26 19:06:45 +0000681 void Perform(const ArgumentTuple& /* args */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000682 *ptr_ = value_;
683 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000684
shiqiane35fdd92008-12-10 05:08:54 +0000685 private:
686 T1* const ptr_;
687 const T2 value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000688
689 GTEST_DISALLOW_ASSIGN_(AssignAction);
shiqiane35fdd92008-12-10 05:08:54 +0000690};
691
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000692#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000693
shiqiane35fdd92008-12-10 05:08:54 +0000694// Implements the SetErrnoAndReturn action to simulate return from
695// various system calls and libc functions.
696template <typename T>
697class SetErrnoAndReturnAction {
698 public:
699 SetErrnoAndReturnAction(int errno_value, T result)
700 : errno_(errno_value),
701 result_(result) {}
702 template <typename Result, typename ArgumentTuple>
zhanyong.wan3fbd2dd2009-03-26 19:06:45 +0000703 Result Perform(const ArgumentTuple& /* args */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000704 errno = errno_;
705 return result_;
706 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000707
shiqiane35fdd92008-12-10 05:08:54 +0000708 private:
709 const int errno_;
710 const T result_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000711
712 GTEST_DISALLOW_ASSIGN_(SetErrnoAndReturnAction);
shiqiane35fdd92008-12-10 05:08:54 +0000713};
714
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000715#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000716
shiqiane35fdd92008-12-10 05:08:54 +0000717// Implements the SetArgumentPointee<N>(x) action for any function
718// whose N-th argument (0-based) is a pointer to x's type. The
719// template parameter kIsProto is true iff type A is ProtocolMessage,
720// proto2::Message, or a sub-class of those.
721template <size_t N, typename A, bool kIsProto>
722class SetArgumentPointeeAction {
723 public:
724 // Constructs an action that sets the variable pointed to by the
725 // N-th function argument to 'value'.
726 explicit SetArgumentPointeeAction(const A& value) : value_(value) {}
727
728 template <typename Result, typename ArgumentTuple>
729 void Perform(const ArgumentTuple& args) const {
730 CompileAssertTypesEqual<void, Result>();
731 *::std::tr1::get<N>(args) = value_;
732 }
733
734 private:
735 const A value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000736
737 GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
shiqiane35fdd92008-12-10 05:08:54 +0000738};
739
740template <size_t N, typename Proto>
741class SetArgumentPointeeAction<N, Proto, true> {
742 public:
743 // Constructs an action that sets the variable pointed to by the
744 // N-th function argument to 'proto'. Both ProtocolMessage and
745 // proto2::Message have the CopyFrom() method, so the same
746 // implementation works for both.
747 explicit SetArgumentPointeeAction(const Proto& proto) : proto_(new Proto) {
748 proto_->CopyFrom(proto);
749 }
750
751 template <typename Result, typename ArgumentTuple>
752 void Perform(const ArgumentTuple& args) const {
753 CompileAssertTypesEqual<void, Result>();
754 ::std::tr1::get<N>(args)->CopyFrom(*proto_);
755 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000756
shiqiane35fdd92008-12-10 05:08:54 +0000757 private:
758 const internal::linked_ptr<Proto> proto_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000759
760 GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
shiqiane35fdd92008-12-10 05:08:54 +0000761};
762
shiqiane35fdd92008-12-10 05:08:54 +0000763// Implements the InvokeWithoutArgs(f) action. The template argument
764// FunctionImpl is the implementation type of f, which can be either a
765// function pointer or a functor. InvokeWithoutArgs(f) can be used as an
766// Action<F> as long as f's type is compatible with F (i.e. f can be
767// assigned to a tr1::function<F>).
768template <typename FunctionImpl>
769class InvokeWithoutArgsAction {
770 public:
771 // The c'tor makes a copy of function_impl (either a function
772 // pointer or a functor).
773 explicit InvokeWithoutArgsAction(FunctionImpl function_impl)
774 : function_impl_(function_impl) {}
775
776 // Allows InvokeWithoutArgs(f) to be used as any action whose type is
777 // compatible with f.
778 template <typename Result, typename ArgumentTuple>
779 Result Perform(const ArgumentTuple&) { return function_impl_(); }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000780
shiqiane35fdd92008-12-10 05:08:54 +0000781 private:
782 FunctionImpl function_impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000783
784 GTEST_DISALLOW_ASSIGN_(InvokeWithoutArgsAction);
shiqiane35fdd92008-12-10 05:08:54 +0000785};
786
787// Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action.
788template <class Class, typename MethodPtr>
789class InvokeMethodWithoutArgsAction {
790 public:
791 InvokeMethodWithoutArgsAction(Class* obj_ptr, MethodPtr method_ptr)
792 : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
793
794 template <typename Result, typename ArgumentTuple>
795 Result Perform(const ArgumentTuple&) const {
796 return (obj_ptr_->*method_ptr_)();
797 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000798
shiqiane35fdd92008-12-10 05:08:54 +0000799 private:
800 Class* const obj_ptr_;
801 const MethodPtr method_ptr_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000802
803 GTEST_DISALLOW_ASSIGN_(InvokeMethodWithoutArgsAction);
shiqiane35fdd92008-12-10 05:08:54 +0000804};
805
806// Implements the IgnoreResult(action) action.
807template <typename A>
808class IgnoreResultAction {
809 public:
810 explicit IgnoreResultAction(const A& action) : action_(action) {}
811
812 template <typename F>
813 operator Action<F>() const {
814 // Assert statement belongs here because this is the best place to verify
815 // conditions on F. It produces the clearest error messages
816 // in most compilers.
817 // Impl really belongs in this scope as a local class but can't
818 // because MSVC produces duplicate symbols in different translation units
819 // in this case. Until MS fixes that bug we put Impl into the class scope
820 // and put the typedef both here (for use in assert statement) and
821 // in the Impl class. But both definitions must be the same.
822 typedef typename internal::Function<F>::Result Result;
823
824 // Asserts at compile time that F returns void.
825 CompileAssertTypesEqual<void, Result>();
826
827 return Action<F>(new Impl<F>(action_));
828 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000829
shiqiane35fdd92008-12-10 05:08:54 +0000830 private:
831 template <typename F>
832 class Impl : public ActionInterface<F> {
833 public:
834 typedef typename internal::Function<F>::Result Result;
835 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
836
837 explicit Impl(const A& action) : action_(action) {}
838
839 virtual void Perform(const ArgumentTuple& args) {
840 // Performs the action and ignores its result.
841 action_.Perform(args);
842 }
843
844 private:
845 // Type OriginalFunction is the same as F except that its return
846 // type is IgnoredValue.
847 typedef typename internal::Function<F>::MakeResultIgnoredValue
848 OriginalFunction;
849
850 const Action<OriginalFunction> action_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000851
852 GTEST_DISALLOW_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000853 };
854
855 const A action_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000856
857 GTEST_DISALLOW_ASSIGN_(IgnoreResultAction);
shiqiane35fdd92008-12-10 05:08:54 +0000858};
859
zhanyong.wana18423e2009-07-22 23:58:19 +0000860// A ReferenceWrapper<T> object represents a reference to type T,
861// which can be either const or not. It can be explicitly converted
862// from, and implicitly converted to, a T&. Unlike a reference,
863// ReferenceWrapper<T> can be copied and can survive template type
864// inference. This is used to support by-reference arguments in the
865// InvokeArgument<N>(...) action. The idea was from "reference
866// wrappers" in tr1, which we don't have in our source tree yet.
867template <typename T>
868class ReferenceWrapper {
869 public:
870 // Constructs a ReferenceWrapper<T> object from a T&.
871 explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {} // NOLINT
872
873 // Allows a ReferenceWrapper<T> object to be implicitly converted to
874 // a T&.
875 operator T&() const { return *pointer_; }
876 private:
877 T* pointer_;
878};
879
880// Allows the expression ByRef(x) to be printed as a reference to x.
881template <typename T>
882void PrintTo(const ReferenceWrapper<T>& ref, ::std::ostream* os) {
883 T& value = ref;
884 UniversalPrinter<T&>::Print(value, os);
885}
886
887// Does two actions sequentially. Used for implementing the DoAll(a1,
888// a2, ...) action.
889template <typename Action1, typename Action2>
890class DoBothAction {
891 public:
892 DoBothAction(Action1 action1, Action2 action2)
893 : action1_(action1), action2_(action2) {}
894
895 // This template type conversion operator allows DoAll(a1, ..., a_n)
896 // to be used in ANY function of compatible type.
897 template <typename F>
898 operator Action<F>() const {
899 return Action<F>(new Impl<F>(action1_, action2_));
900 }
901
902 private:
903 // Implements the DoAll(...) action for a particular function type F.
904 template <typename F>
905 class Impl : public ActionInterface<F> {
906 public:
907 typedef typename Function<F>::Result Result;
908 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
909 typedef typename Function<F>::MakeResultVoid VoidResult;
910
911 Impl(const Action<VoidResult>& action1, const Action<F>& action2)
912 : action1_(action1), action2_(action2) {}
913
914 virtual Result Perform(const ArgumentTuple& args) {
915 action1_.Perform(args);
916 return action2_.Perform(args);
917 }
918
919 private:
920 const Action<VoidResult> action1_;
921 const Action<F> action2_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000922
923 GTEST_DISALLOW_ASSIGN_(Impl);
zhanyong.wana18423e2009-07-22 23:58:19 +0000924 };
925
926 Action1 action1_;
927 Action2 action2_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000928
929 GTEST_DISALLOW_ASSIGN_(DoBothAction);
zhanyong.wana18423e2009-07-22 23:58:19 +0000930};
931
shiqiane35fdd92008-12-10 05:08:54 +0000932} // namespace internal
933
934// An Unused object can be implicitly constructed from ANY value.
935// This is handy when defining actions that ignore some or all of the
936// mock function arguments. For example, given
937//
938// MOCK_METHOD3(Foo, double(const string& label, double x, double y));
939// MOCK_METHOD3(Bar, double(int index, double x, double y));
940//
941// instead of
942//
943// double DistanceToOriginWithLabel(const string& label, double x, double y) {
944// return sqrt(x*x + y*y);
945// }
946// double DistanceToOriginWithIndex(int index, double x, double y) {
947// return sqrt(x*x + y*y);
948// }
949// ...
950// EXEPCT_CALL(mock, Foo("abc", _, _))
951// .WillOnce(Invoke(DistanceToOriginWithLabel));
952// EXEPCT_CALL(mock, Bar(5, _, _))
953// .WillOnce(Invoke(DistanceToOriginWithIndex));
954//
955// you could write
956//
957// // We can declare any uninteresting argument as Unused.
958// double DistanceToOrigin(Unused, double x, double y) {
959// return sqrt(x*x + y*y);
960// }
961// ...
962// EXEPCT_CALL(mock, Foo("abc", _, _)).WillOnce(Invoke(DistanceToOrigin));
963// EXEPCT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin));
964typedef internal::IgnoredValue Unused;
965
966// This constructor allows us to turn an Action<From> object into an
967// Action<To>, as long as To's arguments can be implicitly converted
968// to From's and From's return type cann be implicitly converted to
969// To's.
970template <typename To>
971template <typename From>
972Action<To>::Action(const Action<From>& from)
973 : impl_(new internal::ActionAdaptor<To, From>(from)) {}
974
975// Creates an action that returns 'value'. 'value' is passed by value
976// instead of const reference - otherwise Return("string literal")
977// will trigger a compiler error about using array as initializer.
978template <typename R>
979internal::ReturnAction<R> Return(R value) {
980 return internal::ReturnAction<R>(value);
981}
982
983// Creates an action that returns NULL.
984inline PolymorphicAction<internal::ReturnNullAction> ReturnNull() {
985 return MakePolymorphicAction(internal::ReturnNullAction());
986}
987
988// Creates an action that returns from a void function.
989inline PolymorphicAction<internal::ReturnVoidAction> Return() {
990 return MakePolymorphicAction(internal::ReturnVoidAction());
991}
992
993// Creates an action that returns the reference to a variable.
994template <typename R>
995inline internal::ReturnRefAction<R> ReturnRef(R& x) { // NOLINT
996 return internal::ReturnRefAction<R>(x);
997}
998
zhanyong.wane3bd0982010-07-03 00:16:42 +0000999// Creates an action that returns the reference to a copy of the
1000// argument. The copy is created when the action is constructed and
1001// lives as long as the action.
1002template <typename R>
1003inline internal::ReturnRefOfCopyAction<R> ReturnRefOfCopy(const R& x) {
1004 return internal::ReturnRefOfCopyAction<R>(x);
1005}
1006
shiqiane35fdd92008-12-10 05:08:54 +00001007// Creates an action that does the default action for the give mock function.
1008inline internal::DoDefaultAction DoDefault() {
1009 return internal::DoDefaultAction();
1010}
1011
1012// Creates an action that sets the variable pointed by the N-th
1013// (0-based) function argument to 'value'.
1014template <size_t N, typename T>
1015PolymorphicAction<
1016 internal::SetArgumentPointeeAction<
1017 N, T, internal::IsAProtocolMessage<T>::value> >
zhanyong.wan59214832010-10-05 05:58:51 +00001018SetArgPointee(const T& x) {
1019 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1020 N, T, internal::IsAProtocolMessage<T>::value>(x));
1021}
zhanyong.wanfc8c6c42011-03-09 01:18:08 +00001022
1023#if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
zhanyong.wana684b5a2010-12-02 23:30:50 +00001024// This overload allows SetArgPointee() to accept a string literal.
zhanyong.wanfc8c6c42011-03-09 01:18:08 +00001025// GCC prior to the version 4.0 and Symbian C++ compiler cannot distinguish
1026// this overload from the templated version and emit a compile error.
zhanyong.wana684b5a2010-12-02 23:30:50 +00001027template <size_t N>
1028PolymorphicAction<
1029 internal::SetArgumentPointeeAction<N, const char*, false> >
1030SetArgPointee(const char* p) {
1031 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1032 N, const char*, false>(p));
1033}
zhanyong.wanfc8c6c42011-03-09 01:18:08 +00001034
1035template <size_t N>
1036PolymorphicAction<
1037 internal::SetArgumentPointeeAction<N, const wchar_t*, false> >
1038SetArgPointee(const wchar_t* p) {
1039 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1040 N, const wchar_t*, false>(p));
1041}
1042#endif
1043
zhanyong.wan59214832010-10-05 05:58:51 +00001044// The following version is DEPRECATED.
1045template <size_t N, typename T>
1046PolymorphicAction<
1047 internal::SetArgumentPointeeAction<
1048 N, T, internal::IsAProtocolMessage<T>::value> >
shiqiane35fdd92008-12-10 05:08:54 +00001049SetArgumentPointee(const T& x) {
1050 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1051 N, T, internal::IsAProtocolMessage<T>::value>(x));
1052}
1053
shiqiane35fdd92008-12-10 05:08:54 +00001054// Creates an action that sets a pointer referent to a given value.
1055template <typename T1, typename T2>
1056PolymorphicAction<internal::AssignAction<T1, T2> > Assign(T1* ptr, T2 val) {
1057 return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val));
1058}
1059
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001060#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001061
shiqiane35fdd92008-12-10 05:08:54 +00001062// Creates an action that sets errno and returns the appropriate error.
1063template <typename T>
1064PolymorphicAction<internal::SetErrnoAndReturnAction<T> >
1065SetErrnoAndReturn(int errval, T result) {
1066 return MakePolymorphicAction(
1067 internal::SetErrnoAndReturnAction<T>(errval, result));
1068}
1069
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001070#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001071
shiqiane35fdd92008-12-10 05:08:54 +00001072// Various overloads for InvokeWithoutArgs().
1073
1074// Creates an action that invokes 'function_impl' with no argument.
1075template <typename FunctionImpl>
1076PolymorphicAction<internal::InvokeWithoutArgsAction<FunctionImpl> >
1077InvokeWithoutArgs(FunctionImpl function_impl) {
1078 return MakePolymorphicAction(
1079 internal::InvokeWithoutArgsAction<FunctionImpl>(function_impl));
1080}
1081
1082// Creates an action that invokes the given method on the given object
1083// with no argument.
1084template <class Class, typename MethodPtr>
1085PolymorphicAction<internal::InvokeMethodWithoutArgsAction<Class, MethodPtr> >
1086InvokeWithoutArgs(Class* obj_ptr, MethodPtr method_ptr) {
1087 return MakePolymorphicAction(
1088 internal::InvokeMethodWithoutArgsAction<Class, MethodPtr>(
1089 obj_ptr, method_ptr));
1090}
1091
1092// Creates an action that performs an_action and throws away its
1093// result. In other words, it changes the return type of an_action to
1094// void. an_action MUST NOT return void, or the code won't compile.
1095template <typename A>
1096inline internal::IgnoreResultAction<A> IgnoreResult(const A& an_action) {
1097 return internal::IgnoreResultAction<A>(an_action);
1098}
1099
zhanyong.wana18423e2009-07-22 23:58:19 +00001100// Creates a reference wrapper for the given L-value. If necessary,
1101// you can explicitly specify the type of the reference. For example,
1102// suppose 'derived' is an object of type Derived, ByRef(derived)
1103// would wrap a Derived&. If you want to wrap a const Base& instead,
1104// where Base is a base class of Derived, just write:
1105//
1106// ByRef<const Base>(derived)
1107template <typename T>
1108inline internal::ReferenceWrapper<T> ByRef(T& l_value) { // NOLINT
1109 return internal::ReferenceWrapper<T>(l_value);
1110}
1111
shiqiane35fdd92008-12-10 05:08:54 +00001112} // namespace testing
1113
1114#endif // GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_