71 lines
1.8 KiB
C++
71 lines
1.8 KiB
C++
#pragma once
|
|
|
|
/****************************************************************************************
|
|
* ABSTRACT DEVICE COMPONENTS LIBRARY *
|
|
****************************************************************************************/
|
|
|
|
#include <snipplib/containers/snplib_hmap.h>
|
|
|
|
|
|
namespace adc
|
|
{
|
|
|
|
enum class adc_device_error_code_t : int { ERROR_OK, ERROR_RO_ATTR, ERROR_WO_ATTR, ERROR_ATTR_RANGE };
|
|
|
|
} // namespace adc
|
|
|
|
namespace adc
|
|
{
|
|
|
|
|
|
template <std::formattable<char> ATTR_ID_T = std::string_view, std::formattable<char> CMD_ID_T = std::string_view>
|
|
class adc_device_t
|
|
{
|
|
public:
|
|
typedef std::error_code error_t;
|
|
|
|
template <typename VT>
|
|
using op_result_t = std::expected<VT, error_t>;
|
|
|
|
protected:
|
|
template <typename VT>
|
|
struct attr_t {
|
|
ATTR_ID_T id;
|
|
std::function<op_result_t<VT>()> getter;
|
|
std::function<void(VT const&)> setter;
|
|
};
|
|
|
|
|
|
struct attr_proxy_t {
|
|
template <typename VT>
|
|
VT get(ATTR_ID_T id)
|
|
{
|
|
}
|
|
};
|
|
|
|
std::unordered_map<CMD_ID_T, std::function<void()>> _commands{};
|
|
snplib::HeterogenMap<ATTR_ID_T> _attrs{};
|
|
|
|
public:
|
|
adc_device_t() {}
|
|
|
|
template <snplib::snplib_callable_c ET>
|
|
void addCommand(CMD_ID_T id, ET&& exec_func)
|
|
{
|
|
_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)
|
|
{
|
|
// deduce attribute value type
|
|
using v_t = std::invoke_result_t<GT>;
|
|
|
|
static_assert(std::invocable<ST, v_t const&>, "Invalid setter type!");
|
|
|
|
_attrs.push(std::move(id), attr_t<v_t>{.getter = std::forward<GT>(getter), .setter = std::forward<ST>(setter)});
|
|
}
|
|
};
|
|
|
|
} // namespace adc
|