-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashedregistrable.h
59 lines (49 loc) · 1.69 KB
/
hashedregistrable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef HASHEDREGISTRABLE_H_INCLUDED
#define HASHEDREGISTRABLE_H_INCLUDED
#include "ddobject.h"
#include <unordered_map>
#include <iostream>
namespace dd {
template <typename T>
class HashedRegistrable : public DdObject {
#define HASHEDREGISTRABLE_NAME "HashedRegistrable"
public:
std::unordered_map<string, list<T *>> containers;
/**
* Register element before the given iterator.
*/
virtual typename list<T *>::iterator insert(T * toInsert,
typename list<T *>::iterator antecedent) {
containers[toInsert->typeName()].insert(antecedent, toInsert);
return --antecedent;
}
/**
* Register the element at the end.
*/
virtual typename list<T *>::iterator insert(T * toInsert) {
return insert(toInsert, containers[toInsert->typeName()].end());
}
/**
* Erase the element of the given iterator
*/
virtual void erase(typename list<T *>::iterator toErase) {
containers[(*toErase)->typeName()].erase(toErase);
}
/**
* Get the container of the given type
*/
template <typename U>
list<T *> & getContainer() {
return containers[U::staticTypeName()];
}
/**
* Get the container of the given key
*/
list <T *> & getContainer(const string & key) {
return containers[key];
}
virtual string typeName() const { return HASHEDREGISTRABLE_NAME; }
static string staticTypeName() { return HASHEDREGISTRABLE_NAME; }
};
}
#endif // HASHEDREGISTRABLE_H_INCLUDED