Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

mk4.h

Go to the documentation of this file.
00001 // mk4.h --
00002 // $Id: mk4.h,v 1.40 2004/01/28 21:33:39 jcw Exp $
00003 // This is part of Metakit, see http://www.equi4.com/metakit/
00004 
00009 #ifndef __MK4_H__
00010 #define __MK4_H__
00011 
00012 //---------------------------------------------------------------------------
00013 //
00014 //  TITLE
00015 //                                
00016 //      The Metakit Library, by Jean-Claude Wippler, Equi4 Software, NL.
00017 //      
00018 //  DESCRIPTION
00019 //                                
00020 //      Structured data storage with commit / rollback and on-demand loading.
00021 //  
00022 //  ACKNOWLEDGEMENTS
00023 //                                                                        
00024 //      To Liesbeth and Myra, for making this possible.
00025 //
00026 //---------------------------------------------------------------------------
00027 //
00028 //  NAMING CONVENTIONS        PREFIX    REMARKS
00029 //                              
00030 //      Compile time options    q4_     Always defined as 1 or 0, capitalized
00031 //      Preprocessor defines    d4_     Use with "#ifdef" or "#if defined()"
00032 //      Classes                 c4_     Classes, listed at start of headers
00033 //      Typedefs                t4_     Type definitions, if outside classes
00034 //      Global functions        f4_     Internal, these are rarely defined
00035 //
00036 //      Member functions                Start in uppercase
00037 //      Instance variables      _       And start in lowercase
00038 //      Static members          _       And start in uppercase
00039 //
00040 //      Local variable names            Start in lowercase
00041 //      Formal parameter names          Start lowercase, end with underscore
00042 //
00043 //---------------------------------------------------------------------------
00044 
00046 #define d4_MetakitLibraryVersion 249    // 2.4.9.4 release, Jun 10, 2005
00047 #define d4_MetaKitLibraryVersion d4_MetakitLibraryVersion // compat, yuck
00048 
00049 //---------------------------------------------------------------------------
00050 // Declarations in this file
00051 
00052     class c4_View;                      // a view on underlying data
00053     class c4_Cursor;                    // an index into a view
00054     class c4_RowRef;                    // a reference to a row
00055         class c4_Row;                   // one row in a view
00056     class c4_Bytes;                     // used to pass around generic data
00057     class c4_Storage;                   // manages view persistence
00058     class c4_CustomViewer;              // used for customizable views
00059     class c4_Stream;                    // abstract stream class
00060     class c4_Strategy;                  // system and file interface
00061 
00062     class c4_Property;                  // for access inside rows
00063         class c4_IntProp;
00064         class c4_LongProp;
00065         class c4_FloatProp;
00066         class c4_DoubleProp;
00067         class c4_StringProp;
00068         class c4_BytesProp;
00069         class c4_ViewProp;
00070 
00071     // Everything below is part of the implementation, not for public use
00072 
00073     class c4_Sequence;                  // a collection of rows
00074 
00075     class c4_Reference;                 // refers to the actual data values
00076         class c4_IntRef;
00077         class c4_LongRef;
00078         class c4_FloatRef;
00079         class c4_DoubleRef;
00080         class c4_BytesRef;
00081         class c4_StringRef;
00082         class c4_ViewRef;
00083 
00084     class c4_Dependencies;              // not defined here
00085     class c4_Handler;                   // not defined here
00086     class c4_Notifier;                  // not defined here
00087     class c4_Persist;                   // not defined here
00088 
00089 //---------------------------------------------------------------------------
00090 
00091     // determine whether we need to include "mk4dll.h" to link as DLL
00092 #if defined (MKDLL_EXPORTS) && !defined (q4_KITDLL)
00093 #define q4_KITDLL 1
00094 #endif
00095 
00096     // omit floats and doubles in small model 16-bit Intel builds
00097 #if defined (_DOS) && defined (_M_I86SM) && !defined (q4_TINY)
00098 #define q4_TINY 1
00099 #endif
00100 
00101     // and here's the other end of the scale...
00102 #if !defined (_WIN32) && !defined (q4_LONG64)
00103 #if defined (_PA_RISC2_0) || defined (__powerpc64__) || defined(__sparcv9) || \
00104     defined (__x86_64__) || defined (__s390x__) || defined (__alpha) || \
00105     (defined (__ia64) && (!defined (__HP_aCC) || defined(__LP64__)))
00106 #define q4_LONG64 1
00107 #endif
00108 #endif
00109 
00110     // default to inlining for maximum performance
00111 #if !defined (q4_INLINE)
00112 #define q4_INLINE 1
00113 #endif
00114 
00115 //---------------------------------------------------------------------------
00116 
00117     // Borland C++ and C++ Builder
00118 #if defined (__BORLANDC__)
00119     // by default, if runtime is linked as a DLL, then so is Metakit
00120 #if defined (_RTLDLL) && !defined (q4_KITDLL)
00121 #define q4_KITDLL 1
00122 #endif
00123 
00124     // Borland 5.0 supports the bool datatype
00125 #if __BORLANDC__ >= 0x500
00126 #define q4_BOOL 1
00127 #endif
00128 #endif // __BORLANDC__
00129 
00130     // IRIX supports the bool datatype
00131     // define before gcc to cover both the gcc and MipsPRO compiler
00132 #if defined (sgi)
00133 #define q4_BOOL 1
00134 #undef bool
00135 #undef true
00136 #undef false
00137 #endif
00138 
00139     // GNU gcc/egcs
00140 #if defined (__GNUC__)
00141 #ifndef q4_BOOL
00142 #define q4_BOOL 1
00143 #endif
00144 #ifndef HAVE_LONG_LONG
00145 #define HAVE_LONG_LONG 1
00146 #endif
00147 #endif
00148 
00149     // HP aCC
00150 #if defined (__HP_aCC)
00151 #ifndef HAVE_LONG_LONG
00152 #define HAVE_LONG_LONG 1
00153 #endif
00154 #endif
00155 
00156     // Metrowerks CodeWarrior
00157 #if defined (__MWERKS__)
00158 #if __option(bool)
00159 #define q4_BOOL 1       // bool datatype is optionally supported
00160     // undef, these conflict with c4_Storage::c4_Storage overloading
00161 #undef bool
00162 #undef true
00163 #undef false
00164 #endif
00165 #endif
00166 
00167     // Microsoft Visual C++
00168 #if defined (_MSC_VER)
00169     // MSVC 5.0 supports the bool datatype, MSVC 4.x has no namespaces
00170 #if _MSC_VER >= 1100
00171 #define q4_BOOL 1
00172 #define LONG_LONG __int64
00173 #else
00174 #define q4_NO_NS 1
00175 #endif
00176 
00177     // a kludge to avoid having to use ugly DLL exprt defs in this header
00178 #pragma warning(disable: 4273) // inconsistent dll linkage
00179 #endif // _MSC_VER
00180 
00181 //---------------------------------------------------------------------------
00182 // Other definitions needed by the public Metakit library header files
00183 
00184 #if !q4_BOOL && !q4_STD         // define a bool datatype
00185 #define false 0
00186 #define true 1
00187 #define bool int
00188 #endif
00189 
00190 #if q4_KITDLL                   // add declaration specifiers
00191 #include "mk4dll.h"
00192 #endif
00193 
00194 #if q4_INLINE                   // enable inline expansion
00195 #define d4_inline inline
00196 #else
00197 #define d4_inline
00198 #endif
00199 
00200 typedef unsigned char t4_byte;  // create typedefs for t4_byte, etc.
00201 
00202 #if q4_LONG64
00203 typedef int t4_i32;             // if longs are 64b, then int must be 32b
00204 #else
00205 typedef long t4_i32;            // if longs aren't 64b, then they are 32b
00206 #endif
00207 
00208 #if q4_LONG64           // choose a way to represent 64b integers
00209 typedef long t4_i64;
00210 #elif defined (LONG_LONG)
00211 typedef LONG_LONG t4_i64;
00212 #elif HAVE_LONG_LONG
00213 typedef long long t4_i64;
00214 #else
00215 struct t4_i64 { long l1; long l2; };
00216 bool operator== (const t4_i64 a_, const t4_i64 b_);
00217 bool operator< (const t4_i64 a_, const t4_i64 b_);
00218 #endif
00219 
00220 //---------------------------------------------------------------------------
00221 
00222 class c4_View
00223 {
00224 protected:
00225   c4_Sequence* _seq;
00226 
00227 public:
00228 /* Construction / destruction / assignment */
00229   c4_View (c4_Sequence* =0);
00230   c4_View (c4_CustomViewer*);
00231   c4_View (c4_Stream*);
00232   c4_View (const c4_Property& property_);
00233   c4_View (const c4_View&);
00234   ~c4_View ();
00235   
00236   c4_View& operator= (const c4_View&);
00237   c4_Persist* Persist() const; // added 16-11-2000 to simplify c4_Storage
00238 
00239 /* Getting / setting the number of rows */
00240   int GetSize() const;    
00241   void SetSize(int, int =-1); 
00242 
00243   void RemoveAll();
00244 
00245 /*: Getting / setting individual elements */
00246   c4_RowRef GetAt(int) const;
00247   c4_RowRef operator[] (int) const;
00248   
00249   void SetAt(int, const c4_RowRef&);
00250   c4_RowRef ElementAt(int);
00251 
00252   bool GetItem(int, int, c4_Bytes&) const;
00253   void SetItem(int, int, const c4_Bytes&) const;
00254 
00255 /* These can increase the number of rows */
00256   void SetAtGrow(int, const c4_RowRef&);   
00257   int Add(const c4_RowRef&);
00258 
00259 /* Insertion / deletion of rows */
00260   void InsertAt(int, const c4_RowRef&, int =1);
00261   void RemoveAt(int, int =1);
00262   void InsertAt(int, const c4_View&);
00263 
00264   bool IsCompatibleWith(const c4_View&) const;
00265   void RelocateRows(int, int, c4_View&, int);
00266 
00267 /* Dealing with the properties of this view */
00268   int NumProperties() const; 
00269   const c4_Property& NthProperty(int) const;
00270   int FindProperty(int);
00271   int FindPropIndexByName(const char*) const;
00272   c4_View Duplicate() const;
00273   c4_View Clone() const;
00274   int AddProperty(const c4_Property&);
00275   c4_View operator, (const c4_Property&) const;
00276 
00277   const char* Description() const;
00278   
00279 /* Derived views */
00280   c4_View Sort() const;
00281   c4_View SortOn(const c4_View&) const;
00282   c4_View SortOnReverse(const c4_View&, const c4_View&) const;
00283 
00284   c4_View Select(const c4_RowRef&) const;
00285   c4_View SelectRange(const c4_RowRef&, const c4_RowRef&) const;
00286 
00287   c4_View Project(const c4_View&) const;
00288   c4_View ProjectWithout(const c4_View&) const;
00289 
00290   int GetIndexOf(const c4_RowRef&) const;
00291   int RestrictSearch(const c4_RowRef&, int&, int&);
00292 
00293 /* Custom views */
00294   c4_View Slice(int, int =-1, int =1) const;
00295   c4_View Product(const c4_View&) const;
00296   c4_View RemapWith(const c4_View&) const;
00297   c4_View Pair(const c4_View&) const;
00298   c4_View Concat(const c4_View&) const;
00299   c4_View Rename(const c4_Property&, const c4_Property&) const;
00300 
00301   c4_View GroupBy(const c4_View&, const c4_ViewProp&) const;
00302   c4_View Counts(const c4_View&, const c4_IntProp&) const;
00303   c4_View Unique() const;
00304 
00305   c4_View Union(const c4_View&) const;
00306   c4_View Intersect(const c4_View&) const;
00307   c4_View Different(const c4_View&) const;
00308   c4_View Minus(const c4_View&) const;
00309 
00310   c4_View JoinProp(const c4_ViewProp&, bool =false) const;
00311   c4_View Join(const c4_View&, const c4_View&, bool =false) const;
00312 
00313   c4_View ReadOnly() const;
00314   c4_View Hash(const c4_View&, int =1) const;
00315   c4_View Blocked() const;
00316   c4_View Ordered(int =1) const;
00317   c4_View Indexed(const c4_View&, const c4_View&, bool =false) const;
00318 
00319 /* Searching */
00320   int Find(const c4_RowRef&, int =0) const;
00321   int Search(const c4_RowRef&) const;
00322   int Locate(const c4_RowRef&, int* =0) const;
00323   
00324 /* Comparing view contents */
00325   int Compare(const c4_View&) const;
00326 
00327   friend bool operator== (const c4_View&, const c4_View&);
00328   friend bool operator!= (const c4_View&, const c4_View&);
00329   friend bool operator< (const c4_View&, const c4_View&);
00330   friend bool operator> (const c4_View&, const c4_View&);
00331   friend bool operator<= (const c4_View&, const c4_View&);
00332   friend bool operator>= (const c4_View&, const c4_View&);
00333   
00334 protected:
00335   void _IncSeqRef();
00336   void _DecSeqRef();
00337 
00339   friend class c4_ViewRef;
00340 
00341   // DROPPED: Structure() const;
00342   // DROPPED: Description(const c4_View& view_);
00343 };
00344 
00345 //---------------------------------------------------------------------------
00346 
00347 #if defined(os_aix) && defined(compiler_ibmcxx) && (compiler_ibmcxx > 500)
00348   bool operator== (const c4_RowRef& a_, const c4_RowRef& b_);
00349   bool operator!= (const c4_RowRef& a_, const c4_RowRef& b_);
00350   bool operator<= (const c4_RowRef& a_, const c4_RowRef& b_);
00351   bool operator>= (const c4_RowRef& a_, const c4_RowRef& b_);
00352   bool operator> (const c4_RowRef& a_, const c4_RowRef& b_);
00353   bool operator< (const c4_RowRef& a_, const c4_RowRef& b_);
00354 #endif
00355 
00356 class c4_Cursor
00357 {
00358 public: 
00360   c4_Sequence* _seq;
00362   int _index;
00363 
00364 /* Construction / destruction / dereferencing */
00366   c4_Cursor (c4_Sequence&, int);
00367   
00369   c4_RowRef operator* () const;
00370   
00372   c4_RowRef operator[] (int) const;
00373 
00374 /* Stepping the iterator forwards / backwards */
00376   c4_Cursor& operator++ ();
00378   c4_Cursor operator++ (int);
00380   c4_Cursor& operator-- ();
00382   c4_Cursor operator-- (int);
00383 
00385   c4_Cursor& operator+= (int);
00387   c4_Cursor& operator-= (int);
00388 
00390   c4_Cursor operator- (int) const;
00392   int operator- (c4_Cursor) const;
00393   
00395   friend c4_Cursor operator+ (c4_Cursor, int);
00397   friend c4_Cursor operator+ (int, c4_Cursor);
00398 
00399 /* Comparing row positions */
00401   friend bool operator== (c4_Cursor, c4_Cursor);
00403   friend bool operator!= (c4_Cursor, c4_Cursor);
00405   friend bool operator< (c4_Cursor, c4_Cursor);
00407   friend bool operator> (c4_Cursor, c4_Cursor);
00409   friend bool operator<= (c4_Cursor, c4_Cursor);
00411   friend bool operator>= (c4_Cursor, c4_Cursor);
00412 
00413 /* Comparing row contents */
00415   friend bool operator== (const c4_RowRef&, const c4_RowRef&);
00417   friend bool operator!= (const c4_RowRef&, const c4_RowRef&);
00419   friend bool operator< (const c4_RowRef&, const c4_RowRef&);
00421   friend bool operator> (const c4_RowRef&, const c4_RowRef&);
00423   friend bool operator<= (const c4_RowRef&, const c4_RowRef&);
00425   friend bool operator>= (const c4_RowRef&, const c4_RowRef&);
00426 };
00427 
00428 //---------------------------------------------------------------------------
00429 
00430 class c4_RowRef
00431 {
00433   c4_Cursor _cursor;
00434 
00435 public: 
00436 /* General operations */
00438   c4_RowRef operator= (const c4_RowRef&);
00440   c4_Cursor operator& () const;
00442   c4_View Container() const;
00443 
00444 protected:
00446   c4_RowRef (c4_Cursor);
00447 
00448   friend class c4_Cursor;
00449   friend class c4_Row;
00450 };
00451 
00452 //---------------------------------------------------------------------------
00454 //
00455 //  Rows can exist by themselves and as contents of views.  Row assignment
00456 //  implies that a copy of the contents of the originating row is made.
00457 //
00458 //  A row is implemented as an unattached view with exactly one element.
00459 
00460 class c4_Row : public c4_RowRef 
00461 {
00462 public:
00464   c4_Row ();
00466   c4_Row (const c4_Row&);
00468   c4_Row (const c4_RowRef&);
00470   ~c4_Row ();
00471   
00473   c4_Row& operator= (const c4_Row&);
00475   c4_Row& operator= (const c4_RowRef&);
00476   
00478   void ConcatRow(const c4_RowRef&);
00480   friend c4_Row operator+ (const c4_RowRef&, const c4_RowRef&);
00481   
00482 private:
00483   static c4_Cursor Allocate();
00484   static void Release(c4_Cursor);
00485 };
00486 
00487 //---------------------------------------------------------------------------
00488 
00489 class c4_Bytes
00490 {
00491   union {
00492     t4_byte _buffer [16];
00493     double _aligner; // on a Sparc, the int below wasn't enough...
00494   };
00495 
00496   t4_byte* _contents;
00497   int _size;
00498   bool _copy;
00499 
00500 public:
00501   c4_Bytes ();
00502   c4_Bytes (const void*, int);
00503   c4_Bytes (const void*, int, bool);
00504   c4_Bytes (const c4_Bytes&);
00505   ~c4_Bytes ();
00506   
00507   c4_Bytes& operator= (const c4_Bytes&);
00508   void Swap(c4_Bytes&);
00509   
00510   int Size() const;
00511   const t4_byte* Contents() const;
00512   
00513   t4_byte* SetBuffer(int);
00514   t4_byte* SetBufferClear(int);
00515 
00516   friend bool operator== (const c4_Bytes&, const c4_Bytes&);
00517   friend bool operator!= (const c4_Bytes&, const c4_Bytes&);
00518 
00519 private:
00520   void _MakeCopy();
00521   void _LoseCopy();
00522 };
00523 
00524 //---------------------------------------------------------------------------
00525 
00526 class c4_Storage : public c4_View
00527 {
00528 public:
00530   c4_Storage (); 
00532   c4_Storage (c4_Strategy&, bool =false, int =1); 
00534   c4_Storage (const char*, int);
00536   c4_Storage (const c4_View&);
00538   ~c4_Storage ();
00539   
00540   void SetStructure(const char*);
00541   bool AutoCommit(bool =true);
00542   c4_Strategy& Strategy() const;
00543   const char* Description(const char* =0);
00544   
00545   bool SetAside(c4_Storage&);
00546   c4_Storage* GetAside() const;
00547 
00548   bool Commit(bool =false);
00549   bool Rollback(bool =false);
00550   
00551   c4_ViewRef View(const char*);
00552   c4_View GetAs(const char*);
00553 
00554   bool LoadFrom(c4_Stream&);
00555   void SaveTo(c4_Stream&);
00556 
00557   t4_i32 FreeSpace(t4_i32* bytes_ =0);
00558 
00559   //DROPPED: c4_Storage (const char* filename_, const char* description_);
00560   //DROPPED: c4_View Store(const char* name_, const c4_View& view_);
00561   //DROPPED: c4_HandlerSeq& RootTable() const;
00562   //DROPPED: c4_RowRef xContents() const;
00563 
00564 private:
00565   void Initialize(c4_Strategy&, bool, int);
00566 };
00567 
00568 //---------------------------------------------------------------------------
00569 
00570 class c4_Property
00571 {
00572   short _id;
00573   char _type;
00574 
00575 public:
00577   c4_Property (char, int);
00579   c4_Property (char, const char*);
00580   ~c4_Property ();
00581   
00582   c4_Property (const c4_Property&);
00583   void operator= (const c4_Property&);
00584 
00585   const char* Name() const;
00586   char Type() const;
00587 
00588   int GetId() const;
00589 
00590   c4_Reference operator() (const c4_RowRef&) const;
00591 
00592   void Refs(int) const;
00593 
00594   c4_View operator, (const c4_Property&) const;
00595 
00596   static void CleanupInternalData();
00597 };
00598 
00600 class c4_IntProp : public c4_Property 
00601 {
00602 public:
00604   c4_IntProp (const char*);
00606   ~c4_IntProp ();
00607   
00609   c4_IntRef operator() (const c4_RowRef&) const;
00611   t4_i32 Get(const c4_RowRef&) const;
00613   void Set(const c4_RowRef&, t4_i32) const;
00614 
00616   c4_Row operator[] (t4_i32) const;
00618   c4_Row AsRow(t4_i32) const;
00619 };
00620 
00621 #if !q4_TINY
00622 
00624 class c4_LongProp : public c4_Property 
00625 {
00626 public:
00628   c4_LongProp (const char*);
00630   ~c4_LongProp ();
00631   
00633   c4_LongRef operator() (const c4_RowRef&) const;
00635   t4_i64 Get(const c4_RowRef&) const;
00637   void Set(const c4_RowRef&, t4_i64) const;
00638 
00640   c4_Row operator[] (t4_i64) const;
00642   c4_Row AsRow(t4_i64) const;
00643 };
00644 
00646 class c4_FloatProp : public c4_Property 
00647 {
00648 public:
00650   c4_FloatProp (const char*);
00652   ~c4_FloatProp ();
00653   
00655   c4_FloatRef operator() (const c4_RowRef&) const;
00657   double Get(const c4_RowRef&) const;
00659   void Set(const c4_RowRef&, double) const;
00660 
00662   c4_Row operator[] (double) const;
00664   c4_Row AsRow(double) const;
00665 };
00666 
00668 class c4_DoubleProp : public c4_Property 
00669 {
00670 public:
00672   c4_DoubleProp (const char*);
00674   ~c4_DoubleProp ();
00675   
00677   c4_DoubleRef operator() (const c4_RowRef&) const;
00679   double Get(const c4_RowRef&) const;
00681   void Set(const c4_RowRef&, double) const;
00682 
00684   c4_Row operator[] (double) const;
00686   c4_Row AsRow(double) const;
00687 };
00688 #endif // !q4_TINY
00689 
00691 class c4_StringProp : public c4_Property
00692 {
00693 public:
00695   c4_StringProp (const char*);
00697   ~c4_StringProp ();
00698   
00700   c4_StringRef operator() (const c4_RowRef&) const;
00702   const char* Get(const c4_RowRef&) const;
00704   void Set(const c4_RowRef&, const char*) const;
00705 
00707   c4_Row operator[] (const char*) const;
00709   c4_Row AsRow(const char*) const;
00710 };
00711 
00713 class c4_BytesProp : public c4_Property
00714 {
00715 public:
00717   c4_BytesProp (const char*);
00719   ~c4_BytesProp ();
00720   
00722   c4_BytesRef operator() (const c4_RowRef&) const;
00724   c4_Bytes Get(const c4_RowRef&) const;
00726   void Set(const c4_RowRef&, const c4_Bytes&) const;
00727 
00729   c4_Row operator[] (const c4_Bytes&) const;
00731   c4_Row AsRow(const c4_Bytes&) const;
00732 };
00733 
00735 class c4_ViewProp : public c4_Property
00736 {
00737 public:
00739   c4_ViewProp (const char*);
00741   ~c4_ViewProp ();
00742   
00744   c4_ViewRef operator() (const c4_RowRef&) const;
00746   c4_View Get(const c4_RowRef&) const;
00748   void Set(const c4_RowRef&, const c4_View&) const;
00749 
00751   c4_Row operator[] (const c4_View&) const;
00753   c4_Row AsRow(const c4_View&) const;
00754 };
00755 
00756 //---------------------------------------------------------------------------
00757 
00758 class c4_CustomViewer
00759 {
00760 protected: 
00762   c4_CustomViewer ();
00763 public: 
00765   virtual ~c4_CustomViewer ();
00766   
00768   virtual c4_View GetTemplate() = 0;
00770   virtual int GetSize() = 0;
00771   int Lookup(const c4_RowRef&, int&);
00772   virtual int Lookup(c4_Cursor, int&);
00774   virtual bool GetItem(int, int, c4_Bytes&) = 0;
00775   virtual bool SetItem(int, int, const c4_Bytes&);
00776   bool InsertRows(int, const c4_RowRef&, int =1);
00777   virtual bool InsertRows(int, c4_Cursor, int =1);
00778   virtual bool RemoveRows(int, int =1);
00779 };
00780 
00781 //---------------------------------------------------------------------------
00783 
00784 class c4_Stream
00785 {
00786 public:
00787   virtual ~c4_Stream ();
00788 
00790   virtual int Read(void*, int) = 0;
00792   virtual bool Write(const void*, int) = 0;
00793 };
00794 
00795 //---------------------------------------------------------------------------
00797 
00798 class c4_Strategy
00799 {
00800 public:
00801   c4_Strategy ();
00802   virtual ~c4_Strategy ();
00803 
00804   virtual bool IsValid() const;
00805   virtual int  DataRead(t4_i32, void*, int);
00806   virtual void DataWrite(t4_i32, const void*, int);
00807   virtual void DataCommit(t4_i32);
00808   virtual void ResetFileMapping();
00809   virtual t4_i32 FileSize();
00810   virtual t4_i32 FreshGeneration();
00811 
00812   void SetBase(t4_i32);
00813   t4_i32 EndOfData(t4_i32 =-1);
00814 
00816   bool _bytesFlipped;
00818   int _failure;
00820   const t4_byte* _mapStart;
00822   t4_i32 _dataSize;
00824   t4_i32 _baseOffset;
00826   t4_i32 _rootPos;
00828   t4_i32 _rootLen;
00829 };
00830 
00831 //---------------------------------------------------------------------------
00833 //
00834 //  Sequences represent arrays of rows (or indexed collections / tables).
00835 //  Insertion and removal of entries is allowed, but could take linear time.
00836 //  A reference count is maintained to decide when the object should go away.
00837 
00838 class c4_Sequence
00839 {
00841   int _refCount;
00843   c4_Dependencies* _dependencies;
00844 
00845 protected:
00847   int _propertyLimit;
00849   short* _propertyMap; // see c4_HandlerSeq::Reset()
00851   c4_Bytes* _tempBuf;
00852 
00853 public: 
00854 /* General */
00856   c4_Sequence ();
00857   
00858   virtual int Compare(int, c4_Cursor) const;
00859   virtual bool RestrictSearch(c4_Cursor, int&, int&);
00860   void SetAt(int, c4_Cursor);
00861   virtual int RemapIndex(int, const c4_Sequence*) const;
00862   
00863 /* Reference counting */
00864   void IncRef();
00865   void DecRef();
00866   int NumRefs() const;
00867 
00868 /* Adding / removing rows */
00870   virtual int NumRows() const = 0;
00871   void Resize(int, int =-1);
00872   
00873   virtual void InsertAt(int, c4_Cursor, int =1);
00874   virtual void RemoveAt(int, int =1);
00875   virtual void Move(int, int);
00876 
00877 /* Properties */
00878   int NthPropId(int) const;
00879   int PropIndex(int);
00880   int PropIndex(const c4_Property&);
00881   
00883   virtual int NumHandlers() const = 0;
00885   virtual c4_Handler& NthHandler(int) const = 0;
00887   virtual const c4_Sequence* HandlerContext(int) const = 0;
00889   virtual int AddHandler(c4_Handler*) = 0;
00891   virtual c4_Handler* CreateHandler(const c4_Property&) = 0;
00892 
00893   virtual const char* Description();
00894 
00895 /* Element access */
00897   virtual int ItemSize(int, int);
00899   virtual bool Get(int, int, c4_Bytes&);
00901   virtual void Set(int, const c4_Property&, const c4_Bytes&);
00902   
00903 /* Dependency notification */
00904   void Attach(c4_Sequence*);
00905   void Detach(c4_Sequence*);
00907   c4_Dependencies* GetDependencies() const;
00908 
00909   virtual c4_Notifier* PreChange(c4_Notifier&);
00910   virtual void PostChange(c4_Notifier&);
00911   
00912   const char* UseTempBuffer(const char*);
00913 
00914 protected:
00915   virtual ~c4_Sequence ();
00916 
00917   void ClearCache();
00918 
00919 public: 
00920   virtual void SetNumRows(int) = 0;
00921   virtual c4_Persist* Persist() const;
00922 
00923   c4_Bytes& Buffer();
00924 
00925 private:
00926   c4_Sequence (const c4_Sequence&);   // not implemented
00927   void operator= (const c4_Sequence&); // not implemented
00928 };
00929 
00930 //---------------------------------------------------------------------------
00932 //
00933 //  Objects of this class are only intended to be used as a temporary handle
00934 //  while getting and setting properties in a row.  They are normally only
00935 //  constructed as result of function overload operators: "property (row)".
00936 
00937 class c4_Reference
00938 {
00939 protected:
00941   c4_Cursor _cursor;
00943   const c4_Property& _property;
00944 
00945 public:
00947   c4_Reference (const c4_RowRef&, const c4_Property&);
00948 
00950   c4_Reference& operator= (const c4_Reference&);
00951 
00953   int GetSize() const;
00955   bool GetData(c4_Bytes&) const;
00957   void SetData(const c4_Bytes&) const;
00958   
00960   friend bool operator== (const c4_Reference&, const c4_Reference&);
00962   friend bool operator!= (const c4_Reference&, const c4_Reference&);
00963 
00964 private:
00965   void operator& () const;            // not implemented
00966 };
00967 
00968 //---------------------------------------------------------------------------
00969 
00971 class c4_IntRef : public c4_Reference
00972 {
00973 public:
00975   c4_IntRef (const c4_Reference&);
00977   operator t4_i32 () const;
00979   c4_IntRef& operator= (t4_i32);
00980 };
00981 
00982 #if !q4_TINY
00983 
00985 class c4_LongRef : public c4_Reference
00986 {
00987 public:
00989   c4_LongRef (const c4_Reference&);
00991   operator t4_i64 () const;
00993   c4_LongRef& operator= (t4_i64);
00994 };
00995 
00997 class c4_FloatRef : public c4_Reference
00998 {
00999 public:
01001   c4_FloatRef (const c4_Reference&);
01003   operator double () const;
01005   c4_FloatRef& operator= (double);
01006 };
01007 
01009 class c4_DoubleRef : public c4_Reference
01010 {
01011 public:
01013   c4_DoubleRef (const c4_Reference&);
01015   operator double () const;
01017   c4_DoubleRef& operator= (double);
01018 };
01019 
01020 #endif // !q4_TINY
01021 
01023 class c4_BytesRef : public c4_Reference
01024 {
01025 public:
01027   c4_BytesRef (const c4_Reference&);
01029   operator c4_Bytes () const;
01031   c4_BytesRef& operator= (const c4_Bytes&);
01032 
01034   c4_Bytes Access(t4_i32, int =0) const;
01036   bool Modify(const c4_Bytes&, t4_i32, int =0) const;
01037 };
01038 
01040 class c4_StringRef : public c4_Reference
01041 {
01042 public:
01044   c4_StringRef (const c4_Reference&);
01046   operator const char* () const;
01048   c4_StringRef& operator= (const char*);
01049 };
01050 
01052 class c4_ViewRef : public c4_Reference
01053 {
01054 public:
01056   c4_ViewRef (const c4_Reference&);
01058   operator c4_View () const;
01060   c4_ViewRef& operator= (const c4_View&);
01061 };
01062 
01063 //---------------------------------------------------------------------------
01064 // Debug logging option, can generate log of changes for one/all properties
01065 
01066 #if q4_LOGPROPMODS
01067 FILE* f4_LogPropMods(FILE* fp_, int propId_);
01068 #else
01069 #define f4_LogPropMods(a,b) 0
01070 #endif
01071 
01072 //---------------------------------------------------------------------------
01073 
01074 #if q4_INLINE
01075 #include "mk4.inl"
01076 #endif
01077 
01078 //---------------------------------------------------------------------------
01079 
01080 #endif // __MK4_H__


Metakit C++ API Reference - http://www.equi4.com/metakit.html - extracted with Doxygen