From 60f0269569418d401a576769fd754945df21c236 Mon Sep 17 00:00:00 2001 From: emufan4568 Date: Sat, 16 Jul 2022 09:31:33 +0300 Subject: [PATCH] core: Replace boost::optional with std::optional --- src/common/CMakeLists.txt | 1 + src/common/serialization/optional.h | 66 +++++++++++++++++++++++ src/core/core.cpp | 2 +- src/core/hle/service/apt/applet_manager.h | 11 ++-- src/core/hle/service/http_c.h | 4 +- src/core/hle/service/nwm/nwm_uds.cpp | 4 +- src/core/hle/service/nwm/nwm_uds.h | 4 +- src/core/movie.cpp | 30 +++++------ vcpkg.json | 2 +- 9 files changed, 95 insertions(+), 29 deletions(-) create mode 100644 src/common/serialization/optional.h diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 14f3afb03..56fb77568 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -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 diff --git a/src/common/serialization/optional.h b/src/common/serialization/optional.h new file mode 100644 index 000000000..52c601220 --- /dev/null +++ b/src/common/serialization/optional.h @@ -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 +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace serialization { + +// Allow serialization of std::optional +template +void save(Archive& ar, const std::optional& 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 +void load(Archive& ar, std::optional& 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 +void serialize(Archive& ar, std::optional& t, const unsigned int version) { + boost::serialization::split_free(ar, t, version); +} + +template +struct version> { + BOOST_STATIC_CONSTANT(int, value = 1); +}; + +} // serialization +} // boost \ No newline at end of file diff --git a/src/core/core.cpp b/src/core/core.cpp index 9ae24a53e..f60cfb6a7 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -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 deliver_arg; + std::optional deliver_arg; if (auto apt = Service::APT::GetModule(*this)) { deliver_arg = apt->GetAppletManager()->ReceiveDeliverArg(); } diff --git a/src/core/hle/service/apt/applet_manager.h b/src/core/hle/service/apt/applet_manager.h index 830efa20f..3318e4d3e 100644 --- a/src/core/hle/service/apt/applet_manager.h +++ b/src/core/hle/service/apt/applet_manager.h @@ -9,10 +9,11 @@ #include #include #include +#include #include -#include #include #include +#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 ReceiveDeliverArg() const { + std::optional ReceiveDeliverArg() const { return deliver_arg; } - void SetDeliverArg(boost::optional arg) { + void SetDeliverArg(std::optional 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 next_parameter; + std::optional next_parameter; static constexpr std::size_t NumAppletSlot = 4; @@ -271,7 +272,7 @@ private: }; ApplicationJumpParameters app_jump_parameters{}; - boost::optional deliver_arg{}; + std::optional deliver_arg{}; // Holds data about the concurrently running applets in the system. std::array applet_slots = {}; diff --git a/src/core/hle/service/http_c.h b/src/core/hle/service/http_c.h index 490e06648..37935b786 100644 --- a/src/core/hle/service/http_c.h +++ b/src/core/hle/service/http_c.h @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include #include @@ -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 current_http_context; + std::optional current_http_context; u32 session_id; diff --git a/src/core/hle/service/nwm/nwm_uds.cpp b/src/core/hle/service/nwm/nwm_uds.cpp index 05bff9455..15ab34488 100644 --- a/src/core/hle/service/nwm/nwm_uds.cpp +++ b/src/core/hle/service/nwm/nwm_uds.cpp @@ -544,7 +544,7 @@ void NWM_UDS::OnWifiPacketReceived(const Network::WifiPacket& packet) { } } -boost::optional NWM_UDS::GetNodeMacAddress(u16 dest_node_id, u8 flags) { +std::optional 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 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; } diff --git a/src/core/hle/service/nwm/nwm_uds.h b/src/core/hle/service/nwm/nwm_uds.h index c5bff31ac..c1a7fc6b4 100644 --- a/src/core/hle/service/nwm/nwm_uds.h +++ b/src/core/hle/service/nwm/nwm_uds.h @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #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 GetNodeMacAddress(u16 dest_node_id, u8 flags); + std::optional GetNodeMacAddress(u16 dest_node_id, u8 flags); // Event that is signaled every time the connection status changes. std::shared_ptr connection_status_event; diff --git a/src/core/movie.cpp b/src/core/movie.cpp index 68bcf2237..59a5c5a4b 100644 --- a/src/core/movie.cpp +++ b/src/core/movie.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include #include "common/bit_field.h" @@ -574,30 +574,28 @@ void Movie::SetReadOnly(bool read_only_) { read_only = read_only_; } -static boost::optional ReadHeader(const std::string& movie_file) { +static std::optional 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 author{}; // Add a null terminator + std::memcpy(author.data(), header->author.data(), header->author.size()); - std::array 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() { diff --git a/vcpkg.json b/vcpkg.json index a8b4431c9..b20cb002b 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -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",