> The XQuery Processor
00001 /* 00002 * Copyright 2006-2008 The FLWOR Foundation. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef ZORBA_SMARTPTR_API_H 00017 #define ZORBA_SMARTPTR_API_H 00018 00019 #include <zorba/config.h> 00020 00021 namespace zorba { 00022 00023 class ZORBA_EXTERN_DECL SmartObject 00024 { 00025 protected: 00026 mutable unsigned int theRefCount; 00027 00028 public: 00029 SmartObject() : theRefCount(0) { } 00030 00031 SmartObject(const SmartObject&) : theRefCount(0) { } 00032 00033 virtual ~SmartObject() { } 00034 00035 virtual void free() { delete this; } 00036 00037 long getRefCount() const { return theRefCount; } 00038 00039 void addReference() const { ++theRefCount; } 00040 00041 void removeReference () { 00042 if (--theRefCount == 0) 00043 free(); 00044 } 00045 00046 SmartObject& operator=(const SmartObject&) { return *this; } 00047 }; /* class SmartObject */ 00048 00049 template<class T> 00050 class SmartPtr 00051 { 00052 protected: 00053 T *p; 00054 00055 void init() { 00056 if (p != 0) p->addReference(); 00057 } 00058 00059 template <class otherT> SmartPtr& 00060 assign (const SmartPtr<otherT>& rhs) { 00061 if (p != rhs.get()) 00062 { 00063 if (p) p->removeReference(); 00064 p = static_cast<T*>(rhs.get()); 00065 init(); 00066 } 00067 return *this; 00068 } 00069 00070 public: 00071 SmartPtr(T* realPtr = 0) : p(realPtr) { 00072 init(); 00073 } 00074 00075 SmartPtr(SmartPtr const& rhs) : p(rhs.get()) { 00076 init(); 00077 } 00078 00079 ~SmartPtr() throw () { 00080 if (p) 00081 p->removeReference(); 00082 p = 0; 00083 } 00084 00085 bool isNull () const { return p == 0; } 00086 00087 T* get() const { return p; } 00088 00089 operator T* () { return get(); } 00090 operator const T * () const { return get(); } 00091 00092 T* operator->() const { return p; } 00093 T& operator*() const { return *p; } 00094 00095 bool operator==(SmartPtr const& h) const { return p == h.p; } 00096 bool operator!=(SmartPtr const& h) const { return p != h.p; } 00097 bool operator==(T const* pp) const { return p == pp; } 00098 bool operator!=(T const* pp) const { return p != pp; } 00099 bool operator<(const SmartPtr& h) const { return p < h.p; } 00100 00101 00102 00103 SmartPtr& operator=(SmartPtr const& rhs) { 00104 return assign (rhs); 00105 } 00106 00107 template <class otherT> SmartPtr& operator=(SmartPtr<otherT> const& rhs) { 00108 return assign (rhs); 00109 } 00110 00111 00112 }; /* SmartPtr */ 00113 00114 } /* namespace zorba */ 00115 #endif