diff --git a/device/adc_device.h b/device/adc_device.h index 8119254..7aac2f2 100644 --- a/device/adc_device.h +++ b/device/adc_device.h @@ -122,7 +122,7 @@ public: AdcGenericDevice& addCommand(CommandT&& cmd) { - _deviceCommands.insert(std::move(cmd)); + _deviceCommands.insert({cmd.ident(), std::move(cmd)}); return *this; } @@ -131,9 +131,10 @@ public: template AdcGenericDevice& addCommand(CtorArgTs&&... ctor_args) { - _deviceCommands.emplace(std::forward(ctor_args)...); + // _deviceCommands.emplace(std::forward(ctor_args)...); + return addCommand({std::forward(ctor_args)...}); - return *this; + // return *this; } AdcGenericDevice& delCommand(const cmd_ident_t& cmd_ident) @@ -146,7 +147,7 @@ public: AdcGenericDevice& addAttribute(AttributeT&& attr) { - _deviceAttributes.insert(std::move(attr)); + _deviceAttributes.insert({attr.ident(), std::move(attr)}); return *this; } @@ -154,9 +155,11 @@ public: template AdcGenericDevice& addAttribute(CtorArgTs&&... ctor_args) { - _deviceAttributes.emplace(std::forward(ctor_args)...); + // _deviceAttributes.emplace(std::forward(ctor_args)...); - return *this; + return addAttribute({std::forward(ctor_args)...}); + + // return *this; } diff --git a/device/adc_device_attribute.h b/device/adc_device_attribute.h index 47a4b63..393dc1a 100644 --- a/device/adc_device_attribute.h +++ b/device/adc_device_attribute.h @@ -294,6 +294,15 @@ public: _serializerFunc.emplace(other, _serializerFunc[this]); _deserializerFunc.emplace(other, _deserializerFunc[this]); + + // define copy-function for newly constructed object + other->_copyFunc = [other](AdcDeviceAttribute* o_next) { + _getterFunc.emplace(o_next, _getterFunc[other]); + _setterFunc.emplace(o_next, _setterFunc[other]); + + _serializerFunc.emplace(o_next, _serializerFunc[other]); + _deserializerFunc.emplace(o_next, _deserializerFunc[other]); + }; }; @@ -304,6 +313,15 @@ public: _serializerFunc.emplace(other, std::move(_serializerFunc[this])); _deserializerFunc.emplace(other, std::move(_deserializerFunc[this])); + + // define move-function for newly constructed object + other->_moveFunc = [other](AdcDeviceAttribute* o_next) { + _getterFunc.emplace(o_next, std::move(_getterFunc[other])); + _setterFunc.emplace(o_next, std::move(_setterFunc[other])); + + _serializerFunc.emplace(o_next, std::move(_serializerFunc[other])); + _deserializerFunc.emplace(o_next, std::move(_deserializerFunc[other])); + }; }; } @@ -398,30 +416,34 @@ public: AdcDeviceAttribute(const AdcDeviceAttribute& other) { - _clearFunc(); + // _clearFunc(); - other._copyFunc(this); + if (&other != this) { + other._copyFunc(this); + } - _clearFunc = other._clearFunc; - _copyFunc = other._copyFunc; - _moveFunc = other._moveFunc; + // _clearFunc = other._clearFunc; + // _copyFunc = other._copyFunc; + // _moveFunc = other._moveFunc; } AdcDeviceAttribute(AdcDeviceAttribute&& other) { - _clearFunc(); + if (&other != this) { + other._moveFunc(this); + } - other._moveFunc(this); + // other._clearFunc(); - _clearFunc = std::move(other._clearFunc); - _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); } virtual ~AdcDeviceAttribute() { - _clearFunc(); + // _clearFunc(); } @@ -559,13 +581,13 @@ public: AdcDeviceAttribute& operator=(const AdcDeviceAttribute& other) { if (&other != this) { - _clearFunc(); + // _clearFunc(); other._copyFunc(this); - _clearFunc = other._clearFunc; - _copyFunc = other._copyFunc; - _moveFunc = other._moveFunc; + // _clearFunc = other._clearFunc; + // _copyFunc = other._copyFunc; + // _moveFunc = other._moveFunc; } return *this; @@ -575,13 +597,13 @@ public: AdcDeviceAttribute& operator=(AdcDeviceAttribute&& other) { if (&other != this) { - _clearFunc(); + // _clearFunc(); other._moveFunc(this); - _clearFunc = std::move(other._clearFunc); - _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); } return *this; diff --git a/tests/adc_asio_netserver_test.cpp b/tests/adc_asio_netserver_test.cpp index 1e5f213..e3761ea 100644 --- a/tests/adc_asio_netserver_test.cpp +++ b/tests/adc_asio_netserver_test.cpp @@ -20,11 +20,6 @@ public: template Device1(R&& id) : base_t(id) { - if constexpr (std::is_array_v>) { - this->_ident = std::forward(id); - } else { - std::ranges::copy(std::forward(id), std::back_inserter(this->_ident)); - } } }; @@ -38,11 +33,6 @@ public: template Device2(R&& id) : base_t(id) { - if constexpr (std::is_array_v>) { - this->_ident = std::forward(id); - } else { - std::ranges::copy(std::forward(id), std::back_inserter(this->_ident)); - } } }; @@ -68,17 +58,17 @@ int main(int argc, char* argv[]) Device2 dev2("DEVICE#2"); // 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::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({"DEV1::ATTR2", gt, st}); + dev1.addAttribute("DEV1::ATTR2", gt, st); // device#2 // read-only attr - dev2.addAttribute({0x1, [&dev1_val1]() { return dev1_val1; }}); + dev2.addAttribute(0x1ul, [&dev1_val1]() { return dev1_val1; }); // write-only - dev2.addAttribute({0xff, [&dev1_val1](const int& v) { dev1_val1 = v; }}); + dev2.addAttribute(0xfful, [&dev1_val1](const int& v) { dev1_val1 = v; }); /* COMMANDLINE OPTS */ @@ -92,7 +82,7 @@ int main(int argc, char* argv[]) "endpoints server will be listening for. For 'local' endpoint the '@' symbol at the beginning of the path " "means " "abstract namespace socket.", - cxxopts::value>()->default_value("local://stream/@ADC_ASIO_TEST_SERVER")); + cxxopts::value>()->default_value("local://stream/ADC_ASIO_TEST_SERVER")); options.positional_help("[endpoint0] [enpoint1] ... [endpointN]");