# ReferenceCounted.hpp

Go to the documentation of this file.

    00001 /*
    00002  * Copyright (c) 2001-2006
    00003  *     DecisionSoft Limited. All rights reserved.
    00004  * Copyright (c) 2004-2006
    00005  *     Progress Software Corporation. All rights reserved.
    00006  * Copyright (c) 2004-2006
    00007  *     Oracle. All rights reserved.
    00008  *
    00009  * See the file LICENSE for redistribution information.
    00010  *
    00011  * $Id: ReferenceCounted.hpp,v 1.7 2006/11/01 16:37:12 jpcs Exp $
    00012  */
    00013
    00014 #ifndef _REFERENCECOUNTED_HPP
    00015 #define _REFERENCECOUNTED_HPP
    00016
    00017 #include <xqilla/framework/XQillaExport.hpp>
    00018 #include <xercesc/framework/MemoryManager.hpp>
    00019
    00020 // for null RefCountPointer instances
    00021 #define NULLRCP ((void *)0)
    00022
    00024 class XQILLA_API ReferenceCounted
    00025 {
    00026 public:
    00027   ReferenceCounted()
    00028     : _ref_count(0) {}
    00029   virtual ~ReferenceCounted() {}
    00030
    00032   void incrementRefCount() const
    00033   {
    00034     ++const_cast<unsigned int&>(_ref_count);
    00035   }
    00036
    00038   virtual void decrementRefCount() const
    00039   {
    00040     if(--const_cast<unsigned int&>(_ref_count) == 0) {
    00041       delete this;
    00042     }
    00043   }
    00044
    00045 protected:
    00046   unsigned int _ref_count; // mutable
    00047 };
    00048
    00050 template<class T> class RefCountPointer
    00051 {
    00052 public:
    00053   RefCountPointer(T *p = 0) : _p(p)
    00054   {
    00055     if(_p != 0) _p->incrementRefCount();
    00056   }
    00057
    00058   template<class T2> RefCountPointer(const RefCountPointer<T2> &o) : _p((T*)(T2*)o)
    00059   {
    00060     if(_p != 0) _p->incrementRefCount();
    00061   }
    00062
    00063   RefCountPointer(const RefCountPointer<T> &o) : _p(o._p)
    00064   {
    00065     if(_p != 0) _p->incrementRefCount();
    00066   }
    00067
    00068   RefCountPointer &operator=(const RefCountPointer<T> &o)
    00069   {
    00070     if(_p != o._p) {
    00071       if(_p != 0) _p->decrementRefCount();
    00072       _p = o._p;
    00073       if(_p != 0) _p->incrementRefCount();
    00074     }
    00075     return *this;
    00076   }
    00077
    00078   ~RefCountPointer()
    00079   {
    00080     if(_p != 0) _p->decrementRefCount();
    00081   }
    00082
    00083   T *operator->() const
    00084   {
    00085     return _p;
    00086   }
    00087
    00088   operator T*() const
    00089   {
    00090     return _p;
    00091   }
    00092
    00093   T *get() const
    00094   {
    00095     return _p;
    00096   }
    00097
    00098   bool isNull() const
    00099   {
    00100     return (_p == 0);
    00101   }
    00102
    00103   bool notNull() const
    00104   {
    00105     return (_p != 0);
    00106   }
    00107
    00108 protected:
    00109   T *_p;
    00110 };
    00111
    00112 template<class T1, class T2>
    00113 inline bool operator==(const RefCountPointer<T1> &a, const RefCountPointer<T2> &b)
    00114 {
    00115   return (void*)(T1*)a == (void*)(T2*)b;
    00116 }
    00117
    00118 template<class T1, class T2>
    00119 inline bool operator!=(const RefCountPointer<T1> &a, const RefCountPointer<T2> &b)
    00120 {
    00121   return (void*)(T1*)a != (void*)(T2*)b;
    00122 }
    00123
    00124 template<class T>
    00125 inline bool operator==(const RefCountPointer<T> &a, void *b)
    00126 {
    00127   return (T*)a == (T*)b;
    00128 }
    00129
    00130 template<class T>
    00131 inline bool operator!=(const RefCountPointer<T> &a, void *b)
    00132 {
    00133   return (T*)a != (T*)b;
    00134 }
    00135
    00136 #endif