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