This commit is contained in:
2024-10-31 12:01:02 +03:00
parent ff42a30717
commit 222691d2e9
4 changed files with 51 additions and 29 deletions

View File

@@ -1,9 +1,9 @@
#pragma once
#include <algorithm>
#include <map>
#include <ranges>
#include <system_error>
#include <unordered_map>
#include "adc_device_concepts.h"
@@ -100,7 +100,7 @@ public:
auto it = _deviceCommands.find(cmd_ident);
if (it != _deviceCommands.end()) {
*it();
it->second();
} else {
throw std::system_error(AdcGenericDeviceErrorCode::ERROR_COMMAND_IDENT);
}
@@ -122,13 +122,20 @@ public:
AdcGenericDevice& addCommand(CommandT&& cmd)
{
auto id = cmd.ident();
_deviceCommands.emplace(id, std::move(cmd));
_deviceCommands.insert(std::move(cmd));
return *this;
}
template <typename... CtorArgTs>
AdcGenericDevice& addCommand(CtorArgTs&&... ctor_args)
{
_deviceCommands.emplace(std::forward<CtorArgTs>(ctor_args)...);
return *this;
}
AdcGenericDevice& delCommand(const cmd_ident_t& cmd_ident)
{
_deviceCommands.erase(cmd_ident);
@@ -139,8 +146,7 @@ public:
AdcGenericDevice& addAttribute(AttributeT&& attr)
{
auto id = attr.ident();
_deviceAttributes.emplace(id, std::move(attr));
_deviceAttributes.insert(std::move(attr));
return *this;
}
@@ -148,7 +154,9 @@ public:
template <typename... CtorArgTs>
AdcGenericDevice& addAttribute(CtorArgTs&&... ctor_args)
{
return addAttribute(AttributeT{std::forward<CtorArgTs>(ctor_args)...});
_deviceAttributes.emplace(std::forward<CtorArgTs>(ctor_args)...);
return *this;
}
@@ -179,8 +187,8 @@ public:
protected:
IdentT _ident;
std::map<attr_ident_t, AttributeT> _deviceAttributes;
std::map<cmd_ident_t, CommandT> _deviceCommands;
std::unordered_map<attr_ident_t, AttributeT> _deviceAttributes;
std::unordered_map<cmd_ident_t, CommandT> _deviceCommands;
};
} // namespace adc

View File

@@ -47,6 +47,13 @@ public:
}
AdcDeviceCommand(const AdcDeviceCommand&) = default;
AdcDeviceCommand(AdcDeviceCommand&&) = default;
AdcDeviceCommand& operator=(const AdcDeviceCommand&) = default;
AdcDeviceCommand& operator=(AdcDeviceCommand&&) = default;
virtual ~AdcDeviceCommand() = default;