From: <sv...@ww...> - 2004-06-21 06:41:49
|
Author: mkrose Date: 2004-06-20 23:41:43 -0700 (Sun, 20 Jun 2004) New Revision: 1053 Modified: trunk/CSP/SimData/CHANGES.current trunk/CSP/SimData/Include/SimData/Ref.h Log: Refactored Referenced class to take a counter type as a template parameter; in anticipation of adding thread-safe reference counting. Browse at: https://www.zerobar.net/viewcvs/viewcvs.cgi?view=rev&rev=1053 Modified: trunk/CSP/SimData/CHANGES.current =================================================================== --- trunk/CSP/SimData/CHANGES.current 2004-06-21 06:38:27 UTC (rev 1052) +++ trunk/CSP/SimData/CHANGES.current 2004-06-21 06:41:43 UTC (rev 1053) @@ -4,6 +4,10 @@ 2004-06-20: onsight * Doxygen and copyright notice updates. + * Refactored Referenced class to a templated base class, allowing + different counter types to be used in anticipation of adding + thread-safe reference counting. + 2004-06-16: onsight * Doxygen cleanups. Modified: trunk/CSP/SimData/Include/SimData/Ref.h =================================================================== --- trunk/CSP/SimData/Include/SimData/Ref.h 2004-06-21 06:38:27 UTC (rev 1052) +++ trunk/CSP/SimData/Include/SimData/Ref.h 2004-06-21 06:41:43 UTC (rev 1053) @@ -47,7 +47,6 @@ NAMESPACE_SIMDATA -class Referenced; class ReferencePointer; class LinkBase; @@ -61,20 +60,21 @@ * * @author Mark Rose <mr...@st...> */ -class SIMDATA_EXPORT Referenced: public NonCopyable { +template <typename COUNTER> +class SIMDATA_EXPORT ReferencedBase: public NonCopyable { template <class T> friend class Ref; friend class ReferencePointer; protected: - Referenced(): __count(0) { - //SIMDATA_LOG(LOG_ALL, LOG_ERROR, "Referenced(" << this << ")"); + ReferencedBase(): __count(0) { + //SIMDATA_LOG(LOG_ALL, LOG_ERROR, "ReferencedBase(" << this << ")"); } - virtual ~Referenced() { - //SIMDATA_LOG(LOG_ALL, LOG_ERROR, "~Referenced(" << this << ", " << __count << ")"); + virtual ~ReferencedBase() { + //SIMDATA_LOG(LOG_ALL, LOG_ERROR, "~ReferencedBase(" << this << ", " << __count << ")"); if (__count != 0) { - SIMDATA_LOG(LOG_ALL, LOG_ERROR, "simdata::Referenced(" << std::hex << int(this) << ") deleted with non-zero reference count (" << __count << "): memory corruption possible."); + SIMDATA_LOG(LOG_ALL, LOG_ERROR, "simdata::ReferencedBase(" << std::hex << int(this) << ") deleted with non-zero reference count (" << __count << "): memory corruption possible."); } } @@ -86,13 +86,20 @@ inline void _decref() const { //SIMDATA_LOG(LOG_ALL, LOG_ERROR, "_decref(" << this << ", " << __count << ")"); assert(__count > 0); - if (0 == --__count) delete this; + if (!--__count) delete this; } - inline unsigned _count() const { return __count; } - mutable unsigned __count; + inline int _count() const { return static_cast<int>(__count); } + mutable COUNTER __count; }; +/** Base class for referenced counted objects that are used within a single + * thread. <b>Not thread-safe</b>, use ThreadSafeReferenced for references + * that are shared between threads. + */ +typedef ReferencedBase<int> Referenced; + + /** Reference counting smart-pointer. * * Reference counting smart-pointer for use with simdata::Referenced @@ -104,7 +111,7 @@ * @author Mark Rose <mr...@st...> */ template<class CLASS> -class Ref: protected HasBase<CLASS, Referenced> { +class Ref { public: typedef std::vector< Ref<CLASS> > vector; typedef std::list< Ref<CLASS> > list; @@ -117,24 +124,24 @@ /** Create a null reference. */ - Ref(): HasBase<CLASS, Referenced>(), _reference(0) { } + Ref(): _reference(0) { } /** Create a new reference. */ - Ref(CLASS* ptr): HasBase<CLASS, Referenced>(), _reference(ptr) { + Ref(CLASS* ptr): _reference(ptr) { if (_reference) _reference->_incref(); } /** Light-weight copy with reference counting. */ - Ref(LinkBase const & r): HasBase<CLASS, Referenced>(), _reference(0) { + Ref(LinkBase const & r): _reference(0) { _rebind(r._get()); } /** Light-weight copy with reference counting. */ template <typename Q> - Ref(Ref<Q> const & r): HasBase<CLASS, Referenced>(), _reference(0) { + Ref(Ref<Q> const & r): _reference(0) { Q *rp = r.get(); if (rp != 0) { //rp->_incref(); @@ -145,14 +152,14 @@ /** Light-weight copy with reference counting. */ - Ref(Ref const & r): HasBase<CLASS, Referenced>(), _reference(r._reference) { + Ref(Ref const & r): _reference(r._reference) { if (_reference) _reference->_incref(); } /** Decrement the reference count, and potentially destroy * the referenced object. */ - ~Ref() { + ~Ref() { _unbind(); } @@ -263,8 +270,10 @@ } /** Comparison with other simdata pointers. + * XXX the base class NonCopyable is used here instead of CLASS since + * swig generates bad code if CLASS is const (const const). */ - inline bool operator==(Referenced const * p) const { + inline bool operator==(NonCopyable const * p) const { return _reference == p; } @@ -276,8 +285,10 @@ } /** Comparison with other simdata pointers. + * XXX the base class NonCopyable is used here instead of CLASS since + * swig generates bad code if CLASS is const (const const). */ - inline bool operator!=(Referenced const * p) const { + inline bool operator!=(NonCopyable const * p) const { return _reference != p; } |