author | marci |
Fri, 23 Apr 2004 08:08:41 +0000 | |
changeset 380 | 6399494e30b1 |
child 571 | 9632ea8be6ca |
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@378 | 42 |
MapBase(Registry& r) : registry(0) { |
deba@378 | 43 |
registry->add(*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@378 | 50 |
MapBase(const MapBase& copy) : registry(0) { |
deba@378 | 51 |
if (registry) { |
deba@378 | 52 |
registry->add(*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@378 | 62 |
registry->erase(*this); |
deba@378 | 63 |
} |
deba@378 | 64 |
registry = copy.registry; |
deba@378 | 65 |
if (registry) { |
deba@378 | 66 |
registry->add(*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@378 | 77 |
registry->erase(*this); |
deba@378 | 78 |
} |
deba@378 | 79 |
} |
deba@378 | 80 |
|
deba@378 | 81 |
protected: |
deba@378 | 82 |
|
deba@378 | 83 |
Registry* registry; |
deba@378 | 84 |
|
deba@378 | 85 |
int registry_index; |
deba@378 | 86 |
|
deba@378 | 87 |
/** |
deba@378 | 88 |
Helper function to implement the default constructor in the subclasses. |
deba@378 | 89 |
*/ |
deba@378 | 90 |
|
deba@378 | 91 |
virtual void init(Graph& g) { |
deba@378 | 92 |
|
deba@378 | 93 |
for (KeyIt it(g); g.valid(it); g.next(it)) { |
deba@378 | 94 |
add(it); |
deba@378 | 95 |
} |
deba@378 | 96 |
} |
deba@378 | 97 |
|
deba@378 | 98 |
/** |
deba@378 | 99 |
Helper function to implement the destructor in the subclasses. |
deba@378 | 100 |
*/ |
deba@378 | 101 |
|
deba@378 | 102 |
virtual void destroy(Graph& g) { |
deba@378 | 103 |
for (KeyIt it(g); g.valid(it); g.next(it)) { |
deba@378 | 104 |
erase(it); |
deba@378 | 105 |
} |
deba@378 | 106 |
} |
deba@378 | 107 |
|
deba@378 | 108 |
/** |
deba@378 | 109 |
The add member function should be overloaded in the subclasses. |
deba@378 | 110 |
\e Add extends the map with the new node. |
deba@378 | 111 |
*/ |
deba@378 | 112 |
|
deba@378 | 113 |
virtual void add(const KeyType&) = 0; |
deba@378 | 114 |
|
deba@378 | 115 |
/** |
deba@378 | 116 |
The erase member function should be overloaded in the subclasses. |
deba@378 | 117 |
\e Erase removes the node from the map. |
deba@378 | 118 |
*/ |
deba@378 | 119 |
|
deba@378 | 120 |
virtual void erase(const KeyType&) = 0; |
deba@378 | 121 |
|
deba@378 | 122 |
/** |
deba@378 | 123 |
Exception class to throw at unsupported operation. |
deba@378 | 124 |
*/ |
deba@378 | 125 |
|
deba@378 | 126 |
class NotSupportedOperationException {}; |
deba@378 | 127 |
|
deba@378 | 128 |
}; |
deba@378 | 129 |
|
deba@378 | 130 |
} |
deba@378 | 131 |
|
deba@378 | 132 |
#endif |