1 $$ -*- mode: c++; -*- 2 $$ This is a Pump source file. Please use Pump to convert it to 3 $$ gmock-generated-actions.h. 4 $$ 5 $var n = 10 $$ The maximum arity we support. 6 $$}} This meta comment fixes auto-indentation in editors. 7 // Copyright 2007, Google Inc. 8 // All rights reserved. 9 // 10 // Redistribution and use in source and binary forms, with or without 11 // modification, are permitted provided that the following conditions are 12 // met: 13 // 14 // * Redistributions of source code must retain the above copyright 15 // notice, this list of conditions and the following disclaimer. 16 // * Redistributions in binary form must reproduce the above 17 // copyright notice, this list of conditions and the following disclaimer 18 // in the documentation and/or other materials provided with the 19 // distribution. 20 // * Neither the name of Google Inc. nor the names of its 21 // contributors may be used to endorse or promote products derived from 22 // this software without specific prior written permission. 23 // 24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 28 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 29 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 30 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 34 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 // 36 // Author: wan (a] google.com (Zhanyong Wan) 37 38 // Google Mock - a framework for writing C++ mock classes. 39 // 40 // This file implements some commonly used variadic actions. 41 42 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_ 43 #define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_ 44 45 #include "gmock/gmock-actions.h" 46 #include "gmock/internal/gmock-port.h" 47 48 namespace testing { 49 namespace internal { 50 51 // InvokeHelper<F> knows how to unpack an N-tuple and invoke an N-ary 52 // function or method with the unpacked values, where F is a function 53 // type that takes N arguments. 54 template <typename Result, typename ArgumentTuple> 55 class InvokeHelper; 56 57 58 $range i 0..n 59 $for i [[ 60 $range j 1..i 61 $var types = [[$for j [[, typename A$j]]]] 62 $var as = [[$for j, [[A$j]]]] 63 $var args = [[$if i==0 [[]] $else [[ args]]]] 64 $var gets = [[$for j, [[get<$(j - 1)>(args)]]]] 65 template <typename R$types> 66 class InvokeHelper<R, ::testing::tuple<$as> > { 67 public: 68 template <typename Function> 69 static R Invoke(Function function, const ::testing::tuple<$as>&$args) { 70 return function($gets); 71 } 72 73 template <class Class, typename MethodPtr> 74 static R InvokeMethod(Class* obj_ptr, 75 MethodPtr method_ptr, 76 const ::testing::tuple<$as>&$args) { 77 return (obj_ptr->*method_ptr)($gets); 78 } 79 }; 80 81 82 ]] 83 // An INTERNAL macro for extracting the type of a tuple field. It's 84 // subject to change without notice - DO NOT USE IN USER CODE! 85 #define GMOCK_FIELD_(Tuple, N) \ 86 typename ::testing::tuple_element<N, Tuple>::type 87 88 $range i 1..n 89 90 // SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::type is the 91 // type of an n-ary function whose i-th (1-based) argument type is the 92 // k{i}-th (0-based) field of ArgumentTuple, which must be a tuple 93 // type, and whose return type is Result. For example, 94 // SelectArgs<int, ::testing::tuple<bool, char, double, long>, 0, 3>::type 95 // is int(bool, long). 96 // 97 // SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::Select(args) 98 // returns the selected fields (k1, k2, ..., k_n) of args as a tuple. 99 // For example, 100 // SelectArgs<int, tuple<bool, char, double>, 2, 0>::Select( 101 // ::testing::make_tuple(true, 'a', 2.5)) 102 // returns tuple (2.5, true). 103 // 104 // The numbers in list k1, k2, ..., k_n must be >= 0, where n can be 105 // in the range [0, $n]. Duplicates are allowed and they don't have 106 // to be in an ascending or descending order. 107 108 template <typename Result, typename ArgumentTuple, $for i, [[int k$i]]> 109 class SelectArgs { 110 public: 111 typedef Result type($for i, [[GMOCK_FIELD_(ArgumentTuple, k$i)]]); 112 typedef typename Function<type>::ArgumentTuple SelectedArgs; 113 static SelectedArgs Select(const ArgumentTuple& args) { 114 return SelectedArgs($for i, [[get<k$i>(args)]]); 115 } 116 }; 117 118 119 $for i [[ 120 $range j 1..n 121 $range j1 1..i-1 122 template <typename Result, typename ArgumentTuple$for j1[[, int k$j1]]> 123 class SelectArgs<Result, ArgumentTuple, 124 $for j, [[$if j <= i-1 [[k$j]] $else [[-1]]]]> { 125 public: 126 typedef Result type($for j1, [[GMOCK_FIELD_(ArgumentTuple, k$j1)]]); 127 typedef typename Function<type>::ArgumentTuple SelectedArgs; 128 static SelectedArgs Select(const ArgumentTuple& [[]] 129 $if i == 1 [[/* args */]] $else [[args]]) { 130 return SelectedArgs($for j1, [[get<k$j1>(args)]]); 131 } 132 }; 133 134 135 ]] 136 #undef GMOCK_FIELD_ 137 138 $var ks = [[$for i, [[k$i]]]] 139 140 // Implements the WithArgs action. 141 template <typename InnerAction, $for i, [[int k$i = -1]]> 142 class WithArgsAction { 143 public: 144 explicit WithArgsAction(const InnerAction& action) : action_(action) {} 145 146 template <typename F> 147 operator Action<F>() const { return MakeAction(new Impl<F>(action_)); } 148 149 private: 150 template <typename F> 151 class Impl : public ActionInterface<F> { 152 public: 153 typedef typename Function<F>::Result Result; 154 typedef typename Function<F>::ArgumentTuple ArgumentTuple; 155 156 explicit Impl(const InnerAction& action) : action_(action) {} 157 158 virtual Result Perform(const ArgumentTuple& args) { 159 return action_.Perform(SelectArgs<Result, ArgumentTuple, $ks>::Select(args)); 160 } 161 162 private: 163 typedef typename SelectArgs<Result, ArgumentTuple, 164 $ks>::type InnerFunctionType; 165 166 Action<InnerFunctionType> action_; 167 }; 168 169 const InnerAction action_; 170 171 GTEST_DISALLOW_ASSIGN_(WithArgsAction); 172 }; 173 174 // A macro from the ACTION* family (defined later in this file) 175 // defines an action that can be used in a mock function. Typically, 176 // these actions only care about a subset of the arguments of the mock 177 // function. For example, if such an action only uses the second 178 // argument, it can be used in any mock function that takes >= 2 179 // arguments where the type of the second argument is compatible. 180 // 181 // Therefore, the action implementation must be prepared to take more 182 // arguments than it needs. The ExcessiveArg type is used to 183 // represent those excessive arguments. In order to keep the compiler 184 // error messages tractable, we define it in the testing namespace 185 // instead of testing::internal. However, this is an INTERNAL TYPE 186 // and subject to change without notice, so a user MUST NOT USE THIS 187 // TYPE DIRECTLY. 188 struct ExcessiveArg {}; 189 190 // A helper class needed for implementing the ACTION* macros. 191 template <typename Result, class Impl> 192 class ActionHelper { 193 public: 194 $range i 0..n 195 $for i 196 197 [[ 198 $var template = [[$if i==0 [[]] $else [[ 199 $range j 0..i-1 200 template <$for j, [[typename A$j]]> 201 ]]]] 202 $range j 0..i-1 203 $var As = [[$for j, [[A$j]]]] 204 $var as = [[$for j, [[get<$j>(args)]]]] 205 $range k 1..n-i 206 $var eas = [[$for k, [[ExcessiveArg()]]]] 207 $var arg_list = [[$if (i==0) | (i==n) [[$as$eas]] $else [[$as, $eas]]]] 208 $template 209 static Result Perform(Impl* impl, const ::testing::tuple<$As>& args) { 210 return impl->template gmock_PerformImpl<$As>(args, $arg_list); 211 } 212 213 ]] 214 }; 215 216 } // namespace internal 217 218 // Various overloads for Invoke(). 219 220 // WithArgs<N1, N2, ..., Nk>(an_action) creates an action that passes 221 // the selected arguments of the mock function to an_action and 222 // performs it. It serves as an adaptor between actions with 223 // different argument lists. C++ doesn't support default arguments for 224 // function templates, so we have to overload it. 225 226 $range i 1..n 227 $for i [[ 228 $range j 1..i 229 template <$for j [[int k$j, ]]typename InnerAction> 230 inline internal::WithArgsAction<InnerAction$for j [[, k$j]]> 231 WithArgs(const InnerAction& action) { 232 return internal::WithArgsAction<InnerAction$for j [[, k$j]]>(action); 233 } 234 235 236 ]] 237 // Creates an action that does actions a1, a2, ..., sequentially in 238 // each invocation. 239 $range i 2..n 240 $for i [[ 241 $range j 2..i 242 $var types = [[$for j, [[typename Action$j]]]] 243 $var Aas = [[$for j [[, Action$j a$j]]]] 244 245 template <typename Action1, $types> 246 $range k 1..i-1 247 248 inline $for k [[internal::DoBothAction<Action$k, ]]Action$i$for k [[>]] 249 250 DoAll(Action1 a1$Aas) { 251 $if i==2 [[ 252 253 return internal::DoBothAction<Action1, Action2>(a1, a2); 254 ]] $else [[ 255 $range j2 2..i 256 257 return DoAll(a1, DoAll($for j2, [[a$j2]])); 258 ]] 259 260 } 261 262 ]] 263 264 } // namespace testing 265 266 // The ACTION* family of macros can be used in a namespace scope to 267 // define custom actions easily. The syntax: 268 // 269 // ACTION(name) { statements; } 270 // 271 // will define an action with the given name that executes the 272 // statements. The value returned by the statements will be used as 273 // the return value of the action. Inside the statements, you can 274 // refer to the K-th (0-based) argument of the mock function by 275 // 'argK', and refer to its type by 'argK_type'. For example: 276 // 277 // ACTION(IncrementArg1) { 278 // arg1_type temp = arg1; 279 // return ++(*temp); 280 // } 281 // 282 // allows you to write 283 // 284 // ...WillOnce(IncrementArg1()); 285 // 286 // You can also refer to the entire argument tuple and its type by 287 // 'args' and 'args_type', and refer to the mock function type and its 288 // return type by 'function_type' and 'return_type'. 289 // 290 // Note that you don't need to specify the types of the mock function 291 // arguments. However rest assured that your code is still type-safe: 292 // you'll get a compiler error if *arg1 doesn't support the ++ 293 // operator, or if the type of ++(*arg1) isn't compatible with the 294 // mock function's return type, for example. 295 // 296 // Sometimes you'll want to parameterize the action. For that you can use 297 // another macro: 298 // 299 // ACTION_P(name, param_name) { statements; } 300 // 301 // For example: 302 // 303 // ACTION_P(Add, n) { return arg0 + n; } 304 // 305 // will allow you to write: 306 // 307 // ...WillOnce(Add(5)); 308 // 309 // Note that you don't need to provide the type of the parameter 310 // either. If you need to reference the type of a parameter named 311 // 'foo', you can write 'foo_type'. For example, in the body of 312 // ACTION_P(Add, n) above, you can write 'n_type' to refer to the type 313 // of 'n'. 314 // 315 // We also provide ACTION_P2, ACTION_P3, ..., up to ACTION_P$n to support 316 // multi-parameter actions. 317 // 318 // For the purpose of typing, you can view 319 // 320 // ACTION_Pk(Foo, p1, ..., pk) { ... } 321 // 322 // as shorthand for 323 // 324 // template <typename p1_type, ..., typename pk_type> 325 // FooActionPk<p1_type, ..., pk_type> Foo(p1_type p1, ..., pk_type pk) { ... } 326 // 327 // In particular, you can provide the template type arguments 328 // explicitly when invoking Foo(), as in Foo<long, bool>(5, false); 329 // although usually you can rely on the compiler to infer the types 330 // for you automatically. You can assign the result of expression 331 // Foo(p1, ..., pk) to a variable of type FooActionPk<p1_type, ..., 332 // pk_type>. This can be useful when composing actions. 333 // 334 // You can also overload actions with different numbers of parameters: 335 // 336 // ACTION_P(Plus, a) { ... } 337 // ACTION_P2(Plus, a, b) { ... } 338 // 339 // While it's tempting to always use the ACTION* macros when defining 340 // a new action, you should also consider implementing ActionInterface 341 // or using MakePolymorphicAction() instead, especially if you need to 342 // use the action a lot. While these approaches require more work, 343 // they give you more control on the types of the mock function 344 // arguments and the action parameters, which in general leads to 345 // better compiler error messages that pay off in the long run. They 346 // also allow overloading actions based on parameter types (as opposed 347 // to just based on the number of parameters). 348 // 349 // CAVEAT: 350 // 351 // ACTION*() can only be used in a namespace scope. The reason is 352 // that C++ doesn't yet allow function-local types to be used to 353 // instantiate templates. The up-coming C++0x standard will fix this. 354 // Once that's done, we'll consider supporting using ACTION*() inside 355 // a function. 356 // 357 // MORE INFORMATION: 358 // 359 // To learn more about using these macros, please search for 'ACTION' 360 // on http://code.google.com/p/googlemock/wiki/CookBook. 361 362 $range i 0..n 363 $range k 0..n-1 364 365 // An internal macro needed for implementing ACTION*(). 366 #define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_\ 367 const args_type& args GTEST_ATTRIBUTE_UNUSED_ 368 $for k [[, \ 369 arg$k[[]]_type arg$k GTEST_ATTRIBUTE_UNUSED_]] 370 371 372 // Sometimes you want to give an action explicit template parameters 373 // that cannot be inferred from its value parameters. ACTION() and 374 // ACTION_P*() don't support that. ACTION_TEMPLATE() remedies that 375 // and can be viewed as an extension to ACTION() and ACTION_P*(). 376 // 377 // The syntax: 378 // 379 // ACTION_TEMPLATE(ActionName, 380 // HAS_m_TEMPLATE_PARAMS(kind1, name1, ..., kind_m, name_m), 381 // AND_n_VALUE_PARAMS(p1, ..., p_n)) { statements; } 382 // 383 // defines an action template that takes m explicit template 384 // parameters and n value parameters. name_i is the name of the i-th 385 // template parameter, and kind_i specifies whether it's a typename, 386 // an integral constant, or a template. p_i is the name of the i-th 387 // value parameter. 388 // 389 // Example: 390 // 391 // // DuplicateArg<k, T>(output) converts the k-th argument of the mock 392 // // function to type T and copies it to *output. 393 // ACTION_TEMPLATE(DuplicateArg, 394 // HAS_2_TEMPLATE_PARAMS(int, k, typename, T), 395 // AND_1_VALUE_PARAMS(output)) { 396 // *output = T(::testing::get<k>(args)); 397 // } 398 // ... 399 // int n; 400 // EXPECT_CALL(mock, Foo(_, _)) 401 // .WillOnce(DuplicateArg<1, unsigned char>(&n)); 402 // 403 // To create an instance of an action template, write: 404 // 405 // ActionName<t1, ..., t_m>(v1, ..., v_n) 406 // 407 // where the ts are the template arguments and the vs are the value 408 // arguments. The value argument types are inferred by the compiler. 409 // If you want to explicitly specify the value argument types, you can 410 // provide additional template arguments: 411 // 412 // ActionName<t1, ..., t_m, u1, ..., u_k>(v1, ..., v_n) 413 // 414 // where u_i is the desired type of v_i. 415 // 416 // ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded on the 417 // number of value parameters, but not on the number of template 418 // parameters. Without the restriction, the meaning of the following 419 // is unclear: 420 // 421 // OverloadedAction<int, bool>(x); 422 // 423 // Are we using a single-template-parameter action where 'bool' refers 424 // to the type of x, or are we using a two-template-parameter action 425 // where the compiler is asked to infer the type of x? 426 // 427 // Implementation notes: 428 // 429 // GMOCK_INTERNAL_*_HAS_m_TEMPLATE_PARAMS and 430 // GMOCK_INTERNAL_*_AND_n_VALUE_PARAMS are internal macros for 431 // implementing ACTION_TEMPLATE. The main trick we use is to create 432 // new macro invocations when expanding a macro. For example, we have 433 // 434 // #define ACTION_TEMPLATE(name, template_params, value_params) 435 // ... GMOCK_INTERNAL_DECL_##template_params ... 436 // 437 // which causes ACTION_TEMPLATE(..., HAS_1_TEMPLATE_PARAMS(typename, T), ...) 438 // to expand to 439 // 440 // ... GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS(typename, T) ... 441 // 442 // Since GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS is a macro, the 443 // preprocessor will continue to expand it to 444 // 445 // ... typename T ... 446 // 447 // This technique conforms to the C++ standard and is portable. It 448 // allows us to implement action templates using O(N) code, where N is 449 // the maximum number of template/value parameters supported. Without 450 // using it, we'd have to devote O(N^2) amount of code to implement all 451 // combinations of m and n. 452 453 // Declares the template parameters. 454 455 $range j 1..n 456 $for j [[ 457 $range m 0..j-1 458 #define GMOCK_INTERNAL_DECL_HAS_$j[[]] 459 _TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[kind$m name$m]] 460 461 462 ]] 463 464 // Lists the template parameters. 465 466 $for j [[ 467 $range m 0..j-1 468 #define GMOCK_INTERNAL_LIST_HAS_$j[[]] 469 _TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[name$m]] 470 471 472 ]] 473 474 // Declares the types of value parameters. 475 476 $for i [[ 477 $range j 0..i-1 478 #define GMOCK_INTERNAL_DECL_TYPE_AND_$i[[]] 479 _VALUE_PARAMS($for j, [[p$j]]) $for j [[, typename p$j##_type]] 480 481 482 ]] 483 484 // Initializes the value parameters. 485 486 $for i [[ 487 $range j 0..i-1 488 #define GMOCK_INTERNAL_INIT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])\ 489 ($for j, [[p$j##_type gmock_p$j]])$if i>0 [[ : ]]$for j, [[p$j(gmock_p$j)]] 490 491 492 ]] 493 494 // Declares the fields for storing the value parameters. 495 496 $for i [[ 497 $range j 0..i-1 498 #define GMOCK_INTERNAL_DEFN_AND_$i[[]] 499 _VALUE_PARAMS($for j, [[p$j]]) $for j [[p$j##_type p$j; ]] 500 501 502 ]] 503 504 // Lists the value parameters. 505 506 $for i [[ 507 $range j 0..i-1 508 #define GMOCK_INTERNAL_LIST_AND_$i[[]] 509 _VALUE_PARAMS($for j, [[p$j]]) $for j, [[p$j]] 510 511 512 ]] 513 514 // Lists the value parameter types. 515 516 $for i [[ 517 $range j 0..i-1 518 #define GMOCK_INTERNAL_LIST_TYPE_AND_$i[[]] 519 _VALUE_PARAMS($for j, [[p$j]]) $for j [[, p$j##_type]] 520 521 522 ]] 523 524 // Declares the value parameters. 525 526 $for i [[ 527 $range j 0..i-1 528 #define GMOCK_INTERNAL_DECL_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]] 529 $for j, [[p$j##_type p$j]] 530 531 532 ]] 533 534 // The suffix of the class template implementing the action template. 535 $for i [[ 536 537 538 $range j 0..i-1 539 #define GMOCK_INTERNAL_COUNT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]] 540 $if i==1 [[P]] $elif i>=2 [[P$i]] 541 ]] 542 543 544 // The name of the class template implementing the action template. 545 #define GMOCK_ACTION_CLASS_(name, value_params)\ 546 GTEST_CONCAT_TOKEN_(name##Action, GMOCK_INTERNAL_COUNT_##value_params) 547 548 $range k 0..n-1 549 550 #define ACTION_TEMPLATE(name, template_params, value_params)\ 551 template <GMOCK_INTERNAL_DECL_##template_params\ 552 GMOCK_INTERNAL_DECL_TYPE_##value_params>\ 553 class GMOCK_ACTION_CLASS_(name, value_params) {\ 554 public:\ 555 explicit GMOCK_ACTION_CLASS_(name, value_params)\ 556 GMOCK_INTERNAL_INIT_##value_params {}\ 557 template <typename F>\ 558 class gmock_Impl : public ::testing::ActionInterface<F> {\ 559 public:\ 560 typedef F function_type;\ 561 typedef typename ::testing::internal::Function<F>::Result return_type;\ 562 typedef typename ::testing::internal::Function<F>::ArgumentTuple\ 563 args_type;\ 564 explicit gmock_Impl GMOCK_INTERNAL_INIT_##value_params {}\ 565 virtual return_type Perform(const args_type& args) {\ 566 return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\ 567 Perform(this, args);\ 568 }\ 569 template <$for k, [[typename arg$k[[]]_type]]>\ 570 return_type gmock_PerformImpl(const args_type& args[[]] 571 $for k [[, arg$k[[]]_type arg$k]]) const;\ 572 GMOCK_INTERNAL_DEFN_##value_params\ 573 private:\ 574 GTEST_DISALLOW_ASSIGN_(gmock_Impl);\ 575 };\ 576 template <typename F> operator ::testing::Action<F>() const {\ 577 return ::testing::Action<F>(\ 578 new gmock_Impl<F>(GMOCK_INTERNAL_LIST_##value_params));\ 579 }\ 580 GMOCK_INTERNAL_DEFN_##value_params\ 581 private:\ 582 GTEST_DISALLOW_ASSIGN_(GMOCK_ACTION_CLASS_(name, value_params));\ 583 };\ 584 template <GMOCK_INTERNAL_DECL_##template_params\ 585 GMOCK_INTERNAL_DECL_TYPE_##value_params>\ 586 inline GMOCK_ACTION_CLASS_(name, value_params)<\ 587 GMOCK_INTERNAL_LIST_##template_params\ 588 GMOCK_INTERNAL_LIST_TYPE_##value_params> name(\ 589 GMOCK_INTERNAL_DECL_##value_params) {\ 590 return GMOCK_ACTION_CLASS_(name, value_params)<\ 591 GMOCK_INTERNAL_LIST_##template_params\ 592 GMOCK_INTERNAL_LIST_TYPE_##value_params>(\ 593 GMOCK_INTERNAL_LIST_##value_params);\ 594 }\ 595 template <GMOCK_INTERNAL_DECL_##template_params\ 596 GMOCK_INTERNAL_DECL_TYPE_##value_params>\ 597 template <typename F>\ 598 template <typename arg0_type, typename arg1_type, typename arg2_type, \ 599 typename arg3_type, typename arg4_type, typename arg5_type, \ 600 typename arg6_type, typename arg7_type, typename arg8_type, \ 601 typename arg9_type>\ 602 typename ::testing::internal::Function<F>::Result\ 603 GMOCK_ACTION_CLASS_(name, value_params)<\ 604 GMOCK_INTERNAL_LIST_##template_params\ 605 GMOCK_INTERNAL_LIST_TYPE_##value_params>::gmock_Impl<F>::\ 606 gmock_PerformImpl(\ 607 GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const 608 609 $for i 610 611 [[ 612 $var template = [[$if i==0 [[]] $else [[ 613 $range j 0..i-1 614 615 template <$for j, [[typename p$j##_type]]>\ 616 ]]]] 617 $var class_name = [[name##Action[[$if i==0 [[]] $elif i==1 [[P]] 618 $else [[P$i]]]]]] 619 $range j 0..i-1 620 $var ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]] 621 $var param_types_and_names = [[$for j, [[p$j##_type p$j]]]] 622 $var inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(gmock_p$j)]]]]]] 623 $var param_field_decls = [[$for j 624 [[ 625 626 p$j##_type p$j;\ 627 ]]]] 628 $var param_field_decls2 = [[$for j 629 [[ 630 631 p$j##_type p$j;\ 632 ]]]] 633 $var params = [[$for j, [[p$j]]]] 634 $var param_types = [[$if i==0 [[]] $else [[<$for j, [[p$j##_type]]>]]]] 635 $var typename_arg_types = [[$for k, [[typename arg$k[[]]_type]]]] 636 $var arg_types_and_names = [[$for k, [[arg$k[[]]_type arg$k]]]] 637 $var macro_name = [[$if i==0 [[ACTION]] $elif i==1 [[ACTION_P]] 638 $else [[ACTION_P$i]]]] 639 640 #define $macro_name(name$for j [[, p$j]])\$template 641 class $class_name {\ 642 public:\ 643 [[$if i==1 [[explicit ]]]]$class_name($ctor_param_list)$inits {}\ 644 template <typename F>\ 645 class gmock_Impl : public ::testing::ActionInterface<F> {\ 646 public:\ 647 typedef F function_type;\ 648 typedef typename ::testing::internal::Function<F>::Result return_type;\ 649 typedef typename ::testing::internal::Function<F>::ArgumentTuple\ 650 args_type;\ 651 [[$if i==1 [[explicit ]]]]gmock_Impl($ctor_param_list)$inits {}\ 652 virtual return_type Perform(const args_type& args) {\ 653 return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\ 654 Perform(this, args);\ 655 }\ 656 template <$typename_arg_types>\ 657 return_type gmock_PerformImpl(const args_type& args, [[]] 658 $arg_types_and_names) const;\$param_field_decls 659 private:\ 660 GTEST_DISALLOW_ASSIGN_(gmock_Impl);\ 661 };\ 662 template <typename F> operator ::testing::Action<F>() const {\ 663 return ::testing::Action<F>(new gmock_Impl<F>($params));\ 664 }\$param_field_decls2 665 private:\ 666 GTEST_DISALLOW_ASSIGN_($class_name);\ 667 };\$template 668 inline $class_name$param_types name($param_types_and_names) {\ 669 return $class_name$param_types($params);\ 670 }\$template 671 template <typename F>\ 672 template <$typename_arg_types>\ 673 typename ::testing::internal::Function<F>::Result\ 674 $class_name$param_types::gmock_Impl<F>::gmock_PerformImpl(\ 675 GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const 676 ]] 677 $$ } // This meta comment fixes auto-indentation in Emacs. It won't 678 $$ // show up in the generated code. 679 680 681 namespace testing { 682 683 684 // The ACTION*() macros trigger warning C4100 (unreferenced formal 685 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in 686 // the macro definition, as the warnings are generated when the macro 687 // is expanded and macro expansion cannot contain #pragma. Therefore 688 // we suppress them here. 689 #ifdef _MSC_VER 690 # pragma warning(push) 691 # pragma warning(disable:4100) 692 #endif 693 694 // Various overloads for InvokeArgument<N>(). 695 // 696 // The InvokeArgument<N>(a1, a2, ..., a_k) action invokes the N-th 697 // (0-based) argument, which must be a k-ary callable, of the mock 698 // function, with arguments a1, a2, ..., a_k. 699 // 700 // Notes: 701 // 702 // 1. The arguments are passed by value by default. If you need to 703 // pass an argument by reference, wrap it inside ByRef(). For 704 // example, 705 // 706 // InvokeArgument<1>(5, string("Hello"), ByRef(foo)) 707 // 708 // passes 5 and string("Hello") by value, and passes foo by 709 // reference. 710 // 711 // 2. If the callable takes an argument by reference but ByRef() is 712 // not used, it will receive the reference to a copy of the value, 713 // instead of the original value. For example, when the 0-th 714 // argument of the mock function takes a const string&, the action 715 // 716 // InvokeArgument<0>(string("Hello")) 717 // 718 // makes a copy of the temporary string("Hello") object and passes a 719 // reference of the copy, instead of the original temporary object, 720 // to the callable. This makes it easy for a user to define an 721 // InvokeArgument action from temporary values and have it performed 722 // later. 723 724 namespace internal { 725 namespace invoke_argument { 726 727 // Appears in InvokeArgumentAdl's argument list to help avoid 728 // accidental calls to user functions of the same name. 729 struct AdlTag {}; 730 731 // InvokeArgumentAdl - a helper for InvokeArgument. 732 // The basic overloads are provided here for generic functors. 733 // Overloads for other custom-callables are provided in the 734 // internal/custom/callback-actions.h header. 735 736 $range i 0..n 737 $for i 738 [[ 739 $range j 1..i 740 741 template <typename R, typename F[[$for j [[, typename A$j]]]]> 742 R InvokeArgumentAdl(AdlTag, F f[[$for j [[, A$j a$j]]]]) { 743 return f([[$for j, [[a$j]]]]); 744 } 745 ]] 746 747 } // namespace invoke_argument 748 } // namespace internal 749 750 $range i 0..n 751 $for i [[ 752 $range j 0..i-1 753 754 ACTION_TEMPLATE(InvokeArgument, 755 HAS_1_TEMPLATE_PARAMS(int, k), 756 AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])) { 757 using internal::invoke_argument::InvokeArgumentAdl; 758 return InvokeArgumentAdl<return_type>( 759 internal::invoke_argument::AdlTag(), 760 ::testing::get<k>(args)$for j [[, p$j]]); 761 } 762 763 ]] 764 765 // Various overloads for ReturnNew<T>(). 766 // 767 // The ReturnNew<T>(a1, a2, ..., a_k) action returns a pointer to a new 768 // instance of type T, constructed on the heap with constructor arguments 769 // a1, a2, ..., and a_k. The caller assumes ownership of the returned value. 770 $range i 0..n 771 $for i [[ 772 $range j 0..i-1 773 $var ps = [[$for j, [[p$j]]]] 774 775 ACTION_TEMPLATE(ReturnNew, 776 HAS_1_TEMPLATE_PARAMS(typename, T), 777 AND_$i[[]]_VALUE_PARAMS($ps)) { 778 return new T($ps); 779 } 780 781 ]] 782 783 #ifdef _MSC_VER 784 # pragma warning(pop) 785 #endif 786 787 } // namespace testing 788 789 // Include any custom callback actions added by the local installation. 790 // We must include this header at the end to make sure it can use the 791 // declarations from this file. 792 #include "gmock/internal/custom/gmock-generated-actions.h" 793 794 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_ 795