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