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