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

Unified Diff: base/ref_counted.h

Issue 338065: Add the ability for objects which derive from RefCountedThreadSafe to specify... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/chrome_thread.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/ref_counted.h
===================================================================
--- base/ref_counted.h (revision 30647)
+++ base/ref_counted.h (working copy)
@@ -93,6 +93,21 @@
DISALLOW_COPY_AND_ASSIGN(RefCounted<T>);
};
+// Forward declaration.
+template <class T, typename Traits> class RefCountedThreadSafe;
+
+// Default traits for RefCountedThreadSafe<T>. Deletes the object when its ref
+// count reaches 0. Overload to delete it on a different thread etc.
+template<typename T>
+struct DefaultRefCountedThreadSafeTraits {
+ static void Destruct(T* x) {
+ // Delete through RefCountedThreadSafe to make child classes only need to be
+ // friend with RefCountedThreadSafe instead of this struct, which is an
+ // implementation detail.
+ RefCountedThreadSafe<T, DefaultRefCountedThreadSafeTraits>::DeleteInternal(x);
+ }
+};
+
//
// A thread-safe variant of RefCounted<T>
//
@@ -100,7 +115,12 @@
// ...
// };
//
-template <class T>
+// If you're using the default trait, then you may choose to add compile time
+// asserts that no one else is deleting your object. i.e.
+// private:
+// friend struct base::RefCountedThreadSafe<MyFoo>;
+// ~MyFoo();
+template <class T, typename Traits = DefaultRefCountedThreadSafeTraits<T> >
class RefCountedThreadSafe : public subtle::RefCountedThreadSafeBase {
public:
RefCountedThreadSafe() { }
@@ -112,12 +132,15 @@
void Release() {
if (subtle::RefCountedThreadSafeBase::Release()) {
- delete static_cast<T*>(this);
+ Traits::Destruct(static_cast<T*>(this));
}
}
private:
- DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafe<T>);
+ friend struct DefaultRefCountedThreadSafeTraits<T>;
+ static void DeleteInternal(T* x) { delete x; }
+
+ DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafe);
};
//
« no previous file with comments | « no previous file | chrome/browser/chrome_thread.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698