From 33777d28769058b6d86514f39aabefa3cba2c603 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Fri, 5 Jun 2020 22:27:04 +0200 Subject: [PATCH] Use same style for optional and avoid double check I did not know that `std::optional::value()` did checked access. And I still prefer a more explicit .has_value() over boolean conversion, but this seems to be the accepted way to do this. --- src/common/events.h | 24 ++++++++++++------------ src/common/logging.cpp | 12 ++++++------ src/plugin/configuration.cpp | 4 ++-- src/plugin/plugin-bridge.cpp | 14 +++++++------- src/plugin/utils.cpp | 4 ++-- src/wine-host/bridges/vst2.cpp | 9 ++++----- src/wine-host/utils.cpp | 4 ++-- 7 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/common/events.h b/src/common/events.h index f49544cc..775e475c 100644 --- a/src/common/events.h +++ b/src/common/events.h @@ -153,8 +153,8 @@ intptr_t send_event(boost::asio::local::stream_protocol::socket& socket, const std::optional value_payload = data_converter.read_value(opcode, value); - if (logging.has_value()) { - auto [logger, is_dispatch] = logging.value(); + if (logging) { + auto [logger, is_dispatch] = *logging; logger.log_event(is_dispatch, opcode, index, value, payload, option, value_payload); } @@ -172,8 +172,8 @@ intptr_t send_event(boost::asio::local::stream_protocol::socket& socket, response = read_object(socket); } - if (logging.has_value()) { - auto [logger, is_dispatch] = logging.value(); + if (logging) { + auto [logger, is_dispatch] = *logging; logger.log_event_response(is_dispatch, opcode, response.return_value, response.payload, response.value_payload); } @@ -209,15 +209,15 @@ void receive_event(boost::asio::local::stream_protocol::socket& socket, std::optional> logging, F callback) { auto event = read_object(socket); - if (logging.has_value()) { - auto [logger, is_dispatch] = logging.value(); + if (logging) { + auto [logger, is_dispatch] = *logging; logger.log_event(is_dispatch, event.opcode, event.index, event.value, event.payload, event.option, event.value_payload); } EventResult response = callback(event); - if (logging.has_value()) { - auto [logger, is_dispatch] = logging.value(); + if (logging) { + auto [logger, is_dispatch] = *logging; logger.log_event_response(is_dispatch, event.opcode, response.return_value, response.payload, response.value_payload); @@ -308,9 +308,9 @@ auto passthrough_event(AEffect* plugin, F callback) { // data through the value argument. void* data = std::visit(read_payload_fn, event.payload); intptr_t value = event.value; - if (event.value_payload.has_value()) { + if (event.value_payload) { value = reinterpret_cast( - std::visit(read_payload_fn, event.value_payload.value())); + std::visit(read_payload_fn, *event.value_payload)); } const intptr_t return_value = callback( @@ -388,9 +388,9 @@ auto passthrough_event(AEffect* plugin, F callback) { const EventResultPayload response_data = std::visit(write_payload_fn, event.payload); std::optional value_response_data = std::nullopt; - if (event.value_payload.has_value()) { + if (event.value_payload) { value_response_data = - std::visit(write_payload_fn, event.value_payload.value()); + std::visit(write_payload_fn, *event.value_payload); } EventResult response{return_value, response_data, value_response_data}; diff --git a/src/common/logging.cpp b/src/common/logging.cpp index 5d373c31..642735a5 100644 --- a/src/common/logging.cpp +++ b/src/common/logging.cpp @@ -158,8 +158,8 @@ void Logger::log_event(bool is_dispatch, } const auto opcode_name = opcode_to_string(is_dispatch, opcode); - if (opcode_name.has_value()) { - message << opcode_name.value(); + if (opcode_name) { + message << *opcode_name; } else { message << ""; } @@ -169,7 +169,7 @@ void Logger::log_event(bool is_dispatch, // Only used during `effSetSpeakerArrangement` and // `effGetSpeakerArrangement` - if (value_payload.has_value()) { + if (value_payload) { std::visit( overload{ [&](auto) {}, @@ -177,7 +177,7 @@ void Logger::log_event(bool is_dispatch, message << "<" << speaker_arrangement.speakers.size() << " input_speakers>, "; }}, - value_payload.value()); + *value_payload); } std::visit( @@ -248,7 +248,7 @@ void Logger::log_event_response( // Only used during `effSetSpeakerArrangement` and // `effGetSpeakerArrangement` - if (value_payload.has_value()) { + if (value_payload) { std::visit( overload{ [&](auto) {}, @@ -256,7 +256,7 @@ void Logger::log_event_response( message << ", <" << speaker_arrangement.speakers.size() << " input_speakers>"; }}, - value_payload.value()); + *value_payload); } std::visit( diff --git a/src/plugin/configuration.cpp b/src/plugin/configuration.cpp index b1b42f18..cacecc73 100644 --- a/src/plugin/configuration.cpp +++ b/src/plugin/configuration.cpp @@ -63,9 +63,9 @@ Configuration Configuration::load_for(const fs::path& yabridge_path) { // to default configuration settings if it doesn't exist const std::optional config_file = find_dominating_file("yabridge.toml", yabridge_path); - if (!config_file.has_value()) { + if (!config_file) { return Configuration(); } - return Configuration(config_file.value(), yabridge_path); + return Configuration(*config_file, yabridge_path); } diff --git a/src/plugin/plugin-bridge.cpp b/src/plugin/plugin-bridge.cpp index de1596ab..55e8c1ec 100644 --- a/src/plugin/plugin-bridge.cpp +++ b/src/plugin/plugin-bridge.cpp @@ -62,13 +62,13 @@ PluginBridge::PluginBridge(audioMasterCallback host_callback) create_logger_prefix(socket_endpoint.path()))), wine_version(get_wine_version()), vst_host( - config.group.has_value() + config.group ? std::unique_ptr( std::make_unique(io_context, logger, vst_plugin_path, socket_endpoint.path(), - config.group.value(), + *config.group, host_vst_dispatch)) : std::unique_ptr( std::make_unique(io_context, @@ -554,9 +554,9 @@ float PluginBridge::get_parameter(AEffect* /*plugin*/, int index) { response = read_object(host_vst_parameters); } - logger.log_get_parameter_response(response.value.value()); + logger.log_get_parameter_response(*response.value); - return response.value.value(); + return *response.value; } void PluginBridge::set_parameter(AEffect* /*plugin*/, int index, float value) { @@ -575,7 +575,7 @@ void PluginBridge::set_parameter(AEffect* /*plugin*/, int index, float value) { logger.log_set_parameter_response(); // This should not contain any values and just serve as an acknowledgement - assert(!response.value.has_value()); + assert(!response.value); } void PluginBridge::log_init_message() { @@ -602,8 +602,8 @@ void PluginBridge::log_init_message() { << config.matched_file.value_or("").string() << "'" << std::endl; init_msg << "hosting mode: '"; - if (config.group.has_value()) { - init_msg << "plugin group \"" << config.group.value() << "\""; + if (config.group) { + init_msg << "plugin group \"" << *config.group << "\""; } else { init_msg << "individually"; } diff --git a/src/plugin/utils.cpp b/src/plugin/utils.cpp index af28b3ff..9e61a21c 100644 --- a/src/plugin/utils.cpp +++ b/src/plugin/utils.cpp @@ -57,7 +57,7 @@ std::string create_logger_prefix(const fs::path& socket_path) { std::optional find_wineprefix() { std::optional dosdevices_dir = find_dominating_file("dosdevices", find_vst_plugin(), fs::is_directory); - if (!dosdevices_dir.has_value()) { + if (!dosdevices_dir) { return std::nullopt; } @@ -276,7 +276,7 @@ bp::environment set_wineprefix() { } const auto wineprefix_path = find_wineprefix(); - if (wineprefix_path.has_value()) { + if (wineprefix_path) { env["WINEPREFIX"] = wineprefix_path->string(); } diff --git a/src/wine-host/bridges/vst2.cpp b/src/wine-host/bridges/vst2.cpp index 4f5d1e85..4adb4bde 100644 --- a/src/wine-host/bridges/vst2.cpp +++ b/src/wine-host/bridges/vst2.cpp @@ -245,10 +245,9 @@ void Vst2Bridge::handle_parameters() { // presence of the `value` field tells us which one we're dealing // with. auto request = read_object(host_vst_parameters); - if (request.value.has_value()) { + if (request.value) { // `setParameter` - plugin->setParameter(plugin, request.index, - request.value.value()); + plugin->setParameter(plugin, request.index, *request.value); ParameterResult response{std::nullopt}; write_object(host_vst_parameters, response); @@ -477,8 +476,8 @@ class HostCallbackDataConverter : DefaultDataConverter { // Return a pointer to the `VstTimeInfo` object written in // the function above VstTimeInfo* time_info_pointer = nullptr; - if (time_info.has_value()) { - time_info_pointer = &time_info.value(); + if (time_info) { + time_info_pointer = &*time_info; } return reinterpret_cast(time_info_pointer); diff --git a/src/wine-host/utils.cpp b/src/wine-host/utils.cpp index 1acac4d7..a5b7f4e3 100644 --- a/src/wine-host/utils.cpp +++ b/src/wine-host/utils.cpp @@ -26,8 +26,8 @@ Win32Timer::Win32Timer(HWND window_handle, } Win32Timer::~Win32Timer() { - if (timer_id.has_value()) { - KillTimer(window_handle, timer_id.value()); + if (timer_id) { + KillTimer(window_handle, *timer_id); } }