This commit is contained in:
2024-11-01 17:26:52 +03:00
parent 3b1a318ee7
commit 8e13ad0c3c
5 changed files with 128 additions and 154 deletions

View File

@@ -1,6 +1,7 @@
#pragma once
#include <algorithm>
#include <memory>
#include <ranges>
#include <system_error>
#include <unordered_map>
@@ -100,7 +101,8 @@ public:
auto it = _deviceCommands.find(cmd_ident);
if (it != _deviceCommands.end()) {
it->second();
// it->second();
it->second->operator()();
} else {
throw std::system_error(AdcGenericDeviceErrorCode::ERROR_COMMAND_IDENT);
}
@@ -113,7 +115,8 @@ public:
{
auto it = _deviceAttributes.find(attr_ident);
if (it != _deviceAttributes.end()) {
return it->second;
// return it->second;
return *(it->second);
}
throw std::system_error(AdcGenericDeviceErrorCode::ERROR_ATTR_IDENT);
@@ -122,7 +125,8 @@ public:
AdcGenericDevice& addCommand(CommandT&& cmd)
{
_deviceCommands.insert({cmd.ident(), std::move(cmd)});
// _deviceCommands.insert({cmd.ident(), std::move(cmd)});
_deviceCommands.insert({cmd.ident(), std::make_unique<CommandT>(std::move(cmd))});
return *this;
}
@@ -131,10 +135,7 @@ public:
template <typename... CtorArgTs>
AdcGenericDevice& addCommand(CtorArgTs&&... ctor_args)
{
// _deviceCommands.emplace(std::forward<CtorArgTs>(ctor_args)...);
return addCommand({std::forward<CtorArgTs>(ctor_args)...});
// return *this;
}
AdcGenericDevice& delCommand(const cmd_ident_t& cmd_ident)
@@ -147,7 +148,8 @@ public:
AdcGenericDevice& addAttribute(AttributeT&& attr)
{
_deviceAttributes.insert({attr.ident(), std::move(attr)});
// _deviceAttributes.insert({attr.ident(), std::move(attr)});
_deviceAttributes.insert({attr.ident(), std::make_unique<AttributeT>(std::move(attr))});
return *this;
}
@@ -155,11 +157,7 @@ public:
template <typename... CtorArgTs>
AdcGenericDevice& addAttribute(CtorArgTs&&... ctor_args)
{
// _deviceAttributes.emplace(std::forward<CtorArgTs>(ctor_args)...);
return addAttribute({std::forward<CtorArgTs>(ctor_args)...});
// return *this;
}
@@ -190,8 +188,10 @@ public:
protected:
IdentT _ident;
std::unordered_map<attr_ident_t, AttributeT> _deviceAttributes;
std::unordered_map<cmd_ident_t, CommandT> _deviceCommands;
// std::unordered_map<attr_ident_t, AttributeT> _deviceAttributes;
// std::unordered_map<cmd_ident_t, CommandT> _deviceCommands;
std::unordered_map<attr_ident_t, std::unique_ptr<AttributeT>> _deviceAttributes;
std::unordered_map<cmd_ident_t, std::unique_ptr<CommandT>> _deviceCommands;
};
} // namespace adc