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