1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This defines a set of argument wrappers and related factory methods that
6 // can be used specify the refcounting and reference semantics of arguments
7 // that are bound by the Bind() function in base/bind.h.
9 // The public functions are base::Unretained() and base::ConstRef().
10 // Unretained() allows Bind() to bind a non-refcounted class.
11 // ConstRef() allows binding a constant reference to an argument rather
15 // EXAMPLE OF Unretained():
19 // void func() { cout << "Foo:f" << endl;
22 // // In some function somewhere.
24 // Callback<void(void)> foo_callback =
25 // Bind(&Foo::func, Unretained(&foo));
26 // foo_callback.Run(); // Prints "Foo:f".
28 // Without the Unretained() wrapper on |&foo|, the above call would fail
29 // to compile because Foo does not support the AddRef() and Release() methods.
32 // EXAMPLE OF ConstRef();
33 // void foo(int arg) { cout << arg << endl }
36 // Callback<void(void)> no_ref = Bind(&foo, n);
37 // Callback<void(void)> has_ref = Bind(&foo, ConstRef(n));
39 // no_ref.Run(); // Prints "1"
40 // has_ref.Run(); // Prints "1"
43 // no_ref.Run(); // Prints "1"
44 // has_ref.Run(); // Prints "2"
46 // Note that because ConstRef() takes a reference on |n|, |n| must outlive all
47 // its bound callbacks.
50 #ifndef BASE_BIND_HELPERS_H_
51 #define BASE_BIND_HELPERS_H_
54 #include "base/basictypes.h"
55 #include "base/template_util.h"
60 // Use the Substitution Failure Is Not An Error (SFINAE) trick to inspect T
61 // for the existence of AddRef() and Release() functions of the correct
64 // http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error
65 // http://stackoverflow.com/questions/257288/is-it-possible-to-write-a-c-template-to-check-for-a-functions-existence
66 // http://stackoverflow.com/questions/4358584/sfinae-approach-comparison
67 // http://stackoverflow.com/questions/1966362/sfinae-to-check-for-inherited-member-functions
69 // The last link in particular show the method used below.
71 // For SFINAE to work with inherited methods, we need to pull some extra tricks
72 // with multiple inheritance. In the more standard formulation, the overloads
75 // template <typename C>
76 // Yes NotTheCheckWeWant(Helper<&C::TargetFunc>*);
78 // template <typename C>
79 // No NotTheCheckWeWant(...);
81 // static const bool value = sizeof(NotTheCheckWeWant<T>(0)) == sizeof(Yes);
83 // The problem here is that template resolution will not match
84 // C::TargetFunc if TargetFunc does not exist directly in C. That is, if
85 // TargetFunc in inherited from an ancestor, &C::TargetFunc will not match,
86 // |value| will be false. This formulation only checks for whether or
87 // not TargetFunc exist directly in the class being introspected.
89 // To get around this, we play a dirty trick with multiple inheritance.
90 // First, We create a class BaseMixin that declares each function that we
91 // want to probe for. Then we create a class Base that inherits from both T
92 // (the class we wish to probe) and BaseMixin. Note that the function
93 // signature in BaseMixin does not need to match the signature of the function
94 // we are probing for; thus it's easiest to just use void(void).
96 // Now, if TargetFunc exists somewhere in T, then &Base::TargetFunc has an
97 // ambiguous resolution between BaseMixin and T. This lets us write the
100 // template <typename C>
101 // No GoodCheck(Helper<&C::TargetFunc>*);
103 // template <typename C>
104 // Yes GoodCheck(...);
106 // static const bool value = sizeof(GoodCheck<Base>(0)) == sizeof(Yes);
108 // Notice here that the variadic version of GoodCheck() returns Yes here
109 // instead of No like the previous one. Also notice that we calculate |value|
110 // by specializing GoodCheck() on Base instead of T.
112 // We've reversed the roles of the variadic, and Helper overloads.
113 // GoodCheck(Helper<&C::TargetFunc>*), when C = Base, fails to be a valid
114 // substitution if T::TargetFunc exists. Thus GoodCheck<Base>(0) will resolve
115 // to the variadic version if T has TargetFunc. If T::TargetFunc does not
116 // exist, then &C::TargetFunc is not ambiguous, and the overload resolution
117 // will prefer GoodCheck(Helper<&C::TargetFunc>*).
119 // This method of SFINAE will correctly probe for inherited names, but it cannot
120 // typecheck those names. It's still a good enough sanity check though.
122 // Works on gcc-4.2, gcc-4.4, and Visual Studio 2008.
124 // TODO(ajwong): Move to ref_counted.h or template_util.h when we've vetted
126 template <typename T
>
127 class SupportsAddRefAndRelease
{
136 struct Base
: public T
, public BaseMixin
{
139 template <void(BaseMixin::*)(void)> struct Helper
{};
141 template <typename C
>
142 static No
& Check(Helper
<&C::AddRef
>*, Helper
<&C::Release
>*);
145 static Yes
& Check(...);
148 static const bool value
= sizeof(Check
<Base
>(0,0)) == sizeof(Yes
);
152 // Helpers to assert that arguments of a recounted type are bound with a
154 template <bool IsClasstype
, typename T
>
155 struct UnsafeBindtoRefCountedArgHelper
: false_type
{
158 template <typename T
>
159 struct UnsafeBindtoRefCountedArgHelper
<true, T
>
160 : integral_constant
<bool, SupportsAddRefAndRelease
<T
>::value
> {
163 template <typename T
>
164 struct UnsafeBindtoRefCountedArg
: false_type
{
167 template <typename T
>
168 struct UnsafeBindtoRefCountedArg
<T
*>
169 : UnsafeBindtoRefCountedArgHelper
<is_class
<T
>::value
, T
> {
173 template <typename T
>
174 class UnretainedWrapper
{
176 explicit UnretainedWrapper(T
* o
) : obj_(o
) {}
177 T
* get() { return obj_
; }
182 template <typename T
>
183 class ConstRefWrapper
{
185 explicit ConstRefWrapper(const T
& o
) : ptr_(&o
) {}
186 const T
& get() { return *ptr_
; }
192 // Unwrap the stored parameters for the wrappers above.
193 template <typename T
>
194 T
Unwrap(T o
) { return o
; }
196 template <typename T
>
197 T
* Unwrap(UnretainedWrapper
<T
> unretained
) { return unretained
.get(); }
199 template <typename T
>
200 const T
& Unwrap(ConstRefWrapper
<T
> const_ref
) {
201 return const_ref
.get();
205 // Utility for handling different refcounting semantics in the Bind()
207 template <typename ref
, typename T
>
208 struct MaybeRefcount
;
210 template <typename T
>
211 struct MaybeRefcount
<base::false_type
, T
> {
212 static void AddRef(const T
&) {}
213 static void Release(const T
&) {}
216 template <typename T
, size_t n
>
217 struct MaybeRefcount
<base::false_type
, T
[n
]> {
218 static void AddRef(const T
*) {}
219 static void Release(const T
*) {}
222 template <typename T
>
223 struct MaybeRefcount
<base::true_type
, UnretainedWrapper
<T
> > {
224 static void AddRef(const UnretainedWrapper
<T
>&) {}
225 static void Release(const UnretainedWrapper
<T
>&) {}
228 template <typename T
>
229 struct MaybeRefcount
<base::true_type
, T
*> {
230 static void AddRef(T
* o
) { o
->AddRef(); }
231 static void Release(T
* o
) { o
->Release(); }
234 template <typename T
>
235 struct MaybeRefcount
<base::true_type
, const T
*> {
236 static void AddRef(const T
* o
) { o
->AddRef(); }
237 static void Release(const T
* o
) { o
->Release(); }
240 } // namespace internal
242 template <typename T
>
243 inline internal::UnretainedWrapper
<T
> Unretained(T
* o
) {
244 return internal::UnretainedWrapper
<T
>(o
);
247 template <typename T
>
248 inline internal::ConstRefWrapper
<T
> ConstRef(const T
& o
) {
249 return internal::ConstRefWrapper
<T
>(o
);
254 #endif // BASE_BIND_HELPERS_H_