Module: check_mk
Branch: master
Commit: e3a7cf22249f22614ec60c4988496c977f83e069
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=e3a7cf22249f22…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Thu Apr 7 16:24:16 2016 +0200
Simplified and cleaned up Table interface.
Following item 28 from Scott Meyer's "Effective C++" ('Avoid returning
"handles" to object internals.'), Table::columns has been replaced the new
safe and more versatile Table::any_column member function. Luckily enough,
this opened up the possibility to nuke 2 more member functions and 2
typedefs.
Made Table::dynamicColumn private on the way, it's just used internally.
---
livestatus/src/Query.cc | 2 +-
livestatus/src/Table.cc | 18 ------------------
livestatus/src/Table.h | 29 +++++++++++++++++------------
livestatus/src/TableColumns.cc | 10 ++--------
4 files changed, 20 insertions(+), 39 deletions(-)
diff --git a/livestatus/src/Query.cc b/livestatus/src/Query.cc
index 4f49d33..abb0b15 100644
--- a/livestatus/src/Query.cc
+++ b/livestatus/src/Query.cc
@@ -171,7 +171,7 @@ Query::Query(const list<string> &lines, OutputBuffer
*output, Table *table)
}
if (_columns.empty() && !doStats()) {
- table->addAllColumnsToQuery(this);
+ table->any_column([this](Column *c) { return addColumn(c), false; });
// TODO(sp) We overwrite the value from a possible ColumnHeaders: line
// here, is that really what we want?
_show_column_headers = true;
diff --git a/livestatus/src/Table.cc b/livestatus/src/Table.cc
index 9dc84e8..dc415fe 100644
--- a/livestatus/src/Table.cc
+++ b/livestatus/src/Table.cc
@@ -24,10 +24,8 @@
#include "Table.h"
#include <string.h>
-#include <utility>
#include "Column.h"
#include "DynamicColumn.h"
-#include "Query.h"
using std::make_pair;
using std::string;
@@ -58,12 +56,6 @@ Table::~Table() {
}
}
-void Table::addAllColumnsToQuery(Query *q) {
- for (auto &column : _columns) {
- q->addColumn(column.second);
- }
-}
-
Column *Table::column(const char *colname) {
// We allow the name of the table to be
// prefixed to the column name. So if we
@@ -116,13 +108,3 @@ Column *Table::dynamicColumn(const char *colname_with_args) {
}
return nullptr;
}
-
-bool Table::hasColumn(Column *col) {
- // this is not very efficient but seldomly used
- for (auto &column : _columns) {
- if (col == column.second) {
- return true;
- }
- }
- return false;
-}
diff --git a/livestatus/src/Table.h b/livestatus/src/Table.h
index bd6717c..9b1da6d 100644
--- a/livestatus/src/Table.h
+++ b/livestatus/src/Table.h
@@ -28,6 +28,7 @@
#include "config.h" // IWYU pragma: keep
#include <map>
#include <string>
+#include <utility>
#include "nagios.h" // IWYU pragma: keep
class Column;
class DynamicColumn;
@@ -35,28 +36,32 @@ class Query;
class Table {
public:
- typedef std::map<std::string, Column *> _columns_t;
- typedef std::map<std::string, DynamicColumn *> _dynamic_columns_t;
-
-private:
- _columns_t _columns;
- _dynamic_columns_t _dynamic_columns;
-
-public:
Table() {}
virtual ~Table();
virtual Column *column(const char *colname);
- Column *dynamicColumn(const char *colname_with_args);
virtual void answerQuery(Query *) = 0;
virtual const char *name() = 0;
virtual const char *prefixname() { return name(); }
virtual bool isAuthorized(contact *, void *) { return true; }
virtual void *findObject(char *) { return nullptr; }
void addColumn(Column *);
- bool hasColumn(Column *);
void addDynamicColumn(DynamicColumn *);
- void addAllColumnsToQuery(Query *);
- _columns_t *columns() { return &_columns; }
+
+ template <typename Predicate>
+ bool any_column(Predicate pred) {
+ for (auto &c : _columns) {
+ if (pred(c.second)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+private:
+ Column *dynamicColumn(const char *colname_with_args);
+
+ std::map<std::string, Column *> _columns;
+ std::map<std::string, DynamicColumn *> _dynamic_columns;
};
#endif // Table_h
diff --git a/livestatus/src/TableColumns.cc b/livestatus/src/TableColumns.cc
index 8aef372..3e95a86 100644
--- a/livestatus/src/TableColumns.cc
+++ b/livestatus/src/TableColumns.cc
@@ -23,7 +23,6 @@
// Boston, MA 02110-1301 USA.
#include "TableColumns.h"
-#include <utility>
#include "Column.h"
#include "ColumnsColumn.h"
#include "Query.h"
@@ -44,12 +43,7 @@ void TableColumns::addTable(Table *table) { _tables.push_back(table);
}
void TableColumns::answerQuery(Query *query) {
for (auto table : _tables) {
- Table::_columns_t *columns = table->columns();
- for (auto &column : *columns) {
- if (!query->processDataset(column.second)) {
- break;
- }
- }
+ table->any_column([&](Column *c) { return !query->processDataset(c);
});
}
}
@@ -73,7 +67,7 @@ const char *TableColumns::getValue(Column *column, int colcol) {
const char *TableColumns::tableNameOf(Column *column) {
for (auto table : _tables) {
- if (table->hasColumn(column)) {
+ if (table->any_column([&](Column *c) { return c == column; })) {
return table->name();
}
}