source: XMLIO_V2/dev/dev_rv/src/XMLIO/container.hpp @ 126

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

Amélioration de quelques portions de code.
Ajout de contructeurs par copie.

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