388 lines
9.6 KiB
C++
388 lines
9.6 KiB
C++
#pragma once
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
|
|
#include "../common/adc_traits.h"
|
|
#include "../common/adc_utils.h"
|
|
|
|
namespace adc
|
|
{
|
|
|
|
template <traits::adc_output_char_range ByteStorageT = std::string, traits::adc_char_view ByteViewT = std::string_view>
|
|
class AdcNetMessageCommonInterface
|
|
{
|
|
virtual bool empty() const { return std::ranges::distance(_bytes.begin(), _bytes.end()) == 0; }
|
|
|
|
|
|
// get a copy of message bytes
|
|
template <traits::adc_output_char_range R>
|
|
R bytes() const
|
|
{
|
|
R r;
|
|
std::ranges::copy(_bytes, 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;
|
|
|
|
r.emplace_back(_bytes.begin(), _bytes.end());
|
|
|
|
return r;
|
|
}
|
|
|
|
virtual std::vector<ByteViewT> bytesView() const { return bytesView<std::vector<ByteViewT>>(); }
|
|
|
|
protected:
|
|
ByteStorageT _bytes;
|
|
|
|
|
|
|
|
AdcNetMessageCommonInterface() = default;
|
|
|
|
virtual ~AdcNetMessageCommonInterface() = default;
|
|
|
|
AdcNetMessageCommonInterface(const AdcNetMessageCommonInterface&) = default;
|
|
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
|
|
convertToBytes(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(static_cast<ByteViewT>(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(vs...);
|
|
}
|
|
}
|
|
};
|
|
|
|
|
|
// Generic message class
|
|
template <traits::adc_output_char_range ByteStorageT = std::string, traits::adc_char_view ByteViewT = std::string_view>
|
|
class AdcGenericNetMessage : public AdcNetMessageCommonInterface<ByteStorageT, ByteViewT>
|
|
{
|
|
using base_t = AdcNetMessageCommonInterface<ByteStorageT, ByteViewT>;
|
|
|
|
public:
|
|
using base_t::base_t;
|
|
using base_t::bytes;
|
|
using base_t::byteView;
|
|
|
|
template <typename T, typename... Ts>
|
|
void appendBytes(const T& v, const Ts&... vs)
|
|
{
|
|
this->convertToBytes(this->_bytes, v, vs...);
|
|
}
|
|
|
|
template <typename T, typename... Ts>
|
|
void setBytes(const T& v, const Ts&... vs)
|
|
{
|
|
this->_bytes = ByteStorageT();
|
|
|
|
appendBytes(v, vs...);
|
|
}
|
|
|
|
|
|
template <std::input_iterator IT>
|
|
void appendFromBytes(IT begin, IT end)
|
|
requires std::same_as<std::iter_value_t<IT>, char>
|
|
{
|
|
std::copy(begin, end, std::back_inserter(this->_bytes));
|
|
}
|
|
|
|
|
|
template <std::input_iterator IT>
|
|
void setFromBytes(IT begin, IT end)
|
|
requires std::same_as<std::iter_value_t<IT>, char>
|
|
{
|
|
this->_bytes = ByteStorageT();
|
|
|
|
appendFromBytes(begin, end);
|
|
}
|
|
};
|
|
|
|
|
|
namespace constants
|
|
{
|
|
static constexpr char ADC_DEFAULT_TOKEN_DELIMITER[] = " ";
|
|
static constexpr char ADC_DEFAULT_KEY_TOKEN_DELIMITER[] = " ";
|
|
|
|
} // namespace constants
|
|
|
|
|
|
template <const char* TOKEN_DELIM = constants::ADC_DEFAULT_TOKEN_DELIMITER,
|
|
traits::adc_output_char_range ByteStorageT = std::string,
|
|
traits::adc_char_view ByteViewT = std::string_view>
|
|
class AdcTokenNetMessage : public AdcNetMessageCommonInterface<ByteStorageT, ByteViewT>
|
|
{
|
|
using base_t = AdcNetMessageCommonInterface<ByteStorageT, ByteViewT>;
|
|
|
|
public:
|
|
static constexpr std::string_view tokenDelimiter{TOKEN_DELIM};
|
|
|
|
|
|
AdcTokenNetMessage() = default;
|
|
|
|
template <typename T, typename... Ts>
|
|
AdcTokenNetMessage(const T& v, const Ts&... vs) : AdcTokenNetMessage()
|
|
{
|
|
setTokens(v, vs...);
|
|
}
|
|
|
|
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>
|
|
R tokens() const
|
|
{
|
|
R r;
|
|
|
|
if (_tokens.empty()) {
|
|
return r;
|
|
}
|
|
|
|
std::ranges::copy(_tokens, std::back_inserter(r));
|
|
|
|
return r;
|
|
}
|
|
|
|
std::vector<ByteStorageT> tokens() const
|
|
{
|
|
//
|
|
return tokens<std::vector<ByteStorageT>>();
|
|
}
|
|
|
|
|
|
template <typename T, typename... Ts>
|
|
void appendTokens(const T& v, const Ts&... vs)
|
|
{
|
|
if constexpr (traits::adc_input_char_range<T> || traits::formattable<T>) {
|
|
this->convertToBytes(_tokens.emplace_back(), v);
|
|
} else if constexpr (std::ranges::input_range<T>) {
|
|
using el_t = std::ranges::range_value_t<T>;
|
|
static_assert(traits::adc_input_char_range<el_t> || traits::formattable<el_t>,
|
|
"INVALID TYPE OF INPUT TOKENS!!!");
|
|
|
|
if (std::ranges::distance(v.begin(), v.end())) {
|
|
for (const auto& el : v) {
|
|
this->convertToBytes(_tokens.emplace_back(), el);
|
|
}
|
|
}
|
|
|
|
} else {
|
|
static_assert(false, "UNSUPPORTED (CANNOT BE SERIALIZED TO BYTES) TYPE OF INPUT TOKENS!!!");
|
|
}
|
|
|
|
if constexpr (sizeof...(Ts)) {
|
|
appendTokens(vs...);
|
|
}
|
|
}
|
|
|
|
|
|
template <typename T, typename... Ts>
|
|
void setTokens(const T& v, const Ts&... vs)
|
|
{
|
|
_tokens.clear();
|
|
|
|
appendTokens(v, vs...);
|
|
}
|
|
|
|
|
|
template <std::input_iterator IT>
|
|
void appendFromBytes(IT begin, IT end)
|
|
requires std::same_as<std::iter_value_t<IT>, char>
|
|
{
|
|
auto it = begin, start_it = begin;
|
|
|
|
do {
|
|
it = std::search(start_it, end, tokenDelimiter.begin(), tokenDelimiter.end());
|
|
std::copy(start_it, it, std::back_inserter(_tokens.emplace_back()));
|
|
start_it = it + tokenDelimiter.size();
|
|
} while (it != end);
|
|
}
|
|
|
|
|
|
template <std::input_iterator IT>
|
|
void setFromBytes(IT begin, IT end)
|
|
requires std::same_as<std::iter_value_t<IT>, char>
|
|
{
|
|
_tokens.clear();
|
|
|
|
appendFromBytes(begin, end);
|
|
}
|
|
|
|
protected:
|
|
std::vector<ByteStorageT> _tokens;
|
|
};
|
|
|
|
|
|
// special type of tokenized message:
|
|
// the first token is keyword
|
|
// the other - just tokens
|
|
// delimiter between keyword and other tokens may differ from token-to-token one
|
|
template <const char* KEY_TOKEN_DELIM = constants::ADC_DEFAULT_KEY_TOKEN_DELIMITER,
|
|
const char* TOKEN_DELIM = constants::ADC_DEFAULT_TOKEN_DELIMITER,
|
|
traits::adc_output_char_range ByteStorageT = std::string,
|
|
traits::adc_char_view ByteViewT = std::string_view>
|
|
class AdcKeyTokenNetMessage : public AdcTokenNetMessage<TOKEN_DELIM, ByteStorageT, ByteViewT>
|
|
{
|
|
using base_t = AdcTokenNetMessage<TOKEN_DELIM, ByteStorageT, ByteViewT>;
|
|
|
|
public:
|
|
static constexpr std::string_view keytokenDelim{KEY_TOKEN_DELIM};
|
|
|
|
using base_t::appendTokens;
|
|
using base_t::setTokens;
|
|
using base_t::tokens;
|
|
|
|
|
|
virtual bool empty() const
|
|
{
|
|
auto N = std::distance(this->_bytes.begin(), this->_bytes.end());
|
|
|
|
return (N == 0) && this->_tokens.empty();
|
|
}
|
|
|
|
template <traits::adc_output_char_range R>
|
|
R keyBytes() const
|
|
{
|
|
R r;
|
|
|
|
std::ranges::copy(this->_bytes, r);
|
|
|
|
return r;
|
|
}
|
|
|
|
|
|
ByteStorageT keyBytes() const
|
|
{
|
|
//
|
|
return this->_bytes;
|
|
}
|
|
|
|
|
|
template <traits::adc_char_view R>
|
|
R keyView() const
|
|
{
|
|
//
|
|
return R{this->_bytes.begin(), this->_bytes.end()};
|
|
}
|
|
|
|
|
|
ByteViewT keyView() const
|
|
{
|
|
//
|
|
return ByteViewT{this->_bytes.begin(), this->_bytes.end()};
|
|
}
|
|
|
|
|
|
template <traits::adc_output_char_range R>
|
|
R bytes() const
|
|
{
|
|
R r;
|
|
|
|
if (this->_bytes.empty() && this->_tokens.empty()) {
|
|
return r;
|
|
}
|
|
|
|
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
|
|
{
|
|
R r;
|
|
|
|
if (this->_bytes.empty() && this->_tokens.empty()) {
|
|
return r;
|
|
}
|
|
|
|
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:
|
|
// _bytes: keyword
|
|
// _tokens: tokens
|
|
};
|
|
|
|
} // namespace adc
|