This commit is contained in:
Timur A. Fatkhullin 2024-05-28 23:41:36 +03:00
parent 3821d965cc
commit 2acb97e973
2 changed files with 46 additions and 22 deletions

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <algorithm>
#include <ranges> #include <ranges>
#include <vector> #include <vector>
@ -49,7 +50,8 @@ size_t AdcSplitCharRangeToTokens(OutR& result, const InR& input, const DelimT& d
vv = input | std::views::split(delim); vv = input | std::views::split(delim);
} }
std::ranges::copy(vv, std::back_inserter(result)); std::ranges::transform(vv, std::back_inserter(result),
[](const auto& el) { return std::string(el.begin(), el.end()); });
// return number of tokens // return number of tokens
return std::ranges::distance(vv.begin(), vv.end()); return std::ranges::distance(vv.begin(), vv.end());
@ -152,7 +154,7 @@ protected:
output_seq_t _outputSequence; output_seq_t _outputSequence;
AdcNetMessageInterface() : _storageSequence(), _outputSequence(){}; AdcNetMessageInterface() : _storageSequence(), _outputSequence() {};
template <typename T, typename... Ts> template <typename T, typename... Ts>
AdcNetMessageInterface(const T& v, const Ts&... vs) : AdcNetMessageInterface() AdcNetMessageInterface(const T& v, const Ts&... vs) : AdcNetMessageInterface()
@ -218,6 +220,12 @@ class AdcTokenNetMessage : protected AdcNetMessageInterface // (hide setBytes/a
public: public:
static constexpr std::string_view tokenDelimiter{TOKEN_DELIM}; static constexpr std::string_view tokenDelimiter{TOKEN_DELIM};
// back to public access
using base_t::bytes;
using base_t::byteSize;
using base_t::messageEmpty;
AdcTokenNetMessage() = default; AdcTokenNetMessage() = default;
template <typename T, typename... Ts> template <typename T, typename... Ts>
@ -290,7 +298,7 @@ public:
} }
auto max_el = _storageSequence.size() - start; auto max_el = _storageSequence.size() - start;
N >= max_el ? max_el * 2 - 1 : N * 2 - 1; N = N >= max_el ? max_el * 2 - 1 : N * 2 - 1;
std::ranges::copy(_outputSequence | std::views::drop(start * 2) | std::views::take(N) | std::views::join, std::ranges::copy(_outputSequence | std::views::drop(start * 2) | std::views::take(N) | std::views::join,
std::back_inserter(r)); std::back_inserter(r));
@ -353,10 +361,18 @@ template <const char* KEY_PARAM_DELIM = constants::ADC_DEFAULT_KEY_PARAM_DELIMIT
const char* PARAM_PARAM_DELIM = constants::ADC_DEFAULT_PARAM_PARAM_DELIMITER> const char* PARAM_PARAM_DELIM = constants::ADC_DEFAULT_PARAM_PARAM_DELIMITER>
class AdcKeyParamNetMessage : protected AdcTokenNetMessage<PARAM_PARAM_DELIM> class AdcKeyParamNetMessage : protected AdcTokenNetMessage<PARAM_PARAM_DELIM>
{ {
using base_t = AdcTokenNetMessage<PARAM_PARAM_DELIM>;
public: public:
static constexpr std::string_view keyparamDelimiter{KEY_PARAM_DELIM}; static constexpr std::string_view keyparamDelimiter{KEY_PARAM_DELIM};
static constexpr std::string_view paramparamDelimiter{PARAM_PARAM_DELIM}; static constexpr std::string_view paramparamDelimiter{PARAM_PARAM_DELIM};
// back to public access
using base_t::bytes;
using base_t::byteSize;
using base_t::messageEmpty;
using base_t::storageSeq;
AdcKeyParamNetMessage() = default; AdcKeyParamNetMessage() = default;
template <typename KeyT, typename... ParamTs> template <typename KeyT, typename... ParamTs>
@ -414,7 +430,8 @@ public:
template <traits::adc_output_char_range R> template <traits::adc_output_char_range R>
void paramsBytes(R& r, size_t start = 0, size_t N = std::numeric_limits<size_t>::max()) const void paramsBytes(R& r, size_t start = 0, size_t N = std::numeric_limits<size_t>::max()) const
{ {
this->tokensBytes(r, start, N); // '+1' since the first 2 elements in _outputSequence are key and key-param delimiter
this->tokensBytes(r, start + 1, N);
} }
@ -429,7 +446,9 @@ public:
setKeyParam(sp); setKeyParam(sp);
} else { } else {
auto N = std::ranges::distance(sp.begin(), found.begin()); auto N = std::ranges::distance(sp.begin(), found.begin());
setKeyParam(sp.first(N), sp.last(sp.size() - N - keyparamDelimiter.size())); setKeyParam(sp.first(N));
this->_outputSequence.emplace_back(keyparamDelimiter); // add key-param delimiter in output sequence
base_t::appendFromBytes(found.begin() + keyparamDelimiter.size(), sp.end());
} }
} }
@ -437,17 +456,6 @@ public:
protected: protected:
std::string _key; std::string _key;
// _outputSequence will store parameters // _outputSequence will store parameters
void updateState() override
{
if (this->_outputSequence.size()) {
this->_outputSequence.emplace_back(paramparamDelimiter); // add param-param delimiter in output sequence
} else {
this->_outputSequence.emplace_back(keyparamDelimiter); // add key-param delimiter in output sequence
}
this->_outputSequence.emplace_back(this->_storageSequence.back());
}
}; };
} // namespace adc } // namespace adc

View File

@ -1,3 +1,4 @@
#include <list>
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h> #include <doctest/doctest.h>
#include <iostream> #include <iostream>
@ -6,21 +7,36 @@
using namespace adc; using namespace adc;
static constexpr char DD[] = "=";
TEST_CASE("[ADC NET MESSAGE]") TEST_CASE("[ADC NET MESSAGE]")
{ {
AdcKeyParamNetMessage<> msg; AdcKeyParamNetMessage<DD> msg;
std::string_view bytes{"SET POS 1 2 3 4 5"}; std::string_view bytes{"SET=POS 1 2 3 4 5"};
std::cout << "BYTES: [" << bytes << "]\n"; std::cout << "INPUT BYTES: [" << bytes << "]\n";
msg.setFromBytes(bytes.begin(), bytes.end()); msg.setFromBytes(bytes.begin(), bytes.end());
std::string key, pars; std::string key, pars, bb;
msg.key(key); msg.key(key);
std::cout << "KEY: [" << key << "]\n"; std::cout << "KEY: [" << key << "]\n";
msg.paramsBytes(pars, 1); msg.paramsBytes(pars);
// msg.paramsBytes(pars);
std::cout << "PARS BYTES: [" << pars << "]\n"; std::cout << "PARS BYTES: [" << pars << "]\n";
pars.clear();
msg.paramsBytes(pars, 2, 3);
std::cout << "PARS BYTES: [" << pars << "]\n";
std::list<std::string> ls;
msg.params(ls, 0, 3);
for (auto& el : ls) {
std::cout << "PAR: [" << el << "]\n";
}
msg.bytes(bb);
std::cout << "MESSAGE: [" << bb << "]\n";
REQUIRE_EQ(bb, bytes);
} }