This commit is contained in:
Timur A. Fatkhullin
2026-06-21 11:55:52 +03:00
parent 6ebea9e29d
commit a395c8006b
3 changed files with 53 additions and 14 deletions

View File

@@ -5,6 +5,8 @@
****************************************************************************************/
#include <concepts>
#include <format>
#include <functional>
#include <string>
#include <system_error>
#include <type_traits>
@@ -69,9 +71,9 @@ struct AdcDeviceErrorCategory : std::error_category {
case AdcDeviceErrorCode::ERROR_OK:
return "OK";
case AdcDeviceErrorCode::ERROR_NO_CMD_ID:
return "invalud command ID";
return "invalid command ID";
case AdcDeviceErrorCode::ERROR_NO_ATTR_ID:
return "invalud attribute ID";
return "invalid attribute ID";
case AdcDeviceErrorCode::ERROR_RO_ATTR:
return "read-only attribute";
case AdcDeviceErrorCode::ERROR_WO_ATTR:
@@ -94,6 +96,11 @@ struct AdcDeviceErrorCategory : std::error_category {
}
};
inline std::error_code make_error_code(AdcDeviceErrorCode ec)
{
return std::error_code(static_cast<int>(ec), AdcDeviceErrorCategory::get());
}
template <std::formattable<char> ATTR_ID_T = std::string_view, std::formattable<char> CMD_ID_T = std::string_view>
class AdcGenericDevice
@@ -150,13 +157,13 @@ protected:
template <typename VT>
operator adc_result_t<VT>()
{
auto v = _dev._attrs.template get<VT>(_id);
if (v) {
return v.value();
auto const& attr = _dev._attrs.template get<attr_t<VT>>(_id);
if (attr) {
return attr.value().getter();
}
// error
if (v.error() == snplib::HeterogenMap<ATTR_ID_T>::ERROR_NO_ELEM) {
if (attr.error() == snplib::HeterogenMap<ATTR_ID_T>::ERROR_NO_ELEM) {
return std::unexpected(AdcDeviceErrorCode::ERROR_NO_ATTR_ID);
}
@@ -166,10 +173,12 @@ protected:
template <typename VT>
adc_error_t operator=(VT&& val)
{
auto err = _dev._attrs.set(_id, std::forward<VT>(val));
if (err == snplib::HeterogenMap<ATTR_ID_T>::ERROR_OK) {
return AdcDeviceErrorCode::ERROR_OK;
} else if (err == snplib::HeterogenMap<ATTR_ID_T>::ERROR_NO_ELEM) {
auto const& attr = _dev._attrs.template get<attr_t<VT>>(_id);
if (attr) {
return attr.value().setter(std::forward<VT>(val));
}
if (attr.error() == snplib::HeterogenMap<ATTR_ID_T>::ERROR_NO_ELEM) {
return AdcDeviceErrorCode::ERROR_NO_ATTR_ID;
} else {
return AdcDeviceErrorCode::ERROR_UNKNOWN;
@@ -196,8 +205,8 @@ public:
_commands.emplace(std::move(id), std::forward<ET>(exec_func));
}
template <typename GT, typename ST>
void addAttr(ATTR_ID_T id, GT&& getter, ST&& setter)
template <typename GT, typename ST, typename... VTs>
void addAttr(ATTR_ID_T id, GT&& getter, ST&& setter, std::tuple<VTs...> = std::tuple())
{
// deduce attribute value type
using ret_t = std::invoke_result_t<GT>;
@@ -210,6 +219,19 @@ public:
"Invalid return type of the setter!");
_attrs.push(std::move(id), attr_t<v_t>{.getter = std::forward<GT>(getter), .setter = std::forward<ST>(setter)});
// if constexpr (sizeof...(VTs)) {
// auto func = [id, this]<size_t I>() {
// using u_t = std::tuple_element_t<I, std::tuple<VTs...>>;
// if constexpr (!requires(u_t u, v_t v) {
// u = v;
// v = u;
// }) {
// static_assert(false, "Invalid user type!");
// }
// _attrs.push(std::move(id), attr_t<u_t>{.getter = []() {}, .setter = []() {}});
// };
// }
}
adc_error_t operator()(CMD_ID_T id)