-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternalizer.hpp
70 lines (61 loc) · 2.02 KB
/
internalizer.hpp
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
60
61
62
63
64
65
66
67
68
69
70
#ifndef INTERNALIZER_HEADER
#define INTERNALIZER_HEADER
#include <cassert>
#include <unordered_map>
#include "base_class.hpp"
template<typename T>const T*internalizer_clone(const T©, typename std::enable_if<!std::is_base_of<base_class, T>::value, T>::type* = nullptr) {
return new T{copy};
}
template<typename T>const T*internalizer_clone(const T©, typename std::enable_if<std::is_base_of<base_class, T>::value, T>::type* = nullptr) {
return dynamic_cast<const T*>(copy.clone());
}
template<typename T>class internalizer {
protected:
struct key_type {
const T*value;
bool operator <(const key_type&other) const {
return *value < *other.value;
}
bool operator ==(const key_type&other) const {
return *value == *other.value;
}
};
struct key_type_hash {
size_t operator()(const key_type&key) const {
static std::hash<T>subhash;
return subhash(*key.value);
}
};
using map_type = std::unordered_map<key_type, unsigned, key_type_hash>;
using value_type = typename map_type::value_type;
using insertion_result_type = std::pair<typename map_type::iterator, bool>;
map_type elements;
public:
const T*lookup(const T&key) const {
typename map_type::const_iterator iterator = elements.find({&key});
if (iterator == elements.end()) {
return nullptr;
}
return iterator->first.value;
}
const T&acquire(const T&key) {
typename map_type::iterator iterator = elements.find({&key}), end = elements.end();
if (iterator != end) {
++(iterator->second);
return *(iterator->first.value);
}
const T*internalization = internalizer_clone(key);
insertion_result_type result = elements.insert(value_type{{internalization}, 1});
assert(result.second);
return *internalization;
}
void release(const T&key) {
typename map_type::iterator iterator = elements.find({&key});
assert(iterator != elements.end());
if (!--(iterator->second)) {
delete iterator->first.value;
elements.erase(iterator);
}
}
};
#endif