1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Author: wan@google.com (Zhanyong Wan)
32 // Google Mock - a framework for writing C++ mock classes.
34 // This file implements some actions that depend on gmock-generated-actions.h.
36 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
37 #define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
41 #include "gmock/gmock-generated-actions.h"
46 // Implements the Invoke(f) action. The template argument
47 // FunctionImpl is the implementation type of f, which can be either a
48 // function pointer or a functor. Invoke(f) can be used as an
49 // Action<F> as long as f's type is compatible with F (i.e. f can be
50 // assigned to a tr1::function<F>).
51 template <typename FunctionImpl>
54 // The c'tor makes a copy of function_impl (either a function
55 // pointer or a functor).
56 explicit InvokeAction(FunctionImpl function_impl)
57 : function_impl_(function_impl) {}
59 template <typename Result, typename ArgumentTuple>
60 Result Perform(const ArgumentTuple& args) {
61 return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args);
65 FunctionImpl function_impl_;
67 GTEST_DISALLOW_ASSIGN_(InvokeAction);
70 // Implements the Invoke(object_ptr, &Class::Method) action.
71 template <class Class, typename MethodPtr>
72 class InvokeMethodAction {
74 InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr)
75 : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
77 template <typename Result, typename ArgumentTuple>
78 Result Perform(const ArgumentTuple& args) const {
79 return InvokeHelper<Result, ArgumentTuple>::InvokeMethod(
80 obj_ptr_, method_ptr_, args);
84 Class* const obj_ptr_;
85 const MethodPtr method_ptr_;
87 GTEST_DISALLOW_ASSIGN_(InvokeMethodAction);
90 } // namespace internal
92 // Various overloads for Invoke().
94 // Creates an action that invokes 'function_impl' with the mock
95 // function's arguments.
96 template <typename FunctionImpl>
97 PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke(
98 FunctionImpl function_impl) {
99 return MakePolymorphicAction(
100 internal::InvokeAction<FunctionImpl>(function_impl));
103 // Creates an action that invokes the given method on the given object
104 // with the mock function's arguments.
105 template <class Class, typename MethodPtr>
106 PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke(
107 Class* obj_ptr, MethodPtr method_ptr) {
108 return MakePolymorphicAction(
109 internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr));
112 // WithoutArgs(inner_action) can be used in a mock function with a
113 // non-empty argument list to perform inner_action, which takes no
114 // argument. In other words, it adapts an action accepting no
115 // argument to one that accepts (and ignores) arguments.
116 template <typename InnerAction>
117 inline internal::WithArgsAction<InnerAction>
118 WithoutArgs(const InnerAction& action) {
119 return internal::WithArgsAction<InnerAction>(action);
122 // WithArg<k>(an_action) creates an action that passes the k-th
123 // (0-based) argument of the mock function to an_action and performs
124 // it. It adapts an action accepting one argument to one that accepts
125 // multiple arguments. For convenience, we also provide
126 // WithArgs<k>(an_action) (defined below) as a synonym.
127 template <int k, typename InnerAction>
128 inline internal::WithArgsAction<InnerAction, k>
129 WithArg(const InnerAction& action) {
130 return internal::WithArgsAction<InnerAction, k>(action);
133 // The ACTION*() macros trigger warning C4100 (unreferenced formal
134 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
135 // the macro definition, as the warnings are generated when the macro
136 // is expanded and macro expansion cannot contain #pragma. Therefore
137 // we suppress them here.
139 # pragma warning(push)
140 # pragma warning(disable:4100)
143 // Action ReturnArg<k>() returns the k-th argument of the mock function.
144 ACTION_TEMPLATE(ReturnArg,
145 HAS_1_TEMPLATE_PARAMS(int, k),
146 AND_0_VALUE_PARAMS()) {
147 return std::tr1::get<k>(args);
150 // Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
151 // mock function to *pointer.
152 ACTION_TEMPLATE(SaveArg,
153 HAS_1_TEMPLATE_PARAMS(int, k),
154 AND_1_VALUE_PARAMS(pointer)) {
155 *pointer = ::std::tr1::get<k>(args);
158 // Action SaveArgPointee<k>(pointer) saves the value pointed to
159 // by the k-th (0-based) argument of the mock function to *pointer.
160 ACTION_TEMPLATE(SaveArgPointee,
161 HAS_1_TEMPLATE_PARAMS(int, k),
162 AND_1_VALUE_PARAMS(pointer)) {
163 *pointer = *::std::tr1::get<k>(args);
166 // Action SetArgReferee<k>(value) assigns 'value' to the variable
167 // referenced by the k-th (0-based) argument of the mock function.
168 ACTION_TEMPLATE(SetArgReferee,
169 HAS_1_TEMPLATE_PARAMS(int, k),
170 AND_1_VALUE_PARAMS(value)) {
171 typedef typename ::std::tr1::tuple_element<k, args_type>::type argk_type;
172 // Ensures that argument #k is a reference. If you get a compiler
173 // error on the next line, you are using SetArgReferee<k>(value) in
174 // a mock function whose k-th (0-based) argument is not a reference.
175 GTEST_COMPILE_ASSERT_(internal::is_reference<argk_type>::value,
176 SetArgReferee_must_be_used_with_a_reference_argument);
177 ::std::tr1::get<k>(args) = value;
180 // Action SetArrayArgument<k>(first, last) copies the elements in
181 // source range [first, last) to the array pointed to by the k-th
182 // (0-based) argument, which can be either a pointer or an
183 // iterator. The action does not take ownership of the elements in the
185 ACTION_TEMPLATE(SetArrayArgument,
186 HAS_1_TEMPLATE_PARAMS(int, k),
187 AND_2_VALUE_PARAMS(first, last)) {
188 // Microsoft compiler deprecates ::std::copy, so we want to suppress warning
189 // 4996 (Function call with parameters that may be unsafe) there.
191 # pragma warning(push) // Saves the current warning state.
192 # pragma warning(disable:4996) // Temporarily disables warning 4996.
194 ::std::copy(first, last, ::std::tr1::get<k>(args));
196 # pragma warning(pop) // Restores the warning state.
200 // Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
202 ACTION_TEMPLATE(DeleteArg,
203 HAS_1_TEMPLATE_PARAMS(int, k),
204 AND_0_VALUE_PARAMS()) {
205 delete ::std::tr1::get<k>(args);
208 // This action returns the value pointed to by 'pointer'.
209 ACTION_P(ReturnPointee, pointer) { return *pointer; }
211 // Action Throw(exception) can be used in a mock function of any type
212 // to throw the given exception. Any copyable value can be thrown.
213 #if GTEST_HAS_EXCEPTIONS
215 // Suppresses the 'unreachable code' warning that VC generates in opt modes.
217 # pragma warning(push) // Saves the current warning state.
218 # pragma warning(disable:4702) // Temporarily disables warning 4702.
220 ACTION_P(Throw, exception) { throw exception; }
222 # pragma warning(pop) // Restores the warning state.
225 #endif // GTEST_HAS_EXCEPTIONS
228 # pragma warning(pop)
231 } // namespace testing
233 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_