it_bus_pdk/persistent_map_base.h

00001 #ifndef _IT_BUS_PERSISTENT_MAP_BASE_H_
00002 #define _IT_BUS_PERSISTENT_MAP_BASE_H_
00003 
00004 // @Copyright 2004 IONA Technologies, Plc. All Rights Reserved.
00005 //
00006 
00007 #include <it_dsa/types.h>
00008 #include <it_bus_pdk/dbm.h>
00009 #include <it_bus_pdk/db_config.h>
00010 
00012 //
00013 // When implementing a PersistenceHandler, you must ensure that it contains
00014 // implementations of the following functions, with these exact signatures:
00015 //
00016 //    void*&
00017 //    writebuf();
00018 //
00019 //    void*
00020 //    readbuf();
00021 //
00022 //    IT_ULong&
00023 //    writebuflen();
00024 //
00025 //    IT_ULong
00026 //    readbuflen() const;
00027 //
00028 //    AllocFunc
00029 //    allocator();
00030 //    
00031 //    void
00032 //    update_read_buffer_after_write();
00033 //
00034 // See the documentation for more details on the purposes of these methods
00035 // and how to properly implement them.
00036 // 
00038 
00039 namespace IT_Bus
00040 {
00041     typedef void* (*AllocFunc)(size_t);
00042     
00043     template<class T>
00044     class PODPersistenceHandler
00045     {
00046       public:
00047         IT_EXPLICIT PODPersistenceHandler(
00048             const T& t
00049         ) :
00050             m_t(IT_CONST_CAST(T&, t)),
00051             m_p(&m_t),
00052             m_sz(sizeof(T))
00053         {}
00054         IT_EXPLICIT PODPersistenceHandler(
00055             T& t
00056         ) :
00057             m_t(t),
00058             m_p(&m_t),
00059             m_sz(sizeof(T))
00060         {}
00061 
00062         void*&
00063         writebuf()
00064         {
00065             return m_p;
00066         }    
00067 
00068         void*
00069         readbuf()
00070         {
00071             return &m_t;
00072         }    
00073 
00074         IT_ULong&
00075         writebuflen()
00076         {
00077             return m_sz;
00078         }
00079 
00080         IT_ULong
00081         readbuflen() const
00082         {
00083             return sizeof(T);
00084         }
00085 
00086         AllocFunc
00087         allocator()
00088         {
00089             return 0;
00090         }
00091 
00092         void
00093         update_read_buffer_after_write() {}
00094 
00095       private:
00096         T&       m_t;
00097         void*    m_p;
00098         IT_ULong m_sz;
00099 
00100         // private and unimplemented to prevent copying
00101         PODPersistenceHandler(const PODPersistenceHandler<T>&);
00102         void operator=(const PODPersistenceHandler<T>&);
00103     };
00104 
00105     template<class Key, class Data, class KeyHandler, class DataHandler>
00106     class PersistentMapBase;
00107 
00108     //
00109     // The following class should be nested inside PersistentMapBase
00110     // as class iterator, but unfortunately MSVC6 can't handle
00111     // that. We instead put it here and then typedef its name into the
00112     // PersistentMapBase class.
00113     //
00114     template<class Key, class Data, class KeyHandler, class DataHandler>
00115     class PersistentMapBaseIterator
00116     {
00117         typedef PersistentMapBase<Key, Data, KeyHandler, DataHandler> MapType;
00118 
00119       public:
00120         PersistentMapBaseIterator(
00121         ) :
00122             m_map(0),
00123             m_it(0, 0, 0, 0)
00124         {
00125             // complete
00126         }
00127 
00128         ~PersistentMapBaseIterator() {}
00129 
00130         PersistentMapBaseIterator&
00131         operator=(
00132             const PersistentMapBaseIterator& it
00133         );
00134 
00135         IT_Bool
00136         operator==(
00137             const PersistentMapBaseIterator& it
00138         ) const
00139         {
00140             return m_map == it.m_map && m_it == it.m_it;
00141         }
00142 
00143         IT_Bool
00144         operator!=(
00145             const PersistentMapBaseIterator& it
00146         ) const
00147         {
00148             return !operator==(it);
00149         }
00150 
00151         const IT_Pair<const Key, Data>&
00152         operator*() const;
00153 
00154         PersistentMapBaseIterator&
00155         operator++()
00156         {
00157             ++m_it;
00158             return *this;
00159         }
00160 
00161         PersistentMapBaseIterator
00162         operator++(int)
00163         {
00164             PersistentMapBaseIterator save = *this;
00165             ++*this;
00166             return save;
00167         }
00168 
00169         PersistentMapBaseIterator(
00170             MapType*   mp,
00171             DBIterator it
00172         ) :
00173             m_map(mp),
00174             m_it(it)
00175         {
00176             // complete
00177         }
00178 
00179       private:
00180         MapType*                 m_map;
00181         DBIterator               m_it;
00182         IT_Pair<const Key, Data> m_value;
00183 
00184       friend class PersistentMapBase<Key, Data, KeyHandler, DataHandler>;
00185     };
00186 
00187     template<class Key, class Data, class KeyHandler, class DataHandler>
00188     class PersistentMapBase
00189     {
00190       public:
00191         typedef Key                      key_type;
00192         typedef Data                     data_type;
00193         typedef Data&                    reference_type;
00194         typedef const Data&              const_reference_type;
00195         typedef IT_Pair<const Key, Data> value_type;
00196         typedef size_t                   size_type;
00197         typedef ptrdiff_t                difference_type;
00198 
00199         typedef PersistentMapBase<Key, Data, KeyHandler, DataHandler> MapType;
00200 
00201         typedef PersistentMapBaseIterator <Key, Data,
00202                                            KeyHandler, DataHandler> iterator;
00203 
00204         PersistentMapBase(      
00205             const char* id,
00206             DBConfig*   cfg
00207         );
00208         PersistentMapBase(
00209             const char*       id,
00210             DBConfig*         cfg,
00211             const value_type* first,
00212             const value_type* last
00213         );
00214         PersistentMapBase(
00215             const PersistentMapBase<Key, Data, KeyHandler, DataHandler>& mp
00216         );
00217         ~PersistentMapBase();
00218 
00219         PersistentMapBase<Key, Data, KeyHandler, DataHandler>&
00220         operator=(
00221             const PersistentMapBase<Key, Data, KeyHandler, DataHandler>& mp
00222         );
00223 
00224         iterator
00225         begin() const;
00226 
00227         iterator
00228         end() const;
00229 
00230         IT_Pair<iterator, IT_Bool>
00231         insert(
00232             const value_type& val,
00233             IT_Bool           overwrite = IT_FALSE
00234         );
00235 
00236         void
00237         insert(
00238             const value_type* first,
00239             const value_type* last,
00240             IT_Bool           overwrite = IT_FALSE
00241         );
00242 
00243         size_type
00244         erase(
00245             const key_type& key
00246         );
00247 
00248         void
00249         erase(
00250             const iterator& pos
00251         );
00252 
00253         void
00254         erase(
00255             const iterator& first,
00256             const iterator& last
00257         );
00258 
00259         iterator
00260         find(
00261             const key_type& key
00262         ) const;
00263 
00264         IT_Bool
00265         empty() const;
00266 
00267         size_type
00268         size() const;
00269 
00270       private:
00271         DBM       m_dbm;
00272         String    m_id;
00273         DBConfig* m_cfg;
00274     };
00275 
00276     //
00277     // Inlined functions
00278     //
00279     template<class Key, class Data, class KeyHandler, class DataHandler>
00280     inline IT_TYPENAME PersistentMapBase<Key, Data, KeyHandler, DataHandler>::iterator
00281     PersistentMapBase<Key, Data, KeyHandler, DataHandler>::begin() const
00282     {
00283         MapType* ncthis = IT_CONST_CAST(MapType*, this);
00284         return iterator(ncthis, m_dbm.begin());
00285     }
00286 
00287     template<class Key, class Data, class KeyHandler, class DataHandler>
00288     inline IT_TYPENAME PersistentMapBase<Key, Data, KeyHandler, DataHandler>::iterator
00289     PersistentMapBase<Key, Data, KeyHandler, DataHandler>::end() const
00290     {
00291         MapType* ncthis = IT_CONST_CAST(MapType*, this);
00292         return iterator(ncthis, m_dbm.end());
00293     }
00294 
00295     template<class Key, class Data, class KeyHandler, class DataHandler>
00296     inline IT_Bool
00297     PersistentMapBase<Key, Data, KeyHandler, DataHandler>::empty() const
00298     {
00299         return size() == 0;
00300     }
00301 
00302     template<class Key, class Data, class KeyHandler, class DataHandler>
00303     inline IT_TYPENAME PersistentMapBase<Key, Data, KeyHandler, DataHandler>::size_type
00304     PersistentMapBase<Key, Data, KeyHandler, DataHandler>::size() const
00305     {
00306         return m_dbm.get_size();
00307     }
00308 }
00309 
00310 #if IT_SUPPORTS_COMPILE_TIME_INSTANTIATION
00311 #include <it_bus_pdk/persistent_map_base.cxx>
00312 #endif
00313 
00314 #endif  

Generated on Tue Mar 20 15:27:52 2007 for Artix by  doxygen 1.5.1-p1