From 3821d965cce5c4bbc5c25149ccc17f2ab9f7bbde Mon Sep 17 00:00:00 2001 From: "Timur A. Fatkhullin" Date: Tue, 28 May 2024 11:59:56 +0300 Subject: [PATCH] ... --- CMakeLists.txt | 4 ++++ net/adc_netmessage.h | 27 ++++++++++++++++++++++++--- tests/adc_netmsg_test.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 tests/adc_netmsg_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 896298b..72f69e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,6 +36,9 @@ if (BUILD_TESTS) set(DEVATTR_TEST_APP adc_devattr_test) add_executable(${DEVATTR_TEST_APP} tests/adc_devattr_test.cpp) + set(NETMSG_TEST_APP adc_netmsg_test) + add_executable(${NETMSG_TEST_APP} tests/adc_netmsg_test.cpp) + if (NOT doctest_FOUND) include(FetchContent) FetchContent_Declare( @@ -51,6 +54,7 @@ if (BUILD_TESTS) include(CTest) # add_test(VALUE_HOLDER ${VALUEHOLDER_TEST_APP}) add_test(VALUE_HOLDER ${DEVATTR_TEST_APP}) + add_test(NETMSG_TEST ${NETMSG_TEST_APP}) enable_testing() endif(BUILD_TESTS) diff --git a/net/adc_netmessage.h b/net/adc_netmessage.h index c617559..0af6076 100644 --- a/net/adc_netmessage.h +++ b/net/adc_netmessage.h @@ -1,9 +1,9 @@ #pragma once #include +#include #include "../common/adc_traits.h" -#include "../common/adc_utils.h" /* @@ -152,7 +152,7 @@ protected: output_seq_t _outputSequence; - AdcNetMessageInterface() : _storageSequence(), _outputSequence() {}; + AdcNetMessageInterface() : _storageSequence(), _outputSequence(){}; template AdcNetMessageInterface(const T& v, const Ts&... vs) : AdcNetMessageInterface() @@ -292,7 +292,8 @@ public: auto max_el = _storageSequence.size() - start; N >= max_el ? max_el * 2 - 1 : N * 2 - 1; - std::ranges::copy(_outputSequence | std::views::drop(start * 2) | std::views::take(N), std::back_inserter(r)); + std::ranges::copy(_outputSequence | std::views::drop(start * 2) | std::views::take(N) | std::views::join, + std::back_inserter(r)); } const output_seq_t& storageSeq() override @@ -374,6 +375,8 @@ public: this->_storageSequence.clear(); this->_outputSequence.clear(); + // TODO: what if key is an empty range?!!! + if constexpr (traits::adc_input_char_range || traits::formattable) { std::ranges::copy(key, std::back_inserter(_key)); } else if constexpr (traits::formattable) { @@ -394,6 +397,7 @@ public: void key(R& r) const { if constexpr (std::is_convertible_v) { + r = _key; return; } @@ -413,6 +417,23 @@ public: this->tokensBytes(r, start, N); } + + template + void setFromBytes(IterT begin, IterT end) + { + auto sp = std::span(begin, end); + + auto found = std::ranges::search(sp, keyparamDelimiter); + + if (found.empty()) { // no parameters + setKeyParam(sp); + } else { + auto N = std::ranges::distance(sp.begin(), found.begin()); + setKeyParam(sp.first(N), sp.last(sp.size() - N - keyparamDelimiter.size())); + } + } + + protected: std::string _key; // _outputSequence will store parameters diff --git a/tests/adc_netmsg_test.cpp b/tests/adc_netmsg_test.cpp new file mode 100644 index 0000000..5a85bbc --- /dev/null +++ b/tests/adc_netmsg_test.cpp @@ -0,0 +1,26 @@ +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include +#include + +#include "../net/adc_netmessage.h" + +using namespace adc; + +TEST_CASE("[ADC NET MESSAGE]") +{ + AdcKeyParamNetMessage<> msg; + + std::string_view bytes{"SET POS 1 2 3 4 5"}; + + std::cout << "BYTES: [" << bytes << "]\n"; + + msg.setFromBytes(bytes.begin(), bytes.end()); + + std::string key, pars; + msg.key(key); + std::cout << "KEY: [" << key << "]\n"; + + msg.paramsBytes(pars, 1); + // msg.paramsBytes(pars); + std::cout << "PARS BYTES: [" << pars << "]\n"; +}