00001 // -*- c++ -*- 00002 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 00003 00004 // O S C L _ S H A R E D _ P T R 00005 00006 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 00007 00019 #ifndef OSCL_SHARED_PTR_H_INCLUDED 00020 #define OSCL_SHARED_PTR_H_INCLUDED 00021 00022 #ifndef OSCL_BASE_H_INCLUDED 00023 #include "oscl_base.h" 00024 #endif 00025 00026 #ifndef OSCL_REFCOUNTER_H_INCLUDED 00027 #include "oscl_refcounter.h" 00028 #endif 00029 00030 #define OSCL_DISABLE_WARNING_RETURN_TYPE_NOT_UDT 00031 #include "osclconfig_compiler_warnings.h" 00032 00034 template <class TheClass> 00035 class OsclSharedPtr 00036 { 00037 public: 00039 OsclSharedPtr() : 00040 mpRep(NULL), refcnt(NULL) {} 00041 00043 00046 OsclSharedPtr(TheClass* inClassPtr, OsclRefCounter* in_refcnt) : 00047 mpRep(inClassPtr), refcnt(in_refcnt) {}; 00048 00049 00051 OsclSharedPtr(const OsclSharedPtr& inSharedPtr) : 00052 mpRep(inSharedPtr.mpRep), refcnt(inSharedPtr.refcnt) 00053 { 00054 if (refcnt) 00055 { 00056 refcnt->addRef(); 00057 } 00058 } 00059 00060 00062 virtual ~OsclSharedPtr() 00063 { 00064 if (refcnt != NULL) 00065 { 00066 refcnt->removeRef(); 00067 } 00068 } // end destructor 00069 00070 00073 TheClass* operator->() 00074 { 00075 return mpRep; 00076 } 00077 00079 TheClass& operator*() 00080 { 00081 return *mpRep; 00082 } 00083 00085 operator TheClass*() 00086 { 00087 return mpRep; 00088 } 00089 00091 TheClass* GetRep() 00092 { 00093 return mpRep; 00094 } 00095 00097 OsclRefCounter* GetRefCounter() 00098 { 00099 return refcnt; 00100 } 00101 00103 int get_count() 00104 { 00105 return (refcnt == NULL) ? 0 : refcnt->getCount(); 00106 } 00107 00109 void Bind(const OsclSharedPtr& inHandle); 00110 00112 void Bind(TheClass* ptr, OsclRefCounter* in_refcnt); 00113 00115 void Unbind() 00116 { 00117 Bind(NULL, NULL); 00118 }; 00119 00121 OsclSharedPtr& operator=(const OsclSharedPtr& inSharedPtr) 00122 { 00123 Bind(inSharedPtr); 00124 return *this; 00125 } 00126 00128 bool operator==(const OsclSharedPtr& b) const; 00129 00130 private: 00131 00132 TheClass* mpRep; 00133 OsclRefCounter* refcnt; 00134 00135 }; 00136 00137 00138 template <class TheClass> inline bool OsclSharedPtr<TheClass>::operator==(const OsclSharedPtr<TheClass>& b) const 00139 { 00140 if ((this->mpRep == b.mpRep) && 00141 (this->refcnt == b.refcnt)) 00142 { 00143 return true; 00144 } 00145 return false; 00146 } 00147 00148 00149 template <class TheClass> inline void OsclSharedPtr<TheClass>::Bind(const OsclSharedPtr& inSharedPtr) 00150 { 00151 if (mpRep == inSharedPtr.mpRep) return; 00152 00153 if (refcnt != NULL) 00154 { 00155 refcnt->removeRef(); 00156 } 00157 00158 refcnt = inSharedPtr.refcnt; 00159 mpRep = inSharedPtr.mpRep; 00160 00161 if (refcnt != NULL) 00162 { 00163 refcnt->addRef(); 00164 } 00165 00166 } 00167 00168 template <class TheClass> inline void OsclSharedPtr<TheClass>::Bind(TheClass* ptr, 00169 OsclRefCounter* in_refcnt) 00170 { 00171 if (refcnt != NULL) 00172 { 00173 refcnt->removeRef(); 00174 } 00175 00176 mpRep = ptr; 00177 refcnt = in_refcnt; 00178 00179 } 00180 00181 #endif // OSCL_SHARED_PTR_H_INCLUDED