#ifndef MAP_BASE_H #define MAP_BASE_H using namespace std; /** Template base class for implementing mapping on nodes and edges. \param The first template parameter is the Graph class. \param The second template parameter is the key type. \param The third template parameter is an iterator on the keys. */ namespace hugo { template class MapBase; } #include "map_registry.h" namespace hugo { template class MapBase { public: typedef G Graph; typedef MapRegistry Registry; typedef K Key; typedef KIt KeyIt; friend class Registry; /** Default constructor. */ MapBase() : graph(0), registry(0) {} /** Simple constructor to register into a graph registry. */ MapBase(Graph& g, Registry& r) : graph(&g), registry(0) { r.attach(*this); } /** Copy constructor with registering into the map. */ MapBase(const MapBase& copy) : registry(0), graph(copy.graph) { if (copy.registry) { copy.registry->attach(*this); } } /** Assign operator. */ const MapBase& operator=(const MapBase& copy) { if (registry) { registry->detach(*this); } graph = copy.graph; if (copy.registry) { copy.registry->attach(*this); } } /** Destructor. */ virtual ~MapBase() { if (registry) { registry->detach(*this); } } protected: Graph* graph; Registry* registry; int registry_index; /** Helper function to implement constructors in the subclasses. */ virtual void init() { for (KeyIt it(*graph); graph->valid(it); graph->next(it)) { add(it); } } /** Helper function to implement the destructor in the subclasses. */ virtual void destroy() { for (KeyIt it(*graph); graph->valid(it); graph->next(it)) { erase(it); } } /** The add member function should be overloaded in the subclasses. \e Add extends the map with the new node. */ virtual void add(const Key&) = 0; /** The erase member function should be overloaded in the subclasses. \e Erase removes the node from the map. */ virtual void erase(const Key&) = 0; /** Exception class to throw at unsupported operation. */ class NotSupportedOperationException {}; }; } #endif