ADC/device/adc_device_attribute.h
Timur A. Fatkhullin 5dcc57707b due to GCC strange behavior with cast operator AdcDeviceAttribute class is now
has its own full "value holder" implementation without inheritance from
AdcValueHolder
2024-05-15 02:27:16 +03:00

578 lines
18 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
{
// a callable and it has exactly one argument
template <typename T>
concept adc_is_setter = adc_func_traits<T>::arity == 1;
// a callable with exactly one argument of type VT
template <typename T, typename VT>
concept adc_is_setter_arg = (adc_func_traits<T>::arity == 1) && std::same_as<VT, adc_func_arg1_t<T>>;
// a callable with exactly one argument of type VT and it returns a value of type ST
template <typename T, typename ST, typename VT>
concept adc_is_serializer =
(adc_func_traits<T>::arity == 1) && std::same_as<VT, adc_func_arg1_t<T>> && std::same_as<ST, adc_retval_t<T>>;
} // 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
};
} // 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";
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 };
template <typename T>
constexpr static T DummyGetter()
{
return T();
}
template <typename T>
constexpr static void DummySetter(const T&)
{
}
template <typename T, typename SerializedT = default_serialized_t>
constexpr static SerializedT DummySerializer(const T&)
{
return SerializedT();
}
template <typename T, typename SerializedT = default_serialized_t>
constexpr static T DummyDeserializer(const SerializedT&)
{
return T();
}
/* CONSTRUCTORS AND DESTRUCTOR */
template <std::invocable<> GT,
typename ValueT = ret_value_t<GT>,
std::invocable<const ValueT&> ST,
std::invocable<const ValueT&> SRT =
decltype(utils::AdcDefaultValueConverter<>::serialize<default_serialized_t, ValueT>),
typename SerializedT = ret_value_t<SRT>,
std::invocable<const SerializedT&> DSRT =
decltype(utils::AdcDefaultValueConverter<>::deserialize<ValueT, SerializedT>)>
AdcDeviceAttribute(GT&& getter,
ST&& setter,
SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<default_serialized_t, ValueT>,
DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize<ValueT, SerializedT>)
{
_getterFunc<ValueT>.emplace(this, std::forward<GT>(getter));
_setterFunc<ValueT>.emplace(this, std::forward<ST>(setter));
_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);
};
_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,
std::invocable<> GT,
typename ValueT = ret_value_t<GT>,
std::invocable<const ValueT&> ST,
std::invocable<const ValueT&> SRT =
decltype(utils::AdcDefaultValueConverter<>::serialize<default_serialized_t, ValueT>),
typename SerializedT = ret_value_t<SRT>,
std::invocable<const SerializedT&> DSRT =
decltype(utils::AdcDefaultValueConverter<>::deserialize<ValueT, SerializedT>)>
AdcDeviceAttribute(TupleT&&,
GT&& getter,
ST&& setter,
SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<default_serialized_t, ValueT>,
DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize<ValueT, SerializedT>)
: AdcDeviceAttribute(std::forward<GT>(getter),
std::forward<ST>(setter),
std::forward<SRT>(serializer),
std::forward<DSRT>(deserializer))
{
AdcDeviceAttribute::setupTrivialConvertFunc<ValueT, std::decay_t<TupleT>>();
}
// read-only attribute constructor
template <std::invocable<> GT,
typename ValueT = ret_value_t<GT>,
std::invocable<const 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),
AdcDeviceAttribute::DummySetter<ValueT>,
std::forward<SRT>(serializer),
AdcDeviceAttribute::DummyDeserializer<ValueT, SerializedT>)
{
_accessType = ReadOnly;
}
template <traits::adc_tuple_like<> TupleT,
std::invocable<> GT,
typename ValueT = ret_value_t<GT>,
std::invocable<const 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),
AdcDeviceAttribute::DummySetter<ValueT>,
std::forward<SRT>(serializer),
AdcDeviceAttribute::DummyDeserializer<ValueT, SerializedT>)
{
_accessType = ReadOnly;
}
// write-only attribute constructor
template <traits::adc_is_setter<> 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,
AdcDeviceAttribute::DummyGetter<ValueT>,
std::forward<ST>(setter),
AdcDeviceAttribute::DummySerializer<ValueT>,
std::forward<DSRT>(deserializer))
{
_accessType = WriteOnly;
}
template <traits::adc_tuple_like<> TupleT,
traits::adc_is_setter<> 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{},
AdcDeviceAttribute::DummyGetter<ValueT>,
std::forward<ST>(setter),
AdcDeviceAttribute::DummySerializer<ValueT>,
std::forward<DSRT>(deserializer))
{
_accessType = WriteOnly;
}
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 <typename FromFuncT, typename ToFuncT>
AdcDeviceAttribute& addConvertFunc(FromFuncT&& func_from_internal, ToFuncT&& func_to_internal)
requires std::invocable<FromFuncT, const ret_value_t<ToFuncT>&> &&
std::invocable<ToFuncT, const ret_value_t<FromFuncT>&>
{
using value_t = ret_value_t<ToFuncT>; // it must be internal value type
using user_t = ret_value_t<FromFuncT>;
_getterFunc<user_t>[this] = [wrapper = traits::adc_pf_wrapper(std::forward<FromFuncT>(func_from_internal)),
this]() {
auto& getter = _getterFunc<value_t>[this];
if (getter) {
auto val = getter();
return std::get<0>(wrapper)(val); // convert from internal type
}
// invalid conversion function signature
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_INTERNAL_TYPE_MISMATCH);
};
_setterFunc<user_t>[this] = [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
auto& setter = _setterFunc<value_t>[this];
if (setter) {
setter(value);
} else {
// invalid conversion function signature
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>;
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&& other)
{
if (_accessType == ReadOnly) {
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_READ_ONLY);
}
using val_t = std::decay_t<UT>;
auto setter = _setterFunc<val_t>[this];
if (setter) {
setter(std::forward<UT>(other));
} 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()
{
using s_t = std::decay_t<SerializedT>;
auto& serializer = _serializerFunc<s_t>[this];
if (serializer) {
return serializer();
}
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_INVALID_SERIALIZED_TYPE);
}
template <typename SerializedT>
void deserialize(const SerializedT& sval)
{
using s_t = std::decay_t<SerializedT>;
auto& deserializer = _deserializerFunc<s_t>[this];
if (deserializer) {
deserializer(sval);
} else {
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_INVALID_SERIALIZED_TYPE);
}
}
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