00001 /* 00002 * Copyright (c) 2001, 2008, 00003 * DecisionSoft Limited. All rights reserved. 00004 * Copyright (c) 2004, 2015 Oracle and/or its affiliates. All rights reserved. 00005 * 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 00020 #ifndef _REFERENCECOUNTED_HPP 00021 #define _REFERENCECOUNTED_HPP 00022 00023 #include <xqilla/framework/XQillaExport.hpp> 00024 #include <xercesc/framework/MemoryManager.hpp> 00025 00026 // for null RefCountPointer instances 00027 #define NULLRCP ((void *)0) 00028 00030 class XQILLA_API ReferenceCounted 00031 { 00032 public: 00033 ReferenceCounted() 00034 : _ref_count(0) {} 00035 virtual ~ReferenceCounted() {} 00036 00038 void incrementRefCount() const 00039 { 00040 ++const_cast<unsigned int&>(_ref_count); 00041 } 00042 00044 virtual void decrementRefCount() const 00045 { 00046 if(--const_cast<unsigned int&>(_ref_count) == 0) { 00047 delete this; 00048 } 00049 } 00050 00051 unsigned int getRefCount() const 00052 { 00053 return _ref_count; 00054 } 00055 00056 protected: 00057 unsigned int _ref_count; // mutable 00058 }; 00059 00061 template<class T> class RefCountPointer 00062 { 00063 public: 00064 RefCountPointer(T *p = 0) : _p(p) 00065 { 00066 if(_p != 0) _p->incrementRefCount(); 00067 } 00068 00069 template<class T2> RefCountPointer(const RefCountPointer<T2> &o) : _p((T*)(T2*)o) 00070 { 00071 if(_p != 0) _p->incrementRefCount(); 00072 } 00073 00074 RefCountPointer(const RefCountPointer<T> &o) : _p(o._p) 00075 { 00076 if(_p != 0) _p->incrementRefCount(); 00077 } 00078 00079 RefCountPointer &operator=(const RefCountPointer<T> &o) 00080 { 00081 if(_p != o._p) { 00082 if(_p != 0) _p->decrementRefCount(); 00083 _p = o._p; 00084 if(_p != 0) _p->incrementRefCount(); 00085 } 00086 return *this; 00087 } 00088 00089 ~RefCountPointer() 00090 { 00091 if(_p != 0) _p->decrementRefCount(); 00092 } 00093 00094 T *operator->() const 00095 { 00096 return _p; 00097 } 00098 00099 operator T*() const 00100 { 00101 return _p; 00102 } 00103 00104 T *get() const 00105 { 00106 return _p; 00107 } 00108 00109 bool isNull() const 00110 { 00111 return (_p == 0); 00112 } 00113 00114 bool notNull() const 00115 { 00116 return (_p != 0); 00117 } 00118 00119 protected: 00120 T *_p; 00121 }; 00122 00123 template<class T1, class T2> 00124 inline bool operator==(const RefCountPointer<T1> &a, const RefCountPointer<T2> &b) 00125 { 00126 return (void*)(T1*)a == (void*)(T2*)b; 00127 } 00128 00129 template<class T1, class T2> 00130 inline bool operator!=(const RefCountPointer<T1> &a, const RefCountPointer<T2> &b) 00131 { 00132 return (void*)(T1*)a != (void*)(T2*)b; 00133 } 00134 00135 template<class T> 00136 inline bool operator==(const RefCountPointer<T> &a, void *b) 00137 { 00138 return (T*)a == (T*)b; 00139 } 00140 00141 template<class T> 00142 inline bool operator!=(const RefCountPointer<T> &a, void *b) 00143 { 00144 return (T*)a != (T*)b; 00145 } 00146 00147 #endif