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