working tests/adc_asio_netserver_test.cpp
This commit is contained in:
parent
2f46b08c8e
commit
75b42f40e5
@ -206,10 +206,13 @@ protected:
|
|||||||
|
|
||||||
std::function<void(const SerializedT&)> _deserializerFunc;
|
std::function<void(const SerializedT&)> _deserializerFunc;
|
||||||
|
|
||||||
static inline std::vector<std::function<void(AdcDeviceAttribute*)>> _clearFunc{};
|
// static inline std::vector<std::function<void(AdcDeviceAttribute*)>> _clearFunc{};
|
||||||
|
std::vector<std::function<void(AdcDeviceAttribute*)>> _clearFunc{};
|
||||||
|
|
||||||
static inline std::vector<std::function<void(const AdcDeviceAttribute*, AdcDeviceAttribute*)>> _copyFunc{};
|
// static inline std::vector<std::function<void(const AdcDeviceAttribute*, AdcDeviceAttribute*)>> _copyFunc{};
|
||||||
static inline std::vector<std::function<void(AdcDeviceAttribute*, AdcDeviceAttribute*)>> _moveFunc{};
|
// static inline std::vector<std::function<void(AdcDeviceAttribute*, AdcDeviceAttribute*)>> _moveFunc{};
|
||||||
|
std::vector<std::function<void(const AdcDeviceAttribute*, AdcDeviceAttribute*)>> _copyFunc{};
|
||||||
|
std::vector<std::function<void(AdcDeviceAttribute*, AdcDeviceAttribute*)>> _moveFunc{};
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -251,11 +254,12 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
if constexpr (!std::is_null_pointer_v<GT> && !std::is_null_pointer_v<SRT>) {
|
if constexpr (!std::is_null_pointer_v<GT> && !std::is_null_pointer_v<SRT>) {
|
||||||
_serializerFunc = [wrapper = traits::adc_pf_wrapper(std::forward<SRT>(serializer)), this]() {
|
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& serializer = std::get<0>(wrapper);
|
||||||
|
|
||||||
|
// auto val = _getterFunc<ValueT>[this]();
|
||||||
auto val = _getterFunc<ValueT>[this]();
|
auto val = getter_func();
|
||||||
|
|
||||||
return serializer(val);
|
return serializer(val);
|
||||||
};
|
};
|
||||||
@ -263,13 +267,15 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
if constexpr (!std::is_null_pointer_v<ST> && !std::is_null_pointer_v<DSRT>) {
|
if constexpr (!std::is_null_pointer_v<ST> && !std::is_null_pointer_v<DSRT>) {
|
||||||
_deserializerFunc = [wrapper = traits::adc_pf_wrapper(std::forward<DSRT>(deserializer)),
|
auto& setter_func = _setterFunc<ValueT>[this];
|
||||||
|
_deserializerFunc = [setter_func, wrapper = traits::adc_pf_wrapper(std::forward<DSRT>(deserializer)),
|
||||||
this](const SerializedT& sval) {
|
this](const SerializedT& sval) {
|
||||||
auto& deserializer = std::get<0>(wrapper);
|
auto& deserializer = std::get<0>(wrapper);
|
||||||
|
|
||||||
ValueT val = deserializer(sval);
|
ValueT val = deserializer(sval);
|
||||||
|
|
||||||
AdcDeviceAttribute::_setterFunc<ValueT>[this](val);
|
// _setterFunc<ValueT>[this](val);
|
||||||
|
setter_func(val);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -379,10 +385,14 @@ public:
|
|||||||
AdcDeviceAttribute(const AdcDeviceAttribute& other)
|
AdcDeviceAttribute(const AdcDeviceAttribute& other)
|
||||||
{
|
{
|
||||||
if (&other != this) {
|
if (&other != this) {
|
||||||
for (auto& fn : _copyFunc) {
|
// for (auto& fn : _copyFunc) {
|
||||||
|
for (auto& fn : other._copyFunc) {
|
||||||
fn(&other, this);
|
fn(&other, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_copyFunc = other._copyFunc;
|
||||||
|
_moveFunc = other._moveFunc;
|
||||||
|
_clearFunc = other._clearFunc;
|
||||||
|
|
||||||
_ident = other._ident;
|
_ident = other._ident;
|
||||||
_accessType = other._accessType;
|
_accessType = other._accessType;
|
||||||
@ -394,10 +404,15 @@ public:
|
|||||||
AdcDeviceAttribute(AdcDeviceAttribute&& other)
|
AdcDeviceAttribute(AdcDeviceAttribute&& other)
|
||||||
{
|
{
|
||||||
if (&other != this) {
|
if (&other != this) {
|
||||||
for (auto& fn : _moveFunc) {
|
// for (auto& fn : _moveFunc) {
|
||||||
|
for (auto& fn : other._moveFunc) {
|
||||||
fn(&other, this);
|
fn(&other, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_copyFunc = std::move(other._copyFunc);
|
||||||
|
_moveFunc = std::move(other._moveFunc);
|
||||||
|
_clearFunc = std::move(other._clearFunc);
|
||||||
|
|
||||||
_ident = std::move(other._ident);
|
_ident = std::move(other._ident);
|
||||||
_accessType = std::move(other._accessType);
|
_accessType = std::move(other._accessType);
|
||||||
_serializerFunc = std::move(other._serializerFunc);
|
_serializerFunc = std::move(other._serializerFunc);
|
||||||
@ -408,9 +423,9 @@ public:
|
|||||||
|
|
||||||
virtual ~AdcDeviceAttribute()
|
virtual ~AdcDeviceAttribute()
|
||||||
{
|
{
|
||||||
// for (auto& fn : _clearFunc) {
|
for (auto& fn : _clearFunc) {
|
||||||
// fn(this);
|
fn(this);
|
||||||
// };
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -447,7 +462,8 @@ public:
|
|||||||
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] =
|
_getterFunc<user_t>[this] =
|
||||||
[&getter, wrapper = traits::adc_pf_wrapper(std::forward<FromFuncT>(func_from_internal)), 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();
|
auto val = getter();
|
||||||
return std::get<0>(wrapper)(val); // convert from internal type
|
return std::get<0>(wrapper)(val); // convert from internal type
|
||||||
};
|
};
|
||||||
@ -458,10 +474,13 @@ public:
|
|||||||
if (_accessType != AdcDeviceAttribute::ReadOnly) {
|
if (_accessType != AdcDeviceAttribute::ReadOnly) {
|
||||||
auto& setter = _setterFunc<value_t>.at(this); // throw out_of_range if value_t is invalid
|
auto& setter = _setterFunc<value_t>.at(this); // throw out_of_range if value_t is invalid
|
||||||
|
|
||||||
_setterFunc<user_t>[this] = [&setter,
|
_setterFunc<user_t>[this] = [setter,
|
||||||
wrapper = traits::adc_pf_wrapper(std::forward<ToFuncT>(func_to_internal)),
|
wrapper = traits::adc_pf_wrapper(std::forward<ToFuncT>(func_to_internal)),
|
||||||
this](const user_t& val) {
|
this](const user_t& val) {
|
||||||
value_t value = std::get<0>(wrapper)(val); // convert to internal type
|
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);
|
setter(value);
|
||||||
};
|
};
|
||||||
} // ignore "to_internal" conversional function for read-only attribute
|
} // ignore "to_internal" conversional function for read-only attribute
|
||||||
@ -534,6 +553,10 @@ public:
|
|||||||
fn(&other, this);
|
fn(&other, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_copyFunc = other._copyFunc;
|
||||||
|
_moveFunc = other._moveFunc;
|
||||||
|
_clearFunc = other._clearFunc;
|
||||||
|
|
||||||
_ident = other._ident;
|
_ident = other._ident;
|
||||||
_accessType = other._accessType;
|
_accessType = other._accessType;
|
||||||
_serializerFunc = other._serializerFunc;
|
_serializerFunc = other._serializerFunc;
|
||||||
@ -551,6 +574,9 @@ public:
|
|||||||
fn(&other, this);
|
fn(&other, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_copyFunc = std::move(other._copyFunc);
|
||||||
|
_moveFunc = std::move(other._moveFunc);
|
||||||
|
_clearFunc = std::move(other._clearFunc);
|
||||||
|
|
||||||
_ident = std::move(other._ident);
|
_ident = std::move(other._ident);
|
||||||
_accessType = std::move(other._accessType);
|
_accessType = std::move(other._accessType);
|
||||||
|
|||||||
@ -156,21 +156,13 @@ protected:
|
|||||||
// return serialized_t{id.begin(), id.end()};
|
// return serialized_t{id.begin(), id.end()};
|
||||||
// };
|
// };
|
||||||
|
|
||||||
_get_attr = [dev_ptr, wrapper = traits::adc_pf_wrapper(std::forward<AttrIdDeserialT>(attr_id_deser_func))](
|
_get_attr = [dev_ptr, wrapper = traits::adc_pf_wrapper(std::forward<AttrIdDeserialT>(attr_id_deser_func)),
|
||||||
const auto& attr_name) mutable {
|
this](const auto& attr_name) mutable {
|
||||||
auto attr_id = std::get<0>(wrapper)(attr_name);
|
auto attr_id = std::get<0>(wrapper)(attr_name);
|
||||||
auto& attr = (*dev_ptr)[attr_id];
|
// auto& attr = (*dev_ptr)[attr_id];
|
||||||
|
auto& attr = dev_ptr->operator[](attr_id);
|
||||||
auto val = attr.serialize();
|
auto val = attr.serialize();
|
||||||
return val;
|
return val;
|
||||||
// auto val = (*dev_ptr)[attr_id].serialize();
|
|
||||||
using val_t = std::remove_cvref_t<decltype(val)>;
|
|
||||||
|
|
||||||
if constexpr (std::same_as<serialized_t, val_t> || std::convertible_to<val_t, serialized_t>) {
|
|
||||||
return val;
|
|
||||||
} else {
|
|
||||||
// !!!!!!!! TODO: val_t must be a char range
|
|
||||||
return serialized_t{val.begin(), val.end()};
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
_set_attr = [dev_ptr, wrapper = traits::adc_pf_wrapper(std::forward<AttrIdDeserialT>(attr_id_deser_func))](
|
_set_attr = [dev_ptr, wrapper = traits::adc_pf_wrapper(std::forward<AttrIdDeserialT>(attr_id_deser_func))](
|
||||||
|
|||||||
@ -9,7 +9,7 @@
|
|||||||
#include "../net/asio/adc_device_netserver_asio.h"
|
#include "../net/asio/adc_device_netserver_asio.h"
|
||||||
|
|
||||||
typedef adc::impl::AdcDeviceNetServerASIO server_t;
|
typedef adc::impl::AdcDeviceNetServerASIO server_t;
|
||||||
|
typedef adc::AdcDeviceAttribute<std::string, server_t::serialized_t> attr_t;
|
||||||
|
|
||||||
class Device1 : public adc::AdcGenericDevice<std::string,
|
class Device1 : public adc::AdcGenericDevice<std::string,
|
||||||
adc::AdcDeviceAttribute<std::string, server_t::serialized_t>,
|
adc::AdcDeviceAttribute<std::string, server_t::serialized_t>,
|
||||||
@ -66,8 +66,10 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
// device#1
|
// device#1
|
||||||
dev1.addCommand("DEV1::COM1", []() { std::cout << "EXEC DEV1::COM1\n"; });
|
dev1.addCommand("DEV1::COM1", []() { std::cout << "EXEC DEV1::COM1\n"; });
|
||||||
dev1.addAttribute(
|
// dev1.addAttribute(
|
||||||
"DEV1::ATTR1", [&dev1_val1]() { return dev1_val1; }, [&dev1_val1](const int& v) { dev1_val1 = v; });
|
// "DEV1::ATTR1", [&dev1_val1]() { return dev1_val1; }, [&dev1_val1](const int& v) { dev1_val1 = v; });
|
||||||
|
dev1.addAttribute(Device1::attribute_t::makeArithAttr(
|
||||||
|
"DEV1::ATTR1", [&dev1_val1]() { return dev1_val1; }, [&dev1_val1](const int& v) { dev1_val1 = v; }));
|
||||||
|
|
||||||
dev1.addAttribute("DEV1::ATTR2", gt, st);
|
dev1.addAttribute("DEV1::ATTR2", gt, st);
|
||||||
|
|
||||||
@ -75,7 +77,24 @@ int main(int argc, char* argv[])
|
|||||||
// read-only attr
|
// read-only attr
|
||||||
dev2.addAttribute(0x1ul, [&dev1_val1]() { return dev1_val1; });
|
dev2.addAttribute(0x1ul, [&dev1_val1]() { return dev1_val1; });
|
||||||
// write-only
|
// write-only
|
||||||
dev2.addAttribute(0xfful, [&dev1_val1](const int& v) { dev1_val1 = v; });
|
// dev2.addAttribute(0xfful, [&dev1_val1](const int& v) { dev1_val1 = v; });
|
||||||
|
dev2.addAttribute(Device2::attribute_t::makeArithAttr(0xfful, [&dev1_val1](const int& v) { dev1_val1 = v; }));
|
||||||
|
|
||||||
|
|
||||||
|
double f = 7.7;
|
||||||
|
std::cout << "SET DEV1['DEV1::ATTR1'] to " << f;
|
||||||
|
dev1["DEV1::ATTR1"] = f;
|
||||||
|
std::cout << "\tRESULT ( " << dev1_val1 << " )\n";
|
||||||
|
|
||||||
|
dev1_val1 = 111;
|
||||||
|
std::cout << "GET DEV1['DEV1::ATTR1']";
|
||||||
|
float fl = dev1["DEV1::ATTR1"];
|
||||||
|
std::cout << "\tRESULT ( " << fl << " )\n";
|
||||||
|
|
||||||
|
f *= 3.3;
|
||||||
|
std::cout << "SET DEV2['0xff'] to " << f;
|
||||||
|
dev2[0xff] = f;
|
||||||
|
std::cout << "\tRESULT ( " << dev1_val1 << " )\n";
|
||||||
|
|
||||||
|
|
||||||
/* COMMANDLINE OPTS */
|
/* COMMANDLINE OPTS */
|
||||||
|
|||||||
@ -66,7 +66,7 @@ TEST_CASE("[ADC DEVICE ATTRIBUTE]")
|
|||||||
auto ar1 = ar;
|
auto ar1 = ar;
|
||||||
std::cout << "ATTR1_RO = " << (double)ar1 << "\n";
|
std::cout << "ATTR1_RO = " << (double)ar1 << "\n";
|
||||||
|
|
||||||
auto ar2(ar1);
|
auto ar2(std::move(ar1));
|
||||||
std::cout << "ATTR2_RO = " << (double)ar2 << "\n";
|
std::cout << "ATTR2_RO = " << (double)ar2 << "\n";
|
||||||
|
|
||||||
std::cout << "ATTR2_RO_SER = " << ar2.serialize() << "\n";
|
std::cout << "ATTR2_RO_SER = " << ar2.serialize() << "\n";
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user