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