gmock-more-actions.h 37.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Copyright 2007, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Gennadiy Civil's avatar
 
Gennadiy Civil committed
29

30
31
32
33
// Google Mock - a framework for writing C++ mock classes.
//
// This file implements some commonly used variadic actions.

34
35
36
// IWYU pragma: private, include "gmock/gmock.h"
// IWYU pragma: friend gmock/.*

Abseil Team's avatar
Abseil Team committed
37
38
#ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
#define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
39

misterg's avatar
misterg committed
40
#include <memory>
Abseil Team's avatar
Abseil Team committed
41
42
#include <utility>

43
44
#include "gmock/gmock-actions.h"
#include "gmock/internal/gmock-port.h"
45

ofats's avatar
ofats committed
46
47
// Include any custom callback actions added by the local installation.
#include "gmock/internal/custom/gmock-generated-actions.h"
48

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Sometimes you want to give an action explicit template parameters
// that cannot be inferred from its value parameters.  ACTION() and
// ACTION_P*() don't support that.  ACTION_TEMPLATE() remedies that
// and can be viewed as an extension to ACTION() and ACTION_P*().
//
// The syntax:
//
//   ACTION_TEMPLATE(ActionName,
//                   HAS_m_TEMPLATE_PARAMS(kind1, name1, ..., kind_m, name_m),
//                   AND_n_VALUE_PARAMS(p1, ..., p_n)) { statements; }
//
// defines an action template that takes m explicit template
// parameters and n value parameters.  name_i is the name of the i-th
// template parameter, and kind_i specifies whether it's a typename,
// an integral constant, or a template.  p_i is the name of the i-th
// value parameter.
//
// Example:
//
//   // DuplicateArg<k, T>(output) converts the k-th argument of the mock
//   // function to type T and copies it to *output.
//   ACTION_TEMPLATE(DuplicateArg,
//                   HAS_2_TEMPLATE_PARAMS(int, k, typename, T),
//                   AND_1_VALUE_PARAMS(output)) {
Abseil Team's avatar
Abseil Team committed
73
//     *output = T(::std::get<k>(args));
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//   }
//   ...
//     int n;
//     EXPECT_CALL(mock, Foo(_, _))
//         .WillOnce(DuplicateArg<1, unsigned char>(&n));
//
// To create an instance of an action template, write:
//
//   ActionName<t1, ..., t_m>(v1, ..., v_n)
//
// where the ts are the template arguments and the vs are the value
// arguments.  The value argument types are inferred by the compiler.
// If you want to explicitly specify the value argument types, you can
// provide additional template arguments:
//
//   ActionName<t1, ..., t_m, u1, ..., u_k>(v1, ..., v_n)
//
// where u_i is the desired type of v_i.
//
// ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded on the
// number of value parameters, but not on the number of template
// parameters.  Without the restriction, the meaning of the following
// is unclear:
//
//   OverloadedAction<int, bool>(x);
//
// Are we using a single-template-parameter action where 'bool' refers
// to the type of x, or are we using a two-template-parameter action
// where the compiler is asked to infer the type of x?
//
// Implementation notes:
//
// GMOCK_INTERNAL_*_HAS_m_TEMPLATE_PARAMS and
// GMOCK_INTERNAL_*_AND_n_VALUE_PARAMS are internal macros for
// implementing ACTION_TEMPLATE.  The main trick we use is to create
// new macro invocations when expanding a macro.  For example, we have
//
//   #define ACTION_TEMPLATE(name, template_params, value_params)
//       ... GMOCK_INTERNAL_DECL_##template_params ...
//
// which causes ACTION_TEMPLATE(..., HAS_1_TEMPLATE_PARAMS(typename, T), ...)
// to expand to
//
//       ... GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS(typename, T) ...
//
// Since GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS is a macro, the
// preprocessor will continue to expand it to
//
//       ... typename T ...
//
// This technique conforms to the C++ standard and is portable.  It
// allows us to implement action templates using O(N) code, where N is
// the maximum number of template/value parameters supported.  Without
// using it, we'd have to devote O(N^2) amount of code to implement all
// combinations of m and n.

// Declares the template parameters.
#define GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS(kind0, name0) kind0 name0
132
133
#define GMOCK_INTERNAL_DECL_HAS_2_TEMPLATE_PARAMS(kind0, name0, kind1, name1) \
  kind0 name0, kind1 name1
134
#define GMOCK_INTERNAL_DECL_HAS_3_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
135
136
                                                  kind2, name2)               \
  kind0 name0, kind1 name1, kind2 name2
137
#define GMOCK_INTERNAL_DECL_HAS_4_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
138
139
140
141
142
                                                  kind2, name2, kind3, name3) \
  kind0 name0, kind1 name1, kind2 name2, kind3 name3
#define GMOCK_INTERNAL_DECL_HAS_5_TEMPLATE_PARAMS(                        \
    kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4) \
  kind0 name0, kind1 name1, kind2 name2, kind3 name3, kind4 name4
143
#define GMOCK_INTERNAL_DECL_HAS_6_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
                                                  kind2, name2, kind3, name3, \
                                                  kind4, name4, kind5, name5) \
  kind0 name0, kind1 name1, kind2 name2, kind3 name3, kind4 name4, kind5 name5
#define GMOCK_INTERNAL_DECL_HAS_7_TEMPLATE_PARAMS(                        \
    kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4, \
    kind5, name5, kind6, name6)                                           \
  kind0 name0, kind1 name1, kind2 name2, kind3 name3, kind4 name4,        \
      kind5 name5, kind6 name6
#define GMOCK_INTERNAL_DECL_HAS_8_TEMPLATE_PARAMS(                        \
    kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4, \
    kind5, name5, kind6, name6, kind7, name7)                             \
  kind0 name0, kind1 name1, kind2 name2, kind3 name3, kind4 name4,        \
      kind5 name5, kind6 name6, kind7 name7
#define GMOCK_INTERNAL_DECL_HAS_9_TEMPLATE_PARAMS(                        \
    kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4, \
    kind5, name5, kind6, name6, kind7, name7, kind8, name8)               \
  kind0 name0, kind1 name1, kind2 name2, kind3 name3, kind4 name4,        \
      kind5 name5, kind6 name6, kind7 name7, kind8 name8
#define GMOCK_INTERNAL_DECL_HAS_10_TEMPLATE_PARAMS(                       \
    kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4, \
    kind5, name5, kind6, name6, kind7, name7, kind8, name8, kind9, name9) \
  kind0 name0, kind1 name1, kind2 name2, kind3 name3, kind4 name4,        \
      kind5 name5, kind6 name6, kind7 name7, kind8 name8, kind9 name9
167
168
169

// Lists the template parameters.
#define GMOCK_INTERNAL_LIST_HAS_1_TEMPLATE_PARAMS(kind0, name0) name0
170
171
#define GMOCK_INTERNAL_LIST_HAS_2_TEMPLATE_PARAMS(kind0, name0, kind1, name1) \
  name0, name1
172
#define GMOCK_INTERNAL_LIST_HAS_3_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
173
174
                                                  kind2, name2)               \
  name0, name1, name2
175
#define GMOCK_INTERNAL_LIST_HAS_4_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
176
177
178
179
180
                                                  kind2, name2, kind3, name3) \
  name0, name1, name2, name3
#define GMOCK_INTERNAL_LIST_HAS_5_TEMPLATE_PARAMS(                        \
    kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4) \
  name0, name1, name2, name3, name4
181
#define GMOCK_INTERNAL_LIST_HAS_6_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
                                                  kind2, name2, kind3, name3, \
                                                  kind4, name4, kind5, name5) \
  name0, name1, name2, name3, name4, name5
#define GMOCK_INTERNAL_LIST_HAS_7_TEMPLATE_PARAMS(                        \
    kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4, \
    kind5, name5, kind6, name6)                                           \
  name0, name1, name2, name3, name4, name5, name6
#define GMOCK_INTERNAL_LIST_HAS_8_TEMPLATE_PARAMS(                        \
    kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4, \
    kind5, name5, kind6, name6, kind7, name7)                             \
  name0, name1, name2, name3, name4, name5, name6, name7
#define GMOCK_INTERNAL_LIST_HAS_9_TEMPLATE_PARAMS(                        \
    kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4, \
    kind5, name5, kind6, name6, kind7, name7, kind8, name8)               \
  name0, name1, name2, name3, name4, name5, name6, name7, name8
#define GMOCK_INTERNAL_LIST_HAS_10_TEMPLATE_PARAMS(                       \
    kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4, \
    kind5, name5, kind6, name6, kind7, name7, kind8, name8, kind9, name9) \
  name0, name1, name2, name3, name4, name5, name6, name7, name8, name9
201
202
203
204

// Declares the types of value parameters.
#define GMOCK_INTERNAL_DECL_TYPE_AND_0_VALUE_PARAMS()
#define GMOCK_INTERNAL_DECL_TYPE_AND_1_VALUE_PARAMS(p0) , typename p0##_type
205
206
207
208
209
210
211
212
213
214
215
216
217
#define GMOCK_INTERNAL_DECL_TYPE_AND_2_VALUE_PARAMS(p0, p1) \
  , typename p0##_type, typename p1##_type
#define GMOCK_INTERNAL_DECL_TYPE_AND_3_VALUE_PARAMS(p0, p1, p2) \
  , typename p0##_type, typename p1##_type, typename p2##_type
#define GMOCK_INTERNAL_DECL_TYPE_AND_4_VALUE_PARAMS(p0, p1, p2, p3) \
  , typename p0##_type, typename p1##_type, typename p2##_type,     \
      typename p3##_type
#define GMOCK_INTERNAL_DECL_TYPE_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) \
  , typename p0##_type, typename p1##_type, typename p2##_type,         \
      typename p3##_type, typename p4##_type
#define GMOCK_INTERNAL_DECL_TYPE_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) \
  , typename p0##_type, typename p1##_type, typename p2##_type,             \
      typename p3##_type, typename p4##_type, typename p5##_type
218
#define GMOCK_INTERNAL_DECL_TYPE_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
219
220
221
222
                                                    p6)                     \
  , typename p0##_type, typename p1##_type, typename p2##_type,             \
      typename p3##_type, typename p4##_type, typename p5##_type,           \
      typename p6##_type
223
#define GMOCK_INTERNAL_DECL_TYPE_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
224
225
226
227
                                                    p6, p7)                 \
  , typename p0##_type, typename p1##_type, typename p2##_type,             \
      typename p3##_type, typename p4##_type, typename p5##_type,           \
      typename p6##_type, typename p7##_type
228
#define GMOCK_INTERNAL_DECL_TYPE_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
229
230
231
232
                                                    p6, p7, p8)             \
  , typename p0##_type, typename p1##_type, typename p2##_type,             \
      typename p3##_type, typename p4##_type, typename p5##_type,           \
      typename p6##_type, typename p7##_type, typename p8##_type
233
#define GMOCK_INTERNAL_DECL_TYPE_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
234
235
236
237
238
                                                     p6, p7, p8, p9)         \
  , typename p0##_type, typename p1##_type, typename p2##_type,              \
      typename p3##_type, typename p4##_type, typename p5##_type,            \
      typename p6##_type, typename p7##_type, typename p8##_type,            \
      typename p9##_type
239
240

// Initializes the value parameters.
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#define GMOCK_INTERNAL_INIT_AND_0_VALUE_PARAMS() ()
#define GMOCK_INTERNAL_INIT_AND_1_VALUE_PARAMS(p0) \
  (p0##_type gmock_p0) : p0(::std::move(gmock_p0))
#define GMOCK_INTERNAL_INIT_AND_2_VALUE_PARAMS(p0, p1) \
  (p0##_type gmock_p0, p1##_type gmock_p1)             \
      : p0(::std::move(gmock_p0)), p1(::std::move(gmock_p1))
#define GMOCK_INTERNAL_INIT_AND_3_VALUE_PARAMS(p0, p1, p2)     \
  (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2) \
      : p0(::std::move(gmock_p0)),                             \
        p1(::std::move(gmock_p1)),                             \
        p2(::std::move(gmock_p2))
#define GMOCK_INTERNAL_INIT_AND_4_VALUE_PARAMS(p0, p1, p2, p3) \
  (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
   p3##_type gmock_p3)                                         \
      : p0(::std::move(gmock_p0)),                             \
        p1(::std::move(gmock_p1)),                             \
        p2(::std::move(gmock_p2)),                             \
Abseil Team's avatar
Abseil Team committed
258
        p3(::std::move(gmock_p3))
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#define GMOCK_INTERNAL_INIT_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) \
  (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2,     \
   p3##_type gmock_p3, p4##_type gmock_p4)                         \
      : p0(::std::move(gmock_p0)),                                 \
        p1(::std::move(gmock_p1)),                                 \
        p2(::std::move(gmock_p2)),                                 \
        p3(::std::move(gmock_p3)),                                 \
        p4(::std::move(gmock_p4))
#define GMOCK_INTERNAL_INIT_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) \
  (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2,         \
   p3##_type gmock_p3, p4##_type gmock_p4, p5##_type gmock_p5)         \
      : p0(::std::move(gmock_p0)),                                     \
        p1(::std::move(gmock_p1)),                                     \
        p2(::std::move(gmock_p2)),                                     \
        p3(::std::move(gmock_p3)),                                     \
        p4(::std::move(gmock_p4)),                                     \
Abseil Team's avatar
Abseil Team committed
275
        p5(::std::move(gmock_p5))
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#define GMOCK_INTERNAL_INIT_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6) \
  (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2,             \
   p3##_type gmock_p3, p4##_type gmock_p4, p5##_type gmock_p5,             \
   p6##_type gmock_p6)                                                     \
      : p0(::std::move(gmock_p0)),                                         \
        p1(::std::move(gmock_p1)),                                         \
        p2(::std::move(gmock_p2)),                                         \
        p3(::std::move(gmock_p3)),                                         \
        p4(::std::move(gmock_p4)),                                         \
        p5(::std::move(gmock_p5)),                                         \
        p6(::std::move(gmock_p6))
#define GMOCK_INTERNAL_INIT_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7) \
  (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2,                 \
   p3##_type gmock_p3, p4##_type gmock_p4, p5##_type gmock_p5,                 \
   p6##_type gmock_p6, p7##_type gmock_p7)                                     \
      : p0(::std::move(gmock_p0)),                                             \
        p1(::std::move(gmock_p1)),                                             \
        p2(::std::move(gmock_p2)),                                             \
        p3(::std::move(gmock_p3)),                                             \
        p4(::std::move(gmock_p4)),                                             \
        p5(::std::move(gmock_p5)),                                             \
        p6(::std::move(gmock_p6)),                                             \
Abseil Team's avatar
Abseil Team committed
298
        p7(::std::move(gmock_p7))
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#define GMOCK_INTERNAL_INIT_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7, \
                                               p8)                             \
  (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2,                 \
   p3##_type gmock_p3, p4##_type gmock_p4, p5##_type gmock_p5,                 \
   p6##_type gmock_p6, p7##_type gmock_p7, p8##_type gmock_p8)                 \
      : p0(::std::move(gmock_p0)),                                             \
        p1(::std::move(gmock_p1)),                                             \
        p2(::std::move(gmock_p2)),                                             \
        p3(::std::move(gmock_p3)),                                             \
        p4(::std::move(gmock_p4)),                                             \
        p5(::std::move(gmock_p5)),                                             \
        p6(::std::move(gmock_p6)),                                             \
        p7(::std::move(gmock_p7)),                                             \
        p8(::std::move(gmock_p8))
313
#define GMOCK_INTERNAL_INIT_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
314
315
316
317
318
319
320
321
322
323
324
325
326
327
                                                p7, p8, p9)                 \
  (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2,              \
   p3##_type gmock_p3, p4##_type gmock_p4, p5##_type gmock_p5,              \
   p6##_type gmock_p6, p7##_type gmock_p7, p8##_type gmock_p8,              \
   p9##_type gmock_p9)                                                      \
      : p0(::std::move(gmock_p0)),                                          \
        p1(::std::move(gmock_p1)),                                          \
        p2(::std::move(gmock_p2)),                                          \
        p3(::std::move(gmock_p3)),                                          \
        p4(::std::move(gmock_p4)),                                          \
        p5(::std::move(gmock_p5)),                                          \
        p6(::std::move(gmock_p6)),                                          \
        p7(::std::move(gmock_p7)),                                          \
        p8(::std::move(gmock_p8)),                                          \
Abseil Team's avatar
Abseil Team committed
328
        p9(::std::move(gmock_p9))
329

Abseil Team's avatar
Abseil Team committed
330
331
// Defines the copy constructor
#define GMOCK_INTERNAL_DEFN_COPY_AND_0_VALUE_PARAMS() \
332
  {}  // Avoid https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82134
Abseil Team's avatar
Abseil Team committed
333
334
335
336
337
338
339
340
341
342
343
#define GMOCK_INTERNAL_DEFN_COPY_AND_1_VALUE_PARAMS(...) = default;
#define GMOCK_INTERNAL_DEFN_COPY_AND_2_VALUE_PARAMS(...) = default;
#define GMOCK_INTERNAL_DEFN_COPY_AND_3_VALUE_PARAMS(...) = default;
#define GMOCK_INTERNAL_DEFN_COPY_AND_4_VALUE_PARAMS(...) = default;
#define GMOCK_INTERNAL_DEFN_COPY_AND_5_VALUE_PARAMS(...) = default;
#define GMOCK_INTERNAL_DEFN_COPY_AND_6_VALUE_PARAMS(...) = default;
#define GMOCK_INTERNAL_DEFN_COPY_AND_7_VALUE_PARAMS(...) = default;
#define GMOCK_INTERNAL_DEFN_COPY_AND_8_VALUE_PARAMS(...) = default;
#define GMOCK_INTERNAL_DEFN_COPY_AND_9_VALUE_PARAMS(...) = default;
#define GMOCK_INTERNAL_DEFN_COPY_AND_10_VALUE_PARAMS(...) = default;

344
345
346
// Declares the fields for storing the value parameters.
#define GMOCK_INTERNAL_DEFN_AND_0_VALUE_PARAMS()
#define GMOCK_INTERNAL_DEFN_AND_1_VALUE_PARAMS(p0) p0##_type p0;
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#define GMOCK_INTERNAL_DEFN_AND_2_VALUE_PARAMS(p0, p1) \
  p0##_type p0;                                        \
  p1##_type p1;
#define GMOCK_INTERNAL_DEFN_AND_3_VALUE_PARAMS(p0, p1, p2) \
  p0##_type p0;                                            \
  p1##_type p1;                                            \
  p2##_type p2;
#define GMOCK_INTERNAL_DEFN_AND_4_VALUE_PARAMS(p0, p1, p2, p3) \
  p0##_type p0;                                                \
  p1##_type p1;                                                \
  p2##_type p2;                                                \
  p3##_type p3;
#define GMOCK_INTERNAL_DEFN_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) \
  p0##_type p0;                                                    \
  p1##_type p1;                                                    \
  p2##_type p2;                                                    \
  p3##_type p3;                                                    \
  p4##_type p4;
#define GMOCK_INTERNAL_DEFN_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) \
  p0##_type p0;                                                        \
  p1##_type p1;                                                        \
  p2##_type p2;                                                        \
  p3##_type p3;                                                        \
  p4##_type p4;                                                        \
  p5##_type p5;
#define GMOCK_INTERNAL_DEFN_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6) \
  p0##_type p0;                                                            \
  p1##_type p1;                                                            \
  p2##_type p2;                                                            \
  p3##_type p3;                                                            \
  p4##_type p4;                                                            \
  p5##_type p5;                                                            \
  p6##_type p6;
#define GMOCK_INTERNAL_DEFN_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7) \
  p0##_type p0;                                                                \
  p1##_type p1;                                                                \
  p2##_type p2;                                                                \
  p3##_type p3;                                                                \
  p4##_type p4;                                                                \
  p5##_type p5;                                                                \
  p6##_type p6;                                                                \
  p7##_type p7;
#define GMOCK_INTERNAL_DEFN_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7, \
                                               p8)                             \
  p0##_type p0;                                                                \
  p1##_type p1;                                                                \
  p2##_type p2;                                                                \
  p3##_type p3;                                                                \
  p4##_type p4;                                                                \
  p5##_type p5;                                                                \
  p6##_type p6;                                                                \
  p7##_type p7;                                                                \
  p8##_type p8;
400
#define GMOCK_INTERNAL_DEFN_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
401
402
403
404
405
406
407
408
409
410
411
                                                p7, p8, p9)                 \
  p0##_type p0;                                                             \
  p1##_type p1;                                                             \
  p2##_type p2;                                                             \
  p3##_type p3;                                                             \
  p4##_type p4;                                                             \
  p5##_type p5;                                                             \
  p6##_type p6;                                                             \
  p7##_type p7;                                                             \
  p8##_type p8;                                                             \
  p9##_type p9;
412
413
414
415
416
417
418

// Lists the value parameters.
#define GMOCK_INTERNAL_LIST_AND_0_VALUE_PARAMS()
#define GMOCK_INTERNAL_LIST_AND_1_VALUE_PARAMS(p0) p0
#define GMOCK_INTERNAL_LIST_AND_2_VALUE_PARAMS(p0, p1) p0, p1
#define GMOCK_INTERNAL_LIST_AND_3_VALUE_PARAMS(p0, p1, p2) p0, p1, p2
#define GMOCK_INTERNAL_LIST_AND_4_VALUE_PARAMS(p0, p1, p2, p3) p0, p1, p2, p3
419
420
421
422
423
424
425
426
427
428
429
#define GMOCK_INTERNAL_LIST_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) \
  p0, p1, p2, p3, p4
#define GMOCK_INTERNAL_LIST_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) \
  p0, p1, p2, p3, p4, p5
#define GMOCK_INTERNAL_LIST_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6) \
  p0, p1, p2, p3, p4, p5, p6
#define GMOCK_INTERNAL_LIST_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7) \
  p0, p1, p2, p3, p4, p5, p6, p7
#define GMOCK_INTERNAL_LIST_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7, \
                                               p8)                             \
  p0, p1, p2, p3, p4, p5, p6, p7, p8
430
#define GMOCK_INTERNAL_LIST_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
431
432
                                                p7, p8, p9)                 \
  p0, p1, p2, p3, p4, p5, p6, p7, p8, p9
433
434
435
436

// Lists the value parameter types.
#define GMOCK_INTERNAL_LIST_TYPE_AND_0_VALUE_PARAMS()
#define GMOCK_INTERNAL_LIST_TYPE_AND_1_VALUE_PARAMS(p0) , p0##_type
437
438
439
440
441
442
443
444
445
446
#define GMOCK_INTERNAL_LIST_TYPE_AND_2_VALUE_PARAMS(p0, p1) \
  , p0##_type, p1##_type
#define GMOCK_INTERNAL_LIST_TYPE_AND_3_VALUE_PARAMS(p0, p1, p2) \
  , p0##_type, p1##_type, p2##_type
#define GMOCK_INTERNAL_LIST_TYPE_AND_4_VALUE_PARAMS(p0, p1, p2, p3) \
  , p0##_type, p1##_type, p2##_type, p3##_type
#define GMOCK_INTERNAL_LIST_TYPE_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) \
  , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type
#define GMOCK_INTERNAL_LIST_TYPE_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) \
  , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, p5##_type
447
#define GMOCK_INTERNAL_LIST_TYPE_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
448
449
                                                    p6)                     \
  , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, p5##_type, p6##_type
450
#define GMOCK_INTERNAL_LIST_TYPE_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
451
452
453
                                                    p6, p7)                 \
  , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, p5##_type,       \
      p6##_type, p7##_type
454
#define GMOCK_INTERNAL_LIST_TYPE_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
455
456
457
                                                    p6, p7, p8)             \
  , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, p5##_type,       \
      p6##_type, p7##_type, p8##_type
458
#define GMOCK_INTERNAL_LIST_TYPE_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
459
460
461
                                                     p6, p7, p8, p9)         \
  , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, p5##_type,        \
      p6##_type, p7##_type, p8##_type, p9##_type
462
463
464
465

// Declares the value parameters.
#define GMOCK_INTERNAL_DECL_AND_0_VALUE_PARAMS()
#define GMOCK_INTERNAL_DECL_AND_1_VALUE_PARAMS(p0) p0##_type p0
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#define GMOCK_INTERNAL_DECL_AND_2_VALUE_PARAMS(p0, p1) \
  p0##_type p0, p1##_type p1
#define GMOCK_INTERNAL_DECL_AND_3_VALUE_PARAMS(p0, p1, p2) \
  p0##_type p0, p1##_type p1, p2##_type p2
#define GMOCK_INTERNAL_DECL_AND_4_VALUE_PARAMS(p0, p1, p2, p3) \
  p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3
#define GMOCK_INTERNAL_DECL_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) \
  p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4
#define GMOCK_INTERNAL_DECL_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5)  \
  p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4, \
      p5##_type p5
#define GMOCK_INTERNAL_DECL_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6) \
  p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4,    \
      p5##_type p5, p6##_type p6
#define GMOCK_INTERNAL_DECL_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7) \
  p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4,        \
      p5##_type p5, p6##_type p6, p7##_type p7
#define GMOCK_INTERNAL_DECL_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7, \
                                               p8)                             \
  p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4,        \
      p5##_type p5, p6##_type p6, p7##_type p7, p8##_type p8
487
#define GMOCK_INTERNAL_DECL_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
488
489
490
                                                p7, p8, p9)                 \
  p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4,     \
      p5##_type p5, p6##_type p6, p7##_type p7, p8##_type p8, p9##_type p9
491
492
493
494
495
496
497
498
499
500
501

// The suffix of the class template implementing the action template.
#define GMOCK_INTERNAL_COUNT_AND_0_VALUE_PARAMS()
#define GMOCK_INTERNAL_COUNT_AND_1_VALUE_PARAMS(p0) P
#define GMOCK_INTERNAL_COUNT_AND_2_VALUE_PARAMS(p0, p1) P2
#define GMOCK_INTERNAL_COUNT_AND_3_VALUE_PARAMS(p0, p1, p2) P3
#define GMOCK_INTERNAL_COUNT_AND_4_VALUE_PARAMS(p0, p1, p2, p3) P4
#define GMOCK_INTERNAL_COUNT_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) P5
#define GMOCK_INTERNAL_COUNT_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) P6
#define GMOCK_INTERNAL_COUNT_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6) P7
#define GMOCK_INTERNAL_COUNT_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
502
503
                                                p7)                         \
  P8
504
#define GMOCK_INTERNAL_COUNT_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
505
506
                                                p7, p8)                     \
  P9
507
#define GMOCK_INTERNAL_COUNT_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
508
509
                                                 p7, p8, p9)                 \
  P10
510
511

// The name of the class template implementing the action template.
512
513
#define GMOCK_ACTION_CLASS_(name, value_params) \
  GTEST_CONCAT_TOKEN_(name##Action, GMOCK_INTERNAL_COUNT_##value_params)
514

Abseil Team's avatar
Abseil Team committed
515
516
#define ACTION_TEMPLATE(name, template_params, value_params)                   \
  template <GMOCK_INTERNAL_DECL_##template_params                              \
517
                GMOCK_INTERNAL_DECL_TYPE_##value_params>                       \
Abseil Team's avatar
Abseil Team committed
518
519
520
521
522
  class GMOCK_ACTION_CLASS_(name, value_params) {                              \
   public:                                                                     \
    explicit GMOCK_ACTION_CLASS_(name, value_params)(                          \
        GMOCK_INTERNAL_DECL_##value_params)                                    \
        GMOCK_PP_IF(GMOCK_PP_IS_EMPTY(GMOCK_INTERNAL_COUNT_##value_params),    \
523
524
                    = default;                                                 \
                    ,                                                          \
Abseil Team's avatar
Abseil Team committed
525
                    : impl_(std::make_shared<gmock_Impl>(                      \
526
527
528
                        GMOCK_INTERNAL_LIST_##value_params)){})                \
            GMOCK_ACTION_CLASS_(name, value_params)(const GMOCK_ACTION_CLASS_( \
                name, value_params) &) noexcept GMOCK_INTERNAL_DEFN_COPY_      \
Tom Hughes's avatar
Tom Hughes committed
529
530
531
532
        ##value_params                                                         \
        GMOCK_ACTION_CLASS_(name, value_params)(GMOCK_ACTION_CLASS_(           \
            name, value_params) &&) noexcept GMOCK_INTERNAL_DEFN_COPY_         \
        ##value_params template <typename F>                                   \
533
        operator ::testing::Action<F>() const {                                \
Abseil Team's avatar
Abseil Team committed
534
535
      return GMOCK_PP_IF(                                                      \
          GMOCK_PP_IS_EMPTY(GMOCK_INTERNAL_COUNT_##value_params),              \
536
537
          (::testing::internal::MakeAction<F, gmock_Impl>()),                  \
          (::testing::internal::MakeAction<F>(impl_)));                        \
Abseil Team's avatar
Abseil Team committed
538
    }                                                                          \
539
                                                                               \
Abseil Team's avatar
Abseil Team committed
540
541
542
543
544
545
546
547
548
   private:                                                                    \
    class gmock_Impl {                                                         \
     public:                                                                   \
      explicit gmock_Impl GMOCK_INTERNAL_INIT_##value_params {}                \
      template <typename function_type, typename return_type,                  \
                typename args_type, GMOCK_ACTION_TEMPLATE_ARGS_NAMES_>         \
      return_type gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_) const;  \
      GMOCK_INTERNAL_DEFN_##value_params                                       \
    };                                                                         \
549
550
    GMOCK_PP_IF(GMOCK_PP_IS_EMPTY(GMOCK_INTERNAL_COUNT_##value_params), ,      \
                std::shared_ptr<const gmock_Impl> impl_;)                      \
Abseil Team's avatar
Abseil Team committed
551
552
  };                                                                           \
  template <GMOCK_INTERNAL_DECL_##template_params                              \
553
554
555
556
557
                GMOCK_INTERNAL_DECL_TYPE_##value_params>                       \
  GMOCK_ACTION_CLASS_(                                                         \
      name, value_params)<GMOCK_INTERNAL_LIST_##template_params                \
                              GMOCK_INTERNAL_LIST_TYPE_##value_params>         \
      name(GMOCK_INTERNAL_DECL_##value_params) GTEST_MUST_USE_RESULT_;         \
Abseil Team's avatar
Abseil Team committed
558
  template <GMOCK_INTERNAL_DECL_##template_params                              \
559
560
561
562
563
564
565
566
567
                GMOCK_INTERNAL_DECL_TYPE_##value_params>                       \
  inline GMOCK_ACTION_CLASS_(                                                  \
      name, value_params)<GMOCK_INTERNAL_LIST_##template_params                \
                              GMOCK_INTERNAL_LIST_TYPE_##value_params>         \
  name(GMOCK_INTERNAL_DECL_##value_params) {                                   \
    return GMOCK_ACTION_CLASS_(                                                \
        name, value_params)<GMOCK_INTERNAL_LIST_##template_params              \
                                GMOCK_INTERNAL_LIST_TYPE_##value_params>(      \
        GMOCK_INTERNAL_LIST_##value_params);                                   \
Abseil Team's avatar
Abseil Team committed
568
569
  }                                                                            \
  template <GMOCK_INTERNAL_DECL_##template_params                              \
570
                GMOCK_INTERNAL_DECL_TYPE_##value_params>                       \
Abseil Team's avatar
Abseil Team committed
571
572
  template <typename function_type, typename return_type, typename args_type,  \
            GMOCK_ACTION_TEMPLATE_ARGS_NAMES_>                                 \
573
574
575
576
577
  return_type GMOCK_ACTION_CLASS_(                                             \
      name, value_params)<GMOCK_INTERNAL_LIST_##template_params                \
                              GMOCK_INTERNAL_LIST_TYPE_##value_params>::       \
      gmock_Impl::gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_)  \
          const
578

579
580
namespace testing {

581
582
583
584
585
586
// The ACTION*() macros trigger warning C4100 (unreferenced formal
// parameter) in MSVC with -W4.  Unfortunately they cannot be fixed in
// the macro definition, as the warnings are generated when the macro
// is expanded and macro expansion cannot contain #pragma.  Therefore
// we suppress them here.
#ifdef _MSC_VER
587
588
#pragma warning(push)
#pragma warning(disable : 4100)
589
590
#endif

ofats's avatar
ofats committed
591
592
593
594
595
596
597
598
599
600
601
602
603
namespace internal {

// internal::InvokeArgument - a helper for InvokeArgument action.
// The basic overloads are provided here for generic functors.
// Overloads for other custom-callables are provided in the
// internal/custom/gmock-generated-actions.h header.
template <typename F, typename... Args>
auto InvokeArgument(F f, Args... args) -> decltype(f(args...)) {
  return f(args...);
}

template <std::size_t index, typename... Params>
struct InvokeArgumentAction {
604
605
  template <typename... Args,
            typename = typename std::enable_if<(index < sizeof...(Args))>::type>
Tom Hughes's avatar
Tom Hughes committed
606
  auto operator()(Args &&...args) const -> decltype(internal::InvokeArgument(
ofats's avatar
ofats committed
607
      std::get<index>(std::forward_as_tuple(std::forward<Args>(args)...)),
Tom Hughes's avatar
Tom Hughes committed
608
609
610
611
612
      std::declval<const Params &>()...)) {
    internal::FlatTuple<Args &&...> args_tuple(FlatTupleConstructTag{},
                                               std::forward<Args>(args)...);
    return params.Apply([&](const Params &...unpacked_params) {
      auto &&callable = args_tuple.template Get<index>();
ofats's avatar
ofats committed
613
614
615
616
617
618
619
620
621
622
      return internal::InvokeArgument(
          std::forward<decltype(callable)>(callable), unpacked_params...);
    });
  }

  internal::FlatTuple<Params...> params;
};

}  // namespace internal

623
624
625
626
627
628
629
// The InvokeArgument<N>(a1, a2, ..., a_k) action invokes the N-th
// (0-based) argument, which must be a k-ary callable, of the mock
// function, with arguments a1, a2, ..., a_k.
//
// Notes:
//
//   1. The arguments are passed by value by default.  If you need to
ofats's avatar
ofats committed
630
//   pass an argument by reference, wrap it inside std::ref().  For
631
632
//   example,
//
ofats's avatar
ofats committed
633
//     InvokeArgument<1>(5, string("Hello"), std::ref(foo))
634
635
636
637
//
//   passes 5 and string("Hello") by value, and passes foo by
//   reference.
//
ofats's avatar
ofats committed
638
//   2. If the callable takes an argument by reference but std::ref() is
639
640
641
642
643
644
645
646
647
648
649
//   not used, it will receive the reference to a copy of the value,
//   instead of the original value.  For example, when the 0-th
//   argument of the mock function takes a const string&, the action
//
//     InvokeArgument<0>(string("Hello"))
//
//   makes a copy of the temporary string("Hello") object and passes a
//   reference of the copy, instead of the original temporary object,
//   to the callable.  This makes it easy for a user to define an
//   InvokeArgument action from temporary values and have it performed
//   later.
ofats's avatar
ofats committed
650
651
template <std::size_t index, typename... Params>
internal::InvokeArgumentAction<index, typename std::decay<Params>::type...>
Tom Hughes's avatar
Tom Hughes committed
652
InvokeArgument(Params &&...params) {
ofats's avatar
ofats committed
653
  return {internal::FlatTuple<typename std::decay<Params>::type...>(
Abseil Team's avatar
Abseil Team committed
654
      internal::FlatTupleConstructTag{}, std::forward<Params>(params)...)};
655
}
656

657
#ifdef _MSC_VER
658
#pragma warning(pop)
659
660
#endif

661
662
}  // namespace testing

Abseil Team's avatar
Abseil Team committed
663
#endif  // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_