| OLD | NEW |
| 1 $$ This is a pump file for generating file templates. Pump is a python | 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 | 2 $$ script that is part of the Google Test suite of utilities. Description |
| 3 $$ can be found here: | 3 $$ can be found here: |
| 4 $$ | 4 $$ |
| 5 $$ http://code.google.com/p/googletest/wiki/PumpManual | 5 $$ http://code.google.com/p/googletest/wiki/PumpManual |
| 6 $$ | 6 $$ |
| 7 | 7 |
| 8 $var MAX_ARITY = 6 | 8 $var MAX_ARITY = 6 |
| 9 | 9 |
| 10 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 11 // Use of this source code is governed by a BSD-style license that can be |
| 12 // found in the LICENSE file. | 12 // found in the LICENSE file. |
| 13 | 13 |
| 14 #ifndef BASE_BIND_INTERNAL_H_ | 14 #ifndef BASE_BIND_INTERNAL_H_ |
| 15 #define BASE_BIND_INTERNAL_H_ | 15 #define BASE_BIND_INTERNAL_H_ |
| 16 #pragma once | 16 #pragma once |
| 17 | 17 |
| 18 #include "base/bind_helpers.h" | 18 #include "base/bind_helpers.h" |
| 19 #include "base/callback_internal.h" | 19 #include "base/callback_internal.h" |
| 20 #include "base/template_util.h" | 20 #include "base/template_util.h" |
| 21 | 21 |
| 22 namespace base { | 22 namespace base { |
| 23 namespace internal { | 23 namespace internal { |
| 24 | 24 |
| 25 // The method by which a function is invoked is determined by 3 different | 25 // The method by which a function is invoked is determined by 3 different |
| 26 // dimensions: | 26 // dimensions: |
| 27 // | 27 // |
| 28 // 1) The type of function (normal, method, const-method) | 28 // 1) The type of function (normal or method). |
| 29 // 2) The arity of the function | 29 // 2) The arity of the function. |
| 30 // 3) The number of bound parameters. | 30 // 3) The number of bound parameters. |
| 31 // | 31 // |
| 32 // The FunctionTraitsN classes unwrap the function signature type to | 32 // The templates below handle the determination of each of these dimensions. |
| 33 // specialize based on the first two dimensions. The N in FunctionTraitsN | 33 // In brief: |
| 34 // specifies the 3rd dimension. We could have specified the unbound parameters | |
| 35 // via template parameters, but this method looked cleaner. | |
| 36 // | 34 // |
| 37 // The FunctionTraitsN contains a static DoInvoke() function that is the key to | 35 // FunctionTraits<> -- Provides a normalied signature, and other traits. |
| 38 // implementing type erasure in the Callback() classes. DoInvoke() is a static | 36 // InvokerN<> -- Provides a DoInvoke() function that actually executes |
| 39 // function with a fixed signature that is independent of StorageType; its | 37 // a calback. |
| 40 // first argument is a pointer to the non-templated common baseclass of | 38 // InvokerStorageN<> -- Provides storage for the bound parameters, and |
| 41 // StorageType. This lets us store pointer to DoInvoke() in a function pointer | 39 // typedefs to the above. |
| 42 // that has knowledge of the specific StorageType, and thus no knowledge of the | 40 // |
| 43 // bound function and bound parameter types. | 41 // More details about the design of each class is included in a comment closer |
| 42 // to their defition. |
| 43 |
| 44 // FunctionTraits<> |
| 45 // |
| 46 // The FunctionTraits<> template determines the type of function, and also |
| 47 // creates a NormalizedType used to select the InvokerN classes. It turns out |
| 48 // that syntactically, you only really have 2 variations when invoking a |
| 49 // funciton pointer: normal, and method. One is invoked func_ptr(arg1). The |
| 50 // other is invoked (*obj_->method_ptr(arg1)). |
| 51 // |
| 52 // However, in the type system, there are many more distinctions. In standard |
| 53 // C++, there's all variations of const, and volatile on the function pointer. |
| 54 // In Windows, there are additional calling conventions (eg., __stdcall, |
| 55 // __fastcall, etc.). FunctionTraits<> handles categorizing each of these into |
| 56 // a normalized signature. |
| 57 // |
| 58 // Having a NormalizedSignature signature, reduces the combinatoric |
| 59 // complexity of defintions for the InvokerN<> later. Even though there are |
| 60 // only 2 syntactic variations on invoking a function, without normalizing the |
| 61 // signature, there would need to be one specialization of InvokerN for each |
| 62 // unique (function_type, bound_arg, unbound_args) tuple in order to match all |
| 63 // function signatures. |
| 64 // |
| 65 // By normalizing the function signature, we reduce function_type to exactly 2. |
| 66 |
| 67 template <typename Sig> |
| 68 struct FunctionTraits; |
| 69 |
| 70 $range ARITY 0..MAX_ARITY |
| 71 $for ARITY [[ |
| 72 $range ARG 1..ARITY |
| 73 |
| 74 // Function: Arity $(ARITY). |
| 75 template <typename R[[]] |
| 76 $if ARITY > 0[[, ]] $for ARG , [[typename X$(ARG)]]> |
| 77 struct FunctionTraits<R(*)($for ARG , [[X$(ARG)]])> { |
| 78 typedef R (*NormalizedSig)($for ARG , [[X$(ARG)]]); |
| 79 typedef base::false_type IsMethod; |
| 80 }; |
| 81 |
| 82 // Method: Arity $(ARITY). |
| 83 template <typename R, typename T[[]] |
| 84 $if ARITY > 0[[, ]] $for ARG , [[typename X$(ARG)]]> |
| 85 struct FunctionTraits<R(T::*)($for ARG , [[X$(ARG)]])> { |
| 86 typedef R (T::*NormalizedSig)($for ARG , [[X$(ARG)]]); |
| 87 typedef base::true_type IsMethod; |
| 88 }; |
| 89 |
| 90 // Const Method: Arity $(ARITY). |
| 91 template <typename R, typename T[[]] |
| 92 $if ARITY > 0[[, ]] $for ARG , [[typename X$(ARG)]]> |
| 93 struct FunctionTraits<R(T::*)($for ARG , [[X$(ARG)]]) const> { |
| 94 typedef R (T::*NormalizedSig)($for ARG , [[X$(ARG)]]); |
| 95 typedef base::true_type IsMethod; |
| 96 }; |
| 97 |
| 98 ]] $$for ARITY |
| 99 |
| 100 // InvokerN<> |
| 101 // |
| 102 // The InvokerN templates contain a static DoInvoke() function that is the key |
| 103 // to implementing type erasure in the Callback() classes. |
| 104 // |
| 105 // DoInvoke() is a static function with a fixed signature that is independent |
| 106 // of StorageType; its first argument is a pointer to the non-templated common |
| 107 // baseclass of StorageType. This lets us store pointer to DoInvoke() in a |
| 108 // function pointer that has knowledge of the specific StorageType, and thus |
| 109 // no knowledge of the bound function and bound parameter types. |
| 44 // | 110 // |
| 45 // As long as we ensure that DoInvoke() is only used with pointers there were | 111 // 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 | 112 // upcasted from the correct StorageType, we can be sure that execution is |
| 47 // safe. | 113 // safe. |
| 114 // |
| 115 // The InvokerN templates are the only point that knows the number of bound |
| 116 // and unbound arguments. This is intentional because it allows the other |
| 117 // templates classes in the system to only have as many specializations as |
| 118 // the max arity of function we wish to support. |
| 48 | 119 |
| 49 $range BOUND 0..MAX_ARITY | 120 $range BOUND 0..MAX_ARITY |
| 50 $for BOUND [[ | 121 $for BOUND [[ |
| 51 | 122 |
| 52 template <typename StorageType, typename Sig> | 123 template <typename StorageType, typename NormalizedSig> |
| 53 struct FunctionTraits$(BOUND); | 124 struct Invoker$(BOUND); |
| 54 | 125 |
| 55 $range ARITY 0..MAX_ARITY | 126 $range ARITY 0..MAX_ARITY |
| 56 $for ARITY [[ | 127 $for ARITY [[ |
| 57 | 128 |
| 58 $var UNBOUND = ARITY - BOUND | 129 $var UNBOUND = ARITY - BOUND |
| 59 $if UNBOUND >= 0 [[ | 130 $if UNBOUND >= 0 [[ |
| 60 | 131 |
| 61 $$ Variables for function traits generation. | 132 $$ Variables for function traits generation. |
| 62 $range ARG 1..ARITY | 133 $range ARG 1..ARITY |
| 63 $range BOUND_ARG 1..BOUND | 134 $range BOUND_ARG 1..BOUND |
| 64 $range UNBOUND_ARG (ARITY - UNBOUND + 1)..ARITY | 135 $range UNBOUND_ARG (ARITY - UNBOUND + 1)..ARITY |
| 65 | 136 |
| 66 $$ Variables for method traits generation. We are always short one arity since | 137 $$ Variables for method traits generation. We are always short one arity since |
| 67 $$ the first bound parameter is the object. | 138 $$ the first bound parameter is the object. |
| 68 $var M_ARITY = ARITY - 1 | 139 $var M_ARITY = ARITY - 1 |
| 69 $range M_ARG 1..M_ARITY | 140 $range M_ARG 1..M_ARITY |
| 70 $range M_BOUND_ARG 2..BOUND | 141 $range M_BOUND_ARG 2..BOUND |
| 71 $range M_UNBOUND_ARG (M_ARITY - UNBOUND + 1)..M_ARITY | 142 $range M_UNBOUND_ARG (M_ARITY - UNBOUND + 1)..M_ARITY |
| 72 | 143 |
| 73 // Function: Arity $(ARITY) -> $(UNBOUND). | 144 // Function: Arity $(ARITY) -> $(UNBOUND). |
| 74 template <typename StorageType, typename R[[]] | 145 template <typename StorageType, typename R[[]] |
| 75 $if ARITY > 0 [[,]][[]] | 146 $if ARITY > 0 [[,]][[]] |
| 76 $for ARG , [[typename X$(ARG)]]> | 147 $for ARG , [[typename X$(ARG)]]> |
| 77 struct FunctionTraits$(BOUND)<StorageType, R(*)($for ARG , [[X$(ARG)]])> { | 148 struct Invoker$(BOUND)<StorageType, R(*)($for ARG , [[X$(ARG)]])> { |
| 78 $if ARITY > 0 [[ | 149 $if ARITY > 0 [[ |
| 79 | 150 |
| 80 COMPILE_ASSERT( | 151 COMPILE_ASSERT( |
| 81 !($for ARG || [[ is_non_const_reference<X$(ARG)>::value ]]), | 152 !($for ARG || [[ is_non_const_reference<X$(ARG)>::value ]]), |
| 82 do_not_bind_functions_with_nonconst_ref); | 153 do_not_bind_functions_with_nonconst_ref); |
| 83 | 154 |
| 84 ]] | 155 ]] |
| 85 | 156 |
| 86 typedef base::false_type IsMethod; | |
| 87 | |
| 88 static R DoInvoke(InvokerStorageBase* base[[]] | 157 static R DoInvoke(InvokerStorageBase* base[[]] |
| 89 $if UNBOUND != 0 [[, ]][[]] | 158 $if UNBOUND != 0 [[, ]][[]] |
| 90 $for UNBOUND_ARG , [[const X$(UNBOUND_ARG)& x$(UNBOUND_ARG)]]) { | 159 $for UNBOUND_ARG , [[const X$(UNBOUND_ARG)& x$(UNBOUND_ARG)]]) { |
| 91 StorageType* invoker = static_cast<StorageType*>(base); | 160 StorageType* invoker = static_cast<StorageType*>(base); |
| 92 return invoker->f_($for BOUND_ARG , [[Unwrap(invoker->p$(BOUND_ARG)_)]][[]] | 161 return invoker->f_($for BOUND_ARG , [[Unwrap(invoker->p$(BOUND_ARG)_)]][[]] |
| 93 $$ Add comma if there are both boudn and unbound args. | 162 $$ Add comma if there are both boudn and unbound args. |
| 94 $if UNBOUND > 0 [[$if BOUND > 0 [[, ]]]][[]] | 163 $if UNBOUND > 0 [[$if BOUND > 0 [[, ]]]][[]] |
| 95 $for UNBOUND_ARG , [[x$(UNBOUND_ARG)]]); | 164 $for UNBOUND_ARG , [[x$(UNBOUND_ARG)]]); |
| 96 } | 165 } |
| 97 }; | 166 }; |
| 98 | 167 |
| 99 $if BOUND > 0 [[ | 168 $if BOUND > 0 [[ |
| 100 | 169 |
| 101 // Method: Arity $(M_ARITY) -> $(UNBOUND). | 170 // Method: Arity $(M_ARITY) -> $(UNBOUND). |
| 102 template <typename StorageType, typename R, typename T[[]] | 171 template <typename StorageType, typename R, typename T[[]] |
| 103 $if M_ARITY > 0[[, ]] $for M_ARG , [[typename X$(M_ARG)]]> | 172 $if M_ARITY > 0[[, ]] $for M_ARG , [[typename X$(M_ARG)]]> |
| 104 struct FunctionTraits$(BOUND)<StorageType, R(T::*)($for M_ARG , [[X$(M_ARG)]])>
{ | 173 struct Invoker$(BOUND)<StorageType, R(T::*)($for M_ARG , [[X$(M_ARG)]])> { |
| 105 $if M_ARITY > 0 [[ | 174 $if M_ARITY > 0 [[ |
| 106 | 175 |
| 107 COMPILE_ASSERT( | 176 COMPILE_ASSERT( |
| 108 !($for M_ARG || [[ is_non_const_reference<X$(M_ARG)>::value ]]), | 177 !($for M_ARG || [[ is_non_const_reference<X$(M_ARG)>::value ]]), |
| 109 do_not_bind_functions_with_nonconst_ref); | 178 do_not_bind_functions_with_nonconst_ref); |
| 110 | 179 |
| 111 ]] | 180 ]] |
| 112 | 181 |
| 113 typedef base::true_type IsMethod; | |
| 114 | |
| 115 static R DoInvoke(InvokerStorageBase* base[[]] | 182 static R DoInvoke(InvokerStorageBase* base[[]] |
| 116 $if UNBOUND > 0 [[, ]][[]] | 183 $if UNBOUND > 0 [[, ]][[]] |
| 117 $for M_UNBOUND_ARG , [[const X$(M_UNBOUND_ARG)& x$(M_UNBOUND_ARG)]]) { | 184 $for M_UNBOUND_ARG , [[const X$(M_UNBOUND_ARG)& x$(M_UNBOUND_ARG)]]) { |
| 118 StorageType* invoker = static_cast<StorageType*>(base); | 185 StorageType* invoker = static_cast<StorageType*>(base); |
| 119 return (Unwrap(invoker->p1_)->*invoker->f_)([[]] | 186 return (Unwrap(invoker->p1_)->*invoker->f_)([[]] |
| 120 $for M_BOUND_ARG , [[Unwrap(invoker->p$(M_BOUND_ARG)_)]][[]] | 187 $for M_BOUND_ARG , [[Unwrap(invoker->p$(M_BOUND_ARG)_)]][[]] |
| 121 $if UNBOUND > 0 [[$if BOUND > 1 [[, ]]]][[]] | 188 $if UNBOUND > 0 [[$if BOUND > 1 [[, ]]]][[]] |
| 122 $for M_UNBOUND_ARG , [[x$(M_UNBOUND_ARG)]]); | 189 $for M_UNBOUND_ARG , [[x$(M_UNBOUND_ARG)]]); |
| 123 } | 190 } |
| 124 }; | 191 }; |
| 125 | 192 |
| 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 | 193 ]] $$ if BOUND |
| 152 | 194 |
| 153 ]] $$ if UNBOUND | 195 ]] $$ if UNBOUND |
| 154 ]] $$ for ARITY | 196 ]] $$ for ARITY |
| 155 ]] $$ for BOUND | 197 ]] $$ for BOUND |
| 156 | 198 |
| 157 | 199 |
| 158 // These are the actual storage classes for the invokers. | 200 // InvokerStorageN<> |
| 201 // |
| 202 // These are the actual storage classes for the Invokers. |
| 159 // | 203 // |
| 160 // Though these types are "classes", they are being used as structs with | 204 // 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 | 205 // 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 | 206 // 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 | 207 // 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 | 208 // Sig type like in InvokerN above to know the return type, and the arity |
| 165 // of Run(). | 209 // of Run(). |
| 166 // | 210 // |
| 167 // An alternate solution would be to merge FunctionTraitsN and InvokerStorageN, | 211 // An alternate solution would be to merge InvokerN and InvokerStorageN, |
| 168 // but the generated code seemed harder to read. | 212 // but the generated code seemed harder to read. |
| 169 | 213 |
| 170 $for BOUND [[ | 214 $for BOUND [[ |
| 171 $range BOUND_ARG 1..BOUND | 215 $range BOUND_ARG 1..BOUND |
| 172 | 216 |
| 173 template <typename Sig[[]] | 217 template <typename Sig[[]] |
| 174 $if BOUND > 0 [[, ]] | 218 $if BOUND > 0 [[, ]] |
| 175 $for BOUND_ARG , [[typename P$(BOUND_ARG)]]> | 219 $for BOUND_ARG , [[typename P$(BOUND_ARG)]]> |
| 176 class InvokerStorage$(BOUND) : public InvokerStorageBase { | 220 class InvokerStorage$(BOUND) : public InvokerStorageBase { |
| 177 public: | 221 public: |
| 178 typedef InvokerStorage$(BOUND) StorageType; | 222 typedef InvokerStorage$(BOUND) StorageType; |
| 179 typedef FunctionTraits$(BOUND)<StorageType, Sig> FunctionTraits; | 223 typedef FunctionTraits<Sig> TargetTraits; |
| 180 typedef typename FunctionTraits::IsMethod IsMethod; | 224 typedef Invoker$(BOUND)<StorageType, typename TargetTraits::NormalizedSig> Inv
oker; |
| 181 | 225 typedef typename TargetTraits::IsMethod IsMethod; |
| 182 $for BOUND_ARG [[ | 226 $for BOUND_ARG [[ |
| 183 $if BOUND_ARG == 1 [[ | 227 $if BOUND_ARG == 1 [[ |
| 184 | 228 |
| 185 // For methods, we need to be careful for parameter 1. We skip the | 229 // 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 | 230 // 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. | 231 // disallow binding of an array as the method's target object. |
| 188 COMPILE_ASSERT(IsMethod::value || | 232 COMPILE_ASSERT(IsMethod::value || |
| 189 !internal::UnsafeBindtoRefCountedArg<P$(BOUND_ARG)>::value, | 233 !internal::UnsafeBindtoRefCountedArg<P$(BOUND_ARG)>::value, |
| 190 p$(BOUND_ARG)_is_refcounted_type_and_needs_scoped_refptr); | 234 p$(BOUND_ARG)_is_refcounted_type_and_needs_scoped_refptr); |
| 191 COMPILE_ASSERT(!IsMethod::value || !is_array<P$(BOUND_ARG)>::value, | 235 COMPILE_ASSERT(!IsMethod::value || !is_array<P$(BOUND_ARG)>::value, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 | 272 |
| 229 ]] | 273 ]] |
| 230 }; | 274 }; |
| 231 | 275 |
| 232 ]] $$ for BOUND | 276 ]] $$ for BOUND |
| 233 | 277 |
| 234 } // namespace internal | 278 } // namespace internal |
| 235 } // namespace base | 279 } // namespace base |
| 236 | 280 |
| 237 #endif // BASE_BIND_INTERNAL_H_ | 281 #endif // BASE_BIND_INTERNAL_H_ |
| OLD | NEW |