-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringID.h
49 lines (40 loc) · 859 Bytes
/
stringID.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
#include <iostream>
#include <vector>
#include <map>
class stringID
{
public:
stringID() = delete;
static stringID from_string(const std::string &name)
{
auto &map = get_map();
auto found = map.equal_range(name);
if (found.first == found.second) {
auto &vec = get_vec();
auto idx = vec.size();
map.insert(found.first, {name, idx});
vec.push_back(name);
return stringID{idx};
}
return stringID{found.first->second};
}
const std::string& name() const
{
return get_vec()[_idx];
}
private:
using Map = std::map<std::string, size_t>;
using Vec = std::vector<std::string>;
static Map& get_map()
{
static Map _map;
return _map;
}
static Vec& get_vec()
{
static Vec _vec;
return _vec;
}
explicit stringID(size_t idx) : _idx(idx) { }
size_t _idx;
};