From 1aacc4bca50d05afd697871535f16c72e15b2ee3 Mon Sep 17 00:00:00 2001 From: "Timur A. Fatkhullin" Date: Thu, 8 Oct 2026 23:53:57 +0300 Subject: [PATCH] ... --- include/snipplib/concepts/snplib_concepts.h | 96 +++++++++++++-------- include/snipplib/network/snplib_netproto.h | 23 +++-- include/snipplib/network/snplib_network.h | 6 +- 3 files changed, 80 insertions(+), 45 deletions(-) diff --git a/include/snipplib/concepts/snplib_concepts.h b/include/snipplib/concepts/snplib_concepts.h index 3edfcff..93f6099 100644 --- a/include/snipplib/concepts/snplib_concepts.h +++ b/include/snipplib/concepts/snplib_concepts.h @@ -19,6 +19,59 @@ concept hashable_c = requires(T t) { { std::hash{}(t) } -> std::convertible_to; }; +/* std::tuple-like types */ + +namespace details +{ + +template +concept get_all_indices_c = requires(T t) { (std::get(t), ...); }; + +template +struct check_indices_t { + static constexpr bool value = false; +}; + +// Specialize to extract the index sequence matching the tuple size +template + requires requires { typename std::tuple_size::type; } +struct check_indices_t { + static constexpr bool value = [](std::index_sequence) { + return get_all_indices_c; + }(std::make_index_sequence::value>{}); +}; + +template +static constexpr bool check_indices_v = check_indices_t::value; + +} // namespace details + +template +concept tuple_like_c = details::check_indices_v>; + +// strictly std::tuple +template +concept tuple_c = requires { + [](std::type_identity>) {}(std::type_identity>{}); +}; + + +template +concept range_of_c = std::ranges::range && std::same_as, ET>; + +template +concept input_range_of_c = std::ranges::input_range && std::same_as, ET>; + +template +concept view_of_c = std::ranges::view && std::same_as, ET>; + +template +concept view_or_output_range_of_c = view_of_c || std::ranges::output_range; + +template +concept range_of_ranges_of_c = std::ranges::range && range_of_c, ET>; + + /* some concepts for various types of char sequence */ template @@ -45,6 +98,13 @@ template concept char_range_or_range_of_char_ranges_c = char_range_c || (std::ranges::range && char_range_c, CharT>); +template +concept char_range_or_tuple_of_char_ranges_c = char_range_c || requires { + [](std::type_identity>) { + static_assert((char_range_c & ...), "Tuple element is not a range of chars!!!"); + }(std::type_identity>()); +}; + template concept pointer_to_char_c = std::is_pointer_v && std::same_as>>; @@ -86,42 +146,6 @@ template concept callable_c = std::is_function_v || (std::is_object_v && requires(T) { &T::operator(); }); -/* std::tuple-like types */ - -namespace details -{ - -template -concept get_all_indices_c = requires(T t) { (std::get(t), ...); }; - -template -struct check_indices_t { - static constexpr bool value = false; -}; - -// Specialize to extract the index sequence matching the tuple size -template - requires requires { typename std::tuple_size::type; } -struct check_indices_t { - static constexpr bool value = [](std::index_sequence) { - return get_all_indices_c; - }(std::make_index_sequence::value>{}); -}; - -template -static constexpr bool check_indices_v = check_indices_t::value; - -} // namespace details - -template -concept tuple_like_c = details::check_indices_v>; - -// strictly std::tuple -template -concept tuple_c = requires { - [](std::type_identity>) {}(std::type_identity>{}); -}; - /* fixed-size and non-resizable ranges */ diff --git a/include/snipplib/network/snplib_netproto.h b/include/snipplib/network/snplib_netproto.h index bb1cfb1..2bbf597 100644 --- a/include/snipplib/network/snplib_netproto.h +++ b/include/snipplib/network/snplib_netproto.h @@ -18,7 +18,8 @@ namespace snplib enum class net_repr_proto_result_flag_e : int { OK, NOT_ENOUGH_DATA, INVALID_DATA }; -template +template + requires(char_range_or_range_of_char_ranges_c || char_range_or_tuple_of_char_ranges_c) struct net_repr_proto_result_t { // enum class result_flag_t : int { OK, NOT_ENOUGH_DATA, INVALID_DATA }; typedef net_repr_proto_result_flag_e result_flag_t; @@ -381,17 +382,23 @@ struct _netproto_binary_blob_t { { using char_t = std::ranges::range_value_t; // using ret_t = net_repr_proto_result_t, 2>>; - using ret_t = net_repr_proto_result_t>; + // using ret_t = net_repr_proto_result_t>; + using ret_t = net_repr_proto_result_t>; - _nbytes.emplace_back(""); + // _nbytes.emplace_back(""); + + // std::format_to(std::back_inserter(_nbytes.back()), std::string_view(BLOB_SIZE_FIELD_FORMAT), + // std::ranges::size(std::forward(message))); - std::format_to(std::back_inserter(_nbytes.back()), std::string_view(BLOB_SIZE_FIELD_FORMAT), - std::ranges::size(std::forward(message))); // return ret_t{.flag = net_repr_proto_result_flag_e::OK, // .result = std::array{std::span{_nbytes.back()}, std::span(message)}}; + // return ret_t{.flag = net_repr_proto_result_flag_e::OK, + // .result = std::array{std::string_view{_nbytes.back()}, std::string_view(message)}}; return ret_t{.flag = net_repr_proto_result_flag_e::OK, - .result = std::array{std::string_view{_nbytes.back()}, std::string_view(message)}}; + .result = std::make_tuple(std::format(std::string_view(BLOB_SIZE_FIELD_FORMAT), + std::ranges::size(std::forward(message))), + std::string_view(message))}; } template @@ -483,8 +490,8 @@ struct _netproto_binary_blob_t { return fromNetwork>>(std::forward(bytes)); } -protected: - std::vector _nbytes{}; + // protected: + // std::vector _nbytes{}; }; static_assert(net_repr_proto_c<_netproto_binary_blob_t<>>, "!!!"); diff --git a/include/snipplib/network/snplib_network.h b/include/snipplib/network/snplib_network.h index 3285d99..311a70e 100644 --- a/include/snipplib/network/snplib_network.h +++ b/include/snipplib/network/snplib_network.h @@ -58,7 +58,11 @@ concept net_repr_proto_result_c = requires(T t) { std::same_as; - char_range_or_range_of_char_ranges_c; + // char_range_or_range_of_char_ranges_c; + + requires( + requires { char_range_or_range_of_char_ranges_c; } || + requires { char_range_or_tuple_of_char_ranges_c; }); }; // template