From b8ea6ce660699f5d7aadaf34a030e50ce0fe32ab Mon Sep 17 00:00:00 2001 From: "Timur A. Fatkhullin" Date: Tue, 28 Jul 2026 16:19:34 +0300 Subject: [PATCH] ... --- examples/str_exam.cpp | 3 +- include/snipplib/network/snplib_netmsg.h | 35 ++++++++++++++----- include/snipplib/network/snplib_network.h | 5 +-- include/snipplib/utils/snplib_keyvalue_expr.h | 35 +++++++++++++------ 4 files changed, 56 insertions(+), 22 deletions(-) diff --git a/examples/str_exam.cpp b/examples/str_exam.cpp index 5ab3f3b..82d41db 100644 --- a/examples/str_exam.cpp +++ b/examples/str_exam.cpp @@ -84,7 +84,8 @@ int main() std::println("valueSeq = '{}'", kvl.valueSeq>()); std::println("\nOwning 'key-value'"); - snplib_keyvalue_expr_t> kv1; + // snplib_keyvalue_expr_t> kv1; + snplib_keyvalue_expr_t kv1; kv1.setKeyValue("KEY", 7.7); std::println("key = '{}'", kv1.key()); diff --git a/include/snipplib/network/snplib_netmsg.h b/include/snipplib/network/snplib_netmsg.h index 5c88331..cc084eb 100644 --- a/include/snipplib/network/snplib_netmsg.h +++ b/include/snipplib/network/snplib_netmsg.h @@ -27,8 +27,8 @@ static constexpr char SNPLIB_DEFAULT_NETMSG_KEYVALUE_DELIM[] = "="; static constexpr char SNPLIB_DEFAULT_NETMSG_VALUEVALUE_DELIM[] = ","; -template ProtoT> -class snplib_netmsg_t : public snplib_keyvalue_expr_t, protected ProtoT +template > +class snplib_netmsg_t : public snplib_keyvalue_expr_t { protected: using base_t = snplib_keyvalue_expr_t; @@ -36,30 +36,47 @@ protected: public: using base_t::config_t; + snplib_netmsg_t(base_t::config_t config = {.keyval_delim = SNPLIB_DEFAULT_NETMSG_KEYVALUE_DELIM, + .valseq_delim = SNPLIB_DEFAULT_NETMSG_VALUEVALUE_DELIM}) + : base_t(std::move(config)) + { + } + 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 std::is_default_constructible_v - : base_t(r, std::move(config)), ProtoT() + : base_t(r, std::move(config)) { } ~snplib_netmsg_t() = default; template - static snplib_netmsg_t fromBytes(R&& r) + static snplib_netmsg_t fromBytes(R&& r, + base_t::config_t config = {.keyval_delim = SNPLIB_DEFAULT_NETMSG_KEYVALUE_DELIM, + .valseq_delim = SNPLIB_DEFAULT_NETMSG_VALUEVALUE_DELIM}) { - return base_t::fromByteSequence(); + snplib_netmsg_t msg; + + // deep copy + if constexpr (std::same_as>) { + std::ranges::copy(std::forward(r), std::back_inserter(msg._ownByteSeq)); + msg.fromCharRange(msg._ownByteSeq, std::move(config)); + } else { + msg.fromCharRange(std::forward(r), std::move(config)); + } + + return msg; } template - OR toBytes() + OR toBytes() const { - return ProtoT::template toBytes(this->_byteSeq); + return snplib_transform_char_range(this->_byteSeq); } - std::vector toBytes() + std::vector toBytes() const { return toBytes>(); } diff --git a/include/snipplib/network/snplib_network.h b/include/snipplib/network/snplib_network.h index 10d8cc3..f5f41b0 100644 --- a/include/snipplib/network/snplib_network.h +++ b/include/snipplib/network/snplib_network.h @@ -39,8 +39,9 @@ concept snplib_net_proto_c = requires(T t) { }; template -concept snplib_net_message_c = requires(T t) { - { t.toBytes() } -> snplib_char_range_or_range_of_char_ranges_c; +concept snplib_net_message_c = requires(const T t_const) { + // { t.toBytes() } -> snplib_char_range_or_range_of_char_ranges_c; + { t_const.toBytes() } -> snplib_char_range_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 8f732ea..1f8d445 100644 --- a/include/snipplib/utils/snplib_keyvalue_expr.h +++ b/include/snipplib/utils/snplib_keyvalue_expr.h @@ -13,7 +13,7 @@ namespace snplib template concept snplib_serializable_c = requires { snplib_serializer_t>{}; }; -template +template > requires(std::ranges::viewable_range && !std::ranges::borrowed_range) class snplib_keyvalue_expr_t { @@ -48,11 +48,26 @@ public: virtual ~snplib_keyvalue_expr_t() = default; + // parse from char range (deep copy of the input bytes) + error_t fromCharRange(snplib_input_char_range_c auto&& bytes, config_t cfg = config_t{}) + requires(!std::same_as> && snplib_owning_range_c) + { + if constexpr (requires(CharRangeT t) { t.clear(std::declval()); }) { + _ownByteSeq.clear(); + } else { + _ownByteSeq = CharRangeT(); + } + + std::ranges::copy(std::forward(bytes), std::back_inserter(_ownByteSeq)); + + return fromCharRange(_ownByteSeq, std::move(cfg)); + } + // parse from char range error_t fromCharRange(CharRangeT& bytes, config_t cfg = config_t{}) { _byteSeq = bytes; - _config = cfg; + _config = std::move(cfg); _isValid = false; @@ -237,14 +252,14 @@ public: return {}; } - template - requires mutableContent - error_t appendValue(ValueT&& value, SParT sparams = snplib_serialization_params_t{}) - { - if (_valueSeq.empty()) { - return setValue(std::forward(value), std::move(sparams)); - } - } + // template + // requires mutableContent + // error_t appendValue(ValueT&& value, SParT sparams = snplib_serialization_params_t{}) + // { + // if (_valueSeq.empty()) { + // return setValue(std::forward(value), std::move(sparams)); + // } + // } template