757 lines
28 KiB
C++
757 lines
28 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{};
|
|
|
|
// from user to inner type converter
|
|
template <typename UT>
|
|
inline static std::unordered_map<const AdcDeviceAttribute*,
|
|
std::function<void(const UT&, const AdcDeviceAttribute*)>>
|
|
_convFuncTo{};
|
|
|
|
// from innner to user type converter
|
|
template <typename UT>
|
|
inline static std::unordered_map<const AdcDeviceAttribute*, std::function<UT(const AdcDeviceAttribute*)>>
|
|
_convFuncFrom{};
|
|
|
|
// std::function<SerializedT()> _serializerFunc;
|
|
std::function<SerializedT(const AdcDeviceAttribute*)> _serializerFunc;
|
|
|
|
// std::function<void(const SerializedT&)> _deserializerFunc;
|
|
std::function<void(const SerializedT&, const AdcDeviceAttribute*)> _deserializerFunc;
|
|
|
|
// static inline std::vector<std::function<void(AdcDeviceAttribute*)>> _clearFunc{};
|
|
std::vector<std::function<void(AdcDeviceAttribute*)>> _clearFunc{};
|
|
|
|
// static inline std::vector<std::function<void(const AdcDeviceAttribute*, AdcDeviceAttribute*)>> _copyFunc{};
|
|
// static inline std::vector<std::function<void(AdcDeviceAttribute*, AdcDeviceAttribute*)>> _moveFunc{};
|
|
std::vector<std::function<void(const AdcDeviceAttribute*, AdcDeviceAttribute*)>> _copyFunc{};
|
|
std::vector<std::function<void(AdcDeviceAttribute*, AdcDeviceAttribute*)>> _moveFunc{};
|
|
|
|
|
|
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>) {
|
|
// auto& getter_func = _getterFunc<ValueT>[this];
|
|
// _serializerFunc = [getter_func, wrapper = traits::adc_pf_wrapper(std::forward<SRT>(serializer)), this]()
|
|
// {
|
|
_serializerFunc =
|
|
[wrapper = traits::adc_pf_wrapper(std::forward<SRT>(serializer))](const AdcDeviceAttribute* inst) {
|
|
auto& serializer = std::get<0>(wrapper);
|
|
|
|
// auto val = _getterFunc<ValueT>[this]();
|
|
auto val = _getterFunc<ValueT>[inst]();
|
|
// auto val = getter_func();
|
|
|
|
return serializer(val);
|
|
};
|
|
}
|
|
|
|
|
|
if constexpr (!std::is_null_pointer_v<ST> && !std::is_null_pointer_v<DSRT>) {
|
|
// auto& setter_func = _setterFunc<ValueT>[this];
|
|
// _deserializerFunc = [setter_func, wrapper = traits::adc_pf_wrapper(std::forward<DSRT>(deserializer)),
|
|
// this](const SerializedT& sval) {
|
|
_deserializerFunc = [wrapper = traits::adc_pf_wrapper(std::forward<DSRT>(deserializer))](
|
|
const SerializedT& sval, const AdcDeviceAttribute* inst) {
|
|
auto& deserializer = std::get<0>(wrapper);
|
|
|
|
ValueT val = deserializer(sval);
|
|
|
|
// _setterFunc<ValueT>[this](val);
|
|
_setterFunc<ValueT>[inst](val);
|
|
// setter_func(val);
|
|
};
|
|
}
|
|
|
|
_convFuncTo<ValueT>[this] = [](const ValueT& v, const AdcDeviceAttribute* inst) {
|
|
_setterFunc<ValueT>[inst](v);
|
|
};
|
|
|
|
_convFuncFrom<ValueT>[this] = [](const AdcDeviceAttribute* inst) { return _getterFunc<ValueT>[inst](); };
|
|
|
|
_clearFunc.emplace_back([](AdcDeviceAttribute* inst) {
|
|
_getterFunc<ValueT>.erase(inst);
|
|
_setterFunc<ValueT>.erase(inst);
|
|
|
|
_convFuncFrom<ValueT>.erase(inst);
|
|
_convFuncTo<ValueT>.erase(inst);
|
|
});
|
|
|
|
|
|
// copy instance function
|
|
_copyFunc.emplace_back([](const AdcDeviceAttribute* from, AdcDeviceAttribute* to) {
|
|
_getterFunc<ValueT>.emplace(to, _getterFunc<ValueT>[from]);
|
|
_setterFunc<ValueT>.emplace(to, _setterFunc<ValueT>[from]);
|
|
|
|
_convFuncFrom<ValueT>.emplace(to, _convFuncFrom<ValueT>[from]);
|
|
_convFuncTo<ValueT>.emplace(to, _convFuncTo<ValueT>[from]);
|
|
});
|
|
|
|
// move instance function
|
|
_moveFunc.emplace_back([](AdcDeviceAttribute* from, AdcDeviceAttribute* to) {
|
|
_getterFunc<ValueT>.emplace(to, std::move(_getterFunc<ValueT>[from]));
|
|
_setterFunc<ValueT>.emplace(to, std::move(_setterFunc<ValueT>[from]));
|
|
|
|
_convFuncFrom<ValueT>.emplace(to, std::move(_convFuncFrom<ValueT>[from]));
|
|
_convFuncTo<ValueT>.emplace(to, std::move(_convFuncTo<ValueT>[from]));
|
|
});
|
|
}
|
|
|
|
|
|
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) {
|
|
// for (auto& fn : _copyFunc) {
|
|
for (auto& fn : other._copyFunc) {
|
|
fn(&other, this);
|
|
}
|
|
|
|
_copyFunc = other._copyFunc;
|
|
_moveFunc = other._moveFunc;
|
|
_clearFunc = other._clearFunc;
|
|
|
|
_ident = other._ident;
|
|
_accessType = other._accessType;
|
|
_serializerFunc = other._serializerFunc;
|
|
_deserializerFunc = other._deserializerFunc;
|
|
}
|
|
}
|
|
|
|
AdcDeviceAttribute(AdcDeviceAttribute&& other)
|
|
{
|
|
if (&other != this) {
|
|
// for (auto& fn : _moveFunc) {
|
|
for (auto& fn : other._moveFunc) {
|
|
fn(&other, this);
|
|
}
|
|
|
|
// _copyFunc = std::move(other._copyFunc);
|
|
// _moveFunc = std::move(other._moveFunc);
|
|
// _clearFunc = std::move(other._clearFunc);
|
|
|
|
_copyFunc = other._copyFunc;
|
|
_moveFunc = other._moveFunc;
|
|
_clearFunc = other._clearFunc;
|
|
|
|
_ident = std::move(other._ident);
|
|
_accessType = std::move(other._accessType);
|
|
_serializerFunc = std::move(other._serializerFunc);
|
|
_deserializerFunc = std::move(other._deserializerFunc);
|
|
}
|
|
}
|
|
|
|
|
|
virtual ~AdcDeviceAttribute()
|
|
{
|
|
for (auto& fn : _clearFunc) {
|
|
fn(this);
|
|
};
|
|
}
|
|
|
|
|
|
/* 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!!!");
|
|
|
|
// return *this;
|
|
try {
|
|
if (_accessType != AdcDeviceAttribute::WriteOnly) {
|
|
// auto& getter = _getterFunc<value_t>.at(this); // throw out_of_range if value_t is invalid
|
|
|
|
_convFuncFrom<user_t>[this] = [wrapper = traits::adc_pf_wrapper(std::forward<FromFuncT>(
|
|
func_from_internal))](const AdcDeviceAttribute* inst) {
|
|
auto val = _getterFunc<value_t>[inst]();
|
|
return std::get<0>(wrapper)(val); // convert from internal type
|
|
};
|
|
// _getterFunc<user_t>.try_emplace(
|
|
// this, [getter = _getterFunc<value_t>.at(this),
|
|
// wrapper = traits::adc_pf_wrapper(std::forward<FromFuncT>(func_from_internal))]() {
|
|
// auto val = getter();
|
|
// return std::get<0>(wrapper)(val); // convert from internal type
|
|
// });
|
|
// _getterFunc<user_t>[this] =
|
|
// [getter = _getterFunc<value_t>.at(this),
|
|
// wrapper = traits::adc_pf_wrapper(std::forward<FromFuncT>(func_from_internal))]() {
|
|
// auto val = getter();
|
|
// return std::get<0>(wrapper)(val); // convert from internal type
|
|
// };
|
|
|
|
// _getterFunc<user_t>[this] =
|
|
// [getter, wrapper = traits::adc_pf_wrapper(std::forward<FromFuncT>(func_from_internal)), this]() {
|
|
// // auto val = _getterFunc<value_t>.at(this)(); // throw out_of_range if value_t is invalid
|
|
// 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) {
|
|
_convFuncTo<user_t>[this] = [wrapper = traits::adc_pf_wrapper(std::forward<ToFuncT>(func_to_internal))](
|
|
const user_t& val, const AdcDeviceAttribute* inst) {
|
|
value_t value = std::get<0>(wrapper)(val); // convert to internal type
|
|
_setterFunc<value_t>[inst](value);
|
|
};
|
|
// 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
|
|
|
|
// // throw out_of_range if value_t is invalid
|
|
// // _setterFunc<value_t>.at(this)(value);
|
|
// 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);
|
|
}
|
|
|
|
// return *this;
|
|
_clearFunc.emplace_back([](AdcDeviceAttribute* inst) {
|
|
// _getterFunc<user_t>.erase(inst);
|
|
// _setterFunc<user_t>.erase(inst);
|
|
_convFuncFrom<user_t>.erase(inst);
|
|
_convFuncTo<user_t>.erase(inst);
|
|
});
|
|
|
|
|
|
// copy instance functions
|
|
_copyFunc.emplace_back([](const AdcDeviceAttribute* from, AdcDeviceAttribute* to) {
|
|
// _getterFunc<user_t>.emplace(to, _getterFunc<user_t>[from]);
|
|
// _setterFunc<user_t>.emplace(to, _setterFunc<user_t>[from]);
|
|
_convFuncFrom<user_t>.emplace(to, _convFuncFrom<user_t>[from]);
|
|
_convFuncTo<user_t>.emplace(to, _convFuncTo<user_t>[from]);
|
|
});
|
|
|
|
|
|
// move instance functions
|
|
_moveFunc.emplace_back([](AdcDeviceAttribute* from, AdcDeviceAttribute* to) {
|
|
// _getterFunc<user_t>.emplace(to, std::move(_getterFunc<user_t>[from]));
|
|
// _setterFunc<user_t>.emplace(to, std::move(_setterFunc<user_t>[from]));
|
|
_convFuncFrom<user_t>.emplace(to, std::move(_convFuncFrom<user_t>[from]));
|
|
_convFuncTo<user_t>.emplace(to, std::move(_convFuncTo<user_t>[from]));
|
|
});
|
|
|
|
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)();
|
|
return _convFuncFrom<val_t>.at(this)(this);
|
|
} catch (const std::out_of_range&) {
|
|
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_NO_CONV_FUNC);
|
|
}
|
|
}
|
|
|
|
template <typename UT>
|
|
requires(!std::same_as<AdcDeviceAttribute, std::decay_t<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));
|
|
_convFuncTo<val_t>.at(this)(std::forward<UT>(val), this);
|
|
} 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) {
|
|
for (auto& fn : other._copyFunc) {
|
|
fn(&other, this);
|
|
}
|
|
|
|
_copyFunc = other._copyFunc;
|
|
_moveFunc = other._moveFunc;
|
|
_clearFunc = other._clearFunc;
|
|
|
|
_ident = other._ident;
|
|
_accessType = other._accessType;
|
|
_serializerFunc = other._serializerFunc;
|
|
_deserializerFunc = other._deserializerFunc;
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
AdcDeviceAttribute& operator=(AdcDeviceAttribute&& other)
|
|
{
|
|
if (&other != this) {
|
|
for (auto& fn : other._moveFunc) {
|
|
fn(&other, this);
|
|
}
|
|
|
|
// _copyFunc = std::move(other._copyFunc);
|
|
// _moveFunc = std::move(other._moveFunc);
|
|
// _clearFunc = std::move(other._clearFunc);
|
|
|
|
_copyFunc = other._copyFunc;
|
|
_moveFunc = other._moveFunc;
|
|
_clearFunc = other._clearFunc;
|
|
|
|
_ident = std::move(other._ident);
|
|
_accessType = std::move(other._accessType);
|
|
_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();
|
|
return _serializerFunc(this);
|
|
}
|
|
|
|
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);
|
|
_deserializerFunc(sval, this);
|
|
|
|
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()));
|
|
_deserializerFunc(serialized_t(sval.begin(), sval.end()), this);
|
|
} 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
|