diff options
| author | Rutger Broekhoff | 2026-08-29 12:01:42 +0200 |
|---|---|---|
| committer | Rutger Broekhoff | 2026-08-29 12:01:42 +0200 |
| commit | 52b3cd46c76fd4be18aeb8422a08a073484b9fad (patch) | |
| tree | b4f23957c096e6bce5369bb94a4e8e23de71761a /server/src/util.cpp | |
| parent | 35878b76049b9c3e752480e9f298b7e2da6fdf1f (diff) | |
| download | routemon-52b3cd46c76fd4be18aeb8422a08a073484b9fad.tar.gz routemon-52b3cd46c76fd4be18aeb8422a08a073484b9fad.zip | |
More module implementation partition units
Diffstat (limited to 'server/src/util.cpp')
| -rw-r--r-- | server/src/util.cpp | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/server/src/util.cpp b/server/src/util.cpp new file mode 100644 index 0000000..54040a6 --- /dev/null +++ b/server/src/util.cpp | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | module routemon:util$impl; | ||
| 2 | |||
| 3 | import :util; | ||
| 4 | |||
| 5 | namespace routemon::util { | ||
| 6 | |||
| 7 | lazy_zstring_view::lazy_zstring_view(lazy_zstring_view const& sv) noexcept | ||
| 8 | : s_{sv.s_}, length_{sv.length_.load(std::memory_order_acquire)} | ||
| 9 | { | ||
| 10 | } | ||
| 11 | |||
| 12 | lazy_zstring_view::lazy_zstring_view(lazy_zstring_view&& sv) noexcept | ||
| 13 | : s_{sv.s_}, length_{sv.length_.load(std::memory_order_acquire)} | ||
| 14 | { | ||
| 15 | } | ||
| 16 | |||
| 17 | auto lazy_zstring_view::operator=(lazy_zstring_view const& rhs) noexcept | ||
| 18 | -> lazy_zstring_view& | ||
| 19 | { | ||
| 20 | if (this != &rhs) | ||
| 21 | { | ||
| 22 | s_ = rhs.s_; | ||
| 23 | length_.store( | ||
| 24 | rhs.length_.load(std::memory_order_acquire), std::memory_order_release); | ||
| 25 | } | ||
| 26 | return *this; | ||
| 27 | } | ||
| 28 | |||
| 29 | auto lazy_zstring_view::operator=(lazy_zstring_view&& rhs) noexcept | ||
| 30 | -> lazy_zstring_view& | ||
| 31 | { | ||
| 32 | return *this = rhs; // use the copy assignment operator | ||
| 33 | } | ||
| 34 | |||
| 35 | auto lazy_zstring_view::length() const noexcept -> std::size_t | ||
| 36 | { | ||
| 37 | if (auto v = length_.load(std::memory_order_acquire); v != unset_length) | ||
| 38 | return v; | ||
| 39 | auto l = std::char_traits<char>::length(s_); | ||
| 40 | length_.store(l, std::memory_order_release); | ||
| 41 | return l; | ||
| 42 | } | ||
| 43 | |||
| 44 | auto lazy_zstring_view::c_str() const noexcept -> char const* { return s_; } | ||
| 45 | |||
| 46 | lazy_zstring_view::operator std::string_view() const noexcept | ||
| 47 | { | ||
| 48 | return std::string_view{s_, length()}; | ||
| 49 | } | ||
| 50 | |||
| 51 | lazy_zstring_view::operator char const*() const noexcept { return s_; } | ||
| 52 | |||
| 53 | auto lazy_zstring_view::operator==(std::string_view sv) const noexcept -> bool | ||
| 54 | { | ||
| 55 | if (auto v = length_.load(std::memory_order_acquire); v != unset_length) | ||
| 56 | if (sv.length() != v) | ||
| 57 | return false; | ||
| 58 | auto res = std::char_traits<char>::compare(s_, sv.data(), sv.length()); | ||
| 59 | if (res != 0) | ||
| 60 | return false; | ||
| 61 | // Strings are equal for sv.length() characters. | ||
| 62 | if (s_[sv.length()] != '\0') | ||
| 63 | return false; | ||
| 64 | // Strings are actually equal, and we have just found out the | ||
| 65 | // length of this string, so we might as well set it. | ||
| 66 | length_.store(sv.length(), std::memory_order_release); | ||
| 67 | return true; | ||
| 68 | } | ||
| 69 | |||
| 70 | auto operator==(zstring_view lhs, zstring_view rhs) -> bool | ||
| 71 | { | ||
| 72 | return std::string_view{lhs} == std::string_view{rhs}; | ||
| 73 | } | ||
| 74 | |||
| 75 | } // namespace routemon::util | ||