This commit is contained in:
2026-07-27 17:13:28 +03:00
parent 32ef640e39
commit 40f793930f
4 changed files with 51 additions and 22 deletions

View File

@@ -33,6 +33,17 @@ concept snplib_char_view_c = std::ranges::view<T> && std::same_as<std::ranges::r
template <typename T, typename CharT = char> template <typename T, typename CharT = char>
concept snplib_view_or_output_char_range_c = snplib_char_view_c<T> || snplib_output_char_range_c<T, CharT>; concept snplib_view_or_output_char_range_c = snplib_char_view_c<T> || snplib_output_char_range_c<T, CharT>;
template <typename T, typename CharT = char>
concept snplib_range_of_char_views_or_output_ranges_c =
std::ranges::range<T> && snplib_view_or_output_char_range_c<std::ranges::range_value_t<T>, CharT>;
template <typename T, typename CharT = char>
concept snplib_char_range_or_range_of_char_ranges_c =
snplib_char_range_c<T, CharT> ||
(std::ranges::range<T> && snplib_char_range_c<std::ranges::range_value_t<T>, CharT>);
/* std::chrono related concepts */ /* std::chrono related concepts */
template <typename T> template <typename T>

View File

@@ -1,9 +1,10 @@
#pragma once #pragma once
#include "../utils/snplib_keyvalue_expr.h" #include "../utils/snplib_keyvalue_expr.h"
#include "snplib_network.h"
/* /*
A SIMPLE NETWORK MESSAGE A SIMPLE NETWORK MESSAGE IMPLEMENTATION
format: format:
@@ -26,23 +27,20 @@ static constexpr char SNPLIB_DEFAULT_NETMSG_KEYVALUE_DELIM[] = "=";
static constexpr char SNPLIB_DEFAULT_NETMSG_VALUEVALUE_DELIM[] = ","; static constexpr char SNPLIB_DEFAULT_NETMSG_VALUEVALUE_DELIM[] = ",";
template <snplib_char_range_c ByteSeqT = std::vector<char>> template <snplib_char_range_c CharRangeT, snplib_net_proto_c<CharRangeT> ProtoT>
class snplib_netmsg_t : public snplib_keyvalue_expr_t<ByteSeqT> class snplib_netmsg_t : public snplib_keyvalue_expr_t<CharRangeT>, protected ProtoT
{ {
protected: protected:
using base_t = snplib_keyvalue_expr_t<ByteSeqT>; using base_t = snplib_keyvalue_expr_t<CharRangeT>;
std::conditional_t<base_t::mutableContent, std::vector<char>, void> _byteSeq{};
public: public:
using base_t::config_t; using base_t::config_t;
using base_t::base_t; snplib_netmsg_t(CharRangeT& r,
base_t::config_t config = {.keyval_delim = SNPLIB_DEFAULT_NETMSG_KEYVALUE_DELIM,
snplib_netmsg_t(base_t::config_t config = {.keyval_delim = SNPLIB_DEFAULT_NETMSG_KEYVALUE_DELIM,
.valseq_delim = SNPLIB_DEFAULT_NETMSG_VALUEVALUE_DELIM}) .valseq_delim = SNPLIB_DEFAULT_NETMSG_VALUEVALUE_DELIM})
requires(base_t::mutableContent) requires std::is_default_constructible_v<ProtoT>
: base_t(_byteSeq, std::move(config)) : base_t(r, std::move(config)), ProtoT()
{ {
} }
@@ -58,6 +56,12 @@ public:
template <snplib_char_range_c OR> template <snplib_char_range_c OR>
OR toBytes() OR toBytes()
{ {
return ProtoT::template toBytes<OR>(this->_byteSeq);
}
std::vector<char> toBytes()
{
return toBytes<std::vector<char>>();
} }
}; };

View File

@@ -24,10 +24,23 @@ concept snplib_net_endpoint_c = requires(T t, const T t_const) {
}; };
template <typename T, typename CharRangeT = std::vector<char>>
concept snplib_net_proto_c = requires(T t) {
// from session-level representation to transport-level one
{ t.toBytes(std::declval<CharRangeT>()) } -> snplib_char_range_or_range_of_char_ranges_c;
requires(
requires {
{ t.fromBytes(std::declval<std::span<const char>>()) } -> snplib_char_range_c;
} ||
requires {
{ t.fromBytes(std::declval<std::span<char>>()) } -> snplib_char_range_c;
});
};
template <typename T> template <typename T>
concept snplib_net_message_c = requires(T t) { 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 // can be constructed from contiguous byte range
requires( requires(

View File

@@ -13,14 +13,14 @@ namespace snplib
template <typename T> template <typename T>
concept snplib_serializable_c = requires { snplib_serializer_t<std::remove_cvref_t<T>>{}; }; concept snplib_serializable_c = requires { snplib_serializer_t<std::remove_cvref_t<T>>{}; };
template <snplib_char_range_c ByteSeqT> template <snplib_char_range_c CharRangeT>
requires(std::ranges::viewable_range<ByteSeqT> && !std::ranges::borrowed_range<ByteSeqT>) requires(std::ranges::viewable_range<CharRangeT> && !std::ranges::borrowed_range<CharRangeT>)
class snplib_keyvalue_expr_t class snplib_keyvalue_expr_t
{ {
public: public:
static constexpr bool mutableContent = snplib_output_char_range_c<ByteSeqT>; static constexpr bool mutableContent = snplib_output_char_range_c<CharRangeT>;
static constexpr bool contiguousContent = static constexpr bool contiguousContent =
std::ranges::viewable_range<ByteSeqT> && std::ranges::contiguous_range<ByteSeqT>; std::ranges::viewable_range<CharRangeT> && std::ranges::contiguous_range<CharRangeT>;
typedef std::error_code error_t; typedef std::error_code error_t;
@@ -32,15 +32,15 @@ public:
std::string_view valseq_delim{DEFAULT_VALSEQ_DELIM_SEQ}; 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; virtual ~snplib_keyvalue_expr_t() = default;
// parse from char range // 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; _byteSeq = bytes;
_config = cfg; _config = cfg;
@@ -222,10 +222,11 @@ public:
} }
protected: protected:
ByteSeqT& _byteSeq; CharRangeT& _byteSeq;
std::conditional_t<contiguousContent, std::string_view, std::ranges::subrange<typename ByteSeqT::iterator>> _key{}; std::conditional_t<contiguousContent, std::string_view, std::ranges::subrange<typename CharRangeT::iterator>>
std::conditional_t<contiguousContent, std::string_view, std::ranges::subrange<typename ByteSeqT::iterator>> _key{};
std::conditional_t<contiguousContent, std::string_view, std::ranges::subrange<typename CharRangeT::iterator>>
_value{}; _value{};
std::vector<decltype(_value)> _valueSeq{}; std::vector<decltype(_value)> _valueSeq{};