Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 Bind() function in base/bind.h. | |
| 8 // | |
| 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 | |
| 12 // than a copy. | |
| 13 // | |
| 14 // | |
| 15 // EXAMPLE OF Unretained(): | |
| 16 // | |
| 17 // class Foo { | |
| 18 // public: | |
| 19 // void func() { cout << "Foo:f" << endl; | |
| 20 // }; | |
| 21 // | |
| 22 // // In some function somewhere. | |
| 23 // Foo foo; | |
| 24 // Callback<void(void)> foo_callback = | |
| 25 // Bind(&Foo::func, Unretained(&foo)); | |
| 26 // foo_callback.Run(); // Prints "Foo:f". | |
| 27 // | |
| 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. | |
| 30 // | |
| 31 // | |
| 32 // EXAMPLE OF ConstRef(); | |
| 33 // void foo(int arg) { cout << arg << endl } | |
| 34 // | |
| 35 // int n = 1; | |
| 36 // Callback<void(void)> no_ref = Bind(&foo, n); | |
| 37 // Callback<void(void)> has_ref = Bind(&foo, ConstRef(n)); | |
| 38 // | |
| 39 // no_ref.Run(); // Prints "1" | |
| 40 // has_ref.Run(); // Prints "1" | |
| 41 // | |
| 42 // n = 2; | |
| 43 // no_ref.Run(); // Prints "1" | |
| 44 // has_ref.Run(); // Prints "2" | |
| 45 // | |
| 46 // Note that because ConstRef() takes a reference on |n|, |n| must outlive all | |
| 47 // its bound callbacks. | |
| 48 // | |
| 49 | |
| 50 #ifndef BASE_BIND_HELPERS_H_ | |
| 51 #define BASE_BIND_HELPERS_H_ | |
| 52 #pragma once | |
| 53 | |
| 54 #include "base/basictypes.h" | |
| 55 #include "base/template_util.h" | |
| 56 | |
| 57 namespace base { | |
| 58 namespace internal { | |
| 59 | |
| 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 | |
| 62 // signature. | |
| 63 // | |
| 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-templat e-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-memb er-functions | |
| 68 // | |
| 69 // The last link in particular show the method used below. | |
| 70 // | |
| 71 // For SFINAE to work with inherited methods, we need to pull some extra ticks | |
| 72 // with multiple inheritance. In the more standard formulation, the overloads | |
| 73 // of Check would be: | |
| 74 // | |
| 75 // template <typename C> | |
| 76 // Yes NotTheCheckWeWant(Helper<&C::TargetFunc>*); | |
| 77 // | |
| 78 // template <typename C> | |
| 79 // No NotTheCheckWeWant(...); | |
| 80 // | |
| 81 // static const bool value = sizeof(NotTheCheckWeWant<T>(0)) == sizeof(Yes); | |
| 82 // | |
| 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. | |
| 88 // | |
| 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). | |
| 95 // | |
| 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 | |
| 98 // following: | |
| 99 // | |
| 100 // template <typename C> | |
| 101 // No GoodCheck(Helper<&C::TargetFunc>*); | |
| 102 // | |
| 103 // template <typename C> | |
| 104 // Yes GoodCheck(...); | |
| 105 // | |
| 106 // static const bool value = sizeof(GoodCheck<Base>(0)) == sizeof(Yes); | |
| 107 // | |
| 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. | |
| 111 // | |
| 112 // We've reverse the roles of the variadic, and the function using Helper. | |
| 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>*). | |
| 118 // | |
| 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. | |
| 121 // | |
| 122 // Works on gcc-4.2, gcc-4.4, and Visual Studio 2008. | |
| 123 template <typename T> | |
| 124 class SupportsAddRefAndRelease { | |
|
darin (slow to review)
2011/02/12 00:36:07
note: might be nice to move this to ref_counted.h
awong
2011/02/12 09:44:45
Added TODO to move it later.
| |
| 125 typedef char Yes[1]; | |
| 126 typedef char No[2]; | |
| 127 | |
| 128 struct BaseMixin { | |
| 129 void AddRef(); | |
| 130 void Release(); | |
| 131 }; | |
| 132 | |
| 133 struct Base : public T, public BaseMixin { | |
| 134 }; | |
| 135 | |
| 136 template <void(BaseMixin::*)(void)> struct Helper {}; | |
| 137 | |
| 138 template <typename C> | |
| 139 static No& Check(Helper<&C::AddRef>*, Helper<&C::Release>*); | |
| 140 | |
| 141 template <typename > | |
| 142 static Yes& Check(...); | |
| 143 | |
| 144 public: | |
| 145 static const bool value = sizeof(Check<Base>(0,0)) == sizeof(Yes); | |
| 146 }; | |
| 147 | |
| 148 | |
| 149 // Helpers to assert that arguments of a recounted type are bound with a | |
| 150 // scoped_refptr. | |
| 151 template <typename T> | |
| 152 struct UnsafeBindtoRefCountedArg { | |
| 153 static const bool value = false; | |
| 154 }; | |
| 155 | |
| 156 template <typename T> | |
| 157 struct UnsafeBindtoRefCountedArg<T*> { | |
| 158 static const bool value = SupportsAddRefAndRelease<T>::value; | |
| 159 }; | |
| 160 | |
| 161 | |
| 162 template <typename T> | |
| 163 class UnretainedWrapper { | |
| 164 public: | |
| 165 explicit UnretainedWrapper(T* o) : obj_(o) {} | |
| 166 T* get() { return obj_; } | |
| 167 private: | |
| 168 T* obj_; | |
| 169 }; | |
| 170 | |
| 171 template <typename T> | |
| 172 class ConstRefWrapper { | |
| 173 public: | |
| 174 explicit ConstRefWrapper(const T& o) : ptr_(&o) {} | |
| 175 const T& get() { return *ptr_; } | |
| 176 private: | |
| 177 const T* ptr_; | |
| 178 }; | |
| 179 | |
| 180 | |
| 181 // Unwrap the stored parameters for the wrappers above. | |
| 182 template <typename T> | |
| 183 T Unwrap(T o) { return o; } | |
| 184 | |
| 185 template <typename T> | |
| 186 T* Unwrap(UnretainedWrapper<T> unretained) { return unretained.get(); } | |
| 187 | |
| 188 template <typename T> | |
| 189 const T& Unwrap(ConstRefWrapper<T> const_ref) { | |
| 190 return const_ref.get(); | |
| 191 } | |
| 192 | |
| 193 | |
| 194 // Utility for handling different refcounting semantics in the Bind() | |
| 195 // function. | |
| 196 template <typename ref, typename O> | |
|
darin (slow to review)
2011/02/12 00:36:07
nit: "O" is not the best template parameter.
awong
2011/02/12 09:44:45
Done.
| |
| 197 struct MaybeRefcount; | |
| 198 | |
| 199 template <typename O> | |
| 200 struct MaybeRefcount<base::false_type, O> { | |
| 201 static void AddRef(const O&) {} | |
| 202 static void Release(const O&) {} | |
| 203 }; | |
| 204 | |
| 205 template <typename O, size_t n> | |
| 206 struct MaybeRefcount<base::false_type, O[n]> { | |
| 207 static void AddRef(const O*) {} | |
| 208 static void Release(const O*) {} | |
| 209 }; | |
| 210 | |
| 211 template <typename O> | |
| 212 struct MaybeRefcount<base::true_type, UnretainedWrapper<O> > { | |
| 213 static void AddRef(const UnretainedWrapper<O>&) {} | |
| 214 static void Release(const UnretainedWrapper<O>&) {} | |
| 215 }; | |
| 216 | |
| 217 template <typename O> | |
| 218 struct MaybeRefcount<base::true_type, O*> { | |
| 219 static void AddRef(O* o) { o->AddRef(); } | |
| 220 static void Release(O* o) { o->Release(); } | |
| 221 }; | |
| 222 | |
| 223 template <typename O> | |
| 224 struct MaybeRefcount<base::true_type, const O*> { | |
| 225 static void AddRef(const O* o) { o->AddRef(); } | |
| 226 static void Release(const O* o) { o->Release(); } | |
| 227 }; | |
| 228 | |
| 229 | |
| 230 // This is a typetraits object that's used to convert an argument type into a | |
| 231 // type suitable for storage. In particular, it strips off references, and | |
| 232 // converts arrays to pointers. | |
| 233 // | |
| 234 // This array type becomes an issue because we are passing bound parameters by | |
| 235 // const reference. In this case, we end up passing an actual array type in the | |
| 236 // initializer list which C++ does not allow. This will break passing of | |
| 237 // C-string literals. | |
| 238 template <typename T> | |
| 239 struct BindType { | |
| 240 typedef T StorageType; | |
| 241 }; | |
| 242 | |
| 243 // This should almost be impossible to trigger unless someone manually | |
| 244 // specifies type of the bind parameters. However, in case they do, | |
| 245 // this will guard against us accidentally storing a reference parameter. | |
| 246 template <typename T> | |
| 247 struct BindType<T&> { | |
| 248 typedef T StorageType; | |
| 249 }; | |
| 250 | |
| 251 // Note that for array types, we implicitly add a const in the conversion. This | |
| 252 // means that it is not possible to bind array arguments to functions that take | |
| 253 // a non-const pointer. Trying to specialize the template based on a "const | |
| 254 // T[n]" does not seem to match correctly, so we are stuck with this | |
| 255 // restriction. | |
| 256 template <typename T, size_t n> | |
| 257 struct BindType<T[n]> { | |
| 258 typedef const T* StorageType; | |
| 259 }; | |
| 260 | |
| 261 template <typename T> | |
| 262 struct BindType<T[]> { | |
| 263 typedef const T* StorageType; | |
| 264 }; | |
| 265 | |
| 266 } // namespace internal | |
| 267 | |
| 268 template <typename T> | |
| 269 inline internal::UnretainedWrapper<T> Unretained(T* o) { | |
| 270 COMPILE_ASSERT(internal::SupportsAddRefAndRelease<T>::value == false, | |
|
darin (slow to review)
2011/02/12 00:36:07
even though i might have argued for this in the pa
awong
2011/02/12 09:44:45
Removed.
| |
| 271 must_not_unretain_refcounted_object); | |
| 272 return internal::UnretainedWrapper<T>(o); | |
| 273 } | |
| 274 | |
| 275 template <typename T> | |
| 276 inline internal::ConstRefWrapper<T> ConstRef(const T& o) { | |
| 277 return internal::ConstRefWrapper<T>(o); | |
| 278 } | |
| 279 | |
| 280 } // namespace base | |
| 281 | |
| 282 #endif // BASE_BIND_HELPERS_H_ | |
| OLD | NEW |