source: XMLIO_V2/dev/dev_rv/src/XMLIO/xmlio_container.hpp @ 107

Last change on this file since 107 was 107, checked in by hozdoba, 14 years ago

Commit intermédiaire ...
Remontée de plusieurs fonctionnalités dans l'arbre des héritages.
Amélioration de la lisibilité du code.
etc.

File size: 3.0 KB
Line 
1#ifndef __XMLIO_CONTAINER__
2#define __XMLIO_CONTAINER__
3
4// Classes utilisées issues de Poco
5using Poco::HashMap;
6using Poco::Hash;
7
8// Classes utilisées issues de la STL
9using std::vector;
10using std::string;
11using std::pair;
12using std::ostream;
13
14// Classes XMLIOSERVER
15using XMLIOSERVER::XMLIOUndefinedValueException;
16
17namespace XMLIOSERVER
18{
19   template<class Key, class Mapped, class HashFunc = Hash<Key> >
20      class ExHashMap
21         : private HashMap<Key, Mapped*, Hash<Key> >
22   {
23      public :
24
25         ExHashMap() :  HashMap<Key, Mapped*, Hash<Key> >(), _elemList()
26         {/* Ne rien faire de plus */}
27
28         Mapped* operator[] (const Key& kval) throw (XMLIOUndefinedValueException)
29         {
30            if(!hasMappedValue(kval))
31               throw XMLIOUndefinedValueException("Appel de la méthode ExHashMap::operator["+kval+"] invalide.");
32            return (find(kval)->second);
33         }
34
35         bool hasMappedValue(const Key& kval) {return (find(kval) != this->end());}
36
37         const vector<Mapped*>& getVector(void) const { return (this->_elemList); }
38
39         size_t getVectorSize(void) const {return (this->_elemList.size());}
40         size_t getSize(void) const {return (this->size());}
41
42         virtual ~ExHashMap()
43         {/* Ne rien faire de plus */}
44
45      protected :
46
47         bool addValue(const Key& kval, Mapped* element)
48         {
49            pair<typename ExHashMap::Iterator,bool> p = this->insert(make_pair (kval,element));
50            if(!p.second) return (false);
51            return (this->addValue(element));
52         }
53
54         bool addValue(Mapped* element)
55         {   this->_elemList.insert(this->_elemList.end(), element);   return (true);   }
56
57
58         void removeValue(const Key& kval) // Non testé
59         { Mapped* element = find(kval)->second; removeValue(element); this->erase(kval); }
60
61         void removeValue(const Mapped* element) // Non testé
62         {
63            for (int i = 0; i < this->_elemList.size(); i++)
64               if (*this->_elemList[i] == element )
65                  this->_elemList.erase(this->_elemList.begin()+i);
66
67            delete element;
68         }
69
70      private :
71         vector<Mapped*> _elemList;
72
73   }; // class ExHashMap
74
75   /////////////////////////////////////////////////////////////
76
77   template<class Mapped>
78      class StrHashMap
79         : public ExHashMap<string, Mapped, Hash<string> >
80   {
81      public :
82
83         StrHashMap() :  ExHashMap<string, Mapped, Hash<string> >()
84         {/* Ne rien faire de plus */}
85
86         bool addObject(Mapped* element)
87         { if(element->hasId()) return(addValue(element->getId(), element));   return(addValue(element)); }
88
89         bool removeObject(const string& kval)
90         {
91            if(!ExHashMap<string, Mapped, Hash<string> >::hasMappedValue(kval)) return (false);
92            ExHashMap<string, Mapped, Hash<string> >::removeValue(kval); return (true);
93         }
94
95         virtual ~StrHashMap()
96         {/* Ne rien faire de plus */}
97
98   }; // class StrHashMap
99
100}; // namespace XMLIOSERVER
101
102#endif // __XMLIO_CONTAINER__
Note: See TracBrowser for help on using the repository browser.