Chromium Code Reviews| 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. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 // Though Bind()'s result is meant to be stored in a Callback<> type, it | 24 // Though Bind()'s result is meant to be stored in a Callback<> type, it |
| 25 // cannot actually return the exact type without requiring a large amount | 25 // cannot actually return the exact type without requiring a large amount |
| 26 // of extra template specializations. The problem is that in order to | 26 // of extra template specializations. The problem is that in order to |
| 27 // discern the correct specialization of Callback<>, Bind would need to | 27 // discern the correct specialization of Callback<>, Bind would need to |
| 28 // unwrap the function signature to determine the signature's arity, and | 28 // unwrap the function signature to determine the signature's arity, and |
| 29 // whether or not it is a method. | 29 // whether or not it is a method. |
| 30 // | 30 // |
| 31 // Each unique combination of (arity, function_type, num_prebound) where | 31 // Each unique combination of (arity, function_type, num_prebound) where |
| 32 // function_type is one of {function, method, const_method} would require | 32 // function_type is one of {function, method, const_method} would require |
| 33 // one specialization. We eventually have to do a similar number of | 33 // one specialization. We eventually have to do a similar number of |
| 34 // specializations anyways in the implementation (see the FunctionTraitsN, | 34 // specializations anyways in the implementation (see the Invoker<>, |
| 35 // classes). However, it is avoidable in Bind if we return the result | 35 // classes). However, it is avoidable in Bind if we return the result |
| 36 // via an indirection like we do below. | 36 // via an indirection like we do below. |
| 37 // | |
| 38 // TODO(ajwong): We might be able to avoid this now, but need to test. | |
| 39 // | |
| 40 // It is possible to move most of the COMPILE_ASSERT asserts into BindState<>, | |
| 41 // bit it feels a little nicer to have the asserts here so people do not | |
|
willchan no longer on Chromium
2011/11/10 19:33:39
s/bit/but/
awong
2011/11/11 02:17:57
Done.
| |
| 42 // need to crack open bind_internal.h. On the otherhand, it makes BindState | |
|
willchan no longer on Chromium
2011/11/10 19:33:39
s/otherhand/other hand/
awong
2011/11/11 02:17:57
Done.
| |
| 43 // harder to read. | |
| 37 | 44 |
| 38 namespace base { | 45 namespace base { |
| 39 | 46 |
| 40 $range BOUND 0..MAX_ARITY | 47 $range ARITY 0..MAX_ARITY |
| 41 $for BOUND [[ | 48 $for ARITY [[ |
| 42 $range BOUND_ARG 1..BOUND | 49 $range ARG 1..ARITY |
| 43 | 50 |
| 44 $if BOUND == 0 [[ | 51 template <typename Functor[[]] |
| 52 $if ARITY > 0 [[, ]] $for ARG , [[typename P$(ARG)]]> | |
| 53 internal::BindStateHolder< | |
| 54 internal::BindState< | |
| 55 typename internal::FunctorTraits<Functor>::RunnableType, | |
| 56 typename internal::FunctorTraits<Functor>::RunType, | |
| 57 void($for ARG , [[typename internal::CallbackParamTraits<P$(ARG)>::Stora geType]])> > | |
| 58 Bind(Functor functor | |
| 59 $if ARITY > 0 [[, ]] $for ARG , [[const P$(ARG)& p$(ARG)]]) { | |
| 60 // Typedefs for how to store and run the functor. | |
| 61 typedef typename internal::FunctorTraits<Functor>::RunnableType RunnableType; | |
| 62 typedef typename internal::FunctorTraits<Functor>::RunType RunType; | |
| 45 | 63 |
| 46 template <typename Sig> | 64 // Use RunnableType::RunType instead of RunType above because our |
|
willchan no longer on Chromium
2011/11/10 19:33:39
This is hard to grok. What's the difference betwee
awong
2011/11/11 02:17:57
Naming this is tricky.
RunnableType. Read the to
| |
| 47 internal::InvokerStorageHolder<internal::InvokerStorage0<Sig> > | 65 // checks should below for bound references need to know what the actual |
| 48 Bind(Sig f) { | 66 // functor is going to interpret the argument as. |
| 49 return internal::MakeInvokerStorageHolder( | 67 typedef internal::FunctionTraits<typename RunnableType::RunType> |
| 50 new internal::InvokerStorage0<Sig>(f)); | 68 BoundFunctorTraits; |
| 51 } | |
| 52 | 69 |
| 53 ]] $else [[ | 70 $if ARITY > 0 [[ |
| 54 | 71 |
| 55 template <typename Sig, $for BOUND_ARG , [[typename P$(BOUND_ARG)]]> | 72 // Do not allow binding a non-const reference parameter. Non-const reference |
| 56 internal::InvokerStorageHolder<internal::InvokerStorage$(BOUND)<Sig, | 73 // parameters are disallowed by the Google style guide. Also, binding a |
| 57 $for BOUND_ARG , [[P$(BOUND_ARG)]]> > | 74 // non-const reference parameter can make for subtle bugs because the |
| 58 Bind(Sig f, $for BOUND_ARG , [[const P$(BOUND_ARG)& p$(BOUND_ARG)]]) { | 75 // invoked function will receive a reference to the stored copy of the |
| 59 return internal::MakeInvokerStorageHolder( | 76 // argument and not the original. |
| 60 new internal::InvokerStorage$(BOUND)<Sig, [[]] | 77 COMPILE_ASSERT( |
| 61 $for BOUND_ARG , [[P$(BOUND_ARG)]]>( | 78 !($for ARG || [[ |
| 62 f, $for BOUND_ARG , [[p$(BOUND_ARG)]])); | 79 is_non_const_reference<typename BoundFunctorTraits::A$(ARG)Type>::value ]]), |
| 63 } | 80 do_not_bind_functions_with_nonconst_ref); |
| 64 | |
| 65 ]] | |
| 66 ]] $$ for BOUND | |
| 67 | |
| 68 // Specializations to allow binding all the free arguments in a | |
| 69 // pre-existing base::Callback<>. This does not give full support for | |
| 70 // currying, but is significantly simpler and addresses the use case | |
| 71 // where a base::Callback<> needs to be invoked on another context/thread. | |
| 72 $for BOUND [[ | |
| 73 $range BOUND_ARG 1..BOUND | |
| 74 $if BOUND != 0 [[ | |
| 75 | |
| 76 template <typename Sig, $for BOUND_ARG , [[typename P$(BOUND_ARG)]]> | |
| 77 base::Closure Bind(const base::Callback<Sig>& callback, [[]] | |
| 78 $for BOUND_ARG , [[const P$(BOUND_ARG)& p$(BOUND_ARG)]]) { | |
| 79 return base::Bind([[]] | |
| 80 &internal::BindMoreFunc$(BOUND)<Sig, $for BOUND_ARG , [[P$(BOUND_ARG)]]>, [[]] | |
| 81 callback, [[]] | |
| 82 $for BOUND_ARG , [[p$(BOUND_ARG)]]); | |
| 83 } | |
| 84 | 81 |
| 85 ]] | 82 ]] |
| 86 | 83 |
| 87 ]] $$ for BOUND | 84 |
| 85 $for ARG [[ | |
| 86 | |
| 87 | |
| 88 $if ARG == 1 [[ | |
| 89 // For methods, we need to be careful for parameter 1. We skip the | |
| 90 // scoped_refptr check because the binder itself takes care of this. We also | |
| 91 // disallow binding of an array as the method's target object. | |
| 92 COMPILE_ASSERT( | |
| 93 internal::HasIsMethodTag<RunnableType>::value || | |
| 94 !internal::NeedsScopedRefptrButGetsRawPtr<P$(ARG)>::value, | |
| 95 p$(ARG)_is_refcounted_type_and_needs_scoped_refptr); | |
| 96 COMPILE_ASSERT(!internal::HasIsMethodTag<RunnableType>::value || | |
| 97 !is_array<P$(ARG)>::value, | |
| 98 first_bound_argument_to_method_cannot_be_array); | |
| 99 ]] $else [[ | |
| 100 COMPILE_ASSERT(!internal::NeedsScopedRefptrButGetsRawPtr<P$(ARG)>::value, | |
| 101 p$(ARG)_is_refcounted_type_and_needs_scoped_refptr); | |
| 102 ]] $$ $if ARG | |
| 103 | |
| 104 ]] $$ $for ARG | |
| 105 | |
| 106 | |
| 107 return internal::MakeBindStateHolder( | |
| 108 new internal::BindState<RunnableType, RunType, [[]] | |
| 109 void($for ARG , [[typename internal::CallbackParamTraits<P$(ARG)>::StorageType]] )>( | |
| 110 internal::MakeRunnable(functor)[[]] | |
| 111 $if ARITY > 0 [[, ]] $for ARG , [[p$(ARG)]])); | |
| 112 } | |
| 113 | |
| 114 ]] $$ for ARITY | |
| 88 | 115 |
| 89 } // namespace base | 116 } // namespace base |
| 90 | 117 |
| 91 #endif // BASE_BIND_H_ | 118 #endif // BASE_BIND_H_ |
| OLD | NEW |