Skip to content

Commit 0e506e2

Browse files
committed
Replace boost::optional by std::optional
1 parent f299328 commit 0e506e2

80 files changed

Lines changed: 209 additions & 175 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

extras/ai-battle/main.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@
1616
#include <boost/nowide/args.hpp>
1717
#include <boost/nowide/filesystem.hpp>
1818
#include <boost/nowide/iostream.hpp>
19-
#include <boost/optional.hpp>
2019
#include <boost/program_options.hpp>
20+
#if BOOST_VERSION >= 109000
21+
# include <optional>
22+
using std::optional;
23+
#else
24+
# include <boost/optional.hpp>
25+
using boost::optional;
26+
#endif
2127

2228
namespace bnw = boost::nowide;
2329
namespace bfs = boost::filesystem;
@@ -28,8 +34,8 @@ int main(int argc, char** argv)
2834
bnw::nowide_filesystem();
2935
bnw::args _(argc, argv);
3036

31-
boost::optional<std::string> replay_path;
32-
boost::optional<std::string> savegame_path;
37+
optional<std::string> replay_path;
38+
optional<std::string> savegame_path;
3339
unsigned random_init = static_cast<unsigned>(std::chrono::high_resolution_clock::now().time_since_epoch().count());
3440
unsigned random_ai_init = random_init;
3541

libs/common/include/helpers/OptionalEnum.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
#pragma once
66

77
#include "MaxEnumValue.h"
8-
#include <boost/none.hpp>
9-
#include <boost/optional/bad_optional_access.hpp>
108
#include <limits>
9+
#include <optional>
1110
#include <type_traits>
1211

1312
namespace helpers {
@@ -26,7 +25,7 @@ class OptionalEnum
2625
using value_type = T;
2726

2827
constexpr OptionalEnum() noexcept = default;
29-
OptionalEnum(boost::none_t) noexcept {}
28+
OptionalEnum(std::nullopt_t) noexcept {}
3029
constexpr OptionalEnum(const T& value) noexcept : value_(static_cast<underlying_type>(value)) {}
3130
constexpr OptionalEnum& operator=(const T& value) noexcept
3231
{
@@ -41,7 +40,7 @@ class OptionalEnum
4140
constexpr T value() const
4241
{
4342
if(!has_value())
44-
throw boost::bad_optional_access();
43+
throw std::bad_optional_access();
4544
return **this;
4645
}
4746
constexpr T value_or(T default_value) const { return bool(*this) ? **this : default_value; }
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org)
2+
//
3+
// SPDX-License-Identifier: GPL-2.0-or-later
4+
5+
#pragma once
6+
7+
#include <boost/test/tools/detail/print_helper.hpp>
8+
#include <optional>
9+
#include <ostream>
10+
11+
template<class CharType, class CharTrait, class T>
12+
inline std::basic_ostream<CharType, CharTrait>& operator<<(std::basic_ostream<CharType, CharTrait>& out,
13+
std::optional<T> const& v)
14+
{
15+
return out << (v ? *v : "--");
16+
}
17+
namespace boost::test_tools::tt_detail {
18+
template<typename T>
19+
struct print_log_value<std::optional<T>>
20+
{
21+
void operator()(std::ostream& os, std::optional<T> const& v)
22+
{
23+
if(v)
24+
os << *v;
25+
else
26+
os << "<nullopt>";
27+
}
28+
};
29+
} // namespace boost::test_tools::tt_detail

libs/s25main/BuildingRegister.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ helpers::EnumArray<uint16_t, BuildingType> BuildingRegister::CalcProductivities(
157157

158158
unsigned BuildingRegister::CalcAverageProductivity(BuildingType bldType) const
159159
{
160-
if(holds_alternative<boost::none_t>(BLD_WORK_DESC[bldType].producedWare))
160+
if(holds_alternative<std::nullopt_t>(BLD_WORK_DESC[bldType].producedWare))
161161
return 0;
162162
unsigned productivity = 0;
163163
const auto& buildings = GetBuildings(bldType);
@@ -177,7 +177,7 @@ unsigned short BuildingRegister::CalcAverageProductivity() const
177177
unsigned numBlds = 0;
178178
for(const auto bldType : helpers::enumRange<BuildingType>())
179179
{
180-
if(holds_alternative<boost::none_t>(BLD_WORK_DESC[bldType].producedWare))
180+
if(holds_alternative<std::nullopt_t>(BLD_WORK_DESC[bldType].producedWare))
181181
continue;
182182

183183
const auto& buildings = GetBuildings(bldType);

libs/s25main/EconomyModeHandler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#include "gameTypes/JobTypes.h"
1717
#include "gameData/GoodConsts.h"
1818
#include "gameData/JobConsts.h"
19-
#include <boost/optional.hpp>
2019
#include <array>
20+
#include <optional>
2121

2222
EconomyModeHandler::EconomyModeHandler(unsigned endFrame) : endFrame(endFrame), gfLastUpdated(0)
2323
{
@@ -151,7 +151,7 @@ unsigned EconomyModeHandler::SumUpGood(GoodType good, const Inventory& Inventory
151151
// Add the tools used by workers to the good totals
152152
for(const auto j : helpers::enumRange<Job>())
153153
{
154-
boost::optional<GoodType> tool = JOB_CONSTS[j].tool;
154+
std::optional<GoodType> tool = JOB_CONSTS[j].tool;
155155
if(tool == good)
156156
{
157157
retVal += Inventory.people[j];

libs/s25main/Game.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "lua/LuaInterfaceGame.h"
1414
#include "network/GameClient.h"
1515
#include "gameData/GameConsts.h"
16-
#include <boost/optional.hpp>
16+
#include <optional>
1717

1818
Game::Game(GlobalGameSettings settings, unsigned startGF, const std::vector<PlayerInfo>& players)
1919
: Game(std::move(settings), std::make_unique<EventManager>(startGF), players)
@@ -113,7 +113,7 @@ void Game::CheckObjective()
113113
return;
114114

115115
unsigned maxPoints = 0, maxTeamPoints = 0, totalPoints = 0, bestPlayer = 0;
116-
boost::optional<unsigned> bestTeam;
116+
std::optional<unsigned> bestTeam;
117117

118118
const auto getPlayerMask = [](unsigned playerId) { return 1u << playerId; };
119119

libs/s25main/GameManager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#pragma once
66

77
#include "FrameCounter.h"
8-
#include <boost/optional.hpp>
8+
#include <optional>
99

1010
class Log;
1111
class Settings;
@@ -46,7 +46,7 @@ class GameManager
4646
{
4747
unsigned time, gf;
4848
};
49-
boost::optional<SkipReport> lastSkipReport;
49+
std::optional<SkipReport> lastSkipReport;
5050
};
5151

5252
GameManager& getGlobalGameManager();

libs/s25main/Pathfinding.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ helpers::OptionalEnum<Direction> GameWorldBase::FindHumanPath(const MapPoint sta
2424
PathConditionHuman(*this)))
2525
return first_dir;
2626
else
27-
return boost::none;
27+
return std::nullopt;
2828
}
2929

3030
/// Wegfindung für Menschen im Straßennetz
@@ -102,19 +102,19 @@ helpers::OptionalEnum<Direction> GameWorld::FindTradePath(const MapPoint start,
102102
{
103103
unsigned char owner = GetNode(dest).owner;
104104
if(owner != 0 && !GetPlayer(player).IsAlly(owner - 1))
105-
return boost::none;
105+
return std::nullopt;
106106

107107
RTTR_Assert(GetNO(dest)->GetType() == NodalObjectType::Flag); // Goal should be the flag of a wh
108108

109109
if(!PathConditionHuman(*this).IsNodeOk(dest))
110-
return boost::none;
110+
return std::nullopt;
111111

112112
Direction first_dir{};
113113
if(GetFreePathFinder().FindPath(start, dest, random_route, max_route, route, length, &first_dir,
114114
PathConditionTrade(*this, player)))
115115
return first_dir;
116116
else
117-
return boost::none;
117+
return std::nullopt;
118118
}
119119

120120
bool GameWorld::CheckTradeRoute(const MapPoint start, const std::vector<Direction>& route, unsigned pos,

libs/s25main/Settings.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ const std::map<GUI_ID, std::string> persistentWindows = {{CGI_CHAT, "wnd_chat"},
6060
{CGI_MERCHANDISE_STATISTICS, "wnd_merchandise_statistics"}};
6161

6262
namespace validate {
63-
boost::optional<uint16_t> checkPort(const std::string& port)
63+
std::optional<uint16_t> checkPort(const std::string& port)
6464
{
6565
int32_t iPort;
6666
if((helpers::tryFromString(port, iPort) || s25util::tryFromStringClassic(port, iPort)) && checkPort(iPort))
6767
return static_cast<uint16_t>(iPort);
6868
else
69-
return boost::none;
69+
return std::nullopt;
7070
}
7171
bool checkPort(int port)
7272
{
@@ -309,7 +309,7 @@ void Settings::Load()
309309
// server
310310
// {
311311
server.lastIP = iniServer->getValue("last_ip");
312-
boost::optional<uint16_t> port = validate::checkPort(iniServer->getValue("local_port"));
312+
std::optional<uint16_t> port = validate::checkPort(iniServer->getValue("local_port"));
313313
server.localPort = port.value_or(3665);
314314
server.ipv6 = iniServer->getBoolValue("ipv6");
315315
// }

libs/s25main/Settings.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
#include "driver/VideoMode.h"
1010
#include "s25util/ProxySettings.h"
1111
#include "s25util/Singleton.h"
12-
#include <boost/optional.hpp>
1312
#include <array>
1413
#include <cstdint>
1514
#include <gameData/const_gui_ids.h>
1615
#include <map>
16+
#include <optional>
1717
#include <string>
1818

1919
#undef interface
2020

2121
namespace validate {
22-
boost::optional<uint16_t> checkPort(const std::string& port);
22+
std::optional<uint16_t> checkPort(const std::string& port);
2323
bool checkPort(int port);
2424
} // namespace validate
2525

0 commit comments

Comments
 (0)