Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

smart_pointer.hh

00001 /*
00002 *  Name:      smart_pointer.hh
00003 *  Author:    Rafael Jesus Alcantara Perez
00004 *  Summary:   Smart pointer
00005 *  Date:      $Date: 2003/04/14 00:18:31 $
00006 *  Revision:  $Revision: 1.1 $
00007 *
00008 *  Copyright (C) 1994-2002  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00009 *
00010 *  This program is free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  This program is distributed in the hope that it will be useful,
00016 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 *  GNU General Public License for more details.
00019 *
00020 *  You should have received a copy of the GNU General Public License
00021 *  along with this program; if not, write to the Free Software
00022 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 *  MA 02111-1307, USA.
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         //  C O N S T R U C T O R S
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         //  C O N S T R U C T O R S
00181         //
00182 
00184         TSmartPointer (void)  :
00185           TBaseSmartPointer() {}
00186 
00191         TSmartPointer (const TSmartPointer& rkqtITEM) :
00192           TBaseSmartPointer()
00193         {
00194           attachTo (rkqtITEM);
00195         }
00196 
00197   // <FIXME id="smart-pointers" comment="this method can assign other type of objects">
00202         template <typename TDerivedItem>
00203         explicit TSmartPointer (const TSmartPointer<TDerivedItem>& rkqtITEM_DERIVED) :
00204           TBaseSmartPointer()
00205         {
00206 
00207           //
00208           //  Declares two variables for checking assign ability.
00209           //
00210           register pointer         ptItem;
00211           register TDerivedItem*   ptDerivedItem (NULL);
00212 
00213           //
00214           //  Checks for possible syntax errors about using classes with no relation with
00215           //  this  instance to assign  to this  instance.  At  last,  it attaches to the
00216           //  derived pointer.
00217           //
00218           ptItem = ptDerivedItem;
00219           attachTo (rkqtITEM_DERIVED);
00220 
00221         }  // TSmartPointer()
00222   // </FIXME>
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             //  ptSharedCell->pvItem must be different from NULL.
00274             //  Decrements the reference counter.
00275             //
00276             --(ptSharedCell->luiReferenceCount);
00277             if ( !ptSharedCell->luiReferenceCount )
00278             {
00279               //
00280               //  Warning:  There is no check when  deleting  ptSharedCell->pvItem (for
00281               //            NULL value) cause  there is no way to assign a NULL pointer
00282               //            value.
00283               //
00284               freeCell();
00285             }
00286             
00287             //
00288             //  Anyway, clears the pointer to the shared cell.
00289             //
00290             ptSharedCell = NULL;
00291           }
00292 
00293         }  // release()
00294 
00295 
00296       public:
00297 
00298         //
00299         //  S E L E C T O R S
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   // <FIXME id="smart-pointers" comment="this exception must not be thrown">
00327             throw TConstraintException ("null pointer", __FILE__, __LINE__);
00328   // </FIXME>
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     };  // class TSmartPointer
00414 
00415   }  // namespace memory
00416 
00417 }  // namespace mpcl
00418 
00419 
00420 #endif  // not _MPCL_MEMORY_SMART_POINTER__

Generated on Mon Oct 13 02:35:23 2003 for MPCL by doxygen1.2.18