245 lines
7.5 KiB
C++
245 lines
7.5 KiB
C++
#pragma once
|
|
|
|
/*
|
|
|
|
ABSTRACT DEVICE COMPONENTS LIBRARY
|
|
|
|
*/
|
|
|
|
|
|
#include "../common/adc_value.h"
|
|
|
|
|
|
namespace adc
|
|
{
|
|
|
|
// error codes
|
|
enum class AdcDeviceAttributeErrorCode : int { ERROR_OK, ERROR_READ_ONLY, ERROR_WRITE_ONLY };
|
|
|
|
} // 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_READ_ONLY:
|
|
return "device attribute is read-only";
|
|
case AdcDeviceAttributeErrorCode::ERROR_WRITE_ONLY:
|
|
return "device attribute is write-only";
|
|
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, typename SerializedT = std::string>
|
|
class AdcDeviceAttribute : protected AdcSerializingValueHolder<SerializedT>
|
|
{
|
|
typedef AdcSerializingValueHolder<SerializedT> base_t;
|
|
|
|
template <typename T>
|
|
using ret_value_t = base_t::template ret_value_t<T>;
|
|
|
|
public:
|
|
typedef IdentT ident_t;
|
|
typedef SerializedT 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>
|
|
constexpr static SerializedT DummySerializer(const T&)
|
|
{
|
|
return SerializedT();
|
|
}
|
|
|
|
template <typename T>
|
|
constexpr static T DummyDeserializer(const SerializedT&)
|
|
{
|
|
return T();
|
|
}
|
|
|
|
|
|
/* CONSTRUCTORS AND DESTRUCTOR */
|
|
|
|
// general read-write attribute constructor
|
|
template <typename... SerValHolderTs>
|
|
AdcDeviceAttribute(const IdentT& ident, SerValHolderTs&&... ctor_args)
|
|
: base_t(std::forward<SerValHolderTs>(ctor_args)...), _ident(ident), _accessType(ReadWrite)
|
|
{
|
|
}
|
|
|
|
|
|
// read-only attribute constructor
|
|
template <typename GT,
|
|
typename VALT = ret_value_t<GT>,
|
|
typename SRT = decltype(utils::AdcDefaultValueConverter<>::serialize<SerializedT, VALT>)>
|
|
AdcDeviceAttribute(const IdentT& ident,
|
|
GT&& getter,
|
|
SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<SerializedT, VALT>)
|
|
requires std::invocable<GT> && std::invocable<SRT, const VALT&>
|
|
: AdcDeviceAttribute(ident,
|
|
std::forward<GT>(getter),
|
|
AdcDeviceAttribute::DummySetter<VALT>,
|
|
AdcValueHolder::AdcValueHolderDummyValidator<VALT>,
|
|
std::forward<SRT>(serializer),
|
|
AdcDeviceAttribute::DummyDeserializer<VALT>)
|
|
{
|
|
_accessType = ReadOnly;
|
|
}
|
|
|
|
|
|
template <typename GT,
|
|
typename VALT = ret_value_t<GT>,
|
|
typename SRT = decltype(utils::AdcDefaultValueConverter<>::serialize<SerializedT, VALT>)>
|
|
AdcDeviceAttribute(const IdentT& ident,
|
|
traits::adc_tuple_like<> auto&& tpl,
|
|
GT&& getter,
|
|
SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize<SerializedT, VALT>)
|
|
requires std::invocable<GT> && std::invocable<SRT, const VALT&>
|
|
: AdcDeviceAttribute(ident,
|
|
std::forward<decltype(tpl)>(tpl),
|
|
std::forward<GT>(getter),
|
|
AdcDeviceAttribute::DummySetter<VALT>,
|
|
AdcValueHolder::AdcValueHolderDummyValidator<VALT>,
|
|
std::forward<SRT>(serializer),
|
|
AdcDeviceAttribute::DummyDeserializer<VALT>)
|
|
{
|
|
_accessType = ReadOnly;
|
|
}
|
|
|
|
|
|
// write-only attribute constructor
|
|
template <typename ST,
|
|
typename VALT = std::decay_t<traits::adc_func_arg1_t<ST>>,
|
|
typename DSRT = decltype(utils::AdcDefaultValueConverter<>::deserialize<VALT, SerializedT>)>
|
|
AdcDeviceAttribute(const IdentT& ident,
|
|
ST&& setter,
|
|
std::predicate<const VALT&> auto&& validator,
|
|
DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize<VALT, SerializedT>)
|
|
requires std::invocable<DSRT, const SerializedT&>
|
|
: AdcDeviceAttribute(ident,
|
|
AdcDeviceAttribute::DummyGetter<VALT>,
|
|
std::forward<ST>(setter),
|
|
std::forward<decltype(validator)>(validator),
|
|
AdcDeviceAttribute::DummySerializer<VALT>,
|
|
std::forward<DSRT>(deserializer))
|
|
{
|
|
_accessType = WriteOnly;
|
|
}
|
|
|
|
|
|
template <typename ST,
|
|
typename VALT = std::decay_t<traits::adc_func_arg1_t<ST>>,
|
|
typename DSRT = decltype(utils::AdcDefaultValueConverter<>::deserialize<VALT, SerializedT>)>
|
|
AdcDeviceAttribute(const IdentT& ident,
|
|
traits::adc_tuple_like<> auto&& tpl,
|
|
ST&& setter,
|
|
std::predicate<const VALT&> auto&& validator,
|
|
DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize<VALT, SerializedT>)
|
|
requires std::invocable<DSRT, const SerializedT&>
|
|
: AdcDeviceAttribute(ident,
|
|
std::forward<decltype(tpl)>(tpl),
|
|
AdcDeviceAttribute::DummyGetter<VALT>,
|
|
std::forward<ST>(setter),
|
|
std::forward<decltype(validator)>(validator),
|
|
AdcDeviceAttribute::DummySerializer<VALT>,
|
|
std::forward<DSRT>(deserializer))
|
|
{
|
|
_accessType = WriteOnly;
|
|
}
|
|
|
|
|
|
AdcDeviceAttribute(const AdcDeviceAttribute&) = default;
|
|
AdcDeviceAttribute(AdcDeviceAttribute&&) = default;
|
|
|
|
|
|
virtual ~AdcDeviceAttribute() = default;
|
|
|
|
IdentT ident() const { return _ident; }
|
|
|
|
AccessType accessType() const { return _accessType; }
|
|
|
|
template <typename UT>
|
|
operator UT() const
|
|
{
|
|
if (_accessType == WriteOnly) {
|
|
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_WRITE_ONLY);
|
|
}
|
|
|
|
return base_t::operator UT();
|
|
}
|
|
|
|
AdcDeviceAttribute& operator=(const AdcDeviceAttribute&) = default;
|
|
AdcDeviceAttribute& operator=(AdcDeviceAttribute&&) = default;
|
|
|
|
template <typename UT>
|
|
AdcDeviceAttribute& operator=(UT&& value)
|
|
{
|
|
if (_accessType == ReadOnly) {
|
|
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_READ_ONLY);
|
|
}
|
|
|
|
base_t::operator=(std::forward<UT>(value));
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
protected:
|
|
IdentT _ident;
|
|
AccessType _accessType;
|
|
};
|
|
|
|
} // namespace adc
|