From 3dfebcda2d685b70d18955b08546456727641e72 Mon Sep 17 00:00:00 2001 From: Yura Sorokin Date: Thu, 18 Jun 2026 00:53:37 +0200 Subject: [PATCH] PBS-20 feature: Update project to use c++23 (part 2) https://perconadev.atlassian.net/browse/PBS-20 More c++23 improvements: * removed 'util::to_underlying()' - use 'std::to_underlying()' instead * using 'std::byteswap()' instead of 'boost::endian::little_to_native()' now. Please note that 'std::move_only_function' still cannot be used as it hasn't been implemented in 'libc++'. Fixed some include header guards names that did not follow naming conventions. Fixed some typos. Fixed some GitHub link references in the source code comments. --- src/binsrv/cout_logger.hpp | 6 ++-- src/binsrv/events/common_header.cpp | 4 +-- .../events/format_description_body_impl.cpp | 3 +- .../events/gtid_tagged_log_body_impl.cpp | 33 ++++++++++--------- src/binsrv/s3_error_helpers_private.cpp | 5 ++- src/binsrv/s3_storage_backend.cpp | 3 +- src/easymysql/connection.cpp | 8 ++--- src/util/byte_span_extractors.hpp | 13 ++++---- src/util/byte_span_inserters.hpp | 10 +++--- src/util/conversion_helpers.hpp | 12 ++----- src/util/redirectable.hpp | 6 ++-- 11 files changed, 50 insertions(+), 53 deletions(-) diff --git a/src/binsrv/cout_logger.hpp b/src/binsrv/cout_logger.hpp index ce22eeb..0d2dae3 100644 --- a/src/binsrv/cout_logger.hpp +++ b/src/binsrv/cout_logger.hpp @@ -13,8 +13,8 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -#ifndef BINSRV_OSTREAM_LOGGER_HPP -#define BINSRV_OSTREAM_LOGGER_HPP +#ifndef BINSRV_COUT_LOGGER_HPP +#define BINSRV_COUT_LOGGER_HPP #include @@ -32,4 +32,4 @@ class [[nodiscard]] cout_logger final : public basic_logger { } // namespace binsrv -#endif // BINSRV_OSTREAM_LOGGER_HPP +#endif // BINSRV_COUT_LOGGER_HPP diff --git a/src/binsrv/events/common_header.cpp b/src/binsrv/events/common_header.cpp index 7e9c05d..64aa904 100644 --- a/src/binsrv/events/common_header.cpp +++ b/src/binsrv/events/common_header.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include @@ -27,7 +28,6 @@ #include "binsrv/events/common_header_view.hpp" #include "util/byte_span_fwd.hpp" -#include "util/conversion_helpers.hpp" #include "util/ctime_timestamp.hpp" #include "util/exception_location_helpers.hpp" @@ -41,7 +41,7 @@ common_header::common_header( : timestamp_(static_cast(timestamp.get_value())), server_id_(server_id), event_size_(event_size), next_event_position_(next_event_position), flags_(flags.get_bits()), - type_code_(util::to_underlying(type_code)) {} + type_code_(std::to_underlying(type_code)) {} common_header::common_header(const common_header_view &view) : timestamp_{view.get_timestamp_raw()}, diff --git a/src/binsrv/events/format_description_body_impl.cpp b/src/binsrv/events/format_description_body_impl.cpp index dac0774..8ce301d 100644 --- a/src/binsrv/events/format_description_body_impl.cpp +++ b/src/binsrv/events/format_description_body_impl.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include @@ -36,7 +37,7 @@ namespace binsrv::events { generic_body_impl::generic_body_impl( checksum_algorithm_type checksum_algorithm) noexcept - : checksum_algorithm_{util::to_underlying(checksum_algorithm)} {} + : checksum_algorithm_{std::to_underlying(checksum_algorithm)} {} generic_body_impl::generic_body_impl( util::const_byte_span portion) { diff --git a/src/binsrv/events/gtid_tagged_log_body_impl.cpp b/src/binsrv/events/gtid_tagged_log_body_impl.cpp index d02f45a..0400ae3 100644 --- a/src/binsrv/events/gtid_tagged_log_body_impl.cpp +++ b/src/binsrv/events/gtid_tagged_log_body_impl.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include @@ -431,71 +432,71 @@ generic_body_impl::calculate_tlv_section_size() std::size_t total{0U}; // field: flags - total += util::calculate_varlen_int_size( - util::to_underlying(field_id_type::flags)); + total += + util::calculate_varlen_int_size(std::to_underlying(field_id_type::flags)); total += util::calculate_varlen_int_size(flags_); // field: uuid (16 separate varlen-encoded bytes, one per UUID byte) total += - util::calculate_varlen_int_size(util::to_underlying(field_id_type::uuid)); + util::calculate_varlen_int_size(std::to_underlying(field_id_type::uuid)); for (auto uuid_byte : uuid_) { - total += util::calculate_varlen_int_size(util::to_underlying(uuid_byte)); + total += util::calculate_varlen_int_size(std::to_underlying(uuid_byte)); } // field: gno total += - util::calculate_varlen_int_size(util::to_underlying(field_id_type::gno)); + util::calculate_varlen_int_size(std::to_underlying(field_id_type::gno)); total += util::calculate_varlen_int_size(gno_); // field: tag (varlen length + raw bytes) total += - util::calculate_varlen_int_size(util::to_underlying(field_id_type::tag)); + util::calculate_varlen_int_size(std::to_underlying(field_id_type::tag)); total += util::calculate_varlen_int_size(std::size(tag_)); total += std::size(tag_); // field: last_committed total += util::calculate_varlen_int_size( - util::to_underlying(field_id_type::last_committed)); + std::to_underlying(field_id_type::last_committed)); total += util::calculate_varlen_int_size(last_committed_); // field: sequence_number total += util::calculate_varlen_int_size( - util::to_underlying(field_id_type::sequence_number)); + std::to_underlying(field_id_type::sequence_number)); total += util::calculate_varlen_int_size(sequence_number_); // field: immediate_commit_timestamp total += util::calculate_varlen_int_size( - util::to_underlying(field_id_type::immediate_commit_timestamp)); + std::to_underlying(field_id_type::immediate_commit_timestamp)); total += util::calculate_varlen_int_size(immediate_commit_timestamp_); // field: original_commit_timestamp (optional) if (has_original_commit_timestamp()) { total += util::calculate_varlen_int_size( - util::to_underlying(field_id_type::original_commit_timestamp)); + std::to_underlying(field_id_type::original_commit_timestamp)); total += util::calculate_varlen_int_size(original_commit_timestamp_); } // field: transaction_length total += util::calculate_varlen_int_size( - util::to_underlying(field_id_type::transaction_length)); + std::to_underlying(field_id_type::transaction_length)); total += util::calculate_varlen_int_size(transaction_length_); // field: immediate_server_version total += util::calculate_varlen_int_size( - util::to_underlying(field_id_type::immediate_server_version)); + std::to_underlying(field_id_type::immediate_server_version)); total += util::calculate_varlen_int_size(immediate_server_version_); // field: original_server_version (optional) if (has_original_server_version()) { total += util::calculate_varlen_int_size( - util::to_underlying(field_id_type::original_server_version)); + std::to_underlying(field_id_type::original_server_version)); total += util::calculate_varlen_int_size(original_server_version_); } // field: commit_group_ticket (optional) if (has_commit_group_ticket()) { total += util::calculate_varlen_int_size( - util::to_underlying(field_id_type::commit_group_ticket)); + std::to_underlying(field_id_type::commit_group_ticket)); total += util::calculate_varlen_int_size(commit_group_ticket_); } @@ -541,7 +542,7 @@ void generic_body_impl::encode_tlv_section_to( util::byte_span &destination) const { const auto emit_field_id{[&destination](field_id_type field_id) { [[maybe_unused]] auto result{util::insert_varlen_int_to_byte_span_checked( - destination, util::to_underlying(field_id))}; + destination, std::to_underlying(field_id))}; }}; // It is OK to ignore the result here as we already ensured @@ -557,7 +558,7 @@ void generic_body_impl::encode_tlv_section_to( emit_field_id(field_id_type::uuid); for (auto uuid_byte : uuid_) { insert_result = util::insert_varlen_int_to_byte_span_checked( - destination, util::to_underlying(uuid_byte)); + destination, std::to_underlying(uuid_byte)); } // field: gno diff --git a/src/binsrv/s3_error_helpers_private.cpp b/src/binsrv/s3_error_helpers_private.cpp index 9ac9cb6..77e999a 100644 --- a/src/binsrv/s3_error_helpers_private.cpp +++ b/src/binsrv/s3_error_helpers_private.cpp @@ -18,12 +18,12 @@ #include #include #include +#include #include #include "binsrv/s3_error.hpp" -#include "util/conversion_helpers.hpp" #include "util/exception_location_helpers.hpp" namespace binsrv { @@ -47,9 +47,8 @@ raise_s3_error_from_outcome(std::string_view user_message, message += ", RequestID "; message += sdk_error.GetRequestId(); message += ", ResponseCode "; - // TODO: in c++23 change to std::to_underlying() auto http_status_code = sdk_error.GetResponseCode(); - message += std::to_string(util::to_underlying(http_status_code)); + message += std::to_string(std::to_underlying(http_status_code)); message += ')'; // default value std::source_location::current() for the 'location' diff --git a/src/binsrv/s3_storage_backend.cpp b/src/binsrv/s3_storage_backend.cpp index 78dfb45..dfeeea7 100644 --- a/src/binsrv/s3_storage_backend.cpp +++ b/src/binsrv/s3_storage_backend.cpp @@ -121,6 +121,7 @@ class s3_storage_backend::aws_context : private aws_context_base { struct endpoint_tag {}; using stream_factory_type = std::function; + // TODO: change to std::move_only_function when libc++ supports it using stream_handler_type = std::function; aws_context(bucket_tag tag, const simple_aws_credentials &credentials, @@ -680,7 +681,7 @@ void s3_storage_backend::init_with_endpoint( if (uri_path_it == std::cend(uri_path)) { util::exception_location().raise( - "endpoint s3 URI path must include at least on segment"); + "endpoint s3 URI path must include at least one segment"); } bucket_ = *uri_path_it; ++uri_path_it; diff --git a/src/easymysql/connection.cpp b/src/easymysql/connection.cpp index fd8140a..00a27d6 100644 --- a/src/easymysql/connection.cpp +++ b/src/easymysql/connection.cpp @@ -183,12 +183,12 @@ class connection::rpl_impl { MYSQL_RPL rpl_; // MYSQL_RPL_SKIP_HEARTBEAT - // https://github.com/mysql/mysql-server/blob/mysql-cluster-8.0.43/include/mysql.h#L366 - // https://github.com/mysql/mysql-server/blob/mysql-cluster-8.4.6/include/mysql.h#L367 + // https://github.com/mysql/mysql-server/blob/mysql-8.0.43/include/mysql.h#L366 + // https://github.com/mysql/mysql-server/blob/mysql-8.4.6/include/mysql.h#L367 // USE_HEARTBEAT_EVENT_V2 - // https://github.com/mysql/mysql-server/blob/mysql-cluster-8.0.43/include/mysql.h#L372 - // https://github.com/mysql/mysql-server/blob/mysql-cluster-8.4.6/include/mysql.h#L373 + // https://github.com/mysql/mysql-server/blob/mysql-8.0.43/include/mysql.h#L372 + // https://github.com/mysql/mysql-server/blob/mysql-8.4.6/include/mysql.h#L373 // Explaining BINLOG_DUMP_NON_BLOCK // https://github.com/mysql/mysql-server/blob/mysql-8.0.43/sql/rpl_constants.h#L45 diff --git a/src/util/byte_span_extractors.hpp b/src/util/byte_span_extractors.hpp index 686a908..488a62e 100644 --- a/src/util/byte_span_extractors.hpp +++ b/src/util/byte_span_extractors.hpp @@ -23,8 +23,7 @@ #include #include #include - -#include +#include #include "util/byte_span_fwd.hpp" #include "util/byte_span_packed_int_constants.hpp" @@ -46,11 +45,13 @@ void extract_fixed_int_from_byte_span( bytes_to_extract); // A fixed-length unsigned integer stores its value in a series of // bytes with the least significant byte first. - // TODO: in c++23 use std::byteswap() - value = boost::endian::little_to_native(value_in_network_format); + if constexpr (std::endian::native == std::endian::little) { + value = value_in_network_format; + } else { + value = std::byteswap(value_in_network_format); + } } else { - // TODO: in c++23 change use std::to_underlying(*std::data(remainder)) - value = static_cast(util::to_underlying(*std::data(remainder))); + value = static_cast(std::to_underlying(*std::data(remainder))); } remainder = remainder.subspan(bytes_to_extract); } diff --git a/src/util/byte_span_inserters.hpp b/src/util/byte_span_inserters.hpp index 469fbb4..0a3a19f 100644 --- a/src/util/byte_span_inserters.hpp +++ b/src/util/byte_span_inserters.hpp @@ -24,8 +24,6 @@ #include #include -#include - #include "util/byte_span_fwd.hpp" #include "util/byte_span_packed_int_constants.hpp" #include "util/conversion_helpers.hpp" @@ -43,8 +41,12 @@ void insert_fixed_int_to_byte_span( if constexpr (sizeof(T) != 1U) { // A fixed-length unsigned integer stores its value in a series of // bytes with the least significant byte first. - // TODO: in c++23 use std::byteswap() - T value_in_network_format{boost::endian::native_to_little(value)}; + T value_in_network_format{}; + if constexpr (std::endian::native == std::endian::little) { + value_in_network_format = value; + } else { + value_in_network_format = std::byteswap(value); + } std::memcpy(std::data(remainder), &value_in_network_format, bytes_to_insert); } else { diff --git a/src/util/conversion_helpers.hpp b/src/util/conversion_helpers.hpp index d2be4b8..ca68432 100644 --- a/src/util/conversion_helpers.hpp +++ b/src/util/conversion_helpers.hpp @@ -18,6 +18,7 @@ #include #include +#include namespace util { @@ -30,15 +31,6 @@ template } } -// TODO: remove this function when switch to c++23 and use std::to_underlying -// instead -template - requires std::is_enum_v -[[nodiscard]] constexpr std::underlying_type_t -to_underlying(EnumType enum_value) noexcept { - return static_cast>(enum_value); -} - template requires std::is_enum_v [[nodiscard]] constexpr EnumType @@ -52,7 +44,7 @@ template sizeof(std::underlying_type_t) <= sizeof(std::size_t)) [[nodiscard]] constexpr std::size_t enum_to_index(EnumType enum_value) noexcept { - return static_cast(to_underlying(enum_value)); + return static_cast(std::to_underlying(enum_value)); } template diff --git a/src/util/redirectable.hpp b/src/util/redirectable.hpp index c0584ee..c5b93c5 100644 --- a/src/util/redirectable.hpp +++ b/src/util/redirectable.hpp @@ -13,8 +13,8 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -#ifndef UTIL_REDIRECTABLE_FWD_HPP -#define UTIL_REDIRECTABLE_FWD_HPP +#ifndef UTIL_REDIRECTABLE_HPP +#define UTIL_REDIRECTABLE_HPP namespace util { @@ -23,4 +23,4 @@ concept redirectable = requires { typename T::redirect_type; }; } // namespace util -#endif // UTIL_REDIRECTABLE_FWD_HPP +#endif // UTIL_REDIRECTABLE_HPP