2020-05-12 18:55:45 +00:00
|
|
|
#include "machinebase.h"
|
|
|
|
#include "devices/hwcomponent.h"
|
2020-03-13 21:49:58 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2020-03-23 03:15:12 +00:00
|
|
|
#include <thirdparty/loguru/loguru.hpp>
|
2020-03-13 21:49:58 +00:00
|
|
|
|
|
|
|
std::unique_ptr<MachineBase> gMachineObj = 0;
|
|
|
|
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
MachineBase::MachineBase(std::string name) {
|
2020-03-13 21:49:58 +00:00
|
|
|
this->name = name;
|
|
|
|
|
|
|
|
/* initialize internal maps */
|
|
|
|
this->comp_map.clear();
|
2020-03-14 14:39:34 +00:00
|
|
|
this->subdev_map.clear();
|
2020-03-13 21:49:58 +00:00
|
|
|
this->aliases.clear();
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
MachineBase::~MachineBase() {
|
|
|
|
for (auto it = this->comp_map.begin(); it != this->comp_map.end(); it++) {
|
2020-03-13 21:49:58 +00:00
|
|
|
delete it->second;
|
|
|
|
}
|
|
|
|
this->comp_map.clear();
|
2020-03-14 14:39:34 +00:00
|
|
|
this->aliases.clear();
|
|
|
|
this->subdev_map.clear();
|
2020-03-13 21:49:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
bool MachineBase::add_component(std::string name, HWComponent* dev_obj) {
|
2020-03-13 21:49:58 +00:00
|
|
|
if (this->comp_map.count(name)) {
|
|
|
|
LOG_F(ERROR, "Component %s already exists!", name.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->comp_map[name] = dev_obj;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
bool MachineBase::add_subdevice(std::string name, HWComponent* dev_obj) {
|
2020-03-14 14:39:34 +00:00
|
|
|
if (this->subdev_map.count(name)) {
|
|
|
|
LOG_F(ERROR, "Subdevice %s already exists!", name.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->subdev_map[name] = dev_obj;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
void MachineBase::add_alias(std::string name, std::string alias) {
|
2020-03-14 14:39:34 +00:00
|
|
|
this->aliases[alias] = name;
|
2020-03-13 21:49:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
HWComponent* MachineBase::get_comp_by_name(std::string name) {
|
2020-03-13 21:49:58 +00:00
|
|
|
if (this->aliases.count(name)) {
|
|
|
|
name = this->aliases[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->comp_map.count(name)) {
|
|
|
|
return this->comp_map[name];
|
2020-03-14 14:39:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this->subdev_map.count(name)) {
|
|
|
|
return this->subdev_map[name];
|
2020-03-13 21:49:58 +00:00
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
HWComponent* MachineBase::get_comp_by_type(HWCompType type) {
|
2020-03-13 21:49:58 +00:00
|
|
|
std::string comp_name;
|
|
|
|
bool found = false;
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
for (auto it = this->comp_map.begin(); it != this->comp_map.end(); it++) {
|
2020-03-14 13:23:46 +00:00
|
|
|
if (it->second->supports_type(type)) {
|
2020-03-13 21:49:58 +00:00
|
|
|
comp_name = it->first;
|
2020-05-12 18:55:45 +00:00
|
|
|
found = true;
|
2020-03-13 21:49:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 14:39:34 +00:00
|
|
|
if (found) {
|
|
|
|
return this->get_comp_by_name(comp_name);
|
|
|
|
}
|
2020-03-13 21:49:58 +00:00
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
for (auto it = this->subdev_map.begin(); it != this->subdev_map.end(); it++) {
|
2020-03-14 14:39:34 +00:00
|
|
|
if (it->second->supports_type(type)) {
|
|
|
|
comp_name = it->first;
|
2020-05-12 18:55:45 +00:00
|
|
|
found = true;
|
2020-03-14 14:39:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
return this->get_comp_by_name(comp_name);
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-03-13 21:49:58 +00:00
|
|
|
}
|