]> git.localhorst.tv Git - blobs.git/blob - src/world/Set.hpp
test asset loading
[blobs.git] / src / world / Set.hpp
1 #ifndef BLOBS_WORLD_SET_HPP_
2 #define BLOBS_WORLD_SET_HPP_
3
4 #include <map>
5 #include <stdexcept>
6 #include <string>
7 #include <vector>
8
9 namespace blobs {
10 namespace world {
11
12 template<class Type>
13 class Set {
14
15 public:
16         int Add(const Type &t) {
17                 int id = types.size();
18                 if (!names.emplace(t.name, id).second) {
19                         throw std::runtime_error("duplicate type name " + t.name);
20                 }
21                 types.emplace_back(t);
22                 types.back().id = id;
23                 return id;
24         }
25         bool Has(int id) const noexcept { return id < types.size(); }
26         bool Has(const std::string &name) const noexcept { return names.find(name) != names.end(); }
27
28         typename std::vector<Type>::size_type Size() const noexcept { return types.size(); }
29
30         Type &operator [](int id) noexcept { return types[id]; }
31         const Type &operator [](int id) const noexcept { return types[id]; }
32
33         Type &operator [](const std::string &name) {
34                 auto entry = names.find(name);
35                 if (entry != names.end()) {
36                         return types[entry->second];
37                 } else {
38                         throw std::runtime_error("unknown type " + name);
39                 }
40         }
41         const Type &operator [](const std::string &name) const {
42                 auto entry = names.find(name);
43                 if (entry != names.end()) {
44                         return types[entry->second];
45                 } else {
46                         throw std::runtime_error("unknown type " + name);
47                 }
48         }
49
50 private:
51         std::vector<Type> types;
52         std::map<std::string, int> names;
53
54 };
55
56 }
57 }
58
59 #endif