Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef AVT_VMBAPI_SHAREDPOINTER_IMPL_H
00031 #define AVT_VMBAPI_SHAREDPOINTER_IMPL_H
00032
00033 #include <VimbaCPP/Include/SharedPointer.h>
00034
00035 namespace AVT {
00036 namespace VmbAPI {
00037
00038 template <class T>
00039 ref_count<T>::ref_count(const ref_count &rRefCount)
00040 {
00041 }
00042
00043 template <class T>
00044 ref_count<T>& ref_count<T>::operator = (const ref_count &rRefCount)
00045 {
00046
00047 return *this;
00048 }
00049
00050 template <class T>
00051 ref_count<T>::ref_count(T *pObject)
00052 : m_pObject(pObject)
00053 , m_nCount(1)
00054 {
00055 }
00056
00057 template <class T>
00058 ref_count<T>::~ref_count()
00059 {
00060 if(NULL != m_pObject)
00061 {
00062 delete m_pObject;
00063 }
00064 }
00065
00066 template <class T>
00067 void ref_count<T>::inc()
00068 {
00069 m_Mutex.Lock();
00070
00071 m_nCount++;
00072
00073 m_Mutex.Unlock();
00074 }
00075
00076 template <class T>
00077 void ref_count<T>::dec()
00078 {
00079 m_Mutex.Lock();
00080
00081 if(m_nCount > 1)
00082 {
00083 m_nCount--;
00084
00085 m_Mutex.Unlock();
00086 }
00087 else
00088 {
00089 delete this;
00090 }
00091 }
00092
00093 template <class T>
00094 long ref_count<T>::use_count() const
00095 {
00096 return m_nCount;
00097 }
00098
00099 template <class T>
00100 template <class T2>
00101 void shared_ptr<T>::swap(T2 &rValue1, T2 &rValue2)
00102 {
00103 T2 buffer = rValue1;
00104 rValue1 = rValue2;
00105 rValue2 = buffer;
00106 }
00107
00108 template <class T>
00109 shared_ptr<T>::shared_ptr()
00110 : m_pRefCount(NULL)
00111 , m_pObject(NULL)
00112 {
00113 }
00114
00115 template <class T>
00116 template <class T2>
00117 shared_ptr<T>::shared_ptr(T2 *pObject)
00118 : m_pRefCount(NULL)
00119 , m_pObject(NULL)
00120 {
00121 m_pRefCount = new ref_count<T2>(pObject);
00122 if(NULL == m_pRefCount)
00123 {
00124 delete pObject;
00125
00126 throw std::bad_alloc();
00127 }
00128
00129 m_pObject = pObject;
00130 }
00131
00132 template <class T>
00133 template <class T2>
00134 shared_ptr<T>::shared_ptr(const shared_ptr<T2> &rSharedPointer)
00135 : m_pRefCount(NULL)
00136 , m_pObject(NULL)
00137 {
00138 if(NULL != rSharedPointer.m_pRefCount)
00139 {
00140 rSharedPointer.m_pRefCount->inc();
00141
00142 m_pRefCount = rSharedPointer.m_pRefCount;
00143 m_pObject = rSharedPointer.m_pObject;
00144 }
00145 }
00146
00147 template <class T>
00148 template <class T2>
00149 shared_ptr<T>::shared_ptr(const shared_ptr<T2> &rSharedPointer, dynamic_cast_tag)
00150 : m_pRefCount(NULL)
00151 , m_pObject(NULL)
00152 {
00153 if(NULL != rSharedPointer.m_pRefCount)
00154 {
00155 T *pObject = dynamic_cast<T*>(rSharedPointer.m_pObject);
00156 if(NULL != pObject)
00157 {
00158 rSharedPointer.m_pRefCount->inc();
00159
00160 m_pRefCount = rSharedPointer.m_pRefCount;
00161 m_pObject = pObject;
00162 }
00163 }
00164 }
00165
00166 template <class T>
00167 shared_ptr<T>::shared_ptr(const shared_ptr &rSharedPointer)
00168 : m_pRefCount(NULL)
00169 , m_pObject(NULL)
00170 {
00171 if(NULL != rSharedPointer.m_pRefCount)
00172 {
00173 rSharedPointer.m_pRefCount->inc();
00174
00175 m_pRefCount = rSharedPointer.m_pRefCount;
00176 m_pObject = rSharedPointer.m_pObject;
00177 }
00178 }
00179
00180 template <class T>
00181 shared_ptr<T>::~shared_ptr()
00182 {
00183 if(NULL != m_pRefCount)
00184 {
00185 m_pRefCount->dec();
00186 m_pRefCount = NULL;
00187 m_pObject = NULL;
00188 }
00189 }
00190
00191 template <class T>
00192 template <class T2>
00193 shared_ptr<T>& shared_ptr<T>::operator = (const shared_ptr<T2> &rSharedPointer)
00194 {
00195 shared_ptr(rSharedPointer).swap(*this);
00196
00197 return *this;
00198 }
00199
00200 template <class T>
00201 shared_ptr<T>& shared_ptr<T>::operator = (const shared_ptr &rSharedPointer)
00202 {
00203 shared_ptr(rSharedPointer).swap(*this);
00204
00205 return *this;
00206 }
00207
00208 template <class T>
00209 void shared_ptr<T>::reset()
00210 {
00211 shared_ptr().swap(*this);
00212 }
00213
00214 template <class T>
00215 template <class T2>
00216 void shared_ptr<T>::reset(T2 *pObject)
00217 {
00218 shared_ptr(pObject).swap(*this);
00219 }
00220
00221 template <class T>
00222 T* shared_ptr<T>::get() const
00223 {
00224 return m_pObject;
00225 }
00226
00227 template <class T>
00228 T& shared_ptr<T>::operator * () const
00229 {
00230 return *m_pObject;
00231 }
00232
00233 template <class T>
00234 T* shared_ptr<T>::operator -> () const
00235 {
00236 return m_pObject;
00237 }
00238
00239 template <class T>
00240 long shared_ptr<T>::use_count() const
00241 {
00242 if(NULL == m_pRefCount)
00243 {
00244 return 0;
00245 }
00246
00247 return m_pRefCount->use_count();
00248 }
00249
00250 template <class T>
00251 bool shared_ptr<T>::unique() const
00252 {
00253 return (use_count() == 1);
00254 }
00255
00256 template <class T>
00257 void shared_ptr<T>::swap(shared_ptr &rSharedPointer)
00258 {
00259 swap(m_pObject, rSharedPointer.m_pObject);
00260 swap(m_pRefCount, rSharedPointer.m_pRefCount);
00261 }
00262
00263 template<class T, class T2>
00264 shared_ptr<T> dynamic_pointer_cast(const shared_ptr<T2> &rSharedPointer)
00265 {
00266 return shared_ptr<T>(rSharedPointer, dynamic_cast_tag());
00267 }
00268
00269 template <class T1, class T2>
00270 bool operator==(const shared_ptr<T1>& sp1, const shared_ptr<T2>& sp2)
00271 {
00272 return sp1.get() == sp2.get();
00273 }
00274
00275 template <class T1, class T2>
00276 bool operator!=(const shared_ptr<T1>& sp1, const shared_ptr<T2>& sp2)
00277 {
00278 return sp1.get() != sp2.get();
00279 }
00280
00281 }}
00282
00283 #endif //AVT_VMBAPI_SHAREDPOINTER_IMPL_H