fix: function returns by const value

This commit is contained in:
MihailRis 2024-06-07 15:17:32 +03:00
parent d292ef130c
commit a5dc187481
14 changed files with 41 additions and 28 deletions

View File

@ -42,10 +42,10 @@ const std::string& GLSLExtension::getHeader(const std::string& name) const {
return found->second; return found->second;
} }
const std::string GLSLExtension::getDefine(const std::string& name) const { const std::string& GLSLExtension::getDefine(const std::string& name) const {
auto found = defines.find(name); auto found = defines.find(name);
if (found == defines.end()) { if (found == defines.end()) {
return ""; throw std::runtime_error("name '"+name+"' is not defined");
} }
return found->second; return found->second;
} }
@ -84,7 +84,7 @@ inline void source_line(std::stringstream& ss, uint linenum) {
ss << "#line " << linenum << "\n"; ss << "#line " << linenum << "\n";
} }
const std::string GLSLExtension::process(const fs::path& file, const std::string& source) { std::string GLSLExtension::process(const fs::path& file, const std::string& source) {
std::stringstream ss; std::stringstream ss;
size_t pos = 0; size_t pos = 0;
uint linenum = 1; uint linenum = 1;

View File

@ -24,12 +24,12 @@ public:
void addHeader(const std::string& name, std::string source); void addHeader(const std::string& name, std::string source);
const std::string& getHeader(const std::string& name) const; const std::string& getHeader(const std::string& name) const;
const std::string getDefine(const std::string& name) const; const std::string& getDefine(const std::string& name) const;
bool hasHeader(const std::string& name) const; bool hasHeader(const std::string& name) const;
bool hasDefine(const std::string& name) const; bool hasDefine(const std::string& name) const;
const std::string process( std::string process(
const std::filesystem::path& file, const std::filesystem::path& file,
const std::string& source const std::string& source
); );

View File

@ -120,7 +120,7 @@ const std::string& Node::getTag() const {
return tag; return tag;
} }
const xmlattribute Node::attr(const std::string& name) const { const xmlattribute& Node::attr(const std::string& name) const {
auto found = attrs.find(name); auto found = attrs.find(name);
if (found == attrs.end()) { if (found == attrs.end()) {
throw std::runtime_error("element <"+tag+" ...> missing attribute "+name); throw std::runtime_error("element <"+tag+" ...> missing attribute "+name);
@ -128,7 +128,7 @@ const xmlattribute Node::attr(const std::string& name) const {
return found->second; return found->second;
} }
const xmlattribute Node::attr(const std::string& name, const std::string& def) const { xmlattribute Node::attr(const std::string& name, const std::string& def) const {
auto found = attrs.find(name); auto found = attrs.find(name);
if (found == attrs.end()) { if (found == attrs.end()) {
return Attribute(name, def); return Attribute(name, def);

View File

@ -68,14 +68,14 @@ namespace xml {
/// @param name attribute name /// @param name attribute name
/// @throws std::runtime_error if element has no attribute /// @throws std::runtime_error if element has no attribute
/// @return xmlattribute - {name, value} /// @return xmlattribute - {name, value}
const xmlattribute attr(const std::string& name) const; const xmlattribute& attr(const std::string& name) const;
/// @brief Get attribute by name /// @brief Get attribute by name
/// @param name attribute name /// @param name attribute name
/// @param def default value will be returned wrapped in xmlattribute /// @param def default value will be returned wrapped in xmlattribute
/// if element has no attribute /// if element has no attribute
/// @return xmlattribute - {name, value} or {name, def} if not found*/ /// @return xmlattribute - {name, value} or {name, def} if not found*/
const xmlattribute attr(const std::string& name, const std::string& def) const; xmlattribute attr(const std::string& name, const std::string& def) const;
/// @brief Check if element has attribute /// @brief Check if element has attribute
/// @param name attribute name /// @param name attribute name

View File

@ -33,11 +33,11 @@ const std::string& UiDocument::getId() const {
return id; return id;
} }
const std::shared_ptr<gui::UINode> UiDocument::getRoot() const { std::shared_ptr<gui::UINode> UiDocument::getRoot() const {
return root; return root;
} }
const std::shared_ptr<gui::UINode> UiDocument::get(const std::string& id) const { std::shared_ptr<gui::UINode> UiDocument::get(const std::string& id) const {
auto found = map.find(id); auto found = map.find(id);
if (found == map.end()) { if (found == map.end()) {
return nullptr; return nullptr;

View File

@ -41,8 +41,8 @@ public:
const std::string& getId() const; const std::string& getId() const;
const uinodes_map& getMap() const; const uinodes_map& getMap() const;
uinodes_map& getMapWriteable(); uinodes_map& getMapWriteable();
const std::shared_ptr<gui::UINode> getRoot() const; std::shared_ptr<gui::UINode> getRoot() const;
const std::shared_ptr<gui::UINode> get(const std::string& id) const; std::shared_ptr<gui::UINode> get(const std::string& id) const;
const uidocscript& getScript() const; const uidocscript& getScript() const;
scriptenv getEnvironment() const; scriptenv getEnvironment() const;

View File

@ -114,8 +114,21 @@ SlotView::SlotView(
} }
void SlotView::draw(const DrawContext* pctx, Assets* assets) { void SlotView::draw(const DrawContext* pctx, Assets* assets) {
if (bound == nullptr) if (bound == nullptr) {
return; return;
}
itemid_t itemid = bound->getItemId();
if (itemid != prevItem) {
if (itemid) {
auto def = content->getIndices()->getItemDef(itemid);
tooltip = util::pascal_case(
langs::get(util::str2wstr_utf8(def->caption))
);
} else {
tooltip = L"";
}
}
prevItem = itemid;
const int slotSize = InventoryView::SLOT_SIZE; const int slotSize = InventoryView::SLOT_SIZE;
@ -274,15 +287,12 @@ void SlotView::onFocus(gui::GUI* gui) {
clicked(gui, mousecode::BUTTON_1); clicked(gui, mousecode::BUTTON_1);
} }
const std::wstring SlotView::getTooltip() const { const std::wstring& SlotView::getTooltip() const {
const auto str = UINode::getTooltip(); const auto& str = UINode::getTooltip();
if (!str.empty() || bound->isEmpty()) { if (!str.empty() || tooltip.empty()) {
return str; return str;
} }
auto def = content->getIndices()->getItemDef(bound->getItemId()); return tooltip;
return util::pascal_case(
langs::get(util::str2wstr_utf8(def->caption))
); // TODO: cache
} }
void SlotView::bind( void SlotView::bind(

View File

@ -53,6 +53,9 @@ namespace gui {
int64_t inventoryid = 0; int64_t inventoryid = 0;
ItemStack* bound = nullptr; ItemStack* bound = nullptr;
std::wstring tooltip;
itemid_t prevItem = 0;
public: public:
SlotView(SlotLayout layout); SlotView(SlotLayout layout);
@ -63,7 +66,7 @@ namespace gui {
virtual void clicked(gui::GUI*, mousecode) override; virtual void clicked(gui::GUI*, mousecode) override;
virtual void onFocus(gui::GUI*) override; virtual void onFocus(gui::GUI*) override;
virtual const std::wstring getTooltip() const override; virtual const std::wstring& getTooltip() const override;
void bind( void bind(
int64_t inventoryid, int64_t inventoryid,

View File

@ -138,7 +138,7 @@ void UINode::setTooltip(const std::wstring& text) {
this->tooltip = text; this->tooltip = text;
} }
const std::wstring UINode::getTooltip() const { const std::wstring& UINode::getTooltip() const {
return tooltip; return tooltip;
} }

View File

@ -202,7 +202,7 @@ namespace gui {
virtual bool isResizing() const; virtual bool isResizing() const;
virtual void setTooltip(const std::wstring& text); virtual void setTooltip(const std::wstring& text);
virtual const std::wstring getTooltip() const; virtual const std::wstring& getTooltip() const;
virtual void setTooltipDelay(float delay); virtual void setTooltipDelay(float delay);
virtual float getTooltipDelay() const; virtual float getTooltipDelay() const;

View File

@ -120,7 +120,7 @@ namespace cmd {
); );
Command* get(const std::string& name); Command* get(const std::string& name);
const std::unordered_map<std::string, Command> getCommands() const { const std::unordered_map<std::string, Command>& getCommands() const {
return commands; return commands;
} }
}; };

View File

@ -68,7 +68,7 @@ LuaState::LuaState() {
setglobal(LAMBDAS_TABLE); setglobal(LAMBDAS_TABLE);
} }
const std::string LuaState::envName(int env) { std::string LuaState::envName(int env) {
return "_ENV"+util::mangleid(env); return "_ENV"+util::mangleid(env);
} }

View File

@ -29,7 +29,7 @@ namespace lua {
LuaState(); LuaState();
~LuaState(); ~LuaState();
static const std::string envName(int env); static std::string envName(int env);
void loadbuffer(int env, const std::string& src, const std::string& file); void loadbuffer(int env, const std::string& src, const std::string& file);
int gettop() const; int gettop() const;
int pushivec3(lua_Integer x, lua_Integer y, lua_Integer z); int pushivec3(lua_Integer x, lua_Integer y, lua_Integer z);

View File

@ -157,7 +157,7 @@ struct Binding {
void reset(keycode); void reset(keycode);
void reset(mousecode); void reset(mousecode);
inline const std::string text() const { inline std::string text() const {
switch (type) { switch (type) {
case inputtype::keyboard: { case inputtype::keyboard: {
return input_util::to_string(static_cast<keycode>(code)); return input_util::to_string(static_cast<keycode>(code));