ADC/device/adc_device_attribute.h
2024-10-30 18:20:16 +03:00

686 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>
class AdcDeviceAttribute
{
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{};
template <typename SerializedT>
inline static std::unordered_map<const AdcDeviceAttribute*, std::function<SerializedT()>> _serializerFunc{};
template <typename SerializedT>
inline static std::unordered_map<const AdcDeviceAttribute*, std::function<void(const SerializedT&)>>
_deserializerFunc{};
std::function<void()> _clearFunc;
std::function<void(AdcDeviceAttribute*)> _copyFunc;
std::function<void(AdcDeviceAttribute*)> _moveFunc;
public:
typedef IdentT ident_t;
typedef std::string default_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<default_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>)
: _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));
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<SerializedT>[this] = [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<SerializedT>[this] = [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);
_serializerFunc<SerializedT>.erase(this);
_deserializerFunc<SerializedT>.erase(this);
};
// copy TO other
_copyFunc = [this](AdcDeviceAttribute* other) {
_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]);
};
// 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]));
_serializerFunc<SerializedT>.emplace(other, std::move(_serializerFunc<SerializedT>[this]));
_deserializerFunc<SerializedT>.emplace(other, std::move(_deserializerFunc<SerializedT>[this]));
};
}
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<default_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>)
: 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<default_serialized_t, ValueT>),
typename SerializedT = ret_value_t<SRT>>
AdcDeviceAttribute(const IdentT& ident,
GT&& getter,
SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<default_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<default_serialized_t, ValueT>),
typename SerializedT = ret_value_t<SRT>>
AdcDeviceAttribute(const IdentT& ident,
TupleT&&,
GT&& getter,
SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<default_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, default_serialized_t>),
typename SerializedT = std::decay_t<traits::adc_func_arg1_t<DSRT>>>
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, default_serialized_t>),
typename SerializedT = std::decay_t<traits::adc_func_arg1_t<DSRT>>>
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)
{
_clearFunc();
other._copyFunc(this);
_clearFunc = other._clearFunc;
_copyFunc = other._copyFunc;
_moveFunc = other._moveFunc;
}
AdcDeviceAttribute(AdcDeviceAttribute&& other)
{
_clearFunc();
other._moveFunc(this);
_clearFunc = std::move(other._clearFunc);
_copyFunc = std::move(other._copyFunc);
_moveFunc = std::move(other._moveFunc);
}
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]);
};
// 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]));
};
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);
}
// auto& getter = _getterFunc<val_t>[this];
// if (getter) {
// return getter();
// }
// 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);
}
// 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;
}
AdcDeviceAttribute& operator=(const AdcDeviceAttribute& other)
{
if (&other != this) {
_clearFunc();
other._copyFunc(this);
_clearFunc = other._clearFunc;
_copyFunc = other._copyFunc;
_moveFunc = other._moveFunc;
}
return *this;
}
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);
}
return *this;
}
template <typename SerializedT>
SerializedT 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);
}
default_serialized_t serialize()
{
return serialize<default_serialized_t>();
}
template <typename SerializedT>
AdcDeviceAttribute& deserialize(const SerializedT& sval)
{
if (_accessType == AdcDeviceAttribute::ReadOnly) {
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_READ_ONLY);
}
using s_t = std::decay_t<SerializedT>;
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);
}
// auto& deserializer = _deserializerFunc<s_t>[this];
// if (deserializer) {
// deserializer(sval);
// } else {
// throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_INVALID_SERIALIZED_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