Index: base/bind_helpers.h |
diff --git a/base/bind_helpers.h b/base/bind_helpers.h |
index 07f11f98d2cde4e730b0c811e6b176dac1fa9ca7..24872df87184a7971d36b82e2f6c8f581391f8cc 100644 |
--- a/base/bind_helpers.h |
+++ b/base/bind_helpers.h |
@@ -199,21 +199,42 @@ struct UnsafeBindtoRefCountedArg<T*> |
template <typename T> |
class UnretainedWrapper { |
public: |
- explicit UnretainedWrapper(T* o) : obj_(o) {} |
- T* get() { return obj_; } |
+ explicit UnretainedWrapper(T* o) : ptr_(o) {} |
+ T* get() const { return ptr_; } |
private: |
- T* obj_; |
+ T* ptr_; |
}; |
template <typename T> |
class ConstRefWrapper { |
public: |
explicit ConstRefWrapper(const T& o) : ptr_(&o) {} |
- const T& get() { return *ptr_; } |
+ const T& get() const { return *ptr_; } |
private: |
const T* ptr_; |
}; |
+// An alternate implementation is to avoid the destructive copy, and instead |
+// specialize ParamTraits<> for OwnedWrapper<> to change the StorageType to |
+// a class that is essentially a scoped_ptr<>. |
+// |
+// This has the benefit though of leaving ParamTrais<> fully in |
+// callback_internal.h as well as avoiding type conversions during storage. |
+template <typename T> |
+class OwnedWrapper { |
+ public: |
+ explicit OwnedWrapper(T* o) : ptr_(o) {} |
+ ~OwnedWrapper() { delete ptr_; } |
+ T* get() const { return ptr_; } |
+ OwnedWrapper(const OwnedWrapper& other) { |
+ ptr_ = other.ptr_; |
+ other.ptr_ = NULL; |
+ } |
+ |
+ private: |
+ mutable T* ptr_; |
+}; |
+ |
// Unwrap the stored parameters for the wrappers above. |
template <typename T> |
@@ -233,6 +254,11 @@ T* Unwrap(const scoped_refptr<T>& o) { return o.get(); } |
template <typename T> |
const WeakPtr<T>& Unwrap(const WeakPtr<T>& o) { return o; } |
+template <typename T> |
+T* Unwrap(const OwnedWrapper<T>& o) { |
+ return o.get(); |
+} |
+ |
// Utility for handling different refcounting semantics in the Bind() |
// function. |
template <typename IsMethod, typename T> |
@@ -251,15 +277,21 @@ struct MaybeRefcount<base::false_type, T[n]> { |
}; |
template <typename T> |
+struct MaybeRefcount<base::true_type, T*> { |
+ static void AddRef(T* o) { o->AddRef(); } |
+ static void Release(T* o) { o->Release(); } |
+}; |
+ |
+template <typename T> |
struct MaybeRefcount<base::true_type, UnretainedWrapper<T> > { |
static void AddRef(const UnretainedWrapper<T>&) {} |
static void Release(const UnretainedWrapper<T>&) {} |
}; |
template <typename T> |
-struct MaybeRefcount<base::true_type, T*> { |
- static void AddRef(T* o) { o->AddRef(); } |
- static void Release(T* o) { o->Release(); } |
+struct MaybeRefcount<base::true_type, OwnedWrapper<T> > { |
+ static void AddRef(const OwnedWrapper<T>&) {} |
+ static void Release(const OwnedWrapper<T>&) {} |
}; |
// No need to additionally AddRef() and Release() since we are storing a |
@@ -299,6 +331,11 @@ inline internal::ConstRefWrapper<T> ConstRef(const T& o) { |
return internal::ConstRefWrapper<T>(o); |
} |
+template <typename T> |
+inline internal::OwnedWrapper<T> Owned(T* o) { |
darin (slow to review)
2011/10/11 05:13:50
how about some documentation? i'm concerned that
awong
2011/10/11 17:24:27
This is the start of concern? Or are you concerne
|
+ return internal::OwnedWrapper<T>(o); |
+} |
+ |
template <typename R> |
Closure IgnoreReturn(Callback<R(void)> callback) { |
return Bind(&internal::VoidReturnAdapter<R>, callback); |