Rev | Line | |
---|
[378] | 1 | #ifndef MAP_REGISTRY_H |
---|
| 2 | #define MAP_REGISTRY_H |
---|
| 3 | |
---|
| 4 | #include <vector> |
---|
| 5 | |
---|
[595] | 6 | using namespace std; |
---|
| 7 | |
---|
[378] | 8 | |
---|
| 9 | namespace hugo { |
---|
| 10 | template <typename G, typename K, typename KIt> |
---|
| 11 | class MapRegistry; |
---|
| 12 | } |
---|
| 13 | |
---|
| 14 | #include "map_base.h" |
---|
| 15 | |
---|
| 16 | namespace hugo { |
---|
| 17 | |
---|
| 18 | template <typename G, typename K, typename KIt> |
---|
| 19 | class MapRegistry { |
---|
| 20 | public: |
---|
| 21 | typedef G Graph; |
---|
| 22 | typedef K Key; |
---|
| 23 | typedef KIt KeyIt; |
---|
| 24 | |
---|
| 25 | typedef MapBase<Graph, Key, KIt> Map; |
---|
| 26 | friend class Base; |
---|
| 27 | |
---|
| 28 | protected: |
---|
| 29 | |
---|
| 30 | typedef std::vector<Map*> Container; |
---|
| 31 | Container container; |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | public: |
---|
| 35 | |
---|
[571] | 36 | MapRegistry() {} |
---|
| 37 | |
---|
| 38 | MapRegistry(const MapRegistry&) {} |
---|
| 39 | |
---|
| 40 | MapRegistry& operator=(const MapRegistry&) { |
---|
| 41 | for (it = container.begin(); it != container.end(); ++it) { |
---|
| 42 | (*it)->destroy(); |
---|
| 43 | (*it)->graph = 0; |
---|
| 44 | (*it)->registry = 0; |
---|
| 45 | } |
---|
| 46 | } |
---|
[378] | 47 | |
---|
| 48 | ~MapRegistry() { |
---|
| 49 | typename Container::iterator it; |
---|
| 50 | for (it = container.begin(); it != container.end(); ++it) { |
---|
[571] | 51 | (*it)->destroy(); |
---|
[378] | 52 | (*it)->registry = 0; |
---|
[571] | 53 | (*it)->graph = 0; |
---|
[378] | 54 | } |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | public: |
---|
| 59 | |
---|
[571] | 60 | void attach(Map& map) { |
---|
[378] | 61 | if (map.registry) { |
---|
[571] | 62 | map.registry->detach(map); |
---|
[378] | 63 | } |
---|
| 64 | container.push_back(&map); |
---|
| 65 | map.registry = this; |
---|
| 66 | map.registry_index = container.size()-1; |
---|
| 67 | } |
---|
| 68 | |
---|
[595] | 69 | void detach(Map& map) { |
---|
| 70 | container.back()->registry_index = map.registry_index; |
---|
| 71 | container[map.registry_index] = container.back(); |
---|
[378] | 72 | container.pop_back(); |
---|
[595] | 73 | map.registry = 0; |
---|
| 74 | map.graph = 0; |
---|
[378] | 75 | } |
---|
| 76 | |
---|
| 77 | |
---|
[595] | 78 | virtual void add(Key& key) { |
---|
[378] | 79 | typename Container::iterator it; |
---|
| 80 | for (it = container.begin(); it != container.end(); ++it) { |
---|
| 81 | (*it)->add(key); |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
[595] | 85 | virtual void erase(Key& key) { |
---|
[378] | 86 | typename Container::iterator it; |
---|
| 87 | for (it = container.begin(); it != container.end(); ++it) { |
---|
| 88 | (*it)->erase(key); |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | }; |
---|
| 93 | |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.