From: Brian W. <bw...@st...> - 2002-09-30 01:39:07
|
At the moment I am ( in spare moments ) putting together my Adjustable Logging patch. At the moment I am putting together some file locking, and I thought might give a progress report on a couple of aspects and get some feedback. Ok. I have changed the way I want to do file locking. Given there are two completely separate implementation underneath, I thought I should define an interface class and a factory method. The interface class is class LockedFile { private: //hide the delete operator void operator delete( void*) {} public: //Destructor virtual int Close() = 0; // Will return true if the object // is currently locked virtual int IsLocked() = 0; // Will attempt to lock the file // Will return true if the file is already // locked by this object or if it was able // to successfully lock it. // // Will use the wait mode specified in the // constructor virtual int Lock() = 0; // Will attempt to unlock the file // Will return true if the file is already // unlocked by this object or if it was able // to successfully unlock it. virtual int Unlock() = 0; // Will return the FILE* for use in other // application virtual FILE* GetFP() = 0; // Will detach the FILE* and then destroy // the rest of the object virtual FILE* DetachFPAndRemoveLock() = 0; }; And the factory method for creating one of these beasts is: // Factory method for Creating a Locked File // If it can succeed it will return a pointer to // a locked file object. File is closed // by deleting the pointer (??) LockedFile* OpenLockedFile( const char* path, const char* mode, int waitForLock ); LockedFile* OpenLockedFile( const char* path, const char* mode ); A typical usage would be LockedFile* lkdf = OpenLockedFile( "/path/to/file", "w" ); if ( lkdf ) // The factory function will return 0 if it can't // create the locked file { // Use the GetFP method to access the FILE* for use in things // like fprintf fprintf( lkdf->GetFP(), "Hello World\n" ); // This will remove the lock, close the file and destroy the object lkdf->Close(); } The aim is to provide a general lockfile class. If anyone can knows a way to autocast a LockFile* to a FILE* it would be good. I *could* add an operator like this: public: operator FILE*(){ return GetFP(); } which would allow this to work: fprintf( *lkdf, "Hello World\n" ); but I thought that resolving a pointer to create a pointer is a trifle counter-intuitive and could be a cause of major confusion. The other thing is that I have been working my through and attmepting to debug things very carefully. Part of doing this was that I defined myself some TRACE macros: #ifndef USE_TRACE_MESSAGES #define TRACE(S) #define TRACEVAR(X) #define TRACEPVAR(P,X) #else #define TRACE(S) printf("%s:%d:%s\n", __FILE__, __LINE__, S ); #define TRACEVAR(X) printf("%s:%d:%s=%s\n", __FILE__, __LINE__, \ #X, TraceVarStr(X) ); #define TRACEPVAR(P,X) printf("%s:%d:%s=%s\n", __FILE__, __LINE__, \ #P "->" #X, \ ( P ? TraceVarStr(P->X) : "[" #P " is NULL]" )); ( version of TraceVarStr for every type you wish to trace ) #endif These can be left in the code, and will be preprocessed out of existence for normal use. My question here is : is there any existing feature or philospohy used in the code that does this kind of thing? ( regex.cc and the db code seem to have some kind of debugging code ) Is is worth having available as a generally usable thing? Regs Brian White ------------------------- Brian White Step Two Designs Pty Ltd Knowledge Management Consultancy, SGML & XML Phone: +612-93197901 Web: http://www.steptwo.com.au/ Email: bw...@st... Content Management Requirements Toolkit 112 CMS requirements, ready to cut-and-paste |