674 lines
23 KiB
C++
674 lines
23 KiB
C++
#pragma once
|
|
|
|
/*
|
|
|
|
ABSTRACT DEVICE COMPONENTS LIBRARY
|
|
|
|
*/
|
|
|
|
#include <system_error>
|
|
#include <unordered_map>
|
|
|
|
#include "../common/adc_traits.h"
|
|
#include "../common/adc_utils.h"
|
|
|
|
// #include "../common/adc_value.h"
|
|
|
|
|
|
namespace adc
|
|
{
|
|
|
|
namespace constants
|
|
{
|
|
|
|
static constexpr std::tuple<bool,
|
|
char,
|
|
short,
|
|
int,
|
|
long,
|
|
long long,
|
|
unsigned char,
|
|
unsigned short,
|
|
unsigned int,
|
|
unsigned long,
|
|
unsigned long long,
|
|
float,
|
|
double,
|
|
long double>
|
|
AdcDefaultTrivialConvTypes{};
|
|
|
|
} // namespace constants
|
|
|
|
namespace traits
|
|
{
|
|
|
|
/* concepts */
|
|
|
|
// attribute setter concept
|
|
template <typename T>
|
|
concept adc_attr_getter_c = std::is_null_pointer_v<T> ||
|
|
(traits::adc_func_traits<T>::arity == 0 && !std::same_as<traits::adc_retval_t<T>, void>);
|
|
|
|
|
|
// attribute setter concept
|
|
template <typename T>
|
|
concept adc_attr_setter_c = std::is_null_pointer_v<T> || (traits::adc_func_traits<T>::arity == 1);
|
|
|
|
|
|
// internal-to-user and user-to-internal type conversional function concept
|
|
template <typename T>
|
|
concept adc_attr_convfunc_c = std::is_null_pointer_v<T> ||
|
|
(traits::adc_func_traits<T>::arity == 1 && !std::same_as<traits::adc_retval_t<T>, void>);
|
|
|
|
|
|
// deduce attribute type from getter and setter functions signatures
|
|
template <adc_attr_getter_c GT, adc_attr_setter_c ST>
|
|
using attr_value_t = std::decay_t<
|
|
std::conditional_t<std::is_null_pointer_v<GT>,
|
|
std::conditional_t<std::is_null_pointer_v<ST>, std::nullptr_t, traits::adc_func_arg1_t<ST>>,
|
|
traits::adc_retval_t<GT>>>;
|
|
|
|
|
|
|
|
// deduce attribute internal type from conversional functions signatures
|
|
template <adc_attr_convfunc_c FromFuncT, adc_attr_convfunc_c ToFuncT>
|
|
using attr_internal_t = std::decay_t<std::conditional_t<
|
|
std::is_null_pointer_v<ToFuncT>,
|
|
std::conditional_t<std::is_null_pointer_v<FromFuncT>, std::nullptr_t, traits::adc_retval_t<FromFuncT>>,
|
|
traits::adc_retval_t<ToFuncT>>>;
|
|
|
|
|
|
// deduce user-defined type from conversional functions signatures
|
|
template <adc_attr_convfunc_c FromFuncT, adc_attr_convfunc_c ToFuncT>
|
|
using attr_user_t = std::decay_t<std::conditional_t<
|
|
std::is_null_pointer_v<FromFuncT>,
|
|
std::conditional_t<std::is_null_pointer_v<ToFuncT>, std::nullptr_t, traits::adc_retval_t<ToFuncT>>,
|
|
traits::adc_retval_t<FromFuncT>>>;
|
|
|
|
|
|
// attribute serializer function concept
|
|
template <typename T, typename VT>
|
|
concept adc_serializer_c =
|
|
std::is_null_pointer_v<T> || (std::invocable<T, const VT&> && !std::same_as<void, traits::adc_retval_t<T>>);
|
|
|
|
|
|
// attribute deserializer function concept
|
|
template <typename T, typename VT>
|
|
concept adc_deserializer_c = std::is_null_pointer_v<T> || (traits::adc_func_traits<T>::arity == 1 &&
|
|
std::convertible_to<traits::adc_retval_t<T>, VT>);
|
|
|
|
|
|
template <typename SRT, typename DSRT>
|
|
using adc_attr_serialized_t = std::decay_t<
|
|
std::conditional_t<std::is_null_pointer_v<SRT>,
|
|
std::conditional_t<std::is_null_pointer_v<DSRT>, std::nullptr_t, traits::adc_func_arg1_t<DSRT>>,
|
|
traits::adc_retval_t<SRT>>>;
|
|
|
|
|
|
} // namespace traits
|
|
|
|
// error codes
|
|
enum class AdcDeviceAttributeErrorCode : int {
|
|
ERROR_OK,
|
|
ERROR_NO_CONV_FUNC,
|
|
ERROR_INTERNAL_TYPE_MISMATCH,
|
|
ERROR_READ_ONLY,
|
|
ERROR_WRITE_ONLY,
|
|
ERROR_INVALID_SERIALIZED_TYPE,
|
|
ERROR_NO_SERIALIZER,
|
|
ERROR_NO_DESERIALIZER
|
|
};
|
|
|
|
} // namespace adc
|
|
|
|
|
|
namespace std
|
|
{
|
|
|
|
template <>
|
|
class is_error_code_enum<adc::AdcDeviceAttributeErrorCode> : public true_type
|
|
{
|
|
};
|
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
namespace adc
|
|
{
|
|
|
|
// error category
|
|
struct AdcDeviceAttributeErrorCategory : public std::error_category {
|
|
AdcDeviceAttributeErrorCategory() : std::error_category() {}
|
|
|
|
const char* name() const noexcept
|
|
{
|
|
return "ADC_DEVICE_ATTRIBUTE_CATEGORY";
|
|
}
|
|
|
|
std::string message(int ec) const
|
|
{
|
|
AdcDeviceAttributeErrorCode err = static_cast<AdcDeviceAttributeErrorCode>(ec);
|
|
|
|
switch (err) {
|
|
case AdcDeviceAttributeErrorCode::ERROR_OK:
|
|
return "OK";
|
|
case AdcDeviceAttributeErrorCode::ERROR_NO_CONV_FUNC:
|
|
return "conversion function is not defined";
|
|
case AdcDeviceAttributeErrorCode::ERROR_INTERNAL_TYPE_MISMATCH:
|
|
return "try to setup default conversion function for invalid type (internal type mismatch)";
|
|
case AdcDeviceAttributeErrorCode::ERROR_READ_ONLY:
|
|
return "device attribute is read-only";
|
|
case AdcDeviceAttributeErrorCode::ERROR_WRITE_ONLY:
|
|
return "device attribute is write-only";
|
|
case AdcDeviceAttributeErrorCode::ERROR_INVALID_SERIALIZED_TYPE:
|
|
return "invalid user-passed serialized type";
|
|
case AdcDeviceAttributeErrorCode::ERROR_NO_SERIALIZER:
|
|
return "serializing function was not defined";
|
|
case AdcDeviceAttributeErrorCode::ERROR_NO_DESERIALIZER:
|
|
return "deserializing function was not defined";
|
|
default:
|
|
return "UNKNOWN";
|
|
}
|
|
}
|
|
|
|
static const AdcDeviceAttributeErrorCategory& get()
|
|
{
|
|
static const AdcDeviceAttributeErrorCategory constInst;
|
|
return constInst;
|
|
}
|
|
};
|
|
|
|
|
|
inline std::error_code make_error_code(AdcDeviceAttributeErrorCode ec)
|
|
{
|
|
return std::error_code(static_cast<int>(ec), AdcDeviceAttributeErrorCategory::get());
|
|
}
|
|
|
|
|
|
|
|
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>>;
|
|
|
|
template <typename VT>
|
|
inline static std::unordered_map<const AdcDeviceAttribute*, std::function<VT()>> _getterFunc{};
|
|
|
|
template <typename VT>
|
|
inline static std::unordered_map<const AdcDeviceAttribute*, std::function<void(const VT&)>> _setterFunc{};
|
|
|
|
std::function<SerializedT()> _serializerFunc;
|
|
|
|
std::function<void(const SerializedT&)> _deserializerFunc;
|
|
|
|
std::function<void()> _clearFunc;
|
|
|
|
std::function<void(AdcDeviceAttribute*)> _copyFunc;
|
|
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 SerializedT serialized_t;
|
|
|
|
enum AccessType { ReadOnly, WriteOnly, ReadWrite };
|
|
|
|
|
|
/* CONSTRUCTORS AND DESTRUCTOR */
|
|
|
|
template <traits::adc_attr_getter_c GT,
|
|
traits::adc_attr_setter_c ST,
|
|
typename ValueT = traits::attr_value_t<GT, ST>,
|
|
traits::adc_serializer_c<ValueT> SRT =
|
|
decltype(utils::AdcDefaultValueConverter<>::serialize<serialized_t, ValueT>),
|
|
traits::adc_deserializer_c<ValueT> DSRT =
|
|
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!!!");
|
|
|
|
|
|
_getterFunc<ValueT>.emplace(this, std::forward<GT>(getter));
|
|
_setterFunc<ValueT>.emplace(this, std::forward<ST>(setter));
|
|
|
|
if constexpr (std::is_null_pointer_v<GT>) {
|
|
_accessType = AdcDeviceAttribute::WriteOnly;
|
|
}
|
|
|
|
if constexpr (std::is_null_pointer_v<ST>) {
|
|
_accessType = AdcDeviceAttribute::ReadOnly;
|
|
}
|
|
|
|
if constexpr (!std::is_null_pointer_v<GT> && !std::is_null_pointer_v<SRT>) {
|
|
_serializerFunc = [wrapper = traits::adc_pf_wrapper(std::forward<SRT>(serializer)), this]() {
|
|
auto& serializer = std::get<0>(wrapper);
|
|
|
|
|
|
auto val = _getterFunc<ValueT>[this]();
|
|
|
|
return serializer(val);
|
|
};
|
|
}
|
|
|
|
|
|
if constexpr (!std::is_null_pointer_v<ST> && !std::is_null_pointer_v<DSRT>) {
|
|
_deserializerFunc = [wrapper = traits::adc_pf_wrapper(std::forward<DSRT>(deserializer)),
|
|
this](const SerializedT& sval) {
|
|
auto& deserializer = std::get<0>(wrapper);
|
|
|
|
ValueT val = deserializer(sval);
|
|
|
|
_setterFunc<ValueT>[this](val);
|
|
};
|
|
}
|
|
|
|
_clearFunc = [this]() {
|
|
_getterFunc<ValueT>.erase(this);
|
|
_setterFunc<ValueT>.erase(this);
|
|
};
|
|
|
|
|
|
// copy TO other
|
|
_copyFunc = [this](AdcDeviceAttribute* other) {
|
|
_getterFunc<ValueT>.emplace(other, _getterFunc<ValueT>[this]);
|
|
_setterFunc<ValueT>.emplace(other, _setterFunc<ValueT>[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]);
|
|
};
|
|
};
|
|
|
|
|
|
// move TO other
|
|
_moveFunc = [this](AdcDeviceAttribute* other) {
|
|
_getterFunc<ValueT>.emplace(other, std::move(_getterFunc<ValueT>[this]));
|
|
_setterFunc<ValueT>.emplace(other, std::move(_setterFunc<ValueT>[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]));
|
|
};
|
|
};
|
|
}
|
|
|
|
|
|
template <traits::adc_tuple_like TupleT,
|
|
traits::adc_attr_getter_c GT,
|
|
traits::adc_attr_setter_c ST,
|
|
typename ValueT = traits::attr_value_t<GT, ST>,
|
|
traits::adc_serializer_c<ValueT> SRT =
|
|
decltype(utils::AdcDefaultValueConverter<>::serialize<serialized_t, ValueT>),
|
|
traits::adc_deserializer_c<ValueT> DSRT =
|
|
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),
|
|
std::forward<SRT>(serializer),
|
|
std::forward<DSRT>(deserializer))
|
|
{
|
|
static_assert(!std::is_null_pointer_v<ValueT>, "Getter and Setter can not be nullptr simultaneously!!!");
|
|
|
|
AdcDeviceAttribute::setupTrivialConvertFunc<ValueT, std::decay_t<TupleT>>();
|
|
}
|
|
|
|
|
|
// read-only attribute constructor
|
|
template <std::invocable GT,
|
|
typename ValueT = ret_value_t<GT>,
|
|
traits::adc_serializer_c<ValueT> SRT =
|
|
decltype(utils::AdcDefaultValueConverter<>::serialize<serialized_t, ValueT>)>
|
|
AdcDeviceAttribute(const IdentT& ident,
|
|
GT&& getter,
|
|
SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<serialized_t, ValueT>)
|
|
: AdcDeviceAttribute(ident, std::forward<GT>(getter), nullptr, std::forward<SRT>(serializer), nullptr)
|
|
{
|
|
}
|
|
|
|
|
|
template <traits::adc_tuple_like TupleT,
|
|
std::invocable GT,
|
|
typename ValueT = ret_value_t<GT>,
|
|
traits::adc_serializer_c<ValueT> SRT =
|
|
decltype(utils::AdcDefaultValueConverter<>::serialize<serialized_t, ValueT>)>
|
|
AdcDeviceAttribute(const IdentT& ident,
|
|
TupleT&&,
|
|
GT&& getter,
|
|
SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<serialized_t, ValueT>)
|
|
: AdcDeviceAttribute(ident, TupleT{}, std::forward<GT>(getter), nullptr, std::forward<SRT>(serializer), nullptr)
|
|
{
|
|
}
|
|
|
|
|
|
// 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, serialized_t>)>
|
|
AdcDeviceAttribute(const IdentT& ident,
|
|
ST&& setter,
|
|
DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize<ValueT, SerializedT>)
|
|
: AdcDeviceAttribute(ident, nullptr, std::forward<ST>(setter), nullptr, std::forward<DSRT>(deserializer))
|
|
{
|
|
}
|
|
|
|
|
|
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, serialized_t>)>
|
|
AdcDeviceAttribute(const IdentT& ident,
|
|
TupleT&&,
|
|
ST&& setter,
|
|
DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize<ValueT, SerializedT>)
|
|
: AdcDeviceAttribute(ident,
|
|
TupleT{},
|
|
nullptr,
|
|
std::forward<ST>(setter),
|
|
nullptr,
|
|
std::forward<DSRT>(deserializer))
|
|
{
|
|
}
|
|
|
|
|
|
AdcDeviceAttribute(const AdcDeviceAttribute& other)
|
|
{
|
|
if (&other != this) {
|
|
other._copyFunc(this);
|
|
|
|
_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);
|
|
}
|
|
}
|
|
|
|
|
|
virtual ~AdcDeviceAttribute()
|
|
{
|
|
// _clearFunc();
|
|
}
|
|
|
|
|
|
/* PUBLIC METHODS */
|
|
|
|
|
|
|
|
IdentT ident() const
|
|
{
|
|
return _ident;
|
|
}
|
|
|
|
AccessType accessType() const
|
|
{
|
|
return _accessType;
|
|
}
|
|
|
|
|
|
template <traits::adc_attr_convfunc_c FromFuncT, traits::adc_attr_convfunc_c ToFuncT>
|
|
AdcDeviceAttribute& addConvertFunc(FromFuncT&& func_from_internal, ToFuncT&& func_to_internal)
|
|
{
|
|
// using value_t = ret_value_t<ToFuncT>; // it must be internal value type
|
|
// using user_t = ret_value_t<FromFuncT>;
|
|
|
|
using value_t = traits::attr_internal_t<FromFuncT, ToFuncT>;
|
|
using user_t = traits::attr_user_t<FromFuncT, ToFuncT>;
|
|
|
|
static_assert(!std::is_null_pointer_v<value_t>,
|
|
"Deduced attribute internal type must not be std::nullptr_t!!!");
|
|
static_assert(!std::is_null_pointer_v<user_t>, "Deduced user-defined type must not be std::nullptr_t!!!");
|
|
|
|
try {
|
|
if (_accessType != AdcDeviceAttribute::WriteOnly) {
|
|
auto& getter = _getterFunc<value_t>.at(this); // throw out_of_range if value_t is invalid
|
|
|
|
_getterFunc<user_t>[this] =
|
|
[&getter, wrapper = traits::adc_pf_wrapper(std::forward<FromFuncT>(func_from_internal)), this]() {
|
|
auto val = getter();
|
|
return std::get<0>(wrapper)(val); // convert from internal type
|
|
};
|
|
} // ignore "from_internal" conversional function for write-only attribute
|
|
|
|
|
|
|
|
if (_accessType != AdcDeviceAttribute::ReadOnly) {
|
|
auto& setter = _setterFunc<value_t>.at(this); // throw out_of_range if value_t is invalid
|
|
|
|
_setterFunc<user_t>[this] = [&setter,
|
|
wrapper = traits::adc_pf_wrapper(std::forward<ToFuncT>(func_to_internal)),
|
|
this](const user_t& val) {
|
|
value_t value = std::get<0>(wrapper)(val); // convert to internal type
|
|
setter(value);
|
|
};
|
|
} // ignore "to_internal" conversional function for read-only attribute
|
|
} catch (const std::out_of_range&) {
|
|
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_INTERNAL_TYPE_MISMATCH);
|
|
}
|
|
|
|
_clearFunc = [prev_clear = _clearFunc, this]() {
|
|
prev_clear();
|
|
|
|
_getterFunc<user_t>.erase(this);
|
|
_setterFunc<user_t>.erase(this);
|
|
};
|
|
|
|
|
|
// copy TO other
|
|
_copyFunc = [prev_copy = _copyFunc, this](AdcDeviceAttribute* other) {
|
|
prev_copy(other);
|
|
|
|
_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
|
|
_moveFunc = [prev_move = _moveFunc, this](AdcDeviceAttribute* other) {
|
|
prev_move(other);
|
|
|
|
_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;
|
|
}
|
|
|
|
template <typename UT>
|
|
operator UT() const
|
|
{
|
|
if (_accessType == WriteOnly) {
|
|
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_WRITE_ONLY);
|
|
}
|
|
|
|
using val_t = std::decay_t<UT>;
|
|
|
|
try {
|
|
return _getterFunc<val_t>.at(this)();
|
|
} catch (const std::out_of_range&) {
|
|
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_NO_CONV_FUNC);
|
|
}
|
|
}
|
|
|
|
template <typename UT>
|
|
AdcDeviceAttribute& operator=(UT&& val)
|
|
{
|
|
if (_accessType == ReadOnly) {
|
|
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_READ_ONLY);
|
|
}
|
|
|
|
using val_t = std::decay_t<UT>;
|
|
|
|
try {
|
|
_setterFunc<val_t>.at(this)(std::forward<UT>(val));
|
|
} catch (const std::out_of_range&) {
|
|
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_NO_CONV_FUNC);
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
AdcDeviceAttribute& operator=(const AdcDeviceAttribute& other)
|
|
{
|
|
if (&other != this) {
|
|
other._copyFunc(this);
|
|
|
|
_serializerFunc = other._serializerFunc;
|
|
_deserializerFunc = other._deserializerFunc;
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
AdcDeviceAttribute& operator=(AdcDeviceAttribute&& other)
|
|
{
|
|
if (&other != this) {
|
|
other._moveFunc(this);
|
|
|
|
_serializerFunc = std::move(other._serializerFunc);
|
|
_deserializerFunc = std::move(other._deserializerFunc);
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
|
|
serialized_t serialize()
|
|
{
|
|
if (_accessType == AdcDeviceAttribute::WriteOnly) {
|
|
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_WRITE_ONLY);
|
|
}
|
|
|
|
return _serializerFunc();
|
|
}
|
|
|
|
template <typename ST>
|
|
requires(!std::convertible_to<serialized_t, std::decay_t<ST>>)
|
|
ST serialize()
|
|
{
|
|
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!");
|
|
}
|
|
}
|
|
|
|
AdcDeviceAttribute& deserialize(const serialized_t& sval)
|
|
{
|
|
if (_accessType == AdcDeviceAttribute::ReadOnly) {
|
|
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_READ_ONLY);
|
|
}
|
|
|
|
_deserializerFunc(sval);
|
|
|
|
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!");
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
/* some usefull factory functions */
|
|
|
|
template <typename... CtorArgTs>
|
|
static AdcDeviceAttribute makeArithAttr(const IdentT& ident, CtorArgTs... ctor_args)
|
|
{
|
|
return AdcDeviceAttribute{ident, constants::AdcDefaultTrivialConvTypes, std::forward<CtorArgTs>(ctor_args)...};
|
|
}
|
|
|
|
protected:
|
|
IdentT _ident;
|
|
AccessType _accessType;
|
|
|
|
|
|
template <typename VT, size_t I, typename TupleT>
|
|
void setupTrivialConvertFuncImpl()
|
|
{
|
|
if constexpr (I < std::tuple_size_v<TupleT>) {
|
|
using elem_t = std::tuple_element_t<I, TupleT>;
|
|
|
|
if constexpr (!std::is_same_v<VT, elem_t>) {
|
|
addConvertFunc([](const VT& v) { return static_cast<elem_t>(v); },
|
|
[](const elem_t& v) { return static_cast<VT>(v); });
|
|
}
|
|
|
|
setupTrivialConvertFuncImpl<VT, I + 1, TupleT>();
|
|
}
|
|
}
|
|
|
|
template <typename VT, typename TupleT>
|
|
void setupTrivialConvertFunc()
|
|
{
|
|
setupTrivialConvertFuncImpl<VT, 0, TupleT>();
|
|
}
|
|
};
|
|
|
|
|
|
} // namespace adc
|