Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Database.h

Go to the documentation of this file.
00001 /** $Revision: 1.15 $
00002     Last updated: $Date: 2002/09/20 12:21:30 $
00003 
00004     Copyright (C) 2000-2002 Vlad Mereuta <dizzy@users.sourceforge.net>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
00019 
00020 #ifndef __DATABASE_H__
00021 #define __DATABASE_H__
00022 
00023 #include "GenericDB.h"
00024 #include "FreeCoinsRsc.h"
00025 
00026 /** interface to some of the PalmOS database functions
00027     Using this class:
00028     * derive a new database class as public from this one passing your packed/unpacked structures as parameters.
00029     * in your new class override the pure virtual function from this class and add anything you might thing useful  
00030 */
00031 template <class recordStructure, class packedRecordStructure> class Database
00032 : public GenericDB
00033 {
00034  public:
00035     /** get the unpacked strucure of a given record
00036         @param id   id of the account record
00037         @param rec  pointer in which the unpacked data will be saved
00038         @return pointer to the record handler (of the packed record) or null if error */
00039     MemHandle getUnpackedRecord(UInt16 id,recordStructure* rec) DB_SECTION1;
00040 
00041     /** get the unpacked strucure of the current record
00042         @param rec  pointer in which the unpacked data will be saved
00043         @return pointer to the record handler (of the packed record) or null if error */
00044     MemHandle getUnpackedRecord(recordStructure* rec) DB_SECTION1;
00045     
00046     /** get the unpacked strucure of a given record
00047         @param uid  unique id of the account record
00048         @param rec  pointer in which the unpacked data will be saved
00049         @return pointer to the record handler (of the packed record) or NULL if error */
00050     MemHandle getUnpackedRecord(UInt32 uid,recordStructure* rec) DB_SECTION1;
00051 
00052     /** constructor
00053         use when in need to access functions like pack/unpack from a static function.
00054         @param db_open_ref reference to an *already* opened database */
00055     Database(DmOpenRef db_open_ref) : GenericDB(db_open_ref) {};
00056 
00057     /** initialize the databases
00058         @param creator database creator
00059         @param db_name name of the database 
00060         @param type type of the database (ex. 'DATA') */
00061     Database(UInt32 type, UInt32 creator, Char* db_name): GenericDB(type, creator, db_name) {};
00062 
00063     /** initialize the databases
00064         @param type type of the database (ex. 'DATA')
00065         @param creator database creator
00066         @param db_name - name of the database
00067         @param categ_string - id of the category resource to be used to initialise the categories for this database     */
00068     Database(UInt32 type, UInt32 creator, Char* db_name, UInt16 categ_string) : GenericDB(type, creator, db_name, categ_string) {};
00069 
00070     /// closes the database
00071     virtual ~Database() DB_SECTION1;
00072 
00073     /** unpacks a record
00074         @param rec the record pacxked_rec will be unpacked into this location
00075         @param packed_rec pointer to the record to be unpacked  */
00076     virtual void    unpackRecord (recordStructure* rec, packedRecordStructure* packed_rec)=0;
00077 
00078     /** packs an account record. This function should be overloaded by all database classes
00079         @param rec pointer to the record to be packed
00080         @param db_entry pointer to the newly allocated packed record */
00081     virtual void    packRecord (recordStructure* rec, MemHandle db_entry)=0;
00082     
00083         
00084     /** packs and writes the data from rec at the end of the database.
00085         @return the index to the record     */
00086     virtual UInt16  newRecord(recordStructure* rec) DB_SECTION1;
00087 };
00088 
00089 template <class recordStructure, class packedRecordStructure> MemHandle 
00090 Database<recordStructure, packedRecordStructure>::		getUnpackedRecord(UInt32 uid,recordStructure* rec)
00091 {
00092     return getUnpackedRecord(getRecordId(uid),rec);
00093 }
00094 
00095 template <class recordStructure, class packedRecordStructure> MemHandle 
00096 Database<recordStructure, packedRecordStructure>::		getUnpackedRecord(UInt16 id,recordStructure* rec)
00097 {
00098     MemHandle               rec_h;
00099     packedRecordStructure*  pac_rec;
00100 
00101     
00102     rec_h=DmQueryRecord(ref,id);
00103     if(!rec_h) {
00104         ErrFatalDisplay("Could not find record with given ID");
00105         return NULL;
00106     }
00107 
00108     pac_rec = (packedRecordStructure*) MemHandleLock(rec_h);
00109     if(!pac_rec){
00110         ErrFatalDisplay("Could not get lock on unpacked record");
00111         return NULL;
00112     }
00113 
00114     unpackRecord(rec,pac_rec);
00115     MemHandleUnlock(rec_h);
00116     return rec_h;
00117 }
00118 
00119 template <class recordStructure, class packedRecordStructure> MemHandle 
00120 Database<recordStructure, packedRecordStructure>::		getUnpackedRecord(recordStructure* rec)
00121 {
00122     if(!isRecordSelected()) ErrFatalDisplay("There is no record currently selected");
00123     return getUnpackedRecord(selected_record, rec); 
00124 }
00125 
00126 template <class recordStructure, class packedRecordStructure> UInt16    
00127 Database<recordStructure, packedRecordStructure>::		newRecord(recordStructure* rec)
00128 {
00129     MemHandle   h,p;
00130     UInt16              index=dmMaxRecordIndex;
00131 
00132     //set size to 1 - will be resized by overloading functions
00133     h=DmNewHandle(ref,1);
00134     if(!h) {
00135         MemHandle str_handle = DmGetResource (strRsc,NEW_REC_FAILED_STR);   
00136         ErrFatalDisplay(reinterpret_cast<char*> (MemHandleLock (str_handle)));
00137         MemHandleUnlock (str_handle);
00138         DmReleaseResource (str_handle);
00139     }
00140 
00141     packRecord(rec,h);
00142 
00143     p = (MemHandle) MemHandleLock(h);
00144     MemPtrUnlock(p);
00145     
00146     DmAttachRecord(ref,&index,h,0); //automatically sets the dirty bit  
00147 
00148     if(current_cat!=dmAllCategories) setRecordCategory(index, current_cat);
00149     return index;   
00150 }
00151 
00152 template <class recordStructure, class packedRecordStructure> Database<recordStructure, packedRecordStructure>::~Database()
00153 {}
00154 
00155 template <class Field> void GenericDB::writeAndAdvance(MemPtr s, UInt32& offset, Field* f)
00156 {
00157     DmWrite(s, offset, (MemPtr) f, sizeof(Field));
00158     offset += sizeof(Field);
00159 }
00160 
00161 #endif

Generated on Thu Jan 16 23:11:12 2003 for FreeCoins by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002