From 2acb97e9735d37cdeaf99a980210a5277cc522de Mon Sep 17 00:00:00 2001 From: "Timur A. Fatkhullin" Date: Tue, 28 May 2024 23:41:36 +0300 Subject: [PATCH] ... --- net/adc_netmessage.h | 40 +++++++++++++++++++++++---------------- tests/adc_netmsg_test.cpp | 28 +++++++++++++++++++++------ 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/net/adc_netmessage.h b/net/adc_netmessage.h index 0af6076..837d84d 100644 --- a/net/adc_netmessage.h +++ b/net/adc_netmessage.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -49,7 +50,8 @@ size_t AdcSplitCharRangeToTokens(OutR& result, const InR& input, const DelimT& d 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 std::ranges::distance(vv.begin(), vv.end()); @@ -152,7 +154,7 @@ protected: output_seq_t _outputSequence; - AdcNetMessageInterface() : _storageSequence(), _outputSequence(){}; + AdcNetMessageInterface() : _storageSequence(), _outputSequence() {}; template AdcNetMessageInterface(const T& v, const Ts&... vs) : AdcNetMessageInterface() @@ -218,6 +220,12 @@ class AdcTokenNetMessage : protected AdcNetMessageInterface // (hide setBytes/a public: 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; template @@ -290,7 +298,7 @@ public: } 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::back_inserter(r)); @@ -353,10 +361,18 @@ template class AdcKeyParamNetMessage : protected AdcTokenNetMessage { + using base_t = AdcTokenNetMessage; + public: static constexpr std::string_view keyparamDelimiter{KEY_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; template @@ -414,7 +430,8 @@ public: template void paramsBytes(R& r, size_t start = 0, size_t N = std::numeric_limits::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); } else { 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: std::string _key; // _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 diff --git a/tests/adc_netmsg_test.cpp b/tests/adc_netmsg_test.cpp index 5a85bbc..379d208 100644 --- a/tests/adc_netmsg_test.cpp +++ b/tests/adc_netmsg_test.cpp @@ -1,3 +1,4 @@ +#include #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #include #include @@ -6,21 +7,36 @@ using namespace adc; +static constexpr char DD[] = "="; + TEST_CASE("[ADC NET MESSAGE]") { - AdcKeyParamNetMessage<> msg; + AdcKeyParamNetMessage
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()); - std::string key, pars; + std::string key, pars, bb; msg.key(key); std::cout << "KEY: [" << key << "]\n"; - msg.paramsBytes(pars, 1); - // msg.paramsBytes(pars); + msg.paramsBytes(pars); std::cout << "PARS BYTES: [" << pars << "]\n"; + pars.clear(); + msg.paramsBytes(pars, 2, 3); + std::cout << "PARS BYTES: [" << pars << "]\n"; + + std::list 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); }