Replace Optional with std::optional

This commit is contained in:
Desour 2023-06-11 22:55:36 +02:00 committed by sfan5
parent 34ad551efc
commit e700182f44
15 changed files with 39 additions and 198 deletions

@ -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)

@ -22,10 +22,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <string>
#include <vector>
#include <list>
#include <optional>
#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<std::wstring> saved;
std::optional<std::wstring> saved;
HistoryEntry(const std::wstring &line): line(line) {}

@ -698,7 +698,7 @@ void Camera::drawNametags()
Nametag *Camera::addNametag(scene::ISceneNode *parent_node,
const std::string &text, video::SColor textcolor,
Optional<video::SColor> bgcolor, const v3f &pos)
std::optional<video::SColor> bgcolor, const v3f &pos)
{
Nametag *nametag = new Nametag(parent_node, text, textcolor, bgcolor, pos);
m_nametags.push_back(nametag);

@ -28,7 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <plane3d.h>
#include <array>
#include <list>
#include "util/Optional.h"
#include <optional>
class LocalPlayer;
struct MapDrawControl;
@ -41,13 +41,13 @@ struct Nametag
scene::ISceneNode *parent_node;
std::string text;
video::SColor textcolor;
Optional<video::SColor> bgcolor;
std::optional<video::SColor> bgcolor;
v3f pos;
Nametag(scene::ISceneNode *a_parent_node,
const std::string &text,
const video::SColor &textcolor,
const Optional<video::SColor> &bgcolor,
const std::optional<video::SColor> &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<video::SColor> bgcolor, const v3f &pos);
std::optional<video::SColor> bgcolor, const v3f &pos);
void removeNametag(Nametag *nametag);

@ -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();

@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
#include <optional>
#include <utility>
#include <stack>
#include <unordered_set>
@ -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"
@ -378,7 +378,7 @@ private:
TextDest *m_text_dst;
std::string m_last_formname;
u16 m_formspec_version = 1;
Optional<std::string> m_focused_element = nullopt;
std::optional<std::string> m_focused_element = std::nullopt;
JoystickController *m_joystick;
bool m_show_debug = false;

@ -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<ItemStack> &ret)
// Tiny helper to retrieve the selected item into an std::optional
static inline void getWieldedItem(const PlayerSAO *playersao, std::optional<ItemStack> &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<ItemStack> selected_item;
std::optional<ItemStack> 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<ItemStack> selected_item;
std::optional<ItemStack> 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<ItemStack> selected_item;
std::optional<ItemStack> selected_item;
getWieldedItem(playersao, selected_item);
actionstream << player->getName() << " activates "

@ -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())

@ -19,12 +19,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
#include <optional>
#include <string>
#include "irrlichttypes_bloated.h"
#include <iostream>
#include <map>
#include <vector>
#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<video::SColor> nametag_bgcolor = nullopt;
std::optional<video::SColor> nametag_bgcolor = std::nullopt;
f32 automatic_face_movement_max_rotation_per_sec = -1.0f;
std::string infotext;
//! For dropped items, this contains item information.

@ -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);

@ -59,7 +59,7 @@ bool ScriptApiItem::item_OnDrop(ItemStack &item,
return true;
}
bool ScriptApiItem::item_OnPlace(Optional<ItemStack> &ret_item,
bool ScriptApiItem::item_OnPlace(std::optional<ItemStack> &ret_item,
ServerActiveObject *placer, const PointedThing &pointed)
{
SCRIPTAPI_PRECHECKHEADER
@ -88,13 +88,13 @@ bool ScriptApiItem::item_OnPlace(Optional<ItemStack> &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<ItemStack> &ret_item,
bool ScriptApiItem::item_OnUse(std::optional<ItemStack> &ret_item,
ServerActiveObject *user, const PointedThing &pointed)
{
SCRIPTAPI_PRECHECKHEADER
@ -118,13 +118,13 @@ bool ScriptApiItem::item_OnUse(Optional<ItemStack> &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<ItemStack> &ret_item,
bool ScriptApiItem::item_OnSecondaryUse(std::optional<ItemStack> &ret_item,
ServerActiveObject *user, const PointedThing &pointed)
{
SCRIPTAPI_PRECHECKHEADER
@ -146,7 +146,7 @@ bool ScriptApiItem::item_OnSecondaryUse(Optional<ItemStack> &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;

@ -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 <optional>
struct PointedThing;
struct ItemStack;
@ -37,7 +37,7 @@ class ScriptApiItem
{
public:
/*
* Functions with Optional<ItemStack> are for callbacks where Lua may
* Functions with std::optional<ItemStack> 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<ItemStack> &item,
bool item_OnPlace(std::optional<ItemStack> &item,
ServerActiveObject *placer, const PointedThing &pointed);
bool item_OnUse(Optional<ItemStack> &item,
bool item_OnUse(std::optional<ItemStack> &item,
ServerActiveObject *user, const PointedThing &pointed);
bool item_OnSecondaryUse(Optional<ItemStack> &item,
bool item_OnSecondaryUse(std::optional<ItemStack> &item,
ServerActiveObject *user, const PointedThing &pointed);
bool item_OnCraft(ItemStack &item, ServerActiveObject *user,
const InventoryList *old_craft_grid, const InventoryLocation &craft_inv);

@ -446,7 +446,7 @@ int ModApiEnvMod::l_place_node(lua_State *L)
return 1;
}
// Create item to place
Optional<ItemStack> item = ItemStack(ndef->get(n).name, 1, 0, idef);
std::optional<ItemStack> item = ItemStack(ndef->get(n).name, 1, 0, idef);
// Make pointed position
PointedThing pointed;
pointed.type = POINTEDTHING_NODE;

@ -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);

@ -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 <utility>
#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 <typename T>
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<T> &other) noexcept :
m_has_value(other.m_has_value), m_value(other.m_value)
{}
Optional(Optional<T> &&other) noexcept :
m_has_value(other.m_has_value), m_value(std::move(other.m_value))
{
other.m_has_value = false;
}
Optional<T> &operator=(nullopt_t) noexcept { m_has_value = false; return *this; }
Optional<T> &operator=(const Optional<T> &other) noexcept
{
if (&other == this)
return *this;
m_has_value = other.m_has_value;
m_value = other.m_value;
return *this;
}
Optional<T> &operator=(Optional<T> &&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 <typename T>
constexpr bool operator==(const Optional<T> &opt, nullopt_t)
{
return !opt.has_value();
}
template <typename T>
constexpr bool operator==(nullopt_t, const Optional<T> &opt)
{
return !opt.has_value();
}
template <typename T>
constexpr bool operator!=(const Optional<T> &opt, nullopt_t)
{
return opt.has_value();
}
template <typename T>
constexpr bool operator!=(nullopt_t, const Optional<T> &opt)
{
return opt.has_value();
}
template <typename T, typename U>
constexpr bool operator==(const Optional<T> &opt, const U &value)
{
return opt.has_value() && *opt == value;
}
template <typename T, typename U>
constexpr bool operator==(const T &value, const Optional<U> &opt)
{
return opt.has_value() && value == *opt;
}
template <typename T, typename U>
constexpr bool operator!=(const Optional<T> &opt, const U &value)
{
return !opt.has_value() || *opt != value;
}
template <typename T, typename U>
constexpr bool operator!=(const T &value, const Optional<U> &opt)
{
return !opt.has_value() || value != *opt;
}
template <typename T, typename U>
constexpr bool operator==(const Optional<T> &lhs, const Optional<U> &rhs)
{
return lhs.has_value() ? *lhs == rhs : nullopt == rhs;
}
template <typename T, typename U>
constexpr bool operator!=(const Optional<T> &lhs, const Optional<U> &rhs)
{
return lhs.has_value() ? *lhs != rhs : nullopt != rhs;
}