OLD | NEW |
(Empty) | |
| 1 $$ This is a pump file for generating file templates. Pump is a python |
| 2 $$ script that is part of the Google Test suite of utilities. Description |
| 3 $$ can be found here: |
| 4 $$ |
| 5 $$ http://code.google.com/p/googletest/wiki/PumpManual |
| 6 $$ |
| 7 |
| 8 $var MAX_ARITY = 6 |
| 9 |
| 10 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 11 // Use of this source code is governed by a BSD-style license that can be |
| 12 // found in the LICENSE file. |
| 13 |
| 14 #ifndef BASE_BIND_INTERNAL_H_ |
| 15 #define BASE_BIND_INTERNAL_H_ |
| 16 #pragma once |
| 17 |
| 18 #include "base/bind_helpers.h" |
| 19 #include "base/callback_helpers.h" |
| 20 #include "base/template_util.h" |
| 21 |
| 22 namespace base { |
| 23 namespace internal { |
| 24 |
| 25 // The method by which a function is invoked is determined by 3 different |
| 26 // dimensions: |
| 27 // |
| 28 // 1) The type of function (normal, method, const-method) |
| 29 // 2) The arity of the function |
| 30 // 3) The number of bound parameters. |
| 31 // |
| 32 // The FunctionTraitsN classes unwrap the function signature type to |
| 33 // specialize based on the first two dimensions. The N in FunctionTraitsN |
| 34 // specifies the 3rd dimension. We could have specified the unbound parameters |
| 35 // via template parameters, but this method looked cleaner. |
| 36 // |
| 37 // The FunctionTraitsN contains a static DoInvoke() function that is the key to |
| 38 // implementing type erasure in the Callback() classes. DoInvoke() is a static |
| 39 // function with a fixed signature that is independent of StorageType; its |
| 40 // first argument is a pointer to the non-templated common baseclass of |
| 41 // StorageType. This lets us store pointer to DoInvoke() in a function pointer |
| 42 // that has knowledge of the specific StorageType, and thus no knowledge of the |
| 43 // bound function and bound parameter types. |
| 44 // |
| 45 // As long as we ensure that DoInvoke() is only used with pointers there were |
| 46 // upcasted from the correct StorageType, we can be sure that execution is |
| 47 // safe. |
| 48 |
| 49 $range BOUND 0..MAX_ARITY |
| 50 $for BOUND [[ |
| 51 |
| 52 template <typename StorageType, typename Sig> |
| 53 struct FunctionTraits$(BOUND); |
| 54 |
| 55 $range ARITY 0..MAX_ARITY |
| 56 $for ARITY [[ |
| 57 |
| 58 $var UNBOUND = ARITY - BOUND |
| 59 $if UNBOUND >= 0 [[ |
| 60 |
| 61 $$ Variables for function traits generation. |
| 62 $range ARG 1..ARITY |
| 63 $range BOUND_ARG 1..BOUND |
| 64 $range UNBOUND_ARG (ARITY - UNBOUND + 1)..ARITY |
| 65 |
| 66 $$ Variables for method traits generation. We are always short one arity since |
| 67 $$ the first bound parameter is the object. |
| 68 $var M_ARITY = ARITY - 1 |
| 69 $range M_ARG 1..M_ARITY |
| 70 $range M_BOUND_ARG 2..BOUND |
| 71 $range M_UNBOUND_ARG (M_ARITY - UNBOUND + 1)..M_ARITY |
| 72 |
| 73 // Function: Arity $(ARITY) -> $(UNBOUND). |
| 74 template <typename StorageType, typename R[[]] |
| 75 $if ARITY > 0 [[,]][[]] |
| 76 $for ARG , [[typename X$(ARG)]]> |
| 77 struct FunctionTraits$(BOUND)<StorageType, R(*)($for ARG , [[X$(ARG)]])> { |
| 78 $if ARITY > 0 [[ |
| 79 |
| 80 COMPILE_ASSERT( |
| 81 !($for ARG || [[ is_non_const_reference<X$(ARG)>::value ]]), |
| 82 do_not_bind_functions_with_nonconst_ref); |
| 83 |
| 84 ]] |
| 85 |
| 86 typedef base::false_type IsMethod; |
| 87 |
| 88 static R DoInvoke(InvokerStorageBase* base[[]] |
| 89 $if UNBOUND != 0 [[, ]][[]] |
| 90 $for UNBOUND_ARG , [[const X$(UNBOUND_ARG)& x$(UNBOUND_ARG)]]) { |
| 91 StorageType* invoker = static_cast<StorageType*>(base); |
| 92 return invoker->f_($for BOUND_ARG , [[Unwrap(invoker->p$(BOUND_ARG)_)]][[]] |
| 93 $$ Add comma if there are both boudn and unbound args. |
| 94 $if UNBOUND > 0 [[$if BOUND > 0 [[, ]]]][[]] |
| 95 $for UNBOUND_ARG , [[x$(UNBOUND_ARG)]]); |
| 96 } |
| 97 }; |
| 98 |
| 99 $if BOUND > 0 [[ |
| 100 |
| 101 // Method: Arity $(M_ARITY) -> $(UNBOUND). |
| 102 template <typename StorageType, typename R, typename T[[]] |
| 103 $if M_ARITY > 0[[, ]] $for M_ARG , [[typename X$(M_ARG)]]> |
| 104 struct FunctionTraits$(BOUND)<StorageType, R(T::*)($for M_ARG , [[X$(M_ARG)]])>
{ |
| 105 $if M_ARITY > 0 [[ |
| 106 |
| 107 COMPILE_ASSERT( |
| 108 !($for M_ARG || [[ is_non_const_reference<X$(M_ARG)>::value ]]), |
| 109 do_not_bind_functions_with_nonconst_ref); |
| 110 |
| 111 ]] |
| 112 |
| 113 typedef base::true_type IsMethod; |
| 114 |
| 115 static R DoInvoke(InvokerStorageBase* base[[]] |
| 116 $if UNBOUND > 0 [[, ]][[]] |
| 117 $for M_UNBOUND_ARG , [[const X$(M_UNBOUND_ARG)& x$(M_UNBOUND_ARG)]]) { |
| 118 StorageType* invoker = static_cast<StorageType*>(base); |
| 119 return (Unwrap(invoker->p1_)->*invoker->f_)([[]] |
| 120 $for M_BOUND_ARG , [[Unwrap(invoker->p$(M_BOUND_ARG)_)]][[]] |
| 121 $if UNBOUND > 0 [[$if BOUND > 1 [[, ]]]][[]] |
| 122 $for M_UNBOUND_ARG , [[x$(M_UNBOUND_ARG)]]); |
| 123 } |
| 124 }; |
| 125 |
| 126 // Const Method: Arity $(M_ARITY) -> $(UNBOUND). |
| 127 template <typename StorageType, typename R, typename T[[]] |
| 128 $if M_ARITY > 0[[, ]] $for M_ARG , [[typename X$(M_ARG)]]> |
| 129 struct FunctionTraits$(BOUND)<StorageType, R(T::*)($for M_ARG , [[X$(M_ARG)]]) c
onst> { |
| 130 $if M_ARITY > 0 [[ |
| 131 |
| 132 COMPILE_ASSERT( |
| 133 !($for M_ARG || [[is_non_const_reference<X$(M_ARG)>::value ]]), |
| 134 do_not_bind_functions_with_nonconst_ref); |
| 135 |
| 136 ]] |
| 137 |
| 138 typedef base::true_type IsMethod; |
| 139 |
| 140 static R DoInvoke(InvokerStorageBase* base[[]] |
| 141 $if UNBOUND > 0 [[, ]] |
| 142 [[]] $for M_UNBOUND_ARG , [[const X$(M_UNBOUND_ARG)& x$(M_UNBOUND_ARG)]]) { |
| 143 StorageType* invoker = static_cast<StorageType*>(base); |
| 144 return (Unwrap(invoker->p1_)->*invoker->f_)([[]] |
| 145 $for M_BOUND_ARG , [[Unwrap(invoker->p$(M_BOUND_ARG)_)]][[]] |
| 146 $if UNBOUND > 0 [[$if BOUND > 1 [[, ]]]][[]] |
| 147 $for M_UNBOUND_ARG , [[x$(M_UNBOUND_ARG)]]); |
| 148 } |
| 149 }; |
| 150 |
| 151 ]] $$ if BOUND |
| 152 |
| 153 ]] $$ if UNBOUND |
| 154 ]] $$ for ARITY |
| 155 ]] $$ for BOUND |
| 156 |
| 157 |
| 158 // These are the actual storage classes for the invokers. |
| 159 // |
| 160 // Though these types are "classes", they are being used as structs with |
| 161 // all member variable public. We cannot make it a struct because it inherits |
| 162 // from a class which causes a compiler warning. We cannot add a "Run()" method |
| 163 // that forwards the unbound arguments because that would require we unwrap the |
| 164 // Sig type like in FunctionTraitsN above to know the return type, and the arity |
| 165 // of Run(). |
| 166 // |
| 167 // An alternate solution would be to merge FunctionTraitsN and InvokerStorageN, |
| 168 // but the generated code seemed harder to read. |
| 169 |
| 170 $for BOUND [[ |
| 171 $range BOUND_ARG 1..BOUND |
| 172 |
| 173 template <typename Sig[[]] |
| 174 $if BOUND > 0 [[, ]] |
| 175 $for BOUND_ARG , [[typename P$(BOUND_ARG)]]> |
| 176 class InvokerStorage$(BOUND) : public InvokerStorageBase { |
| 177 public: |
| 178 typedef InvokerStorage$(BOUND) StorageType; |
| 179 typedef FunctionTraits$(BOUND)<StorageType, Sig> FunctionTraits; |
| 180 typedef typename FunctionTraits::IsMethod IsMethod; |
| 181 |
| 182 $for BOUND_ARG [[ |
| 183 $if BOUND_ARG == 1 [[ |
| 184 |
| 185 // For methods, we need to be careful for parameter 1. We skip the |
| 186 // scoped_refptr check because the binder itself takes care of this. We also |
| 187 // disallow binding of an array as the method's target object. |
| 188 COMPILE_ASSERT(IsMethod::value || |
| 189 !internal::UnsafeBindtoRefCountedArg<P$(BOUND_ARG)>::value, |
| 190 p$(BOUND_ARG)_is_refcounted_type_and_needs_scoped_refptr); |
| 191 COMPILE_ASSERT(!IsMethod::value || !is_array<P$(BOUND_ARG)>::value, |
| 192 first_bound_argument_to_method_cannot_be_array); |
| 193 ]] $else [[ |
| 194 |
| 195 COMPILE_ASSERT(!internal::UnsafeBindtoRefCountedArg<P$(BOUND_ARG)>::value, |
| 196 p$(BOUND_ARG)_is_refcounted_type_and_needs_scoped_refptr); |
| 197 ]] $$ $if BOUND_ARG |
| 198 ]] $$ $for BOUND_ARG |
| 199 |
| 200 |
| 201 |
| 202 InvokerStorage$(BOUND)(Sig f |
| 203 $if BOUND > 0 [[, ]] |
| 204 $for BOUND_ARG , [[const P$(BOUND_ARG)& p$(BOUND_ARG)]]) |
| 205 : f_(f)[[]] |
| 206 $if BOUND == 0 [[ |
| 207 { |
| 208 |
| 209 ]] $else [[ |
| 210 , $for BOUND_ARG , [[p$(BOUND_ARG)_(static_cast<typename BindType<P$(BOUND_ARG)>
::StorageType>(p$(BOUND_ARG)))]] { |
| 211 MaybeRefcount<IsMethod, P1>::AddRef(p1_); |
| 212 |
| 213 ]] |
| 214 } |
| 215 |
| 216 virtual ~InvokerStorage$(BOUND)() { |
| 217 $if BOUND > 0 [[ |
| 218 |
| 219 MaybeRefcount<IsMethod, P1>::Release(p1_); |
| 220 |
| 221 ]] |
| 222 } |
| 223 |
| 224 Sig f_; |
| 225 |
| 226 $for BOUND_ARG [[ |
| 227 typename BindType<P$(BOUND_ARG)>::StorageType p$(BOUND_ARG)_; |
| 228 |
| 229 ]] |
| 230 }; |
| 231 |
| 232 ]] $$ for BOUND |
| 233 |
| 234 } // namespace internal |
| 235 } // namespace base |
| 236 |
| 237 #endif // BASE_BIND_INTERNAL_H_ |
OLD | NEW |