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