collection.h

Go to the documentation of this file.
00001 /* Data collection.
00002  *
00003  * PegSoft databases library (c) 2005 PegSoft
00004  * Contact us at pegsoft@pegsoft.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  * (at your option) 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 (see the file COPYING); if not, write to the
00018  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00024 #ifndef DATABASES_COLLECTION_H
00025 #define DATABASES_COLLECTION_H
00026 
00027 /*************************************************************************/
00028 
00029 #include <list>
00030 
00031 #include <containers/hashlist.h>
00032 #include <containers/vectorsp.h>
00033 #include <databases/record.h>
00034 
00035 /*************************************************************************/
00036 
00037 #ifndef ID_HASH_SIZE
00038 # define ID_HASH_SIZE       4096
00039 #endif
00040 
00041 #ifndef NAME_HASH_SIZE
00042 # define NAME_HASH_SIZE     1024
00043 #endif
00044 
00045 /*************************************************************************/
00046 
00048 
00049 namespace databases
00050 {
00051 
00052 /*************************************************************************/
00053 
00054 class Database;
00055 class Field;
00056 
00057 /*************************************************************************/
00058 
00060 
00077 class Collection
00078 {
00079     protected:
00081         static uint32_t const id_hashsize = ID_HASH_SIZE;
00083         typedef containers::HashList<Record *, Record::identifier_type,
00084                 id_hashsize> id_hashlist;
00085         
00087         typedef std::list<Collection *> owned_list;
00088     public:
00089         /* Typedefs */
00090         
00092         typedef id_hashlist::iterator iterator;
00094         typedef id_hashlist::const_iterator const_iterator;
00096         typedef id_hashlist::reverse_iterator reverse_iterator;
00098         typedef id_hashlist::const_reverse_iterator const_reverse_iterator;
00099         
00101         enum
00102         {
00104             ceUnknown = 1,
00106             ceID = 2,
00108             ceList = 3,
00110             ceDatabase = 4,
00112             ceCustom = 128
00113         };
00114         
00115         /* Functions */
00116     
00118         Collection();
00120 
00122         virtual ~Collection();
00123         
00125 
00138         virtual bool begin_initial() { status_add(csInitial); return true; }
00139         
00141 
00154         virtual bool end_initial() { status_remove(csInitial); return true; }
00155         
00157 
00171         virtual bool begin_import(uint16_t type) 
00172                 { status_add(csImport | type); return true; }
00173         
00175 
00189         virtual bool end_import(uint16_t type)
00190                 { status_remove(csImport | type); return true; }
00191         
00193 
00202         virtual bool update_new() { return true; }
00203         
00205 
00213         virtual Record *create(Record::identifier_type id = 0) const = 0;
00214         
00216 
00232         virtual bool add(Record *record, int *err = 0);
00233         
00235 
00248         bool change_s(Record *record, Field const *field, char const *value);
00249         
00251 
00264         bool change_i(Record *record, Field const *field, int32_t value);
00265         
00267 
00280         bool change_ui(Record *record, Field const *field, uint32_t value);
00281         
00283 
00296         bool change_ut(Record *record, Field const *field, time_t value);
00297         
00299 
00312         bool change_ri(Record *record, Field const *field,
00313                 Record::identifier_type value);
00314         
00316 
00333         bool change_multi(Record *record, unsigned int valcount, ...);
00334         
00336 
00348         bool change_owned(Field const *field, Record::identifier_type id,
00349                 Record::identifier_type new_id);
00350         
00352 
00365         virtual bool remove(Record *record, int *err = 0);
00366         
00368 
00384         virtual bool remove_owned(Collection const *coll,
00385                 Record const *record, int *err = 0);
00386         
00388 
00396         virtual bool clear(int *err = 0);
00397         
00399 
00404         Record *find(Record::identifier_type id);
00406         Record const *find(Record::identifier_type id) const;
00407         
00409 
00417         void set_owned(Collection &coll) { m_owned.push_front(&coll); }
00418         
00420 
00424         void unset_owned(Collection &coll) { m_owned.remove(&coll); }
00425         
00427         iterator begin() { return m_members.begin(); }
00429         iterator end() { return m_members.end(); }
00431         const_iterator begin() const { return m_members.begin(); }
00433         const_iterator end() const { return m_members.end(); }
00434         
00436         reverse_iterator rbegin() { return m_members.rbegin(); }
00438         reverse_iterator rend() { return m_members.rend(); }
00440         const_reverse_iterator rbegin() const { return m_members.rbegin(); }
00442         const_reverse_iterator rend() const { return m_members.rend(); }
00443         
00445         bool empty() const { return m_members.empty(); }
00447         id_hashlist::size_type size() const { return m_members.size(); }
00448         
00450 
00455         virtual size_t memory_usage() const;
00456         
00458         enum
00459         {
00461             csChanges  = 0x0001,
00463             csInitial  = 0x0002,
00465             csImport   = 0x0004,
00467             csCreation = 0x0008,
00469             csUpdate   = 0x0010,
00471             csDeletion = 0x0020
00472         };
00473         
00475         Database *db() { return m_db; }
00477 
00484         Database *db(Database *db) { return (m_db = db); }
00485         
00487 
00492         bool readable() const;
00493         
00495 
00500         bool writable() const;
00501                 
00503 
00515         virtual Field const *owner_field(Collection const *coll) const
00516                 { return 0; }
00517         
00519         uint16_t status() const { return m_status; }
00521 
00525         uint16_t status_add(uint16_t flags) { return (m_status |= flags); }
00527 
00531         uint16_t status_remove(uint16_t flags) { return (m_status &= ~flags); }
00532         
00534         Record::identifier_type last_id() const { return m_last_id; }
00535     protected:
00537         id_hashlist m_members;
00539         Database *m_db;
00541         uint16_t m_status;
00543         Record::identifier_type m_last_id;
00544         
00546         owned_list m_owned;
00547         
00549 
00555         virtual bool add_list(Record *record);
00556         
00558 
00564         virtual bool add_db(Record *record);
00565         
00567 
00583         virtual bool remove_owned_db(Collection const *coll,
00584                 Record const *record, int *err = 0);
00585         
00587 
00604         virtual bool remove_owned_list(Collection const *coll,
00605                 Record const *record, int *err = 0);
00606         
00608 
00615         virtual void remove_list(Record *record);
00616         
00618 
00624         virtual bool remove_db(Record *record);
00625         
00627 
00639         virtual bool clear_owned(Collection const *coll, int *err = 0);
00640         
00642 
00646         virtual void clear_list();
00647         
00649 
00653         virtual bool clear_db();
00654     private:
00655         Collection(Collection const &);
00656         Collection &operator=(Collection const &);
00657 };
00658 
00659 /*************************************************************************/
00660 
00662 
00666 class NameCollection : public Collection
00667 {
00668     protected:
00670         static uint32_t const name_hashsize = NAME_HASH_SIZE;
00672         typedef containers::HashList<NameRecord *, char const *,
00673                 name_hashsize> name_hashlist;
00674     public:
00675         /* Typedefs */
00676         
00678         typedef containers::Hash<NameRecord *, char const *> name_hash;
00679         
00681         typedef name_hashlist::iterator niterator;
00683         typedef name_hashlist::const_iterator const_niterator;
00685         typedef name_hashlist::reverse_iterator reverse_niterator;
00687         typedef name_hashlist::const_reverse_iterator const_reverse_niterator;
00688         
00689         /* Functions */
00690         
00692 
00695         explicit NameCollection(name_hash &hash) : Collection(),
00696                 m_nmembers(hash) { }
00697         
00699 
00710         virtual bool change_name(NameRecord *record, char const *newname);
00711         
00713         niterator nbegin() { return m_nmembers.begin(); }
00715         niterator nend() { return m_nmembers.end(); }
00717         const_niterator nbegin() const { return m_nmembers.begin(); }
00719         const_niterator nend() const { return m_nmembers.end(); }
00720         
00722         reverse_niterator nrbegin() { return m_nmembers.rbegin(); }
00724         reverse_niterator nrend() { return m_nmembers.rend(); }
00726         const_reverse_niterator nrbegin() const { return m_nmembers.rbegin(); }
00728         const_reverse_niterator nrend() const { return m_nmembers.rend(); }
00729         
00731         virtual size_t memory_usage() const;
00732         
00734 
00739         NameRecord *nfind(char const *name);
00741         NameRecord const *nfind(char const *name) const;
00742     protected:
00744         name_hashlist m_nmembers;
00745         
00747         virtual bool add_list(Record *record);
00748         
00750         virtual void remove_list(Record *record);
00751         
00753         virtual void clear_list();
00754 };
00755 
00756 /*************************************************************************/
00757 
00759 
00763 class ArrayCollection : public Collection
00764 {
00765     public:
00766         /* Typedefs */
00767         
00769         typedef containers::VectorSP<Record *, uint16_t> array_type;
00770         
00771         /* Constants */
00772         
00774         enum
00775         {
00777             ceName = 64
00778         };
00779         
00780         /* Functions */
00781         
00783 
00787         explicit ArrayCollection(bool named) : Collection(), m_array(),
00788                 m_named(named) { }
00789         
00791 
00803         virtual bool add(Record *record, int *err = 0);
00804         
00806         virtual size_t memory_usage() const;
00807         
00809 
00817         array_type &array() { return m_array; }
00818         
00820     protected:
00822         array_type m_array;
00824         bool m_named;
00825         
00827         virtual bool add_list(Record *record);
00828         
00830         virtual void remove_list(Record *record);
00831         
00833         virtual void clear_list();
00834 };
00835 
00836 /*************************************************************************/
00837 
00838 } /* namespace databases */
00839 
00840 /*************************************************************************/
00841 
00842 #endif /* DATABASES_COLLECTION_H */

Generated on Wed Aug 15 00:37:21 2007 for Epona API by  doxygen 1.5.2