00001 /******************************************************************************/ 00002 /* */ 00003 /* POLiTe - Persistent Object Library Test */ 00004 /* Ph.D. Thesis by Mgr. Michal Kopecky */ 00005 /* */ 00006 /* Charles University Prague */ 00007 /* */ 00008 /******************************************************************************/ 00009 /* */ 00010 /* File name: ... */ 00011 /* Module: ...... */ 00012 /* */ 00013 /******************************************************************************/ 00014 /* */ 00015 /* Abstract common ancestor class of RefBase and Object. */ 00016 /* */ 00017 /******************************************************************************/ 00018 00019 #ifndef __C_OBJREF_H__ 00020 #define __C_OBJREF_H__ 00021 00022 // Standard Header(s) 00023 #include <stdio.h> 00024 00025 // Common POLiTe Header(s) 00026 #include <lStr.h> 00027 #include <lTypes.h> 00028 #include <lTrace.h> 00029 #include <lDefs.h> 00030 #include <lExceptions.h> 00031 00032 // Parent Header(s) 00033 #include <cQueRefProto.h> // ObjRefProto class header 00034 00036 // 00037 // Object - Persistent Object Module : 00038 // See Gc_Obj.cpp for the code to the member functions. 00039 // 00041 00042 class DLL_External ObjRef: public QueRefProto 00043 { 00044 #ifdef POLITE_TEST_FRIEND 00045 friend POLITE_TEST_FRIEND; 00046 #endif // POLITE_TEST_FRIEND 00047 00048 friend class ObjectBuffer; 00049 friend class ProtoBase; 00050 00051 protected: 00052 unsigned short _Strategies; 00053 // Holds all four strategies, each in three independent bits 00054 class Connection *_Connection; // moved from RefBase class 00055 00056 public: 00057 ObjRef(); // constructor 00058 00059 virtual ~ObjRef(); // destructor 00060 00062 00063 protected: 00064 long int _PtrToOid(const ObjRef &ref) const; 00065 class RefBase _OidToPtr( 00066 const long int oid, 00067 class ProtoBase *prot 00068 ) const; 00069 class Object *_OidToMemPtr( 00070 const long int oid, 00071 class ProtoBase *prot 00072 ) const; 00073 00074 public: 00075 // Database Object contents a pointer to Database Connection instead of a pointer to Database 00076 // virtual class Database *DatabaseID() = 0; //Returns the pointer to database the object is stored in. 00077 00078 virtual Object* IsInMemory() const = 0; 00079 00080 virtual class Connection *Connection() const 00081 // Returns the pointer to database connection referenced object is 00082 // (should be) loaded. 00083 { 00084 return _Connection; 00085 }; 00086 00087 virtual class ObjectIdentification ObjectID() const = 0; 00088 //Returns identification of 00089 //an object independent on the instance location 00090 //It consists of either memory pointer, 00091 //or couple of prototype an comma-separated list of primary key values 00092 00093 virtual class ProtoBase *Prototype() const = 0; 00094 // Returns a reference to a prototype instance of the referenced class 00095 00097 00098 public: 00099 virtual bool IsDirty() const = 0; 00100 //Returns true, if an object is marked as dirty (e.g. the 00101 //object was changed in the memory). 00102 virtual bool MarkAsDirty() = 0; 00103 //Marks a persistent object as Dirty. Only Dirty (changed) 00104 //objects are written to the database when flushing the buffer. 00105 00106 protected: 00107 virtual bool _MarkAsClean() = 0; 00108 //Marks a persistent object as Clean. Only Dirty (changed) 00109 //objects are written to the database when flushing the buffer. 00110 00112 00113 public: 00114 virtual bool Refresh() = 0; 00115 // It copies an object from disk to the memory again. 00116 // Returns true, if it succeed. 00117 00118 virtual bool Update() =0; 00119 //Writes an object from the memory to the disk. 00120 00121 virtual bool Free() =0; 00122 //Tries to destroy an object in the memory only. 00123 //Succeeds if all the tests 00124 //(for example, if the object is not locked) 00125 //are passed successfully. 00126 00127 protected : 00128 virtual bool _Free() = 0; 00129 //Destroys an object in the memory. _Free is called by the 00130 //method Free, if all the tests passed. _Free is not visible 00131 //for the programmers of the target application. 00132 00133 public: 00134 virtual bool Delete() = 0; 00135 //Tries to destroy an object from the memory as well as 00136 //from the database. Succeeds if all the tests (for 00137 //example, if the object is not locked) are passed 00138 //successfully. 00139 00140 virtual class Object *MemoryLock() = 0; 00141 //Locks a persistent object. Multiple locks 00142 //can be placed on the object. The object can be 00143 //removed from memory only when all the locks are 00144 //released. 00145 00146 virtual bool MemoryUnlock() = 0; 00147 //Unlocks a persistent object in memory. See MemoryLock. 00148 00149 virtual unsigned int MemoryLocked() const = 0; 00150 //Returns the number of memory locks on receiver or zero 00151 //if it is not locked. 00152 00153 virtual bool RemoveAllMemoryLocks() 00154 //Removes all memory locks existing in the GEN.LIB 00155 { 00156 return true; 00157 }; 00158 00159 virtual bool IsTransient() const 00160 //Returns TRUE if an instance of DatabaseObject is transient, 00161 //i.e. if it has no copy in database. This can happen if the 00162 //object was created as transient and was not made persistent 00163 //by calling BePersistent yet. 00164 { 00165 return (Connection() == NULL); 00166 }; 00167 00168 virtual bool IsPersistent() const 00169 //Returns TRUE if the object is really persistent, otherwise 00170 //it returns false. This function returns the opposite value 00171 //to the method IsTransient. 00172 { 00173 return (Connection() != NULL); 00174 }; 00175 00176 virtual class RefBase BePersistent(class Connection *DbCon) = 0; 00177 //Causes an object to be persistent. 00178 //Persistent objects are created as transient (they exist only 00179 //in memory) and when it is needed (all attributes are set to 00180 //appropriate values), then they can be converted to true 00181 //persistent objects. An advantage is that transient objects do 00182 //not propagate into a database while persistent objects are 00183 //written (in spite of buffering mechanism) to the database 00184 //often. 00185 00186 // Get and set corresponging patrs of attribute _Strategies 00187 // that holds all concurency strategies 00188 virtual enum UpdatingStrategy CurrentUpdatingStrategy() const 00189 // Returns Update Strategy part of attribute _Strategies 00190 { 00191 return (enum UpdatingStrategy)(_Strategies & US_MASK); 00192 }; 00193 virtual bool SetUpdatingStrategy(enum UpdatingStrategy anUpdateStrategy); 00194 virtual enum WaitingStrategy CurrentWaitingStrategy() const 00195 { 00196 return (enum WaitingStrategy)(_Strategies & WS_MASK); 00197 }; 00198 virtual bool SetWaitingStrategy(enum WaitingStrategy aWaitingStrategy); 00199 virtual enum LockingStrategy CurrentLockingStrategy() const 00200 { 00201 return (enum LockingStrategy)(_Strategies & LS_MASK); 00202 }; 00203 virtual bool SetLockingStrategy(enum LockingStrategy aLockingStrategy); 00204 virtual enum ReadingStrategy CurrentReadingStrategy() const 00205 { 00206 return (enum ReadingStrategy)(_Strategies & RS_MASK); 00207 }; 00208 virtual bool SetReadingStrategy(enum ReadingStrategy aReadingStrategy); 00209 00210 protected: 00211 virtual char *_Where() const; 00212 virtual char *_OrderBy() const {return NULL;}; 00213 00214 virtual char *_KeyValues() const = 0; 00215 // return comma-separated list of encoded key values 00216 // encoding allows recognising of ends of individual elements 00217 00218 public: 00219 virtual bool IsAncestor(const ObjRef &descendant) const; 00220 // Returns if *this Object is ancestor 00221 // of *descendant Object 00222 }; 00223 00224 #endif //__C_OBJREF_H__