diff options
| author | Alan Conway <aconway@apache.org> | 2008-02-19 22:58:41 +0000 |
|---|---|---|
| committer | Alan Conway <aconway@apache.org> | 2008-02-19 22:58:41 +0000 |
| commit | 2284b517e15c7397d1271cb948fb1d24fce24841 (patch) | |
| tree | 47fd35abcd32d36b2203719c8afb4a8bcd1850d1 /cpp/src/qpid/RefCounted.h | |
| parent | f9631361fc24787ebe785060b9554a92b90a60d3 (diff) | |
| download | qpid-python-2284b517e15c7397d1271cb948fb1d24fce24841.tar.gz | |
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
Diffstat (limited to 'cpp/src/qpid/RefCounted.h')
| -rw-r--r-- | cpp/src/qpid/RefCounted.h | 23 |
1 files changed, 18 insertions, 5 deletions
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 |
