From 31c86da50b7b8fb4732090326e7fedf9e5afe300 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 13 Jan 2016 01:05:01 +0100 Subject: [PATCH] refs #568, only one IDatastoreObjectList by making the key type a template parameter --- src/blackmisc/aviation/aircrafticaocodelist.h | 2 +- src/blackmisc/aviation/airlineicaocodelist.h | 2 +- src/blackmisc/aviation/liverylist.h | 2 +- src/blackmisc/countrylist.cpp | 2 +- src/blackmisc/countrylist.h | 2 +- src/blackmisc/datastoreobjectlist.cpp | 116 +++++------------- src/blackmisc/datastoreobjectlist.h | 55 +++------ src/blackmisc/simulation/aircraftmodellist.h | 2 +- src/blackmisc/simulation/distributorlist.h | 2 +- 9 files changed, 56 insertions(+), 129 deletions(-) diff --git a/src/blackmisc/aviation/aircrafticaocodelist.h b/src/blackmisc/aviation/aircrafticaocodelist.h index 818000c4e..a18b096e3 100644 --- a/src/blackmisc/aviation/aircrafticaocodelist.h +++ b/src/blackmisc/aviation/aircrafticaocodelist.h @@ -26,7 +26,7 @@ namespace BlackMisc //! Value object encapsulating a list of ICAO codes. class BLACKMISC_EXPORT CAircraftIcaoCodeList : public CSequence, - public BlackMisc::IDatastoreObjectListWithIntegerKey, + public BlackMisc::IDatastoreObjectList, public BlackMisc::Mixin::MetaType { public: diff --git a/src/blackmisc/aviation/airlineicaocodelist.h b/src/blackmisc/aviation/airlineicaocodelist.h index e0b5aa8a8..6ced35e8d 100644 --- a/src/blackmisc/aviation/airlineicaocodelist.h +++ b/src/blackmisc/aviation/airlineicaocodelist.h @@ -26,7 +26,7 @@ namespace BlackMisc //! Value object encapsulating a list of ICAO codes. class BLACKMISC_EXPORT CAirlineIcaoCodeList : public CSequence, - public BlackMisc::IDatastoreObjectListWithIntegerKey, + public BlackMisc::IDatastoreObjectList, public BlackMisc::Mixin::MetaType { diff --git a/src/blackmisc/aviation/liverylist.h b/src/blackmisc/aviation/liverylist.h index 68b19ed60..48466219b 100644 --- a/src/blackmisc/aviation/liverylist.h +++ b/src/blackmisc/aviation/liverylist.h @@ -24,7 +24,7 @@ namespace BlackMisc //! Value object for a list of airports. class BLACKMISC_EXPORT CLiveryList : public CSequence, - public BlackMisc::IDatastoreObjectListWithIntegerKey, + public BlackMisc::IDatastoreObjectList, public BlackMisc::Mixin::MetaType { public: diff --git a/src/blackmisc/countrylist.cpp b/src/blackmisc/countrylist.cpp index b387ca19e..475775480 100644 --- a/src/blackmisc/countrylist.cpp +++ b/src/blackmisc/countrylist.cpp @@ -19,7 +19,7 @@ namespace BlackMisc { QString iso(isoCode.trimmed().toUpper()); if (!CCountry::isValidIsoCode(iso)) { return CCountry(); } - return IDatastoreObjectListWithStringKey::findByKey(isoCode); + return IDatastoreObjectList::findByKey(isoCode); } CCountry CCountryList::findBestMatchByCountryName(const QString &countryName) const diff --git a/src/blackmisc/countrylist.h b/src/blackmisc/countrylist.h index 089a1700a..acea89068 100644 --- a/src/blackmisc/countrylist.h +++ b/src/blackmisc/countrylist.h @@ -25,7 +25,7 @@ namespace BlackMisc //! \remark: I do not use CCollection as I want to sort per column class BLACKMISC_EXPORT CCountryList : public CSequence, - public BlackMisc::IDatastoreObjectListWithStringKey, + public BlackMisc::IDatastoreObjectList, public BlackMisc::Mixin::MetaType { public: diff --git a/src/blackmisc/datastoreobjectlist.cpp b/src/blackmisc/datastoreobjectlist.cpp index e7c105539..82e2d951e 100644 --- a/src/blackmisc/datastoreobjectlist.cpp +++ b/src/blackmisc/datastoreobjectlist.cpp @@ -20,36 +20,26 @@ namespace BlackMisc { - template - IDatastoreObjectListWithIntegerKey::IDatastoreObjectListWithIntegerKey() + template + IDatastoreObjectList::IDatastoreObjectList() { } - template - IDatastoreObjectListWithStringKey::IDatastoreObjectListWithStringKey() - { } - - template - OBJ IDatastoreObjectListWithIntegerKey::findByKey(int key, const OBJ ¬Found) const + template + OBJ IDatastoreObjectList::findByKey(KEYTYPE key, const OBJ ¬Found) const { return this->container().findFirstByOrDefault(&OBJ::getDbKey, key, notFound); } - template - OBJ IDatastoreObjectListWithStringKey::findByKey(const QString &key, const OBJ ¬Found) const - { - return this->container().findFirstByOrDefault(&OBJ::getDbKey, key, notFound); - } - - template - void IDatastoreObjectListWithIntegerKey::sortByKey() + template + void IDatastoreObjectList::sortByKey() { this->container().sort(BlackMisc::Predicates::MemberLess(&OBJ::getDbKey)); } - template - QList IDatastoreObjectListWithIntegerKey::toDbKeyList() const + template + QList IDatastoreObjectList::toDbKeyList() const { - QList keys; + QList keys; for (const OBJ &obj : ITimestampObjectList::container()) { if (!obj.hasValidDbKey()) { continue; } @@ -58,8 +48,8 @@ namespace BlackMisc return keys; } - template - int IDatastoreObjectListWithIntegerKey::removeObjectsWithKeys(const QList &keys) + template + int IDatastoreObjectList::removeObjectsWithKeys(const QList &keys) { if (keys.isEmpty()) { return 0; } if (this->container().isEmpty()) { return 0; } @@ -70,46 +60,30 @@ namespace BlackMisc newValues.push_back(obj); } int delta = this->container().size() - newValues.size(); - *this = newValues; + this->container() = newValues; return delta; } - template - int IDatastoreObjectListWithStringKey::removeObjectsWithKeys(const QStringList &keys) + template + int IDatastoreObjectList::replaceOrAddObjectsByKey(const CONTAINER &container) { - if (keys.isEmpty()) { return 0; } - if (this->container().isEmpty()) { return 0; } - CONTAINER newValues; - for (const OBJ &obj : ITimestampObjectList::container()) + if (container.isEmpty()) { return 0; } + if (this->container().isEmpty()) { - if (keys.contains(obj.getDbKey(), Qt::CaseInsensitive)) { continue; } - newValues.push_back(obj); + this->container() = container; + return this->container().size(); } - int delta = this->container().size() - newValues.size(); - *this = newValues; + CONTAINER newValues(this->container()); + const QList keys(container.toDbKeyList()); + newValues.removeObjectsWithKeys(keys); + newValues.push_back(container); + int delta = newValues.size() - this->container().size(); + this->container() = newValues; return delta; } - template - void IDatastoreObjectListWithStringKey::sortByKey() - { - this->container().sort(BlackMisc::Predicates::MemberLess(&OBJ::getDbKey)); - } - - template - QStringList IDatastoreObjectListWithStringKey::toDbKeyList() const - { - QStringList keys; - for (const OBJ &obj : ITimestampObjectList::container()) - { - if (!obj.hasValidDbKey()) { continue; } - keys.append(obj.getDbKey()); - } - return keys; - } - - template - CONTAINER IDatastoreObjectListWithIntegerKey::fromDatabaseJson(const QJsonArray &array) + template + CONTAINER IDatastoreObjectList::fromDatabaseJson(const QJsonArray &array) { CONTAINER container; for (const QJsonValue &value : array) @@ -119,30 +93,8 @@ namespace BlackMisc return container; } - template - CONTAINER IDatastoreObjectListWithIntegerKey::fromDatabaseJson(const QJsonArray &array, const QString &prefix) - { - CONTAINER container; - for (const QJsonValue &value : array) - { - container.push_back(OBJ::fromDatabaseJson(value.toObject(), prefix)); - } - return container; - } - - template - CONTAINER IDatastoreObjectListWithStringKey::fromDatabaseJson(const QJsonArray &array) - { - CONTAINER container; - for (const QJsonValue &value : array) - { - container.push_back(OBJ::fromDatabaseJson(value.toObject())); - } - return container; - } - - template - CONTAINER IDatastoreObjectListWithStringKey::fromDatabaseJson(const QJsonArray &array, const QString &prefix) + template + CONTAINER IDatastoreObjectList::fromDatabaseJson(const QJsonArray &array, const QString &prefix) { CONTAINER container; for (const QJsonValue &value : array) @@ -154,11 +106,11 @@ namespace BlackMisc // see here for the reason of thess forward instantiations // http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html - template class IDatastoreObjectListWithIntegerKey; - template class IDatastoreObjectListWithIntegerKey; - template class IDatastoreObjectListWithIntegerKey; - template class IDatastoreObjectListWithIntegerKey; - template class IDatastoreObjectListWithStringKey; - template class IDatastoreObjectListWithStringKey; + template class IDatastoreObjectList; + template class IDatastoreObjectList; + template class IDatastoreObjectList; + template class IDatastoreObjectList; + template class IDatastoreObjectList; + template class IDatastoreObjectList; } // namespace diff --git a/src/blackmisc/datastoreobjectlist.h b/src/blackmisc/datastoreobjectlist.h index b85eb5643..fcdcb0938 100644 --- a/src/blackmisc/datastoreobjectlist.h +++ b/src/blackmisc/datastoreobjectlist.h @@ -21,21 +21,24 @@ namespace BlackMisc { //! List of objects read from database. - //! Such objects should implement \sa ITimestampBased and \sa IDatastoreObjectWithIntegerKey - template class IDatastoreObjectListWithIntegerKey : public ITimestampObjectList + //! Such objects should implement \sa ITimestampBased and \sa IDatastoreObjectWithIntegerKey or \sa IDatastoreObjectWithStringKey + template class IDatastoreObjectList : public ITimestampObjectList { public: //! Object with key, notFound otherwise - OBJ findByKey(int key, const OBJ ¬Found = OBJ()) const; + OBJ findByKey(KEYTYPE key, const OBJ ¬Found = OBJ()) const; //! Sort by timestamp void sortByKey(); //! All keys as list - QList toDbKeyList() const; + QList toDbKeyList() const; //! Remove objects with key - int removeObjectsWithKeys(const QList &keys); + int removeObjectsWithKeys(const QList &keys); + + //! Update or insert data (based on DB key) + int replaceOrAddObjectsByKey(const CONTAINER &container); //! From DB JSON with default prefixes static CONTAINER fromDatabaseJson(const QJsonArray &array); @@ -45,35 +48,7 @@ namespace BlackMisc protected: //! Constructor - IDatastoreObjectListWithIntegerKey(); - }; - - //! List of objects read from database. - //! Such objects should implement \sa ITimestampBased and \sa IDatastoreObjectWithString - template class IDatastoreObjectListWithStringKey : public ITimestampObjectList - { - public: - //! Object with key, not found otherwise - OBJ findByKey(const QString &key, const OBJ ¬Found = OBJ()) const; - - //! Sort by timestamp - void sortByKey(); - - //! All keys as string list - QStringList toDbKeyList() const; - - //! Remove objects with key - int removeObjectsWithKeys(const QStringList &keys); - - //! From DB JSON with default prefixes - static CONTAINER fromDatabaseJson(const QJsonArray &array); - - //! From DB JSON - static CONTAINER fromDatabaseJson(const QJsonArray &array, const QString &prefix); - - protected: - //! Constructor - IDatastoreObjectListWithStringKey(); + IDatastoreObjectList(); }; //! \cond PRIVATE @@ -91,12 +66,12 @@ namespace BlackMisc class CAircraftModelList; } - extern template class BLACKMISC_EXPORT_TEMPLATE IDatastoreObjectListWithIntegerKey; - extern template class BLACKMISC_EXPORT_TEMPLATE IDatastoreObjectListWithIntegerKey; - extern template class BLACKMISC_EXPORT_TEMPLATE IDatastoreObjectListWithIntegerKey; - extern template class BLACKMISC_EXPORT_TEMPLATE IDatastoreObjectListWithIntegerKey; - extern template class BLACKMISC_EXPORT_TEMPLATE IDatastoreObjectListWithStringKey; - extern template class BLACKMISC_EXPORT_TEMPLATE IDatastoreObjectListWithStringKey; + extern template class BLACKMISC_EXPORT_TEMPLATE IDatastoreObjectList; + extern template class BLACKMISC_EXPORT_TEMPLATE IDatastoreObjectList; + extern template class BLACKMISC_EXPORT_TEMPLATE IDatastoreObjectList; + extern template class BLACKMISC_EXPORT_TEMPLATE IDatastoreObjectList; + extern template class BLACKMISC_EXPORT_TEMPLATE IDatastoreObjectList; + extern template class BLACKMISC_EXPORT_TEMPLATE IDatastoreObjectList; //! \endcond } //namespace diff --git a/src/blackmisc/simulation/aircraftmodellist.h b/src/blackmisc/simulation/aircraftmodellist.h index f94510dd8..40108cc72 100644 --- a/src/blackmisc/simulation/aircraftmodellist.h +++ b/src/blackmisc/simulation/aircraftmodellist.h @@ -25,7 +25,7 @@ namespace BlackMisc //! Value object encapsulating a list of aircraft models class BLACKMISC_EXPORT CAircraftModelList : public CSequence, - public IDatastoreObjectListWithIntegerKey, + public IDatastoreObjectList, public BlackMisc::Mixin::MetaType { public: diff --git a/src/blackmisc/simulation/distributorlist.h b/src/blackmisc/simulation/distributorlist.h index 8801a089e..1ac97156c 100644 --- a/src/blackmisc/simulation/distributorlist.h +++ b/src/blackmisc/simulation/distributorlist.h @@ -28,7 +28,7 @@ namespace BlackMisc //! Value object encapsulating a list of distributors. class BLACKMISC_EXPORT CDistributorList : public BlackMisc::CSequence, - public BlackMisc::IDatastoreObjectListWithStringKey, + public BlackMisc::IDatastoreObjectList, public BlackMisc::Mixin::MetaType { public: