summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Mathias Agopian <mathias@google.com> 2011-05-19 18:03:31 -0700
committer Mathias Agopian <mathias@google.com> 2011-05-19 19:40:01 -0700
commit20aeb1caa4e2232153d3a59923722c19a3563a78 (patch)
tree1e933bee5fbb9c1927204a06145473808b71b0d2
parent18c7868cecfe3b1c18113d13bda4e853d43e9fb1 (diff)
RefBase subclasses can now decide how they want to be destroyed.
This adds a destroy() virtual on RefBase which sublasses can implement. destroy() is called in lieu of the destructor whenthe last strong ref goes away.
-rw-r--r--include/utils/RefBase.h10
-rw-r--r--libs/utils/RefBase.cpp6
2 files changed, 13 insertions, 3 deletions
diff --git a/include/utils/RefBase.h b/include/utils/RefBase.h
index f355087713..412622596c 100644
--- a/include/utils/RefBase.h
+++ b/include/utils/RefBase.h
@@ -51,7 +51,6 @@ inline bool operator _op_ (const U* o) const { \
}
// ---------------------------------------------------------------------------
-
class ReferenceMover;
class ReferenceConverterBase {
public:
@@ -120,7 +119,14 @@ public:
protected:
RefBase();
virtual ~RefBase();
-
+
+ // called when the last reference goes away. this is responsible for
+ // calling the destructor. The default implementation just does
+ // "delete this;".
+ // Make sure to never acquire a strong reference from this function. The
+ // same restrictions than for destructors apply.
+ virtual void destroy() const;
+
//! Flags for extendObjectLifetime()
enum {
OBJECT_LIFETIME_WEAK = 0x0001,
diff --git a/libs/utils/RefBase.cpp b/libs/utils/RefBase.cpp
index 2034486aac..9f55a71579 100644
--- a/libs/utils/RefBase.cpp
+++ b/libs/utils/RefBase.cpp
@@ -345,6 +345,10 @@ void RefBase::incStrong(const void* id) const
const_cast<RefBase*>(this)->onFirstRef();
}
+void RefBase::destroy() const {
+ delete this;
+}
+
void RefBase::decStrong(const void* id) const
{
weakref_impl* const refs = mRefs;
@@ -357,7 +361,7 @@ void RefBase::decStrong(const void* id) const
if (c == 1) {
const_cast<RefBase*>(this)->onLastStrongRef(id);
if ((refs->mFlags&OBJECT_LIFETIME_WEAK) != OBJECT_LIFETIME_WEAK) {
- delete this;
+ destroy();
}
}
refs->decWeak(id);