Files
ADCLIB/include/adclib/adclib_netmsg.h
2026-08-07 17:22:08 +03:00

120 lines
3.2 KiB
C++

#pragma once
#include <snipplib/utils/snplib_keyvalue_expr.h>
namespace adc
{
/* */
static constexpr std::string_view ADCLIB_NETMSG_KEYWORD_PARAM_DELIM{" "};
static constexpr std::string_view ADCLIB_NETMSG_PARAM_PARAM_DELIM{" "};
/* network message valid keywords */
//
static constexpr std::string_view ADCLIB_NETMSG_KEYWORD_ACK{"ACK"};
// error message
static constexpr std::string_view ADCLIB_NETMSG_KEYWORD_ERR{"ERR"};
// set device attribute value
static constexpr std::string_view ADCLIB_NETMSG_KEYWORD_SET{"SET"};
// get device attribute value
static constexpr std::string_view ADCLIB_NETMSG_KEYWORD_GET{"GET"};
// execute device command
static constexpr std::string_view ADCLIB_NETMSG_KEYWORD_CMD{"CMD"};
static constexpr std::array ADCLIB_NETMSG_VALID_KEYWORDS{ADCLIB_NETMSG_KEYWORD_ACK, ADCLIB_NETMSG_KEYWORD_ERR,
ADCLIB_NETMSG_KEYWORD_SET, ADCLIB_NETMSG_KEYWORD_GET,
ADCLIB_NETMSG_KEYWORD_CMD};
template <snplib::snplib_char_range_c CharRangeT = std::vector<char>>
class AdcNetworkMessage final : protected snplib::snplib_keyvalue_expr_t<CharRangeT>
{
typedef snplib::snplib_keyvalue_expr_t<CharRangeT> base_t;
public:
using typename base_t::error_t;
using base_t::isValid;
using base_t::key;
AdcNetworkMessage() = default;
bool isACK() const
{
return std::ranges::equal(this->_key, ADCLIB_NETMSG_KEYWORD_ACK);
}
bool isERR() const
{
return std::ranges::equal(this->_key, ADCLIB_NETMSG_KEYWORD_ERR);
}
bool isCMD() const
{
return std::ranges::equal(this->_key, ADCLIB_NETMSG_KEYWORD_CMD);
}
bool isSET() const
{
return std::ranges::equal(this->_key, ADCLIB_NETMSG_KEYWORD_SET);
}
bool isGET() const
{
return std::ranges::equal(this->_key, ADCLIB_NETMSG_KEYWORD_GET);
}
error_t createACK()
{
return this->setKey(ADCLIB_NETMSG_KEYWORD_ACK);
}
error_t createERR(error_t err)
{
return this->setKeyValue(ADCLIB_NETMSG_KEYWORD_ERR, err.value());
}
template <snplib::snplib_serializable_c ATTR_NAME_T, typename... ArgTs>
requires(sizeof...(ArgTs) > 0)
error_t createSET(ATTR_NAME_T&& attr_name, ArgTs&&... args)
{
auto err = this->setKeyValue(ADCLIB_NETMSG_KEYWORD_SET, std::forward<ATTR_NAME_T>(attr_name));
if (err) {
return err;
}
return this->appendValue(std::forward<ArgTs>(args)...);
}
template <snplib::snplib_serializable_c ATTR_NAME_T, typename... ArgTs>
error_t createGET(ATTR_NAME_T&& attr_name, ArgTs&&... args)
{
auto err = this->setKeyValue(ADCLIB_NETMSG_KEYWORD_GET, std::forward<ATTR_NAME_T>(attr_name));
if (err) {
return err;
}
if constexpr (sizeof...(ArgTs)) {
return this->appendValue(std::forward<ArgTs>(args)...);
}
return {};
}
template <snplib::snplib_serializable_c CMD_NAME_T>
error_t createCMD(CMD_NAME_T&& cmd_name)
{
return this->setKeyValue(ADCLIB_NETMSG_KEYWORD_CMD, std::forward<CMD_NAME_T>(cmd_name));
}
};
} // namespace adc