shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1 | $$ -*- mode: c++; -*- |
| 2 | $$ This is a Pump source file. Please use Pump to convert it to |
| 3 | $$ gmock-generated-variadic-actions.h. |
| 4 | $$ |
| 5 | $var n = 10 $$ The maximum arity we support. |
| 6 | // Copyright 2007, Google Inc. |
| 7 | // All rights reserved. |
| 8 | // |
| 9 | // Redistribution and use in source and binary forms, with or without |
| 10 | // modification, are permitted provided that the following conditions are |
| 11 | // met: |
| 12 | // |
| 13 | // * Redistributions of source code must retain the above copyright |
| 14 | // notice, this list of conditions and the following disclaimer. |
| 15 | // * Redistributions in binary form must reproduce the above |
| 16 | // copyright notice, this list of conditions and the following disclaimer |
| 17 | // in the documentation and/or other materials provided with the |
| 18 | // distribution. |
| 19 | // * Neither the name of Google Inc. nor the names of its |
| 20 | // contributors may be used to endorse or promote products derived from |
| 21 | // this software without specific prior written permission. |
| 22 | // |
| 23 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 24 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 25 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 26 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 27 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 28 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 29 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 30 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 31 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 33 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | // |
| 35 | // Author: wan@google.com (Zhanyong Wan) |
| 36 | |
| 37 | // Google Mock - a framework for writing C++ mock classes. |
| 38 | // |
| 39 | // This file implements some commonly used variadic actions. |
| 40 | |
| 41 | #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_ |
| 42 | #define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_ |
| 43 | |
| 44 | #include <gmock/gmock-actions.h> |
| 45 | #include <gmock/internal/gmock-port.h> |
| 46 | |
| 47 | namespace testing { |
| 48 | namespace internal { |
| 49 | |
| 50 | // InvokeHelper<F> knows how to unpack an N-tuple and invoke an N-ary |
| 51 | // function or method with the unpacked values, where F is a function |
| 52 | // type that takes N arguments. |
| 53 | template <typename Result, typename ArgumentTuple> |
| 54 | class InvokeHelper; |
| 55 | |
| 56 | |
| 57 | $range i 0..n |
| 58 | $for i [[ |
| 59 | $range j 1..i |
| 60 | $var types = [[$for j [[, typename A$j]]]] |
| 61 | $var as = [[$for j, [[A$j]]]] |
| 62 | $var args = [[$if i==0 [[]] $else [[ args]]]] |
| 63 | $var import = [[$if i==0 [[]] $else [[ |
| 64 | using ::std::tr1::get; |
| 65 | |
| 66 | ]]]] |
| 67 | $var gets = [[$for j, [[get<$(j - 1)>(args)]]]] |
| 68 | template <typename R$types> |
| 69 | class InvokeHelper<R, ::std::tr1::tuple<$as> > { |
| 70 | public: |
| 71 | template <typename Function> |
| 72 | static R Invoke(Function function, const ::std::tr1::tuple<$as>&$args) { |
| 73 | $import return function($gets); |
| 74 | } |
| 75 | |
| 76 | template <class Class, typename MethodPtr> |
| 77 | static R InvokeMethod(Class* obj_ptr, |
| 78 | MethodPtr method_ptr, |
| 79 | const ::std::tr1::tuple<$as>&$args) { |
| 80 | $import return (obj_ptr->*method_ptr)($gets); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | |
| 85 | ]] |
| 86 | |
| 87 | // Implements the Invoke(f) action. The template argument |
| 88 | // FunctionImpl is the implementation type of f, which can be either a |
| 89 | // function pointer or a functor. Invoke(f) can be used as an |
| 90 | // Action<F> as long as f's type is compatible with F (i.e. f can be |
| 91 | // assigned to a tr1::function<F>). |
| 92 | template <typename FunctionImpl> |
| 93 | class InvokeAction { |
| 94 | public: |
| 95 | // The c'tor makes a copy of function_impl (either a function |
| 96 | // pointer or a functor). |
| 97 | explicit InvokeAction(FunctionImpl function_impl) |
| 98 | : function_impl_(function_impl) {} |
| 99 | |
| 100 | template <typename Result, typename ArgumentTuple> |
| 101 | Result Perform(const ArgumentTuple& args) { |
| 102 | return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args); |
| 103 | } |
| 104 | private: |
| 105 | FunctionImpl function_impl_; |
| 106 | }; |
| 107 | |
| 108 | // Implements the Invoke(object_ptr, &Class::Method) action. |
| 109 | template <class Class, typename MethodPtr> |
| 110 | class InvokeMethodAction { |
| 111 | public: |
| 112 | InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr) |
| 113 | : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {} |
| 114 | |
| 115 | template <typename Result, typename ArgumentTuple> |
| 116 | Result Perform(const ArgumentTuple& args) const { |
| 117 | return InvokeHelper<Result, ArgumentTuple>::InvokeMethod( |
| 118 | obj_ptr_, method_ptr_, args); |
| 119 | } |
| 120 | private: |
| 121 | Class* const obj_ptr_; |
| 122 | const MethodPtr method_ptr_; |
| 123 | }; |
| 124 | |
| 125 | // A ReferenceWrapper<T> object represents a reference to type T, |
| 126 | // which can be either const or not. It can be explicitly converted |
| 127 | // from, and implicitly converted to, a T&. Unlike a reference, |
| 128 | // ReferenceWrapper<T> can be copied and can survive template type |
| 129 | // inference. This is used to support by-reference arguments in the |
| 130 | // InvokeArgument<N>(...) action. The idea was from "reference |
| 131 | // wrappers" in tr1, which we don't have in our source tree yet. |
| 132 | template <typename T> |
| 133 | class ReferenceWrapper { |
| 134 | public: |
| 135 | // Constructs a ReferenceWrapper<T> object from a T&. |
| 136 | explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {} // NOLINT |
| 137 | |
| 138 | // Allows a ReferenceWrapper<T> object to be implicitly converted to |
| 139 | // a T&. |
| 140 | operator T&() const { return *pointer_; } |
| 141 | private: |
| 142 | T* pointer_; |
| 143 | }; |
| 144 | |
| 145 | // CallableHelper has static methods for invoking "callables", |
| 146 | // i.e. function pointers and functors. It uses overloading to |
| 147 | // provide a uniform interface for invoking different kinds of |
| 148 | // callables. In particular, you can use: |
| 149 | // |
| 150 | // CallableHelper<R>::Call(callable, a1, a2, ..., an) |
| 151 | // |
| 152 | // to invoke an n-ary callable, where R is its return type. If an |
| 153 | // argument, say a2, needs to be passed by reference, you should write |
| 154 | // ByRef(a2) instead of a2 in the above expression. |
| 155 | template <typename R> |
| 156 | class CallableHelper { |
| 157 | public: |
| 158 | // Calls a nullary callable. |
| 159 | template <typename Function> |
| 160 | static R Call(Function function) { return function(); } |
| 161 | |
| 162 | // Calls a unary callable. |
| 163 | |
| 164 | // We deliberately pass a1 by value instead of const reference here |
| 165 | // in case it is a C-string literal. If we had declared the |
| 166 | // parameter as 'const A1& a1' and write Call(function, "Hi"), the |
| 167 | // compiler would've thought A1 is 'char[3]', which causes trouble |
| 168 | // when you need to copy a value of type A1. By declaring the |
| 169 | // parameter as 'A1 a1', the compiler will correctly infer that A1 |
| 170 | // is 'const char*' when it sees Call(function, "Hi"). |
| 171 | // |
| 172 | // Since this function is defined inline, the compiler can get rid |
| 173 | // of the copying of the arguments. Therefore the performance won't |
| 174 | // be hurt. |
| 175 | template <typename Function, typename A1> |
| 176 | static R Call(Function function, A1 a1) { return function(a1); } |
| 177 | |
| 178 | $range i 2..n |
| 179 | $for i |
| 180 | [[ |
| 181 | $var arity = [[$if i==2 [[binary]] $elif i==3 [[ternary]] $else [[$i-ary]]]] |
| 182 | |
| 183 | // Calls a $arity callable. |
| 184 | |
| 185 | $range j 1..i |
| 186 | $var typename_As = [[$for j, [[typename A$j]]]] |
| 187 | $var Aas = [[$for j, [[A$j a$j]]]] |
| 188 | $var as = [[$for j, [[a$j]]]] |
| 189 | $var typename_Ts = [[$for j, [[typename T$j]]]] |
| 190 | $var Ts = [[$for j, [[T$j]]]] |
| 191 | template <typename Function, $typename_As> |
| 192 | static R Call(Function function, $Aas) { |
| 193 | return function($as); |
| 194 | } |
| 195 | |
| 196 | ]] |
| 197 | |
| 198 | }; // class CallableHelper |
| 199 | |
| 200 | // Invokes a nullary callable argument. |
| 201 | template <size_t N> |
| 202 | class InvokeArgumentAction0 { |
| 203 | public: |
| 204 | template <typename Result, typename ArgumentTuple> |
| 205 | static Result Perform(const ArgumentTuple& args) { |
| 206 | return CallableHelper<Result>::Call(::std::tr1::get<N>(args)); |
| 207 | } |
| 208 | }; |
| 209 | |
| 210 | // Invokes a unary callable argument with the given argument. |
| 211 | template <size_t N, typename A1> |
| 212 | class InvokeArgumentAction1 { |
| 213 | public: |
| 214 | // We deliberately pass a1 by value instead of const reference here |
| 215 | // in case it is a C-string literal. |
| 216 | // |
| 217 | // Since this function is defined inline, the compiler can get rid |
| 218 | // of the copying of the arguments. Therefore the performance won't |
| 219 | // be hurt. |
| 220 | explicit InvokeArgumentAction1(A1 a1) : arg1_(a1) {} |
| 221 | |
| 222 | template <typename Result, typename ArgumentTuple> |
| 223 | Result Perform(const ArgumentTuple& args) { |
| 224 | return CallableHelper<Result>::Call(::std::tr1::get<N>(args), arg1_); |
| 225 | } |
| 226 | private: |
| 227 | const A1 arg1_; |
| 228 | }; |
| 229 | |
| 230 | $range i 2..n |
| 231 | $for i [[ |
| 232 | $var arity = [[$if i==2 [[binary]] $elif i==3 [[ternary]] $else [[$i-ary]]]] |
| 233 | $range j 1..i |
| 234 | $var typename_As = [[$for j, [[typename A$j]]]] |
| 235 | $var args_ = [[$for j, [[arg$j[[]]_]]]] |
| 236 | |
| 237 | // Invokes a $arity callable argument with the given arguments. |
| 238 | template <size_t N, $typename_As> |
| 239 | class InvokeArgumentAction$i { |
| 240 | public: |
| 241 | InvokeArgumentAction$i($for j, [[A$j a$j]]) : |
| 242 | $for j, [[arg$j[[]]_(a$j)]] {} |
| 243 | |
| 244 | template <typename Result, typename ArgumentTuple> |
| 245 | Result Perform(const ArgumentTuple& args) { |
| 246 | $if i <= 4 [[ |
| 247 | |
| 248 | return CallableHelper<Result>::Call(::std::tr1::get<N>(args), $args_); |
| 249 | |
| 250 | ]] $else [[ |
| 251 | |
| 252 | // We extract the callable to a variable before invoking it, in |
| 253 | // case it is a functor passed by value and its operator() is not |
| 254 | // const. |
| 255 | typename ::std::tr1::tuple_element<N, ArgumentTuple>::type function = |
| 256 | ::std::tr1::get<N>(args); |
| 257 | return function($args_); |
| 258 | |
| 259 | ]] |
| 260 | } |
| 261 | private: |
| 262 | $for j [[ |
| 263 | |
| 264 | const A$j arg$j[[]]_; |
| 265 | ]] |
| 266 | |
| 267 | }; |
| 268 | |
| 269 | ]] |
| 270 | |
| 271 | // An INTERNAL macro for extracting the type of a tuple field. It's |
| 272 | // subject to change without notice - DO NOT USE IN USER CODE! |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 273 | #define GMOCK_FIELD_(Tuple, N) \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 274 | typename ::std::tr1::tuple_element<N, Tuple>::type |
| 275 | |
| 276 | $range i 1..n |
| 277 | |
| 278 | // SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::type is the |
| 279 | // type of an n-ary function whose i-th (1-based) argument type is the |
| 280 | // k{i}-th (0-based) field of ArgumentTuple, which must be a tuple |
| 281 | // type, and whose return type is Result. For example, |
| 282 | // SelectArgs<int, ::std::tr1::tuple<bool, char, double, long>, 0, 3>::type |
| 283 | // is int(bool, long). |
| 284 | // |
| 285 | // SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::Select(args) |
| 286 | // returns the selected fields (k1, k2, ..., k_n) of args as a tuple. |
| 287 | // For example, |
| 288 | // SelectArgs<int, ::std::tr1::tuple<bool, char, double>, 2, 0>::Select( |
| 289 | // ::std::tr1::make_tuple(true, 'a', 2.5)) |
| 290 | // returns ::std::tr1::tuple (2.5, true). |
| 291 | // |
| 292 | // The numbers in list k1, k2, ..., k_n must be >= 0, where n can be |
| 293 | // in the range [0, $n]. Duplicates are allowed and they don't have |
| 294 | // to be in an ascending or descending order. |
| 295 | |
| 296 | template <typename Result, typename ArgumentTuple, $for i, [[int k$i]]> |
| 297 | class SelectArgs { |
| 298 | public: |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 299 | typedef Result type($for i, [[GMOCK_FIELD_(ArgumentTuple, k$i)]]); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 300 | typedef typename Function<type>::ArgumentTuple SelectedArgs; |
| 301 | static SelectedArgs Select(const ArgumentTuple& args) { |
| 302 | using ::std::tr1::get; |
| 303 | return SelectedArgs($for i, [[get<k$i>(args)]]); |
| 304 | } |
| 305 | }; |
| 306 | |
| 307 | |
| 308 | $for i [[ |
| 309 | $range j 1..n |
| 310 | $range j1 1..i-1 |
| 311 | template <typename Result, typename ArgumentTuple$for j1[[, int k$j1]]> |
| 312 | class SelectArgs<Result, ArgumentTuple, |
| 313 | $for j, [[$if j <= i-1 [[k$j]] $else [[-1]]]]> { |
| 314 | public: |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 315 | typedef Result type($for j1, [[GMOCK_FIELD_(ArgumentTuple, k$j1)]]); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 316 | typedef typename Function<type>::ArgumentTuple SelectedArgs; |
| 317 | static SelectedArgs Select(const ArgumentTuple& args) { |
| 318 | using ::std::tr1::get; |
| 319 | return SelectedArgs($for j1, [[get<k$j1>(args)]]); |
| 320 | } |
| 321 | }; |
| 322 | |
| 323 | |
| 324 | ]] |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 325 | #undef GMOCK_FIELD_ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 326 | |
| 327 | $var ks = [[$for i, [[k$i]]]] |
| 328 | |
| 329 | // Implements the WithArgs action. |
| 330 | template <typename InnerAction, $for i, [[int k$i = -1]]> |
| 331 | class WithArgsAction { |
| 332 | public: |
| 333 | explicit WithArgsAction(const InnerAction& action) : action_(action) {} |
| 334 | |
| 335 | template <typename F> |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 336 | operator Action<F>() const { return MakeAction(new Impl<F>(action_)); } |
| 337 | |
| 338 | private: |
| 339 | template <typename F> |
| 340 | class Impl : public ActionInterface<F> { |
| 341 | public: |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 342 | typedef typename Function<F>::Result Result; |
| 343 | typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 344 | |
| 345 | explicit Impl(const InnerAction& action) : action_(action) {} |
| 346 | |
| 347 | virtual Result Perform(const ArgumentTuple& args) { |
| 348 | return action_.Perform(SelectArgs<Result, ArgumentTuple, $ks>::Select(args)); |
| 349 | } |
| 350 | |
| 351 | private: |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 352 | typedef typename SelectArgs<Result, ArgumentTuple, |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 353 | $ks>::type InnerFunctionType; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 354 | |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 355 | Action<InnerFunctionType> action_; |
| 356 | }; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 357 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 358 | const InnerAction action_; |
| 359 | }; |
| 360 | |
| 361 | // Does two actions sequentially. Used for implementing the DoAll(a1, |
| 362 | // a2, ...) action. |
| 363 | template <typename Action1, typename Action2> |
| 364 | class DoBothAction { |
| 365 | public: |
| 366 | DoBothAction(Action1 action1, Action2 action2) |
| 367 | : action1_(action1), action2_(action2) {} |
| 368 | |
| 369 | // This template type conversion operator allows DoAll(a1, ..., a_n) |
| 370 | // to be used in ANY function of compatible type. |
| 371 | template <typename F> |
| 372 | operator Action<F>() const { |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 373 | return Action<F>(new Impl<F>(action1_, action2_)); |
| 374 | } |
| 375 | |
| 376 | private: |
| 377 | // Implements the DoAll(...) action for a particular function type F. |
| 378 | template <typename F> |
| 379 | class Impl : public ActionInterface<F> { |
| 380 | public: |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 381 | typedef typename Function<F>::Result Result; |
| 382 | typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
| 383 | typedef typename Function<F>::MakeResultVoid VoidResult; |
| 384 | |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 385 | Impl(const Action<VoidResult>& action1, const Action<F>& action2) |
| 386 | : action1_(action1), action2_(action2) {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 387 | |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 388 | virtual Result Perform(const ArgumentTuple& args) { |
| 389 | action1_.Perform(args); |
| 390 | return action2_.Perform(args); |
| 391 | } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 392 | |
zhanyong.wan | 38ca64d | 2009-02-19 22:30:22 +0000 | [diff] [blame] | 393 | private: |
| 394 | const Action<VoidResult> action1_; |
| 395 | const Action<F> action2_; |
| 396 | }; |
| 397 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 398 | Action1 action1_; |
| 399 | Action2 action2_; |
| 400 | }; |
| 401 | |
shiqian | 326aa56 | 2009-01-09 21:43:57 +0000 | [diff] [blame] | 402 | // A macro from the ACTION* family (defined later in this file) |
| 403 | // defines an action that can be used in a mock function. Typically, |
| 404 | // these actions only care about a subset of the arguments of the mock |
| 405 | // function. For example, if such an action only uses the second |
| 406 | // argument, it can be used in any mock function that takes >= 2 |
| 407 | // arguments where the type of the second argument is compatible. |
| 408 | // |
| 409 | // Therefore, the action implementation must be prepared to take more |
| 410 | // arguments than it needs. The ExcessiveArg type is used to |
| 411 | // represent those excessive arguments. In order to keep the compiler |
| 412 | // error messages tractable, we define it in the testing namespace |
| 413 | // instead of testing::internal. However, this is an INTERNAL TYPE |
| 414 | // and subject to change without notice, so a user MUST NOT USE THIS |
| 415 | // TYPE DIRECTLY. |
| 416 | struct ExcessiveArg {}; |
| 417 | |
| 418 | // A helper class needed for implementing the ACTION* macros. |
| 419 | template <typename Result, class Impl> |
| 420 | class ActionHelper { |
| 421 | public: |
| 422 | $range i 0..n |
| 423 | $for i |
| 424 | |
| 425 | [[ |
| 426 | $var template = [[$if i==0 [[]] $else [[ |
| 427 | $range j 0..i-1 |
| 428 | template <$for j, [[typename A$j]]> |
| 429 | ]]]] |
| 430 | $range j 0..i-1 |
| 431 | $var As = [[$for j, [[A$j]]]] |
| 432 | $var as = [[$for j, [[get<$j>(args)]]]] |
| 433 | $range k 1..n-i |
| 434 | $var eas = [[$for k, [[ExcessiveArg()]]]] |
| 435 | $var arg_list = [[$if (i==0) | (i==n) [[$as$eas]] $else [[$as, $eas]]]] |
| 436 | $template |
| 437 | static Result Perform(Impl* impl, const ::std::tr1::tuple<$As>& args) { |
| 438 | using ::std::tr1::get; |
zhanyong.wan | 7f4c2c0 | 2009-02-19 22:38:27 +0000 | [diff] [blame^] | 439 | return impl->template gmock_PerformImpl<$As>(args, $arg_list); |
shiqian | 326aa56 | 2009-01-09 21:43:57 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | ]] |
| 443 | }; |
| 444 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 445 | } // namespace internal |
| 446 | |
| 447 | // Various overloads for Invoke(). |
| 448 | |
| 449 | // Creates an action that invokes 'function_impl' with the mock |
| 450 | // function's arguments. |
| 451 | template <typename FunctionImpl> |
| 452 | PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke( |
| 453 | FunctionImpl function_impl) { |
| 454 | return MakePolymorphicAction( |
| 455 | internal::InvokeAction<FunctionImpl>(function_impl)); |
| 456 | } |
| 457 | |
| 458 | // Creates an action that invokes the given method on the given object |
| 459 | // with the mock function's arguments. |
| 460 | template <class Class, typename MethodPtr> |
| 461 | PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke( |
| 462 | Class* obj_ptr, MethodPtr method_ptr) { |
| 463 | return MakePolymorphicAction( |
| 464 | internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr)); |
| 465 | } |
| 466 | |
| 467 | // Creates a reference wrapper for the given L-value. If necessary, |
| 468 | // you can explicitly specify the type of the reference. For example, |
| 469 | // suppose 'derived' is an object of type Derived, ByRef(derived) |
| 470 | // would wrap a Derived&. If you want to wrap a const Base& instead, |
| 471 | // where Base is a base class of Derived, just write: |
| 472 | // |
| 473 | // ByRef<const Base>(derived) |
| 474 | template <typename T> |
| 475 | inline internal::ReferenceWrapper<T> ByRef(T& l_value) { // NOLINT |
| 476 | return internal::ReferenceWrapper<T>(l_value); |
| 477 | } |
| 478 | |
| 479 | // Various overloads for InvokeArgument<N>(). |
| 480 | // |
| 481 | // The InvokeArgument<N>(a1, a2, ..., a_k) action invokes the N-th |
| 482 | // (0-based) argument, which must be a k-ary callable, of the mock |
| 483 | // function, with arguments a1, a2, ..., a_k. |
| 484 | // |
| 485 | // Notes: |
| 486 | // |
| 487 | // 1. The arguments are passed by value by default. If you need to |
| 488 | // pass an argument by reference, wrap it inside ByRef(). For |
| 489 | // example, |
| 490 | // |
| 491 | // InvokeArgument<1>(5, string("Hello"), ByRef(foo)) |
| 492 | // |
| 493 | // passes 5 and string("Hello") by value, and passes foo by |
| 494 | // reference. |
| 495 | // |
| 496 | // 2. If the callable takes an argument by reference but ByRef() is |
| 497 | // not used, it will receive the reference to a copy of the value, |
| 498 | // instead of the original value. For example, when the 0-th |
| 499 | // argument of the mock function takes a const string&, the action |
| 500 | // |
| 501 | // InvokeArgument<0>(string("Hello")) |
| 502 | // |
| 503 | // makes a copy of the temporary string("Hello") object and passes a |
| 504 | // reference of the copy, instead of the original temporary object, |
| 505 | // to the callable. This makes it easy for a user to define an |
| 506 | // InvokeArgument action from temporary values and have it performed |
| 507 | // later. |
| 508 | template <size_t N> |
| 509 | inline PolymorphicAction<internal::InvokeArgumentAction0<N> > InvokeArgument() { |
| 510 | return MakePolymorphicAction(internal::InvokeArgumentAction0<N>()); |
| 511 | } |
| 512 | |
| 513 | // We deliberately pass a1 by value instead of const reference here in |
| 514 | // case it is a C-string literal. If we had declared the parameter as |
| 515 | // 'const A1& a1' and write InvokeArgument<0>("Hi"), the compiler |
| 516 | // would've thought A1 is 'char[3]', which causes trouble as the |
| 517 | // implementation needs to copy a value of type A1. By declaring the |
| 518 | // parameter as 'A1 a1', the compiler will correctly infer that A1 is |
| 519 | // 'const char*' when it sees InvokeArgument<0>("Hi"). |
| 520 | // |
| 521 | // Since this function is defined inline, the compiler can get rid of |
| 522 | // the copying of the arguments. Therefore the performance won't be |
| 523 | // hurt. |
| 524 | template <size_t N, typename A1> |
| 525 | inline PolymorphicAction<internal::InvokeArgumentAction1<N, A1> > |
| 526 | InvokeArgument(A1 a1) { |
| 527 | return MakePolymorphicAction(internal::InvokeArgumentAction1<N, A1>(a1)); |
| 528 | } |
| 529 | |
| 530 | $range i 2..n |
| 531 | $for i [[ |
| 532 | $range j 1..i |
| 533 | $var typename_As = [[$for j, [[typename A$j]]]] |
| 534 | $var As = [[$for j, [[A$j]]]] |
| 535 | $var Aas = [[$for j, [[A$j a$j]]]] |
| 536 | $var as = [[$for j, [[a$j]]]] |
| 537 | |
| 538 | template <size_t N, $typename_As> |
| 539 | inline PolymorphicAction<internal::InvokeArgumentAction$i<N, $As> > |
| 540 | InvokeArgument($Aas) { |
| 541 | return MakePolymorphicAction( |
| 542 | internal::InvokeArgumentAction$i<N, $As>($as)); |
| 543 | } |
| 544 | |
| 545 | ]] |
| 546 | |
| 547 | // WithoutArgs(inner_action) can be used in a mock function with a |
| 548 | // non-empty argument list to perform inner_action, which takes no |
| 549 | // argument. In other words, it adapts an action accepting no |
| 550 | // argument to one that accepts (and ignores) arguments. |
| 551 | template <typename InnerAction> |
| 552 | inline internal::WithArgsAction<InnerAction> |
| 553 | WithoutArgs(const InnerAction& action) { |
| 554 | return internal::WithArgsAction<InnerAction>(action); |
| 555 | } |
| 556 | |
| 557 | // WithArg<k>(an_action) creates an action that passes the k-th |
| 558 | // (0-based) argument of the mock function to an_action and performs |
| 559 | // it. It adapts an action accepting one argument to one that accepts |
| 560 | // multiple arguments. For convenience, we also provide |
| 561 | // WithArgs<k>(an_action) (defined below) as a synonym. |
| 562 | template <int k, typename InnerAction> |
| 563 | inline internal::WithArgsAction<InnerAction, k> |
| 564 | WithArg(const InnerAction& action) { |
| 565 | return internal::WithArgsAction<InnerAction, k>(action); |
| 566 | } |
| 567 | |
| 568 | // WithArgs<N1, N2, ..., Nk>(an_action) creates an action that passes |
| 569 | // the selected arguments of the mock function to an_action and |
| 570 | // performs it. It serves as an adaptor between actions with |
| 571 | // different argument lists. C++ doesn't support default arguments for |
| 572 | // function templates, so we have to overload it. |
| 573 | |
| 574 | $range i 1..n |
| 575 | $for i [[ |
| 576 | $range j 1..i |
| 577 | template <$for j [[int k$j, ]]typename InnerAction> |
| 578 | inline internal::WithArgsAction<InnerAction$for j [[, k$j]]> |
| 579 | WithArgs(const InnerAction& action) { |
| 580 | return internal::WithArgsAction<InnerAction$for j [[, k$j]]>(action); |
| 581 | } |
| 582 | |
| 583 | |
| 584 | ]] |
| 585 | // Creates an action that does actions a1, a2, ..., sequentially in |
| 586 | // each invocation. |
| 587 | $range i 2..n |
| 588 | $for i [[ |
| 589 | $range j 2..i |
| 590 | $var types = [[$for j, [[typename Action$j]]]] |
| 591 | $var Aas = [[$for j [[, Action$j a$j]]]] |
| 592 | |
| 593 | template <typename Action1, $types> |
| 594 | $range k 1..i-1 |
| 595 | |
| 596 | inline $for k [[internal::DoBothAction<Action$k, ]]Action$i$for k [[>]] |
| 597 | |
| 598 | DoAll(Action1 a1$Aas) { |
| 599 | $if i==2 [[ |
| 600 | |
| 601 | return internal::DoBothAction<Action1, Action2>(a1, a2); |
| 602 | ]] $else [[ |
| 603 | $range j2 2..i |
| 604 | |
| 605 | return DoAll(a1, DoAll($for j2, [[a$j2]])); |
| 606 | ]] |
| 607 | |
| 608 | } |
| 609 | |
| 610 | ]] |
| 611 | |
| 612 | } // namespace testing |
| 613 | |
shiqian | 326aa56 | 2009-01-09 21:43:57 +0000 | [diff] [blame] | 614 | // The ACTION* family of macros can be used in a namespace scope to |
| 615 | // define custom actions easily. The syntax: |
| 616 | // |
| 617 | // ACTION(name) { statements; } |
| 618 | // |
| 619 | // will define an action with the given name that executes the |
| 620 | // statements. The value returned by the statements will be used as |
| 621 | // the return value of the action. Inside the statements, you can |
| 622 | // refer to the K-th (0-based) argument of the mock function by |
| 623 | // 'argK', and refer to its type by 'argK_type'. For example: |
| 624 | // |
| 625 | // ACTION(IncrementArg1) { |
| 626 | // arg1_type temp = arg1; |
| 627 | // return ++(*temp); |
| 628 | // } |
| 629 | // |
| 630 | // allows you to write |
| 631 | // |
| 632 | // ...WillOnce(IncrementArg1()); |
| 633 | // |
| 634 | // You can also refer to the entire argument tuple and its type by |
| 635 | // 'args' and 'args_type', and refer to the mock function type and its |
| 636 | // return type by 'function_type' and 'return_type'. |
| 637 | // |
| 638 | // Note that you don't need to specify the types of the mock function |
| 639 | // arguments. However rest assured that your code is still type-safe: |
| 640 | // you'll get a compiler error if *arg1 doesn't support the ++ |
| 641 | // operator, or if the type of ++(*arg1) isn't compatible with the |
| 642 | // mock function's return type, for example. |
| 643 | // |
| 644 | // Sometimes you'll want to parameterize the action. For that you can use |
| 645 | // another macro: |
| 646 | // |
| 647 | // ACTION_P(name, param_name) { statements; } |
| 648 | // |
| 649 | // For example: |
| 650 | // |
| 651 | // ACTION_P(Add, n) { return arg0 + n; } |
| 652 | // |
| 653 | // will allow you to write: |
| 654 | // |
| 655 | // ...WillOnce(Add(5)); |
| 656 | // |
| 657 | // Note that you don't need to provide the type of the parameter |
| 658 | // either. If you need to reference the type of a parameter named |
| 659 | // 'foo', you can write 'foo_type'. For example, in the body of |
| 660 | // ACTION_P(Add, n) above, you can write 'n_type' to refer to the type |
| 661 | // of 'n'. |
| 662 | // |
| 663 | // We also provide ACTION_P2, ACTION_P3, ..., up to ACTION_P$n to support |
| 664 | // multi-parameter actions. |
| 665 | // |
| 666 | // For the purpose of typing, you can view |
| 667 | // |
| 668 | // ACTION_Pk(Foo, p1, ..., pk) { ... } |
| 669 | // |
| 670 | // as shorthand for |
| 671 | // |
| 672 | // template <typename p1_type, ..., typename pk_type> |
| 673 | // FooActionPk<p1_type, ..., pk_type> Foo(p1_type p1, ..., pk_type pk) { ... } |
| 674 | // |
| 675 | // In particular, you can provide the template type arguments |
| 676 | // explicitly when invoking Foo(), as in Foo<long, bool>(5, false); |
| 677 | // although usually you can rely on the compiler to infer the types |
| 678 | // for you automatically. You can assign the result of expression |
| 679 | // Foo(p1, ..., pk) to a variable of type FooActionPk<p1_type, ..., |
| 680 | // pk_type>. This can be useful when composing actions. |
| 681 | // |
| 682 | // You can also overload actions with different numbers of parameters: |
| 683 | // |
| 684 | // ACTION_P(Plus, a) { ... } |
| 685 | // ACTION_P2(Plus, a, b) { ... } |
| 686 | // |
| 687 | // While it's tempting to always use the ACTION* macros when defining |
| 688 | // a new action, you should also consider implementing ActionInterface |
| 689 | // or using MakePolymorphicAction() instead, especially if you need to |
| 690 | // use the action a lot. While these approaches require more work, |
| 691 | // they give you more control on the types of the mock function |
| 692 | // arguments and the action parameters, which in general leads to |
| 693 | // better compiler error messages that pay off in the long run. They |
| 694 | // also allow overloading actions based on parameter types (as opposed |
| 695 | // to just based on the number of parameters). |
| 696 | // |
| 697 | // CAVEAT: |
| 698 | // |
| 699 | // ACTION*() can only be used in a namespace scope. The reason is |
| 700 | // that C++ doesn't yet allow function-local types to be used to |
| 701 | // instantiate templates. The up-coming C++0x standard will fix this. |
| 702 | // Once that's done, we'll consider supporting using ACTION*() inside |
| 703 | // a function. |
| 704 | // |
| 705 | // MORE INFORMATION: |
| 706 | // |
| 707 | // To learn more about using these macros, please search for 'ACTION' |
| 708 | // on http://code.google.com/p/googlemock/wiki/CookBook. |
| 709 | |
| 710 | $range i 0..n |
| 711 | $for i |
| 712 | |
| 713 | [[ |
| 714 | $var template = [[$if i==0 [[]] $else [[ |
| 715 | $range j 0..i-1 |
| 716 | |
| 717 | template <$for j, [[typename p$j##_type]]>\ |
| 718 | ]]]] |
| 719 | $var class_name = [[name##Action[[$if i==0 [[]] $elif i==1 [[P]] |
| 720 | $else [[P$i]]]]]] |
| 721 | $range j 0..i-1 |
| 722 | $var ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]] |
| 723 | $var param_types_and_names = [[$for j, [[p$j##_type p$j]]]] |
| 724 | $var inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(gmock_p$j)]]]]]] |
zhanyong.wan | c069d7f | 2009-02-02 20:51:53 +0000 | [diff] [blame] | 725 | $var param_field_decls = [[$for j |
shiqian | 326aa56 | 2009-01-09 21:43:57 +0000 | [diff] [blame] | 726 | [[ |
| 727 | |
zhanyong.wan | c069d7f | 2009-02-02 20:51:53 +0000 | [diff] [blame] | 728 | p$j##_type p$j;\ |
shiqian | 326aa56 | 2009-01-09 21:43:57 +0000 | [diff] [blame] | 729 | ]]]] |
zhanyong.wan | c069d7f | 2009-02-02 20:51:53 +0000 | [diff] [blame] | 730 | $var param_field_decls2 = [[$for j |
shiqian | 326aa56 | 2009-01-09 21:43:57 +0000 | [diff] [blame] | 731 | [[ |
| 732 | |
zhanyong.wan | c069d7f | 2009-02-02 20:51:53 +0000 | [diff] [blame] | 733 | p$j##_type p$j;\ |
shiqian | 326aa56 | 2009-01-09 21:43:57 +0000 | [diff] [blame] | 734 | ]]]] |
| 735 | $var params = [[$for j, [[p$j]]]] |
| 736 | $var param_types = [[$if i==0 [[]] $else [[<$for j, [[p$j##_type]]>]]]] |
| 737 | $range k 0..n-1 |
| 738 | $var typename_arg_types = [[$for k, [[typename arg$k[[]]_type]]]] |
| 739 | $var arg_types_and_names = [[$for k, [[arg$k[[]]_type arg$k]]]] |
| 740 | $var macro_name = [[$if i==0 [[ACTION]] $elif i==1 [[ACTION_P]] |
| 741 | $else [[ACTION_P$i]]]] |
| 742 | |
| 743 | #define $macro_name(name$for j [[, p$j]])\$template |
| 744 | class $class_name {\ |
| 745 | public:\ |
| 746 | $class_name($ctor_param_list)$inits {}\ |
| 747 | template <typename F>\ |
| 748 | class gmock_Impl : public ::testing::ActionInterface<F> {\ |
| 749 | public:\ |
| 750 | typedef F function_type;\ |
| 751 | typedef typename ::testing::internal::Function<F>::Result return_type;\ |
| 752 | typedef typename ::testing::internal::Function<F>::ArgumentTuple\ |
| 753 | args_type;\ |
| 754 | [[$if i==1 [[explicit ]]]]gmock_Impl($ctor_param_list)$inits {}\ |
| 755 | virtual return_type Perform(const args_type& args) {\ |
| 756 | return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\ |
| 757 | Perform(this, args);\ |
| 758 | }\ |
| 759 | template <$typename_arg_types>\ |
| 760 | return_type gmock_PerformImpl(const args_type& args, [[]] |
zhanyong.wan | c069d7f | 2009-02-02 20:51:53 +0000 | [diff] [blame] | 761 | $arg_types_and_names) const;\$param_field_decls |
shiqian | 326aa56 | 2009-01-09 21:43:57 +0000 | [diff] [blame] | 762 | };\ |
| 763 | template <typename F> operator ::testing::Action<F>() const {\ |
| 764 | return ::testing::Action<F>(new gmock_Impl<F>($params));\ |
zhanyong.wan | c069d7f | 2009-02-02 20:51:53 +0000 | [diff] [blame] | 765 | }\$param_field_decls2 |
shiqian | 326aa56 | 2009-01-09 21:43:57 +0000 | [diff] [blame] | 766 | };\$template |
| 767 | inline $class_name$param_types name($param_types_and_names) {\ |
| 768 | return $class_name$param_types($params);\ |
| 769 | }\$template |
| 770 | template <typename F>\ |
| 771 | template <$typename_arg_types>\ |
| 772 | typename ::testing::internal::Function<F>::Result\ |
| 773 | $class_name$param_types::\ |
| 774 | gmock_Impl<F>::gmock_PerformImpl(const args_type& args, [[]] |
| 775 | $arg_types_and_names) const |
| 776 | ]] |
zhanyong.wan | 7f4c2c0 | 2009-02-19 22:38:27 +0000 | [diff] [blame^] | 777 | $$ } // This meta comment fixes auto-indentation in Emacs. It won't |
| 778 | $$ // show up in the generated code. |
shiqian | 326aa56 | 2009-01-09 21:43:57 +0000 | [diff] [blame] | 779 | |
| 780 | |
zhanyong.wan | 7f4c2c0 | 2009-02-19 22:38:27 +0000 | [diff] [blame^] | 781 | // TODO(wan@google.com): move the following to a different .h file |
| 782 | // such that we don't have to run 'pump' every time the code is |
| 783 | // updated. |
zhanyong.wan | e1cdce5 | 2009-02-06 01:09:43 +0000 | [diff] [blame] | 784 | namespace testing { |
| 785 | |
zhanyong.wan | 7f4c2c0 | 2009-02-19 22:38:27 +0000 | [diff] [blame^] | 786 | namespace internal { |
| 787 | |
| 788 | // Saves argument #0 to where the pointer points. |
| 789 | ACTION_P(SaveArg0, pointer) { *pointer = arg0; } |
| 790 | |
| 791 | // Assigns 'value' to the variable referenced by argument #0. |
| 792 | ACTION_P(SetArg0Referee, value) { |
| 793 | // Ensures that argument #0 is a reference. If you get a compiler |
| 794 | // error on the next line, you are using SetArgReferee<k>(value) in |
| 795 | // a mock function whose k-th (0-based) argument is not a reference. |
| 796 | GMOCK_COMPILE_ASSERT_(internal::is_reference<arg0_type>::value, |
| 797 | SetArgReferee_must_be_used_with_a_reference_argument); |
| 798 | arg0 = value; |
| 799 | } |
| 800 | |
| 801 | } // namespace internal |
| 802 | |
| 803 | // Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the |
| 804 | // mock function to *pointer. |
| 805 | template <int k, typename Pointer> |
| 806 | inline internal::WithArgsAction<internal::SaveArg0ActionP<Pointer>, k> |
| 807 | SaveArg(const Pointer& pointer) { |
| 808 | return WithArg<k>(internal::SaveArg0(pointer)); |
| 809 | } |
| 810 | |
| 811 | // Action SetArgReferee<k>(value) assigns 'value' to the variable |
| 812 | // referenced by the k-th (0-based) argument of the mock function. |
| 813 | template <int k, typename Value> |
| 814 | inline internal::WithArgsAction<internal::SetArg0RefereeActionP<Value>, k> |
| 815 | SetArgReferee(const Value& value) { |
| 816 | return WithArg<k>(internal::SetArg0Referee(value)); |
| 817 | } |
| 818 | |
zhanyong.wan | e1cdce5 | 2009-02-06 01:09:43 +0000 | [diff] [blame] | 819 | // Action Throw(exception) can be used in a mock function of any type |
| 820 | // to throw the given exception. Any copyable value can be thrown. |
| 821 | #if GTEST_HAS_EXCEPTIONS |
| 822 | ACTION_P(Throw, exception) { throw exception; } |
| 823 | #endif // GTEST_HAS_EXCEPTIONS |
| 824 | |
| 825 | } // namespace testing |
| 826 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 827 | #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_ |