Commit 8aab4e4e authored by Micha Mueller's avatar Micha Mueller
Browse files

Various fixes to compile with std::shared_ptr

parent 07149b15
...@@ -126,7 +126,7 @@ void MQTTPusher::sendReadings(SensorBase& s, reading_t* reads, std::size_t& tota ...@@ -126,7 +126,7 @@ void MQTTPusher::sendReadings(SensorBase& s, reading_t* reads, std::size_t& tota
//totalCount+= count; //totalCount+= count;
totalCount+= 1; totalCount+= 1;
#ifdef DEBUG #ifdef DEBUG
LOGM(debug) << "Sending " << count << " values from " << s->getName(); LOGM(debug) << "Sending " << count << " values from " << s.getName();
#endif #endif
#if DEBUG #if DEBUG
......
...@@ -207,13 +207,18 @@ public: ...@@ -207,13 +207,18 @@ public:
SB_Ptr sensor; SB_Ptr sensor;
//perhaps one sensor is already present because it was copied from the template group //perhaps one sensor is already present because it was copied from the template group
if (group->getSensors().size() != 0) { if (group->getSensors().size() != 0) {
sensor = static_pointer_cast<SB_Ptr::element_type>(group->getSensors()[0]); sensor = std::dynamic_pointer_cast<SBase>(group->getSensors()[0]);
//check if cast was successful (sensor != nullptr)
if (sensor) {
sensor->setName(val.second.data()); sensor->setName(val.second.data());
if (readSensorBase(*sensor, val.second)) { if (readSensorBase(*sensor, val.second)) {
storeSensorGroup(group); storeSensorGroup(group);
} else { } else {
LOG(warning) << "Single " << _baseName << " " << val.second.data() << " could not be read! Omitting"; LOG(warning) << "Single " << _baseName << " " << val.second.data() << " could not be read! Omitting";
} }
} else {
LOG(warning) << "Single " << _baseName << " " << val.second.data() << " had a type mismatch when casting! Omitting";
}
} else { } else {
sensor = std::make_shared<SBase>(val.second.data()); sensor = std::make_shared<SBase>(val.second.data());
if (readSensorBase(*sensor, val.second)) { if (readSensorBase(*sensor, val.second)) {
...@@ -230,8 +235,8 @@ public: ...@@ -230,8 +235,8 @@ public:
} }
} }
//read of config finished. Now we build the mqtt-topic for every sensor //read of config finished. Now we build the mqtt-topic for every sensor
for(auto g : _sensorGroups) { for(const auto& g : _sensorGroups) {
for(auto s : g->getSensors()) { for(const auto& s : g->getSensors()) {
s->setMqtt(_mqttPrefix + g->getMqttPart() + s->getMqtt()); s->setMqtt(_mqttPrefix + g->getMqttPart() + s->getMqtt());
LOG(debug) << g->getGroupName() << "::" << s->getName() << " using MQTT-topic \"" << s->getMqtt() << "\""; LOG(debug) << g->getGroupName() << "::" << s->getName() << " using MQTT-topic \"" << s->getMqtt() << "\"";
} }
...@@ -497,13 +502,17 @@ protected: ...@@ -497,13 +502,17 @@ protected:
SB_Ptr sensor; SB_Ptr sensor;
//perhaps one sensor is already present because it was copied from the template group //perhaps one sensor is already present because it was copied from the template group
if (group->getSensors().size() != 0) { if (group->getSensors().size() != 0) {
sensor = static_pointer_cast<SBase>(group->getSensors()[0]); sensor = std::dynamic_pointer_cast<SBase>(group->getSensors()[0]);
//check if cast was successful (sensor != nullptr)
if (sensor) {
sensor->setName(val.second.data()); sensor->setName(val.second.data());
if (readSensorBase(*sensor, val.second)) { if (readSensorBase(*sensor, val.second)) {
storeSensorGroup(group); storeSensorGroup(group);
} else { } else {
LOG(warning) << "Single " << _baseName << " " << val.second.data() << " could not be read! Omitting"; LOG(warning) << "Single " << _baseName << " " << val.second.data() << " could not be read! Omitting";
delete group; }
} else {
LOG(warning) << "Single " << _baseName << " " << val.second.data() << " had a type mismatch when casting! Omitting";
} }
} else { } else {
sensor = std::make_shared<SBase>(val.second.data()); sensor = std::make_shared<SBase>(val.second.data());
...@@ -512,12 +521,10 @@ protected: ...@@ -512,12 +521,10 @@ protected:
storeSensorGroup(group); storeSensorGroup(group);
} else { } else {
LOG(warning) << "Single " << _baseName << " " << val.second.data() << " could not be read! Omitting"; LOG(warning) << "Single " << _baseName << " " << val.second.data() << " could not be read! Omitting";
delete group;
} }
} }
} else { } else {
LOG(warning) << "Single " << _baseName << " \"" << val.second.data() << "\" has bad values! Ignoring..."; LOG(warning) << "Single " << _baseName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
delete group;
} }
} }
} }
...@@ -525,7 +532,7 @@ protected: ...@@ -525,7 +532,7 @@ protected:
} }
if(!isTemplate) { if(!isTemplate) {
for(auto g : _sensorGroups) { for(const auto& g : _sensorGroups) {
if(isEntityOfGroup(sEntity, *g)) { if(isEntityOfGroup(sEntity, *g)) {
finalizeGroup(*g); finalizeGroup(*g);
} }
......
...@@ -56,9 +56,8 @@ public: ...@@ -56,9 +56,8 @@ public:
} }
virtual void pushBackSensor(SBasePtr s) override { virtual void pushBackSensor(SBasePtr s) override {
//Undefined behavior will arise if a ptr type != S_Ptr is given to static_pointer_cast //check if dynamic cast returns nullptr
//Use dynamic_pointer_cast instead if this is a problem if (S_Ptr dSensor = std::dynamic_pointer_cast<S>(s)) {
if (S_Ptr dSensor = static_pointer_cast<S_Ptr::element_type>(s)) {
_sensors.push_back(dSensor); _sensors.push_back(dSensor);
_baseSensors.push_back(s); _baseSensors.push_back(s);
} else { } else {
......
...@@ -164,7 +164,7 @@ void ProcfsConfigurator::sensorGroup(ProcfsSensorGroup& sGroup, CFG_VAL config) ...@@ -164,7 +164,7 @@ void ProcfsConfigurator::sensorGroup(ProcfsSensorGroup& sGroup, CFG_VAL config)
// Adding the sensors corresponding to availableMetrics // Adding the sensors corresponding to availableMetrics
for(int i=0;i < numMetrics; i++) { for(int i=0;i < numMetrics; i++) {
ProcfsSBPtr sensor = static_pointer_cast<ProcfsSBPtr>(sGroup.getSensors().at(i)); ProcfsSBPtr sensor = std::static_pointer_cast<ProcfsSBPtr::element_type>(sGroup.getSensors().at(i));
if ( autoMQTT ) sensor->setMqtt(increaseMqtt(mqttStart, metricsCounter->at(sensor->getCPUId() + 1)++ )); if ( autoMQTT ) sensor->setMqtt(increaseMqtt(mqttStart, metricsCounter->at(sensor->getCPUId() + 1)++ ));
// If the metric does not refer to a specific CPU core, the topic is prefix + default mqttPart + suffix // If the metric does not refer to a specific CPU core, the topic is prefix + default mqttPart + suffix
// The suffix is increased automatically through the metricsCounter vector // The suffix is increased automatically through the metricsCounter vector
......
Supports Markdown
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