home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / dos / prg / todb101 / todb / lib / kspersis.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-26  |  3.2 KB  |  133 lines

  1. // ========================================================================
  2. //  TODB LIBRARY
  3. //    kspersis.h
  4. //
  5. //    KSPersistent class
  6. //
  7. //    Version: see TODB.H file
  8. //
  9. //    Copyright 1993 Christian Thérien
  10. //    All rights reserved
  11. // ========================================================================
  12.  
  13. #ifndef _KLPERSIS_H
  14. #define _KLPERSIS_H
  15.  
  16. #include "fblock.h"
  17. #include "boolean.h"
  18. #include <stdio.h>
  19.  
  20. class ErrReporter;
  21.  
  22. enum KSPersistentError
  23. {
  24.     KSP_FHEAD
  25. };
  26.  
  27. class KSPersistent
  28. {
  29. public:
  30.     virtual ~KSPersistent();
  31.  
  32.     // -- called from derived class's constructor
  33.     void LoadObject( ObjAdr oa = 0 );
  34.     void LoadFirstBlock( ObjAdr oa = 0 );
  35.  
  36.     // -- called from derived class's destructor
  37.     virtual void SaveObject();
  38.  
  39.     // -- class interface methods for modifying database
  40.     Boolean AddObject();
  41.     void ChangeObject();
  42.     void DeleteObject();
  43.     ObjAdr ObjectAddress() const;
  44.  
  45.     // error control
  46.     static void SetErrOut( ErrReporter * er );
  47.  
  48. protected:
  49.     KSPersistent( TODB & db, int cid );
  50.  
  51.     // -- derived class methods for reading members
  52.     void ReadObject( void * buf, int length );
  53.     void ReadObject( String & str );
  54.     void ReadObject( unsigned int & i );
  55.     void ReadObject( unsigned long & l );
  56.  
  57.     // -- derived class methods for writing members
  58.     void WriteObject( void * buf, int length );
  59.     void WriteObject( String & str );
  60.     void WriteObject( unsigned int i );
  61.     void WriteObject( unsigned long l );
  62.  
  63.     // -- specialized class members for reading and writing
  64.     KeyNbr GetCount();
  65.     void AppendIf( KeyNbr count, long pos, KeyType k );
  66.  
  67.     // -- provided by derived class
  68.     virtual void Write() = 0;
  69.     virtual void Read() = 0;
  70.  
  71. private:
  72.     ObjHeader objhdr_;
  73.     BlockNbr objaddress_;      // first block address for this object
  74.     TODB & todb_;           // database for this object
  75.     Block * firstblock_;       // first block for reading/writing
  76.     Block * curblock_;           // current block for reading/writing
  77.     int offset_;           // current char position
  78.     Boolean changed_;           // true if user changed the object
  79.     Boolean deleted_;           // true if user deleted the object
  80.     Boolean newobject_;           // true if user is adding the object
  81.     BlockNbr lastblock_;
  82.  
  83.     void ObjectOut();
  84.     void ReadFirstBlock();
  85.     void UpdateFirstBlockHeader();
  86.  
  87.     // -- error control
  88.     static ErrReporter * ErrOut;
  89.     static void ErrorHandler( KSPersistentError err );
  90. };
  91.  
  92. inline KSPersistent::~KSPersistent()
  93. {
  94.     delete firstblock_;
  95. }
  96.  
  97. inline void KSPersistent::ChangeObject()
  98. {
  99.     changed_ = True;
  100. }
  101.  
  102. inline void KSPersistent::DeleteObject()
  103. {
  104.     deleted_ = True;
  105. }
  106.  
  107. inline ObjAdr KSPersistent::ObjectAddress() const
  108. {
  109.     return objaddress_;
  110. }
  111.  
  112. inline void KSPersistent::ReadObject( unsigned int & i )
  113. {
  114.     ReadObject( &i, sizeof(int) );
  115. }
  116.  
  117. inline void KSPersistent::ReadObject( unsigned long & l )
  118. {
  119.     ReadObject( &l, sizeof(long) );
  120. }
  121.  
  122. inline void KSPersistent::WriteObject( unsigned int i )
  123. {
  124.     WriteObject( &i, sizeof(int) );
  125. }
  126.  
  127. inline void KSPersistent::WriteObject( unsigned long l )
  128. {
  129.     WriteObject( &l, sizeof(long) );
  130. }
  131.  
  132. #endif // _KLPERSIS_H
  133.