core: Replace boost::optional with std::optional

This commit is contained in:
emufan4568
2022-07-16 09:31:33 +03:00
parent ceadee0358
commit 60f0269569
9 changed files with 95 additions and 29 deletions

View File

@@ -100,6 +100,7 @@ add_library(common STATIC
serialization/boost_flat_set.h
serialization/boost_small_vector.hpp
serialization/boost_vector.hpp
serialization/optional.h
string_util.cpp
string_util.h
swap.h

View File

@@ -0,0 +1,66 @@
// Copyright 2022 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <optional>
#include <boost/config.hpp>
#include <boost/serialization/item_version_type.hpp>
#include <boost/serialization/library_version_type.hpp>
#include <boost/serialization/version.hpp>
#include <boost/serialization/split_free.hpp>
#include <boost/serialization/nvp.hpp>
namespace boost {
namespace serialization {
// Allow serialization of std::optional
template<class Archive, class T>
void save(Archive& ar, const std::optional<T>& t, const unsigned int /*version*/) {
const bool tflag = t.has_value();
ar << boost::serialization::make_nvp("initialized", tflag);
if (tflag) {
ar << boost::serialization::make_nvp("value", *t);
}
}
template<class Archive, class T>
void load(Archive& ar, std::optional<T>& t, const unsigned int version) {
bool tflag;
ar >> boost::serialization::make_nvp("initialized", tflag);
if (!tflag) {
t.reset();
return;
}
if (version == 0) {
boost::serialization::item_version_type item_version(0);
boost::serialization::library_version_type library_version(
ar.get_library_version()
);
if (boost::serialization::library_version_type(3) < library_version) {
ar >> BOOST_SERIALIZATION_NVP(item_version);
}
}
if (!t.has_value()) {
t = T();
}
ar >> boost::serialization::make_nvp("value", *t);
}
template<class Archive, class T>
void serialize(Archive& ar, std::optional<T>& t, const unsigned int version) {
boost::serialization::split_free(ar, t, version);
}
template<class T>
struct version<std::optional<T>> {
BOOST_STATIC_CONSTANT(int, value = 1);
};
} // serialization
} // boost

View File

@@ -568,7 +568,7 @@ void System::Reset() {
// TODO: Properly implement the reset
// Since the system is completely reinitialized, we'll have to store the deliver arg manually.
boost::optional<Service::APT::AppletManager::DeliverArg> deliver_arg;
std::optional<Service::APT::AppletManager::DeliverArg> deliver_arg;
if (auto apt = Service::APT::GetModule(*this)) {
deliver_arg = apt->GetAppletManager()->ReceiveDeliverArg();
}

View File

@@ -9,10 +9,11 @@
#include <memory>
#include <optional>
#include <vector>
#include <optional>
#include <boost/serialization/array.hpp>
#include <boost/serialization/optional.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/vector.hpp>
#include "common/serialization/optional.h"
#include "core/global.h"
#include "core/hle/kernel/event.h"
#include "core/hle/result.h"
@@ -178,10 +179,10 @@ public:
ResultCode DoApplicationJump(DeliverArg arg);
boost::optional<DeliverArg> ReceiveDeliverArg() const {
std::optional<DeliverArg> ReceiveDeliverArg() const {
return deliver_arg;
}
void SetDeliverArg(boost::optional<DeliverArg> arg) {
void SetDeliverArg(std::optional<DeliverArg> arg) {
deliver_arg = std::move(arg);
}
@@ -224,7 +225,7 @@ public:
private:
/// Parameter data to be returned in the next call to Glance/ReceiveParameter.
// NOTE: A bug in gcc prevents serializing std::optional
boost::optional<MessageParameter> next_parameter;
std::optional<MessageParameter> next_parameter;
static constexpr std::size_t NumAppletSlot = 4;
@@ -271,7 +272,7 @@ private:
};
ApplicationJumpParameters app_jump_parameters{};
boost::optional<DeliverArg> deliver_arg{};
std::optional<DeliverArg> deliver_arg{};
// Holds data about the concurrently running applets in the system.
std::array<AppletSlotData, NumAppletSlot> applet_slots = {};

View File

@@ -10,7 +10,7 @@
#include <string>
#include <unordered_map>
#include <vector>
#include <boost/optional.hpp>
#include <optional>
#include <boost/serialization/optional.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/string.hpp>
@@ -221,7 +221,7 @@ public:
struct SessionData : public Kernel::SessionRequestHandler::SessionDataBase {
/// The HTTP context that is currently bound to this session, this can be empty if no context
/// has been bound. Certain commands can only be called on a session with a bound context.
boost::optional<Context::Handle> current_http_context;
std::optional<Context::Handle> current_http_context;
u32 session_id;

View File

@@ -544,7 +544,7 @@ void NWM_UDS::OnWifiPacketReceived(const Network::WifiPacket& packet) {
}
}
boost::optional<Network::MacAddress> NWM_UDS::GetNodeMacAddress(u16 dest_node_id, u8 flags) {
std::optional<Network::MacAddress> NWM_UDS::GetNodeMacAddress(u16 dest_node_id, u8 flags) {
constexpr u8 BroadcastFlag = 0x2;
if ((flags & BroadcastFlag) || dest_node_id == BroadcastNetworkNodeId) {
// Broadcast
@@ -559,7 +559,7 @@ boost::optional<Network::MacAddress> NWM_UDS::GetNodeMacAddress(u16 dest_node_id
return node.second.node_id == dest_node_id && node.second.connected;
});
if (destination == node_map.end()) {
return {};
return std::nullopt;
}
return destination->first;
}

View File

@@ -14,7 +14,7 @@
#include <mutex>
#include <unordered_map>
#include <vector>
#include <boost/optional.hpp>
#include <optional>
#include <boost/serialization/export.hpp>
#include "common/common_types.h"
#include "common/swap.h"
@@ -506,7 +506,7 @@ private:
/// Callback to parse and handle a received wifi packet.
void OnWifiPacketReceived(const Network::WifiPacket& packet);
boost::optional<Network::MacAddress> GetNodeMacAddress(u16 dest_node_id, u8 flags);
std::optional<Network::MacAddress> GetNodeMacAddress(u16 dest_node_id, u8 flags);
// Event that is signaled every time the connection status changes.
std::shared_ptr<Kernel::Event> connection_status_event;

View File

@@ -7,7 +7,7 @@
#include <stdexcept>
#include <string>
#include <vector>
#include <boost/optional.hpp>
#include <optional>
#include <cryptopp/hex.h>
#include <cryptopp/osrng.h>
#include "common/bit_field.h"
@@ -574,30 +574,28 @@ void Movie::SetReadOnly(bool read_only_) {
read_only = read_only_;
}
static boost::optional<CTMHeader> ReadHeader(const std::string& movie_file) {
static std::optional<CTMHeader> ReadHeader(const std::string& movie_file) {
FileUtil::IOFile save_record(movie_file, "rb");
const u64 size = save_record.GetSize();
if (!save_record || size <= sizeof(CTMHeader)) {
return boost::none;
return std::nullopt;
}
CTMHeader header;
save_record.ReadArray(&header, 1);
if (header_magic_bytes != header.filetype) {
return boost::none;
return std::nullopt;
}
return header;
}
void Movie::PrepareForPlayback(const std::string& movie_file) {
auto header = ReadHeader(movie_file);
if (header == boost::none)
return;
init_time = header.value().clock_init_time;
if (auto header = ReadHeader(movie_file); header) {
init_time = header.value().clock_init_time;
}
}
void Movie::PrepareForRecording() {
@@ -638,15 +636,15 @@ Movie::ValidationResult Movie::ValidateMovie(const std::string& movie_file) cons
}
Movie::MovieMetadata Movie::GetMovieMetadata(const std::string& movie_file) const {
auto header = ReadHeader(movie_file);
if (header == boost::none)
return {};
if (auto header = ReadHeader(movie_file); header) {
std::array<char, 33> author{}; // Add a null terminator
std::memcpy(author.data(), header->author.data(), header->author.size());
std::array<char, 33> author{}; // Add a null terminator
std::memcpy(author.data(), header->author.data(), header->author.size());
return {header->program_id, std::string{author.data()}, header->rerecord_count,
header->input_count};
}
return {header->program_id, std::string{author.data()}, header->rerecord_count,
header->input_count};
return {};
}
void Movie::Shutdown() {

View File

@@ -6,7 +6,6 @@
"boost-serialization",
"boost-system",
"boost-range",
"boost-optional",
"boost-icl",
"boost-date-time",
"boost-crc",
@@ -38,6 +37,7 @@
"lodepng",
"glslang",
"soundtouch",
"robin-map",
"xbyak",
"zstd",
"sdl2",