From 40f793930fb4509822626c5805801171887378ad Mon Sep 17 00:00:00 2001 From: "Timur A. Fatkhullin" Date: Mon, 27 Jul 2026 17:13:28 +0300 Subject: [PATCH] ... --- include/snipplib/concepts/snplib_concepts.h | 11 ++++++++ include/snipplib/network/snplib_netmsg.h | 26 +++++++++++-------- include/snipplib/network/snplib_network.h | 15 ++++++++++- include/snipplib/utils/snplib_keyvalue_expr.h | 21 ++++++++------- 4 files changed, 51 insertions(+), 22 deletions(-) diff --git a/include/snipplib/concepts/snplib_concepts.h b/include/snipplib/concepts/snplib_concepts.h index 42a7c29..7d70c20 100644 --- a/include/snipplib/concepts/snplib_concepts.h +++ b/include/snipplib/concepts/snplib_concepts.h @@ -33,6 +33,17 @@ concept snplib_char_view_c = std::ranges::view && std::same_as concept snplib_view_or_output_char_range_c = snplib_char_view_c || snplib_output_char_range_c; +template +concept snplib_range_of_char_views_or_output_ranges_c = + std::ranges::range && snplib_view_or_output_char_range_c, CharT>; + + +template +concept snplib_char_range_or_range_of_char_ranges_c = + snplib_char_range_c || + (std::ranges::range && snplib_char_range_c, CharT>); + + /* std::chrono related concepts */ template diff --git a/include/snipplib/network/snplib_netmsg.h b/include/snipplib/network/snplib_netmsg.h index cd5996e..5c88331 100644 --- a/include/snipplib/network/snplib_netmsg.h +++ b/include/snipplib/network/snplib_netmsg.h @@ -1,9 +1,10 @@ #pragma once #include "../utils/snplib_keyvalue_expr.h" +#include "snplib_network.h" /* - A SIMPLE NETWORK MESSAGE + A SIMPLE NETWORK MESSAGE IMPLEMENTATION format: @@ -26,23 +27,20 @@ static constexpr char SNPLIB_DEFAULT_NETMSG_KEYVALUE_DELIM[] = "="; static constexpr char SNPLIB_DEFAULT_NETMSG_VALUEVALUE_DELIM[] = ","; -template > -class snplib_netmsg_t : public snplib_keyvalue_expr_t +template ProtoT> +class snplib_netmsg_t : public snplib_keyvalue_expr_t, protected ProtoT { protected: - using base_t = snplib_keyvalue_expr_t; - - std::conditional_t, void> _byteSeq{}; + using base_t = snplib_keyvalue_expr_t; public: using base_t::config_t; - using base_t::base_t; - - snplib_netmsg_t(base_t::config_t config = {.keyval_delim = SNPLIB_DEFAULT_NETMSG_KEYVALUE_DELIM, + snplib_netmsg_t(CharRangeT& r, + base_t::config_t config = {.keyval_delim = SNPLIB_DEFAULT_NETMSG_KEYVALUE_DELIM, .valseq_delim = SNPLIB_DEFAULT_NETMSG_VALUEVALUE_DELIM}) - requires(base_t::mutableContent) - : base_t(_byteSeq, std::move(config)) + requires std::is_default_constructible_v + : base_t(r, std::move(config)), ProtoT() { } @@ -58,6 +56,12 @@ public: template OR toBytes() { + return ProtoT::template toBytes(this->_byteSeq); + } + + std::vector toBytes() + { + return toBytes>(); } }; diff --git a/include/snipplib/network/snplib_network.h b/include/snipplib/network/snplib_network.h index 5bbc27c..10d8cc3 100644 --- a/include/snipplib/network/snplib_network.h +++ b/include/snipplib/network/snplib_network.h @@ -24,10 +24,23 @@ concept snplib_net_endpoint_c = requires(T t, const T t_const) { }; +template > +concept snplib_net_proto_c = requires(T t) { + // from session-level representation to transport-level one + { t.toBytes(std::declval()) } -> snplib_char_range_or_range_of_char_ranges_c; + + requires( + requires { + { t.fromBytes(std::declval>()) } -> snplib_char_range_c; + } || + requires { + { t.fromBytes(std::declval>()) } -> snplib_char_range_c; + }); +}; template concept snplib_net_message_c = requires(T t) { - { t.toBytes() } -> snplib_output_char_range_c; + { t.toBytes() } -> snplib_char_range_or_range_of_char_ranges_c; // can be constructed from contiguous byte range requires( diff --git a/include/snipplib/utils/snplib_keyvalue_expr.h b/include/snipplib/utils/snplib_keyvalue_expr.h index dceacc8..2ea57e7 100644 --- a/include/snipplib/utils/snplib_keyvalue_expr.h +++ b/include/snipplib/utils/snplib_keyvalue_expr.h @@ -13,14 +13,14 @@ namespace snplib template concept snplib_serializable_c = requires { snplib_serializer_t>{}; }; -template - requires(std::ranges::viewable_range && !std::ranges::borrowed_range) +template + requires(std::ranges::viewable_range && !std::ranges::borrowed_range) class snplib_keyvalue_expr_t { public: - static constexpr bool mutableContent = snplib_output_char_range_c; + static constexpr bool mutableContent = snplib_output_char_range_c; static constexpr bool contiguousContent = - std::ranges::viewable_range && std::ranges::contiguous_range; + std::ranges::viewable_range && std::ranges::contiguous_range; typedef std::error_code error_t; @@ -32,15 +32,15 @@ public: std::string_view valseq_delim{DEFAULT_VALSEQ_DELIM_SEQ}; }; - constexpr snplib_keyvalue_expr_t(ByteSeqT& bytes, config_t cfg = config_t{}) : _byteSeq(bytes), _config(cfg) + constexpr snplib_keyvalue_expr_t(CharRangeT& bytes, config_t cfg = config_t{}) : _byteSeq(bytes), _config(cfg) { - fromByteSequence(bytes, cfg); + fromCharRange(bytes, cfg); } virtual ~snplib_keyvalue_expr_t() = default; // parse from char range - error_t fromByteSequence(ByteSeqT& bytes, config_t cfg = config_t{}) + error_t fromCharRange(CharRangeT& bytes, config_t cfg = config_t{}) { _byteSeq = bytes; _config = cfg; @@ -222,10 +222,11 @@ public: } protected: - ByteSeqT& _byteSeq; + CharRangeT& _byteSeq; - std::conditional_t> _key{}; - std::conditional_t> + std::conditional_t> + _key{}; + std::conditional_t> _value{}; std::vector _valueSeq{};