diff options
Diffstat (limited to 'cpp/src/qpid/RefCounted.h')
-rw-r--r-- | cpp/src/qpid/RefCounted.h | 64 |
1 files changed, 28 insertions, 36 deletions
diff --git a/cpp/src/qpid/RefCounted.h b/cpp/src/qpid/RefCounted.h index c325b204d4..d67f6c31db 100644 --- a/cpp/src/qpid/RefCounted.h +++ b/cpp/src/qpid/RefCounted.h @@ -10,9 +10,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -22,39 +22,28 @@ * */ -#include "qpid/sys/AtomicCount.h" - -#include <boost/intrusive_ptr.hpp> - +#include <boost/utility.hpp> +#include <boost/detail/atomic_count.hpp> namespace qpid { -/** Abstract interface for reference counted objects */ -class AbstractRefCounted { - public: - virtual void addRef() const=0; - virtual void release() const=0; - protected: - virtual ~AbstractRefCounted() {} -}; - /** * Reference-counted base class. + * Note: this class isn't copyable - you must copy the intrusive_ptr that points + * to the class that has mixed this in not the class itself (as that would sidestep + * the reference counting) */ -class RefCounted : public AbstractRefCounted { - public: - RefCounted() {} - virtual void addRef() const { ++count; } - virtual void release() const { if (--count==0) released(); } +class RefCounted : boost::noncopyable { + mutable boost::detail::atomic_count count; - protected: - virtual ~RefCounted() {}; - // Copy/assign do not copy refcounts. - RefCounted(const RefCounted&) : AbstractRefCounted() {} - RefCounted& operator=(const RefCounted&) { return *this; } - virtual void released() const { assert(count==0); delete this; } +public: + RefCounted() : count(0) {} + void addRef() const { ++count; } + void release() const { if (--count==0) delete this; } + long refCount() { return count; } - mutable sys::AtomicCount count; +protected: + virtual ~RefCounted() {}; }; /** @@ -62,24 +51,27 @@ class RefCounted : public AbstractRefCounted { * 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. + * TODO: Delete this class if it's unused as I don't think this class makes much sense: */ -struct RefCountedChild : public AbstractRefCounted { - protected: - AbstractRefCounted& parent; - RefCountedChild(AbstractRefCounted& parent_) : parent(parent_) {} - public: +struct RefCountedChild { + RefCounted& parent; + +protected: + RefCountedChild(RefCounted& parent_) : parent(parent_) {} + +public: void addRef() const { parent.addRef(); } void release() const { parent.release(); } }; -using boost::intrusive_ptr; - } // namespace qpid // intrusive_ptr support. namespace boost { -inline void intrusive_ptr_add_ref(const qpid::AbstractRefCounted* p) { p->addRef(); } -inline void intrusive_ptr_release(const qpid::AbstractRefCounted* p) { p->release(); } +inline void intrusive_ptr_add_ref(const qpid::RefCounted* p) { p->addRef(); } +inline void intrusive_ptr_release(const qpid::RefCounted* p) { p->release(); } +inline void intrusive_ptr_add_ref(const qpid::RefCountedChild* p) { p->addRef(); } +inline void intrusive_ptr_release(const qpid::RefCountedChild* p) { p->release(); } } |