From 405a3100e13aaa82da7b8068c68b87203708f3b6 Mon Sep 17 00:00:00 2001 From: sha512sum Date: Tue, 29 Oct 2024 12:17:08 +0000 Subject: [PATCH] Add utils::RangeToWrapper --- library/include/larra/stream_error.hpp | 36 +++++++++++--------------- library/include/larra/utils.hpp | 28 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/library/include/larra/stream_error.hpp b/library/include/larra/stream_error.hpp index 8f1b500..4a6615c 100644 --- a/library/include/larra/stream_error.hpp +++ b/library/include/larra/stream_error.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include #include @@ -23,20 +23,18 @@ constexpr auto ToKebabCaseName() -> std::string_view { static constexpr auto rawStr = nameof::nameof_short_type(); constexpr auto str = [] { - return rawStr | std::views::transform([](auto ch) { - return impl::IsUpper(ch) ? std::array{'-', impl::ToLower(ch)} : std::array{ch, '\0'}; - }) | - std::views::join | std::views::filter([](char ch) { - return ch != '\0'; - }); + return rawStr // + | std::views::transform([](auto ch) { + return impl::IsUpper(ch) ? std::array{'-', impl::ToLower(ch)} : std::array{ch, '\0'}; + }) // + | std::views::join // + | std::views::filter([](char ch) { + return ch != '\0'; + }) // + | std::views::drop(1); }; - constexpr auto size = std::ranges::distance(str()) - 1; - static constexpr auto arr = [&] { - std::array response; // NOLINT - std::ranges::copy(str() | std::views::drop(1), response.begin()); - return response; - }(); - return {arr.data(), size}; + static constexpr auto arr = str() | std::ranges::to>>(); + return {arr.data(), arr.size()}; } } // namespace impl @@ -53,15 +51,11 @@ struct ErrorImpl : BaseError { static inline const auto kKebabCaseName = static_cast(impl::ToKebabCaseName()); static constexpr auto kErrorMessage = [] -> std::string_view { + static constexpr auto name = nameof::nameof_short_type(); static constexpr auto str = [] { - return std::array{std::string_view{"Stream Error: "}, nameof::nameof_short_type(), std::string_view{"\0", 1}} | std::views::join; + return std::array{std::string_view{"Stream Error: "}, std::string_view{name}, std::string_view{"\0", 1}} | std::views::join; }; - constexpr auto size = std::ranges::distance(str()); - static constexpr auto array = [&] { - std::array response; // NOLINT - std::ranges::copy(str(), response.begin()); - return response; - }(); + static constexpr auto array = str() | std::ranges::to>>(); return {array.data(), array.size() - 1}; }(); static constexpr auto TryParse(xmlpp::Element* element) -> std::optional { diff --git a/library/include/larra/utils.hpp b/library/include/larra/utils.hpp index bdd7574..1fb05a5 100644 --- a/library/include/larra/utils.hpp +++ b/library/include/larra/utils.hpp @@ -267,4 +267,32 @@ inline auto StartLifetimeAs(const void* ptr) -> const T* { #endif +// Example: "Hello" | std::ranges::to>>() +template +struct RangeToWrapper : T { + [[nodiscard]] constexpr auto Base() & -> decltype(auto) { + return static_cast(*this); + } + [[nodiscard]] constexpr auto Base() const& -> decltype(auto) { + return static_cast(*this); + } + [[nodiscard]] constexpr auto Base() && -> decltype(auto) { + return static_cast(*this); + } + [[nodiscard]] constexpr auto Base() const&& -> decltype(auto) { + return static_cast(*this); + } + template // NOLINTNEXTLINE + constexpr RangeToWrapper(std::from_range_t, Range&& range) { + auto result = std::ranges::copy(range, this->begin()); + if(result.in != std::ranges::end(range) || result.out != std::ranges::end(*this)) { + throw std::invalid_argument{"Invalid range size"}; + } + } + template // NOLINTNEXTLINE + constexpr RangeToWrapper(Args&&... args) + requires requires { T{std::forward(args)...}; } + : T{std::forward(args)...} {}; +}; + } // namespace larra::xmpp::utils