]> git.localhorst.tv Git - blank.git/blob - src/world/BlockTypeRegistry.hpp
server save path argument without trailing slash
[blank.git] / src / world / BlockTypeRegistry.hpp
1 #ifndef BLANK_WORLD_BLOCKTYPEREGISTRY_HPP_
2 #define BLANK_WORLD_BLOCKTYPEREGISTRY_HPP_
3
4 #include "BlockType.hpp"
5
6 #include <map>
7 #include <string>
8 #include <vector>
9
10
11 namespace blank {
12
13 class BlockTypeRegistry {
14
15 public:
16         using size_type = std::vector<BlockType>::size_type;
17         using reference = std::vector<BlockType>::reference;
18         using const_reference = std::vector<BlockType>::const_reference;
19         using iterator = std::vector<BlockType>::iterator;
20         using const_iterator = std::vector<BlockType>::const_iterator;
21
22 public:
23         BlockTypeRegistry();
24
25         /// register a new block type
26         /// this may throw if the name is already taken (names must be unique)
27         Block::Type Add(BlockType &&);
28
29         size_t size() const noexcept { return types.size(); }
30
31         iterator begin() noexcept { return types.begin(); }
32         const_iterator begin() const noexcept { return types.begin(); }
33         iterator end() noexcept { return types.end(); }
34         const_iterator end() const noexcept { return types.end(); }
35
36         /// lookup by ID
37         BlockType &operator [](Block::Type id) noexcept { return types[id]; }
38         const BlockType &operator [](Block::Type id) const noexcept { return types[id]; }
39
40         BlockType &Get(Block::Type id) noexcept { return types[id]; }
41         const BlockType &Get(Block::Type id) const noexcept { return types[id]; }
42
43         /// lookup by name
44         BlockType &Get(const std::string &name);
45         const BlockType &Get(const std::string &name) const;
46
47 private:
48         std::vector<BlockType> types;
49         std::map<std::string, Block::Type> names;
50
51 };
52
53 }
54
55 #endif