132 lines
3.9 KiB
C++
132 lines
3.9 KiB
C++
#include <iostream>
|
|
|
|
#include "../common/adc_traits.h"
|
|
#include "../device/adc_device.h"
|
|
#include "../device/adc_device_attribute.h"
|
|
#include "../device/adc_device_command.h"
|
|
|
|
using namespace adc;
|
|
|
|
typedef std::vector<char> serialized_t;
|
|
|
|
class DeviceWrapper
|
|
{
|
|
public:
|
|
// using char_range_t = std::span<const char>;
|
|
using char_range_t = std::span<char>;
|
|
|
|
private:
|
|
serialized_t _id;
|
|
|
|
std::function<serialized_t(const serialized_t&)> _get_attr = [](auto) -> serialized_t {
|
|
throw std::invalid_argument("BAD _get_attr");
|
|
};
|
|
std::function<void(const serialized_t&, const serialized_t&)> _set_attr = [](auto, auto) {
|
|
throw std::invalid_argument("BAD _set_attr");
|
|
};
|
|
std::function<void(const serialized_t&)> _exec_cmd = [](auto) { throw std::invalid_argument("BAD _exec_cmd"); };
|
|
|
|
public:
|
|
DeviceWrapper() : _id()
|
|
{
|
|
// null device
|
|
std::string_view id{"NULL-DEVICE"};
|
|
std::ranges::copy(id, std::back_inserter(_id));
|
|
};
|
|
|
|
template <typename DeviceT,
|
|
typename AttrIdDeserialT = traits::adc_char_identity<typename DeviceT::attr_ident_t>,
|
|
typename CmdIdDeserialT = traits::adc_char_identity<typename DeviceT::cmd_ident_t>>
|
|
DeviceWrapper(DeviceT* dev_ptr,
|
|
const serialized_t& id,
|
|
AttrIdDeserialT&& attr_id_deser_func = {},
|
|
CmdIdDeserialT&& cmd_id_deser_func = {})
|
|
: _id(id)
|
|
{
|
|
_get_attr = [dev_ptr, wrapper = traits::adc_pf_wrapper(std::forward<AttrIdDeserialT>(attr_id_deser_func))](
|
|
const auto& attr_name) mutable {
|
|
auto attr_id = std::get<0>(wrapper)(attr_name);
|
|
auto& attr = (*dev_ptr)[attr_id];
|
|
// auto& attr = dev_ptr->operator[](attr_id);
|
|
auto val = attr.serialize();
|
|
return val;
|
|
};
|
|
|
|
_set_attr = [dev_ptr, wrapper = traits::adc_pf_wrapper(std::forward<AttrIdDeserialT>(attr_id_deser_func))](
|
|
const auto& attr_name, const auto& val) mutable {
|
|
auto attr_id = std::get<0>(wrapper)(attr_name);
|
|
(*dev_ptr)[attr_id].deserialize(val);
|
|
};
|
|
|
|
_exec_cmd = [dev_ptr, wrapper = traits::adc_pf_wrapper(std::forward<CmdIdDeserialT>(cmd_id_deser_func))](
|
|
const auto& cmd_name) mutable {
|
|
auto cmd_id = std::get<0>(wrapper)(cmd_name);
|
|
(*dev_ptr)(cmd_id);
|
|
};
|
|
}
|
|
|
|
|
|
serialized_t ident() const
|
|
{
|
|
// return _get_id();
|
|
return _id;
|
|
}
|
|
|
|
serialized_t getAttr(const serialized_t& attr_name)
|
|
{
|
|
return _get_attr(attr_name);
|
|
}
|
|
|
|
void setAttr(const serialized_t& attr_name, const serialized_t& val)
|
|
{
|
|
_set_attr(attr_name, val);
|
|
}
|
|
|
|
void exec(const serialized_t& cmd_name)
|
|
{
|
|
_exec_cmd(cmd_name);
|
|
}
|
|
};
|
|
|
|
|
|
static std::vector<DeviceWrapper> devs;
|
|
|
|
|
|
auto getAttr(size_t dev_idx, const serialized_t& name)
|
|
{
|
|
DeviceWrapper& dev = devs[dev_idx];
|
|
|
|
return dev.getAttr(name);
|
|
}
|
|
|
|
int main()
|
|
{
|
|
typedef AdcGenericDevice<std::string_view, AdcDeviceAttribute<std::string_view, serialized_t>, AdcDeviceCommand<>>
|
|
dev_t;
|
|
|
|
dev_t dev1("DEV1");
|
|
|
|
int attr1_val = 10;
|
|
// dev1.addAttribute(dev_t::attribute_t::makeArithAttr(
|
|
// "ATTR1", [&attr1_val]() { return attr1_val; }, [&attr1_val](const int& v) { attr1_val = v; }));
|
|
dev1.addAttribute(dev_t::attribute_t::makeArithAttr(
|
|
"ATTR1", [&attr1_val]() { return attr1_val; }, [&attr1_val](const int& v) { attr1_val = v; },
|
|
utils::AdcDefaultValueConverter<>::serialize<dev_t::attribute_t::serialized_t, int>));
|
|
|
|
devs.push_back({&dev1, {'D', '1'}});
|
|
|
|
serialized_t sn;
|
|
std::ranges::copy(std::string_view("ATTR1"), std::back_inserter(sn));
|
|
|
|
devs[0].setAttr(sn, {'7', '7'});
|
|
auto r = getAttr(0, sn);
|
|
|
|
std::string rs;
|
|
std::ranges::copy(r, std::back_inserter(rs));
|
|
|
|
std::cout << "ATTR1 = " << rs << "\n";
|
|
|
|
|
|
return 0;
|
|
}
|