Skip to content

Commit

Permalink
Replace Optional with std::optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Desour authored and sfan5 committed Jun 15, 2023
1 parent 34ad551 commit e700182
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 198 deletions.
2 changes: 1 addition & 1 deletion src/chat.cpp
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/chat.h
Expand Up @@ -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
Expand Down Expand Up @@ -247,7 +247,7 @@ class ChatPrompt
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) {}

Expand Down
2 changes: 1 addition & 1 deletion src/client/camera.cpp
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/client/camera.h
Expand Up @@ -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;
Expand All @@ -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),
Expand Down Expand Up @@ -201,7 +201,7 @@ class Camera

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

Expand Down
2 changes: 1 addition & 1 deletion src/gui/guiFormSpecMenu.cpp
Expand Up @@ -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();
Expand Down
16 changes: 8 additions & 8 deletions src/gui/guiFormSpecMenu.h
Expand Up @@ -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>
Expand All @@ -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"
Expand Down Expand Up @@ -374,13 +374,13 @@ class GUIFormSpecMenu : public GUIModalMenu
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<std::string> 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<std::string> m_focused_element = std::nullopt;
JoystickController *m_joystick;
bool m_show_debug = false;

struct parserData {
bool explicit_size;
Expand Down
10 changes: 5 additions & 5 deletions src/network/serverpackethandler.cpp
Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 "
Expand Down
2 changes: 1 addition & 1 deletion src/object_properties.cpp
Expand Up @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions src/object_properties.h
Expand Up @@ -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
{
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/script/common/c_content.cpp
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/script/cpp_api/s_item.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions src/script/cpp_api/s_item.h
Expand Up @@ -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;
Expand All @@ -37,19 +37,19 @@ 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).
*/

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);
Expand Down
2 changes: 1 addition & 1 deletion src/script/lua_api/l_env.cpp
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/script/lua_api/l_object.cpp
Expand Up @@ -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);
Expand Down

0 comments on commit e700182

Please sign in to comment.