Files
ADCLIB/include/adclib/adclib_netmsg.h
Timur A. Fatkhullin 176022d490 ...
2026-08-05 23:10:56 +03:00

101 lines
2.8 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 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->setKey(ADCLIB_NETMSG_KEYWORD_ERR, err.value());
}
template <snplib::snplib_serializable_c ATTR_NAME_T, snplib::snplib_serializable_c ValueT>
error_t createSET(ATTR_NAME_T&& attr_name, ValueT&& value)
{
return this->setKeyValue(ADCLIB_NETMSG_KEYWORD_SET, std::forward<ATTR_NAME_T>(attr_name),
std::forward<ValueT>(value));
}
template <snplib::snplib_serializable_c ATTR_NAME_T>
error_t createGET(ATTR_NAME_T&& attr_name)
{
return this->setKeyValue(ADCLIB_NETMSG_KEYWORD_GET, std::forward<ATTR_NAME_T>(attr_name));
}
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