#pragma once /* ABSTRACT DEVICE COMPONENTS LIBRARY */ #include #include #include "../common/adc_traits.h" #include "../common/adc_utils.h" // #include "../common/adc_value.h" namespace adc { namespace constants { static constexpr std::tuple AdcDefaultTrivialConvTypes{}; } // namespace constants namespace traits { /* concepts */ // attribute setter concept template concept adc_attr_getter_c = std::is_null_pointer_v || (traits::adc_func_traits::arity == 0 && !std::same_as, void>); // attribute setter concept template concept adc_attr_setter_c = std::is_null_pointer_v || (traits::adc_func_traits::arity == 1); // internal-to-user and user-to-internal type conversional function concept template concept adc_attr_convfunc_c = std::is_null_pointer_v || (traits::adc_func_traits::arity == 1 && !std::same_as, void>); // deduce attribute type from getter and setter functions signatures template using attr_value_t = std::conditional_t, std::conditional_t, std::nullptr_t, traits::adc_func_arg1_t>, traits::adc_retval_t>; // deduce attribute internal type from conversional functions signatures template using attr_internal_t = std::conditional_t< std::is_null_pointer_v, std::conditional_t, std::nullptr_t, traits::adc_retval_t>, traits::adc_retval_t>; // deduce user-defined type from conversional functions signatures template using attr_user_t = std::conditional_t< std::is_null_pointer_v, std::conditional_t, std::nullptr_t, traits::adc_retval_t>, traits::adc_retval_t>; // attribute serializer function concept template concept adc_serializer_c = std::invocable && !std::same_as>; // attribute deserializer function concept template concept adc_deserializer_c = std::invocable && !std::same_as>; // a callable and it has exactly one argument template concept adc_is_setter = adc_func_traits::arity == 1; // a callable with exactly one argument of type VT template concept adc_is_setter_arg = (adc_func_traits::arity == 1) && std::same_as>; // a callable with exactly one argument of type VT and it returns a value of type ST template concept adc_is_serializer = (adc_func_traits::arity == 1) && std::same_as> && std::same_as>; } // 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 : 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(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(ec), AdcDeviceAttributeErrorCategory::get()); } template class AdcDeviceAttribute { protected: template using ret_value_t = std::decay_t>; template inline static std::unordered_map> _getterFunc{}; template inline static std::unordered_map> _setterFunc{}; template inline static std::unordered_map> _serializerFunc{}; template inline static std::unordered_map> _deserializerFunc{}; std::function _clearFunc; std::function _copyFunc; std::function _moveFunc; public: typedef IdentT ident_t; typedef std::string default_serialized_t; enum AccessType { ReadOnly, WriteOnly, ReadWrite }; template constexpr static T DummyGetter() { return T(); } template constexpr static void DummySetter(const T&) { } template constexpr static SerializedT DummySerializer(const T&) { return SerializedT(); } template constexpr static T DummyDeserializer(const SerializedT&) { return T(); } /* CONSTRUCTORS AND DESTRUCTOR */ template GT, typename ValueT = ret_value_t, std::invocable ST, std::invocable SRT = decltype(utils::AdcDefaultValueConverter<>::serialize), typename SerializedT = ret_value_t, std::invocable DSRT = decltype(utils::AdcDefaultValueConverter<>::deserialize)> AdcDeviceAttribute(GT&& getter, ST&& setter, SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize, DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize) { _getterFunc.emplace(this, std::forward(getter)); _setterFunc.emplace(this, std::forward(setter)); _serializerFunc[this] = [wrapper = traits::adc_pf_wrapper(std::forward(serializer)), this]() { auto& serializer = std::get<0>(wrapper); auto val = _getterFunc[this](); return serializer(val); }; _deserializerFunc[this] = [wrapper = traits::adc_pf_wrapper(std::forward(deserializer)), this](const SerializedT& sval) { auto& deserializer = std::get<0>(wrapper); ValueT val = deserializer(sval); _setterFunc[this](val); }; _clearFunc = [this]() { _getterFunc.erase(this); _setterFunc.erase(this); _serializerFunc.erase(this); _deserializerFunc.erase(this); }; // copy TO other _copyFunc = [this](AdcDeviceAttribute* other) { _getterFunc.emplace(other, _getterFunc[this]); _setterFunc.emplace(other, _setterFunc[this]); _serializerFunc.emplace(other, _serializerFunc[this]); _deserializerFunc.emplace(other, _deserializerFunc[this]); }; // move TO other _moveFunc = [this](AdcDeviceAttribute* other) { _getterFunc.emplace(other, std::move(_getterFunc[this])); _setterFunc.emplace(other, std::move(_setterFunc[this])); _serializerFunc.emplace(other, std::move(_serializerFunc[this])); _deserializerFunc.emplace(other, std::move(_deserializerFunc[this])); }; } template TupleT, std::invocable<> GT, typename ValueT = ret_value_t, std::invocable ST, std::invocable SRT = decltype(utils::AdcDefaultValueConverter<>::serialize), typename SerializedT = ret_value_t, std::invocable DSRT = decltype(utils::AdcDefaultValueConverter<>::deserialize)> AdcDeviceAttribute(TupleT&&, GT&& getter, ST&& setter, SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize, DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize) : AdcDeviceAttribute(std::forward(getter), std::forward(setter), std::forward(serializer), std::forward(deserializer)) { AdcDeviceAttribute::setupTrivialConvertFunc>(); } // read-only attribute constructor template GT, typename ValueT = ret_value_t, std::invocable SRT = decltype(utils::AdcDefaultValueConverter<>::serialize), typename SerializedT = ret_value_t> AdcDeviceAttribute(const IdentT& ident, GT&& getter, SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize) : AdcDeviceAttribute(ident, std::forward(getter), AdcDeviceAttribute::DummySetter, std::forward(serializer), AdcDeviceAttribute::DummyDeserializer) { _accessType = ReadOnly; } template TupleT, std::invocable<> GT, typename ValueT = ret_value_t, std::invocable SRT = decltype(utils::AdcDefaultValueConverter<>::serialize), typename SerializedT = ret_value_t> AdcDeviceAttribute(const IdentT& ident, TupleT&&, GT&& getter, SRT&& serializer = utils::AdcDefaultValueConverter<>::serialize) : AdcDeviceAttribute(ident, TupleT{}, std::forward(getter), AdcDeviceAttribute::DummySetter, std::forward(serializer), AdcDeviceAttribute::DummyDeserializer) { _accessType = ReadOnly; } // write-only attribute constructor template ST, typename ValueT = std::decay_t>, typename DSRT = decltype(utils::AdcDefaultValueConverter<>::deserialize), typename SerializedT = std::decay_t>> AdcDeviceAttribute(const IdentT& ident, ST&& setter, DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize) : AdcDeviceAttribute(ident, AdcDeviceAttribute::DummyGetter, std::forward(setter), AdcDeviceAttribute::DummySerializer, std::forward(deserializer)) { _accessType = WriteOnly; } template TupleT, traits::adc_is_setter<> ST, typename ValueT = std::decay_t>, typename DSRT = decltype(utils::AdcDefaultValueConverter<>::deserialize), typename SerializedT = std::decay_t>> AdcDeviceAttribute(const IdentT& ident, TupleT&&, ST&& setter, DSRT&& deserializer = utils::AdcDefaultValueConverter<>::deserialize) : AdcDeviceAttribute(ident, TupleT{}, AdcDeviceAttribute::DummyGetter, std::forward(setter), AdcDeviceAttribute::DummySerializer, std::forward(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 AdcDeviceAttribute& addConvertFunc(FromFuncT&& func_from_internal, ToFuncT&& func_to_internal) requires std::invocable&> && std::invocable&> { using value_t = ret_value_t; // it must be internal value type using user_t = ret_value_t; _getterFunc[this] = [wrapper = traits::adc_pf_wrapper(std::forward(func_from_internal)), this]() { auto& getter = _getterFunc[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[this] = [wrapper = traits::adc_pf_wrapper(std::forward(func_to_internal)), this](const user_t& val) { value_t value = std::get<0>(wrapper)(val); // convert to internal type auto& setter = _setterFunc[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.erase(this); _setterFunc.erase(this); }; // copy TO other _copyFunc = [prev_copy = _copyFunc, this](AdcDeviceAttribute* other) { prev_copy(other); _getterFunc.emplace(other, _getterFunc[this]); _setterFunc.emplace(other, _setterFunc[this]); }; // move TO other _moveFunc = [prev_move = _moveFunc, this](AdcDeviceAttribute* other) { prev_move(other); _getterFunc.emplace(other, std::move(_getterFunc[this])); _setterFunc.emplace(other, std::move(_setterFunc[this])); }; return *this; } template operator UT() const { if (_accessType == WriteOnly) { throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_WRITE_ONLY); } using val_t = std::decay_t; auto getter = _getterFunc[this]; if (getter) { return getter(); } throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_NO_CONV_FUNC); } template AdcDeviceAttribute& operator=(UT&& other) { if (_accessType == ReadOnly) { throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_READ_ONLY); } using val_t = std::decay_t; auto setter = _setterFunc[this]; if (setter) { setter(std::forward(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 SerializedT serialize() { using s_t = std::decay_t; auto& serializer = _serializerFunc[this]; if (serializer) { return serializer(); } throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_INVALID_SERIALIZED_TYPE); } template void deserialize(const SerializedT& sval) { using s_t = std::decay_t; auto& deserializer = _deserializerFunc[this]; if (deserializer) { deserializer(sval); } else { throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_INVALID_SERIALIZED_TYPE); } } protected: IdentT _ident; AccessType _accessType; template void setupTrivialConvertFuncImpl() { if constexpr (I < std::tuple_size_v) { using elem_t = std::tuple_element_t; if constexpr (!std::is_same_v) { addConvertFunc([](const VT& v) { return static_cast(v); }, [](const elem_t& v) { return static_cast(v); }); } setupTrivialConvertFuncImpl(); } } template void setupTrivialConvertFunc() { setupTrivialConvertFuncImpl(); } }; } // namespace adc