Commit 11971f93 authored by Micha Mueller's avatar Micha Mueller
Browse files

Make _cache and _readingQueue unique_ptr

parent 632f1dc0
......@@ -8,6 +8,7 @@
#ifndef SRC_SENSORBASE_H_
#define SRC_SENSORBASE_H_
#include <memory>
#include <string>
#include <boost/lockfree/spsc_queue.hpp>
......@@ -20,27 +21,37 @@ class SensorBase {
public:
SensorBase(const std::string& name) :
_name(name),
_mqtt("") {
_mqtt(""),
_cache(nullptr),
_readingQueue(nullptr) {
_latestValue.timestamp = 0;
_latestValue.value = 0;
_cache = NULL;
_readingQueue = NULL;
}
virtual ~SensorBase() {
if (_cache) {
delete[] _cache;
}
if (_readingQueue) {
delete _readingQueue;
}
SensorBase(const SensorBase& other) :
_name(other._name),
_mqtt(other._mqtt),
_cache(nullptr),
_latestValue(other._latestValue),
_readingQueue(nullptr) {}
virtual ~SensorBase() {}
SensorBase& operator=(const SensorBase& other) {
_name = other._name;
_mqtt = other._mqtt;
_cache.reset(nullptr);
_latestValue.timestamp = other._latestValue.timestamp;
_latestValue.value = other._latestValue.value;
_readingQueue.reset(nullptr);
return *this;
}
const std::string& getName() const { return _name; }
const std::string& getMqtt() const { return _mqtt; }
const reading_t * const getCache() const { return _cache; }
const reading_t * const getCache() const { return _cache.get(); }
const reading_t getLatestValue() const { return _latestValue; }
void setName(const std::string& name) { _name = name; }
......@@ -52,13 +63,13 @@ public:
void initSensor(unsigned cacheSize) {
if(!_cache) {
_cache = new reading_t[cacheSize];
_cache.reset(new reading_t[cacheSize]);
for(unsigned i = 0; i < cacheSize; i++) {
_cache[i] = _latestValue; //_latestValue should equal (0,0) at this point
}
}
if(!_readingQueue) {
_readingQueue = new boost::lockfree::spsc_queue<reading_t>(1024);
_readingQueue.reset(new boost::lockfree::spsc_queue<reading_t>(1024));
}
}
......@@ -73,9 +84,9 @@ protected:
std::string _name;
std::string _mqtt;
reading_t * _cache;
std::unique_ptr<reading_t[]> _cache;
reading_t _latestValue;
boost::lockfree::spsc_queue<reading_t>* _readingQueue;
std::unique_ptr<boost::lockfree::spsc_queue<reading_t>> _readingQueue;
};
#endif /* SRC_SENSORBASE_H_ */
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