Optimize parameter passing to avoid unnecessary copying
This commit is contained in:
@@ -3,14 +3,14 @@
|
||||
|
||||
using std::vector;
|
||||
|
||||
void LevelEvents::listen(lvl_event_type type, chunk_event_func func) {
|
||||
void LevelEvents::listen(lvl_event_type type, const chunk_event_func& func) {
|
||||
auto& callbacks = chunk_callbacks[type];
|
||||
callbacks.push_back(func);
|
||||
}
|
||||
|
||||
void LevelEvents::trigger(lvl_event_type type, Chunk* chunk) {
|
||||
const auto& callbacks = chunk_callbacks[type];
|
||||
for (chunk_event_func func : callbacks) {
|
||||
for (const chunk_event_func& func : callbacks) {
|
||||
func(type, chunk);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ using chunk_event_func = std::function<void(lvl_event_type, Chunk*)>;
|
||||
class LevelEvents {
|
||||
std::unordered_map<lvl_event_type, std::vector<chunk_event_func>> chunk_callbacks;
|
||||
public:
|
||||
void listen(lvl_event_type type, chunk_event_func func);
|
||||
void listen(lvl_event_type type, const chunk_event_func& func);
|
||||
void trigger(lvl_event_type type, Chunk* chunk);
|
||||
};
|
||||
|
||||
|
||||
+11
-10
@@ -16,23 +16,24 @@
|
||||
|
||||
#include <memory>
|
||||
#include <glm/glm.hpp>
|
||||
#include <utility>
|
||||
|
||||
static debug::Logger logger("world");
|
||||
|
||||
world_load_error::world_load_error(std::string message)
|
||||
world_load_error::world_load_error(const std::string& message)
|
||||
: std::runtime_error(message) {
|
||||
}
|
||||
|
||||
World::World(
|
||||
std::string name,
|
||||
std::string generator,
|
||||
fs::path directory,
|
||||
const fs::path& directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack> packs
|
||||
) : name(name),
|
||||
generator(generator),
|
||||
const std::vector<ContentPack>& packs
|
||||
) : name(std::move(name)),
|
||||
generator(std::move(generator)),
|
||||
seed(seed),
|
||||
settings(settings),
|
||||
content(content),
|
||||
@@ -70,7 +71,7 @@ void World::write(Level* level) {
|
||||
auto playerFile = dynamic::Map();
|
||||
|
||||
auto& players = playerFile.putList("players");
|
||||
for (auto object : level->objects) {
|
||||
for (const auto& object : level->objects) {
|
||||
if (auto player = std::dynamic_pointer_cast<Player>(object)) {
|
||||
players.put(player->serialize());
|
||||
}
|
||||
@@ -79,9 +80,9 @@ void World::write(Level* level) {
|
||||
}
|
||||
|
||||
std::unique_ptr<Level> World::create(
|
||||
std::string name,
|
||||
std::string generator,
|
||||
fs::path directory,
|
||||
const std::string& name,
|
||||
const std::string& generator,
|
||||
const fs::path& directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
@@ -94,7 +95,7 @@ std::unique_ptr<Level> World::create(
|
||||
}
|
||||
|
||||
std::unique_ptr<Level> World::load(
|
||||
fs::path directory,
|
||||
const fs::path& directory,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs
|
||||
|
||||
+7
-7
@@ -22,7 +22,7 @@ namespace fs = std::filesystem;
|
||||
|
||||
class world_load_error : public std::runtime_error {
|
||||
public:
|
||||
world_load_error(std::string message);
|
||||
world_load_error(const std::string &message);
|
||||
};
|
||||
|
||||
/// @brief holds all world data except the level (chunks and objects)
|
||||
@@ -55,11 +55,11 @@ public:
|
||||
World(
|
||||
std::string name,
|
||||
std::string generator,
|
||||
fs::path directory,
|
||||
const fs::path& directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
std::vector<ContentPack> packs
|
||||
const std::vector<ContentPack>& packs
|
||||
);
|
||||
|
||||
~World();
|
||||
@@ -90,9 +90,9 @@ public:
|
||||
/// @param packs vector of all world content-packs
|
||||
/// @return Level instance containing World instance
|
||||
static std::unique_ptr<Level> create(
|
||||
std::string name,
|
||||
std::string generator,
|
||||
fs::path directory,
|
||||
const std::string& name,
|
||||
const std::string& generator,
|
||||
const fs::path& directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
@@ -108,7 +108,7 @@ public:
|
||||
/// @return Level instance containing World instance
|
||||
/// @throws world_load_error on world.json load error
|
||||
static std::unique_ptr<Level> load(
|
||||
fs::path directory,
|
||||
const fs::path& directory,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs
|
||||
|
||||
@@ -16,7 +16,7 @@ std::string WorldGenerators::getDefaultGeneratorID() {
|
||||
return "core:default";
|
||||
}
|
||||
|
||||
std::unique_ptr<WorldGenerator> WorldGenerators::createGenerator(std::string id, const Content* content) {
|
||||
std::unique_ptr<WorldGenerator> WorldGenerators::createGenerator(const std::string& id, const Content* content) {
|
||||
auto found = generators.find(id);
|
||||
if (found == generators.end()) {
|
||||
throw std::runtime_error("unknown generator id: "+id);
|
||||
|
||||
@@ -22,7 +22,7 @@ public:
|
||||
static std::string getDefaultGeneratorID();
|
||||
|
||||
static std::unique_ptr<WorldGenerator> createGenerator(
|
||||
std::string id, const Content* content
|
||||
const std::string &id, const Content *content
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user