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

base_smart_pointer.hh

00001 /*
00002 *  Name:      base_smart_pointer.hh
00003 *  Author:    Rafael Jesus Alcantara Perez
00004 *  Summary:   Base class for smart pointers
00005 *  Date:      $Date: 2003/04/14 00:18:31 $
00006 *  Revision:  $Revision: 1.1 $
00007 *
00008 *  Copyright (C) 2001-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_BASE_SMART_POINTER__
00027 #define _MPCL_MEMORY_BASE_SMART_POINTER__
00028 
00029 #include "../exceptions.hh"
00030 
00031 
00033 namespace mpcl
00034 {
00035 
00037   namespace memory
00038   {
00039 
00045     class TSharedCell
00046     {
00047 
00048       public:
00049 
00051         long unsigned int   luiReferenceCount;
00052 
00054         void*   pvItem;
00055 
00056 
00057       public:
00058 
00059         //
00060         //  C O N S T R U C T O R S
00061         //
00062 
00064         TSharedCell (void)         :
00065           luiReferenceCount (0L)   ,
00066           pvItem            (NULL) {}
00067 
00072         TSharedCell (const void* pkvITEM)                 :
00073           luiReferenceCount (1L)                          ,
00074           pvItem            (const_cast<void*> (pkvITEM)) {}
00075 
00076     };  // struct TSharedCell
00077 
00079     class TBaseSmartPointer
00080     {
00081 
00082       protected:
00083 
00088         typedef
00089           const TSharedCell&
00090           cell_const_reference;
00091 
00096         typedef
00097           TSharedCell*
00098           cell_pointer;
00099 
00104         typedef
00105           TSharedCell&
00106           cell_reference;
00107 
00109         mutable cell_pointer   ptSharedCell;
00110 
00111 
00112       public:
00113 
00115         typedef
00116           TSharedCell
00117           cell_type;
00118 
00119 
00120       protected:
00121 
00122         //
00123         //  C O N S T R U C T O R S
00124         //
00125 
00126         void attachTo (const TBaseSmartPointer& rkqtBASE_SMART_POINTER)
00127         {
00128           ptSharedCell = rkqtBASE_SMART_POINTER.ptSharedCell;
00129           if ( ptSharedCell )
00130           {
00131             ++(ptSharedCell->luiReferenceCount);
00132           }
00133         }
00134 
00135 
00136       public:
00137 
00138         //
00139         //  C O N S T R U C T O R S
00140         //
00141 
00143         TBaseSmartPointer (void)
00144           throw()             :
00145           ptSharedCell (NULL) {}
00146 
00148         virtual ~TBaseSmartPointer (void) {}
00149 
00150 
00151       public:
00152 
00153         //
00154         //  S E L E C T O R S
00155         //
00156 
00161         const void* address (void) const throw()
00162         {
00163           const void*   pkvRet = NULL;
00164 
00165           if ( ptSharedCell )
00166           {
00167             pkvRet = ptSharedCell->pvItem;
00168           }
00169           return pkvRet;
00170         }
00171 
00172         bool equal (const TBaseSmartPointer& rkqtBASE_SMART_POINTER) const
00173           throw()
00174         {
00175           //
00176           //  Checks that both  smart pointers  point to the  same cell and that both
00177           //  pointers are not null.
00178           //
00179           register bool   gEqual = ( !ptSharedCell && !rkqtBASE_SMART_POINTER.ptSharedCell );
00180 
00181           if ( ptSharedCell && rkqtBASE_SMART_POINTER.ptSharedCell )
00182           {
00183             gEqual = ( ptSharedCell->pvItem == rkqtBASE_SMART_POINTER.ptSharedCell->pvItem );
00184           }
00185           return gEqual;
00186         }
00187 
00192         bool isNull (void) const throw()
00193         {
00194           return ( ptSharedCell == NULL );
00195         }
00196 
00202         long unsigned int references (void) const
00203           throw (TConstraintException)
00204         {
00205           if ( !ptSharedCell )
00206           {
00207             throw TConstraintException ("null pointer", __FILE__, __LINE__);
00208           }
00209           return ptSharedCell->luiReferenceCount;
00210         }
00211 
00212     };  // class TBaseSmartPointer
00213 
00214   }  // namespace memory
00215 
00216 }  // namespace mpcl
00217 
00218 
00219 #endif  // not _MPCL_MEMORY_BASE_SMART_POINTER__

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