This commit is contained in:
2026-07-28 16:19:34 +03:00
parent 52ae5f4c5a
commit b8ea6ce660
4 changed files with 56 additions and 22 deletions

View File

@@ -84,7 +84,8 @@ int main()
std::println("valueSeq = '{}'", kvl.valueSeq<std::list<std::string>>());
std::println("\nOwning 'key-value'");
snplib_keyvalue_expr_t<std::vector<char>> kv1;
// snplib_keyvalue_expr_t<std::vector<char>> kv1;
snplib_keyvalue_expr_t kv1;
kv1.setKeyValue("KEY", 7.7);
std::println("key = '{}'", kv1.key());

View File

@@ -27,8 +27,8 @@ static constexpr char SNPLIB_DEFAULT_NETMSG_KEYVALUE_DELIM[] = "=";
static constexpr char SNPLIB_DEFAULT_NETMSG_VALUEVALUE_DELIM[] = ",";
template <snplib_char_range_c CharRangeT, snplib_net_proto_c<CharRangeT> ProtoT>
class snplib_netmsg_t : public snplib_keyvalue_expr_t<CharRangeT>, protected ProtoT
template <snplib_char_range_c CharRangeT = std::vector<char>>
class snplib_netmsg_t : public snplib_keyvalue_expr_t<CharRangeT>
{
protected:
using base_t = snplib_keyvalue_expr_t<CharRangeT>;
@@ -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<ProtoT>
: base_t(r, std::move(config)), ProtoT()
: base_t(r, std::move(config))
{
}
~snplib_netmsg_t() = default;
template <snplib_char_range_c R>
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<CharRangeT, std::remove_cvref_t<R>>) {
std::ranges::copy(std::forward<R>(r), std::back_inserter(msg._ownByteSeq));
msg.fromCharRange(msg._ownByteSeq, std::move(config));
} else {
msg.fromCharRange(std::forward<R>(r), std::move(config));
}
return msg;
}
template <snplib_char_range_c OR>
OR toBytes()
OR toBytes() const
{
return ProtoT::template toBytes<OR>(this->_byteSeq);
return snplib_transform_char_range<OR>(this->_byteSeq);
}
std::vector<char> toBytes()
std::vector<char> toBytes() const
{
return toBytes<std::vector<char>>();
}

View File

@@ -39,8 +39,9 @@ concept snplib_net_proto_c = requires(T t) {
};
template <typename T>
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(

View File

@@ -13,7 +13,7 @@ namespace snplib
template <typename T>
concept snplib_serializable_c = requires { snplib_serializer_t<std::remove_cvref_t<T>>{}; };
template <snplib_char_range_c CharRangeT>
template <snplib_char_range_c CharRangeT = std::vector<char>>
requires(std::ranges::viewable_range<CharRangeT> && !std::ranges::borrowed_range<CharRangeT>)
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<CharRangeT, std::remove_cvref_t<decltype(bytes)>> && snplib_owning_range_c<CharRangeT>)
{
if constexpr (requires(CharRangeT t) { t.clear(std::declval<size_t>()); }) {
_ownByteSeq.clear();
} else {
_ownByteSeq = CharRangeT();
}
std::ranges::copy(std::forward<decltype(bytes)>(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 <snplib_serializable_c ValueT, snplib_serialization_params_c SParT = snplib_serialization_params_t>
requires mutableContent
error_t appendValue(ValueT&& value, SParT sparams = snplib_serialization_params_t{})
{
if (_valueSeq.empty()) {
return setValue(std::forward<ValueT>(value), std::move(sparams));
}
}
// template <snplib_serializable_c ValueT, snplib_serialization_params_c SParT = snplib_serialization_params_t>
// requires mutableContent
// error_t appendValue(ValueT&& value, SParT sparams = snplib_serialization_params_t{})
// {
// if (_valueSeq.empty()) {
// return setValue(std::forward<ValueT>(value), std::move(sparams));
// }
// }
template <snplib_serializable_c KeyT,