forked from Mirrorlandia_minetest/minetest
Replace Optional with std::optional
This commit is contained in:
parent
34ad551efc
commit
e700182f44
@ -509,7 +509,7 @@ void ChatPrompt::addToHistory(const std::wstring &line)
|
|||||||
auto entry = m_history.begin() + m_history_index;
|
auto entry = m_history.begin() + m_history_index;
|
||||||
if (entry->saved && entry->line == line) {
|
if (entry->saved && entry->line == line) {
|
||||||
entry->line = *entry->saved;
|
entry->line = *entry->saved;
|
||||||
entry->saved = nullopt;
|
entry->saved = std::nullopt;
|
||||||
// Remove potential duplicates
|
// Remove potential duplicates
|
||||||
auto dup_before = std::find(m_history.begin(), entry, *entry);
|
auto dup_before = std::find(m_history.begin(), entry, *entry);
|
||||||
if (dup_before != entry)
|
if (dup_before != entry)
|
||||||
|
@ -22,10 +22,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
#include "irrlichttypes.h"
|
#include "irrlichttypes.h"
|
||||||
#include "util/enriched_string.h"
|
#include "util/enriched_string.h"
|
||||||
#include "util/Optional.h"
|
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
|
||||||
// Chat console related classes
|
// Chat console related classes
|
||||||
@ -247,7 +247,7 @@ private:
|
|||||||
struct HistoryEntry {
|
struct HistoryEntry {
|
||||||
std::wstring line;
|
std::wstring line;
|
||||||
// If line is edited, saved holds the unedited version.
|
// 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) {}
|
HistoryEntry(const std::wstring &line): line(line) {}
|
||||||
|
|
||||||
|
@ -698,7 +698,7 @@ void Camera::drawNametags()
|
|||||||
|
|
||||||
Nametag *Camera::addNametag(scene::ISceneNode *parent_node,
|
Nametag *Camera::addNametag(scene::ISceneNode *parent_node,
|
||||||
const std::string &text, video::SColor textcolor,
|
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);
|
Nametag *nametag = new Nametag(parent_node, text, textcolor, bgcolor, pos);
|
||||||
m_nametags.push_back(nametag);
|
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 <plane3d.h>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include "util/Optional.h"
|
#include <optional>
|
||||||
|
|
||||||
class LocalPlayer;
|
class LocalPlayer;
|
||||||
struct MapDrawControl;
|
struct MapDrawControl;
|
||||||
@ -41,13 +41,13 @@ struct Nametag
|
|||||||
scene::ISceneNode *parent_node;
|
scene::ISceneNode *parent_node;
|
||||||
std::string text;
|
std::string text;
|
||||||
video::SColor textcolor;
|
video::SColor textcolor;
|
||||||
Optional<video::SColor> bgcolor;
|
std::optional<video::SColor> bgcolor;
|
||||||
v3f pos;
|
v3f pos;
|
||||||
|
|
||||||
Nametag(scene::ISceneNode *a_parent_node,
|
Nametag(scene::ISceneNode *a_parent_node,
|
||||||
const std::string &text,
|
const std::string &text,
|
||||||
const video::SColor &textcolor,
|
const video::SColor &textcolor,
|
||||||
const Optional<video::SColor> &bgcolor,
|
const std::optional<video::SColor> &bgcolor,
|
||||||
const v3f &pos):
|
const v3f &pos):
|
||||||
parent_node(a_parent_node),
|
parent_node(a_parent_node),
|
||||||
text(text),
|
text(text),
|
||||||
@ -201,7 +201,7 @@ public:
|
|||||||
|
|
||||||
Nametag *addNametag(scene::ISceneNode *parent_node,
|
Nametag *addNametag(scene::ISceneNode *parent_node,
|
||||||
const std::string &text, video::SColor textcolor,
|
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);
|
void removeNametag(Nametag *nametag);
|
||||||
|
|
||||||
|
@ -3053,7 +3053,7 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Don't keep old focus value
|
// Don't keep old focus value
|
||||||
m_focused_element = nullopt;
|
m_focused_element = std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
removeAll();
|
removeAll();
|
||||||
|
@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
@ -33,7 +34,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
#include "guiTable.h"
|
#include "guiTable.h"
|
||||||
#include "network/networkprotocol.h"
|
#include "network/networkprotocol.h"
|
||||||
#include "client/joystick_controller.h"
|
#include "client/joystick_controller.h"
|
||||||
#include "util/Optional.h"
|
|
||||||
#include "util/string.h"
|
#include "util/string.h"
|
||||||
#include "util/enriched_string.h"
|
#include "util/enriched_string.h"
|
||||||
#include "StyleSpec.h"
|
#include "StyleSpec.h"
|
||||||
@ -374,13 +374,13 @@ protected:
|
|||||||
video::SColor m_default_tooltip_color;
|
video::SColor m_default_tooltip_color;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
IFormSource *m_form_src;
|
IFormSource *m_form_src;
|
||||||
TextDest *m_text_dst;
|
TextDest *m_text_dst;
|
||||||
std::string m_last_formname;
|
std::string m_last_formname;
|
||||||
u16 m_formspec_version = 1;
|
u16 m_formspec_version = 1;
|
||||||
Optional<std::string> m_focused_element = nullopt;
|
std::optional<std::string> m_focused_element = std::nullopt;
|
||||||
JoystickController *m_joystick;
|
JoystickController *m_joystick;
|
||||||
bool m_show_debug = false;
|
bool m_show_debug = false;
|
||||||
|
|
||||||
struct parserData {
|
struct parserData {
|
||||||
bool explicit_size;
|
bool explicit_size;
|
||||||
|
@ -911,8 +911,8 @@ bool Server::checkInteractDistance(RemotePlayer *player, const f32 d, const std:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tiny helper to retrieve the selected item into an Optional
|
// Tiny helper to retrieve the selected item into an std::optional
|
||||||
static inline void getWieldedItem(const PlayerSAO *playersao, Optional<ItemStack> &ret)
|
static inline void getWieldedItem(const PlayerSAO *playersao, std::optional<ItemStack> &ret)
|
||||||
{
|
{
|
||||||
ret = ItemStack();
|
ret = ItemStack();
|
||||||
playersao->getWieldedItem(&(*ret));
|
playersao->getWieldedItem(&(*ret));
|
||||||
@ -1226,7 +1226,7 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
|
|||||||
|
|
||||||
// Place block or right-click object
|
// Place block or right-click object
|
||||||
case INTERACT_PLACE: {
|
case INTERACT_PLACE: {
|
||||||
Optional<ItemStack> selected_item;
|
std::optional<ItemStack> selected_item;
|
||||||
getWieldedItem(playersao, selected_item);
|
getWieldedItem(playersao, selected_item);
|
||||||
|
|
||||||
// Reset build time counter
|
// Reset build time counter
|
||||||
@ -1285,7 +1285,7 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
|
|||||||
} // action == INTERACT_PLACE
|
} // action == INTERACT_PLACE
|
||||||
|
|
||||||
case INTERACT_USE: {
|
case INTERACT_USE: {
|
||||||
Optional<ItemStack> selected_item;
|
std::optional<ItemStack> selected_item;
|
||||||
getWieldedItem(playersao, selected_item);
|
getWieldedItem(playersao, selected_item);
|
||||||
|
|
||||||
actionstream << player->getName() << " uses " << selected_item->name
|
actionstream << player->getName() << " uses " << selected_item->name
|
||||||
@ -1302,7 +1302,7 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
|
|||||||
|
|
||||||
// Rightclick air
|
// Rightclick air
|
||||||
case INTERACT_ACTIVATE: {
|
case INTERACT_ACTIVATE: {
|
||||||
Optional<ItemStack> selected_item;
|
std::optional<ItemStack> selected_item;
|
||||||
getWieldedItem(playersao, selected_item);
|
getWieldedItem(playersao, selected_item);
|
||||||
|
|
||||||
actionstream << player->getName() << " activates "
|
actionstream << player->getName() << " activates "
|
||||||
|
@ -237,7 +237,7 @@ void ObjectProperties::deSerialize(std::istream &is)
|
|||||||
if (bgcolor != NULL_BGCOLOR)
|
if (bgcolor != NULL_BGCOLOR)
|
||||||
nametag_bgcolor = bgcolor;
|
nametag_bgcolor = bgcolor;
|
||||||
else
|
else
|
||||||
nametag_bgcolor = nullopt;
|
nametag_bgcolor = std::nullopt;
|
||||||
|
|
||||||
tmp = readU8(is);
|
tmp = readU8(is);
|
||||||
if (is.eof())
|
if (is.eof())
|
||||||
|
@ -19,12 +19,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "irrlichttypes_bloated.h"
|
#include "irrlichttypes_bloated.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "util/Optional.h"
|
|
||||||
|
|
||||||
struct ObjectProperties
|
struct ObjectProperties
|
||||||
{
|
{
|
||||||
@ -55,7 +55,7 @@ struct ObjectProperties
|
|||||||
s8 glow = 0;
|
s8 glow = 0;
|
||||||
std::string nametag = "";
|
std::string nametag = "";
|
||||||
video::SColor nametag_color = video::SColor(255, 255, 255, 255);
|
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;
|
f32 automatic_face_movement_max_rotation_per_sec = -1.0f;
|
||||||
std::string infotext;
|
std::string infotext;
|
||||||
//! For dropped items, this contains item information.
|
//! 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))
|
if (read_color(L, -1, &color))
|
||||||
prop->nametag_bgcolor = color;
|
prop->nametag_bgcolor = color;
|
||||||
} else {
|
} else {
|
||||||
prop->nametag_bgcolor = nullopt;
|
prop->nametag_bgcolor = std::nullopt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
|
@ -59,7 +59,7 @@ bool ScriptApiItem::item_OnDrop(ItemStack &item,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ScriptApiItem::item_OnPlace(Optional<ItemStack> &ret_item,
|
bool ScriptApiItem::item_OnPlace(std::optional<ItemStack> &ret_item,
|
||||||
ServerActiveObject *placer, const PointedThing &pointed)
|
ServerActiveObject *placer, const PointedThing &pointed)
|
||||||
{
|
{
|
||||||
SCRIPTAPI_PRECHECKHEADER
|
SCRIPTAPI_PRECHECKHEADER
|
||||||
@ -88,13 +88,13 @@ bool ScriptApiItem::item_OnPlace(Optional<ItemStack> &ret_item,
|
|||||||
throw WRAP_LUAERROR(e, "item=" + item.name);
|
throw WRAP_LUAERROR(e, "item=" + item.name);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ret_item = nullopt;
|
ret_item = std::nullopt;
|
||||||
}
|
}
|
||||||
lua_pop(L, 2); // Pop item and error handler
|
lua_pop(L, 2); // Pop item and error handler
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ScriptApiItem::item_OnUse(Optional<ItemStack> &ret_item,
|
bool ScriptApiItem::item_OnUse(std::optional<ItemStack> &ret_item,
|
||||||
ServerActiveObject *user, const PointedThing &pointed)
|
ServerActiveObject *user, const PointedThing &pointed)
|
||||||
{
|
{
|
||||||
SCRIPTAPI_PRECHECKHEADER
|
SCRIPTAPI_PRECHECKHEADER
|
||||||
@ -118,13 +118,13 @@ bool ScriptApiItem::item_OnUse(Optional<ItemStack> &ret_item,
|
|||||||
throw WRAP_LUAERROR(e, "item=" + item.name);
|
throw WRAP_LUAERROR(e, "item=" + item.name);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ret_item = nullopt;
|
ret_item = std::nullopt;
|
||||||
}
|
}
|
||||||
lua_pop(L, 2); // Pop item and error handler
|
lua_pop(L, 2); // Pop item and error handler
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ScriptApiItem::item_OnSecondaryUse(Optional<ItemStack> &ret_item,
|
bool ScriptApiItem::item_OnSecondaryUse(std::optional<ItemStack> &ret_item,
|
||||||
ServerActiveObject *user, const PointedThing &pointed)
|
ServerActiveObject *user, const PointedThing &pointed)
|
||||||
{
|
{
|
||||||
SCRIPTAPI_PRECHECKHEADER
|
SCRIPTAPI_PRECHECKHEADER
|
||||||
@ -146,7 +146,7 @@ bool ScriptApiItem::item_OnSecondaryUse(Optional<ItemStack> &ret_item,
|
|||||||
throw WRAP_LUAERROR(e, "item=" + item.name);
|
throw WRAP_LUAERROR(e, "item=" + item.name);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ret_item = nullopt;
|
ret_item = std::nullopt;
|
||||||
}
|
}
|
||||||
lua_pop(L, 2); // Pop item and error handler
|
lua_pop(L, 2); // Pop item and error handler
|
||||||
return true;
|
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 "cpp_api/s_base.h"
|
||||||
#include "irr_v3d.h"
|
#include "irr_v3d.h"
|
||||||
#include "util/Optional.h"
|
#include <optional>
|
||||||
|
|
||||||
struct PointedThing;
|
struct PointedThing;
|
||||||
struct ItemStack;
|
struct ItemStack;
|
||||||
@ -37,7 +37,7 @@ class ScriptApiItem
|
|||||||
{
|
{
|
||||||
public:
|
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.
|
* 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
|
* This has a longer backstory where on_use may need to empty the player's
|
||||||
* inventory without the engine interfering (see issue #6546).
|
* inventory without the engine interfering (see issue #6546).
|
||||||
@ -45,11 +45,11 @@ public:
|
|||||||
|
|
||||||
bool item_OnDrop(ItemStack &item,
|
bool item_OnDrop(ItemStack &item,
|
||||||
ServerActiveObject *dropper, v3f pos);
|
ServerActiveObject *dropper, v3f pos);
|
||||||
bool item_OnPlace(Optional<ItemStack> &item,
|
bool item_OnPlace(std::optional<ItemStack> &item,
|
||||||
ServerActiveObject *placer, const PointedThing &pointed);
|
ServerActiveObject *placer, const PointedThing &pointed);
|
||||||
bool item_OnUse(Optional<ItemStack> &item,
|
bool item_OnUse(std::optional<ItemStack> &item,
|
||||||
ServerActiveObject *user, const PointedThing &pointed);
|
ServerActiveObject *user, const PointedThing &pointed);
|
||||||
bool item_OnSecondaryUse(Optional<ItemStack> &item,
|
bool item_OnSecondaryUse(std::optional<ItemStack> &item,
|
||||||
ServerActiveObject *user, const PointedThing &pointed);
|
ServerActiveObject *user, const PointedThing &pointed);
|
||||||
bool item_OnCraft(ItemStack &item, ServerActiveObject *user,
|
bool item_OnCraft(ItemStack &item, ServerActiveObject *user,
|
||||||
const InventoryList *old_craft_grid, const InventoryLocation &craft_inv);
|
const InventoryList *old_craft_grid, const InventoryLocation &craft_inv);
|
||||||
|
@ -446,7 +446,7 @@ int ModApiEnvMod::l_place_node(lua_State *L)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
// Create item to place
|
// 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
|
// Make pointed position
|
||||||
PointedThing pointed;
|
PointedThing pointed;
|
||||||
pointed.type = POINTEDTHING_NODE;
|
pointed.type = POINTEDTHING_NODE;
|
||||||
|
@ -716,7 +716,7 @@ int ObjectRef::l_set_nametag_attributes(lua_State *L)
|
|||||||
if (read_color(L, -1, &color))
|
if (read_color(L, -1, &color))
|
||||||
prop->nametag_bgcolor = color;
|
prop->nametag_bgcolor = color;
|
||||||
} else {
|
} else {
|
||||||
prop->nametag_bgcolor = nullopt;
|
prop->nametag_bgcolor = std::nullopt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lua_pop(L, 1);
|
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;
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user