This commit is contained in:
Timur A. Fatkhullin 2024-11-01 17:26:52 +03:00
parent 3b1a318ee7
commit 8e13ad0c3c
5 changed files with 128 additions and 154 deletions

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <algorithm> #include <algorithm>
#include <memory>
#include <ranges> #include <ranges>
#include <system_error> #include <system_error>
#include <unordered_map> #include <unordered_map>
@ -100,7 +101,8 @@ public:
auto it = _deviceCommands.find(cmd_ident); auto it = _deviceCommands.find(cmd_ident);
if (it != _deviceCommands.end()) { if (it != _deviceCommands.end()) {
it->second(); // it->second();
it->second->operator()();
} else { } else {
throw std::system_error(AdcGenericDeviceErrorCode::ERROR_COMMAND_IDENT); throw std::system_error(AdcGenericDeviceErrorCode::ERROR_COMMAND_IDENT);
} }
@ -113,7 +115,8 @@ public:
{ {
auto it = _deviceAttributes.find(attr_ident); auto it = _deviceAttributes.find(attr_ident);
if (it != _deviceAttributes.end()) { if (it != _deviceAttributes.end()) {
return it->second; // return it->second;
return *(it->second);
} }
throw std::system_error(AdcGenericDeviceErrorCode::ERROR_ATTR_IDENT); throw std::system_error(AdcGenericDeviceErrorCode::ERROR_ATTR_IDENT);
@ -122,7 +125,8 @@ public:
AdcGenericDevice& addCommand(CommandT&& cmd) 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; return *this;
} }
@ -131,10 +135,7 @@ public:
template <typename... CtorArgTs> template <typename... CtorArgTs>
AdcGenericDevice& addCommand(CtorArgTs&&... ctor_args) AdcGenericDevice& addCommand(CtorArgTs&&... ctor_args)
{ {
// _deviceCommands.emplace(std::forward<CtorArgTs>(ctor_args)...);
return addCommand({std::forward<CtorArgTs>(ctor_args)...}); return addCommand({std::forward<CtorArgTs>(ctor_args)...});
// return *this;
} }
AdcGenericDevice& delCommand(const cmd_ident_t& cmd_ident) AdcGenericDevice& delCommand(const cmd_ident_t& cmd_ident)
@ -147,7 +148,8 @@ public:
AdcGenericDevice& addAttribute(AttributeT&& attr) 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; return *this;
} }
@ -155,11 +157,7 @@ public:
template <typename... CtorArgTs> template <typename... CtorArgTs>
AdcGenericDevice& addAttribute(CtorArgTs&&... ctor_args) AdcGenericDevice& addAttribute(CtorArgTs&&... ctor_args)
{ {
// _deviceAttributes.emplace(std::forward<CtorArgTs>(ctor_args)...);
return addAttribute({std::forward<CtorArgTs>(ctor_args)...}); return addAttribute({std::forward<CtorArgTs>(ctor_args)...});
// return *this;
} }
@ -190,8 +188,10 @@ public:
protected: protected:
IdentT _ident; IdentT _ident;
std::unordered_map<attr_ident_t, AttributeT> _deviceAttributes; // std::unordered_map<attr_ident_t, AttributeT> _deviceAttributes;
std::unordered_map<cmd_ident_t, CommandT> _deviceCommands; // 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 } // namespace adc

View File

@ -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 class AdcDeviceAttribute
{ {
static_assert(!std::is_null_pointer_v<SerializedT>, "Deduced serialized type must not be std::nullptr_t!!!");
protected: protected:
template <typename T> template <typename T>
using ret_value_t = std::decay_t<traits::adc_retval_t<T>>; using ret_value_t = std::decay_t<traits::adc_retval_t<T>>;
@ -200,12 +202,9 @@ protected:
template <typename VT> template <typename VT>
inline static std::unordered_map<const AdcDeviceAttribute*, std::function<void(const VT&)>> _setterFunc{}; inline static std::unordered_map<const AdcDeviceAttribute*, std::function<void(const VT&)>> _setterFunc{};
template <typename SerializedT> std::function<SerializedT()> _serializerFunc;
inline static std::unordered_map<const AdcDeviceAttribute*, std::function<SerializedT()>> _serializerFunc{};
template <typename SerializedT> std::function<void(const SerializedT&)> _deserializerFunc;
inline static std::unordered_map<const AdcDeviceAttribute*, std::function<void(const SerializedT&)>>
_deserializerFunc{};
std::function<void()> _clearFunc; std::function<void()> _clearFunc;
@ -213,10 +212,18 @@ protected:
std::function<void(AdcDeviceAttribute*)> _moveFunc; 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: public:
typedef IdentT ident_t; typedef IdentT ident_t;
typedef std::string default_serialized_t; typedef SerializedT serialized_t;
enum AccessType { ReadOnly, WriteOnly, ReadWrite }; enum AccessType { ReadOnly, WriteOnly, ReadWrite };
@ -227,21 +234,18 @@ public:
traits::adc_attr_setter_c ST, traits::adc_attr_setter_c ST,
typename ValueT = traits::attr_value_t<GT, ST>, typename ValueT = traits::attr_value_t<GT, ST>,
traits::adc_serializer_c<ValueT> SRT = 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 = traits::adc_deserializer_c<ValueT> DSRT =
decltype(utils::AdcDefaultValueConverter<>::deserialize<ValueT, default_serialized_t>)> decltype(utils::AdcDefaultValueConverter<>::deserialize<ValueT, serialized_t>)>
AdcDeviceAttribute( AdcDeviceAttribute(const IdentT& ident,
const IdentT& ident,
GT&& getter, GT&& getter,
ST&& setter, ST&& setter,
SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<default_serialized_t, ValueT>, SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<serialized_t, ValueT>,
DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize<ValueT, default_serialized_t>) DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize<ValueT, serialized_t>)
: _ident(ident), _accessType(AdcDeviceAttribute::ReadWrite) : _ident(ident), _accessType(AdcDeviceAttribute::ReadWrite)
{ {
static_assert(!std::is_null_pointer_v<ValueT>, "Getter and Setter can not be nullptr simultaneously!!!"); 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)); _getterFunc<ValueT>.emplace(this, std::forward<GT>(getter));
_setterFunc<ValueT>.emplace(this, std::forward<ST>(setter)); _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>) { 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)), _serializerFunc = [wrapper = traits::adc_pf_wrapper(std::forward<SRT>(serializer)), this]() {
this]() {
auto& serializer = std::get<0>(wrapper); auto& serializer = std::get<0>(wrapper);
@ -268,7 +271,7 @@ public:
if constexpr (!std::is_null_pointer_v<ST> && !std::is_null_pointer_v<DSRT>) { 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)), _deserializerFunc = [wrapper = traits::adc_pf_wrapper(std::forward<DSRT>(deserializer)),
this](const SerializedT& sval) { this](const SerializedT& sval) {
auto& deserializer = std::get<0>(wrapper); auto& deserializer = std::get<0>(wrapper);
@ -281,9 +284,6 @@ public:
_clearFunc = [this]() { _clearFunc = [this]() {
_getterFunc<ValueT>.erase(this); _getterFunc<ValueT>.erase(this);
_setterFunc<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]); _getterFunc<ValueT>.emplace(other, _getterFunc<ValueT>[this]);
_setterFunc<ValueT>.emplace(other, _setterFunc<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 // define copy-function for newly constructed object
other->_copyFunc = [other](AdcDeviceAttribute* o_next) { other->_copyFunc = [other](AdcDeviceAttribute* o_next) {
_getterFunc<ValueT>.emplace(o_next, _getterFunc<ValueT>[other]); _getterFunc<ValueT>.emplace(o_next, _getterFunc<ValueT>[other]);
_setterFunc<ValueT>.emplace(o_next, _setterFunc<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])); _getterFunc<ValueT>.emplace(other, std::move(_getterFunc<ValueT>[this]));
_setterFunc<ValueT>.emplace(other, std::move(_setterFunc<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 // define move-function for newly constructed object
other->_moveFunc = [other](AdcDeviceAttribute* o_next) { other->_moveFunc = [other](AdcDeviceAttribute* o_next) {
_getterFunc<ValueT>.emplace(o_next, std::move(_getterFunc<ValueT>[other])); _getterFunc<ValueT>.emplace(o_next, std::move(_getterFunc<ValueT>[other]));
_setterFunc<ValueT>.emplace(o_next, std::move(_setterFunc<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, traits::adc_attr_setter_c ST,
typename ValueT = traits::attr_value_t<GT, ST>, typename ValueT = traits::attr_value_t<GT, ST>,
traits::adc_serializer_c<ValueT> SRT = 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 = traits::adc_deserializer_c<ValueT> DSRT =
decltype(utils::AdcDefaultValueConverter<>::deserialize<ValueT, default_serialized_t>)> decltype(utils::AdcDefaultValueConverter<>::deserialize<ValueT, serialized_t>)>
AdcDeviceAttribute( AdcDeviceAttribute(const IdentT& ident,
const IdentT& ident,
TupleT&&, TupleT&&,
GT&& getter, GT&& getter,
ST&& setter, ST&& setter,
SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<default_serialized_t, ValueT>, SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<serialized_t, ValueT>,
DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize<ValueT, default_serialized_t>) DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize<ValueT, serialized_t>)
: AdcDeviceAttribute(ident, : AdcDeviceAttribute(ident,
std::forward<GT>(getter), std::forward<GT>(getter),
std::forward<ST>(setter), std::forward<ST>(setter),
@ -357,11 +344,10 @@ public:
template <std::invocable GT, template <std::invocable GT,
typename ValueT = ret_value_t<GT>, typename ValueT = ret_value_t<GT>,
traits::adc_serializer_c<ValueT> SRT = traits::adc_serializer_c<ValueT> SRT =
decltype(utils::AdcDefaultValueConverter<>::serialize<default_serialized_t, ValueT>), decltype(utils::AdcDefaultValueConverter<>::serialize<serialized_t, ValueT>)>
typename SerializedT = ret_value_t<SRT>>
AdcDeviceAttribute(const IdentT& ident, AdcDeviceAttribute(const IdentT& ident,
GT&& getter, 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) : AdcDeviceAttribute(ident, std::forward<GT>(getter), nullptr, std::forward<SRT>(serializer), nullptr)
{ {
} }
@ -371,12 +357,11 @@ public:
std::invocable GT, std::invocable GT,
typename ValueT = ret_value_t<GT>, typename ValueT = ret_value_t<GT>,
traits::adc_serializer_c<ValueT> SRT = traits::adc_serializer_c<ValueT> SRT =
decltype(utils::AdcDefaultValueConverter<>::serialize<default_serialized_t, ValueT>), decltype(utils::AdcDefaultValueConverter<>::serialize<serialized_t, ValueT>)>
typename SerializedT = ret_value_t<SRT>>
AdcDeviceAttribute(const IdentT& ident, AdcDeviceAttribute(const IdentT& ident,
TupleT&&, TupleT&&,
GT&& getter, 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) : AdcDeviceAttribute(ident, TupleT{}, std::forward<GT>(getter), nullptr, std::forward<SRT>(serializer), nullptr)
{ {
} }
@ -385,8 +370,7 @@ public:
// write-only attribute constructor // write-only attribute constructor
template <traits::adc_attr_setter_c ST, template <traits::adc_attr_setter_c ST,
typename ValueT = std::decay_t<traits::adc_func_arg1_t<ST>>, typename ValueT = std::decay_t<traits::adc_func_arg1_t<ST>>,
typename DSRT = decltype(utils::AdcDefaultValueConverter<>::deserialize<ValueT, default_serialized_t>), typename DSRT = decltype(utils::AdcDefaultValueConverter<>::deserialize<ValueT, serialized_t>)>
typename SerializedT = std::decay_t<traits::adc_func_arg1_t<DSRT>>>
AdcDeviceAttribute(const IdentT& ident, AdcDeviceAttribute(const IdentT& ident,
ST&& setter, ST&& setter,
DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize<ValueT, SerializedT>) DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize<ValueT, SerializedT>)
@ -398,8 +382,7 @@ public:
template <traits::adc_tuple_like TupleT, template <traits::adc_tuple_like TupleT,
traits::adc_attr_setter_c ST, traits::adc_attr_setter_c ST,
typename ValueT = std::decay_t<traits::adc_func_arg1_t<ST>>, typename ValueT = std::decay_t<traits::adc_func_arg1_t<ST>>,
typename DSRT = decltype(utils::AdcDefaultValueConverter<>::deserialize<ValueT, default_serialized_t>), typename DSRT = decltype(utils::AdcDefaultValueConverter<>::deserialize<ValueT, serialized_t>)>
typename SerializedT = std::decay_t<traits::adc_func_arg1_t<DSRT>>>
AdcDeviceAttribute(const IdentT& ident, AdcDeviceAttribute(const IdentT& ident,
TupleT&&, TupleT&&,
ST&& setter, ST&& setter,
@ -416,28 +399,22 @@ public:
AdcDeviceAttribute(const AdcDeviceAttribute& other) AdcDeviceAttribute(const AdcDeviceAttribute& other)
{ {
// _clearFunc();
if (&other != this) { if (&other != this) {
other._copyFunc(this); other._copyFunc(this);
}
// _clearFunc = other._clearFunc; _serializerFunc = other._serializerFunc;
// _copyFunc = other._copyFunc; _deserializerFunc = other._deserializerFunc;
// _moveFunc = other._moveFunc; }
} }
AdcDeviceAttribute(AdcDeviceAttribute&& other) AdcDeviceAttribute(AdcDeviceAttribute&& other)
{ {
if (&other != this) { if (&other != this) {
other._moveFunc(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]); _getterFunc<user_t>.emplace(other, _getterFunc<user_t>[this]);
_setterFunc<user_t>.emplace(other, _setterFunc<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 // move TO other
@ -524,6 +510,15 @@ public:
_getterFunc<user_t>.emplace(other, std::move(_getterFunc<user_t>[this])); _getterFunc<user_t>.emplace(other, std::move(_getterFunc<user_t>[this]));
_setterFunc<user_t>.emplace(other, std::move(_setterFunc<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; return *this;
@ -543,13 +538,6 @@ public:
} catch (const std::out_of_range&) { } catch (const std::out_of_range&) {
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_NO_CONV_FUNC); 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> template <typename UT>
@ -567,13 +555,6 @@ public:
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_NO_CONV_FUNC); 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; return *this;
} }
@ -581,13 +562,10 @@ public:
AdcDeviceAttribute& operator=(const AdcDeviceAttribute& other) AdcDeviceAttribute& operator=(const AdcDeviceAttribute& other)
{ {
if (&other != this) { if (&other != this) {
// _clearFunc();
other._copyFunc(this); other._copyFunc(this);
// _clearFunc = other._clearFunc; _serializerFunc = other._serializerFunc;
// _copyFunc = other._copyFunc; _deserializerFunc = other._deserializerFunc;
// _moveFunc = other._moveFunc;
} }
return *this; return *this;
@ -597,72 +575,60 @@ public:
AdcDeviceAttribute& operator=(AdcDeviceAttribute&& other) AdcDeviceAttribute& operator=(AdcDeviceAttribute&& other)
{ {
if (&other != this) { if (&other != this) {
// _clearFunc();
other._moveFunc(this); other._moveFunc(this);
// _clearFunc = std::move(other._clearFunc); _serializerFunc = std::move(other._serializerFunc);
// _copyFunc = std::move(other._copyFunc); _deserializerFunc = std::move(other._deserializerFunc);
// _moveFunc = std::move(other._moveFunc);
} }
return *this; return *this;
} }
template <typename SerializedT> serialized_t serialize()
SerializedT serialize()
{ {
if (_accessType == AdcDeviceAttribute::WriteOnly) { if (_accessType == AdcDeviceAttribute::WriteOnly) {
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_WRITE_ONLY); throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_WRITE_ONLY);
} }
using s_t = std::decay_t<SerializedT>; return _serializerFunc();
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]; template <typename ST>
requires(!std::convertible_to<serialized_t, std::decay_t<ST>>)
// if (serializer) { ST serialize()
// return serializer();
// }
// throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_INVALID_SERIALIZED_TYPE);
}
default_serialized_t 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 serialized_t& sval)
AdcDeviceAttribute& deserialize(const SerializedT& sval)
{ {
if (_accessType == AdcDeviceAttribute::ReadOnly) { if (_accessType == AdcDeviceAttribute::ReadOnly) {
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_READ_ONLY); throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_READ_ONLY);
} }
using s_t = std::decay_t<SerializedT>; _deserializerFunc(sval);
try { return *this;
_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);
} }
// auto& deserializer = _deserializerFunc<s_t>[this]; template <typename ST>
// if (deserializer) { requires(!std::convertible_to<std::decay_t<ST>, serialized_t>)
// deserializer(sval); AdcDeviceAttribute& deserialize(const ST& sval)
// } else { {
// throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_INVALID_SERIALIZED_TYPE); 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!");
}
return *this; return *this;
} }

View File

@ -7,21 +7,21 @@ namespace adc::interfaces
// ADC device attribute concept // ADC device attribute concept
template <typename T> 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::ident_t;
typename T::default_serialized_t; typename T::serialized_t;
{ const_t.ident() } -> std::same_as<typename T::ident_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 // serializer and deserializer must return/accept at least a value of type T::serialized_t
{ t.serialize() } -> std::convertible_to<typename T::default_serialized_t>; { t.serialize() } -> std::convertible_to<typename T::serialized_t>;
t.deserialize(std::declval<typename T::default_serialized_t>()); t.deserialize(std::declval<typename T::serialized_t>());
}; };
// ADC device command concept // ADC device command concept
template <typename T> 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; typename T::ident_t;
{ const_t.ident() } -> std::same_as<typename T::ident_t>; { const_t.ident() } -> std::same_as<typename T::ident_t>;

View File

@ -159,6 +159,8 @@ protected:
_get_attr = [dev_ptr, wrapper = traits::adc_pf_wrapper(std::forward<AttrIdDeserialT>(attr_id_deser_func))]( _get_attr = [dev_ptr, wrapper = traits::adc_pf_wrapper(std::forward<AttrIdDeserialT>(attr_id_deser_func))](
const auto& attr_name) mutable { const auto& attr_name) mutable {
auto attr_id = std::get<0>(wrapper)(attr_name); 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(); auto val = (*dev_ptr)[attr_id].serialize();
using val_t = std::remove_cvref_t<decltype(val)>; using val_t = std::remove_cvref_t<decltype(val)>;
@ -316,7 +318,7 @@ public:
} }
} }
if (found) { if (found) {
msg.ack(); msg.ack(attrs);
} else { } else {
msg.err(std::make_error_code(AdcDeviceNetServerSessionError::ERROR_UNKNOWN_DEVICE_ID)); msg.err(std::make_error_code(AdcDeviceNetServerSessionError::ERROR_UNKNOWN_DEVICE_ID));
} }

View File

@ -8,12 +8,16 @@
#include "../net/adc_netproto.h" #include "../net/adc_netproto.h"
#include "../net/asio/adc_device_netserver_asio.h" #include "../net/asio/adc_device_netserver_asio.h"
typedef adc::impl::AdcDeviceNetServerASIO server_t;
class Device1 : public adc::AdcGenericDevice<std::string, class Device1 : public adc::AdcGenericDevice<std::string,
adc::AdcDeviceAttribute<std::string>, adc::AdcDeviceAttribute<std::string, server_t::serialized_t>,
adc::AdcDeviceCommand<std::string>> 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; base_t;
public: public:
@ -23,10 +27,13 @@ public:
} }
}; };
class Device2 class Device2 : public adc::AdcGenericDevice<std::string,
: public adc::AdcGenericDevice<std::string, adc::AdcDeviceAttribute<size_t>, adc::AdcDeviceCommand<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; base_t;
public: public:
@ -106,7 +113,6 @@ int main(int argc, char* argv[])
asio::signal_set signals(io_ctx, SIGINT, SIGTERM); asio::signal_set signals(io_ctx, SIGINT, SIGTERM);
signals.async_wait([&](std::error_code, int) { io_ctx.stop(); }); signals.async_wait([&](std::error_code, int) { io_ctx.stop(); });
using server_t = adc::impl::AdcDeviceNetServerASIO;
adc::impl::AdcDeviceNetServerASIO server("TEST SRV", io_ctx); adc::impl::AdcDeviceNetServerASIO server("TEST SRV", io_ctx);
server.setupSignals(); server.setupSignals();