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 #ifndef _MPCL_MEMORY_SMART_POINTER__
00027 #define _MPCL_MEMORY_SMART_POINTER__
00028
00029 #include <memory>
00030 #include "base_smart_pointer.hh"
00031
00032
00034 namespace mpcl
00035 {
00036
00038 namespace memory
00039 {
00040
00092 template <typename TItem>
00093 class TSmartPointer: public TBaseSmartPointer
00094 {
00095
00096 public:
00097
00102 typedef
00103 const TItem*
00104 const_pointer;
00105
00110 typedef
00111 const TItem&
00112 const_reference;
00113
00118 typedef
00119 TItem*
00120 pointer;
00121
00126 typedef
00127 TItem&
00128 reference;
00129
00134 typedef
00135 size_t
00136 size_type;
00137
00142 typedef
00143 TItem
00144 value_type;
00145
00146
00147 protected:
00148
00149
00150
00151
00152
00158 void freeCell (void)
00159 {
00160 delete (pointer) ptSharedCell->pvItem;
00161 delete ptSharedCell;
00162 }
00163
00168 void reserveCell (const_pointer pktITEM)
00169 {
00170 if ( pktITEM )
00171 {
00172 ptSharedCell = new TSharedCell (pktITEM);
00173 }
00174 }
00175
00176
00177 public:
00178
00179
00180
00181
00182
00184 TSmartPointer (void) :
00185 TBaseSmartPointer() {}
00186
00191 TSmartPointer (const TSmartPointer& rkqtITEM) :
00192 TBaseSmartPointer()
00193 {
00194 attachTo (rkqtITEM);
00195 }
00196
00197
00202 template <typename TDerivedItem>
00203 explicit TSmartPointer (const TSmartPointer<TDerivedItem>& rkqtITEM_DERIVED) :
00204 TBaseSmartPointer()
00205 {
00206
00207
00208
00209
00210 register pointer ptItem;
00211 register TDerivedItem* ptDerivedItem (NULL);
00212
00213
00214
00215
00216
00217
00218 ptItem = ptDerivedItem;
00219 attachTo (rkqtITEM_DERIVED);
00220
00221 }
00222
00223
00229 explicit TSmartPointer (const_pointer pktITEM) :
00230 TBaseSmartPointer()
00231 {
00232 reserveCell (pktITEM);
00233 }
00234
00236 ~TSmartPointer (void)
00237 {
00238 release();
00239 }
00240
00246 TSmartPointer& operator = (const_pointer pktITEM)
00247 {
00248 release();
00249 reserveCell (pktITEM);
00250 return *this;
00251 }
00252
00253 TSmartPointer& operator = (const TSmartPointer& rkqtITEM)
00254 {
00255 if ( rkqtITEM.ptSharedCell != ptSharedCell )
00256 {
00257 release();
00258 attachTo (rkqtITEM);
00259 }
00260 return *this;
00261 }
00262
00267 void release (void)
00268 {
00269
00270 if ( ptSharedCell )
00271 {
00272
00273
00274
00275
00276 --(ptSharedCell->luiReferenceCount);
00277 if ( !ptSharedCell->luiReferenceCount )
00278 {
00279
00280
00281
00282
00283
00284 freeCell();
00285 }
00286
00287
00288
00289
00290 ptSharedCell = NULL;
00291 }
00292
00293 }
00294
00295
00296 public:
00297
00298
00299
00300
00301
00306 template <typename TPointer>
00307 TPointer constCast (void) const throw()
00308 {
00309 TPointer tPointer = NULL;
00310
00311 if ( ptSharedCell )
00312 {
00313 tPointer = const_cast<TPointer> ((pointer) ptSharedCell->pvItem);
00314 }
00315 return tPointer;
00316 }
00317
00318 template <typename TPointer>
00319 TPointer dynamicCast (void) const
00320 throw (TConstraintException)
00321 {
00322 TPointer tPointer = NULL;
00323
00324 if ( !ptSharedCell )
00325 {
00326
00327 throw TConstraintException ("null pointer", __FILE__, __LINE__);
00328
00329 }
00330 else
00331 {
00332 tPointer = dynamic_cast<TPointer> ((pointer) ptSharedCell->pvItem);
00333 if ( !tPointer )
00334 {
00335 throw TConstraintException ("can not dynamic-cast pointer", __FILE__, __LINE__);
00336 }
00337 }
00338 return tPointer;
00339 }
00340
00345 pointer get (void) const throw()
00346 {
00347 register pointer ptResultItem = NULL;
00348
00349 if ( ptSharedCell )
00350 {
00351 ptResultItem = (pointer) ptSharedCell->pvItem;
00352 }
00353 return ptResultItem;
00354 }
00355
00361 bool operator == (const TSmartPointer& rkqtITEM) const throw()
00362 {
00363 return equal (rkqtITEM);
00364 }
00365
00371 bool operator != (const TSmartPointer& rkqtITEM) const throw()
00372 {
00373 return !operator == (rkqtITEM);
00374 }
00375
00380 bool operator ! (void) const throw()
00381 {
00382 return ( !ptSharedCell );
00383 }
00384
00389 pointer operator -> (void) const
00390 throw (TConstraintException)
00391 {
00392 if ( !ptSharedCell )
00393 {
00394 throw TConstraintException ("null pointer", __FILE__, __LINE__);
00395 }
00396 return (pointer) ptSharedCell->pvItem;
00397 }
00398
00403 reference operator * (void) const
00404 throw (TConstraintException)
00405 {
00406 if ( !ptSharedCell )
00407 {
00408 throw TConstraintException ("null pointer", __FILE__, __LINE__);
00409 }
00410 return *((pointer) ptSharedCell->pvItem);
00411 }
00412
00413 };
00414
00415 }
00416
00417 }
00418
00419
00420 #endif // not _MPCL_MEMORY_SMART_POINTER__