VOX
A little voxel engine
Loading...
Searching...
No Matches
List.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <unordered_map>
4#include <mutex>
5#include <vector>
6
7template <typename IdType>
9{
10
11public:
12
13 IdGenerator() : m_next_id(1) {}
15
16 IdGenerator(IdGenerator & other) = delete;
17 IdGenerator(IdGenerator && other) = delete;
18 IdGenerator & operator=(IdGenerator & other) = delete;
19 IdGenerator & operator=(IdGenerator && other) = delete;
20
21 IdType nextId()
22 {
23 return m_next_id++;
24 }
25
26 static const inline IdType invalid_id = 0;
27
28private:
29
30 IdType m_next_id;
31
32};
33
34template <typename Key, typename Value, typename container = std::unordered_map<Key, Value>, typename IdGen = IdGenerator<Key>>
35class IdList: private container
36{
37
38public:
39
40 IdList() {}
42
43 IdList(const IdList & other) = delete;
44 IdList(IdList && other) = delete;
45 IdList & operator=(const IdList & other) = delete;
46 IdList & operator=(IdList && other) = delete;
47
48
49 std::lock_guard<std::mutex> lock()
50 {
51 return std::lock_guard(m_mutex);
52 }
53
54 using container::begin;
55 using container::end;
56 using container::at;
57 using container::find;
58
59 Value get(const Key & key)
60 {
61 std::lock_guard lock(m_mutex);
62 return container::at(key);
63 }
64
65 uint32_t size() const
66 {
67 std::lock_guard lock(m_mutex);
68 return container::size();
69 }
70
71 bool contains(const Key & key) const
72 {
73 std::lock_guard lock(m_mutex);
74 return container::find(key) != container::end();
75 }
76
77 Key insert(const Value & value)
78 {
79 std::lock_guard lock(m_mutex);
80 Key key = m_id_generator.nextId();
81 container::insert(std::make_pair(key, value));
82 return key;
83 }
84
85 Key insert(Value && value)
86 {
87 std::lock_guard lock(m_mutex);
88 Key key = m_id_generator.nextId();
89 container::insert(std::make_pair(key, std::move(value)));
90 return key;
91 }
92
93 void insert(const Key & key, const Value & value)
94 {
95 std::lock_guard lock(m_mutex);
96 container::insert(std::make_pair(key, value));
97 }
98
99 void insert(const Key & key, Value && value)
100 {
101 std::lock_guard lock(m_mutex);
102 container::insert(std::make_pair(key, std::move(value)));
103 }
104
105 void erase(const Key & key)
106 {
107 std::lock_guard lock(m_mutex);
108 container::erase(key);
109 }
110
111 std::vector<Value> values() const
112 {
113 std::lock_guard lock(m_mutex);
114 std::vector<Value> values;
115 values.reserve(container::size());
116 for (const auto & [key, value] : *this)
117 {
118 values.push_back(value);
119 }
120 return values;
121 }
122
123 static const inline Key invalid_id = IdGen::invalid_id;
124
125private:
126
127 IdGen m_id_generator;
128
129 mutable std::mutex m_mutex;
130};
131
Definition: List.hpp:9
IdGenerator(IdGenerator &&other)=delete
~IdGenerator()
Definition: List.hpp:14
static const IdType invalid_id
Definition: List.hpp:26
IdGenerator & operator=(IdGenerator &&other)=delete
IdGenerator & operator=(IdGenerator &other)=delete
IdGenerator()
Definition: List.hpp:13
IdGenerator(IdGenerator &other)=delete
IdType nextId()
Definition: List.hpp:21
Definition: List.hpp:36
std::vector< Value > values() const
Definition: List.hpp:111
IdList()
Definition: List.hpp:40
Key insert(Value &&value)
Definition: List.hpp:85
~IdList()
Definition: List.hpp:41
void erase(const Key &key)
Definition: List.hpp:105
void insert(const Key &key, const Value &value)
Definition: List.hpp:93
void insert(const Key &key, Value &&value)
Definition: List.hpp:99
IdList(const IdList &other)=delete
Value get(const Key &key)
Definition: List.hpp:59
bool contains(const Key &key) const
Definition: List.hpp:71
uint32_t size() const
Definition: List.hpp:65
std::lock_guard< std::mutex > lock()
Definition: List.hpp:49
Key insert(const Value &value)
Definition: List.hpp:77
static const Key invalid_id
Definition: List.hpp:123
IdList & operator=(const IdList &other)=delete
IdList & operator=(IdList &&other)=delete
IdList(IdList &&other)=delete