ESLA

Embeddable Scriting LAnguage

frantz@pangea.stanford.edu

Stanford University, Rock Fracture Project research group

© 2003

smart_pointer.h

Go to the documentation of this file.
00001 /*************************************************************************
00002  *              ESLA: Embeddable Scripting LAnguage
00003  *                  Copyright (C) 2003 Frantz Maerten
00004  *
00005  *   This  program  is  free  software;  you  can  redistribute it and/or
00006  *   modify  it  under  the  terms  of  the GNU General Public License as
00007  *   published  by  the Free Software Foundation; either version 2 of the
00008  *   License, or (at your option) any later version.
00009  *
00010  *   If  you modify this software, you should contact the author, include
00011  *   a  notice giving the name of the person performing the modification,
00012  *   the date of modification, and the reason for such modification.
00013  *
00014  *   Note   that   the   GNU  General  Public  License  does  not  permit
00015  *   incorporating the Software into proprietary programs.
00016  *
00017  *  Contact: Frantz Maerten
00018  *     frantz@pangea.stanford.edu
00019  *
00020  *     Dept. of Geological & Environmental Sciences
00021  *     Stanford University 
00022  *     Stanford, CA 94305-2115
00023  *     USA
00024  *
00025  * Adapted from Bruno Levy (levy@loria.fr)
00026  *************************************************************************/
00027 
00028 
00029 #ifndef SMART_POINTER__
00030 #define SMART_POINTER__
00031 
00032 #include "../esla_namespace.h"
00033 #include <assert.h>
00034 
00035 #ifndef NULL
00036 #define NULL 0
00037 #endif
00038 
00039 //------------------------------------------------------------------
00040 
00041 BEGIN_LIB_NAMESPACE
00042 
00043 template <class T>
00044 class SmartPointer {
00045 
00046 public:
00047     SmartPointer() ;
00048     SmartPointer(T* ptr) ;
00049     SmartPointer(const SmartPointer<T>& rhs) ;
00050     ~SmartPointer() ;
00051 
00052     SmartPointer<T>& operator=(T* ptr) ;
00053     SmartPointer<T>& operator=(const SmartPointer<T>& rhs) ;
00054 
00060     void forget() ;
00061 
00062     T* operator->() const ;
00063     T& operator*() const ;
00064     operator T*() const ;
00065 
00069 #ifdef WIN32
00070     bool operator<(const SmartPointer<T>& p) const {return true ;}
00071     bool operator==(const SmartPointer<T>& p) const {return pointer_==p.pointer_;}
00072 #endif
00073 
00077     bool is_nil() const ;
00078 
00079 private:
00080     T* pointer_ ;
00081 
00082 } ;
00083 
00084 //------------------------------------------------------------------
00085 
00086 template <class T> inline 
00087 SmartPointer<T>::SmartPointer() : pointer_(NULL) {
00088 }
00089 
00090 template <class T> inline 
00091 SmartPointer<T>::SmartPointer(T* ptr) : pointer_(ptr) {
00092     T::inc_ref(pointer_) ;
00093 }
00094 
00095 template <class T> inline 
00096 SmartPointer<T>::SmartPointer(
00097     const SmartPointer<T>& rhs
00098 ) : pointer_(rhs) {
00099     T::inc_ref(pointer_) ;
00100 } 
00101 
00102 template <class T> inline
00103 SmartPointer<T>::~SmartPointer() {
00104     T::dec_ref(pointer_) ;
00105 }
00106 
00107 template <class T> inline
00108 SmartPointer<T>& SmartPointer<T>::operator=(T* ptr) {
00109     if(ptr != pointer_) {
00110         T::dec_ref(pointer_) ;
00111         pointer_ = ptr ;
00112         T::inc_ref(pointer_) ;
00113     }
00114     return *this ;
00115 }
00116 
00117 template <class T> inline
00118 SmartPointer<T>& SmartPointer<T>::operator=(const SmartPointer<T>& rhs) {
00119     T* rhs_p = rhs ; 
00120     if(rhs_p != pointer_) {
00121         T::dec_ref(pointer_) ;
00122         pointer_ = rhs_p ;
00123         T::inc_ref(pointer_) ;
00124     }
00125     return *this ;
00126 }
00127 
00128 template <class T> inline
00129 void SmartPointer<T>::forget() {
00130     T::rec_ref(pointer_) ;
00131     pointer_ = NULL ;
00132 }
00133 
00134 template <class T> inline
00135 T* SmartPointer<T>::operator->() const {
00136     assert(pointer_ != NULL) ;
00137     return pointer_ ;
00138 }
00139 
00140 template <class T> inline
00141 T& SmartPointer<T>::operator*() const {
00142     assert(pointer_ != NULL) ;
00143     return *pointer_ ;
00144 }
00145 
00146 template <class T> inline 
00147 SmartPointer<T>::operator T*() const {
00148     return pointer_ ;
00149 }
00150 
00151 template <class T> inline 
00152 bool SmartPointer<T>::is_nil() const {
00153     return (pointer_ == NULL) ;
00154 }
00155 
00156 
00157 END_LIB_NAMESPACE
00158 
00159 //------------------------------------------------------------------
00160 
00161 #endif
00162 

Generated on Wed May 14 11:42:31 2003 for Esla-lib by doxygen1.3-rc1