....
This commit is contained in:
parent
7dabd9394d
commit
2f662b0d79
403
net/adc_netmsg.h
403
net/adc_netmsg.h
@ -13,14 +13,62 @@
|
|||||||
namespace adc
|
namespace adc
|
||||||
{
|
{
|
||||||
|
|
||||||
template <traits::adc_output_char_range ByteStorageT = std::string, traits::adc_char_view ByteViewT = std::string_view>
|
namespace utils {
|
||||||
class AdcNetMessageCommonInterface
|
|
||||||
{
|
|
||||||
virtual bool empty() const { return std::ranges::distance(_bytes.begin(), _bytes.end()) == 0; }
|
|
||||||
|
|
||||||
|
|
||||||
|
template<typename ByteStorageT, typename T, typename... Ts>
|
||||||
|
void convertToBytes(ByteStorageT &res, const T &v, const Ts &...vs)
|
||||||
|
{
|
||||||
|
if constexpr (std::is_array_v<std::remove_cvref_t<T>>) { // to handle with trailing '\0' in plain char array
|
||||||
|
convertToBytes(res, std::string_view(v), vs...);
|
||||||
|
} else {
|
||||||
|
if constexpr (traits::adc_input_char_range<T>) {
|
||||||
|
std::ranges::copy(v, std::back_inserter(res));
|
||||||
|
} else if constexpr (std::convertible_to<T, ByteStorageT>) {
|
||||||
|
convertToBytes(res, static_cast<ByteStorageT>(v), vs...);
|
||||||
|
} else if constexpr (traits::formattable<T>) {
|
||||||
|
std::format_to(std::back_inserter(res), "{}", v);
|
||||||
|
} else {
|
||||||
|
static_assert(false, "UNSUPPORTED TYPE!!!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if constexpr (sizeof...(Ts)) {
|
||||||
|
convertToBytes(res, vs...);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace utils
|
||||||
|
|
||||||
|
|
||||||
|
namespace traits {
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
concept adc_netmessage_c = requires(const T t) { // const methods
|
||||||
|
{ t.empty() } -> std::convertible_to<bool>;
|
||||||
|
{ t.bytes() } -> adc_output_char_range;
|
||||||
|
{ t.byteView() } -> adc_range_of_view_char_range;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace traits
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
Trivial message interface:
|
||||||
|
byte storage: just a char-range
|
||||||
|
*/
|
||||||
|
template<traits::adc_output_char_range ByteStorageT = std::string, traits::adc_char_view ByteViewT = std::string_view>
|
||||||
|
class AdcNetMessageTrivialInterface
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
virtual ~AdcNetMessageTrivialInterface() = default;
|
||||||
|
|
||||||
|
bool empty() const { return std::ranges::distance(_bytes.begin(), _bytes.end()) == 0; }
|
||||||
|
|
||||||
// get a copy of message bytes
|
// get a copy of message bytes
|
||||||
template <traits::adc_output_char_range R>
|
template<traits::adc_output_char_range R>
|
||||||
R bytes() const
|
R bytes() const
|
||||||
{
|
{
|
||||||
R r;
|
R r;
|
||||||
@ -30,10 +78,14 @@ class AdcNetMessageCommonInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
virtual ByteStorageT bytes() const { return bytes<ByteStorageT>(); }
|
virtual ByteStorageT bytes() const
|
||||||
|
{
|
||||||
|
//
|
||||||
|
return bytes<ByteStorageT>();
|
||||||
|
}
|
||||||
|
|
||||||
// get a view of message bytes
|
// get a view of message bytes
|
||||||
template <traits::adc_range_of_view_char_range R>
|
template<traits::adc_range_of_view_char_range R>
|
||||||
R bytesView() const
|
R bytesView() const
|
||||||
{
|
{
|
||||||
R r;
|
R r;
|
||||||
@ -43,63 +95,128 @@ class AdcNetMessageCommonInterface
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual std::vector<ByteViewT> bytesView() const { return bytesView<std::vector<ByteViewT>>(); }
|
std::vector<ByteViewT> bytesView() const
|
||||||
|
{
|
||||||
|
//
|
||||||
|
return bytesView<std::vector<ByteViewT>>();
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
ByteStorageT _bytes;
|
ByteStorageT _bytes;
|
||||||
|
|
||||||
|
AdcNetMessageTrivialInterface() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
AdcNetMessageCommonInterface() = default;
|
|
||||||
|
|
||||||
virtual ~AdcNetMessageCommonInterface() = default;
|
/*
|
||||||
|
interface for more complex messages:
|
||||||
|
byte storage: std::vector of char-range (sequence of buffers)
|
||||||
|
*/
|
||||||
|
template<traits::adc_output_char_range ByteStorageT = std::string, traits::adc_char_view ByteViewT = std::string_view>
|
||||||
|
class AdcNetMessageSeqInterface
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~AdcNetMessageSeqInterface() = default;
|
||||||
|
|
||||||
AdcNetMessageCommonInterface(const AdcNetMessageCommonInterface&) = default;
|
bool empty() const
|
||||||
AdcNetMessageCommonInterface(AdcNetMessageCommonInterface&&) = default;
|
|
||||||
|
|
||||||
AdcNetMessageCommonInterface& operator=(const AdcNetMessageCommonInterface&) = default;
|
|
||||||
AdcNetMessageCommonInterface& operator=(AdcNetMessageCommonInterface&&) = default;
|
|
||||||
|
|
||||||
|
|
||||||
template <typename T, typename... Ts>
|
|
||||||
void convertToBytes(ByteViewT& res, const T& v, const Ts&... vs)
|
|
||||||
{
|
{
|
||||||
if constexpr (std::is_array_v<std::remove_cvref_t<T>>) { // to handle with trailing '\0' in plain char array
|
if (_bytes.empty()) {
|
||||||
convertToBytes(std::string_view(v), vs...);
|
return true;
|
||||||
} else {
|
}
|
||||||
if constexpr (traits::adc_input_char_range<T>) {
|
|
||||||
std::ranges::copy(v, std::back_inserter(res));
|
bool emp = true;
|
||||||
} else if constexpr (std::convertible_to<T, ByteStorageT>) {
|
for (const auto &el : _bytes) {
|
||||||
convertToBytes(static_cast<ByteViewT>(v), vs...);
|
if (std::ranges::distance(el.begin(), el.end())) {
|
||||||
} else if constexpr (traits::formattable<T>) {
|
emp = false;
|
||||||
std::format_to(std::back_inserter(res), "{}", v);
|
break;
|
||||||
} else {
|
|
||||||
static_assert(false, "UNSUPPORTED TYPE!!!");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if constexpr (sizeof...(Ts)) {
|
return emp;
|
||||||
convertToBytes(vs...);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get a copy of message bytes
|
||||||
|
template<traits::adc_output_char_range R>
|
||||||
|
R bytes() const
|
||||||
|
{
|
||||||
|
R r;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < _bytes.size(); ++i) {
|
||||||
|
std::ranges::for_each(storageViewByIndex(i),
|
||||||
|
[&r](const auto &el) { std::ranges::copy(el, std::back_inserter(r)); });
|
||||||
|
}
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
virtual ByteStorageT bytes() const
|
||||||
|
{
|
||||||
|
//
|
||||||
|
return bytes<ByteStorageT>();
|
||||||
|
}
|
||||||
|
|
||||||
|
// get a view of message bytes
|
||||||
|
template<traits::adc_range_of_view_char_range R>
|
||||||
|
R bytesView() const
|
||||||
|
{
|
||||||
|
R r;
|
||||||
|
|
||||||
|
|
||||||
|
for (size_t i = 0; i < _bytes.size(); ++i) {
|
||||||
|
std::ranges::for_each(storageViewByIndex(i), [&r](const auto &el) { r.emplace_back(el.begin(), el.end()); });
|
||||||
|
}
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<ByteViewT> bytesView() const
|
||||||
|
{
|
||||||
|
//
|
||||||
|
return bytesView<std::vector<ByteViewT>>();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::vector<ByteStorageT> _bytes;
|
||||||
|
|
||||||
|
|
||||||
|
AdcNetMessageSeqInterface() = default;
|
||||||
|
|
||||||
|
// return a sequence of storage element view and possible additional
|
||||||
|
// byte views (e.g. elements delimiter)
|
||||||
|
virtual std::vector<ByteViewT> storageViewByIndex(size_t) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Generic message class
|
// Generic message class
|
||||||
template <traits::adc_output_char_range ByteStorageT = std::string, traits::adc_char_view ByteViewT = std::string_view>
|
template <traits::adc_output_char_range ByteStorageT = std::string, traits::adc_char_view ByteViewT = std::string_view>
|
||||||
class AdcGenericNetMessage : public AdcNetMessageCommonInterface<ByteStorageT, ByteViewT>
|
class AdcGenericNetMessage : public AdcNetMessageTrivialInterface<ByteStorageT, ByteViewT>
|
||||||
{
|
{
|
||||||
using base_t = AdcNetMessageCommonInterface<ByteStorageT, ByteViewT>;
|
using base_t = AdcNetMessageTrivialInterface<ByteStorageT, ByteViewT>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using base_t::base_t;
|
using base_t::base_t;
|
||||||
using base_t::bytes;
|
using base_t::bytes;
|
||||||
using base_t::byteView;
|
using base_t::bytesView;
|
||||||
|
using base_t::empty;
|
||||||
|
|
||||||
|
|
||||||
|
template<typename T, typename... Ts>
|
||||||
|
AdcGenericNetMessage(const T &v, const Ts &...vs) : AdcGenericNetMessage()
|
||||||
|
{
|
||||||
|
appendBytes(v, vs...);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename T, typename... Ts>
|
template <typename T, typename... Ts>
|
||||||
void appendBytes(const T& v, const Ts&... vs)
|
void appendBytes(const T& v, const Ts&... vs)
|
||||||
{
|
{
|
||||||
this->convertToBytes(this->_bytes, v, vs...);
|
utils::convertToBytes(this->_bytes, v, vs...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, typename... Ts>
|
template <typename T, typename... Ts>
|
||||||
@ -141,13 +258,17 @@ static constexpr char ADC_DEFAULT_KEY_TOKEN_DELIMITER[] = " ";
|
|||||||
template <const char* TOKEN_DELIM = constants::ADC_DEFAULT_TOKEN_DELIMITER,
|
template <const char* TOKEN_DELIM = constants::ADC_DEFAULT_TOKEN_DELIMITER,
|
||||||
traits::adc_output_char_range ByteStorageT = std::string,
|
traits::adc_output_char_range ByteStorageT = std::string,
|
||||||
traits::adc_char_view ByteViewT = std::string_view>
|
traits::adc_char_view ByteViewT = std::string_view>
|
||||||
class AdcTokenNetMessage : public AdcNetMessageCommonInterface<ByteStorageT, ByteViewT>
|
class AdcTokenNetMessage : public AdcNetMessageSeqInterface<ByteStorageT, ByteViewT>
|
||||||
{
|
{
|
||||||
using base_t = AdcNetMessageCommonInterface<ByteStorageT, ByteViewT>;
|
// AdcNetMessageSeqInterface<ByteStorageT, ByteViewT>::_bytes - tokens
|
||||||
|
using base_t = AdcNetMessageSeqInterface<ByteStorageT, ByteViewT>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static constexpr std::string_view tokenDelimiter{TOKEN_DELIM};
|
static constexpr std::string_view tokenDelimiter{TOKEN_DELIM};
|
||||||
|
|
||||||
|
using base_t::bytesView;
|
||||||
|
using base_t::bytes;
|
||||||
|
using base_t::empty;
|
||||||
|
|
||||||
AdcTokenNetMessage() = default;
|
AdcTokenNetMessage() = default;
|
||||||
|
|
||||||
@ -160,61 +281,24 @@ public:
|
|||||||
virtual ~AdcTokenNetMessage() = default;
|
virtual ~AdcTokenNetMessage() = default;
|
||||||
|
|
||||||
|
|
||||||
virtual bool empty() const
|
|
||||||
{
|
|
||||||
//
|
|
||||||
return _tokens.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <traits::adc_output_char_range R>
|
|
||||||
R bytes() const
|
|
||||||
{
|
|
||||||
R r;
|
|
||||||
|
|
||||||
if (_tokens.empty()) {
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
utils::AdcJoinRange(_tokens, tokenDelimiter, r);
|
|
||||||
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template <traits::adc_range_of_view_char_range R>
|
|
||||||
R bytesView() const
|
|
||||||
{
|
|
||||||
R r;
|
|
||||||
|
|
||||||
if (_tokens.empty()) {
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
utils::AdcReturnRangeElementsView(_tokens, tokenDelimiter, r);
|
|
||||||
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template <std::ranges::output_range<ByteStorageT> R>
|
template <std::ranges::output_range<ByteStorageT> R>
|
||||||
R tokens() const
|
R tokens(size_t start = 0, size_t N = std::numeric_limits<size_t>::max()) const
|
||||||
{
|
{
|
||||||
R r;
|
R r;
|
||||||
|
|
||||||
if (_tokens.empty()) {
|
if (empty()) {
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ranges::copy(_tokens, std::back_inserter(r));
|
std::ranges::copy(this->_bytes | std::views::drop(start) | std::views::take(N), std::back_inserter(r));
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<ByteStorageT> tokens() const
|
std::vector<ByteStorageT> tokens(size_t start = 0, size_t N = std::numeric_limits<size_t>::max()) const
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
return tokens<std::vector<ByteStorageT>>();
|
return tokens<std::vector<ByteStorageT>>(start, N);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -222,7 +306,7 @@ public:
|
|||||||
void appendTokens(const T& v, const Ts&... vs)
|
void appendTokens(const T& v, const Ts&... vs)
|
||||||
{
|
{
|
||||||
if constexpr (traits::adc_input_char_range<T> || traits::formattable<T>) {
|
if constexpr (traits::adc_input_char_range<T> || traits::formattable<T>) {
|
||||||
this->convertToBytes(_tokens.emplace_back(), v);
|
utils::convertToBytes(this->_bytes.emplace_back(), v);
|
||||||
} else if constexpr (std::ranges::input_range<T>) {
|
} else if constexpr (std::ranges::input_range<T>) {
|
||||||
using el_t = std::ranges::range_value_t<T>;
|
using el_t = std::ranges::range_value_t<T>;
|
||||||
static_assert(traits::adc_input_char_range<el_t> || traits::formattable<el_t>,
|
static_assert(traits::adc_input_char_range<el_t> || traits::formattable<el_t>,
|
||||||
@ -230,7 +314,7 @@ public:
|
|||||||
|
|
||||||
if (std::ranges::distance(v.begin(), v.end())) {
|
if (std::ranges::distance(v.begin(), v.end())) {
|
||||||
for (const auto& el : v) {
|
for (const auto& el : v) {
|
||||||
this->convertToBytes(_tokens.emplace_back(), el);
|
utils::convertToBytes(this->_bytes.emplace_back(), el);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,7 +331,7 @@ public:
|
|||||||
template <typename T, typename... Ts>
|
template <typename T, typename... Ts>
|
||||||
void setTokens(const T& v, const Ts&... vs)
|
void setTokens(const T& v, const Ts&... vs)
|
||||||
{
|
{
|
||||||
_tokens.clear();
|
this->_bytes.clear();
|
||||||
|
|
||||||
appendTokens(v, vs...);
|
appendTokens(v, vs...);
|
||||||
}
|
}
|
||||||
@ -261,8 +345,9 @@ public:
|
|||||||
|
|
||||||
do {
|
do {
|
||||||
it = std::search(start_it, end, tokenDelimiter.begin(), tokenDelimiter.end());
|
it = std::search(start_it, end, tokenDelimiter.begin(), tokenDelimiter.end());
|
||||||
std::copy(start_it, it, std::back_inserter(_tokens.emplace_back()));
|
std::copy(start_it, it, std::back_inserter(this->_bytes.emplace_back()));
|
||||||
start_it = it + tokenDelimiter.size();
|
start_it = it;
|
||||||
|
std::advance(start_it, tokenDelimiter.size()); // to support not only random-access iterators
|
||||||
} while (it != end);
|
} while (it != end);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -271,13 +356,24 @@ public:
|
|||||||
void setFromBytes(IT begin, IT end)
|
void setFromBytes(IT begin, IT end)
|
||||||
requires std::same_as<std::iter_value_t<IT>, char>
|
requires std::same_as<std::iter_value_t<IT>, char>
|
||||||
{
|
{
|
||||||
_tokens.clear();
|
this->_bytes.clear();
|
||||||
|
|
||||||
appendFromBytes(begin, end);
|
appendFromBytes(begin, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::vector<ByteStorageT> _tokens;
|
virtual std::vector<ByteViewT> storageViewByIndex(size_t idx) const override
|
||||||
|
{
|
||||||
|
const ByteStorageT &el = this->_bytes[idx];
|
||||||
|
std::vector<ByteViewT> vw{{el.begin(), el.end()}};
|
||||||
|
|
||||||
|
auto last_idx = this->_bytes.size() - 1;
|
||||||
|
if (idx < last_idx) { // add delimiter
|
||||||
|
vw.emplace_back(tokenDelimiter.begin(), tokenDelimiter.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
return vw;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -294,26 +390,40 @@ class AdcKeyTokenNetMessage : public AdcTokenNetMessage<TOKEN_DELIM, ByteStorage
|
|||||||
using base_t = AdcTokenNetMessage<TOKEN_DELIM, ByteStorageT, ByteViewT>;
|
using base_t = AdcTokenNetMessage<TOKEN_DELIM, ByteStorageT, ByteViewT>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static constexpr std::string_view keytokenDelim{KEY_TOKEN_DELIM};
|
static constexpr std::string_view keytokenDelimiter{KEY_TOKEN_DELIM};
|
||||||
|
|
||||||
using base_t::appendTokens;
|
using base_t::appendTokens;
|
||||||
using base_t::setTokens;
|
using base_t::setTokens;
|
||||||
using base_t::tokens;
|
using base_t::tokens;
|
||||||
|
using base_t::appendFromBytes; // append only tokens not keyword
|
||||||
|
using base_t::bytesView;
|
||||||
|
using base_t::bytes;
|
||||||
|
using base_t::empty;
|
||||||
|
|
||||||
|
|
||||||
virtual bool empty() const
|
AdcKeyTokenNetMessage() = default;
|
||||||
|
|
||||||
|
|
||||||
|
template<typename KT, typename T, typename... Ts>
|
||||||
|
AdcKeyTokenNetMessage(const KT &key, const T &tok, const Ts &...toks)
|
||||||
{
|
{
|
||||||
auto N = std::distance(this->_bytes.begin(), this->_bytes.end());
|
utils::convertToBytes(this->_bytes.emplace_back(), key);
|
||||||
|
setTokens(tok, toks...);
|
||||||
return (N == 0) && this->_tokens.empty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
virtual ~AdcKeyTokenNetMessage() = default;
|
||||||
|
|
||||||
template <traits::adc_output_char_range R>
|
template <traits::adc_output_char_range R>
|
||||||
R keyBytes() const
|
R keyBytes() const
|
||||||
{
|
{
|
||||||
R r;
|
R r;
|
||||||
|
|
||||||
std::ranges::copy(this->_bytes, r);
|
if (empty()) {
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ranges::copy(this->_bytes[0], r);
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
@ -321,67 +431,100 @@ public:
|
|||||||
|
|
||||||
ByteStorageT keyBytes() const
|
ByteStorageT keyBytes() const
|
||||||
{
|
{
|
||||||
//
|
if (empty()) {
|
||||||
return this->_bytes;
|
return ByteStorageT();
|
||||||
|
}
|
||||||
|
|
||||||
|
return this->_bytes[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <traits::adc_char_view R>
|
template <traits::adc_char_view R>
|
||||||
R keyView() const
|
R keyView() const
|
||||||
{
|
{
|
||||||
//
|
if (empty()) {
|
||||||
return R{this->_bytes.begin(), this->_bytes.end()};
|
return R();
|
||||||
|
}
|
||||||
|
|
||||||
|
return R{this->_bytes[0].begin(), this->_bytes[0].end()};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ByteViewT keyView() const
|
ByteViewT keyView() const
|
||||||
|
{
|
||||||
|
if (empty()) {
|
||||||
|
return ByteViewT();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ByteViewT{this->_bytes[0].begin(), this->_bytes[0].end()};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<std::ranges::output_range<ByteStorageT> R>
|
||||||
|
R tokens(size_t start = 0, size_t N = std::numeric_limits<size_t>::max()) const
|
||||||
|
{
|
||||||
|
// "+1" since the first element is keyword
|
||||||
|
return base_t::template tokens<R>(start + 1, N);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<ByteStorageT> tokens(size_t start = 0, size_t N = std::numeric_limits<size_t>::max()) const
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
return ByteViewT{this->_bytes.begin(), this->_bytes.end()};
|
return tokens<std::vector<ByteStorageT>>(start, N);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <traits::adc_output_char_range R>
|
template<typename KT, typename T, typename... Ts>
|
||||||
R bytes() const
|
void setKeyTokens(const KT &key, const T &tok, const Ts &...toks)
|
||||||
{
|
{
|
||||||
R r;
|
this->_bytes.clear();
|
||||||
|
|
||||||
if (this->_bytes.empty() && this->_tokens.empty()) {
|
utils::convertToBytes(this->_bytes.emplace_back(), key);
|
||||||
return r;
|
appendTokens(tok, toks...);
|
||||||
}
|
|
||||||
|
|
||||||
std::ranges::copy(this->_bytes, std::back_inserter(r)); // keyword
|
|
||||||
|
|
||||||
if (!this->_tokens.empty()) {
|
|
||||||
std::ranges::copy(keytokenDelim, std::back_inserter(r)); // keyword-to-tokens delimiter
|
|
||||||
utils::AdcJoinRange(this->_tokens, base_t::tokenDelimiter, r);
|
|
||||||
}
|
|
||||||
|
|
||||||
return r;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <traits::adc_range_of_view_char_range R>
|
|
||||||
R bytesView() const
|
template <typename T, typename... Ts>
|
||||||
|
void setTokens(const T& v, const Ts&... vs)
|
||||||
{
|
{
|
||||||
R r;
|
this->_bytes.resize(1); // the first element is keyword
|
||||||
|
|
||||||
if (this->_bytes.empty() && this->_tokens.empty()) {
|
appendTokens(v, vs...);
|
||||||
return r;
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<std::input_iterator IT>
|
||||||
|
void setFromBytes(IT begin, IT end)
|
||||||
|
requires std::same_as<std::iter_value_t<IT>, char>
|
||||||
|
{
|
||||||
|
this->_bytes.clear();
|
||||||
|
|
||||||
|
auto it = std::search(begin, end, keytokenDelimiter.begin(), keytokenDelimiter.end());
|
||||||
|
|
||||||
|
std::copy(begin, it, std::back_inserter(this->_bytes.emplace_back())); // keyword
|
||||||
|
|
||||||
|
if (it != end) {
|
||||||
|
std::advance(it, keytokenDelimiter.size());
|
||||||
|
appendFromBytes(it, end); // tokens
|
||||||
}
|
}
|
||||||
|
|
||||||
std::back_inserter(r) = {this->_bytes.begin(), this->_bytes.end()}; // keyword
|
|
||||||
|
|
||||||
if (!this->_tokens.empty()) {
|
|
||||||
std::back_inserter(r) = {keytokenDelim.begin(), keytokenDelim.end()}; // keyword-to-tokens delimiter
|
|
||||||
utils::AdcReturnRangeElementsView(this->_tokens, base_t::tokenDelimiter, r);
|
|
||||||
}
|
|
||||||
|
|
||||||
return r;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// _bytes: keyword
|
|
||||||
// _tokens: tokens
|
virtual std::vector<ByteViewT> storageViewByIndex(size_t idx) const override
|
||||||
|
{
|
||||||
|
if (idx == 0) { // the first element is keyword
|
||||||
|
const ByteStorageT &el = this->_bytes[idx];
|
||||||
|
std::vector<ByteViewT> vw{{el.begin(), el.end()}};
|
||||||
|
|
||||||
|
if (this->_bytes.size() > 1) { // there are tokens, so add keyword-to-tokens delimiter
|
||||||
|
vw.emplace_back(keytokenDelimiter.begin(), keytokenDelimiter.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
return vw;
|
||||||
|
} else {
|
||||||
|
return base_t::storageViewByIndex(idx);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace adc
|
} // namespace adc
|
||||||
|
|||||||
@ -3,13 +3,65 @@
|
|||||||
#include <doctest/doctest.h>
|
#include <doctest/doctest.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "../common/adc_utils.h"
|
// #include "../common/adc_utils.h"
|
||||||
#include "../net/adc_netmessage.h"
|
// #include "../net/adc_netmessage.h"
|
||||||
|
|
||||||
|
#include "../net/adc_netmsg.h"
|
||||||
|
|
||||||
using namespace adc;
|
using namespace adc;
|
||||||
|
|
||||||
static constexpr char DD[] = "=";
|
static constexpr char DD[] = "=";
|
||||||
|
static constexpr char TD[] = "-";
|
||||||
|
|
||||||
|
|
||||||
|
TEST_CASE("[ADC NET MESSAGE]")
|
||||||
|
{
|
||||||
|
AdcKeyTokenNetMessage<DD> msg;
|
||||||
|
|
||||||
|
std::string_view bytes{"SET=POS 1 2 3 4 5"};
|
||||||
|
|
||||||
|
std::cout << "INPUT BYTES: [" << bytes << "]\n";
|
||||||
|
|
||||||
|
msg.setFromBytes(bytes.begin(), bytes.end());
|
||||||
|
|
||||||
|
auto keyv = msg.keyView();
|
||||||
|
auto keyb = msg.keyBytes();
|
||||||
|
|
||||||
|
std::cout << "KEY VIEW: [" << keyv << "]\n";
|
||||||
|
std::cout << "KEY BYTE: [" << keyb << "]\n";
|
||||||
|
|
||||||
|
auto tks = msg.tokens(1, 3);
|
||||||
|
for (auto& el : tks) {
|
||||||
|
std::cout << "TOKEN: [" << el << "]\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
auto t2 = msg.bytesView();
|
||||||
|
for (auto& el : t2) {
|
||||||
|
std::cout << "BYTES VIEW: [" << el << "]\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::cout << "\n\n---------\n\n";
|
||||||
|
|
||||||
|
|
||||||
|
msg.setTokens("FILTER", "A1", "B3");
|
||||||
|
std::cout << "BYTES: " << msg.bytes() << "\n";
|
||||||
|
|
||||||
|
std::cout << "\n\n---------\n\n";
|
||||||
|
|
||||||
|
|
||||||
|
AdcTokenNetMessage<> tmsg;
|
||||||
|
std::list<char> bb{'1', ' ', '2', ' ', '3', ' ', '4', ' ', '5'};
|
||||||
|
|
||||||
|
tmsg.setFromBytes(bb.begin(), bb.end());
|
||||||
|
|
||||||
|
auto t1 = tmsg.bytesView();
|
||||||
|
for (auto& el : t1) {
|
||||||
|
std::cout << "TOKEN VIEW: [" << el << "]\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
TEST_CASE("[ADC NET MESSAGE]")
|
TEST_CASE("[ADC NET MESSAGE]")
|
||||||
{
|
{
|
||||||
AdcKeyParamNetMessage<DD> msg;
|
AdcKeyParamNetMessage<DD> msg;
|
||||||
@ -73,3 +125,5 @@ TEST_CASE("[ADC NET MESSAGE]")
|
|||||||
std::cout << "\n\nN = " << N << "\n";
|
std::cout << "\n\nN = " << N << "\n";
|
||||||
std::cout << sr << "\n";
|
std::cout << sr << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user