From 2284b517e15c7397d1271cb948fb1d24fce24841 Mon Sep 17 00:00:00 2001 From: Alan Conway Date: Tue, 19 Feb 2008 22:58:41 +0000 Subject: sys::RefCountedMap - reference-counted weak map of reference-counted objects. Ensures objects are atomically deleted and removed from the map. git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@629263 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/src/qpid/RefCounted.h | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'cpp/src/qpid/RefCounted.h') diff --git a/cpp/src/qpid/RefCounted.h b/cpp/src/qpid/RefCounted.h index bcef69d21e..c325b204d4 100644 --- a/cpp/src/qpid/RefCounted.h +++ b/cpp/src/qpid/RefCounted.h @@ -39,10 +39,9 @@ class AbstractRefCounted { }; /** - * Reference-counted virtual base class. + * Reference-counted base class. */ -class RefCounted : public AbstractRefCounted -{ +class RefCounted : public AbstractRefCounted { public: RefCounted() {} virtual void addRef() const { ++count; } @@ -53,12 +52,26 @@ class RefCounted : public AbstractRefCounted // Copy/assign do not copy refcounts. RefCounted(const RefCounted&) : AbstractRefCounted() {} RefCounted& operator=(const RefCounted&) { return *this; } - virtual void released() const { delete this; } + virtual void released() const { assert(count==0); delete this; } - private: mutable sys::AtomicCount count; }; +/** + * Reference-counted member of a reference-counted parent class. + * Delegates reference counts to the parent so that the parent is + * deleted only when there are no references to the parent or any of + * its children. + */ +struct RefCountedChild : public AbstractRefCounted { + protected: + AbstractRefCounted& parent; + RefCountedChild(AbstractRefCounted& parent_) : parent(parent_) {} + public: + void addRef() const { parent.addRef(); } + void release() const { parent.release(); } +}; + using boost::intrusive_ptr; } // namespace qpid -- cgit v1.2.1