src/work/deba/node_map_base.h
author marci
Tue, 20 Apr 2004 12:10:36 +0000
changeset 353 eeae2f4a0d74
parent 336 8ff3b3e05478
child 377 33fe0ee01dc5
permissions -rw-r--r--
template first goes to comment...
     1 #ifndef NODE_MAP_BASE_H
     2 #define NODE_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 NodeMapRegistry class.
     8 	\param The second template parameter is the Node type of the class.
     9 	
    10 */
    11 
    12 template <typename G, typename K>
    13 class NodeMapBase {
    14 public:
    15 	typedef G Graph;
    16 
    17 	typedef K KeyType;
    18 	
    19 	/** 
    20 		Default constructor.
    21 	*/	
    22 	
    23 	NodeMapBase() : graph(0) {}
    24 
    25 	/** 
    26 		Simple constructor to register into a graph.
    27 	*/
    28 	
    29 	NodeMapBase(Graph& g) : graph(&g) {
    30 		graph->node_maps.add(*this);
    31 	}
    32 
    33 	/** 
    34 		Copy constructor with registering into the map.
    35 	*/	
    36 	
    37 	NodeMapBase(const NodeMapBase& copy) : graph(copy.graph) {
    38 		if (graph) {
    39 			graph->node_maps.add(*this);
    40 		}
    41 	} 
    42 	
    43 	/** 
    44 		Assign operator.
    45 	*/	
    46 
    47 	const NodeMapBase& operator=(const NodeMapBase& copy) {
    48 		if (graph) {
    49 			graph.node_maps.erase(*this);
    50 		}
    51 		graph = copy.graph;
    52 		if (graph) {
    53 			graph->node_maps.add(*this);
    54 		}
    55 		
    56 	}
    57 	
    58 
    59 	/** 
    60 		Destructor.
    61 	*/	
    62 
    63 	virtual ~NodeMapBase() {
    64 		if (graph) {
    65 			graph.node_maps.erase(*this);
    66 		}
    67 	}
    68 	
    69 protected:
    70 	
    71 	Graph* graph;
    72 
    73 	int graph_index;
    74 	
    75 	/**
    76 		Helper function to implement the default constructor in the subclasses.
    77 	*/
    78 	
    79 	void init() {
    80 		for (Graph::NodeIt it(g); g.valid(it); g.next(it)) {
    81 			add(it);
    82 		}
    83 	}
    84 	
    85 	/**
    86 		Helper function to implement the destructor in the subclasses.
    87 	*/
    88 	
    89 	void destroy() {
    90 		for (Graph::NodeIt it(g); g.valid(it); g.next(it)) {
    91 			erase(it);
    92 		}
    93 	}
    94 	
    95 	/** 
    96 		The add member function should be overloaded in the subclasses.
    97 		\e Add extends the map with the new node.
    98 	*/
    99 	
   100 	virtual void add(const KeyType&) = 0;
   101 	
   102 	/** 
   103 		The erase member function should be overloaded in the subclasses.
   104 		\e Erase removes the node from the map.
   105 	*/
   106 	
   107 	virtual void erase(const KeyType&) = 0;
   108 	
   109 	/**
   110 		Exception class to throw at unsupported operation.
   111 	*/
   112 	
   113 	class NotSupportedOperationException {};
   114 
   115 	friend class Graph;
   116 };
   117 
   118 #endif