From 4da6a0a73261a34a71e8f95261769128c3c1df0b Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 25 Nov 2017 15:04:38 +0100 Subject: Replace bool * array by std::vector in MultiFilterInterface This replaces a dynamically allocated array of bool by std::vector. 1) This makes the code shorter and less error prone, because memory management has not to be done by hand. 2) It fixes a bug in the old code: memset(checkState, false, list.count()) is wrong, because bool is not guaranteed to be the same size as char! Two notes: 1) QMap<>, QVector<>, etc. are used numerous times in the code, so this doesn't introduce a new C++ concept. Here, the std:: version is used, because there is no need for reference counting, COW semantics, etc. 2) std::vector is used instead of std::vector, because the latter does a pessimization where a bitfield is used! Signed-off-by: Berthold Stoeger --- qt-models/filtermodels.cpp | 28 ++++++++++------------------ qt-models/filtermodels.h | 5 +++-- 2 files changed, 13 insertions(+), 20 deletions(-) (limited to 'qt-models') diff --git a/qt-models/filtermodels.cpp b/qt-models/filtermodels.cpp index f6fa45617..684999fbd 100644 --- a/qt-models/filtermodels.cpp +++ b/qt-models/filtermodels.cpp @@ -10,6 +10,7 @@ #endif #include +#include #define CREATE_INSTANCE_METHOD( CLASS ) \ CLASS *CLASS::instance() \ @@ -39,8 +40,7 @@ bool CLASS::setData(const QModelIndex &index, const QVariant &value, int role) \ #define CREATE_CLEAR_FILTER_METHOD( CLASS ) \ void CLASS::clearFilter() \ { \ - memset(checkState, false, rowCount()); \ - checkState[rowCount() - 1] = false; \ + std::fill(checkState.begin(), checkState.end(), false); \ anyChecked = false; \ emit dataChanged(createIndex(0,0), createIndex(rowCount()-1, 0)); \ } @@ -128,10 +128,8 @@ void SuitsFilterModel::repopulate() qSort(list); list << tr("No suit set"); setStringList(list); - delete[] checkState; - checkState = new bool[list.count()]; - memset(checkState, false, list.count()); - checkState[list.count() - 1] = false; + checkState.resize(list.count()); + std::fill(checkState.begin(), checkState.end(), false); anyChecked = false; } @@ -153,10 +151,8 @@ void TagFilterModel::repopulate() qSort(list); list << tr("Empty tags"); setStringList(list); - delete[] checkState; - checkState = new bool[list.count()]; - memset(checkState, false, list.count()); - checkState[list.count() - 1] = false; + checkState.resize(list.count()); + std::fill(checkState.begin(), checkState.end(), false); anyChecked = false; } @@ -248,10 +244,8 @@ void BuddyFilterModel::repopulate() qSort(list); list << tr("No buddies"); setStringList(list); - delete[] checkState; - checkState = new bool[list.count()]; - memset(checkState, false, list.count()); - checkState[list.count() - 1] = false; + checkState.resize(list.count()); + std::fill(checkState.begin(), checkState.end(), false); anyChecked = false; } @@ -301,10 +295,8 @@ void LocationFilterModel::repopulate() qSort(list); list << tr("No location set"); setStringList(list); - delete[] checkState; - checkState = new bool[list.count()]; - memset(checkState, false, list.count()); - checkState[list.count() - 1] = false; + checkState.resize(list.count()); + std::fill(checkState.begin(), checkState.end(), false); anyChecked = false; } diff --git a/qt-models/filtermodels.h b/qt-models/filtermodels.h index 992582776..cda6e2369 100644 --- a/qt-models/filtermodels.h +++ b/qt-models/filtermodels.h @@ -5,13 +5,14 @@ #include #include #include +#include class MultiFilterInterface { public: - MultiFilterInterface() : checkState(NULL), anyChecked(false) {} + MultiFilterInterface() : anyChecked(false) {} virtual bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const = 0; virtual void clearFilter() = 0; - bool *checkState; + std::vector checkState; bool anyChecked; }; -- cgit v1.2.3-70-g09d2