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-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 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 tricks 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 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>*). 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 // 124 // TODO(ajwong): Move to ref_counted.h or template_util.h when we've vetted 125 // this works well. 126 template <typename T> 127 class SupportsAddRefAndRelease { 128 typedef char Yes[1]; 129 typedef char No[2]; 130 131 struct BaseMixin { 132 void AddRef(); 133 void Release(); 134 }; 135 136 // MSVC warns when you try to use Base if T has a private destructor, the 137 // common pattern for refcounted types. It does this even though no attempt to 138 // instantiate Base is made. We disable the warning for this definition. 139 #if defined(OS_WIN) 140 #pragma warning(disable:4624) 141 #endif 142 struct Base : public T, public BaseMixin { 143 }; 144 #if defined(OS_WIN) 145 #pragma warning(default:4624) 146 #endif 147 148 template <void(BaseMixin::*)(void)> struct Helper {}; 149 150 template <typename C> 151 static No& Check(Helper<&C::AddRef>*, Helper<&C::Release>*); 152 153 template <typename > 154 static Yes& Check(...); 155 156 public: 157 static const bool value = sizeof(Check<Base>(0,0)) == sizeof(Yes); 158 }; 159 160 161 // Helpers to assert that arguments of a recounted type are bound with a 162 // scoped_refptr. 163 template <bool IsClasstype, typename T> 164 struct UnsafeBindtoRefCountedArgHelper : false_type { 165 }; 166 167 template <typename T> 168 struct UnsafeBindtoRefCountedArgHelper<true, T> 169 : integral_constant<bool, SupportsAddRefAndRelease<T>::value> { 170 }; 171 172 template <typename T> 173 struct UnsafeBindtoRefCountedArg : false_type { 174 }; 175 176 template <typename T> 177 struct UnsafeBindtoRefCountedArg<T*> 178 : UnsafeBindtoRefCountedArgHelper<is_class<T>::value, T> { 179 }; 180 181 182 template <typename T> 183 class UnretainedWrapper { 184 public: 185 explicit UnretainedWrapper(T* o) : obj_(o) {} 186 T* get() { return obj_; } 187 private: 188 T* obj_; 189 }; 190 191 template <typename T> 192 class ConstRefWrapper { 193 public: 194 explicit ConstRefWrapper(const T& o) : ptr_(&o) {} 195 const T& get() { return *ptr_; } 196 private: 197 const T* ptr_; 198 }; 199 200 201 // Unwrap the stored parameters for the wrappers above. 202 template <typename T> 203 T Unwrap(T o) { return o; } 204 205 template <typename T> 206 T* Unwrap(UnretainedWrapper<T> unretained) { return unretained.get(); } 207 208 template <typename T> 209 const T& Unwrap(ConstRefWrapper<T> const_ref) { 210 return const_ref.get(); 211 } 212 213 214 // Utility for handling different refcounting semantics in the Bind() 215 // function. 216 template <typename ref, typename T> 217 struct MaybeRefcount; 218 219 template <typename T> 220 struct MaybeRefcount<base::false_type, T> { 221 static void AddRef(const T&) {} 222 static void Release(const T&) {} 223 }; 224 225 template <typename T, size_t n> 226 struct MaybeRefcount<base::false_type, T[n]> { 227 static void AddRef(const T*) {} 228 static void Release(const T*) {} 229 }; 230 231 template <typename T> 232 struct MaybeRefcount<base::true_type, UnretainedWrapper<T> > { 233 static void AddRef(const UnretainedWrapper<T>&) {} 234 static void Release(const UnretainedWrapper<T>&) {} 235 }; 236 237 template <typename T> 238 struct MaybeRefcount<base::true_type, T*> { 239 static void AddRef(T* o) { o->AddRef(); } 240 static void Release(T* o) { o->Release(); } 241 }; 242 243 template <typename T> 244 struct MaybeRefcount<base::true_type, const T*> { 245 static void AddRef(const T* o) { o->AddRef(); } 246 static void Release(const T* o) { o->Release(); } 247 }; 248 249 } // namespace internal 250 251 template <typename T> 252 inline internal::UnretainedWrapper<T> Unretained(T* o) { 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_BIND_HELPERS_H_ 264