Compare commits

..

2 Commits

Author SHA1 Message Date
Timur A. Fatkhullin
c45dceaf0e BIG FIXES!!!
convert functions are now not getter/setter!!!
2024-12-15 02:31:26 +03:00
Timur A. Fatkhullin
9af89bd180 ... 2024-12-14 22:26:42 +03:00
2 changed files with 173 additions and 52 deletions

View File

@ -202,9 +202,22 @@ protected:
template <typename VT>
inline static std::unordered_map<const AdcDeviceAttribute*, std::function<void(const VT&)>> _setterFunc{};
std::function<SerializedT()> _serializerFunc;
// from user to inner type converter
template <typename UT>
inline static std::unordered_map<const AdcDeviceAttribute*,
std::function<void(const UT&, const AdcDeviceAttribute*)>>
_convFuncTo{};
std::function<void(const SerializedT&)> _deserializerFunc;
// 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{};
@ -254,34 +267,50 @@ public:
}
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]() {
auto& serializer = std::get<0>(wrapper);
// 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 = getter_func();
// auto val = _getterFunc<ValueT>[this]();
auto val = _getterFunc<ValueT>[inst]();
// auto val = getter_func();
return serializer(val);
};
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) {
// 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);
setter_func(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);
});
@ -289,12 +318,18 @@ public:
_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]));
});
}
@ -409,9 +444,13 @@ public:
fn(&other, this);
}
_copyFunc = std::move(other._copyFunc);
_moveFunc = std::move(other._moveFunc);
_clearFunc = std::move(other._clearFunc);
// _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);
@ -457,54 +496,86 @@ public:
"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
// 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 = _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
};
_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) {
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) {
_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
// throw out_of_range if value_t is invalid
// _setterFunc<value_t>.at(this)(value);
setter(value);
_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);
// _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]);
// _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]));
// _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;
@ -520,7 +591,8 @@ public:
using val_t = std::decay_t<UT>;
try {
return _getterFunc<val_t>.at(this)();
// 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);
}
@ -537,7 +609,8 @@ public:
using val_t = std::decay_t<UT>;
try {
_setterFunc<val_t>.at(this)(std::forward<UT>(val));
// _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);
}
@ -574,9 +647,13 @@ public:
fn(&other, this);
}
_copyFunc = std::move(other._copyFunc);
_moveFunc = std::move(other._moveFunc);
_clearFunc = std::move(other._clearFunc);
// _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);
@ -593,7 +670,8 @@ public:
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_WRITE_ONLY);
}
return _serializerFunc();
// return _serializerFunc();
return _serializerFunc(this);
}
template <typename ST>
@ -616,7 +694,8 @@ public:
throw std::system_error(AdcDeviceAttributeErrorCode::ERROR_READ_ONLY);
}
_deserializerFunc(sval);
// _deserializerFunc(sval);
_deserializerFunc(sval, this);
return *this;
}
@ -628,7 +707,8 @@ public:
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()));
_deserializerFunc(serialized_t(sval.begin(), sval.end()), this);
} else {
static_assert(false, "INVALID USER SERIALIZATION TYPE!");
}
@ -659,6 +739,8 @@ protected:
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); });
} else {
std::cout << "SKIP! I = " << I << "\n";
}
setupTrivialConvertFuncImpl<VT, I + 1, TupleT>();

View File

@ -107,16 +107,54 @@ int main()
dev_t dev1("DEV1");
int attr1_val = 10;
dev1.addAttribute(dev_t::attribute_t::makeArithAttr(
"ATTR1", [&attr1_val]() { return attr1_val; }, [&attr1_val](const int& v) { attr1_val = v; }));
dev1.addAttribute(dev_t::attribute_t::makeArithAttr(
"ATTR2", [&attr1_val]() { return attr1_val + 10; }, [&attr1_val](const int& v) { attr1_val = v; },
utils::AdcDefaultValueConverter<>::serialize<dev_t::attribute_t::serialized_t, int>));
dev1.addAttribute(
"ATTR1",
[&attr1_val]() {
std::cout << "ATTR1 getter\n";
return attr1_val;
},
[&attr1_val](const int& v) {
std::cout << "ATTR1 setter\n";
attr1_val = v;
});
dev1.addAttribute(
"ATTR2",
[&attr1_val]() {
std::cout << "ATTR2 getter\n";
return attr1_val + 10;
},
[&attr1_val](const int& v) {
std::cout << "ATTR2 setter\n";
attr1_val = v;
},
utils::AdcDefaultValueConverter<>::serialize<dev_t::attribute_t::serialized_t, int>);
// dev1.addAttribute(dev_t::attribute_t::makeArithAttr(
// "ATTR1",
// [&attr1_val]() {
// std::cout << "ATTR1 getter\n";
// return attr1_val;
// },
// [&attr1_val](const int& v) {
// std::cout << "ATTR1 setter\n";
// attr1_val = v;
// }));
// dev1.addAttribute(dev_t::attribute_t::makeArithAttr(
// "ATTR2",
// [&attr1_val]() {
// std::cout << "ATTR2 getter\n";
// return (long)attr1_val + 10;
// },
// [&attr1_val](const long& v) {
// std::cout << "ATTR2 setter\n";
// attr1_val = v;
// },
// utils::AdcDefaultValueConverter<>::serialize<dev_t::attribute_t::serialized_t, long>));
devs.push_back({&dev1, {'D', '1'}});
serialized_t sn;
std::ranges::copy(std::string_view("ATTR2"), std::back_inserter(sn));
// std::ranges::copy(std::string_view("ATTR2"), std::back_inserter(sn));
std::ranges::copy(std::string_view("ATTR1"), std::back_inserter(sn));
devs[0].setAttr(sn, {'7', '7'});
auto r = getAttr(0, sn);
@ -124,6 +162,7 @@ int main()
std::string rs;
std::ranges::copy(r, std::back_inserter(rs));
// std::cout << "ATTR2 = " << rs << "\n";
std::cout << "ATTR1 = " << rs << "\n";