1 // 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
31 // Google Mock - a framework for writing C++ mock classes.
32 //
33 // This file implements some commonly used variadic actions.
34
35 // GOOGLETEST_CM0002 DO NOT DELETE
36
37 #ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
38 #define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
39
40 #include <memory>
41 #include <utility>
42
43 #include "gmock/gmock-actions.h"
44 #include "gmock/internal/gmock-port.h"
45
46 // Include any custom callback actions added by the local installation.
47 #include "gmock/internal/custom/gmock-generated-actions.h"
48
49 // Sometimes you want to give an action explicit template parameters
50 // that cannot be inferred from its value parameters. ACTION() and
51 // ACTION_P*() don't support that. ACTION_TEMPLATE() remedies that
52 // and can be viewed as an extension to ACTION() and ACTION_P*().
53 //
54 // The syntax:
55 //
56 // ACTION_TEMPLATE(ActionName,
57 // HAS_m_TEMPLATE_PARAMS(kind1, name1, ..., kind_m, name_m),
58 // AND_n_VALUE_PARAMS(p1, ..., p_n)) { statements; }
59 //
60 // defines an action template that takes m explicit template
61 // parameters and n value parameters. name_i is the name of the i-th
62 // template parameter, and kind_i specifies whether it's a typename,
63 // an integral constant, or a template. p_i is the name of the i-th
64 // value parameter.
65 //
66 // Example:
67 //
68 // // DuplicateArg<k, T>(output) converts the k-th argument of the mock
69 // // function to type T and copies it to *output.
70 // ACTION_TEMPLATE(DuplicateArg,
71 // HAS_2_TEMPLATE_PARAMS(int, k, typename, T),
72 // AND_1_VALUE_PARAMS(output)) {
73 // *output = T(::std::get<k>(args));
74 // }
75 // ...
76 // int n;
77 // EXPECT_CALL(mock, Foo(_, _))
78 // .WillOnce(DuplicateArg<1, unsigned char>(&n));
79 //
80 // To create an instance of an action template, write:
81 //
82 // ActionName<t1, ..., t_m>(v1, ..., v_n)
83 //
84 // where the ts are the template arguments and the vs are the value
85 // arguments. The value argument types are inferred by the compiler.
86 // If you want to explicitly specify the value argument types, you can
87 // provide additional template arguments:
88 //
89 // ActionName<t1, ..., t_m, u1, ..., u_k>(v1, ..., v_n)
90 //
91 // where u_i is the desired type of v_i.
92 //
93 // ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded on the
94 // number of value parameters, but not on the number of template
95 // parameters. Without the restriction, the meaning of the following
96 // is unclear:
97 //
98 // OverloadedAction<int, bool>(x);
99 //
100 // Are we using a single-template-parameter action where 'bool' refers
101 // to the type of x, or are we using a two-template-parameter action
102 // where the compiler is asked to infer the type of x?
103 //
104 // Implementation notes:
105 //
106 // GMOCK_INTERNAL_*_HAS_m_TEMPLATE_PARAMS and
107 // GMOCK_INTERNAL_*_AND_n_VALUE_PARAMS are internal macros for
108 // implementing ACTION_TEMPLATE. The main trick we use is to create
109 // new macro invocations when expanding a macro. For example, we have
110 //
111 // #define ACTION_TEMPLATE(name, template_params, value_params)
112 // ... GMOCK_INTERNAL_DECL_##template_params ...
113 //
114 // which causes ACTION_TEMPLATE(..., HAS_1_TEMPLATE_PARAMS(typename, T), ...)
115 // to expand to
116 //
117 // ... GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS(typename, T) ...
118 //
119 // Since GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS is a macro, the
120 // preprocessor will continue to expand it to
121 //
122 // ... typename T ...
123 //
124 // This technique conforms to the C++ standard and is portable. It
125 // allows us to implement action templates using O(N) code, where N is
126 // the maximum number of template/value parameters supported. Without
127 // using it, we'd have to devote O(N^2) amount of code to implement all
128 // combinations of m and n.
129
130 // Declares the template parameters.
131 #define GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS(kind0, name0) kind0 name0
132 #define GMOCK_INTERNAL_DECL_HAS_2_TEMPLATE_PARAMS(kind0, name0, kind1, \
133 name1) kind0 name0, kind1 name1
134 #define GMOCK_INTERNAL_DECL_HAS_3_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
135 kind2, name2) kind0 name0, kind1 name1, kind2 name2
136 #define GMOCK_INTERNAL_DECL_HAS_4_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
137 kind2, name2, kind3, name3) kind0 name0, kind1 name1, kind2 name2, \
138 kind3 name3
139 #define GMOCK_INTERNAL_DECL_HAS_5_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
140 kind2, name2, kind3, name3, kind4, name4) kind0 name0, kind1 name1, \
141 kind2 name2, kind3 name3, kind4 name4
142 #define GMOCK_INTERNAL_DECL_HAS_6_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
143 kind2, name2, kind3, name3, kind4, name4, kind5, name5) kind0 name0, \
144 kind1 name1, kind2 name2, kind3 name3, kind4 name4, kind5 name5
145 #define GMOCK_INTERNAL_DECL_HAS_7_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
146 kind2, name2, kind3, name3, kind4, name4, kind5, name5, kind6, \
147 name6) kind0 name0, kind1 name1, kind2 name2, kind3 name3, kind4 name4, \
148 kind5 name5, kind6 name6
149 #define GMOCK_INTERNAL_DECL_HAS_8_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
150 kind2, name2, kind3, name3, kind4, name4, kind5, name5, kind6, name6, \
151 kind7, name7) kind0 name0, kind1 name1, kind2 name2, kind3 name3, \
152 kind4 name4, kind5 name5, kind6 name6, kind7 name7
153 #define GMOCK_INTERNAL_DECL_HAS_9_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
154 kind2, name2, kind3, name3, kind4, name4, kind5, name5, kind6, name6, \
155 kind7, name7, kind8, name8) kind0 name0, kind1 name1, kind2 name2, \
156 kind3 name3, kind4 name4, kind5 name5, kind6 name6, kind7 name7, \
157 kind8 name8
158 #define GMOCK_INTERNAL_DECL_HAS_10_TEMPLATE_PARAMS(kind0, name0, kind1, \
159 name1, kind2, name2, kind3, name3, kind4, name4, kind5, name5, kind6, \
160 name6, kind7, name7, kind8, name8, kind9, name9) kind0 name0, \
161 kind1 name1, kind2 name2, kind3 name3, kind4 name4, kind5 name5, \
162 kind6 name6, kind7 name7, kind8 name8, kind9 name9
163
164 // Lists the template parameters.
165 #define GMOCK_INTERNAL_LIST_HAS_1_TEMPLATE_PARAMS(kind0, name0) name0
166 #define GMOCK_INTERNAL_LIST_HAS_2_TEMPLATE_PARAMS(kind0, name0, kind1, \
167 name1) name0, name1
168 #define GMOCK_INTERNAL_LIST_HAS_3_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
169 kind2, name2) name0, name1, name2
170 #define GMOCK_INTERNAL_LIST_HAS_4_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
171 kind2, name2, kind3, name3) name0, name1, name2, name3
172 #define GMOCK_INTERNAL_LIST_HAS_5_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
173 kind2, name2, kind3, name3, kind4, name4) name0, name1, name2, name3, \
174 name4
175 #define GMOCK_INTERNAL_LIST_HAS_6_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
176 kind2, name2, kind3, name3, kind4, name4, kind5, name5) name0, name1, \
177 name2, name3, name4, name5
178 #define GMOCK_INTERNAL_LIST_HAS_7_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
179 kind2, name2, kind3, name3, kind4, name4, kind5, name5, kind6, \
180 name6) name0, name1, name2, name3, name4, name5, name6
181 #define GMOCK_INTERNAL_LIST_HAS_8_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
182 kind2, name2, kind3, name3, kind4, name4, kind5, name5, kind6, name6, \
183 kind7, name7) name0, name1, name2, name3, name4, name5, name6, name7
184 #define GMOCK_INTERNAL_LIST_HAS_9_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
185 kind2, name2, kind3, name3, kind4, name4, kind5, name5, kind6, name6, \
186 kind7, name7, kind8, name8) name0, name1, name2, name3, name4, name5, \
187 name6, name7, name8
188 #define GMOCK_INTERNAL_LIST_HAS_10_TEMPLATE_PARAMS(kind0, name0, kind1, \
189 name1, kind2, name2, kind3, name3, kind4, name4, kind5, name5, kind6, \
190 name6, kind7, name7, kind8, name8, kind9, name9) name0, name1, name2, \
191 name3, name4, name5, name6, name7, name8, name9
192
193 // Declares the types of value parameters.
194 #define GMOCK_INTERNAL_DECL_TYPE_AND_0_VALUE_PARAMS()
195 #define GMOCK_INTERNAL_DECL_TYPE_AND_1_VALUE_PARAMS(p0) , typename p0##_type
196 #define GMOCK_INTERNAL_DECL_TYPE_AND_2_VALUE_PARAMS(p0, p1) , \
197 typename p0##_type, typename p1##_type
198 #define GMOCK_INTERNAL_DECL_TYPE_AND_3_VALUE_PARAMS(p0, p1, p2) , \
199 typename p0##_type, typename p1##_type, typename p2##_type
200 #define GMOCK_INTERNAL_DECL_TYPE_AND_4_VALUE_PARAMS(p0, p1, p2, p3) , \
201 typename p0##_type, typename p1##_type, typename p2##_type, \
202 typename p3##_type
203 #define GMOCK_INTERNAL_DECL_TYPE_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) , \
204 typename p0##_type, typename p1##_type, typename p2##_type, \
205 typename p3##_type, typename p4##_type
206 #define GMOCK_INTERNAL_DECL_TYPE_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) , \
207 typename p0##_type, typename p1##_type, typename p2##_type, \
208 typename p3##_type, typename p4##_type, typename p5##_type
209 #define GMOCK_INTERNAL_DECL_TYPE_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
210 p6) , typename p0##_type, typename p1##_type, typename p2##_type, \
211 typename p3##_type, typename p4##_type, typename p5##_type, \
212 typename p6##_type
213 #define GMOCK_INTERNAL_DECL_TYPE_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
214 p6, p7) , typename p0##_type, typename p1##_type, typename p2##_type, \
215 typename p3##_type, typename p4##_type, typename p5##_type, \
216 typename p6##_type, typename p7##_type
217 #define GMOCK_INTERNAL_DECL_TYPE_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
218 p6, p7, p8) , typename p0##_type, typename p1##_type, typename p2##_type, \
219 typename p3##_type, typename p4##_type, typename p5##_type, \
220 typename p6##_type, typename p7##_type, typename p8##_type
221 #define GMOCK_INTERNAL_DECL_TYPE_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
222 p6, p7, p8, p9) , typename p0##_type, typename p1##_type, \
223 typename p2##_type, typename p3##_type, typename p4##_type, \
224 typename p5##_type, typename p6##_type, typename p7##_type, \
225 typename p8##_type, typename p9##_type
226
227 // Initializes the value parameters.
228 #define GMOCK_INTERNAL_INIT_AND_0_VALUE_PARAMS()\
229 ()
230 #define GMOCK_INTERNAL_INIT_AND_1_VALUE_PARAMS(p0)\
231 (p0##_type gmock_p0) : p0(::std::move(gmock_p0))
232 #define GMOCK_INTERNAL_INIT_AND_2_VALUE_PARAMS(p0, p1)\
233 (p0##_type gmock_p0, p1##_type gmock_p1) : p0(::std::move(gmock_p0)), \
234 p1(::std::move(gmock_p1))
235 #define GMOCK_INTERNAL_INIT_AND_3_VALUE_PARAMS(p0, p1, p2)\
236 (p0##_type gmock_p0, p1##_type gmock_p1, \
237 p2##_type gmock_p2) : p0(::std::move(gmock_p0)), \
238 p1(::std::move(gmock_p1)), p2(::std::move(gmock_p2))
239 #define GMOCK_INTERNAL_INIT_AND_4_VALUE_PARAMS(p0, p1, p2, p3)\
240 (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
241 p3##_type gmock_p3) : p0(::std::move(gmock_p0)), \
242 p1(::std::move(gmock_p1)), p2(::std::move(gmock_p2)), \
243 p3(::std::move(gmock_p3))
244 #define GMOCK_INTERNAL_INIT_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4)\
245 (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
246 p3##_type gmock_p3, p4##_type gmock_p4) : p0(::std::move(gmock_p0)), \
247 p1(::std::move(gmock_p1)), p2(::std::move(gmock_p2)), \
248 p3(::std::move(gmock_p3)), p4(::std::move(gmock_p4))
249 #define GMOCK_INTERNAL_INIT_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5)\
250 (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
251 p3##_type gmock_p3, p4##_type gmock_p4, \
252 p5##_type gmock_p5) : p0(::std::move(gmock_p0)), \
253 p1(::std::move(gmock_p1)), p2(::std::move(gmock_p2)), \
254 p3(::std::move(gmock_p3)), p4(::std::move(gmock_p4)), \
255 p5(::std::move(gmock_p5))
256 #define GMOCK_INTERNAL_INIT_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6)\
257 (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
258 p3##_type gmock_p3, p4##_type gmock_p4, p5##_type gmock_p5, \
259 p6##_type gmock_p6) : p0(::std::move(gmock_p0)), \
260 p1(::std::move(gmock_p1)), p2(::std::move(gmock_p2)), \
261 p3(::std::move(gmock_p3)), p4(::std::move(gmock_p4)), \
262 p5(::std::move(gmock_p5)), p6(::std::move(gmock_p6))
263 #define GMOCK_INTERNAL_INIT_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7)\
264 (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
265 p3##_type gmock_p3, p4##_type gmock_p4, p5##_type gmock_p5, \
266 p6##_type gmock_p6, p7##_type gmock_p7) : p0(::std::move(gmock_p0)), \
267 p1(::std::move(gmock_p1)), p2(::std::move(gmock_p2)), \
268 p3(::std::move(gmock_p3)), p4(::std::move(gmock_p4)), \
269 p5(::std::move(gmock_p5)), p6(::std::move(gmock_p6)), \
270 p7(::std::move(gmock_p7))
271 #define GMOCK_INTERNAL_INIT_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
272 p7, p8)\
273 (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
274 p3##_type gmock_p3, p4##_type gmock_p4, p5##_type gmock_p5, \
275 p6##_type gmock_p6, p7##_type gmock_p7, \
276 p8##_type gmock_p8) : p0(::std::move(gmock_p0)), \
277 p1(::std::move(gmock_p1)), p2(::std::move(gmock_p2)), \
278 p3(::std::move(gmock_p3)), p4(::std::move(gmock_p4)), \
279 p5(::std::move(gmock_p5)), p6(::std::move(gmock_p6)), \
280 p7(::std::move(gmock_p7)), p8(::std::move(gmock_p8))
281 #define GMOCK_INTERNAL_INIT_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
282 p7, p8, p9)\
283 (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
284 p3##_type gmock_p3, p4##_type gmock_p4, p5##_type gmock_p5, \
285 p6##_type gmock_p6, p7##_type gmock_p7, p8##_type gmock_p8, \
286 p9##_type gmock_p9) : p0(::std::move(gmock_p0)), \
287 p1(::std::move(gmock_p1)), p2(::std::move(gmock_p2)), \
288 p3(::std::move(gmock_p3)), p4(::std::move(gmock_p4)), \
289 p5(::std::move(gmock_p5)), p6(::std::move(gmock_p6)), \
290 p7(::std::move(gmock_p7)), p8(::std::move(gmock_p8)), \
291 p9(::std::move(gmock_p9))
292
293 // Defines the copy constructor
294 #define GMOCK_INTERNAL_DEFN_COPY_AND_0_VALUE_PARAMS() \
295 {} // Avoid https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82134
296 #define GMOCK_INTERNAL_DEFN_COPY_AND_1_VALUE_PARAMS(...) = default;
297 #define GMOCK_INTERNAL_DEFN_COPY_AND_2_VALUE_PARAMS(...) = default;
298 #define GMOCK_INTERNAL_DEFN_COPY_AND_3_VALUE_PARAMS(...) = default;
299 #define GMOCK_INTERNAL_DEFN_COPY_AND_4_VALUE_PARAMS(...) = default;
300 #define GMOCK_INTERNAL_DEFN_COPY_AND_5_VALUE_PARAMS(...) = default;
301 #define GMOCK_INTERNAL_DEFN_COPY_AND_6_VALUE_PARAMS(...) = default;
302 #define GMOCK_INTERNAL_DEFN_COPY_AND_7_VALUE_PARAMS(...) = default;
303 #define GMOCK_INTERNAL_DEFN_COPY_AND_8_VALUE_PARAMS(...) = default;
304 #define GMOCK_INTERNAL_DEFN_COPY_AND_9_VALUE_PARAMS(...) = default;
305 #define GMOCK_INTERNAL_DEFN_COPY_AND_10_VALUE_PARAMS(...) = default;
306
307 // Declares the fields for storing the value parameters.
308 #define GMOCK_INTERNAL_DEFN_AND_0_VALUE_PARAMS()
309 #define GMOCK_INTERNAL_DEFN_AND_1_VALUE_PARAMS(p0) p0##_type p0;
310 #define GMOCK_INTERNAL_DEFN_AND_2_VALUE_PARAMS(p0, p1) p0##_type p0; \
311 p1##_type p1;
312 #define GMOCK_INTERNAL_DEFN_AND_3_VALUE_PARAMS(p0, p1, p2) p0##_type p0; \
313 p1##_type p1; p2##_type p2;
314 #define GMOCK_INTERNAL_DEFN_AND_4_VALUE_PARAMS(p0, p1, p2, p3) p0##_type p0; \
315 p1##_type p1; p2##_type p2; p3##_type p3;
316 #define GMOCK_INTERNAL_DEFN_AND_5_VALUE_PARAMS(p0, p1, p2, p3, \
317 p4) p0##_type p0; p1##_type p1; p2##_type p2; p3##_type p3; p4##_type p4;
318 #define GMOCK_INTERNAL_DEFN_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, \
319 p5) p0##_type p0; p1##_type p1; p2##_type p2; p3##_type p3; p4##_type p4; \
320 p5##_type p5;
321 #define GMOCK_INTERNAL_DEFN_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
322 p6) p0##_type p0; p1##_type p1; p2##_type p2; p3##_type p3; p4##_type p4; \
323 p5##_type p5; p6##_type p6;
324 #define GMOCK_INTERNAL_DEFN_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
325 p7) p0##_type p0; p1##_type p1; p2##_type p2; p3##_type p3; p4##_type p4; \
326 p5##_type p5; p6##_type p6; p7##_type p7;
327 #define GMOCK_INTERNAL_DEFN_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
328 p7, p8) p0##_type p0; p1##_type p1; p2##_type p2; p3##_type p3; \
329 p4##_type p4; p5##_type p5; p6##_type p6; p7##_type p7; p8##_type p8;
330 #define GMOCK_INTERNAL_DEFN_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
331 p7, p8, p9) p0##_type p0; p1##_type p1; p2##_type p2; p3##_type p3; \
332 p4##_type p4; p5##_type p5; p6##_type p6; p7##_type p7; p8##_type p8; \
333 p9##_type p9;
334
335 // Lists the value parameters.
336 #define GMOCK_INTERNAL_LIST_AND_0_VALUE_PARAMS()
337 #define GMOCK_INTERNAL_LIST_AND_1_VALUE_PARAMS(p0) p0
338 #define GMOCK_INTERNAL_LIST_AND_2_VALUE_PARAMS(p0, p1) p0, p1
339 #define GMOCK_INTERNAL_LIST_AND_3_VALUE_PARAMS(p0, p1, p2) p0, p1, p2
340 #define GMOCK_INTERNAL_LIST_AND_4_VALUE_PARAMS(p0, p1, p2, p3) p0, p1, p2, p3
341 #define GMOCK_INTERNAL_LIST_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) p0, p1, \
342 p2, p3, p4
343 #define GMOCK_INTERNAL_LIST_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) p0, \
344 p1, p2, p3, p4, p5
345 #define GMOCK_INTERNAL_LIST_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
346 p6) p0, p1, p2, p3, p4, p5, p6
347 #define GMOCK_INTERNAL_LIST_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
348 p7) p0, p1, p2, p3, p4, p5, p6, p7
349 #define GMOCK_INTERNAL_LIST_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
350 p7, p8) p0, p1, p2, p3, p4, p5, p6, p7, p8
351 #define GMOCK_INTERNAL_LIST_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
352 p7, p8, p9) p0, p1, p2, p3, p4, p5, p6, p7, p8, p9
353
354 // Lists the value parameter types.
355 #define GMOCK_INTERNAL_LIST_TYPE_AND_0_VALUE_PARAMS()
356 #define GMOCK_INTERNAL_LIST_TYPE_AND_1_VALUE_PARAMS(p0) , p0##_type
357 #define GMOCK_INTERNAL_LIST_TYPE_AND_2_VALUE_PARAMS(p0, p1) , p0##_type, \
358 p1##_type
359 #define GMOCK_INTERNAL_LIST_TYPE_AND_3_VALUE_PARAMS(p0, p1, p2) , p0##_type, \
360 p1##_type, p2##_type
361 #define GMOCK_INTERNAL_LIST_TYPE_AND_4_VALUE_PARAMS(p0, p1, p2, p3) , \
362 p0##_type, p1##_type, p2##_type, p3##_type
363 #define GMOCK_INTERNAL_LIST_TYPE_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) , \
364 p0##_type, p1##_type, p2##_type, p3##_type, p4##_type
365 #define GMOCK_INTERNAL_LIST_TYPE_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) , \
366 p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, p5##_type
367 #define GMOCK_INTERNAL_LIST_TYPE_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
368 p6) , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, p5##_type, \
369 p6##_type
370 #define GMOCK_INTERNAL_LIST_TYPE_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
371 p6, p7) , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, \
372 p5##_type, p6##_type, p7##_type
373 #define GMOCK_INTERNAL_LIST_TYPE_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
374 p6, p7, p8) , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, \
375 p5##_type, p6##_type, p7##_type, p8##_type
376 #define GMOCK_INTERNAL_LIST_TYPE_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
377 p6, p7, p8, p9) , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, \
378 p5##_type, p6##_type, p7##_type, p8##_type, p9##_type
379
380 // Declares the value parameters.
381 #define GMOCK_INTERNAL_DECL_AND_0_VALUE_PARAMS()
382 #define GMOCK_INTERNAL_DECL_AND_1_VALUE_PARAMS(p0) p0##_type p0
383 #define GMOCK_INTERNAL_DECL_AND_2_VALUE_PARAMS(p0, p1) p0##_type p0, \
384 p1##_type p1
385 #define GMOCK_INTERNAL_DECL_AND_3_VALUE_PARAMS(p0, p1, p2) p0##_type p0, \
386 p1##_type p1, p2##_type p2
387 #define GMOCK_INTERNAL_DECL_AND_4_VALUE_PARAMS(p0, p1, p2, p3) p0##_type p0, \
388 p1##_type p1, p2##_type p2, p3##_type p3
389 #define GMOCK_INTERNAL_DECL_AND_5_VALUE_PARAMS(p0, p1, p2, p3, \
390 p4) p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4
391 #define GMOCK_INTERNAL_DECL_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, \
392 p5) p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4, \
393 p5##_type p5
394 #define GMOCK_INTERNAL_DECL_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
395 p6) p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4, \
396 p5##_type p5, p6##_type p6
397 #define GMOCK_INTERNAL_DECL_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
398 p7) p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4, \
399 p5##_type p5, p6##_type p6, p7##_type p7
400 #define GMOCK_INTERNAL_DECL_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
401 p7, p8) p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, \
402 p4##_type p4, p5##_type p5, p6##_type p6, p7##_type p7, p8##_type p8
403 #define GMOCK_INTERNAL_DECL_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
404 p7, p8, p9) p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, \
405 p4##_type p4, p5##_type p5, p6##_type p6, p7##_type p7, p8##_type p8, \
406 p9##_type p9
407
408 // The suffix of the class template implementing the action template.
409 #define GMOCK_INTERNAL_COUNT_AND_0_VALUE_PARAMS()
410 #define GMOCK_INTERNAL_COUNT_AND_1_VALUE_PARAMS(p0) P
411 #define GMOCK_INTERNAL_COUNT_AND_2_VALUE_PARAMS(p0, p1) P2
412 #define GMOCK_INTERNAL_COUNT_AND_3_VALUE_PARAMS(p0, p1, p2) P3
413 #define GMOCK_INTERNAL_COUNT_AND_4_VALUE_PARAMS(p0, p1, p2, p3) P4
414 #define GMOCK_INTERNAL_COUNT_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) P5
415 #define GMOCK_INTERNAL_COUNT_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) P6
416 #define GMOCK_INTERNAL_COUNT_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6) P7
417 #define GMOCK_INTERNAL_COUNT_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
418 p7) P8
419 #define GMOCK_INTERNAL_COUNT_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
420 p7, p8) P9
421 #define GMOCK_INTERNAL_COUNT_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
422 p7, p8, p9) P10
423
424 // The name of the class template implementing the action template.
425 #define GMOCK_ACTION_CLASS_(name, value_params)\
426 GTEST_CONCAT_TOKEN_(name##Action, GMOCK_INTERNAL_COUNT_##value_params)
427
428 #define ACTION_TEMPLATE(name, template_params, value_params) \
429 template <GMOCK_INTERNAL_DECL_##template_params \
430 GMOCK_INTERNAL_DECL_TYPE_##value_params> \
431 class GMOCK_ACTION_CLASS_(name, value_params) { \
432 public: \
433 explicit GMOCK_ACTION_CLASS_(name, value_params)( \
434 GMOCK_INTERNAL_DECL_##value_params) \
435 GMOCK_PP_IF(GMOCK_PP_IS_EMPTY(GMOCK_INTERNAL_COUNT_##value_params), \
436 = default; , \
437 : impl_(std::make_shared<gmock_Impl>( \
438 GMOCK_INTERNAL_LIST_##value_params)) { }) \
439 GMOCK_ACTION_CLASS_(name, value_params)( \
440 const GMOCK_ACTION_CLASS_(name, value_params)&) noexcept \
441 GMOCK_INTERNAL_DEFN_COPY_##value_params \
442 GMOCK_ACTION_CLASS_(name, value_params)( \
443 GMOCK_ACTION_CLASS_(name, value_params)&&) noexcept \
444 GMOCK_INTERNAL_DEFN_COPY_##value_params \
445 template <typename F> \
446 operator ::testing::Action<F>() const { \
447 return GMOCK_PP_IF( \
448 GMOCK_PP_IS_EMPTY(GMOCK_INTERNAL_COUNT_##value_params), \
449 (::testing::internal::MakeAction<F, gmock_Impl>()), \
450 (::testing::internal::MakeAction<F>(impl_))); \
451 } \
452 private: \
453 class gmock_Impl { \
454 public: \
455 explicit gmock_Impl GMOCK_INTERNAL_INIT_##value_params {} \
456 template <typename function_type, typename return_type, \
457 typename args_type, GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
458 return_type gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_) const; \
459 GMOCK_INTERNAL_DEFN_##value_params \
460 }; \
461 GMOCK_PP_IF(GMOCK_PP_IS_EMPTY(GMOCK_INTERNAL_COUNT_##value_params), \
462 , std::shared_ptr<const gmock_Impl> impl_;) \
463 }; \
464 template <GMOCK_INTERNAL_DECL_##template_params \
465 GMOCK_INTERNAL_DECL_TYPE_##value_params> \
466 GMOCK_ACTION_CLASS_(name, value_params)< \
467 GMOCK_INTERNAL_LIST_##template_params \
468 GMOCK_INTERNAL_LIST_TYPE_##value_params> name( \
469 GMOCK_INTERNAL_DECL_##value_params) GTEST_MUST_USE_RESULT_; \
470 template <GMOCK_INTERNAL_DECL_##template_params \
471 GMOCK_INTERNAL_DECL_TYPE_##value_params> \
472 inline GMOCK_ACTION_CLASS_(name, value_params)< \
473 GMOCK_INTERNAL_LIST_##template_params \
474 GMOCK_INTERNAL_LIST_TYPE_##value_params> name( \
475 GMOCK_INTERNAL_DECL_##value_params) { \
476 return GMOCK_ACTION_CLASS_(name, value_params)< \
477 GMOCK_INTERNAL_LIST_##template_params \
478 GMOCK_INTERNAL_LIST_TYPE_##value_params>( \
479 GMOCK_INTERNAL_LIST_##value_params); \
480 } \
481 template <GMOCK_INTERNAL_DECL_##template_params \
482 GMOCK_INTERNAL_DECL_TYPE_##value_params> \
483 template <typename function_type, typename return_type, typename args_type, \
484 GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
485 return_type GMOCK_ACTION_CLASS_(name, value_params)< \
486 GMOCK_INTERNAL_LIST_##template_params \
487 GMOCK_INTERNAL_LIST_TYPE_##value_params>::gmock_Impl::gmock_PerformImpl( \
488 GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
489
490 namespace testing {
491
492 // The ACTION*() macros trigger warning C4100 (unreferenced formal
493 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
494 // the macro definition, as the warnings are generated when the macro
495 // is expanded and macro expansion cannot contain #pragma. Therefore
496 // we suppress them here.
497 #ifdef _MSC_VER
498 # pragma warning(push)
499 # pragma warning(disable:4100)
500 #endif
501
502 namespace internal {
503
504 // internal::InvokeArgument - a helper for InvokeArgument action.
505 // The basic overloads are provided here for generic functors.
506 // Overloads for other custom-callables are provided in the
507 // internal/custom/gmock-generated-actions.h header.
508 template <typename F, typename... Args>
509 auto InvokeArgument(F f, Args... args) -> decltype(f(args...)) {
510 return f(args...);
511 }
512
513 template <std::size_t index, typename... Params>
514 struct InvokeArgumentAction {
515 template <typename... Args>
516 auto operator()(Args&&... args) const -> decltype(internal::InvokeArgument(
517 std::get<index>(std::forward_as_tuple(std::forward<Args>(args)...)),
518 std::declval<const Params&>()...)) {
519 internal::FlatTuple<Args&&...> args_tuple(FlatTupleConstructTag{},
520 std::forward<Args>(args)...);
521 return params.Apply([&](const Params&... unpacked_params) {
522 auto&& callable = args_tuple.template Get<index>();
523 return internal::InvokeArgument(
524 std::forward<decltype(callable)>(callable), unpacked_params...);
525 });
526 }
527
528 internal::FlatTuple<Params...> params;
529 };
530
531 } // namespace internal
532
533 // The InvokeArgument<N>(a1, a2, ..., a_k) action invokes the N-th
534 // (0-based) argument, which must be a k-ary callable, of the mock
535 // function, with arguments a1, a2, ..., a_k.
536 //
537 // Notes:
538 //
539 // 1. The arguments are passed by value by default. If you need to
540 // pass an argument by reference, wrap it inside std::ref(). For
541 // example,
542 //
543 // InvokeArgument<1>(5, string("Hello"), std::ref(foo))
544 //
545 // passes 5 and string("Hello") by value, and passes foo by
546 // reference.
547 //
548 // 2. If the callable takes an argument by reference but std::ref() is
549 // not used, it will receive the reference to a copy of the value,
550 // instead of the original value. For example, when the 0-th
551 // argument of the mock function takes a const string&, the action
552 //
553 // InvokeArgument<0>(string("Hello"))
554 //
555 // makes a copy of the temporary string("Hello") object and passes a
556 // reference of the copy, instead of the original temporary object,
557 // to the callable. This makes it easy for a user to define an
558 // InvokeArgument action from temporary values and have it performed
559 // later.
560 template <std::size_t index, typename... Params>
561 internal::InvokeArgumentAction<index, typename std::decay<Params>::type...>
InvokeArgument(Params &&...params)562 InvokeArgument(Params&&... params) {
563 return {internal::FlatTuple<typename std::decay<Params>::type...>(
564 internal::FlatTupleConstructTag{}, std::forward<Params>(params)...)};
565 }
566
567 #ifdef _MSC_VER
568 # pragma warning(pop)
569 #endif
570
571 } // namespace testing
572
573 #endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
574