[378] | 1 | #ifndef MAP_BASE_H |
---|
| 2 | #define MAP_BASE_H |
---|
| 3 | |
---|
[595] | 4 | using namespace std; |
---|
| 5 | |
---|
[378] | 6 | /** |
---|
[595] | 7 | Template base class for implementing mapping on nodes and edges. |
---|
| 8 | \param The first template parameter is the Graph class. |
---|
| 9 | \param The second template parameter is the key type. |
---|
| 10 | \param The third template parameter is an iterator on |
---|
| 11 | the keys. |
---|
[378] | 12 | */ |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | namespace hugo { |
---|
| 16 | template <typename G, typename K, typename KIt> |
---|
| 17 | class MapBase; |
---|
| 18 | } |
---|
| 19 | |
---|
| 20 | #include "map_registry.h" |
---|
| 21 | |
---|
| 22 | namespace hugo { |
---|
| 23 | |
---|
| 24 | template <typename G, typename K, typename KIt> |
---|
| 25 | class MapBase { |
---|
| 26 | public: |
---|
| 27 | typedef G Graph; |
---|
| 28 | typedef MapRegistry<G, K, KIt> Registry; |
---|
[595] | 29 | typedef K Key; |
---|
[378] | 30 | typedef KIt KeyIt; |
---|
| 31 | |
---|
| 32 | friend class Registry; |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | Default constructor. |
---|
| 36 | */ |
---|
| 37 | |
---|
[595] | 38 | MapBase() : graph(0), registry(0) {} |
---|
[378] | 39 | |
---|
| 40 | /** |
---|
| 41 | Simple constructor to register into a graph registry. |
---|
| 42 | */ |
---|
| 43 | |
---|
[571] | 44 | MapBase(Graph& g, Registry& r) : graph(&g), registry(0) { |
---|
[595] | 45 | r.attach(*this); |
---|
[378] | 46 | } |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | Copy constructor with registering into the map. |
---|
| 50 | */ |
---|
| 51 | |
---|
[571] | 52 | MapBase(const MapBase& copy) : registry(0), graph(copy.graph) { |
---|
| 53 | if (copy.registry) { |
---|
| 54 | copy.registry->attach(*this); |
---|
[378] | 55 | } |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | Assign operator. |
---|
| 60 | */ |
---|
| 61 | |
---|
| 62 | const MapBase& operator=(const MapBase& copy) { |
---|
| 63 | if (registry) { |
---|
[571] | 64 | registry->detach(*this); |
---|
[378] | 65 | } |
---|
[571] | 66 | graph = copy.graph; |
---|
| 67 | if (copy.registry) { |
---|
| 68 | copy.registry->attach(*this); |
---|
[378] | 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | Destructor. |
---|
| 75 | */ |
---|
| 76 | |
---|
| 77 | virtual ~MapBase() { |
---|
| 78 | if (registry) { |
---|
[571] | 79 | registry->detach(*this); |
---|
[378] | 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | protected: |
---|
| 84 | |
---|
[595] | 85 | Graph* graph; |
---|
[378] | 86 | Registry* registry; |
---|
| 87 | |
---|
| 88 | int registry_index; |
---|
| 89 | |
---|
| 90 | /** |
---|
[595] | 91 | Helper function to implement constructors in the subclasses. |
---|
[378] | 92 | */ |
---|
| 93 | |
---|
[571] | 94 | virtual void init() { |
---|
| 95 | for (KeyIt it(*graph); graph->valid(it); graph->next(it)) { |
---|
[378] | 96 | add(it); |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | Helper function to implement the destructor in the subclasses. |
---|
| 102 | */ |
---|
| 103 | |
---|
[571] | 104 | virtual void destroy() { |
---|
| 105 | for (KeyIt it(*graph); graph->valid(it); graph->next(it)) { |
---|
[378] | 106 | erase(it); |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | /** |
---|
| 111 | The add member function should be overloaded in the subclasses. |
---|
| 112 | \e Add extends the map with the new node. |
---|
| 113 | */ |
---|
| 114 | |
---|
[595] | 115 | virtual void add(const Key&) = 0; |
---|
[378] | 116 | /** |
---|
| 117 | The erase member function should be overloaded in the subclasses. |
---|
| 118 | \e Erase removes the node from the map. |
---|
| 119 | */ |
---|
| 120 | |
---|
[595] | 121 | virtual void erase(const Key&) = 0; |
---|
[378] | 122 | |
---|
| 123 | /** |
---|
| 124 | Exception class to throw at unsupported operation. |
---|
| 125 | */ |
---|
| 126 | |
---|
| 127 | class NotSupportedOperationException {}; |
---|
| 128 | |
---|
| 129 | }; |
---|
| 130 | |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | #endif |
---|