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         
00100         /* Constants */
00101         
00103 
00110         static Record::identifier_type const external_id = 0x80000000; 
00111         
00113         enum
00114         {
00116             ceUnknown = 1,
00118             ceID = 2,
00120             ceList = 3,
00122             ceDatabase = 4,
00124             ceCustom = 128
00125         };
00126         
00127         /* Functions */
00128     
00130         Collection();
00132 
00134         virtual ~Collection();
00135         
00137 
00150         virtual bool begin_initial() { status_add(csInitial); return true; }
00151         
00153 
00166         virtual bool end_initial() { status_remove(csInitial); return true; }
00167         
00169 
00183         virtual bool begin_import(uint16_t type) 
00184                 { status_add(csImport | type); return true; }
00185         
00187 
00201         virtual bool end_import(uint16_t type)
00202                 { status_remove(csImport | type); return true; }
00203         
00205 
00214         virtual bool update_new() { return true; }
00215         
00217 
00225         virtual Record *create(Record::identifier_type id = 0) const = 0;
00226         
00228 
00244         virtual bool add(Record *record, int *err = 0);
00245         
00247 
00260         bool change_s(Record *record, Field const *field, char const *value);
00261         
00263 
00276         bool change_i(Record *record, Field const *field, int32_t value);
00277         
00279 
00292         bool change_ui(Record *record, Field const *field, uint32_t value);
00293         
00295 
00308         bool change_ut(Record *record, Field const *field, time_t value);
00309         
00311 
00324         bool change_ri(Record *record, Field const *field,
00325                 Record::identifier_type value);
00326         
00328 
00345         bool change_multi(Record *record, unsigned int valcount, ...);
00346         
00348 
00360         bool change_owned(Field const *field, Record::identifier_type id,
00361                 Record::identifier_type new_id);
00362         
00364 
00377         virtual bool remove(Record *record, int *err = 0);
00378         
00380 
00396         virtual bool remove_owned(Collection const *coll,
00397                 Record const *record, int *err = 0);
00398         
00400 
00408         virtual bool clear(int *err = 0);
00409         
00411 
00416         Record *find(Record::identifier_type id);
00418         Record const *find(Record::identifier_type id) const;
00419         
00421 
00429         void set_owned(Collection &coll) { m_owned.push_front(&coll); }
00430         
00432 
00436         void unset_owned(Collection &coll) { m_owned.remove(&coll); }
00437         
00439         iterator begin() { return m_members.begin(); }
00441         iterator end() { return m_members.end(); }
00443         const_iterator begin() const { return m_members.begin(); }
00445         const_iterator end() const { return m_members.end(); }
00446         
00448         reverse_iterator rbegin() { return m_members.rbegin(); }
00450         reverse_iterator rend() { return m_members.rend(); }
00452         const_reverse_iterator rbegin() const { return m_members.rbegin(); }
00454         const_reverse_iterator rend() const { return m_members.rend(); }
00455         
00457         bool empty() const { return m_members.empty(); }
00459         id_hashlist::size_type size() const { return m_members.size(); }
00460         
00462 
00467         virtual size_t memory_usage() const;
00468         
00470         enum
00471         {
00473             csChanges  = 0x0001,
00475             csInitial  = 0x0002,
00477             csImport   = 0x0004,
00479             csCreation = 0x0008,
00481             csUpdate   = 0x0010,
00483             csDeletion = 0x0020
00484         };
00485         
00487         Database *db() { return m_db; }
00489 
00496         Database *db(Database *db) { return (m_db = db); }
00497         
00499 
00504         bool readable() const;
00505         
00507 
00512         bool writable() const;
00513                 
00515 
00527         virtual Field const *owner_field(Collection const *coll) const
00528                 { return 0; }
00529         
00531         uint16_t status() const { return m_status; }
00533 
00537         uint16_t status_add(uint16_t flags) { return (m_status |= flags); }
00539 
00543         uint16_t status_remove(uint16_t flags) { return (m_status &= ~flags); }
00544         
00546         Record::identifier_type last_id() const { return m_last_id; }
00547     protected:
00549         id_hashlist m_members;
00551         Database *m_db;
00553         uint16_t m_status;
00555         Record::identifier_type m_last_id;
00556         
00558         owned_list m_owned;
00559         
00561 
00567         virtual bool add_list(Record *record);
00568         
00570 
00576         virtual bool add_db(Record *record);
00577         
00579 
00595         virtual bool remove_owned_db(Collection const *coll,
00596                 Record const *record, int *err = 0);
00597         
00599 
00616         virtual bool remove_owned_list(Collection const *coll,
00617                 Record const *record, int *err = 0);
00618         
00620 
00627         virtual void remove_list(Record *record);
00628         
00630 
00636         virtual bool remove_db(Record *record);
00637         
00639 
00651         virtual bool clear_owned(Collection const *coll, int *err = 0);
00652         
00654 
00658         virtual void clear_list();
00659         
00661 
00665         virtual bool clear_db();
00666     private:
00667         Collection(Collection const &);
00668         Collection &operator=(Collection const &);
00669 };
00670 
00671 /*************************************************************************/
00672 
00674 
00678 class NameCollection : public Collection
00679 {
00680     protected:
00682         static uint32_t const name_hashsize = NAME_HASH_SIZE;
00684         typedef containers::HashList<NameRecord *, char const *,
00685                 name_hashsize> name_hashlist;
00686     public:
00687         /* Typedefs */
00688         
00690         typedef containers::Hash<NameRecord *, char const *> name_hash;
00691         
00693         typedef name_hashlist::iterator niterator;
00695         typedef name_hashlist::const_iterator const_niterator;
00697         typedef name_hashlist::reverse_iterator reverse_niterator;
00699         typedef name_hashlist::const_reverse_iterator const_reverse_niterator;
00700         
00701         /* Functions */
00702         
00704 
00707         explicit NameCollection(name_hash &hash) : Collection(),
00708                 m_nmembers(hash) { }
00709         
00711 
00722         virtual bool change_name(NameRecord *record, char const *newname);
00723         
00725         niterator nbegin() { return m_nmembers.begin(); }
00727         niterator nend() { return m_nmembers.end(); }
00729         const_niterator nbegin() const { return m_nmembers.begin(); }
00731         const_niterator nend() const { return m_nmembers.end(); }
00732         
00734         reverse_niterator nrbegin() { return m_nmembers.rbegin(); }
00736         reverse_niterator nrend() { return m_nmembers.rend(); }
00738         const_reverse_niterator nrbegin() const { return m_nmembers.rbegin(); }
00740         const_reverse_niterator nrend() const { return m_nmembers.rend(); }
00741         
00743         virtual size_t memory_usage() const;
00744         
00746 
00751         NameRecord *nfind(char const *name);
00753         NameRecord const *nfind(char const *name) const;
00754     protected:
00756         name_hashlist m_nmembers;
00757         
00759         virtual bool add_list(Record *record);
00760         
00762         virtual void remove_list(Record *record);
00763         
00765         virtual void clear_list();
00766 };
00767 
00768 /*************************************************************************/
00769 
00771 
00775 class ArrayCollection : public Collection
00776 {
00777     public:
00778         /* Typedefs */
00779         
00781         typedef containers::VectorSP<Record *, uint16_t> array_type;
00782         
00783         /* Constants */
00784         
00786         enum
00787         {
00789             ceName = 64
00790         };
00791         
00792         /* Functions */
00793         
00795 
00799         explicit ArrayCollection(bool named) : Collection(), m_array(),
00800                 m_named(named) { }
00801         
00803 
00815         virtual bool add(Record *record, int *err = 0);
00816         
00818         virtual size_t memory_usage() const;
00819         
00821 
00829         array_type &array() { return m_array; }
00830         
00832     protected:
00834         array_type m_array;
00836         bool m_named;
00837         
00839         virtual bool add_list(Record *record);
00840         
00842         virtual void remove_list(Record *record);
00843         
00845         virtual void clear_list();
00846 };
00847 
00848 /*************************************************************************/
00849 
00850 } /* namespace databases */
00851 
00852 /*************************************************************************/
00853 
00854 #endif /* DATABASES_COLLECTION_H */

Generated on Sun May 20 21:32:13 2007 for Epona API by  doxygen 1.4.6