From 52b3cd46c76fd4be18aeb8422a08a073484b9fad Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Sat, 29 Aug 2026 12:01:42 +0200 Subject: More module implementation partition units --- server/src/util.cppm | 109 ++++++++++++++------------------------------------- 1 file changed, 30 insertions(+), 79 deletions(-) (limited to 'server/src/util.cppm') diff --git a/server/src/util.cppm b/server/src/util.cppm index bdccf10..2857fe5 100644 --- a/server/src/util.cppm +++ b/server/src/util.cppm @@ -13,7 +13,7 @@ struct overloaded : Ts... using Ts::operator()...; }; -export constexpr auto parse_double( +constexpr auto parse_double( std::string_view s, std::chars_format fmt = std::chars_format::general) noexcept -> std::optional @@ -30,7 +30,7 @@ export constexpr auto parse_double( } } -export constexpr auto parse_float( +constexpr auto parse_float( std::string_view s, std::chars_format fmt = std::chars_format::general) noexcept -> std::optional @@ -47,7 +47,7 @@ export constexpr auto parse_float( } } -export template +template class aolist : public std::enable_shared_from_this> { T v_; @@ -72,7 +72,7 @@ public: auto value() const noexcept -> T const& { return v_; } }; -export constexpr auto size_from_int(int x) -> std::optional +constexpr auto size_from_int(int x) -> std::optional { static_assert( sizeof(int) <= sizeof(std::size_t), @@ -82,7 +82,7 @@ export constexpr auto size_from_int(int x) -> std::optional return static_cast(x); } -export constexpr auto int_from_size(std::size_t x) -> std::optional +constexpr auto int_from_size(std::size_t x) -> std::optional { constexpr auto int_max = size_from_int(std::numeric_limits::max()); static_assert(int_max.has_value()); @@ -91,7 +91,7 @@ export constexpr auto int_from_size(std::size_t x) -> std::optional return static_cast(x); } -export class zstring_view +class zstring_view { char const* s_; std::size_t length_; @@ -107,13 +107,16 @@ public: { } - auto length() const -> std::size_t { return length_; } + constexpr auto length() const -> std::size_t { return length_; } - auto c_str() const -> char const* { return s_; } + constexpr auto c_str() const -> char const* { return s_; } - operator std::string_view() const { return std::string_view{s_, length_}; } + constexpr operator std::string_view() const + { + return std::string_view{s_, length_}; + } - operator char const*() const { return s_; } + constexpr operator char const*() const { return s_; } }; // View for null-terminated strings for which we might not @@ -123,7 +126,7 @@ public: // // It is undefined behavior to assign to a lazy_zstring_view when // it is in use by other threads. -export class lazy_zstring_view +class lazy_zstring_view { static constexpr auto unset_length = std::numeric_limits::max(); @@ -136,70 +139,21 @@ public: : s_{s}, length_{s ? unset_length : 0} { } + lazy_zstring_view(lazy_zstring_view const& sv) noexcept; + lazy_zstring_view(lazy_zstring_view&& sv) noexcept; ~lazy_zstring_view() = default; - lazy_zstring_view(lazy_zstring_view const& sv) noexcept - : s_{sv.s_}, length_{sv.length_.load(std::memory_order_acquire)} - { - } - - lazy_zstring_view(lazy_zstring_view&& sv) noexcept - : s_{sv.s_}, length_{sv.length_.load(std::memory_order_acquire)} - { - } - - auto operator=(lazy_zstring_view const& rhs) noexcept -> lazy_zstring_view& - { - if (this != &rhs) - { - s_ = rhs.s_; - length_.store( - rhs.length_.load(std::memory_order_acquire), - std::memory_order_release); - } - return *this; - } - - auto operator=(lazy_zstring_view&& rhs) noexcept -> lazy_zstring_view& - { - return *this = rhs; // use the copy assignment operator - } - - auto length() const noexcept -> std::size_t - { - if (auto v = length_.load(std::memory_order_acquire); v != unset_length) - return v; - auto l = std::char_traits::length(s_); - length_.store(l, std::memory_order_release); - return l; - } - - auto c_str() const noexcept -> char const* { return s_; } + auto operator=(lazy_zstring_view const& rhs) noexcept -> lazy_zstring_view&; + auto operator=(lazy_zstring_view&& rhs) noexcept -> lazy_zstring_view&; - operator std::string_view() const noexcept - { - return std::string_view{s_, length()}; - } + auto length() const noexcept -> std::size_t; + auto c_str() const noexcept -> char const*; - operator char const*() const noexcept { return s_; } + operator std::string_view() const noexcept; + operator char const*() const noexcept; - auto operator==(std::string_view sv) const noexcept -> bool - { - if (auto v = length_.load(std::memory_order_acquire); v != unset_length) - if (sv.length() != v) - return false; - auto res = std::char_traits::compare(s_, sv.data(), sv.length()); - if (res != 0) - return false; - // Strings are equal for sv.length() characters. - if (s_[sv.length()] != '\0') - return false; - // Strings are actually equal, and we have just found out the - // length of this string, so we might as well set it. - length_.store(sv.length(), std::memory_order_release); - return true; - } + auto operator==(std::string_view sv) const noexcept -> bool; }; constexpr auto operator""_zsv(char const* s, std::size_t length) noexcept @@ -208,12 +162,9 @@ constexpr auto operator""_zsv(char const* s, std::size_t length) noexcept return zstring_view{s, length}; } -auto operator==(zstring_view lhs, zstring_view rhs) -> bool -{ - return std::string_view{lhs} == std::string_view{rhs}; -} +auto operator==(zstring_view lhs, zstring_view rhs) -> bool; -export constexpr auto split_on(std::string_view s, char c) +constexpr auto split_on(std::string_view s, char c) -> std::pair> { if (auto i = s.find(c); i != std::string_view::npos) @@ -221,10 +172,10 @@ export constexpr auto split_on(std::string_view s, char c) return std::make_pair(s, std::nullopt); } -export template +template class not_null; -export template +template class not_null { T* p_; @@ -264,10 +215,10 @@ public: auto operator->() const noexcept -> T* { return p_; } }; -export template +template explicit not_null(T*) -> not_null; -export template <> +template <> class not_null { lazy_zstring_view s_; @@ -285,6 +236,6 @@ public: operator std::string_view() const noexcept { return s_; } operator char const*() const noexcept { return s_; } }; -export explicit not_null(lazy_zstring_view s) -> not_null; +explicit not_null(lazy_zstring_view s) -> not_null; } // namespace routemon::util -- cgit v1.3