...
This commit is contained in:
@@ -84,7 +84,8 @@ int main()
|
|||||||
std::println("valueSeq = '{}'", kvl.valueSeq<std::list<std::string>>());
|
std::println("valueSeq = '{}'", kvl.valueSeq<std::list<std::string>>());
|
||||||
|
|
||||||
std::println("\nOwning 'key-value'");
|
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);
|
kv1.setKeyValue("KEY", 7.7);
|
||||||
std::println("key = '{}'", kv1.key());
|
std::println("key = '{}'", kv1.key());
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ 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 CharRangeT, snplib_net_proto_c<CharRangeT> ProtoT>
|
template <snplib_char_range_c CharRangeT = std::vector<char>>
|
||||||
class snplib_netmsg_t : public snplib_keyvalue_expr_t<CharRangeT>, protected ProtoT
|
class snplib_netmsg_t : public snplib_keyvalue_expr_t<CharRangeT>
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
using base_t = snplib_keyvalue_expr_t<CharRangeT>;
|
using base_t = snplib_keyvalue_expr_t<CharRangeT>;
|
||||||
@@ -36,30 +36,47 @@ protected:
|
|||||||
public:
|
public:
|
||||||
using base_t::config_t;
|
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,
|
snplib_netmsg_t(CharRangeT& r,
|
||||||
base_t::config_t config = {.keyval_delim = SNPLIB_DEFAULT_NETMSG_KEYVALUE_DELIM,
|
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 std::is_default_constructible_v<ProtoT>
|
: base_t(r, std::move(config))
|
||||||
: base_t(r, std::move(config)), ProtoT()
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
~snplib_netmsg_t() = default;
|
~snplib_netmsg_t() = default;
|
||||||
|
|
||||||
template <snplib_char_range_c R>
|
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>
|
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>>();
|
return toBytes<std::vector<char>>();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,8 +39,9 @@ concept snplib_net_proto_c = requires(T t) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
concept snplib_net_message_c = requires(T t) {
|
concept snplib_net_message_c = requires(const T t_const) {
|
||||||
{ t.toBytes() } -> snplib_char_range_or_range_of_char_ranges_c;
|
// { 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
|
// can be constructed from contiguous byte range
|
||||||
requires(
|
requires(
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ 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 CharRangeT>
|
template <snplib_char_range_c CharRangeT = std::vector<char>>
|
||||||
requires(std::ranges::viewable_range<CharRangeT> && !std::ranges::borrowed_range<CharRangeT>)
|
requires(std::ranges::viewable_range<CharRangeT> && !std::ranges::borrowed_range<CharRangeT>)
|
||||||
class snplib_keyvalue_expr_t
|
class snplib_keyvalue_expr_t
|
||||||
{
|
{
|
||||||
@@ -48,11 +48,26 @@ public:
|
|||||||
|
|
||||||
virtual ~snplib_keyvalue_expr_t() = default;
|
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
|
// parse from char range
|
||||||
error_t fromCharRange(CharRangeT& bytes, config_t cfg = config_t{})
|
error_t fromCharRange(CharRangeT& bytes, config_t cfg = config_t{})
|
||||||
{
|
{
|
||||||
_byteSeq = bytes;
|
_byteSeq = bytes;
|
||||||
_config = cfg;
|
_config = std::move(cfg);
|
||||||
|
|
||||||
_isValid = false;
|
_isValid = false;
|
||||||
|
|
||||||
@@ -237,14 +252,14 @@ public:
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
template <snplib_serializable_c ValueT, snplib_serialization_params_c SParT = snplib_serialization_params_t>
|
// template <snplib_serializable_c ValueT, snplib_serialization_params_c SParT = snplib_serialization_params_t>
|
||||||
requires mutableContent
|
// requires mutableContent
|
||||||
error_t appendValue(ValueT&& value, SParT sparams = snplib_serialization_params_t{})
|
// error_t appendValue(ValueT&& value, SParT sparams = snplib_serialization_params_t{})
|
||||||
{
|
// {
|
||||||
if (_valueSeq.empty()) {
|
// if (_valueSeq.empty()) {
|
||||||
return setValue(std::forward<ValueT>(value), std::move(sparams));
|
// return setValue(std::forward<ValueT>(value), std::move(sparams));
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
template <snplib_serializable_c KeyT,
|
template <snplib_serializable_c KeyT,
|
||||||
|
|||||||
Reference in New Issue
Block a user