forked from rasmus-storjohann/Inject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeMap.hpp
More file actions
61 lines (43 loc) · 1.51 KB
/
TypeMap.hpp
File metadata and controls
61 lines (43 loc) · 1.51 KB
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
#include <unordered_map>
#include <atomic>
namespace Injection
{
namespace Detail
{
extern std::atomic<int> _typeCounter;
template <class T>
static int RemovedConstTypeId()
{
static int id = ++_typeCounter;
return id;
}
template <class T>
static int TypeId()
{
using TRemovedConst = typename std::remove_const<T>::type;
return RemovedConstTypeId<TRemovedConst>();
}
template <class T>
class TypeMap
{
using Storage = std::unordered_map<int, T>;
public:
using iterator = typename Storage::iterator;
using const_iterator = typename Storage::const_iterator;
iterator begin() { return _storage.begin(); }
iterator end() { return _storage.end(); }
const_iterator begin() const { return _storage.begin(); }
const_iterator end() const { return _storage.end(); }
const_iterator cbegin() const { return _storage.cbegin(); }
const_iterator cend() const { return _storage.cend(); }
template <class Key>
iterator find() { return _storage.find(TypeId<Key>()); }
template <class Key>
const_iterator find() const { return _storage.find(TypeId<Key>()); }
template <class Key>
void put(T&& t) { _storage[TypeId<Key>()] = std::forward<T>(t); }
private:
Storage _storage;
};
}
}