SourceForge.net Logo

ReferenceCounted.hpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2007
00003  *     DecisionSoft Limited. All rights reserved.
00004  * Copyright (c) 2004-2007
00005  *     Oracle. All rights reserved.
00006  *
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  * See the License for the specific language governing permissions and
00017  * limitations under the License.
00018  *
00019  * $Id: ReferenceCounted.hpp,v 1.8 2007/11/28 13:13:22 jpcs Exp $
00020  */
00021 
00022 #ifndef _REFERENCECOUNTED_HPP
00023 #define _REFERENCECOUNTED_HPP
00024 
00025 #include <xqilla/framework/XQillaExport.hpp>
00026 #include <xercesc/framework/MemoryManager.hpp>
00027 
00028 // for null RefCountPointer instances
00029 #define NULLRCP ((void *)0)
00030 
00032 class XQILLA_API ReferenceCounted
00033 {
00034 public:
00035   ReferenceCounted()
00036     : _ref_count(0) {}
00037   virtual ~ReferenceCounted() {}
00038 
00040   void incrementRefCount() const
00041   {
00042     ++const_cast<unsigned int&>(_ref_count);
00043   }
00044 
00046   virtual void decrementRefCount() const
00047   {
00048     if(--const_cast<unsigned int&>(_ref_count) == 0) {
00049       delete this;
00050     }
00051   }
00052 
00053 protected:
00054   unsigned int _ref_count; // mutable
00055 };
00056 
00058 template<class T> class RefCountPointer
00059 {
00060 public:
00061   RefCountPointer(T *p = 0) : _p(p)
00062   {
00063     if(_p != 0) _p->incrementRefCount();
00064   }
00065 
00066   template<class T2> RefCountPointer(const RefCountPointer<T2> &o) : _p((T*)(T2*)o)
00067   {
00068     if(_p != 0) _p->incrementRefCount();
00069   }
00070 
00071   RefCountPointer(const RefCountPointer<T> &o) : _p(o._p)
00072   {
00073     if(_p != 0) _p->incrementRefCount();
00074   }
00075 
00076   RefCountPointer &operator=(const RefCountPointer<T> &o)
00077   {
00078     if(_p != o._p) {
00079       if(_p != 0) _p->decrementRefCount();
00080       _p = o._p;
00081       if(_p != 0) _p->incrementRefCount();
00082     }
00083     return *this;
00084   }
00085 
00086   ~RefCountPointer()
00087   {
00088     if(_p != 0) _p->decrementRefCount();
00089   }
00090 
00091   T *operator->() const
00092   {
00093     return _p;
00094   }
00095 
00096   operator T*() const
00097   {
00098     return _p;
00099   }
00100 
00101   T *get() const
00102   {
00103     return _p;
00104   }
00105 
00106   bool isNull() const
00107   {
00108     return (_p == 0);
00109   }
00110 
00111   bool notNull() const
00112   {
00113     return (_p != 0);
00114   }
00115 
00116 protected:
00117   T *_p;
00118 };
00119 
00120 template<class T1, class T2>
00121 inline bool operator==(const RefCountPointer<T1> &a, const RefCountPointer<T2> &b)
00122 {
00123   return (void*)(T1*)a == (void*)(T2*)b;
00124 }
00125 
00126 template<class T1, class T2>
00127 inline bool operator!=(const RefCountPointer<T1> &a, const RefCountPointer<T2> &b)
00128 {
00129   return (void*)(T1*)a != (void*)(T2*)b;
00130 }
00131 
00132 template<class T>
00133 inline bool operator==(const RefCountPointer<T> &a, void *b)
00134 {
00135   return (T*)a == (T*)b;
00136 }
00137 
00138 template<class T>
00139 inline bool operator!=(const RefCountPointer<T> &a, void *b)
00140 {
00141   return (T*)a != (T*)b;
00142 }
00143 
00144 #endif

Generated on Wed Dec 12 20:16:43 2007 for XQilla Simple API by  doxygen 1.5.1