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