blob: af3483f75d12694e34e7866630e22dabd9c30f10 [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
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) {
166 delete value_;
167 value_ = new T(x);
168 }
169
170 // Unsets the default value for type T.
171 static void Clear() {
172 delete value_;
173 value_ = NULL;
174 }
175
176 // Returns true iff the user has set the default value for type T.
177 static bool IsSet() { return value_ != NULL; }
178
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000179 // Returns true if T has a default return value set by the user or there
180 // exists a built-in default value.
181 static bool Exists() {
182 return IsSet() || internal::BuiltInDefaultValue<T>::Exists();
183 }
184
shiqiane35fdd92008-12-10 05:08:54 +0000185 // Returns the default value for type T if the user has set one;
186 // otherwise returns the built-in default value if there is one;
187 // otherwise aborts the process.
188 static T Get() {
189 return value_ == NULL ?
190 internal::BuiltInDefaultValue<T>::Get() : *value_;
191 }
192 private:
193 static const T* value_;
194};
195
196// This partial specialization allows a user to set default values for
197// reference types.
198template <typename T>
199class DefaultValue<T&> {
200 public:
201 // Sets the default value for type T&.
202 static void Set(T& x) { // NOLINT
203 address_ = &x;
204 }
205
206 // Unsets the default value for type T&.
207 static void Clear() {
208 address_ = NULL;
209 }
210
211 // Returns true iff the user has set the default value for type T&.
212 static bool IsSet() { return address_ != NULL; }
213
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000214 // Returns true if T has a default return value set by the user or there
215 // exists a built-in default value.
216 static bool Exists() {
217 return IsSet() || internal::BuiltInDefaultValue<T&>::Exists();
218 }
219
shiqiane35fdd92008-12-10 05:08:54 +0000220 // Returns the default value for type T& if the user has set one;
221 // otherwise returns the built-in default value if there is one;
222 // otherwise aborts the process.
223 static T& Get() {
224 return address_ == NULL ?
225 internal::BuiltInDefaultValue<T&>::Get() : *address_;
226 }
227 private:
228 static T* address_;
229};
230
231// This specialization allows DefaultValue<void>::Get() to
232// compile.
233template <>
234class DefaultValue<void> {
235 public:
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000236 static bool Exists() { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000237 static void Get() {}
238};
239
240// Points to the user-set default value for type T.
241template <typename T>
242const T* DefaultValue<T>::value_ = NULL;
243
244// Points to the user-set default value for type T&.
245template <typename T>
246T* DefaultValue<T&>::address_ = NULL;
247
248// Implement this interface to define an action for function type F.
249template <typename F>
250class ActionInterface {
251 public:
252 typedef typename internal::Function<F>::Result Result;
253 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
254
zhanyong.waned6c9272011-02-23 19:39:27 +0000255 ActionInterface() {}
shiqiane35fdd92008-12-10 05:08:54 +0000256 virtual ~ActionInterface() {}
257
258 // Performs the action. This method is not const, as in general an
259 // action can have side effects and be stateful. For example, a
260 // get-the-next-element-from-the-collection action will need to
261 // remember the current element.
262 virtual Result Perform(const ArgumentTuple& args) = 0;
263
shiqiane35fdd92008-12-10 05:08:54 +0000264 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000265 GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionInterface);
shiqiane35fdd92008-12-10 05:08:54 +0000266};
267
268// An Action<F> is a copyable and IMMUTABLE (except by assignment)
269// object that represents an action to be taken when a mock function
270// of type F is called. The implementation of Action<T> is just a
271// linked_ptr to const ActionInterface<T>, so copying is fairly cheap.
272// Don't inherit from Action!
273//
274// You can view an object implementing ActionInterface<F> as a
275// concrete action (including its current state), and an Action<F>
276// object as a handle to it.
277template <typename F>
278class Action {
279 public:
280 typedef typename internal::Function<F>::Result Result;
281 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
282
283 // Constructs a null Action. Needed for storing Action objects in
284 // STL containers.
285 Action() : impl_(NULL) {}
286
zhanyong.waned6c9272011-02-23 19:39:27 +0000287 // Constructs an Action from its implementation. A NULL impl is
288 // used to represent the "do-default" action.
shiqiane35fdd92008-12-10 05:08:54 +0000289 explicit Action(ActionInterface<F>* impl) : impl_(impl) {}
290
291 // Copy constructor.
292 Action(const Action& action) : impl_(action.impl_) {}
293
294 // This constructor allows us to turn an Action<Func> object into an
295 // Action<F>, as long as F's arguments can be implicitly converted
vladloseva070cbd2009-11-18 00:09:28 +0000296 // to Func's and Func's return type can be implicitly converted to
shiqiane35fdd92008-12-10 05:08:54 +0000297 // F's.
298 template <typename Func>
299 explicit Action(const Action<Func>& action);
300
301 // Returns true iff this is the DoDefault() action.
zhanyong.waned6c9272011-02-23 19:39:27 +0000302 bool IsDoDefault() const { return impl_.get() == NULL; }
shiqiane35fdd92008-12-10 05:08:54 +0000303
304 // Performs the action. Note that this method is const even though
305 // the corresponding method in ActionInterface is not. The reason
306 // is that a const Action<F> means that it cannot be re-bound to
307 // another concrete action, not that the concrete action it binds to
308 // cannot change state. (Think of the difference between a const
309 // pointer and a pointer to const.)
310 Result Perform(const ArgumentTuple& args) const {
zhanyong.waned6c9272011-02-23 19:39:27 +0000311 internal::Assert(
312 !IsDoDefault(), __FILE__, __LINE__,
313 "You are using DoDefault() inside a composite action like "
314 "DoAll() or WithArgs(). This is not supported for technical "
315 "reasons. Please instead spell out the default action, or "
316 "assign the default action to an Action variable and use "
317 "the variable in various places.");
shiqiane35fdd92008-12-10 05:08:54 +0000318 return impl_->Perform(args);
319 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000320
shiqiane35fdd92008-12-10 05:08:54 +0000321 private:
322 template <typename F1, typename F2>
323 friend class internal::ActionAdaptor;
324
325 internal::linked_ptr<ActionInterface<F> > impl_;
326};
327
328// The PolymorphicAction class template makes it easy to implement a
329// polymorphic action (i.e. an action that can be used in mock
330// functions of than one type, e.g. Return()).
331//
332// To define a polymorphic action, a user first provides a COPYABLE
333// implementation class that has a Perform() method template:
334//
335// class FooAction {
336// public:
337// template <typename Result, typename ArgumentTuple>
338// Result Perform(const ArgumentTuple& args) const {
339// // Processes the arguments and returns a result, using
340// // tr1::get<N>(args) to get the N-th (0-based) argument in the tuple.
341// }
342// ...
343// };
344//
345// Then the user creates the polymorphic action using
346// MakePolymorphicAction(object) where object has type FooAction. See
347// the definition of Return(void) and SetArgumentPointee<N>(value) for
348// complete examples.
349template <typename Impl>
350class PolymorphicAction {
351 public:
352 explicit PolymorphicAction(const Impl& impl) : impl_(impl) {}
353
354 template <typename F>
355 operator Action<F>() const {
356 return Action<F>(new MonomorphicImpl<F>(impl_));
357 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000358
shiqiane35fdd92008-12-10 05:08:54 +0000359 private:
360 template <typename F>
361 class MonomorphicImpl : public ActionInterface<F> {
362 public:
363 typedef typename internal::Function<F>::Result Result;
364 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
365
366 explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
367
368 virtual Result Perform(const ArgumentTuple& args) {
369 return impl_.template Perform<Result>(args);
370 }
371
372 private:
373 Impl impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000374
375 GTEST_DISALLOW_ASSIGN_(MonomorphicImpl);
shiqiane35fdd92008-12-10 05:08:54 +0000376 };
377
378 Impl impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000379
380 GTEST_DISALLOW_ASSIGN_(PolymorphicAction);
shiqiane35fdd92008-12-10 05:08:54 +0000381};
382
383// Creates an Action from its implementation and returns it. The
384// created Action object owns the implementation.
385template <typename F>
386Action<F> MakeAction(ActionInterface<F>* impl) {
387 return Action<F>(impl);
388}
389
390// Creates a polymorphic action from its implementation. This is
391// easier to use than the PolymorphicAction<Impl> constructor as it
392// doesn't require you to explicitly write the template argument, e.g.
393//
394// MakePolymorphicAction(foo);
395// vs
396// PolymorphicAction<TypeOfFoo>(foo);
397template <typename Impl>
398inline PolymorphicAction<Impl> MakePolymorphicAction(const Impl& impl) {
399 return PolymorphicAction<Impl>(impl);
400}
401
402namespace internal {
403
404// Allows an Action<F2> object to pose as an Action<F1>, as long as F2
405// and F1 are compatible.
406template <typename F1, typename F2>
407class ActionAdaptor : public ActionInterface<F1> {
408 public:
409 typedef typename internal::Function<F1>::Result Result;
410 typedef typename internal::Function<F1>::ArgumentTuple ArgumentTuple;
411
412 explicit ActionAdaptor(const Action<F2>& from) : impl_(from.impl_) {}
413
414 virtual Result Perform(const ArgumentTuple& args) {
415 return impl_->Perform(args);
416 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000417
shiqiane35fdd92008-12-10 05:08:54 +0000418 private:
419 const internal::linked_ptr<ActionInterface<F2> > impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000420
421 GTEST_DISALLOW_ASSIGN_(ActionAdaptor);
shiqiane35fdd92008-12-10 05:08:54 +0000422};
423
424// Implements the polymorphic Return(x) action, which can be used in
425// any function that returns the type of x, regardless of the argument
426// types.
vladloseva070cbd2009-11-18 00:09:28 +0000427//
428// Note: The value passed into Return must be converted into
429// Function<F>::Result when this action is cast to Action<F> rather than
430// when that action is performed. This is important in scenarios like
431//
432// MOCK_METHOD1(Method, T(U));
433// ...
434// {
435// Foo foo;
436// X x(&foo);
437// EXPECT_CALL(mock, Method(_)).WillOnce(Return(x));
438// }
439//
440// In the example above the variable x holds reference to foo which leaves
441// scope and gets destroyed. If copying X just copies a reference to foo,
442// that copy will be left with a hanging reference. If conversion to T
443// makes a copy of foo, the above code is safe. To support that scenario, we
444// need to make sure that the type conversion happens inside the EXPECT_CALL
445// statement, and conversion of the result of Return to Action<T(U)> is a
446// good place for that.
447//
shiqiane35fdd92008-12-10 05:08:54 +0000448template <typename R>
449class ReturnAction {
450 public:
451 // Constructs a ReturnAction object from the value to be returned.
452 // 'value' is passed by value instead of by const reference in order
453 // to allow Return("string literal") to compile.
454 explicit ReturnAction(R value) : value_(value) {}
455
456 // This template type conversion operator allows Return(x) to be
457 // used in ANY function that returns x's type.
458 template <typename F>
459 operator Action<F>() const {
460 // Assert statement belongs here because this is the best place to verify
461 // conditions on F. It produces the clearest error messages
462 // in most compilers.
463 // Impl really belongs in this scope as a local class but can't
464 // because MSVC produces duplicate symbols in different translation units
465 // in this case. Until MS fixes that bug we put Impl into the class scope
466 // and put the typedef both here (for use in assert statement) and
467 // in the Impl class. But both definitions must be the same.
468 typedef typename Function<F>::Result Result;
zhanyong.wan02f71062010-05-10 17:14:29 +0000469 GTEST_COMPILE_ASSERT_(
zhanyong.wane0d051e2009-02-19 00:33:37 +0000470 !internal::is_reference<Result>::value,
471 use_ReturnRef_instead_of_Return_to_return_a_reference);
shiqiane35fdd92008-12-10 05:08:54 +0000472 return Action<F>(new Impl<F>(value_));
473 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000474
shiqiane35fdd92008-12-10 05:08:54 +0000475 private:
476 // Implements the Return(x) action for a particular function type F.
477 template <typename F>
478 class Impl : public ActionInterface<F> {
479 public:
480 typedef typename Function<F>::Result Result;
481 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
482
vladloseva070cbd2009-11-18 00:09:28 +0000483 // The implicit cast is necessary when Result has more than one
484 // single-argument constructor (e.g. Result is std::vector<int>) and R
485 // has a type conversion operator template. In that case, value_(value)
486 // won't compile as the compiler doesn't known which constructor of
zhanyong.wan5b61ce32011-02-01 00:00:03 +0000487 // Result to call. ImplicitCast_ forces the compiler to convert R to
vladloseva070cbd2009-11-18 00:09:28 +0000488 // Result without considering explicit constructors, thus resolving the
489 // ambiguity. value_ is then initialized using its copy constructor.
490 explicit Impl(R value)
zhanyong.wan5b61ce32011-02-01 00:00:03 +0000491 : value_(::testing::internal::ImplicitCast_<Result>(value)) {}
shiqiane35fdd92008-12-10 05:08:54 +0000492
493 virtual Result Perform(const ArgumentTuple&) { return value_; }
494
495 private:
zhanyong.wan02f71062010-05-10 17:14:29 +0000496 GTEST_COMPILE_ASSERT_(!internal::is_reference<Result>::value,
vladloseva070cbd2009-11-18 00:09:28 +0000497 Result_cannot_be_a_reference_type);
498 Result value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000499
500 GTEST_DISALLOW_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000501 };
502
503 R value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000504
505 GTEST_DISALLOW_ASSIGN_(ReturnAction);
shiqiane35fdd92008-12-10 05:08:54 +0000506};
507
508// Implements the ReturnNull() action.
509class ReturnNullAction {
510 public:
511 // Allows ReturnNull() to be used in any pointer-returning function.
512 template <typename Result, typename ArgumentTuple>
513 static Result Perform(const ArgumentTuple&) {
zhanyong.wan02f71062010-05-10 17:14:29 +0000514 GTEST_COMPILE_ASSERT_(internal::is_pointer<Result>::value,
zhanyong.wane0d051e2009-02-19 00:33:37 +0000515 ReturnNull_can_be_used_to_return_a_pointer_only);
shiqiane35fdd92008-12-10 05:08:54 +0000516 return NULL;
517 }
518};
519
520// Implements the Return() action.
521class ReturnVoidAction {
522 public:
523 // Allows Return() to be used in any void-returning function.
524 template <typename Result, typename ArgumentTuple>
525 static void Perform(const ArgumentTuple&) {
526 CompileAssertTypesEqual<void, Result>();
527 }
528};
529
530// Implements the polymorphic ReturnRef(x) action, which can be used
531// in any function that returns a reference to the type of x,
532// regardless of the argument types.
533template <typename T>
534class ReturnRefAction {
535 public:
536 // Constructs a ReturnRefAction object from the reference to be returned.
537 explicit ReturnRefAction(T& ref) : ref_(ref) {} // NOLINT
538
539 // This template type conversion operator allows ReturnRef(x) to be
540 // used in ANY function that returns a reference to x's type.
541 template <typename F>
542 operator Action<F>() const {
543 typedef typename Function<F>::Result Result;
544 // Asserts that the function return type is a reference. This
545 // catches the user error of using ReturnRef(x) when Return(x)
546 // should be used, and generates some helpful error message.
zhanyong.wan02f71062010-05-10 17:14:29 +0000547 GTEST_COMPILE_ASSERT_(internal::is_reference<Result>::value,
zhanyong.wane0d051e2009-02-19 00:33:37 +0000548 use_Return_instead_of_ReturnRef_to_return_a_value);
shiqiane35fdd92008-12-10 05:08:54 +0000549 return Action<F>(new Impl<F>(ref_));
550 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000551
shiqiane35fdd92008-12-10 05:08:54 +0000552 private:
553 // Implements the ReturnRef(x) action for a particular function type F.
554 template <typename F>
555 class Impl : public ActionInterface<F> {
556 public:
557 typedef typename Function<F>::Result Result;
558 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
559
560 explicit Impl(T& ref) : ref_(ref) {} // NOLINT
561
562 virtual Result Perform(const ArgumentTuple&) {
563 return ref_;
564 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000565
shiqiane35fdd92008-12-10 05:08:54 +0000566 private:
567 T& ref_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000568
569 GTEST_DISALLOW_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000570 };
571
572 T& ref_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000573
574 GTEST_DISALLOW_ASSIGN_(ReturnRefAction);
shiqiane35fdd92008-12-10 05:08:54 +0000575};
576
zhanyong.wane3bd0982010-07-03 00:16:42 +0000577// Implements the polymorphic ReturnRefOfCopy(x) action, which can be
578// used in any function that returns a reference to the type of x,
579// regardless of the argument types.
580template <typename T>
581class ReturnRefOfCopyAction {
582 public:
583 // Constructs a ReturnRefOfCopyAction object from the reference to
584 // be returned.
585 explicit ReturnRefOfCopyAction(const T& value) : value_(value) {} // NOLINT
586
587 // This template type conversion operator allows ReturnRefOfCopy(x) to be
588 // used in ANY function that returns a reference to x's type.
589 template <typename F>
590 operator Action<F>() const {
591 typedef typename Function<F>::Result Result;
592 // Asserts that the function return type is a reference. This
593 // catches the user error of using ReturnRefOfCopy(x) when Return(x)
594 // should be used, and generates some helpful error message.
595 GTEST_COMPILE_ASSERT_(
596 internal::is_reference<Result>::value,
597 use_Return_instead_of_ReturnRefOfCopy_to_return_a_value);
598 return Action<F>(new Impl<F>(value_));
599 }
600
601 private:
602 // Implements the ReturnRefOfCopy(x) action for a particular function type F.
603 template <typename F>
604 class Impl : public ActionInterface<F> {
605 public:
606 typedef typename Function<F>::Result Result;
607 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
608
609 explicit Impl(const T& value) : value_(value) {} // NOLINT
610
611 virtual Result Perform(const ArgumentTuple&) {
612 return value_;
613 }
614
615 private:
616 T value_;
617
618 GTEST_DISALLOW_ASSIGN_(Impl);
619 };
620
621 const T value_;
622
623 GTEST_DISALLOW_ASSIGN_(ReturnRefOfCopyAction);
624};
625
shiqiane35fdd92008-12-10 05:08:54 +0000626// Implements the polymorphic DoDefault() action.
627class DoDefaultAction {
628 public:
629 // This template type conversion operator allows DoDefault() to be
630 // used in any function.
631 template <typename F>
zhanyong.waned6c9272011-02-23 19:39:27 +0000632 operator Action<F>() const { return Action<F>(NULL); }
shiqiane35fdd92008-12-10 05:08:54 +0000633};
634
635// Implements the Assign action to set a given pointer referent to a
636// particular value.
637template <typename T1, typename T2>
638class AssignAction {
639 public:
640 AssignAction(T1* ptr, T2 value) : ptr_(ptr), value_(value) {}
641
642 template <typename Result, typename ArgumentTuple>
zhanyong.wan3fbd2dd2009-03-26 19:06:45 +0000643 void Perform(const ArgumentTuple& /* args */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000644 *ptr_ = value_;
645 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000646
shiqiane35fdd92008-12-10 05:08:54 +0000647 private:
648 T1* const ptr_;
649 const T2 value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000650
651 GTEST_DISALLOW_ASSIGN_(AssignAction);
shiqiane35fdd92008-12-10 05:08:54 +0000652};
653
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000654#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000655
shiqiane35fdd92008-12-10 05:08:54 +0000656// Implements the SetErrnoAndReturn action to simulate return from
657// various system calls and libc functions.
658template <typename T>
659class SetErrnoAndReturnAction {
660 public:
661 SetErrnoAndReturnAction(int errno_value, T result)
662 : errno_(errno_value),
663 result_(result) {}
664 template <typename Result, typename ArgumentTuple>
zhanyong.wan3fbd2dd2009-03-26 19:06:45 +0000665 Result Perform(const ArgumentTuple& /* args */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000666 errno = errno_;
667 return result_;
668 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000669
shiqiane35fdd92008-12-10 05:08:54 +0000670 private:
671 const int errno_;
672 const T result_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000673
674 GTEST_DISALLOW_ASSIGN_(SetErrnoAndReturnAction);
shiqiane35fdd92008-12-10 05:08:54 +0000675};
676
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000677#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000678
shiqiane35fdd92008-12-10 05:08:54 +0000679// Implements the SetArgumentPointee<N>(x) action for any function
680// whose N-th argument (0-based) is a pointer to x's type. The
681// template parameter kIsProto is true iff type A is ProtocolMessage,
682// proto2::Message, or a sub-class of those.
683template <size_t N, typename A, bool kIsProto>
684class SetArgumentPointeeAction {
685 public:
686 // Constructs an action that sets the variable pointed to by the
687 // N-th function argument to 'value'.
688 explicit SetArgumentPointeeAction(const A& value) : value_(value) {}
689
690 template <typename Result, typename ArgumentTuple>
691 void Perform(const ArgumentTuple& args) const {
692 CompileAssertTypesEqual<void, Result>();
693 *::std::tr1::get<N>(args) = value_;
694 }
695
696 private:
697 const A value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000698
699 GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
shiqiane35fdd92008-12-10 05:08:54 +0000700};
701
702template <size_t N, typename Proto>
703class SetArgumentPointeeAction<N, Proto, true> {
704 public:
705 // Constructs an action that sets the variable pointed to by the
706 // N-th function argument to 'proto'. Both ProtocolMessage and
707 // proto2::Message have the CopyFrom() method, so the same
708 // implementation works for both.
709 explicit SetArgumentPointeeAction(const Proto& proto) : proto_(new Proto) {
710 proto_->CopyFrom(proto);
711 }
712
713 template <typename Result, typename ArgumentTuple>
714 void Perform(const ArgumentTuple& args) const {
715 CompileAssertTypesEqual<void, Result>();
716 ::std::tr1::get<N>(args)->CopyFrom(*proto_);
717 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000718
shiqiane35fdd92008-12-10 05:08:54 +0000719 private:
720 const internal::linked_ptr<Proto> proto_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000721
722 GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
shiqiane35fdd92008-12-10 05:08:54 +0000723};
724
shiqiane35fdd92008-12-10 05:08:54 +0000725// Implements the InvokeWithoutArgs(f) action. The template argument
726// FunctionImpl is the implementation type of f, which can be either a
727// function pointer or a functor. InvokeWithoutArgs(f) can be used as an
728// Action<F> as long as f's type is compatible with F (i.e. f can be
729// assigned to a tr1::function<F>).
730template <typename FunctionImpl>
731class InvokeWithoutArgsAction {
732 public:
733 // The c'tor makes a copy of function_impl (either a function
734 // pointer or a functor).
735 explicit InvokeWithoutArgsAction(FunctionImpl function_impl)
736 : function_impl_(function_impl) {}
737
738 // Allows InvokeWithoutArgs(f) to be used as any action whose type is
739 // compatible with f.
740 template <typename Result, typename ArgumentTuple>
741 Result Perform(const ArgumentTuple&) { return function_impl_(); }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000742
shiqiane35fdd92008-12-10 05:08:54 +0000743 private:
744 FunctionImpl function_impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000745
746 GTEST_DISALLOW_ASSIGN_(InvokeWithoutArgsAction);
shiqiane35fdd92008-12-10 05:08:54 +0000747};
748
749// Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action.
750template <class Class, typename MethodPtr>
751class InvokeMethodWithoutArgsAction {
752 public:
753 InvokeMethodWithoutArgsAction(Class* obj_ptr, MethodPtr method_ptr)
754 : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
755
756 template <typename Result, typename ArgumentTuple>
757 Result Perform(const ArgumentTuple&) const {
758 return (obj_ptr_->*method_ptr_)();
759 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000760
shiqiane35fdd92008-12-10 05:08:54 +0000761 private:
762 Class* const obj_ptr_;
763 const MethodPtr method_ptr_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000764
765 GTEST_DISALLOW_ASSIGN_(InvokeMethodWithoutArgsAction);
shiqiane35fdd92008-12-10 05:08:54 +0000766};
767
768// Implements the IgnoreResult(action) action.
769template <typename A>
770class IgnoreResultAction {
771 public:
772 explicit IgnoreResultAction(const A& action) : action_(action) {}
773
774 template <typename F>
775 operator Action<F>() const {
776 // Assert statement belongs here because this is the best place to verify
777 // conditions on F. It produces the clearest error messages
778 // in most compilers.
779 // Impl really belongs in this scope as a local class but can't
780 // because MSVC produces duplicate symbols in different translation units
781 // in this case. Until MS fixes that bug we put Impl into the class scope
782 // and put the typedef both here (for use in assert statement) and
783 // in the Impl class. But both definitions must be the same.
784 typedef typename internal::Function<F>::Result Result;
785
786 // Asserts at compile time that F returns void.
787 CompileAssertTypesEqual<void, Result>();
788
789 return Action<F>(new Impl<F>(action_));
790 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000791
shiqiane35fdd92008-12-10 05:08:54 +0000792 private:
793 template <typename F>
794 class Impl : public ActionInterface<F> {
795 public:
796 typedef typename internal::Function<F>::Result Result;
797 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
798
799 explicit Impl(const A& action) : action_(action) {}
800
801 virtual void Perform(const ArgumentTuple& args) {
802 // Performs the action and ignores its result.
803 action_.Perform(args);
804 }
805
806 private:
807 // Type OriginalFunction is the same as F except that its return
808 // type is IgnoredValue.
809 typedef typename internal::Function<F>::MakeResultIgnoredValue
810 OriginalFunction;
811
812 const Action<OriginalFunction> action_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000813
814 GTEST_DISALLOW_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000815 };
816
817 const A action_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000818
819 GTEST_DISALLOW_ASSIGN_(IgnoreResultAction);
shiqiane35fdd92008-12-10 05:08:54 +0000820};
821
zhanyong.wana18423e2009-07-22 23:58:19 +0000822// A ReferenceWrapper<T> object represents a reference to type T,
823// which can be either const or not. It can be explicitly converted
824// from, and implicitly converted to, a T&. Unlike a reference,
825// ReferenceWrapper<T> can be copied and can survive template type
826// inference. This is used to support by-reference arguments in the
827// InvokeArgument<N>(...) action. The idea was from "reference
828// wrappers" in tr1, which we don't have in our source tree yet.
829template <typename T>
830class ReferenceWrapper {
831 public:
832 // Constructs a ReferenceWrapper<T> object from a T&.
833 explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {} // NOLINT
834
835 // Allows a ReferenceWrapper<T> object to be implicitly converted to
836 // a T&.
837 operator T&() const { return *pointer_; }
838 private:
839 T* pointer_;
840};
841
842// Allows the expression ByRef(x) to be printed as a reference to x.
843template <typename T>
844void PrintTo(const ReferenceWrapper<T>& ref, ::std::ostream* os) {
845 T& value = ref;
846 UniversalPrinter<T&>::Print(value, os);
847}
848
849// Does two actions sequentially. Used for implementing the DoAll(a1,
850// a2, ...) action.
851template <typename Action1, typename Action2>
852class DoBothAction {
853 public:
854 DoBothAction(Action1 action1, Action2 action2)
855 : action1_(action1), action2_(action2) {}
856
857 // This template type conversion operator allows DoAll(a1, ..., a_n)
858 // to be used in ANY function of compatible type.
859 template <typename F>
860 operator Action<F>() const {
861 return Action<F>(new Impl<F>(action1_, action2_));
862 }
863
864 private:
865 // Implements the DoAll(...) action for a particular function type F.
866 template <typename F>
867 class Impl : public ActionInterface<F> {
868 public:
869 typedef typename Function<F>::Result Result;
870 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
871 typedef typename Function<F>::MakeResultVoid VoidResult;
872
873 Impl(const Action<VoidResult>& action1, const Action<F>& action2)
874 : action1_(action1), action2_(action2) {}
875
876 virtual Result Perform(const ArgumentTuple& args) {
877 action1_.Perform(args);
878 return action2_.Perform(args);
879 }
880
881 private:
882 const Action<VoidResult> action1_;
883 const Action<F> action2_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000884
885 GTEST_DISALLOW_ASSIGN_(Impl);
zhanyong.wana18423e2009-07-22 23:58:19 +0000886 };
887
888 Action1 action1_;
889 Action2 action2_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000890
891 GTEST_DISALLOW_ASSIGN_(DoBothAction);
zhanyong.wana18423e2009-07-22 23:58:19 +0000892};
893
shiqiane35fdd92008-12-10 05:08:54 +0000894} // namespace internal
895
896// An Unused object can be implicitly constructed from ANY value.
897// This is handy when defining actions that ignore some or all of the
898// mock function arguments. For example, given
899//
900// MOCK_METHOD3(Foo, double(const string& label, double x, double y));
901// MOCK_METHOD3(Bar, double(int index, double x, double y));
902//
903// instead of
904//
905// double DistanceToOriginWithLabel(const string& label, double x, double y) {
906// return sqrt(x*x + y*y);
907// }
908// double DistanceToOriginWithIndex(int index, double x, double y) {
909// return sqrt(x*x + y*y);
910// }
911// ...
912// EXEPCT_CALL(mock, Foo("abc", _, _))
913// .WillOnce(Invoke(DistanceToOriginWithLabel));
914// EXEPCT_CALL(mock, Bar(5, _, _))
915// .WillOnce(Invoke(DistanceToOriginWithIndex));
916//
917// you could write
918//
919// // We can declare any uninteresting argument as Unused.
920// double DistanceToOrigin(Unused, double x, double y) {
921// return sqrt(x*x + y*y);
922// }
923// ...
924// EXEPCT_CALL(mock, Foo("abc", _, _)).WillOnce(Invoke(DistanceToOrigin));
925// EXEPCT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin));
926typedef internal::IgnoredValue Unused;
927
928// This constructor allows us to turn an Action<From> object into an
929// Action<To>, as long as To's arguments can be implicitly converted
930// to From's and From's return type cann be implicitly converted to
931// To's.
932template <typename To>
933template <typename From>
934Action<To>::Action(const Action<From>& from)
935 : impl_(new internal::ActionAdaptor<To, From>(from)) {}
936
937// Creates an action that returns 'value'. 'value' is passed by value
938// instead of const reference - otherwise Return("string literal")
939// will trigger a compiler error about using array as initializer.
940template <typename R>
941internal::ReturnAction<R> Return(R value) {
942 return internal::ReturnAction<R>(value);
943}
944
945// Creates an action that returns NULL.
946inline PolymorphicAction<internal::ReturnNullAction> ReturnNull() {
947 return MakePolymorphicAction(internal::ReturnNullAction());
948}
949
950// Creates an action that returns from a void function.
951inline PolymorphicAction<internal::ReturnVoidAction> Return() {
952 return MakePolymorphicAction(internal::ReturnVoidAction());
953}
954
955// Creates an action that returns the reference to a variable.
956template <typename R>
957inline internal::ReturnRefAction<R> ReturnRef(R& x) { // NOLINT
958 return internal::ReturnRefAction<R>(x);
959}
960
zhanyong.wane3bd0982010-07-03 00:16:42 +0000961// Creates an action that returns the reference to a copy of the
962// argument. The copy is created when the action is constructed and
963// lives as long as the action.
964template <typename R>
965inline internal::ReturnRefOfCopyAction<R> ReturnRefOfCopy(const R& x) {
966 return internal::ReturnRefOfCopyAction<R>(x);
967}
968
shiqiane35fdd92008-12-10 05:08:54 +0000969// Creates an action that does the default action for the give mock function.
970inline internal::DoDefaultAction DoDefault() {
971 return internal::DoDefaultAction();
972}
973
974// Creates an action that sets the variable pointed by the N-th
975// (0-based) function argument to 'value'.
976template <size_t N, typename T>
977PolymorphicAction<
978 internal::SetArgumentPointeeAction<
979 N, T, internal::IsAProtocolMessage<T>::value> >
zhanyong.wan59214832010-10-05 05:58:51 +0000980SetArgPointee(const T& x) {
981 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
982 N, T, internal::IsAProtocolMessage<T>::value>(x));
983}
zhanyong.wana684b5a2010-12-02 23:30:50 +0000984// This overload allows SetArgPointee() to accept a string literal.
985template <size_t N>
986PolymorphicAction<
987 internal::SetArgumentPointeeAction<N, const char*, false> >
988SetArgPointee(const char* p) {
989 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
990 N, const char*, false>(p));
991}
zhanyong.wan59214832010-10-05 05:58:51 +0000992// The following version is DEPRECATED.
993template <size_t N, typename T>
994PolymorphicAction<
995 internal::SetArgumentPointeeAction<
996 N, T, internal::IsAProtocolMessage<T>::value> >
shiqiane35fdd92008-12-10 05:08:54 +0000997SetArgumentPointee(const T& x) {
998 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
999 N, T, internal::IsAProtocolMessage<T>::value>(x));
1000}
1001
shiqiane35fdd92008-12-10 05:08:54 +00001002// Creates an action that sets a pointer referent to a given value.
1003template <typename T1, typename T2>
1004PolymorphicAction<internal::AssignAction<T1, T2> > Assign(T1* ptr, T2 val) {
1005 return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val));
1006}
1007
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001008#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001009
shiqiane35fdd92008-12-10 05:08:54 +00001010// Creates an action that sets errno and returns the appropriate error.
1011template <typename T>
1012PolymorphicAction<internal::SetErrnoAndReturnAction<T> >
1013SetErrnoAndReturn(int errval, T result) {
1014 return MakePolymorphicAction(
1015 internal::SetErrnoAndReturnAction<T>(errval, result));
1016}
1017
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001018#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001019
shiqiane35fdd92008-12-10 05:08:54 +00001020// Various overloads for InvokeWithoutArgs().
1021
1022// Creates an action that invokes 'function_impl' with no argument.
1023template <typename FunctionImpl>
1024PolymorphicAction<internal::InvokeWithoutArgsAction<FunctionImpl> >
1025InvokeWithoutArgs(FunctionImpl function_impl) {
1026 return MakePolymorphicAction(
1027 internal::InvokeWithoutArgsAction<FunctionImpl>(function_impl));
1028}
1029
1030// Creates an action that invokes the given method on the given object
1031// with no argument.
1032template <class Class, typename MethodPtr>
1033PolymorphicAction<internal::InvokeMethodWithoutArgsAction<Class, MethodPtr> >
1034InvokeWithoutArgs(Class* obj_ptr, MethodPtr method_ptr) {
1035 return MakePolymorphicAction(
1036 internal::InvokeMethodWithoutArgsAction<Class, MethodPtr>(
1037 obj_ptr, method_ptr));
1038}
1039
1040// Creates an action that performs an_action and throws away its
1041// result. In other words, it changes the return type of an_action to
1042// void. an_action MUST NOT return void, or the code won't compile.
1043template <typename A>
1044inline internal::IgnoreResultAction<A> IgnoreResult(const A& an_action) {
1045 return internal::IgnoreResultAction<A>(an_action);
1046}
1047
zhanyong.wana18423e2009-07-22 23:58:19 +00001048// Creates a reference wrapper for the given L-value. If necessary,
1049// you can explicitly specify the type of the reference. For example,
1050// suppose 'derived' is an object of type Derived, ByRef(derived)
1051// would wrap a Derived&. If you want to wrap a const Base& instead,
1052// where Base is a base class of Derived, just write:
1053//
1054// ByRef<const Base>(derived)
1055template <typename T>
1056inline internal::ReferenceWrapper<T> ByRef(T& l_value) { // NOLINT
1057 return internal::ReferenceWrapper<T>(l_value);
1058}
1059
shiqiane35fdd92008-12-10 05:08:54 +00001060} // namespace testing
1061
1062#endif // GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_