Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

pointermap.h

Go to the documentation of this file.
00001 
00007 #ifndef __POINTERMAP_H__
00008 #define __POINTERMAP_H__
00009 
00010 #include <map>
00011 #include <string>
00012 #include <algorithm>
00013 #include <functional>
00014 
00015 #include "generic_stdlib.h"
00016 
00017 
00018 
00019 /******************************************************************************/
00020 /* PointerMap class                                                           */
00021 /******************************************************************************/
00022 
00031 template<class T, class K=string, bool owns_objects=true>
00032 class PointerMap {
00033 public:
00034 
00036   typedef K  key_type;
00037 
00039   typedef T* data_type;
00040 
00042   typedef pair<const key_type, data_type> value_type;
00043 
00045   typedef std::map< key_type, data_type > map_type;
00046 
00048   typedef typename map_type::iterator iterator;
00049 
00051   typedef typename map_type::const_iterator const_iterator;  
00052 
00053   
00054   PointerMap() { }
00055   virtual ~PointerMap() {  erase();  }
00056 
00058   iterator       begin()       {  return m.begin();  } 
00059   const_iterator begin() const {  return m.begin();  } 
00060   iterator       end()         {  return m.end();    } 
00061   const_iterator end()   const {  return m.end();    } 
00063   pair<iterator,bool> insert(const value_type& x)  {  return m.insert(x);  }  
00064 
00072   void set(const key_type& k, const data_type d) {
00073     iterator existing = m.find(k);
00074     if (existing == end())
00075       insert(Generic::make_pair_leftconst(k,d));
00076     else {
00077       if (owns_objects) delete (*existing).second;
00078       (*existing).second=d;
00079     }
00080   }
00081 
00082   
00087   T* getptr(const key_type& k) {
00088     const_iterator existing = m.find(k);
00089     return (existing == end() ? 0 : (*existing).second);
00090   }
00091 
00093   const T* getptr(const key_type& k) const {
00094     const_iterator existing = m.find(k);
00095     return (existing == end() ? 0 : (*existing).second);
00096   }
00097 
00098     
00099   //  virtual T* clone() const { return new PointerMap(*this); };
00100 
00102   void erase() {
00103     if (owns_objects) 
00104       Generic::delete_contents(m);
00105     else
00106       m.erase(m.begin(),m.end());
00107   }
00108 
00110   void erase(const key_type& k) {
00111     T* ptr = getptr(k);
00112     if (ptr) {
00113       if (owns_objects) delete ptr;
00114       m.erase(k);
00115     }
00116   }
00117 protected:
00119   map_type m;
00120 };
00121 
00122 
00123 #endif /* __POINTERMAP_H__ */

Generated at Mon Aug 21 00:30:54 2000 for RF-LISSOM by doxygen1.2.1 written by Dimitri van Heesch, © 1997-2000