...
This commit is contained in:
parent
3b1a318ee7
commit
8e13ad0c3c
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <ranges>
|
||||
#include <system_error>
|
||||
#include <unordered_map>
|
||||
@ -100,7 +101,8 @@ public:
|
||||
auto it = _deviceCommands.find(cmd_ident);
|
||||
|
||||
if (it != _deviceCommands.end()) {
|
||||
it->second();
|
||||
// it->second();
|
||||
it->second->operator()();
|
||||
} else {
|
||||
throw std::system_error(AdcGenericDeviceErrorCode::ERROR_COMMAND_IDENT);
|
||||
}
|
||||
@ -113,7 +115,8 @@ public:
|
||||
{
|
||||
auto it = _deviceAttributes.find(attr_ident);
|
||||
if (it != _deviceAttributes.end()) {
|
||||
return it->second;
|
||||
// return it->second;
|
||||
return *(it->second);
|
||||
}
|
||||
|
||||
throw std::system_error(AdcGenericDeviceErrorCode::ERROR_ATTR_IDENT);
|
||||
@ -122,7 +125,8 @@ public:
|
||||
|
||||
AdcGenericDevice& addCommand(CommandT&& cmd)
|
||||
{
|
||||
_deviceCommands.insert({cmd.ident(), std::move(cmd)});
|
||||
// _deviceCommands.insert({cmd.ident(), std::move(cmd)});
|
||||
_deviceCommands.insert({cmd.ident(), std::make_unique<CommandT>(std::move(cmd))});
|
||||
|
||||
return *this;
|
||||
}
|
||||
@ -131,10 +135,7 @@ public:
|
||||
template <typename... CtorArgTs>
|
||||
AdcGenericDevice& addCommand(CtorArgTs&&... ctor_args)
|
||||
{
|
||||
// _deviceCommands.emplace(std::forward<CtorArgTs>(ctor_args)...);
|
||||
return addCommand({std::forward<CtorArgTs>(ctor_args)...});
|
||||
|
||||
// return *this;
|
||||
}
|
||||
|
||||
AdcGenericDevice& delCommand(const cmd_ident_t& cmd_ident)
|
||||
@ -147,7 +148,8 @@ public:
|
||||
|
||||
AdcGenericDevice& addAttribute(AttributeT&& attr)
|
||||
{
|
||||
_deviceAttributes.insert({attr.ident(), std::move(attr)});
|
||||
// _deviceAttributes.insert({attr.ident(), std::move(attr)});
|
||||
_deviceAttributes.insert({attr.ident(), std::make_unique<AttributeT>(std::move(attr))});
|
||||
|
||||
return *this;
|
||||
}
|
||||
@ -155,11 +157,7 @@ public:
|
||||
template <typename... CtorArgTs>
|
||||
AdcGenericDevice& addAttribute(CtorArgTs&&... ctor_args)
|
||||
{
|
||||
// _deviceAttributes.emplace(std::forward<CtorArgTs>(ctor_args)...);
|
||||
|
||||
return addAttribute({std::forward<CtorArgTs>(ctor_args)...});
|
||||
|
||||
// return *this;
|
||||
}
|
||||
|
||||
|
||||
@ -190,8 +188,10 @@ public:
|
||||
protected:
|
||||
IdentT _ident;
|
||||
|
||||
std::unordered_map<attr_ident_t, AttributeT> _deviceAttributes;
|
||||
std::unordered_map<cmd_ident_t, CommandT> _deviceCommands;
|
||||
// std::unordered_map<attr_ident_t, AttributeT> _deviceAttributes;
|
||||
// std::unordered_map<cmd_ident_t, CommandT> _deviceCommands;
|
||||
std::unordered_map<attr_ident_t, std::unique_ptr<AttributeT>> _deviceAttributes;
|
||||
std::unordered_map<cmd_ident_t, std::unique_ptr<CommandT>> _deviceCommands;
|
||||
};
|
||||
|
||||
} // namespace adc
|
||||
|
||||
@ -187,9 +187,11 @@ inline std::error_code make_error_code(AdcDeviceAttributeErrorCode ec)
|
||||
|
||||
|
||||
|
||||
template <typename IdentT>
|
||||
template <typename IdentT = std::string, typename SerializedT = std::vector<char>>
|
||||
class AdcDeviceAttribute
|
||||
{
|
||||
static_assert(!std::is_null_pointer_v<SerializedT>, "Deduced serialized type must not be std::nullptr_t!!!");
|
||||
|
||||
protected:
|
||||
template <typename T>
|
||||
using ret_value_t = std::decay_t<traits::adc_retval_t<T>>;
|
||||
@ -200,12 +202,9 @@ protected:
|
||||
template <typename VT>
|
||||
inline static std::unordered_map<const AdcDeviceAttribute*, std::function<void(const VT&)>> _setterFunc{};
|
||||
|
||||
template <typename SerializedT>
|
||||
inline static std::unordered_map<const AdcDeviceAttribute*, std::function<SerializedT()>> _serializerFunc{};
|
||||
std::function<SerializedT()> _serializerFunc;
|
||||
|
||||
template <typename SerializedT>
|
||||
inline static std::unordered_map<const AdcDeviceAttribute*, std::function<void(const SerializedT&)>>
|
||||
_deserializerFunc{};
|
||||
std::function<void(const SerializedT&)> _deserializerFunc;
|
||||
|
||||
std::function<void()> _clearFunc;
|
||||
|
||||
@ -213,10 +212,18 @@ protected:
|
||||
std::function<void(AdcDeviceAttribute*)> _moveFunc;
|
||||
|
||||
|
||||
template <typename VT>
|
||||
struct ValueHolder {
|
||||
std::function<VT()> _getterFunc;
|
||||
std::function<void(const VT&)> _setterFunc;
|
||||
|
||||
// std::unordered_map<const AdcDeviceAttribute*>
|
||||
};
|
||||
|
||||
public:
|
||||
typedef IdentT ident_t;
|
||||
|
||||
typedef std::string default_serialized_t;
|
||||
typedef SerializedT serialized_t;
|
||||
|
||||
enum AccessType { ReadOnly, WriteOnly, ReadWrite };
|
||||
|
||||
@ -227,21 +234,18 @@ public:
|
||||
traits::adc_attr_setter_c ST,
|
||||
typename ValueT = traits::attr_value_t<GT, ST>,
|
||||
traits::adc_serializer_c<ValueT> SRT =
|
||||
decltype(utils::AdcDefaultValueConverter<>::serialize<default_serialized_t, ValueT>),
|
||||
decltype(utils::AdcDefaultValueConverter<>::serialize<serialized_t, ValueT>),
|
||||
traits::adc_deserializer_c<ValueT> DSRT =
|
||||
decltype(utils::AdcDefaultValueConverter<>::deserialize<ValueT, default_serialized_t>)>
|
||||
AdcDeviceAttribute(
|
||||
const IdentT& ident,
|
||||
GT&& getter,
|
||||
ST&& setter,
|
||||
SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<default_serialized_t, ValueT>,
|
||||
DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize<ValueT, default_serialized_t>)
|
||||
decltype(utils::AdcDefaultValueConverter<>::deserialize<ValueT, serialized_t>)>
|
||||
AdcDeviceAttribute(const IdentT& ident,
|
||||
GT&& getter,
|
||||
ST&& setter,
|
||||
SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<serialized_t, ValueT>,
|
||||
DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize<ValueT, serialized_t>)
|
||||
: _ident(ident), _accessType(AdcDeviceAttribute::ReadWrite)
|
||||
{
|
||||
static_assert(!std::is_null_pointer_v<ValueT>, "Getter and Setter can not be nullptr simultaneously!!!");
|
||||
|
||||
using SerializedT = traits::adc_attr_serialized_t<SRT, DSRT>;
|
||||
static_assert(!std::is_null_pointer_v<SerializedT>, "Deduced serialized type must not be std::nullptr_t!!!");
|
||||
|
||||
_getterFunc<ValueT>.emplace(this, std::forward<GT>(getter));
|
||||
_setterFunc<ValueT>.emplace(this, std::forward<ST>(setter));
|
||||
@ -255,8 +259,7 @@ public:
|
||||
}
|
||||
|
||||
if constexpr (!std::is_null_pointer_v<GT> && !std::is_null_pointer_v<SRT>) {
|
||||
_serializerFunc<SerializedT>[this] = [wrapper = traits::adc_pf_wrapper(std::forward<SRT>(serializer)),
|
||||
this]() {
|
||||
_serializerFunc = [wrapper = traits::adc_pf_wrapper(std::forward<SRT>(serializer)), this]() {
|
||||
auto& serializer = std::get<0>(wrapper);
|
||||
|
||||
|
||||
@ -268,8 +271,8 @@ public:
|
||||
|
||||
|
||||
if constexpr (!std::is_null_pointer_v<ST> && !std::is_null_pointer_v<DSRT>) {
|
||||
_deserializerFunc<SerializedT>[this] = [wrapper = traits::adc_pf_wrapper(std::forward<DSRT>(deserializer)),
|
||||
this](const SerializedT& sval) {
|
||||
_deserializerFunc = [wrapper = traits::adc_pf_wrapper(std::forward<DSRT>(deserializer)),
|
||||
this](const SerializedT& sval) {
|
||||
auto& deserializer = std::get<0>(wrapper);
|
||||
|
||||
ValueT val = deserializer(sval);
|
||||
@ -281,9 +284,6 @@ public:
|
||||
_clearFunc = [this]() {
|
||||
_getterFunc<ValueT>.erase(this);
|
||||
_setterFunc<ValueT>.erase(this);
|
||||
|
||||
_serializerFunc<SerializedT>.erase(this);
|
||||
_deserializerFunc<SerializedT>.erase(this);
|
||||
};
|
||||
|
||||
|
||||
@ -292,16 +292,10 @@ public:
|
||||
_getterFunc<ValueT>.emplace(other, _getterFunc<ValueT>[this]);
|
||||
_setterFunc<ValueT>.emplace(other, _setterFunc<ValueT>[this]);
|
||||
|
||||
_serializerFunc<SerializedT>.emplace(other, _serializerFunc<SerializedT>[this]);
|
||||
_deserializerFunc<SerializedT>.emplace(other, _deserializerFunc<SerializedT>[this]);
|
||||
|
||||
// define copy-function for newly constructed object
|
||||
other->_copyFunc = [other](AdcDeviceAttribute* o_next) {
|
||||
_getterFunc<ValueT>.emplace(o_next, _getterFunc<ValueT>[other]);
|
||||
_setterFunc<ValueT>.emplace(o_next, _setterFunc<ValueT>[other]);
|
||||
|
||||
_serializerFunc<SerializedT>.emplace(o_next, _serializerFunc<SerializedT>[other]);
|
||||
_deserializerFunc<SerializedT>.emplace(o_next, _deserializerFunc<SerializedT>[other]);
|
||||
};
|
||||
};
|
||||
|
||||
@ -311,16 +305,10 @@ public:
|
||||
_getterFunc<ValueT>.emplace(other, std::move(_getterFunc<ValueT>[this]));
|
||||
_setterFunc<ValueT>.emplace(other, std::move(_setterFunc<ValueT>[this]));
|
||||
|
||||
_serializerFunc<SerializedT>.emplace(other, std::move(_serializerFunc<SerializedT>[this]));
|
||||
_deserializerFunc<SerializedT>.emplace(other, std::move(_deserializerFunc<SerializedT>[this]));
|
||||
|
||||
// define move-function for newly constructed object
|
||||
other->_moveFunc = [other](AdcDeviceAttribute* o_next) {
|
||||
_getterFunc<ValueT>.emplace(o_next, std::move(_getterFunc<ValueT>[other]));
|
||||
_setterFunc<ValueT>.emplace(o_next, std::move(_setterFunc<ValueT>[other]));
|
||||
|
||||
_serializerFunc<SerializedT>.emplace(o_next, std::move(_serializerFunc<SerializedT>[other]));
|
||||
_deserializerFunc<SerializedT>.emplace(o_next, std::move(_deserializerFunc<SerializedT>[other]));
|
||||
};
|
||||
};
|
||||
}
|
||||
@ -331,16 +319,15 @@ public:
|
||||
traits::adc_attr_setter_c ST,
|
||||
typename ValueT = traits::attr_value_t<GT, ST>,
|
||||
traits::adc_serializer_c<ValueT> SRT =
|
||||
decltype(utils::AdcDefaultValueConverter<>::serialize<default_serialized_t, ValueT>),
|
||||
decltype(utils::AdcDefaultValueConverter<>::serialize<serialized_t, ValueT>),
|
||||
traits::adc_deserializer_c<ValueT> DSRT =
|
||||
decltype(utils::AdcDefaultValueConverter<>::deserialize<ValueT, default_serialized_t>)>
|
||||
AdcDeviceAttribute(
|
||||
const IdentT& ident,
|
||||
TupleT&&,
|
||||
GT&& getter,
|
||||
ST&& setter,
|
||||
SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<default_serialized_t, ValueT>,
|
||||
DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize<ValueT, default_serialized_t>)
|
||||
decltype(utils::AdcDefaultValueConverter<>::deserialize<ValueT, serialized_t>)>
|
||||
AdcDeviceAttribute(const IdentT& ident,
|
||||
TupleT&&,
|
||||
GT&& getter,
|
||||
ST&& setter,
|
||||
SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<serialized_t, ValueT>,
|
||||
DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize<ValueT, serialized_t>)
|
||||
: AdcDeviceAttribute(ident,
|
||||
std::forward<GT>(getter),
|
||||
std::forward<ST>(setter),
|
||||
@ -357,11 +344,10 @@ public:
|
||||
template <std::invocable GT,
|
||||
typename ValueT = ret_value_t<GT>,
|
||||
traits::adc_serializer_c<ValueT> SRT =
|
||||
decltype(utils::AdcDefaultValueConverter<>::serialize<default_serialized_t, ValueT>),
|
||||
typename SerializedT = ret_value_t<SRT>>
|
||||
decltype(utils::AdcDefaultValueConverter<>::serialize<serialized_t, ValueT>)>
|
||||
AdcDeviceAttribute(const IdentT& ident,
|
||||
GT&& getter,
|
||||
SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<default_serialized_t, ValueT>)
|
||||
SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<serialized_t, ValueT>)
|
||||
: AdcDeviceAttribute(ident, std::forward<GT>(getter), nullptr, std::forward<SRT>(serializer), nullptr)
|
||||
{
|
||||
}
|
||||
@ -371,12 +357,11 @@ public:
|
||||
std::invocable GT,
|
||||
typename ValueT = ret_value_t<GT>,
|
||||
traits::adc_serializer_c<ValueT> SRT =
|
||||
decltype(utils::AdcDefaultValueConverter<>::serialize<default_serialized_t, ValueT>),
|
||||
typename SerializedT = ret_value_t<SRT>>
|
||||
decltype(utils::AdcDefaultValueConverter<>::serialize<serialized_t, ValueT>)>
|
||||
AdcDeviceAttribute(const IdentT& ident,
|
||||
TupleT&&,
|
||||
GT&& getter,
|
||||
SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<default_serialized_t, ValueT>)
|
||||
SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<serialized_t, ValueT>)
|
||||
: AdcDeviceAttribute(ident, TupleT{}, std::forward<GT>(getter), nullptr, std::forward<SRT>(serializer), nullptr)
|
||||
{
|
||||
}
|
||||
@ -385,8 +370,7 @@ public:
|
||||
// write-only attribute constructor
|
||||
template <traits::adc_attr_setter_c ST,
|
||||
typename ValueT = std::decay_t<traits::adc_func_arg1_t<ST>>,
|
||||
typename DSRT = decltype(utils::AdcDefaultValueConverter<>::deserialize<ValueT, default_serialized_t>),
|
||||
typename SerializedT = std::decay_t<traits::adc_func_arg1_t<DSRT>>>
|
||||
typename DSRT = decltype(utils::AdcDefaultValueConverter<>::deserialize<ValueT, serialized_t>)>
|
||||
AdcDeviceAttribute(const IdentT& ident,
|
||||
ST&& setter,
|
||||
DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize<ValueT, SerializedT>)
|
||||
@ -398,8 +382,7 @@ public:
|
||||
template <traits::adc_tuple_like TupleT,
|
||||
traits::adc_attr_setter_c ST,
|
||||
typename ValueT = std::decay_t<traits::adc_func_arg1_t<ST>>,
|
||||
typename DSRT = decltype(utils::AdcDefaultValueConverter<>::deserialize<ValueT, default_serialized_t>),
|
||||
typename SerializedT = std::decay_t<traits::adc_func_arg1_t<DSRT>>>
|
||||
typename DSRT = decltype(utils::AdcDefaultValueConverter<>::deserialize<ValueT, serialized_t>)>
|
||||
AdcDeviceAttribute(const IdentT& ident,
|
||||
TupleT&&,
|
||||
ST&& setter,
|
||||
@ -416,28 +399,22 @@ public:
|
||||
|
||||
AdcDeviceAttribute(const AdcDeviceAttribute& other)
|
||||
{
|
||||
// _clearFunc();
|
||||
|
||||
if (&other != this) {
|
||||
other._copyFunc(this);
|
||||
}
|
||||
|
||||
// _clearFunc = other._clearFunc;
|
||||
// _copyFunc = other._copyFunc;
|
||||
// _moveFunc = other._moveFunc;
|
||||
_serializerFunc = other._serializerFunc;
|
||||
_deserializerFunc = other._deserializerFunc;
|
||||
}
|
||||
}
|
||||
|
||||
AdcDeviceAttribute(AdcDeviceAttribute&& other)
|
||||
{
|
||||
if (&other != this) {
|
||||
other._moveFunc(this);
|
||||
|
||||
_serializerFunc = std::move(other._serializerFunc);
|
||||
_deserializerFunc = std::move(other._deserializerFunc);
|
||||
}
|
||||
|
||||
// other._clearFunc();
|
||||
|
||||
// _clearFunc = std::move(other._clearFunc);
|
||||
// _copyFunc = std::move(other._copyFunc);
|
||||
// _moveFunc = std::move(other._moveFunc);
|
||||
}
|
||||
|
||||
|
||||
@ -516,6 +493,15 @@ public:
|
||||
|
||||
_getterFunc<user_t>.emplace(other, _getterFunc<user_t>[this]);
|
||||
_setterFunc<user_t>.emplace(other, _setterFunc<user_t>[this]);
|
||||
|
||||
// redefine copy-function for newly constructed instance
|
||||
// other._copyFunc was defined in the 'prev_copy(other)' call above
|
||||
other->_moveFunc = [other, copy_f = other->_copyFunc](AdcDeviceAttribute* o_next) {
|
||||
copy_f(o_next);
|
||||
|
||||
_getterFunc<user_t>.emplace(o_next, _getterFunc<user_t>[other]);
|
||||
_setterFunc<user_t>.emplace(o_next, _setterFunc<user_t>[other]);
|
||||
};
|
||||
};
|
||||
|
||||
// move TO other
|
||||
@ -524,6 +510,15 @@ public:
|
||||
|
||||
_getterFunc<user_t>.emplace(other, std::move(_getterFunc<user_t>[this]));
|
||||
_setterFunc<user_t>.emplace(other, std::move(_setterFunc<user_t>[this]));
|
||||
|
||||
// redefine move-function for newly constructed instance
|
||||
// other._moveFunc was defined in the 'prev_move(other)' call above
|
||||
other->_moveFunc = [other, move_f = other->_moveFunc](AdcDeviceAttribute* o_next) {
|
||||
move_f(o_next);
|
||||
|
||||
_getterFunc<user_t>.emplace(o_next, std::move(_getterFunc<user_t>[other]));
|
||||
_setterFunc<user_t>.emplace(o_next, std::move(_setterFunc<user_t>[other]));
|
||||
};
|
||||
};
|
||||
|
||||
return *this;
|
||||
@ -543,13 +538,6 @@ public:
|
||||
} catch (const std::out_of_range&) {
|
||||
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_NO_CONV_FUNC);
|
||||
}
|
||||
|
||||
// auto& getter = _getterFunc<val_t>[this];
|
||||
// if (getter) {
|
||||
// return getter();
|
||||
// }
|
||||
|
||||
// throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_NO_CONV_FUNC);
|
||||
}
|
||||
|
||||
template <typename UT>
|
||||
@ -567,13 +555,6 @@ public:
|
||||
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_NO_CONV_FUNC);
|
||||
}
|
||||
|
||||
// auto& setter = _setterFunc<val_t>[this];
|
||||
// if (setter) {
|
||||
// setter(std::forward<UT>(val));
|
||||
// } else {
|
||||
// throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_NO_CONV_FUNC);
|
||||
// }
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -581,13 +562,10 @@ public:
|
||||
AdcDeviceAttribute& operator=(const AdcDeviceAttribute& other)
|
||||
{
|
||||
if (&other != this) {
|
||||
// _clearFunc();
|
||||
|
||||
other._copyFunc(this);
|
||||
|
||||
// _clearFunc = other._clearFunc;
|
||||
// _copyFunc = other._copyFunc;
|
||||
// _moveFunc = other._moveFunc;
|
||||
_serializerFunc = other._serializerFunc;
|
||||
_deserializerFunc = other._deserializerFunc;
|
||||
}
|
||||
|
||||
return *this;
|
||||
@ -597,73 +575,61 @@ public:
|
||||
AdcDeviceAttribute& operator=(AdcDeviceAttribute&& other)
|
||||
{
|
||||
if (&other != this) {
|
||||
// _clearFunc();
|
||||
|
||||
other._moveFunc(this);
|
||||
|
||||
// _clearFunc = std::move(other._clearFunc);
|
||||
// _copyFunc = std::move(other._copyFunc);
|
||||
// _moveFunc = std::move(other._moveFunc);
|
||||
_serializerFunc = std::move(other._serializerFunc);
|
||||
_deserializerFunc = std::move(other._deserializerFunc);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename SerializedT>
|
||||
SerializedT serialize()
|
||||
serialized_t serialize()
|
||||
{
|
||||
if (_accessType == AdcDeviceAttribute::WriteOnly) {
|
||||
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_WRITE_ONLY);
|
||||
}
|
||||
|
||||
using s_t = std::decay_t<SerializedT>;
|
||||
|
||||
try {
|
||||
_serializerFunc<s_t>.at(this)();
|
||||
} catch (const std::out_of_range&) {
|
||||
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_INVALID_SERIALIZED_TYPE);
|
||||
} catch (const std::bad_function_call&) { // serializer was not defined in ctor!
|
||||
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_NO_SERIALIZER);
|
||||
}
|
||||
|
||||
// auto& serializer = _serializerFunc<s_t>[this];
|
||||
|
||||
// if (serializer) {
|
||||
// return serializer();
|
||||
// }
|
||||
|
||||
// throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_INVALID_SERIALIZED_TYPE);
|
||||
return _serializerFunc();
|
||||
}
|
||||
|
||||
default_serialized_t serialize()
|
||||
template <typename ST>
|
||||
requires(!std::convertible_to<serialized_t, std::decay_t<ST>>)
|
||||
ST serialize()
|
||||
{
|
||||
return serialize<default_serialized_t>();
|
||||
using s_t = std::decay_t<ST>;
|
||||
|
||||
if constexpr (traits::adc_output_char_range<s_t> && traits::adc_output_char_range<serialized_t>) {
|
||||
s_t res;
|
||||
std::ranges::copy(serialize(), std::back_inserter(res));
|
||||
} else {
|
||||
static_assert(false, "INVALID USER SERIALIZATION TYPE!");
|
||||
}
|
||||
}
|
||||
|
||||
template <typename SerializedT>
|
||||
AdcDeviceAttribute& deserialize(const SerializedT& sval)
|
||||
AdcDeviceAttribute& deserialize(const serialized_t& sval)
|
||||
{
|
||||
if (_accessType == AdcDeviceAttribute::ReadOnly) {
|
||||
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_READ_ONLY);
|
||||
}
|
||||
|
||||
using s_t = std::decay_t<SerializedT>;
|
||||
_deserializerFunc(sval);
|
||||
|
||||
try {
|
||||
_deserializerFunc<s_t>.at(this)(sval);
|
||||
} catch (const std::out_of_range&) {
|
||||
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_INVALID_SERIALIZED_TYPE);
|
||||
} catch (const std::bad_function_call&) { // deserializer was not defined in ctor!
|
||||
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_NO_DESERIALIZER);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename ST>
|
||||
requires(!std::convertible_to<std::decay_t<ST>, serialized_t>)
|
||||
AdcDeviceAttribute& deserialize(const ST& sval)
|
||||
{
|
||||
using s_t = std::decay_t<ST>;
|
||||
|
||||
if constexpr (traits::adc_input_char_range<s_t> && traits::adc_input_char_range<serialized_t>) {
|
||||
_deserializerFunc(serialized_t(sval.begin(), sval.end()));
|
||||
} else {
|
||||
static_assert(false, "INVALID USER SERIALIZATION TYPE!");
|
||||
}
|
||||
|
||||
// auto& deserializer = _deserializerFunc<s_t>[this];
|
||||
// if (deserializer) {
|
||||
// deserializer(sval);
|
||||
// } else {
|
||||
// throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_INVALID_SERIALIZED_TYPE);
|
||||
// }
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
@ -7,21 +7,21 @@ namespace adc::interfaces
|
||||
|
||||
// ADC device attribute concept
|
||||
template <typename T>
|
||||
concept adc_device_attr_c = requires(T t, const T const_t) {
|
||||
concept adc_device_attr_c = std::movable<T> && requires(T t, const T const_t) {
|
||||
typename T::ident_t;
|
||||
typename T::default_serialized_t;
|
||||
typename T::serialized_t;
|
||||
|
||||
{ const_t.ident() } -> std::same_as<typename T::ident_t>;
|
||||
|
||||
// serializer and desiarializer must return/accept at least a value of type T::default_serialized_t
|
||||
{ t.serialize() } -> std::convertible_to<typename T::default_serialized_t>;
|
||||
t.deserialize(std::declval<typename T::default_serialized_t>());
|
||||
// serializer and deserializer must return/accept at least a value of type T::serialized_t
|
||||
{ t.serialize() } -> std::convertible_to<typename T::serialized_t>;
|
||||
t.deserialize(std::declval<typename T::serialized_t>());
|
||||
};
|
||||
|
||||
|
||||
// ADC device command concept
|
||||
template <typename T>
|
||||
concept adc_device_cmd_c = requires(T t, const T const_t) {
|
||||
concept adc_device_cmd_c = std::movable<T> && requires(T t, const T const_t) {
|
||||
typename T::ident_t;
|
||||
|
||||
{ const_t.ident() } -> std::same_as<typename T::ident_t>;
|
||||
|
||||
@ -159,6 +159,8 @@ protected:
|
||||
_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 val = attr.serialize();
|
||||
auto val = (*dev_ptr)[attr_id].serialize();
|
||||
using val_t = std::remove_cvref_t<decltype(val)>;
|
||||
|
||||
@ -316,7 +318,7 @@ public:
|
||||
}
|
||||
}
|
||||
if (found) {
|
||||
msg.ack();
|
||||
msg.ack(attrs);
|
||||
} else {
|
||||
msg.err(std::make_error_code(AdcDeviceNetServerSessionError::ERROR_UNKNOWN_DEVICE_ID));
|
||||
}
|
||||
|
||||
@ -8,12 +8,16 @@
|
||||
#include "../net/adc_netproto.h"
|
||||
#include "../net/asio/adc_device_netserver_asio.h"
|
||||
|
||||
typedef adc::impl::AdcDeviceNetServerASIO server_t;
|
||||
|
||||
|
||||
class Device1 : public adc::AdcGenericDevice<std::string,
|
||||
adc::AdcDeviceAttribute<std::string>,
|
||||
adc::AdcDeviceAttribute<std::string, server_t::serialized_t>,
|
||||
adc::AdcDeviceCommand<std::string>>
|
||||
{
|
||||
typedef adc::AdcGenericDevice<std::string, adc::AdcDeviceAttribute<std::string>, adc::AdcDeviceCommand<std::string>>
|
||||
typedef adc::AdcGenericDevice<std::string,
|
||||
adc::AdcDeviceAttribute<std::string, server_t::serialized_t>,
|
||||
adc::AdcDeviceCommand<std::string>>
|
||||
base_t;
|
||||
|
||||
public:
|
||||
@ -23,10 +27,13 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class Device2
|
||||
: public adc::AdcGenericDevice<std::string, adc::AdcDeviceAttribute<size_t>, adc::AdcDeviceCommand<std::string>>
|
||||
class Device2 : public adc::AdcGenericDevice<std::string,
|
||||
adc::AdcDeviceAttribute<size_t, server_t::serialized_t>,
|
||||
adc::AdcDeviceCommand<std::string>>
|
||||
{
|
||||
typedef adc::AdcGenericDevice<std::string, adc::AdcDeviceAttribute<size_t>, adc::AdcDeviceCommand<std::string>>
|
||||
typedef adc::AdcGenericDevice<std::string,
|
||||
adc::AdcDeviceAttribute<size_t, server_t::serialized_t>,
|
||||
adc::AdcDeviceCommand<std::string>>
|
||||
base_t;
|
||||
|
||||
public:
|
||||
@ -106,7 +113,6 @@ int main(int argc, char* argv[])
|
||||
asio::signal_set signals(io_ctx, SIGINT, SIGTERM);
|
||||
signals.async_wait([&](std::error_code, int) { io_ctx.stop(); });
|
||||
|
||||
using server_t = adc::impl::AdcDeviceNetServerASIO;
|
||||
adc::impl::AdcDeviceNetServerASIO server("TEST SRV", io_ctx);
|
||||
server.setupSignals();
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user