Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(347)

Side by Side Diff: base/prebind_helpers.h

Issue 6109007: Unified callback system. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/base
Patch Set: more bits of cleanup Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
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 Prebind() function in base/prebind.h.
8 //
9 // The public functions are base::Unretained() and base::ConstRef().
10 // Unretained() allows Prebind() to bind a non-refcounted class. ConstRef()
11 // allows binding a constant reference to an argument rather than a copy.
12 //
13 //
14 // EXAMPLE OF Unretained():
15 //
16 // class Foo {
17 // public:
18 // void func() { cout << "Foo:f" << endl;
19 // };
20 //
21 // // In some function somewhere.
22 // Foo foo;
23 // Callback<void(void)> foo_callback = Prebind(&Foo::func, Unretained(&foo));
24 // foo_callback.Run(); // Prints "Foo:f".
25 //
26 // Without the Unretained() wrapper on |&foo|, the above call would fail to
27 // compile because Foo does not support the AddRef() and Release() methods.
28 //
29 //
30 // EXAMPLE OF ConstRef();
31 // void foo(int arg) { cout << arg << endl }
32 //
33 // int n = 1;
34 // Callback<void(void)> no_ref = Prebind(&foo, n);
35 // Callback<void(void)> has_ref = Prebind(&foo, ConstRef(n));
36 //
37 // no_ref.Run(); // Prints "1"
38 // has_ref.Run(); // Prints "1"
39 //
40 // n = 2;
41 // no_ref.Run(); // Prints "1"
42 // has_ref.Run(); // Prints "2"
43 //
44 // Note that because ConstRef() takes a reference on |n|, |n| must outlive all
45 // its bound callbacks.
46 //
47
48 #ifndef BASE_PREBIND_HELPERS_H_
49 #define BASE_PREBIND_HELPERS_H_
50 #pragma once
51
52 #include "base/basictypes.h"
53 #include "base/template_util.h"
54
55 namespace base {
56 namespace internal {
57
58 // Use the Substitution Failure Is Not An Error (SFINAE) trick to inspect T
59 // for the existance of AddRef() and Release() functions of the correct
60 // signature.
61 //
62 // http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error
63 // http://stackoverflow.com/questions/257288/is-it-possible-to-write-a-c-templat e-to-check-for-a-functions-existence
64 // http://stackoverflow.com/questions/4358584/sfinae-approach-comparison
65 // http://stackoverflow.com/questions/1966362/sfinae-to-check-for-inherited-memb er-functions
66 //
67 // The last link in particular show the method used below.
68 //
69 // For SFINAE to work with inherited methods, we need to pull some extra ticks
70 // with multiple inheritance. In the more standard formulation, the overloads
71 // of Check would be:
72 //
73 // template <typename C>
74 // Yes NotTheCheckWeWant(Helper<&C::TargetFunc>*);
75 //
76 // template <typename C>
77 // No NotTheCheckWeWant(...);
78 //
79 // static const bool value = sizeof(NotTheCheckWeWant<T>(0)) == sizeof(Yes);
80 //
81 // The problem here is that template resolution will not match
82 // C::TargetFunc if TargetFunc does not exist directly in C. That is, if
83 // TargetFunc in inherited from an ancestor, &C::TargetFunc will not match,
84 // |value| will be false. This formulation only checks for whether or
85 // not TargetFunc exist direclty in the class being introspected.
86 //
87 // To get around this, we play a dirty trick with multiple inheritance.
88 // First, We create a class BaseMixin that declares each function that we
89 // want to probe for. Then we create a class Base that inherits from both T
90 // (the class we wish to probe) and BaseMixin. Note that the function
91 // signature in BaseMixin does not need to match the signature of the function
92 // we are probing for; thus it's easiest to just use void(void).
93 //
94 // Now, if TargetFunc exists somewhere in T, then &Base::TargetFunc has an
95 // ambiguous resolution between BaseMixin and T. This lets us write the
96 // following:
97 //
98 // template <typename C>
99 // No GoodCheck(Helper<&C::TargetFunc>*);
100 //
101 // template <typename C>
102 // Yes GoodCheck(...);
103 //
104 // static const bool value = sizeof(GoodCheck<Base>(0)) == sizeof(Yes);
105 //
106 // Notice here that the variadic version of GoodCheck() returns Yes here
107 // instead of No like the previous one. Also notice that we calculate |value|
108 // by specializing GoodCheck() on Base instead of T.
109 //
110 // We've reverse the roles of the variadic, and the function using Helper.
111 // GoodCheck(Helper<&C::TargetFunc>*), when C = Base, fails to be a valid
112 // substituion if T::TargetFunc exists. Thus GoodCheck<Base>(0) will resolve
113 // to the variadic version if T has TargetFunc. If T::TargetFunc does not
114 // exist, then &C::TargetFunc is not ambiguous, and the overload resolution
115 // will prefer GoodCheck(Helper<&C::TargetFunc>*).
116 //
117 // This method of SFINAE will correctly probe for inerited names, but it cannot
118 // typecheck those names. It's still a good enough sanity check though.
119 //
120 // Works on gcc-4.2, gcc-4.4, and Visual Studio 2008.
121 template <typename T>
122 struct SupportsAddRefAndRelease {
123 typedef char Yes[1];
124 typedef char No[2];
125
126 struct BaseMixin {
127 void AddRef();
128 void Release();
129 };
130
131 struct Base : public T, public BaseMixin {
132 };
133
134 template <void(BaseMixin::*)(void)> struct Helper {};
135
136 template <typename C>
137 static No& Check(Helper<&C::AddRef>*, Helper<&C::Release>*);
138
139 template <typename >
140 static Yes& Check(...);
141
142 static bool const value = sizeof(Check<Base>(0,0)) == sizeof(Yes);
143 };
144
145 template <typename T>
146 class UnretainedWrapper {
147 public:
148 explicit UnretainedWrapper(T* o) : obj_(o) {}
149 T* get() { return obj_; }
150 private:
151 T* obj_;
152 };
153
154 template <typename T>
155 class ConstRefWrapper {
156 public:
157 explicit ConstRefWrapper(const T& o) : ptr_(&o) {}
158 const T& get() { return *ptr_; }
159 private:
160 const T* ptr_;
161 };
162
163
164 // Unwrap the stored parameters for the wrappers above.
165 template <typename T>
166 T Unwrap(T o) { return o; }
167
168 template <typename T>
169 T* Unwrap(UnretainedWrapper<T> unretained) { return unretained.get(); }
170
171 template <typename T>
172 const T& Unwrap(ConstRefWrapper<T> const_ref) { return const_ref.get(); }
173
174
175 // Utility for handling different refcounting semantics in the Prebind()
176 // function.
177 template <typename ref, typename O>
178 struct MaybeRefcount;
179
180 template <typename O>
181 struct MaybeRefcount<base::false_type, O> {
182 static void AddRef(const O&) {}
183 static void Release(const O&) {}
184 };
185
186 template <typename O, size_t n>
187 struct MaybeRefcount<base::false_type, O[n]> {
188 static void AddRef(const O*) {}
189 static void Release(const O*) {}
190 };
191
192 template <typename O>
193 struct MaybeRefcount<base::true_type, UnretainedWrapper<O> > {
194 static void AddRef(const UnretainedWrapper<O>&) {}
195 static void Release(const UnretainedWrapper<O>&) {}
196 };
197
198 template <typename O>
199 struct MaybeRefcount<base::true_type, O* > {
willchan no longer on Chromium 2011/02/07 20:51:48 Unnecessary whitespace after O*.
awong 2011/02/08 18:52:26 Done.
200 static void AddRef(O* o) { o->AddRef(); }
201 static void Release(O* o) { o->Release(); }
202 };
203
204 template <typename O>
205 struct MaybeRefcount<base::true_type, const O* > {
206 static void AddRef(const O* o) { o->AddRef(); }
207 static void Release(const O* o) { o->Release(); }
208 };
209
210
211 // This is a typetraits object that's used to convert an argument type into a
212 // type suitable for storage. In particular, it strips off references, and
213 // converts arrays to pointers.
214 //
215 // This array type becomes an issue because we are passing bound parameters by
216 // const reference. In this case, we end up passing an actual array type in the
217 // initializer list which C++ does not allow. This will break passing of
218 // C-string literals.
219 template <typename T>
220 struct PrebindType {
221 typedef T StorageType;
222 };
223
224 // This should almost be impossible to trigger unless someone manually
225 // specifies type of the prebind parameters. However, in case they do,
226 // this will guard against us accidentally storing a reference parameter.
227 template <typename T>
228 struct PrebindType<T&> {
229 typedef T StorageType;
230 };
231
232 // Note that for array types, we implicitly add a const in the conversion. This
233 // means that it is not possible to bind array arguments to functions that take
234 // a non-const pointer. Trying to specialize the template based on a "const
235 // T[n]" does not seem to match correctly, so we are stuck with this
236 // restriction.
237 template <typename T, size_t n>
238 struct PrebindType<T[n]> {
239 typedef const T* StorageType;
240 };
241
242 template <typename T>
243 struct PrebindType<T[]> {
244 typedef const T* StorageType;
245 };
246
247 } // namespace internal
248
249 template <typename T>
250 inline internal::UnretainedWrapper<T> Unretained(T* o) {
251 COMPILE_ASSERT(internal::SupportsAddRefAndRelease<T>::value == false,
willchan no longer on Chromium 2011/02/06 10:26:50 I'm trying to figure out if this is more desirable
awong 2011/02/08 18:52:26 So...I recall from the meeting about this a while
252 must_not_unretain_refcounted_object);
253 return internal::UnretainedWrapper<T>(o);
254 }
255
256 template <typename T>
257 inline internal::ConstRefWrapper<T> ConstRef(const T& o) {
258 return internal::ConstRefWrapper<T>(o);
259 }
260
261 } // namespace base
262
263 #endif // BASE_PREBIND_HELPERS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698