1.1 --- a/src/work/deba/map_registry.h Wed May 12 14:07:00 2004 +0000
1.2 +++ b/src/work/deba/map_registry.h Thu May 13 08:20:39 2004 +0000
1.3 @@ -5,91 +5,195 @@
1.4
1.5 using namespace std;
1.6
1.7 -
1.8 -namespace hugo {
1.9 - template <typename G, typename K, typename KIt>
1.10 - class MapRegistry;
1.11 -}
1.12 -
1.13 -#include "map_base.h"
1.14 +/**
1.15 + Registry class to register edge or node maps in the graph. The
1.16 + registry helps you to implement an observer pattern. If you add
1.17 + or erase an edge or node you must notify all the maps about the
1.18 + event.
1.19 +*/
1.20
1.21 namespace hugo {
1.22
1.23 - template <typename G, typename K, typename KIt>
1.24 - class MapRegistry {
1.25 - public:
1.26 - typedef G Graph;
1.27 - typedef K Key;
1.28 - typedef KIt KeyIt;
1.29 + template <typename G, typename K, typename KIt>
1.30 + class MapRegistry {
1.31 + public:
1.32 + typedef G Graph;
1.33 + typedef K Key;
1.34 + typedef KIt KeyIt;
1.35
1.36 - typedef MapBase<Graph, Key, KIt> Map;
1.37 - friend class Base;
1.38 +
1.39 +
1.40 + class MapBase {
1.41 + public:
1.42 + typedef G Graph;
1.43 + typedef MapRegistry<G, K, KIt> Registry;
1.44 + typedef K Key;
1.45 + typedef KIt KeyIt;
1.46
1.47 - protected:
1.48 + friend class Registry;
1.49 +
1.50 + /**
1.51 + Default constructor.
1.52 + */
1.53 +
1.54 + MapBase() : graph(0), registry(0) {}
1.55 +
1.56 + /**
1.57 + Simple constructor to register into a graph registry.
1.58 + */
1.59
1.60 - typedef std::vector<Map*> Container;
1.61 - Container container;
1.62 + MapBase(Graph& g, Registry& r) : graph(&g), registry(0) {
1.63 + r.attach(*this);
1.64 + }
1.65 +
1.66 + /**
1.67 + Copy constructor with registering into the map.
1.68 + */
1.69 +
1.70 + MapBase(const MapBase& copy) : registry(0), graph(copy.graph) {
1.71 + if (copy.registry) {
1.72 + copy.registry->attach(*this);
1.73 + }
1.74 + }
1.75 +
1.76 + /**
1.77 + Assign operator.
1.78 + */
1.79 +
1.80 + const MapBase& operator=(const MapBase& copy) {
1.81 + if (registry) {
1.82 + registry->detach(*this);
1.83 + }
1.84 + graph = copy.graph;
1.85 + if (copy.registry) {
1.86 + copy.registry->attach(*this);
1.87 + }
1.88 + }
1.89 +
1.90 +
1.91 + /**
1.92 + Destructor.
1.93 + */
1.94 +
1.95 + virtual ~MapBase() {
1.96 + if (registry) {
1.97 + registry->detach(*this);
1.98 + }
1.99 + }
1.100 +
1.101 + protected:
1.102 +
1.103 + Graph* graph;
1.104 + Registry* registry;
1.105 +
1.106 + int registry_index;
1.107 +
1.108 + /**
1.109 + Helper function to implement constructors in the subclasses.
1.110 + */
1.111 +
1.112 + virtual void init() {
1.113 + for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
1.114 + add(it);
1.115 + }
1.116 + }
1.117 +
1.118 + /**
1.119 + Helper function to implement the destructor in the subclasses.
1.120 + */
1.121 +
1.122 + virtual void destroy() {
1.123 + for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
1.124 + erase(it);
1.125 + }
1.126 + }
1.127 +
1.128 + /**
1.129 + The add member function should be overloaded in the subclasses.
1.130 + \e Add extends the map with the new node.
1.131 + */
1.132 +
1.133 + virtual void add(const Key&) = 0;
1.134 + /**
1.135 + The erase member function should be overloaded in the subclasses.
1.136 + \e Erase removes the node from the map.
1.137 + */
1.138 +
1.139 + virtual void erase(const Key&) = 0;
1.140 +
1.141 + /**
1.142 + Exception class to throw at unsupported operation.
1.143 + */
1.144 +
1.145 + class NotSupportedOperationException {};
1.146 +
1.147 + };
1.148 +
1.149 + protected:
1.150 +
1.151 + typedef std::vector<MapBase*> Container;
1.152 + Container container;
1.153
1.154
1.155 - public:
1.156 + public:
1.157
1.158 - MapRegistry() {}
1.159 + MapRegistry() {}
1.160
1.161 - MapRegistry(const MapRegistry&) {}
1.162 + MapRegistry(const MapRegistry&) {}
1.163
1.164 - MapRegistry& operator=(const MapRegistry&) {
1.165 - for (it = container.begin(); it != container.end(); ++it) {
1.166 - (*it)->destroy();
1.167 - (*it)->graph = 0;
1.168 - (*it)->registry = 0;
1.169 - }
1.170 - }
1.171 + MapRegistry& operator=(const MapRegistry&) {
1.172 + for (it = container.begin(); it != container.end(); ++it) {
1.173 + (*it)->destroy();
1.174 + (*it)->graph = 0;
1.175 + (*it)->registry = 0;
1.176 + }
1.177 + }
1.178
1.179 - ~MapRegistry() {
1.180 - typename Container::iterator it;
1.181 - for (it = container.begin(); it != container.end(); ++it) {
1.182 - (*it)->destroy();
1.183 - (*it)->registry = 0;
1.184 - (*it)->graph = 0;
1.185 - }
1.186 - }
1.187 + ~MapRegistry() {
1.188 + typename Container::iterator it;
1.189 + for (it = container.begin(); it != container.end(); ++it) {
1.190 + (*it)->destroy();
1.191 + (*it)->registry = 0;
1.192 + (*it)->graph = 0;
1.193 + }
1.194 + }
1.195
1.196
1.197 - public:
1.198 + public:
1.199
1.200 - void attach(Map& map) {
1.201 - if (map.registry) {
1.202 - map.registry->detach(map);
1.203 - }
1.204 - container.push_back(&map);
1.205 - map.registry = this;
1.206 - map.registry_index = container.size()-1;
1.207 - }
1.208 + void attach(MapBase& map) {
1.209 + if (map.registry) {
1.210 + map.registry->detach(map);
1.211 + }
1.212 + container.push_back(&map);
1.213 + map.registry = this;
1.214 + map.registry_index = container.size()-1;
1.215 + }
1.216
1.217 - void detach(Map& map) {
1.218 - container.back()->registry_index = map.registry_index;
1.219 - container[map.registry_index] = container.back();
1.220 - container.pop_back();
1.221 - map.registry = 0;
1.222 - map.graph = 0;
1.223 - }
1.224 + void detach(MapBase& map) {
1.225 + container.back()->registry_index = map.registry_index;
1.226 + container[map.registry_index] = container.back();
1.227 + container.pop_back();
1.228 + map.registry = 0;
1.229 + map.graph = 0;
1.230 + }
1.231
1.232
1.233 - virtual void add(Key& key) {
1.234 - typename Container::iterator it;
1.235 - for (it = container.begin(); it != container.end(); ++it) {
1.236 - (*it)->add(key);
1.237 - }
1.238 - }
1.239 + virtual void add(Key& key) {
1.240 + typename Container::iterator it;
1.241 + for (it = container.begin(); it != container.end(); ++it) {
1.242 + (*it)->add(key);
1.243 + }
1.244 + }
1.245
1.246 - virtual void erase(Key& key) {
1.247 - typename Container::iterator it;
1.248 - for (it = container.begin(); it != container.end(); ++it) {
1.249 - (*it)->erase(key);
1.250 - }
1.251 - }
1.252 + virtual void erase(Key& key) {
1.253 + typename Container::iterator it;
1.254 + for (it = container.begin(); it != container.end(); ++it) {
1.255 + (*it)->erase(key);
1.256 + }
1.257 + }
1.258
1.259 - };
1.260 + };
1.261
1.262 }
1.263