Commit 038f1ab7 authored by Micha Mueller's avatar Micha Mueller
Browse files

Make template maps store only pointers for clear memory management

parent d7e398c1
......@@ -55,26 +55,36 @@ class ConfiguratorTemplate : public ConfiguratorInterface {
static_assert(std::is_base_of<SensorGroupTemplate, SGroup>::value, "SGroup must derive from SensorGroupTemplate!");
protected:
typedef std::map<std::string, SGroup> sGroupMap_t;
typedef std::map<std::string, SEntity> sEntityMap_t;
typedef std::map<std::string, SGroup*> sGroupMap_t;
typedef std::map<std::string, SEntity*> sEntityMap_t;
public:
ConfiguratorTemplate() :
_entityName("entity"),
_groupName("group"),
_sensorName("sensor"),
_baseName("sensor"),
_cfgPath(""),
_mqttPrefix(""),
_cacheInterval(900000) {}
ConfiguratorTemplate(const ConfiguratorTemplate&) = delete;
virtual ~ConfiguratorTemplate() {
for (auto g : _sensorGroups) {
delete g;
}
for (auto tg : _templateSensorGroups) {
delete tg;
}
for (auto te : _templateSensorEntitys) {
delete te;
}
_templateSensorGroups.clear();
_templateSensorEntitys.clear();
}
ConfiguratorTemplate& operator=(const ConfiguratorTemplate&) = delete;
/**
* Read in the given configuration
*
......@@ -98,23 +108,16 @@ public:
if (boost::iequals(tempVal.first, _entityName)) {
//TODO
} else if (boost::iequals(tempVal.first, _groupName)) {
LOG(debug) << "Template Group \"" << tempVal.second.data() << "\"";
LOG(debug) << "Template " << _groupName << " \"" << tempVal.second.data() << "\"";
if (!tempVal.second.empty()) {
SGroup group(tempVal.second.data());
if (readSensorGroup(group, tempVal.second)) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &sBaseVal, tempVal.second) {
if (boost::iequals(sBaseVal.first, _baseName)) {
SBase* sensor = new SBase(sBaseVal.second.data());
if (readSensorBase(*sensor, sBaseVal.second)) {
group.pushBackSensor(sensor);
} else {
LOG(warning) << "Template sensor \"" << group.getName() << "::" << sBaseVal.second.data() << "\" has bad values! Ignoring...";
}
}
SGroup* group = new SGroup(tempVal.second.data());
if (readSensorGroup(*group, tempVal.second)) {
auto ret = _templateSensorGroups.insert(std::pair<std::string, SGroup*>(group->getName(), group));
if(!ret.second) {
LOG(warning) << "Template " << _groupName << " " << group->getName() << " already exists! Omitting...";
}
_templateSensorGroups.insert(std::pair<std::string, SGroup>(group.getName(), group));
} else {
LOG(warning) << "Template group \"" << tempVal.second.data() << "\" has bad values! Ignoring...";
LOG(warning) << "Template " << _groupName << " \"" << tempVal.second.data() << "\" has bad values! Ignoring...";
}
}
} //else if (boost::iequals(tempVal.first, _baseName)) {//TODO allow for template sensors? even useful?}
......@@ -123,24 +126,24 @@ public:
//TODO read actual entitys/groups/bases. How to handle defaults?
//read template groups
boost::optional<boost::property_tree::iptree&> tempSens = cfg.get_child_optional("groupTemplates");
if (tempSens) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &groupVal, cfg.get_child("groupTemplates")) {
if (STRCMP(groupVal, "group")) {
LOG(debug) << "Template Group \"" << groupVal.second.data() << "\"";
if (!groupVal.second.empty()) {
//TODO
SGroup group(groupVal.second.data());
if(readSensorGroup(group, groupVal.second)) {
_templateSensorGroups.insert(std::pair<std::string, SGroup>(group.getName(), group));
} else {
LOG(warning) << "Template group \"" << groupVal.second.data() << "\" has bad values! Ignoring...";
}
}
}
}
}
// //read template groups
// boost::optional<boost::property_tree::iptree&> tempSens = cfg.get_child_optional("groupTemplates");
// if (tempSens) {
// BOOST_FOREACH(boost::property_tree::iptree::value_type &groupVal, cfg.get_child("groupTemplates")) {
// if (STRCMP(groupVal, "group")) {
// LOG(debug) << "Template " << _groupName << " \"" << groupVal.second.data() << "\"";
// if (!groupVal.second.empty()) {
// //TODO
// SGroup group(groupVal.second.data());
// if(readSensorGroup(group, groupVal.second)) {
// _templateSensorGroups.insert(std::pair<std::string, SGroup>(group.getName(), group));
// } else {
// LOG(warning) << "Template " << _groupName << " \"" << groupVal.second.data() << "\" has bad values! Ignoring...";
// }
// }
// }
// }
// }
//read in plugin specific stuff (including actual sensors)
return derivedReadConfig(cfg);
......@@ -165,13 +168,19 @@ public:
//clean up plugin specific stuff
derivedReReadConfig();
//clean up sensors and groups
//clean up sensors/groups and templates
for(auto g : _sensorGroups) {
delete g;
}
for (auto tg : _templateSensorGroups) {
delete tg;
}
for (auto te : _templateSensorEntitys) {
delete te;
}
_sensorGroups.clear();
_templateSensorGroups.clear();
_templateSensorEntitys.clear();
//back to the very beginning
return readConfig(_cfgPath);
......@@ -239,9 +248,12 @@ protected:
} else if (STRCMP(val, "sensor")) {
SBase* sensor = new SBase(val.second.data());
if (readSensorBase(*sensor, val.second)) {
sGroup.pushBackSensor(sensor);
auto ret = sGroup.pushBackSensor(sensor);
if(!ret.second) {
LOG(warning) << _baseName << " " << sensor->getName() << " already exists! Omitting...";
}
} else {
LOG(warning) << "Sensor " << sGroup.getGroupName() << "::" << sensor->getName() << " could not be read! Omitting";
LOG(warning) << _baseName << " " << sGroup.getGroupName() << "::" << sensor->getName() << " could not be read! Omitting";
}
}
}
......
......@@ -21,18 +21,44 @@ public:
SensorGroupTemplate(const std::string groupName) :
SensorGroupInterface(groupName) {}
SensorGroupTemplate(const SensorGroupTemplate& other) :
SensorGroupInterface(other) {
for(auto s : other._sensors) {
S* sensor = new S(s);
_sensors.push_back(sensor);
_baseSensors.push_back(sensor);
}
}
virtual ~SensorGroupTemplate() {
for(auto s : _sensors) {
delete s;
}
}
SensorGroupTemplate& operator=(const SensorGroupTemplate& other) {
SensorGroupInterface::operator=(other);
for(auto s : _sensors) {
delete s;
}
_sensors.clear();
_baseSensors.clear();
for(auto s : other._sensors) {
S* sensor = new S(s);
_sensors.push_back(sensor);
_baseSensors.push_back(sensor);
}
return *this;
}
virtual void pushBackSensor(SensorBase* s) override {
if (S* dSensor = dynamic_cast<S*>(s)) {
_sensors.push_back(dSensor);
_baseSensors.push_back(s);
} else {
LOG(warning) << "Sensorgroup " << _groupName << ": Type mismatch when storing sensor! Sensor omitted";
LOG(warning) << "Group " << _groupName << ": Type mismatch when storing sensor! Sensor omitted";
}
}
......
......@@ -22,7 +22,7 @@ public:
SysfsConfigurator() {
_entityName = "host";
_groupName = "group";
_sensorName = "sensor";
_baseName = "sensor";
}
virtual ~SysfsConfigurator() {}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment