blob: f88ac80d147beb4dcfd4df090d5c0401bb73d990 [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
39#include <algorithm>
40#include <string>
zhanyong.wan5b5d62f2009-03-11 23:37:56 +000041
42#ifndef _WIN32_WCE
shiqiane35fdd92008-12-10 05:08:54 +000043#include <errno.h>
zhanyong.wan5b5d62f2009-03-11 23:37:56 +000044#endif
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
62template <typename F>
63class MonomorphicDoDefaultActionImpl;
64
65template <typename F1, typename F2>
66class ActionAdaptor;
67
68// BuiltInDefaultValue<T>::Get() returns the "built-in" default
69// value for type T, which is NULL when T is a pointer type, 0 when T
70// is a numeric type, false when T is bool, or "" when T is string or
71// std::string. For any other type T, this value is undefined and the
72// function will abort the process.
73template <typename T>
74class BuiltInDefaultValue {
75 public:
zhanyong.wan5b95fa72009-01-27 22:28:45 +000076 // This function returns true iff type T has a built-in default value.
77 static bool Exists() { return false; }
shiqiane35fdd92008-12-10 05:08:54 +000078 static T Get() {
79 Assert(false, __FILE__, __LINE__,
80 "Default action undefined for the function return type.");
81 return internal::Invalid<T>();
82 // The above statement will never be reached, but is required in
83 // order for this function to compile.
84 }
85};
86
87// This partial specialization says that we use the same built-in
88// default value for T and const T.
89template <typename T>
90class BuiltInDefaultValue<const T> {
91 public:
zhanyong.wan5b95fa72009-01-27 22:28:45 +000092 static bool Exists() { return BuiltInDefaultValue<T>::Exists(); }
shiqiane35fdd92008-12-10 05:08:54 +000093 static T Get() { return BuiltInDefaultValue<T>::Get(); }
94};
95
96// This partial specialization defines the default values for pointer
97// types.
98template <typename T>
99class BuiltInDefaultValue<T*> {
100 public:
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000101 static bool Exists() { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000102 static T* Get() { return NULL; }
103};
104
105// The following specializations define the default values for
106// specific types we care about.
zhanyong.wane0d051e2009-02-19 00:33:37 +0000107#define GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(type, value) \
shiqiane35fdd92008-12-10 05:08:54 +0000108 template <> \
109 class BuiltInDefaultValue<type> { \
110 public: \
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000111 static bool Exists() { return true; } \
shiqiane35fdd92008-12-10 05:08:54 +0000112 static type Get() { return value; } \
113 }
114
zhanyong.wane0d051e2009-02-19 00:33:37 +0000115GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(void, ); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000116#if GTEST_HAS_GLOBAL_STRING
zhanyong.wane0d051e2009-02-19 00:33:37 +0000117GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::string, "");
shiqiane35fdd92008-12-10 05:08:54 +0000118#endif // GTEST_HAS_GLOBAL_STRING
zhanyong.wane0d051e2009-02-19 00:33:37 +0000119GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::std::string, "");
zhanyong.wane0d051e2009-02-19 00:33:37 +0000120GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(bool, false);
121GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned char, '\0');
122GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed char, '\0');
123GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(char, '\0');
shiqiane35fdd92008-12-10 05:08:54 +0000124
shiqiane35fdd92008-12-10 05:08:54 +0000125// There's no need for a default action for signed wchar_t, as that
126// type is the same as wchar_t for gcc, and invalid for MSVC.
127//
128// There's also no need for a default action for unsigned wchar_t, as
129// that type is the same as unsigned int for gcc, and invalid for
130// MSVC.
zhanyong.wan95b12332009-09-25 18:55:50 +0000131#if GMOCK_WCHAR_T_IS_NATIVE_
zhanyong.wane0d051e2009-02-19 00:33:37 +0000132GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(wchar_t, 0U); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000133#endif
134
zhanyong.wane0d051e2009-02-19 00:33:37 +0000135GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned short, 0U); // NOLINT
136GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed short, 0); // NOLINT
137GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned int, 0U);
138GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed int, 0);
139GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned long, 0UL); // NOLINT
140GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed long, 0L); // NOLINT
141GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(UInt64, 0);
142GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(Int64, 0);
143GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(float, 0);
144GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(double, 0);
shiqiane35fdd92008-12-10 05:08:54 +0000145
zhanyong.wane0d051e2009-02-19 00:33:37 +0000146#undef GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_
shiqiane35fdd92008-12-10 05:08:54 +0000147
148} // namespace internal
149
150// When an unexpected function call is encountered, Google Mock will
151// let it return a default value if the user has specified one for its
152// return type, or if the return type has a built-in default value;
153// otherwise Google Mock won't know what value to return and will have
154// to abort the process.
155//
156// The DefaultValue<T> class allows a user to specify the
157// default value for a type T that is both copyable and publicly
158// destructible (i.e. anything that can be used as a function return
159// type). The usage is:
160//
161// // Sets the default value for type T to be foo.
162// DefaultValue<T>::Set(foo);
163template <typename T>
164class DefaultValue {
165 public:
166 // Sets the default value for type T; requires T to be
167 // copy-constructable and have a public destructor.
168 static void Set(T x) {
169 delete value_;
170 value_ = new T(x);
171 }
172
173 // Unsets the default value for type T.
174 static void Clear() {
175 delete value_;
176 value_ = NULL;
177 }
178
179 // Returns true iff the user has set the default value for type T.
180 static bool IsSet() { return value_ != NULL; }
181
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000182 // Returns true if T has a default return value set by the user or there
183 // exists a built-in default value.
184 static bool Exists() {
185 return IsSet() || internal::BuiltInDefaultValue<T>::Exists();
186 }
187
shiqiane35fdd92008-12-10 05:08:54 +0000188 // Returns the default value for type T if the user has set one;
189 // otherwise returns the built-in default value if there is one;
190 // otherwise aborts the process.
191 static T Get() {
192 return value_ == NULL ?
193 internal::BuiltInDefaultValue<T>::Get() : *value_;
194 }
195 private:
196 static const T* value_;
197};
198
199// This partial specialization allows a user to set default values for
200// reference types.
201template <typename T>
202class DefaultValue<T&> {
203 public:
204 // Sets the default value for type T&.
205 static void Set(T& x) { // NOLINT
206 address_ = &x;
207 }
208
209 // Unsets the default value for type T&.
210 static void Clear() {
211 address_ = NULL;
212 }
213
214 // Returns true iff the user has set the default value for type T&.
215 static bool IsSet() { return address_ != NULL; }
216
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000217 // Returns true if T has a default return value set by the user or there
218 // exists a built-in default value.
219 static bool Exists() {
220 return IsSet() || internal::BuiltInDefaultValue<T&>::Exists();
221 }
222
shiqiane35fdd92008-12-10 05:08:54 +0000223 // Returns the default value for type T& if the user has set one;
224 // otherwise returns the built-in default value if there is one;
225 // otherwise aborts the process.
226 static T& Get() {
227 return address_ == NULL ?
228 internal::BuiltInDefaultValue<T&>::Get() : *address_;
229 }
230 private:
231 static T* address_;
232};
233
234// This specialization allows DefaultValue<void>::Get() to
235// compile.
236template <>
237class DefaultValue<void> {
238 public:
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000239 static bool Exists() { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000240 static void Get() {}
241};
242
243// Points to the user-set default value for type T.
244template <typename T>
245const T* DefaultValue<T>::value_ = NULL;
246
247// Points to the user-set default value for type T&.
248template <typename T>
249T* DefaultValue<T&>::address_ = NULL;
250
251// Implement this interface to define an action for function type F.
252template <typename F>
253class ActionInterface {
254 public:
255 typedef typename internal::Function<F>::Result Result;
256 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
257
258 ActionInterface() : is_do_default_(false) {}
259
260 virtual ~ActionInterface() {}
261
262 // Performs the action. This method is not const, as in general an
263 // action can have side effects and be stateful. For example, a
264 // get-the-next-element-from-the-collection action will need to
265 // remember the current element.
266 virtual Result Perform(const ArgumentTuple& args) = 0;
267
268 // Returns true iff this is the DoDefault() action.
269 bool IsDoDefault() const { return is_do_default_; }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000270
shiqiane35fdd92008-12-10 05:08:54 +0000271 private:
272 template <typename Function>
273 friend class internal::MonomorphicDoDefaultActionImpl;
274
275 // This private constructor is reserved for implementing
276 // DoDefault(), the default action for a given mock function.
277 explicit ActionInterface(bool is_do_default)
278 : is_do_default_(is_do_default) {}
279
280 // True iff this action is DoDefault().
281 const bool is_do_default_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000282
283 GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionInterface);
shiqiane35fdd92008-12-10 05:08:54 +0000284};
285
286// An Action<F> is a copyable and IMMUTABLE (except by assignment)
287// object that represents an action to be taken when a mock function
288// of type F is called. The implementation of Action<T> is just a
289// linked_ptr to const ActionInterface<T>, so copying is fairly cheap.
290// Don't inherit from Action!
291//
292// You can view an object implementing ActionInterface<F> as a
293// concrete action (including its current state), and an Action<F>
294// object as a handle to it.
295template <typename F>
296class Action {
297 public:
298 typedef typename internal::Function<F>::Result Result;
299 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
300
301 // Constructs a null Action. Needed for storing Action objects in
302 // STL containers.
303 Action() : impl_(NULL) {}
304
305 // Constructs an Action from its implementation.
306 explicit Action(ActionInterface<F>* impl) : impl_(impl) {}
307
308 // Copy constructor.
309 Action(const Action& action) : impl_(action.impl_) {}
310
311 // This constructor allows us to turn an Action<Func> object into an
312 // Action<F>, as long as F's arguments can be implicitly converted
vladloseva070cbd2009-11-18 00:09:28 +0000313 // to Func's and Func's return type can be implicitly converted to
shiqiane35fdd92008-12-10 05:08:54 +0000314 // F's.
315 template <typename Func>
316 explicit Action(const Action<Func>& action);
317
318 // Returns true iff this is the DoDefault() action.
319 bool IsDoDefault() const { return impl_->IsDoDefault(); }
320
321 // Performs the action. Note that this method is const even though
322 // the corresponding method in ActionInterface is not. The reason
323 // is that a const Action<F> means that it cannot be re-bound to
324 // another concrete action, not that the concrete action it binds to
325 // cannot change state. (Think of the difference between a const
326 // pointer and a pointer to const.)
327 Result Perform(const ArgumentTuple& args) const {
328 return impl_->Perform(args);
329 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000330
shiqiane35fdd92008-12-10 05:08:54 +0000331 private:
332 template <typename F1, typename F2>
333 friend class internal::ActionAdaptor;
334
335 internal::linked_ptr<ActionInterface<F> > impl_;
336};
337
338// The PolymorphicAction class template makes it easy to implement a
339// polymorphic action (i.e. an action that can be used in mock
340// functions of than one type, e.g. Return()).
341//
342// To define a polymorphic action, a user first provides a COPYABLE
343// implementation class that has a Perform() method template:
344//
345// class FooAction {
346// public:
347// template <typename Result, typename ArgumentTuple>
348// Result Perform(const ArgumentTuple& args) const {
349// // Processes the arguments and returns a result, using
350// // tr1::get<N>(args) to get the N-th (0-based) argument in the tuple.
351// }
352// ...
353// };
354//
355// Then the user creates the polymorphic action using
356// MakePolymorphicAction(object) where object has type FooAction. See
357// the definition of Return(void) and SetArgumentPointee<N>(value) for
358// complete examples.
359template <typename Impl>
360class PolymorphicAction {
361 public:
362 explicit PolymorphicAction(const Impl& impl) : impl_(impl) {}
363
364 template <typename F>
365 operator Action<F>() const {
366 return Action<F>(new MonomorphicImpl<F>(impl_));
367 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000368
shiqiane35fdd92008-12-10 05:08:54 +0000369 private:
370 template <typename F>
371 class MonomorphicImpl : public ActionInterface<F> {
372 public:
373 typedef typename internal::Function<F>::Result Result;
374 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
375
376 explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
377
378 virtual Result Perform(const ArgumentTuple& args) {
379 return impl_.template Perform<Result>(args);
380 }
381
382 private:
383 Impl impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000384
385 GTEST_DISALLOW_ASSIGN_(MonomorphicImpl);
shiqiane35fdd92008-12-10 05:08:54 +0000386 };
387
388 Impl impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000389
390 GTEST_DISALLOW_ASSIGN_(PolymorphicAction);
shiqiane35fdd92008-12-10 05:08:54 +0000391};
392
393// Creates an Action from its implementation and returns it. The
394// created Action object owns the implementation.
395template <typename F>
396Action<F> MakeAction(ActionInterface<F>* impl) {
397 return Action<F>(impl);
398}
399
400// Creates a polymorphic action from its implementation. This is
401// easier to use than the PolymorphicAction<Impl> constructor as it
402// doesn't require you to explicitly write the template argument, e.g.
403//
404// MakePolymorphicAction(foo);
405// vs
406// PolymorphicAction<TypeOfFoo>(foo);
407template <typename Impl>
408inline PolymorphicAction<Impl> MakePolymorphicAction(const Impl& impl) {
409 return PolymorphicAction<Impl>(impl);
410}
411
412namespace internal {
413
414// Allows an Action<F2> object to pose as an Action<F1>, as long as F2
415// and F1 are compatible.
416template <typename F1, typename F2>
417class ActionAdaptor : public ActionInterface<F1> {
418 public:
419 typedef typename internal::Function<F1>::Result Result;
420 typedef typename internal::Function<F1>::ArgumentTuple ArgumentTuple;
421
422 explicit ActionAdaptor(const Action<F2>& from) : impl_(from.impl_) {}
423
424 virtual Result Perform(const ArgumentTuple& args) {
425 return impl_->Perform(args);
426 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000427
shiqiane35fdd92008-12-10 05:08:54 +0000428 private:
429 const internal::linked_ptr<ActionInterface<F2> > impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000430
431 GTEST_DISALLOW_ASSIGN_(ActionAdaptor);
shiqiane35fdd92008-12-10 05:08:54 +0000432};
433
434// Implements the polymorphic Return(x) action, which can be used in
435// any function that returns the type of x, regardless of the argument
436// types.
vladloseva070cbd2009-11-18 00:09:28 +0000437//
438// Note: The value passed into Return must be converted into
439// Function<F>::Result when this action is cast to Action<F> rather than
440// when that action is performed. This is important in scenarios like
441//
442// MOCK_METHOD1(Method, T(U));
443// ...
444// {
445// Foo foo;
446// X x(&foo);
447// EXPECT_CALL(mock, Method(_)).WillOnce(Return(x));
448// }
449//
450// In the example above the variable x holds reference to foo which leaves
451// scope and gets destroyed. If copying X just copies a reference to foo,
452// that copy will be left with a hanging reference. If conversion to T
453// makes a copy of foo, the above code is safe. To support that scenario, we
454// need to make sure that the type conversion happens inside the EXPECT_CALL
455// statement, and conversion of the result of Return to Action<T(U)> is a
456// good place for that.
457//
shiqiane35fdd92008-12-10 05:08:54 +0000458template <typename R>
459class ReturnAction {
460 public:
461 // Constructs a ReturnAction object from the value to be returned.
462 // 'value' is passed by value instead of by const reference in order
463 // to allow Return("string literal") to compile.
464 explicit ReturnAction(R value) : value_(value) {}
465
466 // This template type conversion operator allows Return(x) to be
467 // used in ANY function that returns x's type.
468 template <typename F>
469 operator Action<F>() const {
470 // Assert statement belongs here because this is the best place to verify
471 // conditions on F. It produces the clearest error messages
472 // in most compilers.
473 // Impl really belongs in this scope as a local class but can't
474 // because MSVC produces duplicate symbols in different translation units
475 // in this case. Until MS fixes that bug we put Impl into the class scope
476 // and put the typedef both here (for use in assert statement) and
477 // in the Impl class. But both definitions must be the same.
478 typedef typename Function<F>::Result Result;
zhanyong.wan02f71062010-05-10 17:14:29 +0000479 GTEST_COMPILE_ASSERT_(
zhanyong.wane0d051e2009-02-19 00:33:37 +0000480 !internal::is_reference<Result>::value,
481 use_ReturnRef_instead_of_Return_to_return_a_reference);
shiqiane35fdd92008-12-10 05:08:54 +0000482 return Action<F>(new Impl<F>(value_));
483 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000484
shiqiane35fdd92008-12-10 05:08:54 +0000485 private:
486 // Implements the Return(x) action for a particular function type F.
487 template <typename F>
488 class Impl : public ActionInterface<F> {
489 public:
490 typedef typename Function<F>::Result Result;
491 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
492
vladloseva070cbd2009-11-18 00:09:28 +0000493 // The implicit cast is necessary when Result has more than one
494 // single-argument constructor (e.g. Result is std::vector<int>) and R
495 // has a type conversion operator template. In that case, value_(value)
496 // won't compile as the compiler doesn't known which constructor of
zhanyong.wan5b61ce32011-02-01 00:00:03 +0000497 // Result to call. ImplicitCast_ forces the compiler to convert R to
vladloseva070cbd2009-11-18 00:09:28 +0000498 // Result without considering explicit constructors, thus resolving the
499 // ambiguity. value_ is then initialized using its copy constructor.
500 explicit Impl(R value)
zhanyong.wan5b61ce32011-02-01 00:00:03 +0000501 : value_(::testing::internal::ImplicitCast_<Result>(value)) {}
shiqiane35fdd92008-12-10 05:08:54 +0000502
503 virtual Result Perform(const ArgumentTuple&) { return value_; }
504
505 private:
zhanyong.wan02f71062010-05-10 17:14:29 +0000506 GTEST_COMPILE_ASSERT_(!internal::is_reference<Result>::value,
vladloseva070cbd2009-11-18 00:09:28 +0000507 Result_cannot_be_a_reference_type);
508 Result value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000509
510 GTEST_DISALLOW_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000511 };
512
513 R value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000514
515 GTEST_DISALLOW_ASSIGN_(ReturnAction);
shiqiane35fdd92008-12-10 05:08:54 +0000516};
517
518// Implements the ReturnNull() action.
519class ReturnNullAction {
520 public:
521 // Allows ReturnNull() to be used in any pointer-returning function.
522 template <typename Result, typename ArgumentTuple>
523 static Result Perform(const ArgumentTuple&) {
zhanyong.wan02f71062010-05-10 17:14:29 +0000524 GTEST_COMPILE_ASSERT_(internal::is_pointer<Result>::value,
zhanyong.wane0d051e2009-02-19 00:33:37 +0000525 ReturnNull_can_be_used_to_return_a_pointer_only);
shiqiane35fdd92008-12-10 05:08:54 +0000526 return NULL;
527 }
528};
529
530// Implements the Return() action.
531class ReturnVoidAction {
532 public:
533 // Allows Return() to be used in any void-returning function.
534 template <typename Result, typename ArgumentTuple>
535 static void Perform(const ArgumentTuple&) {
536 CompileAssertTypesEqual<void, Result>();
537 }
538};
539
540// Implements the polymorphic ReturnRef(x) action, which can be used
541// in any function that returns a reference to the type of x,
542// regardless of the argument types.
543template <typename T>
544class ReturnRefAction {
545 public:
546 // Constructs a ReturnRefAction object from the reference to be returned.
547 explicit ReturnRefAction(T& ref) : ref_(ref) {} // NOLINT
548
549 // This template type conversion operator allows ReturnRef(x) to be
550 // used in ANY function that returns a reference to x's type.
551 template <typename F>
552 operator Action<F>() const {
553 typedef typename Function<F>::Result Result;
554 // Asserts that the function return type is a reference. This
555 // catches the user error of using ReturnRef(x) when Return(x)
556 // should be used, and generates some helpful error message.
zhanyong.wan02f71062010-05-10 17:14:29 +0000557 GTEST_COMPILE_ASSERT_(internal::is_reference<Result>::value,
zhanyong.wane0d051e2009-02-19 00:33:37 +0000558 use_Return_instead_of_ReturnRef_to_return_a_value);
shiqiane35fdd92008-12-10 05:08:54 +0000559 return Action<F>(new Impl<F>(ref_));
560 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000561
shiqiane35fdd92008-12-10 05:08:54 +0000562 private:
563 // Implements the ReturnRef(x) action for a particular function type F.
564 template <typename F>
565 class Impl : public ActionInterface<F> {
566 public:
567 typedef typename Function<F>::Result Result;
568 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
569
570 explicit Impl(T& ref) : ref_(ref) {} // NOLINT
571
572 virtual Result Perform(const ArgumentTuple&) {
573 return ref_;
574 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000575
shiqiane35fdd92008-12-10 05:08:54 +0000576 private:
577 T& ref_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000578
579 GTEST_DISALLOW_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000580 };
581
582 T& ref_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000583
584 GTEST_DISALLOW_ASSIGN_(ReturnRefAction);
shiqiane35fdd92008-12-10 05:08:54 +0000585};
586
zhanyong.wane3bd0982010-07-03 00:16:42 +0000587// Implements the polymorphic ReturnRefOfCopy(x) action, which can be
588// used in any function that returns a reference to the type of x,
589// regardless of the argument types.
590template <typename T>
591class ReturnRefOfCopyAction {
592 public:
593 // Constructs a ReturnRefOfCopyAction object from the reference to
594 // be returned.
595 explicit ReturnRefOfCopyAction(const T& value) : value_(value) {} // NOLINT
596
597 // This template type conversion operator allows ReturnRefOfCopy(x) to be
598 // used in ANY function that returns a reference to x's type.
599 template <typename F>
600 operator Action<F>() const {
601 typedef typename Function<F>::Result Result;
602 // Asserts that the function return type is a reference. This
603 // catches the user error of using ReturnRefOfCopy(x) when Return(x)
604 // should be used, and generates some helpful error message.
605 GTEST_COMPILE_ASSERT_(
606 internal::is_reference<Result>::value,
607 use_Return_instead_of_ReturnRefOfCopy_to_return_a_value);
608 return Action<F>(new Impl<F>(value_));
609 }
610
611 private:
612 // Implements the ReturnRefOfCopy(x) action for a particular function type F.
613 template <typename F>
614 class Impl : public ActionInterface<F> {
615 public:
616 typedef typename Function<F>::Result Result;
617 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
618
619 explicit Impl(const T& value) : value_(value) {} // NOLINT
620
621 virtual Result Perform(const ArgumentTuple&) {
622 return value_;
623 }
624
625 private:
626 T value_;
627
628 GTEST_DISALLOW_ASSIGN_(Impl);
629 };
630
631 const T value_;
632
633 GTEST_DISALLOW_ASSIGN_(ReturnRefOfCopyAction);
634};
635
shiqiane35fdd92008-12-10 05:08:54 +0000636// Implements the DoDefault() action for a particular function type F.
637template <typename F>
638class MonomorphicDoDefaultActionImpl : public ActionInterface<F> {
639 public:
640 typedef typename Function<F>::Result Result;
641 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
642
643 MonomorphicDoDefaultActionImpl() : ActionInterface<F>(true) {}
644
645 // For technical reasons, DoDefault() cannot be used inside a
646 // composite action (e.g. DoAll(...)). It can only be used at the
647 // top level in an EXPECT_CALL(). If this function is called, the
648 // user must be using DoDefault() inside a composite action, and we
649 // have to generate a run-time error.
650 virtual Result Perform(const ArgumentTuple&) {
651 Assert(false, __FILE__, __LINE__,
652 "You are using DoDefault() inside a composite action like "
653 "DoAll() or WithArgs(). This is not supported for technical "
654 "reasons. Please instead spell out the default action, or "
655 "assign the default action to an Action variable and use "
656 "the variable in various places.");
657 return internal::Invalid<Result>();
658 // The above statement will never be reached, but is required in
659 // order for this function to compile.
660 }
661};
662
663// Implements the polymorphic DoDefault() action.
664class DoDefaultAction {
665 public:
666 // This template type conversion operator allows DoDefault() to be
667 // used in any function.
668 template <typename F>
669 operator Action<F>() const {
670 return Action<F>(new MonomorphicDoDefaultActionImpl<F>);
671 }
672};
673
674// Implements the Assign action to set a given pointer referent to a
675// particular value.
676template <typename T1, typename T2>
677class AssignAction {
678 public:
679 AssignAction(T1* ptr, T2 value) : ptr_(ptr), value_(value) {}
680
681 template <typename Result, typename ArgumentTuple>
zhanyong.wan3fbd2dd2009-03-26 19:06:45 +0000682 void Perform(const ArgumentTuple& /* args */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000683 *ptr_ = value_;
684 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000685
shiqiane35fdd92008-12-10 05:08:54 +0000686 private:
687 T1* const ptr_;
688 const T2 value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000689
690 GTEST_DISALLOW_ASSIGN_(AssignAction);
shiqiane35fdd92008-12-10 05:08:54 +0000691};
692
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000693#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000694
shiqiane35fdd92008-12-10 05:08:54 +0000695// Implements the SetErrnoAndReturn action to simulate return from
696// various system calls and libc functions.
697template <typename T>
698class SetErrnoAndReturnAction {
699 public:
700 SetErrnoAndReturnAction(int errno_value, T result)
701 : errno_(errno_value),
702 result_(result) {}
703 template <typename Result, typename ArgumentTuple>
zhanyong.wan3fbd2dd2009-03-26 19:06:45 +0000704 Result Perform(const ArgumentTuple& /* args */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000705 errno = errno_;
706 return result_;
707 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000708
shiqiane35fdd92008-12-10 05:08:54 +0000709 private:
710 const int errno_;
711 const T result_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000712
713 GTEST_DISALLOW_ASSIGN_(SetErrnoAndReturnAction);
shiqiane35fdd92008-12-10 05:08:54 +0000714};
715
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000716#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000717
shiqiane35fdd92008-12-10 05:08:54 +0000718// Implements the SetArgumentPointee<N>(x) action for any function
719// whose N-th argument (0-based) is a pointer to x's type. The
720// template parameter kIsProto is true iff type A is ProtocolMessage,
721// proto2::Message, or a sub-class of those.
722template <size_t N, typename A, bool kIsProto>
723class SetArgumentPointeeAction {
724 public:
725 // Constructs an action that sets the variable pointed to by the
726 // N-th function argument to 'value'.
727 explicit SetArgumentPointeeAction(const A& value) : value_(value) {}
728
729 template <typename Result, typename ArgumentTuple>
730 void Perform(const ArgumentTuple& args) const {
731 CompileAssertTypesEqual<void, Result>();
732 *::std::tr1::get<N>(args) = value_;
733 }
734
735 private:
736 const A value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000737
738 GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
shiqiane35fdd92008-12-10 05:08:54 +0000739};
740
741template <size_t N, typename Proto>
742class SetArgumentPointeeAction<N, Proto, true> {
743 public:
744 // Constructs an action that sets the variable pointed to by the
745 // N-th function argument to 'proto'. Both ProtocolMessage and
746 // proto2::Message have the CopyFrom() method, so the same
747 // implementation works for both.
748 explicit SetArgumentPointeeAction(const Proto& proto) : proto_(new Proto) {
749 proto_->CopyFrom(proto);
750 }
751
752 template <typename Result, typename ArgumentTuple>
753 void Perform(const ArgumentTuple& args) const {
754 CompileAssertTypesEqual<void, Result>();
755 ::std::tr1::get<N>(args)->CopyFrom(*proto_);
756 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000757
shiqiane35fdd92008-12-10 05:08:54 +0000758 private:
759 const internal::linked_ptr<Proto> proto_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000760
761 GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
shiqiane35fdd92008-12-10 05:08:54 +0000762};
763
shiqiane35fdd92008-12-10 05:08:54 +0000764// Implements the InvokeWithoutArgs(f) action. The template argument
765// FunctionImpl is the implementation type of f, which can be either a
766// function pointer or a functor. InvokeWithoutArgs(f) can be used as an
767// Action<F> as long as f's type is compatible with F (i.e. f can be
768// assigned to a tr1::function<F>).
769template <typename FunctionImpl>
770class InvokeWithoutArgsAction {
771 public:
772 // The c'tor makes a copy of function_impl (either a function
773 // pointer or a functor).
774 explicit InvokeWithoutArgsAction(FunctionImpl function_impl)
775 : function_impl_(function_impl) {}
776
777 // Allows InvokeWithoutArgs(f) to be used as any action whose type is
778 // compatible with f.
779 template <typename Result, typename ArgumentTuple>
780 Result Perform(const ArgumentTuple&) { return function_impl_(); }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000781
shiqiane35fdd92008-12-10 05:08:54 +0000782 private:
783 FunctionImpl function_impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000784
785 GTEST_DISALLOW_ASSIGN_(InvokeWithoutArgsAction);
shiqiane35fdd92008-12-10 05:08:54 +0000786};
787
788// Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action.
789template <class Class, typename MethodPtr>
790class InvokeMethodWithoutArgsAction {
791 public:
792 InvokeMethodWithoutArgsAction(Class* obj_ptr, MethodPtr method_ptr)
793 : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
794
795 template <typename Result, typename ArgumentTuple>
796 Result Perform(const ArgumentTuple&) const {
797 return (obj_ptr_->*method_ptr_)();
798 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000799
shiqiane35fdd92008-12-10 05:08:54 +0000800 private:
801 Class* const obj_ptr_;
802 const MethodPtr method_ptr_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000803
804 GTEST_DISALLOW_ASSIGN_(InvokeMethodWithoutArgsAction);
shiqiane35fdd92008-12-10 05:08:54 +0000805};
806
807// Implements the IgnoreResult(action) action.
808template <typename A>
809class IgnoreResultAction {
810 public:
811 explicit IgnoreResultAction(const A& action) : action_(action) {}
812
813 template <typename F>
814 operator Action<F>() const {
815 // Assert statement belongs here because this is the best place to verify
816 // conditions on F. It produces the clearest error messages
817 // in most compilers.
818 // Impl really belongs in this scope as a local class but can't
819 // because MSVC produces duplicate symbols in different translation units
820 // in this case. Until MS fixes that bug we put Impl into the class scope
821 // and put the typedef both here (for use in assert statement) and
822 // in the Impl class. But both definitions must be the same.
823 typedef typename internal::Function<F>::Result Result;
824
825 // Asserts at compile time that F returns void.
826 CompileAssertTypesEqual<void, Result>();
827
828 return Action<F>(new Impl<F>(action_));
829 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000830
shiqiane35fdd92008-12-10 05:08:54 +0000831 private:
832 template <typename F>
833 class Impl : public ActionInterface<F> {
834 public:
835 typedef typename internal::Function<F>::Result Result;
836 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
837
838 explicit Impl(const A& action) : action_(action) {}
839
840 virtual void Perform(const ArgumentTuple& args) {
841 // Performs the action and ignores its result.
842 action_.Perform(args);
843 }
844
845 private:
846 // Type OriginalFunction is the same as F except that its return
847 // type is IgnoredValue.
848 typedef typename internal::Function<F>::MakeResultIgnoredValue
849 OriginalFunction;
850
851 const Action<OriginalFunction> action_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000852
853 GTEST_DISALLOW_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000854 };
855
856 const A action_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000857
858 GTEST_DISALLOW_ASSIGN_(IgnoreResultAction);
shiqiane35fdd92008-12-10 05:08:54 +0000859};
860
zhanyong.wana18423e2009-07-22 23:58:19 +0000861// A ReferenceWrapper<T> object represents a reference to type T,
862// which can be either const or not. It can be explicitly converted
863// from, and implicitly converted to, a T&. Unlike a reference,
864// ReferenceWrapper<T> can be copied and can survive template type
865// inference. This is used to support by-reference arguments in the
866// InvokeArgument<N>(...) action. The idea was from "reference
867// wrappers" in tr1, which we don't have in our source tree yet.
868template <typename T>
869class ReferenceWrapper {
870 public:
871 // Constructs a ReferenceWrapper<T> object from a T&.
872 explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {} // NOLINT
873
874 // Allows a ReferenceWrapper<T> object to be implicitly converted to
875 // a T&.
876 operator T&() const { return *pointer_; }
877 private:
878 T* pointer_;
879};
880
881// Allows the expression ByRef(x) to be printed as a reference to x.
882template <typename T>
883void PrintTo(const ReferenceWrapper<T>& ref, ::std::ostream* os) {
884 T& value = ref;
885 UniversalPrinter<T&>::Print(value, os);
886}
887
888// Does two actions sequentially. Used for implementing the DoAll(a1,
889// a2, ...) action.
890template <typename Action1, typename Action2>
891class DoBothAction {
892 public:
893 DoBothAction(Action1 action1, Action2 action2)
894 : action1_(action1), action2_(action2) {}
895
896 // This template type conversion operator allows DoAll(a1, ..., a_n)
897 // to be used in ANY function of compatible type.
898 template <typename F>
899 operator Action<F>() const {
900 return Action<F>(new Impl<F>(action1_, action2_));
901 }
902
903 private:
904 // Implements the DoAll(...) action for a particular function type F.
905 template <typename F>
906 class Impl : public ActionInterface<F> {
907 public:
908 typedef typename Function<F>::Result Result;
909 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
910 typedef typename Function<F>::MakeResultVoid VoidResult;
911
912 Impl(const Action<VoidResult>& action1, const Action<F>& action2)
913 : action1_(action1), action2_(action2) {}
914
915 virtual Result Perform(const ArgumentTuple& args) {
916 action1_.Perform(args);
917 return action2_.Perform(args);
918 }
919
920 private:
921 const Action<VoidResult> action1_;
922 const Action<F> action2_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000923
924 GTEST_DISALLOW_ASSIGN_(Impl);
zhanyong.wana18423e2009-07-22 23:58:19 +0000925 };
926
927 Action1 action1_;
928 Action2 action2_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000929
930 GTEST_DISALLOW_ASSIGN_(DoBothAction);
zhanyong.wana18423e2009-07-22 23:58:19 +0000931};
932
shiqiane35fdd92008-12-10 05:08:54 +0000933} // namespace internal
934
935// An Unused object can be implicitly constructed from ANY value.
936// This is handy when defining actions that ignore some or all of the
937// mock function arguments. For example, given
938//
939// MOCK_METHOD3(Foo, double(const string& label, double x, double y));
940// MOCK_METHOD3(Bar, double(int index, double x, double y));
941//
942// instead of
943//
944// double DistanceToOriginWithLabel(const string& label, double x, double y) {
945// return sqrt(x*x + y*y);
946// }
947// double DistanceToOriginWithIndex(int index, double x, double y) {
948// return sqrt(x*x + y*y);
949// }
950// ...
951// EXEPCT_CALL(mock, Foo("abc", _, _))
952// .WillOnce(Invoke(DistanceToOriginWithLabel));
953// EXEPCT_CALL(mock, Bar(5, _, _))
954// .WillOnce(Invoke(DistanceToOriginWithIndex));
955//
956// you could write
957//
958// // We can declare any uninteresting argument as Unused.
959// double DistanceToOrigin(Unused, double x, double y) {
960// return sqrt(x*x + y*y);
961// }
962// ...
963// EXEPCT_CALL(mock, Foo("abc", _, _)).WillOnce(Invoke(DistanceToOrigin));
964// EXEPCT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin));
965typedef internal::IgnoredValue Unused;
966
967// This constructor allows us to turn an Action<From> object into an
968// Action<To>, as long as To's arguments can be implicitly converted
969// to From's and From's return type cann be implicitly converted to
970// To's.
971template <typename To>
972template <typename From>
973Action<To>::Action(const Action<From>& from)
974 : impl_(new internal::ActionAdaptor<To, From>(from)) {}
975
976// Creates an action that returns 'value'. 'value' is passed by value
977// instead of const reference - otherwise Return("string literal")
978// will trigger a compiler error about using array as initializer.
979template <typename R>
980internal::ReturnAction<R> Return(R value) {
981 return internal::ReturnAction<R>(value);
982}
983
984// Creates an action that returns NULL.
985inline PolymorphicAction<internal::ReturnNullAction> ReturnNull() {
986 return MakePolymorphicAction(internal::ReturnNullAction());
987}
988
989// Creates an action that returns from a void function.
990inline PolymorphicAction<internal::ReturnVoidAction> Return() {
991 return MakePolymorphicAction(internal::ReturnVoidAction());
992}
993
994// Creates an action that returns the reference to a variable.
995template <typename R>
996inline internal::ReturnRefAction<R> ReturnRef(R& x) { // NOLINT
997 return internal::ReturnRefAction<R>(x);
998}
999
zhanyong.wane3bd0982010-07-03 00:16:42 +00001000// Creates an action that returns the reference to a copy of the
1001// argument. The copy is created when the action is constructed and
1002// lives as long as the action.
1003template <typename R>
1004inline internal::ReturnRefOfCopyAction<R> ReturnRefOfCopy(const R& x) {
1005 return internal::ReturnRefOfCopyAction<R>(x);
1006}
1007
shiqiane35fdd92008-12-10 05:08:54 +00001008// Creates an action that does the default action for the give mock function.
1009inline internal::DoDefaultAction DoDefault() {
1010 return internal::DoDefaultAction();
1011}
1012
1013// Creates an action that sets the variable pointed by the N-th
1014// (0-based) function argument to 'value'.
1015template <size_t N, typename T>
1016PolymorphicAction<
1017 internal::SetArgumentPointeeAction<
1018 N, T, internal::IsAProtocolMessage<T>::value> >
zhanyong.wan59214832010-10-05 05:58:51 +00001019SetArgPointee(const T& x) {
1020 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1021 N, T, internal::IsAProtocolMessage<T>::value>(x));
1022}
zhanyong.wana684b5a2010-12-02 23:30:50 +00001023// This overload allows SetArgPointee() to accept a string literal.
1024template <size_t N>
1025PolymorphicAction<
1026 internal::SetArgumentPointeeAction<N, const char*, false> >
1027SetArgPointee(const char* p) {
1028 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1029 N, const char*, false>(p));
1030}
zhanyong.wan59214832010-10-05 05:58:51 +00001031// The following version is DEPRECATED.
1032template <size_t N, typename T>
1033PolymorphicAction<
1034 internal::SetArgumentPointeeAction<
1035 N, T, internal::IsAProtocolMessage<T>::value> >
shiqiane35fdd92008-12-10 05:08:54 +00001036SetArgumentPointee(const T& x) {
1037 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1038 N, T, internal::IsAProtocolMessage<T>::value>(x));
1039}
1040
shiqiane35fdd92008-12-10 05:08:54 +00001041// Creates an action that sets a pointer referent to a given value.
1042template <typename T1, typename T2>
1043PolymorphicAction<internal::AssignAction<T1, T2> > Assign(T1* ptr, T2 val) {
1044 return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val));
1045}
1046
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001047#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001048
shiqiane35fdd92008-12-10 05:08:54 +00001049// Creates an action that sets errno and returns the appropriate error.
1050template <typename T>
1051PolymorphicAction<internal::SetErrnoAndReturnAction<T> >
1052SetErrnoAndReturn(int errval, T result) {
1053 return MakePolymorphicAction(
1054 internal::SetErrnoAndReturnAction<T>(errval, result));
1055}
1056
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001057#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001058
shiqiane35fdd92008-12-10 05:08:54 +00001059// Various overloads for InvokeWithoutArgs().
1060
1061// Creates an action that invokes 'function_impl' with no argument.
1062template <typename FunctionImpl>
1063PolymorphicAction<internal::InvokeWithoutArgsAction<FunctionImpl> >
1064InvokeWithoutArgs(FunctionImpl function_impl) {
1065 return MakePolymorphicAction(
1066 internal::InvokeWithoutArgsAction<FunctionImpl>(function_impl));
1067}
1068
1069// Creates an action that invokes the given method on the given object
1070// with no argument.
1071template <class Class, typename MethodPtr>
1072PolymorphicAction<internal::InvokeMethodWithoutArgsAction<Class, MethodPtr> >
1073InvokeWithoutArgs(Class* obj_ptr, MethodPtr method_ptr) {
1074 return MakePolymorphicAction(
1075 internal::InvokeMethodWithoutArgsAction<Class, MethodPtr>(
1076 obj_ptr, method_ptr));
1077}
1078
1079// Creates an action that performs an_action and throws away its
1080// result. In other words, it changes the return type of an_action to
1081// void. an_action MUST NOT return void, or the code won't compile.
1082template <typename A>
1083inline internal::IgnoreResultAction<A> IgnoreResult(const A& an_action) {
1084 return internal::IgnoreResultAction<A>(an_action);
1085}
1086
zhanyong.wana18423e2009-07-22 23:58:19 +00001087// Creates a reference wrapper for the given L-value. If necessary,
1088// you can explicitly specify the type of the reference. For example,
1089// suppose 'derived' is an object of type Derived, ByRef(derived)
1090// would wrap a Derived&. If you want to wrap a const Base& instead,
1091// where Base is a base class of Derived, just write:
1092//
1093// ByRef<const Base>(derived)
1094template <typename T>
1095inline internal::ReferenceWrapper<T> ByRef(T& l_value) { // NOLINT
1096 return internal::ReferenceWrapper<T>(l_value);
1097}
1098
shiqiane35fdd92008-12-10 05:08:54 +00001099} // namespace testing
1100
1101#endif // GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_