Introduced Content, ContentIndices, no more integer ids hardcoded, common refactor
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
#include "Content.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include "../voxels/Block.h"
|
||||
|
||||
using std::vector;
|
||||
using std::string;
|
||||
using std::unordered_map;
|
||||
|
||||
void ContentBuilder::add(Block* def) {
|
||||
if (blockDefs.find(def->name) != blockDefs.end()) {
|
||||
throw std::runtime_error("block name duplicate: "+def->name);
|
||||
}
|
||||
blockDefs[def->name] = def;
|
||||
blockIds.push_back(def->name);
|
||||
}
|
||||
|
||||
Content* ContentBuilder::build() {
|
||||
vector<Block*> blockDefsIndices;
|
||||
for (const string& name : blockIds) {
|
||||
Block* def = blockDefs[name];
|
||||
def->id = blockDefsIndices.size();
|
||||
blockDefsIndices.push_back(def);
|
||||
}
|
||||
ContentIndices* indices = new ContentIndices(blockDefsIndices);
|
||||
return new Content(indices, blockDefs);
|
||||
}
|
||||
|
||||
ContentIndices::ContentIndices(vector<Block*> blockDefs)
|
||||
: blockDefs(blockDefs) {
|
||||
}
|
||||
|
||||
Content::Content(ContentIndices* indices,
|
||||
unordered_map<string, Block*> blockDefs)
|
||||
: blockDefs(blockDefs),
|
||||
indices(indices) {
|
||||
}
|
||||
|
||||
Content::~Content() {
|
||||
delete indices;
|
||||
}
|
||||
|
||||
Block* Content::require(std::string id) const {
|
||||
auto found = blockDefs.find(id);
|
||||
if (found == blockDefs.end()) {
|
||||
throw std::runtime_error("missing block "+id);
|
||||
}
|
||||
return found->second;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
#ifndef CONTENT_CONTENT_H_
|
||||
#define CONTENT_CONTENT_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include "../typedefs.h"
|
||||
|
||||
class Block;
|
||||
class Content;
|
||||
|
||||
class ContentBuilder {
|
||||
std::unordered_map<std::string, Block*> blockDefs;
|
||||
std::vector<std::string> blockIds;
|
||||
public:
|
||||
void add(Block* def);
|
||||
|
||||
Content* build();
|
||||
};
|
||||
|
||||
/* Runtime defs cache: indices */
|
||||
class ContentIndices {
|
||||
// blockDefs must be a plain vector with block id used as index
|
||||
std::vector<Block*> blockDefs;
|
||||
public:
|
||||
ContentIndices(std::vector<Block*> blockDefs);
|
||||
|
||||
inline Block* getBlockDef(blockid_t id) const {
|
||||
if (id >= blockDefs.size())
|
||||
return nullptr;
|
||||
return blockDefs[id];
|
||||
}
|
||||
|
||||
inline size_t countBlockDefs() const {
|
||||
return blockDefs.size();
|
||||
}
|
||||
|
||||
// use this for critical spots to prevent range check overhead
|
||||
const Block* const* getBlockDefs() const {
|
||||
return blockDefs.data();
|
||||
}
|
||||
};
|
||||
|
||||
/* Content is a definitions repository */
|
||||
class Content {
|
||||
std::unordered_map<std::string, Block*> blockDefs;
|
||||
public:
|
||||
ContentIndices* const indices;
|
||||
|
||||
Content(ContentIndices* indices,
|
||||
std::unordered_map<std::string, Block*> blockDefs);
|
||||
~Content();
|
||||
|
||||
Block* require(std::string id) const;
|
||||
};
|
||||
|
||||
#endif // CONTENT_CONTENT_H_
|
||||
Reference in New Issue
Block a user