00001 #ifndef __J2K__PThread_Handle_HPP__ 00002 #define __J2K__PThread_Handle_HPP__ 00003 00004 #include <j2k/Fred/Basic.hpp> 00005 #include <j2k/Fred/Boolean.hpp> 00006 #include <j2k/Fred/StdTypes.hpp> 00007 #include <j2k/Fred/Object.hpp> 00008 00009 #include <pthread.h> 00010 00011 /* A reference-counting, thread-safe smart pointer template. 00012 * It can point to a free-store object of any type, and can be dereferenced 00013 * using -> and *. The Handle itself should be passed by value, 00014 * and should not be allocated on the free store. 00015 * 00016 * When there are no more Handles that point to an object, 00017 * the object is deleted. A Handle<T1> can be converted 00018 * to a Handle<T2> if a T1* can be converted to a T2* 00019 * (e.g. if T1 is derived from T2). 00020 * 00021 * When converted to a boolean value, 00022 * a Handle is false if the underlying pointer is null. 00023 */ 00024 00025 class Handle { 00026 public: 00027 Handle(); 00028 Handle( JObject* obj ); 00029 Handle( const Handle& right ); 00030 00031 Handle( JObject* obj, ULONG* rc, 00032 pthread_mutexattr_t* a, pthread_mutex_t* m ); 00033 00034 virtual ~Handle(); 00035 00036 Handle& operator=(const Handle& right); 00037 00038 JObject* operator->() const; 00039 JObject& operator*() const; 00040 00041 operator BOOL() const; 00042 00043 private: 00044 inline void addRef(); 00045 void removeRef(); 00046 00047 private: 00048 JObject* object; 00049 ULONG* refCount; 00050 pthread_mutexattr_t* mutexAttr; 00051 pthread_mutex_t* mutex; 00052 }; 00053 00054 #endif