Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Deprecated/Posix.old/Handle.cpp

Go to the documentation of this file.
00001 #ifndef __J2K__PThread_Handle_CPP__
00002 #define __J2K__PThread_Handle_CPP__
00003 
00004 #include <j2k/Fred/PThread/Handle.hpp>
00005 
00006 inline void Handle::addRef() {
00007   if ( object != NULL ) {
00008     pthread_mutex_lock(mutex);
00009     ++(*refCount);
00010     pthread_mutex_unlock(mutex);
00011   }
00012 }
00013 
00014 void Handle::removeRef() {
00015   if ( object != NULL ) {
00016     pthread_mutex_lock(mutex);
00017 
00018     if ( --(*refCount) == 0 ) {
00019       pthread_mutex_unlock(mutex);
00020       pthread_mutex_destroy(mutex);
00021       pthread_mutexattr_destroy(mutexAttr);
00022 
00023       if ( mutex != NULL ) {
00024         delete mutex;
00025         mutex = NULL;
00026       }
00027 
00028       if ( mutexAttr != NULL ) {
00029         delete mutexAttr;
00030         mutexAttr = NULL;
00031       }
00032 
00033       if ( refCount != NULL ) {
00034         delete refCount;
00035         refCount = NULL;
00036       }
00037 
00038       if ( object != NULL ) {
00039         delete object;
00040         object = NULL;
00041       }
00042 
00043     } else {
00044       pthread_mutex_unlock(mutex);
00045     }
00046 
00047   }
00048 }
00049 
00050 // Create a new Handle for a free-store object.
00051 Handle::Handle() : object( NULL ), refCount(  NULL ),
00052                    mutex(  NULL ), mutexAttr( NULL ) { }
00053 
00054 // Create a new Handle for a free-store object.
00055 Handle::Handle( JObject* obj )
00056   : object( obj )
00057 {
00058 
00059   if ( object != NULL ) {
00060     refCount  = new ULONG(1);
00061     mutexAttr = new pthread_mutexattr_t;
00062     mutex     = new pthread_mutex_t;
00063     pthread_mutexattr_init(mutexAttr);
00064     pthread_mutex_init(mutex, mutexAttr);
00065   } else {
00066     refCount  = NULL;
00067     mutexAttr = NULL;
00068     mutex     = NULL;
00069   }
00070 }
00071 
00072 Handle::Handle( const Handle& right )
00073   : object( right.object ), refCount(  right.refCount  ),
00074     mutex(  right.mutex  ), mutexAttr( right.mutexAttr )
00075 {
00076   addRef();
00077 }
00078 
00079 Handle::Handle( JObject* obj, ULONG* rc,
00080                 pthread_mutexattr_t* a, pthread_mutex_t* m )
00081   : object( obj ), refCount( rc ), mutexAttr( a ), mutex( m )
00082 {
00083   addRef();
00084 }
00085 
00086 virtual Handle::~Handle() {
00087   removeRef();
00088 }
00089 
00090 Handle&  Handle::operator=( const Handle& right ) {
00091   if (object != right.object) {
00092     removeRef();
00093     object    = right.object;
00094     mutexAttr = right.mutexAttr;
00095     mutex     = right.mutex;
00096     refCount  = right.refCount;
00097 
00098     addRef();
00099   }
00100 
00101   return *this;
00102 }
00103 
00104 // Dereferences the Handle to access a member of the object pointed to.
00105 JObject*  Handle::operator->() const {
00106   return object;
00107 }
00108 
00109 // Returns the object pointed to.
00110 JObject&  Handle::operator*() const {
00111   return *object;
00112 }
00113 
00114 // Return TRUE, if the underlying pointer is non-null.
00115 Handle::operator BOOL() const {
00116   return ( object != NULL );
00117 }
00118 
00119 #endif

Generated on Sun Oct 14 18:46:30 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001