00001
00006 #ifndef __BGDNetShare_h__
00007 #define __BGDNetShare_h__
00008
00009 #include "BGDNetConfig.h"
00010
00011 #include <assert.h>
00012
00013 namespace BGDNet
00014 {
00015
00024 class BGDNET_API Share
00025 {
00026 public:
00027
00028 Share( void );
00034 Share( const Share& );
00040 Share& operator =( const Share& );
00047 void addRef( void );
00052 void release( void );
00058 bool shared( void ) const;
00065 protected:
00066
00067 virtual ~Share( void ) {};
00072 private:
00073
00074 unsigned long m_refCnt;
00078 };
00079
00080 inline
00081 Share::Share( void ) : m_refCnt(1)
00082 {
00083 }
00084
00085 inline
00086 Share::Share( const Share& ) : m_refCnt(1)
00087 {
00088 }
00089
00090 inline
00091 Share& Share::operator =( const Share& )
00092 {
00093 return *this;
00094 }
00095
00096 inline
00097 void Share::addRef( void )
00098 {
00099 ++m_refCnt;
00100 }
00101
00102 inline
00103 void Share::release( void )
00104 {
00105 if (!--m_refCnt)
00106 delete this;
00107 }
00108
00109 inline
00110 bool Share::shared( void ) const
00111 {
00112 return m_refCnt > 0;
00113 }
00114
00115
00116
00117
00125 template <class S>
00126 class BGDNET_API SharePtr
00127 {
00128 public:
00129
00130 SharePtr( S* p = 0 );
00138 SharePtr( const SharePtr& p );
00143 ~SharePtr( void );
00149 SharePtr& operator =( S* p );
00154 SharePtr& operator =( const SharePtr& p );
00159 const S* operator ->( void ) const;
00164 S* operator ->( void );
00169 const S& operator *( void ) const;
00174 S& operator *( void );
00179 operator const S*( void ) const;
00184 operator S*( void );
00189 private:
00190
00191 S* m_pShare;
00195 };
00196
00197 template <class S> inline
00198 SharePtr<S>::SharePtr( S* p ) : m_pShare(p)
00199 {
00200 }
00201
00202 template <class S> inline
00203 SharePtr<S>::SharePtr( const SharePtr& p ) : m_pShare(p.m_pShare)
00204 {
00205 if (m_pShare)
00206 m_pShare->addRef();
00207 }
00208
00209 template <class S> inline
00210 SharePtr<S>::~SharePtr( void )
00211 {
00212 if (m_pShare)
00213 m_pShare->release();
00214 }
00215
00216 template <class S>
00217 SharePtr<S>& SharePtr<S>::operator =( S* p )
00218 {
00219 if (m_pShare != p)
00220 {
00221 if (m_pShare)
00222 m_pShare->release();
00223 m_pShare = p;
00224 }
00225 return *this;
00226 }
00227
00228 template <class S>
00229 SharePtr<S>& SharePtr<S>::operator =( const SharePtr& p )
00230 {
00231 if (this != &p)
00232 {
00233
00234 if (m_pShare)
00235 m_pShare->release();
00236
00237
00238 m_pShare = p.m_pShare;
00239
00240
00241 if (m_pShare)
00242 m_pShare->addRef();
00243 }
00244 return *this;
00245 }
00246
00247 template <class S> inline
00248 const S* SharePtr<S>::operator ->( void ) const
00249 {
00250 return m_pShare;
00251 }
00252
00253 template <class S> inline
00254 S* SharePtr<S>::operator ->( void )
00255 {
00256 return m_pShare;
00257 }
00258
00259 template <class S> inline
00260 const S& SharePtr<S>::operator *( void ) const
00261 {
00262 assert(m_pShare);
00263 return *m_pShare;
00264 }
00265
00266 template <class S> inline
00267 S& SharePtr<S>::operator *( void )
00268 {
00269 assert(m_pShare);
00270 return *m_pShare;
00271 }
00272
00273 template <class S> inline
00274 SharePtr<S>::operator const S*( void ) const
00275 {
00276 return m_pShare;
00277 }
00278
00279 template <class S> inline
00280 SharePtr<S>::operator S*( void )
00281 {
00282 return m_pShare;
00283 }
00284
00285 }
00286
00287 #endif //__BGDNetShare_h__