Module: check_mk
Branch: master
Commit: cf41811bbb92099162ba1d9be1b540fbf3f72e16
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=cf41811bbb9209…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Fri Apr 15 15:04:30 2016 +0200
Use unique_ptr in DowntimesOrComments instead of old-skool pointers, vastly simplifying
things.
---
livestatus/src/DownCommColumn.cc | 9 +++----
livestatus/src/DownCommColumn.h | 2 +-
livestatus/src/DowntimesOrComments.cc | 42 ++++++++++-----------------------
livestatus/src/DowntimesOrComments.h | 9 +++----
livestatus/src/TableDownComm.cc | 6 +++--
5 files changed, 25 insertions(+), 43 deletions(-)
diff --git a/livestatus/src/DownCommColumn.cc b/livestatus/src/DownCommColumn.cc
index eb816cb..9b7b583 100644
--- a/livestatus/src/DownCommColumn.cc
+++ b/livestatus/src/DownCommColumn.cc
@@ -25,6 +25,7 @@
#include "DownCommColumn.h"
#include <stdint.h>
#include <stdlib.h>
+#include <memory>
#include <utility>
#include "DowntimeOrComment.h"
#include "DowntimesOrComments.h"
@@ -43,9 +44,9 @@ void DownCommColumn::output(void *data, Query *query) {
data = shiftPointer(data); // points to host or service
if (data != nullptr) {
bool first = true;
- for (auto entry : holder()) {
+ for (const auto &entry : holder()) {
unsigned long id = entry.first;
- DowntimeOrComment *dt = entry.second;
+ DowntimeOrComment *dt = entry.second.get();
if (match(dt, data)) {
if (first) {
first = false;
@@ -115,8 +116,8 @@ bool DownCommColumn::isEmpty(void *data) {
return true;
}
- for (auto entry : holder()) {
- DowntimeOrComment *dt = entry.second;
+ for (const auto &entry : holder()) {
+ DowntimeOrComment *dt = entry.second.get();
if (dt->_service == data ||
(dt->_service == nullptr && dt->_host == data)) {
return false;
diff --git a/livestatus/src/DownCommColumn.h b/livestatus/src/DownCommColumn.h
index 5c5ec3e..27b5f7f 100644
--- a/livestatus/src/DownCommColumn.h
+++ b/livestatus/src/DownCommColumn.h
@@ -29,7 +29,7 @@
#include <string>
#include "Column.h"
#include "ListColumn.h"
-class DowntimeOrComment;
+struct DowntimeOrComment;
class DowntimesOrComments;
class Query;
diff --git a/livestatus/src/DowntimesOrComments.cc
b/livestatus/src/DowntimesOrComments.cc
index 6810fcf..f842220 100644
--- a/livestatus/src/DowntimesOrComments.cc
+++ b/livestatus/src/DowntimesOrComments.cc
@@ -27,20 +27,19 @@
#include "DowntimeOrComment.h"
#include "logger.h"
-DowntimesOrComments::~DowntimesOrComments() {
- for (auto &entry : _entries) {
- delete entry.second;
- }
-}
+using std::make_unique;
void DowntimesOrComments::registerDowntime(nebstruct_downtime_data *data) {
+ unsigned long id = data->downtime_id;
switch (data->type) {
case NEBTYPE_DOWNTIME_ADD:
case NEBTYPE_DOWNTIME_LOAD:
- add(new Downtime(data));
+ _entries[id] = make_unique<Downtime>(data);
break;
case NEBTYPE_DOWNTIME_DELETE:
- remove(data->downtime_id);
+ if (_entries.erase(id) == 0) {
+ logger(LG_INFO, "Cannot delete non-existing downtime %lu",
id);
+ }
break;
default:
break;
@@ -48,40 +47,23 @@ void DowntimesOrComments::registerDowntime(nebstruct_downtime_data
*data) {
}
void DowntimesOrComments::registerComment(nebstruct_comment_data *data) {
+ unsigned long id = data->comment_id;
switch (data->type) {
case NEBTYPE_COMMENT_ADD:
case NEBTYPE_COMMENT_LOAD:
- add(new Comment(data));
+ _entries[id] = make_unique<Comment>(data);
break;
case NEBTYPE_COMMENT_DELETE:
- remove(data->comment_id);
+ if (_entries.erase(id) == 0) {
+ logger(LG_INFO, "Cannot delete non-existing comment %lu", id);
+ }
break;
default:
break;
}
}
-void DowntimesOrComments::add(DowntimeOrComment *data) {
- auto it = _entries.find(data->_id);
- if (it == _entries.end()) {
- _entries.emplace(data->_id, data);
- } else {
- delete it->second;
- it->second = data;
- }
-}
-
-void DowntimesOrComments::remove(unsigned long id) {
- auto it = _entries.find(id);
- if (it == _entries.end()) {
- logger(LG_INFO, "Cannot delete non-existing downtime/comment %lu",
id);
- } else {
- delete it->second;
- _entries.erase(it);
- }
-}
-
DowntimeOrComment *DowntimesOrComments::findEntry(unsigned long id) const {
auto it = _entries.find(id);
- return it == _entries.end() ? nullptr : it->second;
+ return it == _entries.end() ? nullptr : it->second.get();
}
diff --git a/livestatus/src/DowntimesOrComments.h b/livestatus/src/DowntimesOrComments.h
index e0d9092..00c7552 100644
--- a/livestatus/src/DowntimesOrComments.h
+++ b/livestatus/src/DowntimesOrComments.h
@@ -27,12 +27,12 @@
#include "config.h" // IWYU pragma: keep
#include <map>
+#include <memory>
+#include "DowntimeOrComment.h" // IWYU pragma: keep
#include "nagios.h"
-struct DowntimeOrComment;
class DowntimesOrComments {
public:
- ~DowntimesOrComments();
void registerDowntime(nebstruct_downtime_data *data);
void registerComment(nebstruct_comment_data *data);
DowntimeOrComment *findEntry(unsigned long id) const;
@@ -40,10 +40,7 @@ public:
auto end() const { return _entries.cend(); }
private:
- std::map<unsigned long, DowntimeOrComment *> _entries;
-
- void add(DowntimeOrComment *data);
- void remove(unsigned long id);
+ std::map<unsigned long, std::unique_ptr<DowntimeOrComment>> _entries;
};
#endif // DowntimesOrComments_h
diff --git a/livestatus/src/TableDownComm.cc b/livestatus/src/TableDownComm.cc
index 2a13e3d..ce163c9 100644
--- a/livestatus/src/TableDownComm.cc
+++ b/livestatus/src/TableDownComm.cc
@@ -23,6 +23,7 @@
// Boston, MA 02110-1301 USA.
#include "TableDownComm.h"
+#include <memory>
#include <utility>
#include "DowntimeOrComment.h"
#include "DowntimesOrComments.h" // IWYU pragma: keep
@@ -36,7 +37,8 @@
// TODO(sp): the dynamic data in this table must be locked with a mutex
-TableDownComm::TableDownComm(const DowntimesOrComments &holder, bool is_downtime)
+TableDownComm::TableDownComm(const DowntimesOrComments &holder,
+ bool is_downtime)
: _is_downtime(is_downtime), _holder(holder) {
DowntimeOrComment *ref = nullptr;
addColumn(new OffsetStringColumn(
@@ -131,7 +133,7 @@ TableDownComm::TableDownComm(const DowntimesOrComments &holder,
bool is_downtime
void TableDownComm::answerQuery(Query *query) {
for (const auto &entry : _holder) {
- if (!query->processDataset(entry.second)) {
+ if (!query->processDataset(entry.second.get())) {
break;
}
}