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
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
00038
00039 #include "Exception.h"
00040
00041
00042
00043
00044
00045
00046
00047
00048
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
00198 if ( newobj == object ) {
00199 return;
00200 }
00201
00202
00203 if ( newobj ) {
00204 newobj->increaseReferenceCount();
00205 }
00206
00207
00208 if ( object ) {
00209 if ( object->decreaseReferenceCount() == 0 ) {
00210 delete object;
00211 }
00212 }
00213
00214
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
00289
00290
00291
00292
00293
00294
00295 #endif
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320