Optimize parameter passing to avoid unnecessary copying

This commit is contained in:
Pugemon
2024-06-07 04:00:38 +03:00
parent 46ca1bb04a
commit f25a425cb9
123 changed files with 578 additions and 522 deletions
+6 -4
View File
@@ -1,5 +1,7 @@
#include "UiDocument.hpp"
#include <utility>
#include "../files/files.hpp"
#include "../graphics/ui/elements/UINode.hpp"
#include "../graphics/ui/elements/InventoryView.hpp"
@@ -9,9 +11,9 @@
UiDocument::UiDocument(
std::string id,
uidocscript script,
std::shared_ptr<gui::UINode> root,
const std::shared_ptr<gui::UINode>& root,
scriptenv env
) : id(id), script(script), root(root), env(env) {
) : id(std::move(id)), script(script), root(root), env(std::move(env)) {
gui::UINode::getIndices(root, map);
}
@@ -51,7 +53,7 @@ scriptenv UiDocument::getEnvironment() const {
return env;
}
std::unique_ptr<UiDocument> UiDocument::read(scriptenv penv, std::string name, fs::path file) {
std::unique_ptr<UiDocument> UiDocument::read(const scriptenv& penv, const std::string& name, const fs::path& file) {
const std::string text = files::read_string(file);
auto xmldoc = xml::parse(file.u8string(), text);
@@ -72,7 +74,7 @@ std::unique_ptr<UiDocument> UiDocument::read(scriptenv penv, std::string name, f
return std::make_unique<UiDocument>(name, script, view, env);
}
std::shared_ptr<gui::UINode> UiDocument::readElement(fs::path file) {
std::shared_ptr<gui::UINode> UiDocument::readElement(const fs::path& file) {
auto document = read(nullptr, file.filename().u8string(), file);
return document->getRoot();
}
+3 -3
View File
@@ -32,7 +32,7 @@ public:
UiDocument(
std::string id,
uidocscript script,
std::shared_ptr<gui::UINode> root,
const std::shared_ptr<gui::UINode> &root,
scriptenv env
);
@@ -46,8 +46,8 @@ public:
const uidocscript& getScript() const;
scriptenv getEnvironment() const;
static std::unique_ptr<UiDocument> read(scriptenv parent_env, std::string name, fs::path file);
static std::shared_ptr<gui::UINode> readElement(fs::path file);
static std::unique_ptr<UiDocument> read(const scriptenv& parent_env, const std::string& name, const fs::path& file);
static std::shared_ptr<gui::UINode> readElement(const fs::path& file);
};
#endif // FRONTEND_UI_DOCUMENT_HPP_
+8 -7
View File
@@ -21,12 +21,13 @@
#include <memory>
#include <sstream>
#include <bitset>
#include <utility>
using namespace gui;
static std::shared_ptr<Label> create_label(wstringsupplier supplier) {
auto label = std::make_shared<Label>(L"-");
label->textSupplier(supplier);
label->textSupplier(std::move(supplier));
return label;
}
@@ -75,13 +76,13 @@ std::shared_ptr<UINode> create_debug_panel(
panel->add(create_label([=](){
std::wstringstream stream;
stream << "r:" << player->selectedVoxel.state.rotation << " s:"
<< player->selectedVoxel.state.segment << " u:"
<< std::bitset<8>(player->selectedVoxel.state.userbits);
<< player->selectedVoxel.state.segment << " u:"
<< std::bitset<8>(player->selectedVoxel.state.userbits);
if (player->selectedVoxel.id == BLOCK_VOID) {
return std::wstring {L"block: -"};
} else {
return L"block: "+std::to_wstring(player->selectedVoxel.id)+
L" "+stream.str();
L" "+stream.str();
}
}));
panel->add(create_label([=](){
@@ -114,7 +115,7 @@ std::shared_ptr<UINode> create_debug_panel(
Hitbox* hitbox = player->hitbox.get();
return util::to_wstring(hitbox->position[ax], 2);
});
box->setTextConsumer([=](std::wstring text) {
box->setTextConsumer([=](const std::wstring& text) {
try {
glm::vec3 position = player->hitbox->position;
position[ax] = std::stoi(text);
@@ -137,8 +138,8 @@ std::shared_ptr<UINode> create_debug_panel(
timeutil::from_value(level->getWorld()->daytime, hour, minute, second);
std::wstring timeString =
util::lfill(std::to_wstring(hour), 2, L'0') + L":" +
util::lfill(std::to_wstring(minute), 2, L'0');
util::lfill(std::to_wstring(hour), 2, L'0') + L":" +
util::lfill(std::to_wstring(minute), 2, L'0');
return L"time: "+timeString;
}));
{
+4 -3
View File
@@ -48,6 +48,7 @@
#include <memory>
#include <stdexcept>
#include <string>
#include <utility>
using namespace gui;
@@ -63,7 +64,7 @@ HudElement::HudElement(
UiDocument* document,
std::shared_ptr<UINode> node,
bool debug
) : mode(mode), document(document), node(node), debug(debug) {
) : mode(mode), document(document), node(std::move(node)), debug(debug) {
}
void HudElement::update(bool pause, bool inventoryOpen, bool debugMode) {
@@ -398,7 +399,7 @@ void Hud::closeInventory() {
cleanup();
}
void Hud::add(HudElement element) {
void Hud::add(const HudElement& element) {
using namespace dynamic;
gui->add(element.getNode());
@@ -435,7 +436,7 @@ void Hud::onRemove(const HudElement& element) {
gui->remove(element.getNode());
}
void Hud::remove(std::shared_ptr<UINode> node) {
void Hud::remove(const std::shared_ptr<UINode>& node) {
for (auto& element : elements) {
if (element.getNode() == node) {
element.setRemoved();
+2 -2
View File
@@ -161,9 +161,9 @@ public:
/// @param doc element layout
void openPermanent(UiDocument* doc);
void add(HudElement element);
void add(const HudElement& element);
void onRemove(const HudElement& element);
void remove(std::shared_ptr<gui::UINode> node);
void remove(const std::shared_ptr<gui::UINode>& node);
Player* getPlayer() const;
+4 -2
View File
@@ -1,5 +1,7 @@
#include "locale.hpp"
#include <utility>
#include "../coders/json.hpp"
#include "../coders/commons.hpp"
#include "../content/ContentPack.hpp"
@@ -16,7 +18,7 @@ using namespace std::literals;
std::unique_ptr<langs::Lang> langs::current;
std::unordered_map<std::string, langs::LocaleInfo> langs::locales_info;
langs::Lang::Lang(std::string locale) : locale(locale) {
langs::Lang::Lang(std::string locale) : locale(std::move(locale)) {
}
const std::wstring& langs::Lang::get(const std::wstring& key) const {
@@ -50,7 +52,7 @@ public:
Reader(std::string_view file, std::string_view source) : BasicParser(file, source) {
}
void read(langs::Lang& lang, std::string prefix) {
void read(langs::Lang& lang, const std::string &prefix) {
skipWhitespace();
while (hasNext()) {
std::string key = parseString('=', true);
+1 -1
View File
@@ -88,7 +88,7 @@ UiDocument* menus::show(Engine* engine, const std::string& name, std::vector<dyn
return document;
}
void menus::show_process_panel(Engine* engine, std::shared_ptr<Task> task, std::wstring text) {
void menus::show_process_panel(Engine* engine, const std::shared_ptr<Task>& task, const std::wstring& text) {
using namespace dynamic;
uint initialWork = task->getWorkTotal();
+1 -1
View File
@@ -25,7 +25,7 @@ namespace menus {
std::vector<dynamic::Value> args
);
void show_process_panel(Engine* engine, std::shared_ptr<Task> task, std::wstring text=L"");
void show_process_panel(Engine* engine, const std::shared_ptr<Task>& task, const std::wstring& text=L"");
}
#endif // FRONTEND_MENU_MENU_HPP_