Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

Ref.h

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------------
00002 
00003    Copyright (c) 2000 Tyrell Corporation. All rights reserved.
00004 
00005    Tyrell DarkIce
00006 
00007    File     : Ref.h
00008    Version  : $Revision: 1.5 $
00009    Author   : $Author: darkeye $
00010    Location : $Source: /cvsroot/darkice/darkice/src/Ref.h,v $
00011    
00012    Copyright notice:
00013 
00014     This program is free software; you can redistribute it and/or
00015     modify it under the terms of the GNU General Public License  
00016     as published by the Free Software Foundation; either version 2
00017     of the License, or (at your option) any later version.
00018     
00019     This program is distributed in the hope that it will be useful,
00020     but WITHOUT ANY WARRANTY; without even the implied warranty of 
00021     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
00022     GNU General Public License for more details.
00023     
00024     You should have received a copy of the GNU General Public License
00025     along with this program; if not, write to the Free Software
00026     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 
00028 ------------------------------------------------------------------------------*/
00029 #ifndef REF_H
00030 #define REF_H
00031 
00032 #ifndef __cplusplus
00033 #error This is a C++ include file
00034 #endif
00035 
00036 
00037 /* ============================================================ include files */
00038 
00039 #include "Exception.h"
00040 
00041 
00042 /* ================================================================ constants */
00043 
00044 
00045 /* =================================================================== macros */
00046 
00047 
00048 /* =============================================================== data types */
00049 
00081 template <class T>
00082 class Ref
00083 {
00084     private:
00085         
00090         T* object;
00091 
00092 
00093     protected:
00094 
00095 
00096     public:
00097 
00101         inline
00102         Ref ( void )                            throw ()
00103         {
00104             object = NULL;
00105         }
00106 
00113         inline
00114         Ref ( const Ref<T> &    other )         throw ( Exception )
00115         {
00116             object = NULL;
00117             set( other.object);
00118         }
00119 
00126         inline
00127         Ref ( T   * obj )                       throw ( Exception )
00128         {
00129             object = obj;
00130             obj->increaseReferenceCount();
00131         }
00132 
00138         inline virtual
00139         ~Ref ( void )                           throw ( Exception )
00140         {
00141             set( 0 );
00142         }
00143 
00149         inline T*
00150         operator->() const                      throw ( Exception )
00151         {
00152             if ( !object ) {
00153                 throw Exception( __FILE__, __LINE__,
00154                                  "reference to NULL object");
00155             }
00156             return object;
00157         }
00158 
00166         inline Ref<T> &
00167         operator= ( Ref<T>  other )             throw ( Exception )
00168         {
00169             set( other.object);
00170             return *this;
00171         }
00172 
00180         inline Ref<T> &
00181         operator= ( T*  obj )                   throw ( Exception )
00182         {
00183             set( obj);
00184             return *this;
00185         }
00186 
00194         inline void
00195         set ( T   * newobj )                    throw ( Exception )
00196         {
00197             // If equal do nothing
00198             if ( newobj == object ) {
00199                 return;
00200             }
00201 
00202             // Increase reference count
00203             if ( newobj ) {
00204                 newobj->increaseReferenceCount();
00205             }
00206 
00207             // Decrease the reference count of the old referable
00208             if ( object ) {
00209                 if ( object->decreaseReferenceCount() == 0 ) {
00210                     delete object;
00211                 }
00212             }
00213 
00214             // Assign
00215             object = newobj;
00216         }
00217 
00229         inline T*
00230         get ( void ) const                      throw ()
00231         {
00232             return object;
00233         }
00234 
00242         inline bool
00243         operator== ( const T        * other ) const     throw ()
00244         {
00245             return object == other;
00246         }
00247 
00255         inline bool
00256         operator== ( const Ref<T> &   other ) const     throw ()
00257         {
00258             return object == other.object;
00259         }
00260 
00268         inline bool
00269         operator!= ( const T        * other ) const     throw ()
00270         {
00271             return object != other;
00272         }
00273 
00281         inline bool
00282         operator!= ( const Ref<T> &   other ) const     throw ()
00283         {
00284             return object != other.object;
00285         }
00286 };
00287 
00288 /* ================================================= external data structures */
00289 
00290 
00291 /* ====================================================== function prototypes */
00292 
00293 
00294 
00295 #endif  /* REF_H */
00296 
00297 
00298 /*------------------------------------------------------------------------------
00299  
00300   $Source: /cvsroot/darkice/darkice/src/Ref.h,v $
00301 
00302   $Log: Ref.h,v $
00303   Revision 1.5  2002/08/20 20:07:36  darkeye
00304   minor fixes
00305 
00306   Revision 1.4  2002/02/20 11:51:27  darkeye
00307   added equality operators to compare with pointers
00308 
00309   Revision 1.3  2000/11/11 14:55:31  darkeye
00310   minor bugfix
00311 
00312   Revision 1.2  2000/11/11 12:33:13  darkeye
00313   added kdoc-style documentation
00314 
00315   Revision 1.1.1.1  2000/11/05 10:05:54  darkeye
00316   initial version
00317 
00318   
00319 ------------------------------------------------------------------------------*/
00320 

Generated on Thu Apr 14 13:59:12 2005 for DarkIce by  doxygen 1.4.1