1 #ifndef BLOBS_WORLD_SET_HPP_
2 #define BLOBS_WORLD_SET_HPP_
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);
21 types.emplace_back(t);
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(); }
28 typename std::vector<Type>::size_type Size() const noexcept { return types.size(); }
30 Type &operator [](int id) noexcept { return types[id]; }
31 const Type &operator [](int id) const noexcept { return types[id]; }
33 Type &operator [](const std::string &name) {
34 auto entry = names.find(name);
35 if (entry != names.end()) {
36 return types[entry->second];
38 throw std::runtime_error("unknown type " + name);
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];
46 throw std::runtime_error("unknown type " + name);
51 std::vector<Type> types;
52 std::map<std::string, int> names;