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 Type &operator [](int id) noexcept { return types[id]; }
29 const Type &operator [](int id) const noexcept { return types[id]; }
31 Type &operator [](const std::string &name) {
32 auto entry = names.find(name);
33 if (entry != names.end()) {
34 return types[entry->second];
36 throw std::runtime_error("unknown type " + name);
39 const Type &operator [](const std::string &name) const {
40 auto entry = names.find(name);
41 if (entry != names.end()) {
42 return types[entry->second];
44 throw std::runtime_error("unknown type " + name);
49 std::vector<Type> types;
50 std::map<std::string, int> names;