From e700182f44e41658cead71993bfe233951b0aa5c Mon Sep 17 00:00:00 2001 From: Desour Date: Sun, 11 Jun 2023 22:55:36 +0200 Subject: [PATCH] Replace Optional with std::optional --- src/chat.cpp | 2 +- src/chat.h | 4 +- src/client/camera.cpp | 2 +- src/client/camera.h | 8 +- src/gui/guiFormSpecMenu.cpp | 2 +- src/gui/guiFormSpecMenu.h | 16 +-- src/network/serverpackethandler.cpp | 10 +- src/object_properties.cpp | 2 +- src/object_properties.h | 4 +- src/script/common/c_content.cpp | 2 +- src/script/cpp_api/s_item.cpp | 12 +-- src/script/cpp_api/s_item.h | 10 +- src/script/lua_api/l_env.cpp | 2 +- src/script/lua_api/l_object.cpp | 2 +- src/util/Optional.h | 159 ---------------------------- 15 files changed, 39 insertions(+), 198 deletions(-) delete mode 100644 src/util/Optional.h diff --git a/src/chat.cpp b/src/chat.cpp index ddce6cd75..7bbaa8016 100644 --- a/src/chat.cpp +++ b/src/chat.cpp @@ -509,7 +509,7 @@ void ChatPrompt::addToHistory(const std::wstring &line) auto entry = m_history.begin() + m_history_index; if (entry->saved && entry->line == line) { entry->line = *entry->saved; - entry->saved = nullopt; + entry->saved = std::nullopt; // Remove potential duplicates auto dup_before = std::find(m_history.begin(), entry, *entry); if (dup_before != entry) diff --git a/src/chat.h b/src/chat.h index 280701d47..049cabcd1 100644 --- a/src/chat.h +++ b/src/chat.h @@ -22,10 +22,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include #include +#include #include "irrlichttypes.h" #include "util/enriched_string.h" -#include "util/Optional.h" #include "settings.h" // Chat console related classes @@ -247,7 +247,7 @@ private: struct HistoryEntry { std::wstring line; // If line is edited, saved holds the unedited version. - Optional saved; + std::optional saved; HistoryEntry(const std::wstring &line): line(line) {} diff --git a/src/client/camera.cpp b/src/client/camera.cpp index dce94495b..2d8648f52 100644 --- a/src/client/camera.cpp +++ b/src/client/camera.cpp @@ -698,7 +698,7 @@ void Camera::drawNametags() Nametag *Camera::addNametag(scene::ISceneNode *parent_node, const std::string &text, video::SColor textcolor, - Optional bgcolor, const v3f &pos) + std::optional bgcolor, const v3f &pos) { Nametag *nametag = new Nametag(parent_node, text, textcolor, bgcolor, pos); m_nametags.push_back(nametag); diff --git a/src/client/camera.h b/src/client/camera.h index cd6c89a96..1d794d166 100644 --- a/src/client/camera.h +++ b/src/client/camera.h @@ -28,7 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include #include -#include "util/Optional.h" +#include class LocalPlayer; struct MapDrawControl; @@ -41,13 +41,13 @@ struct Nametag scene::ISceneNode *parent_node; std::string text; video::SColor textcolor; - Optional bgcolor; + std::optional bgcolor; v3f pos; Nametag(scene::ISceneNode *a_parent_node, const std::string &text, const video::SColor &textcolor, - const Optional &bgcolor, + const std::optional &bgcolor, const v3f &pos): parent_node(a_parent_node), text(text), @@ -201,7 +201,7 @@ public: Nametag *addNametag(scene::ISceneNode *parent_node, const std::string &text, video::SColor textcolor, - Optional bgcolor, const v3f &pos); + std::optional bgcolor, const v3f &pos); void removeNametag(Nametag *nametag); diff --git a/src/gui/guiFormSpecMenu.cpp b/src/gui/guiFormSpecMenu.cpp index 826c0dfd3..764e068ca 100644 --- a/src/gui/guiFormSpecMenu.cpp +++ b/src/gui/guiFormSpecMenu.cpp @@ -3053,7 +3053,7 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize) } } else { // Don't keep old focus value - m_focused_element = nullopt; + m_focused_element = std::nullopt; } removeAll(); diff --git a/src/gui/guiFormSpecMenu.h b/src/gui/guiFormSpecMenu.h index d11651266..4b22602cc 100644 --- a/src/gui/guiFormSpecMenu.h +++ b/src/gui/guiFormSpecMenu.h @@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #pragma once +#include #include #include #include @@ -33,7 +34,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "guiTable.h" #include "network/networkprotocol.h" #include "client/joystick_controller.h" -#include "util/Optional.h" #include "util/string.h" #include "util/enriched_string.h" #include "StyleSpec.h" @@ -374,13 +374,13 @@ protected: video::SColor m_default_tooltip_color; private: - IFormSource *m_form_src; - TextDest *m_text_dst; - std::string m_last_formname; - u16 m_formspec_version = 1; - Optional m_focused_element = nullopt; - JoystickController *m_joystick; - bool m_show_debug = false; + IFormSource *m_form_src; + TextDest *m_text_dst; + std::string m_last_formname; + u16 m_formspec_version = 1; + std::optional m_focused_element = std::nullopt; + JoystickController *m_joystick; + bool m_show_debug = false; struct parserData { bool explicit_size; diff --git a/src/network/serverpackethandler.cpp b/src/network/serverpackethandler.cpp index b4b8b8a3e..c78967874 100644 --- a/src/network/serverpackethandler.cpp +++ b/src/network/serverpackethandler.cpp @@ -911,8 +911,8 @@ bool Server::checkInteractDistance(RemotePlayer *player, const f32 d, const std: return true; } -// Tiny helper to retrieve the selected item into an Optional -static inline void getWieldedItem(const PlayerSAO *playersao, Optional &ret) +// Tiny helper to retrieve the selected item into an std::optional +static inline void getWieldedItem(const PlayerSAO *playersao, std::optional &ret) { ret = ItemStack(); playersao->getWieldedItem(&(*ret)); @@ -1226,7 +1226,7 @@ void Server::handleCommand_Interact(NetworkPacket *pkt) // Place block or right-click object case INTERACT_PLACE: { - Optional selected_item; + std::optional selected_item; getWieldedItem(playersao, selected_item); // Reset build time counter @@ -1285,7 +1285,7 @@ void Server::handleCommand_Interact(NetworkPacket *pkt) } // action == INTERACT_PLACE case INTERACT_USE: { - Optional selected_item; + std::optional selected_item; getWieldedItem(playersao, selected_item); actionstream << player->getName() << " uses " << selected_item->name @@ -1302,7 +1302,7 @@ void Server::handleCommand_Interact(NetworkPacket *pkt) // Rightclick air case INTERACT_ACTIVATE: { - Optional selected_item; + std::optional selected_item; getWieldedItem(playersao, selected_item); actionstream << player->getName() << " activates " diff --git a/src/object_properties.cpp b/src/object_properties.cpp index e4c656946..b6fddd15d 100644 --- a/src/object_properties.cpp +++ b/src/object_properties.cpp @@ -237,7 +237,7 @@ void ObjectProperties::deSerialize(std::istream &is) if (bgcolor != NULL_BGCOLOR) nametag_bgcolor = bgcolor; else - nametag_bgcolor = nullopt; + nametag_bgcolor = std::nullopt; tmp = readU8(is); if (is.eof()) diff --git a/src/object_properties.h b/src/object_properties.h index f4c425d7b..44d2f29b2 100644 --- a/src/object_properties.h +++ b/src/object_properties.h @@ -19,12 +19,12 @@ with this program; if not, write to the Free Software Foundation, Inc., #pragma once +#include #include #include "irrlichttypes_bloated.h" #include #include #include -#include "util/Optional.h" struct ObjectProperties { @@ -55,7 +55,7 @@ struct ObjectProperties s8 glow = 0; std::string nametag = ""; video::SColor nametag_color = video::SColor(255, 255, 255, 255); - Optional nametag_bgcolor = nullopt; + std::optional nametag_bgcolor = std::nullopt; f32 automatic_face_movement_max_rotation_per_sec = -1.0f; std::string infotext; //! For dropped items, this contains item information. diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 4be6457d8..9a9132c09 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -337,7 +337,7 @@ void read_object_properties(lua_State *L, int index, if (read_color(L, -1, &color)) prop->nametag_bgcolor = color; } else { - prop->nametag_bgcolor = nullopt; + prop->nametag_bgcolor = std::nullopt; } } lua_pop(L, 1); diff --git a/src/script/cpp_api/s_item.cpp b/src/script/cpp_api/s_item.cpp index b1916070e..2f7436bcf 100644 --- a/src/script/cpp_api/s_item.cpp +++ b/src/script/cpp_api/s_item.cpp @@ -59,7 +59,7 @@ bool ScriptApiItem::item_OnDrop(ItemStack &item, return true; } -bool ScriptApiItem::item_OnPlace(Optional &ret_item, +bool ScriptApiItem::item_OnPlace(std::optional &ret_item, ServerActiveObject *placer, const PointedThing &pointed) { SCRIPTAPI_PRECHECKHEADER @@ -88,13 +88,13 @@ bool ScriptApiItem::item_OnPlace(Optional &ret_item, throw WRAP_LUAERROR(e, "item=" + item.name); } } else { - ret_item = nullopt; + ret_item = std::nullopt; } lua_pop(L, 2); // Pop item and error handler return true; } -bool ScriptApiItem::item_OnUse(Optional &ret_item, +bool ScriptApiItem::item_OnUse(std::optional &ret_item, ServerActiveObject *user, const PointedThing &pointed) { SCRIPTAPI_PRECHECKHEADER @@ -118,13 +118,13 @@ bool ScriptApiItem::item_OnUse(Optional &ret_item, throw WRAP_LUAERROR(e, "item=" + item.name); } } else { - ret_item = nullopt; + ret_item = std::nullopt; } lua_pop(L, 2); // Pop item and error handler return true; } -bool ScriptApiItem::item_OnSecondaryUse(Optional &ret_item, +bool ScriptApiItem::item_OnSecondaryUse(std::optional &ret_item, ServerActiveObject *user, const PointedThing &pointed) { SCRIPTAPI_PRECHECKHEADER @@ -146,7 +146,7 @@ bool ScriptApiItem::item_OnSecondaryUse(Optional &ret_item, throw WRAP_LUAERROR(e, "item=" + item.name); } } else { - ret_item = nullopt; + ret_item = std::nullopt; } lua_pop(L, 2); // Pop item and error handler return true; diff --git a/src/script/cpp_api/s_item.h b/src/script/cpp_api/s_item.h index 5015d8bd4..0772cebdf 100644 --- a/src/script/cpp_api/s_item.h +++ b/src/script/cpp_api/s_item.h @@ -21,7 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "cpp_api/s_base.h" #include "irr_v3d.h" -#include "util/Optional.h" +#include struct PointedThing; struct ItemStack; @@ -37,7 +37,7 @@ class ScriptApiItem { public: /* - * Functions with Optional are for callbacks where Lua may + * Functions with std::optional are for callbacks where Lua may * want to prevent the engine from modifying the inventory after it's done. * This has a longer backstory where on_use may need to empty the player's * inventory without the engine interfering (see issue #6546). @@ -45,11 +45,11 @@ public: bool item_OnDrop(ItemStack &item, ServerActiveObject *dropper, v3f pos); - bool item_OnPlace(Optional &item, + bool item_OnPlace(std::optional &item, ServerActiveObject *placer, const PointedThing &pointed); - bool item_OnUse(Optional &item, + bool item_OnUse(std::optional &item, ServerActiveObject *user, const PointedThing &pointed); - bool item_OnSecondaryUse(Optional &item, + bool item_OnSecondaryUse(std::optional &item, ServerActiveObject *user, const PointedThing &pointed); bool item_OnCraft(ItemStack &item, ServerActiveObject *user, const InventoryList *old_craft_grid, const InventoryLocation &craft_inv); diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 4a9c9b75e..3b9a20a76 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -446,7 +446,7 @@ int ModApiEnvMod::l_place_node(lua_State *L) return 1; } // Create item to place - Optional item = ItemStack(ndef->get(n).name, 1, 0, idef); + std::optional item = ItemStack(ndef->get(n).name, 1, 0, idef); // Make pointed position PointedThing pointed; pointed.type = POINTEDTHING_NODE; diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index 542e1c89f..7bb467c2e 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -716,7 +716,7 @@ int ObjectRef::l_set_nametag_attributes(lua_State *L) if (read_color(L, -1, &color)) prop->nametag_bgcolor = color; } else { - prop->nametag_bgcolor = nullopt; + prop->nametag_bgcolor = std::nullopt; } } lua_pop(L, 1); diff --git a/src/util/Optional.h b/src/util/Optional.h deleted file mode 100644 index c060efeb5..000000000 --- a/src/util/Optional.h +++ /dev/null @@ -1,159 +0,0 @@ -/* -Minetest -Copyright (C) 2021 rubenwardy - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Lesser General Public License for more details. - -You should have received a copy of the GNU Lesser General Public License along -with this program; if not, write to the Free Software Foundation, Inc., -51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -*/ - -#pragma once - -#include -#include "debug.h" - -struct nullopt_t -{ -}; -constexpr nullopt_t nullopt{}; - -/** - * An implementation of optional for C++11, which aims to be - * compatible with a subset of std::optional features. - * - * Unfortunately, Minetest doesn't use C++17 yet. - * - * @tparam T The type to be stored - */ -template -class Optional -{ - bool m_has_value = false; - T m_value; - -public: - Optional() noexcept {} - Optional(nullopt_t) noexcept {} - - Optional(const T &value) noexcept : m_has_value(true), m_value(value) {} - Optional(T &&value) noexcept : m_has_value(true), m_value(std::move(value)) {} - - Optional(const Optional &other) noexcept : - m_has_value(other.m_has_value), m_value(other.m_value) - {} - Optional(Optional &&other) noexcept : - m_has_value(other.m_has_value), m_value(std::move(other.m_value)) - { - other.m_has_value = false; - } - - Optional &operator=(nullopt_t) noexcept { m_has_value = false; return *this; } - - Optional &operator=(const Optional &other) noexcept - { - if (&other == this) - return *this; - m_has_value = other.m_has_value; - m_value = other.m_value; - return *this; - } - - Optional &operator=(Optional &&other) noexcept - { - if (&other == this) - return *this; - m_has_value = other.m_has_value; - m_value = std::move(other.m_value); - other.m_has_value = false; - return *this; - } - - T &value() - { - FATAL_ERROR_IF(!m_has_value, "optional doesn't have value"); - return m_value; - } - - const T &value() const - { - FATAL_ERROR_IF(!m_has_value, "optional doesn't have value"); - return m_value; - } - - const T &value_or(const T &def) const { return m_has_value ? m_value : def; } - - // Unchecked access consistent with std::optional - T* operator->() { return &m_value; } - const T* operator->() const { return &m_value; } - - T& operator*() { return m_value; } - const T& operator*() const { return m_value; } - - bool has_value() const noexcept { return m_has_value; } - - explicit operator bool() const { return m_has_value; } -}; - -template -constexpr bool operator==(const Optional &opt, nullopt_t) -{ - return !opt.has_value(); -} -template -constexpr bool operator==(nullopt_t, const Optional &opt) -{ - return !opt.has_value(); -} -template -constexpr bool operator!=(const Optional &opt, nullopt_t) -{ - return opt.has_value(); -} -template -constexpr bool operator!=(nullopt_t, const Optional &opt) -{ - return opt.has_value(); -} - -template -constexpr bool operator==(const Optional &opt, const U &value) -{ - return opt.has_value() && *opt == value; -} -template -constexpr bool operator==(const T &value, const Optional &opt) -{ - return opt.has_value() && value == *opt; -} -template -constexpr bool operator!=(const Optional &opt, const U &value) -{ - return !opt.has_value() || *opt != value; -} -template -constexpr bool operator!=(const T &value, const Optional &opt) -{ - return !opt.has_value() || value != *opt; -} - - -template -constexpr bool operator==(const Optional &lhs, const Optional &rhs) -{ - return lhs.has_value() ? *lhs == rhs : nullopt == rhs; -} -template -constexpr bool operator!=(const Optional &lhs, const Optional &rhs) -{ - return lhs.has_value() ? *lhs != rhs : nullopt != rhs; -}