CCAFFEINE
0.8.8
|
00001 #ifndef RefObject_seen 00002 #define RefObject_seen 00003 00004 class SafeRef; 00005 00006 class RefCountedObject : public virtual JCPN(Object) { 00007 private: 00008 CCAFEMutex mut; // BUGBUG - how lightweight are mutexes??? 00009 int refCount; 00010 protected: 00011 friend SafeRef; 00012 int addRef() { 00013 CCAFEMutexLock lock(&mut); 00014 return refCount++; 00015 }; 00016 // BUGBUG - throw when decremented too far? 00017 int deleteRef() { 00018 CCAFEMutexLock lock(&mut); 00019 return refCount--; 00020 }; 00021 public: 00022 RefCountedObject() { 00023 refCount = 0; 00024 }; 00025 ~RefCountedObject() { 00026 }; 00027 }; 00028 00029 class SafeRef :public virtual JCPN(Object) { 00030 private: 00031 RefCountedObject* obj; 00032 public: 00033 SafeRef(RefCountedObject* obj) { 00034 this->obj = obj; 00035 if (obj) 00036 obj->addRef(); 00037 }; 00038 ~SafeRef() { 00039 int count = obj->deleteRef(); 00040 if (count <= 0) 00041 { 00042 delete obj; 00043 obj = null; 00044 } 00045 }; 00046 RefCountedObject* getObject() { 00047 return obj; 00048 }; 00049 }; 00050 00051 00052 class ExceptionSafeFileDescriptor { 00053 private: 00054 FILE* fileid; 00055 public: 00056 ExceptionSafeFileDescriptor() { fileid = NULL; }; 00057 ExceptionSafeFileDescriptor(FILE* fileid) 00058 { 00059 this->fileid = fileid; 00060 }; 00061 FILE* getFileId () { return fileid; }; 00062 void setFileId (FILE* fileid) { this->fileid = fileid; }; 00063 00064 ~ExceptionSafeFileDescriptor() 00065 { 00066 ::fclose(fileid); 00067 }; 00068 }; 00069 00070 #endif // RefObject_seen