/* Copyright (C) 2015 * swift Project Community / Contributors * * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, * including this file, may be copied, modified, propagated, or distributed except according to the terms * contained in the LICENSE file. */ #include "blackmisc/datastoreobjectlist.h" #include "blackmisc/predicates.h" #include "blackmisc/countrylist.h" #include "blackmisc/aviation/liverylist.h" #include "blackmisc/aviation/aircrafticaocodelist.h" #include "blackmisc/aviation/airlineicaocodelist.h" #include "blackmisc/simulation/aircraftmodellist.h" #include "blackmisc/simulation/distributorlist.h" #include #include namespace BlackMisc { template IDatastoreObjectList::IDatastoreObjectList() { } template OBJ IDatastoreObjectList::findByKey(KEYTYPE key, const OBJ ¬Found) const { return this->container().findFirstByOrDefault(&OBJ::getDbKey, key, notFound); } template OBJ IDatastoreObjectList::maxKeyObject() const { if (this->container().isEmpty()) { return OBJ(); } const OBJ max = *std::max_element(this->container().begin(), this->container().end(), [](const OBJ & obj1, const OBJ & obj2) { bool v1 = obj1.hasValidDbKey(); bool v2 = obj2.hasValidDbKey(); if (v1 && v2) { return obj1.getDbKey() < obj2.getDbKey(); } return v2; }); return max; } template void IDatastoreObjectList::sortByKey() { this->container().sort(BlackMisc::Predicates::MemberLess(&OBJ::getDbKey)); } template QList IDatastoreObjectList::toDbKeyList() const { QList keys; for (const OBJ &obj : ITimestampObjectList::container()) { if (!obj.hasValidDbKey()) { continue; } keys.append(obj.getDbKey()); } return keys; } template KEYTYPE IDatastoreObjectList::getMaxKey(bool *ok) const { QList keys(this->toDbKeyList()); if (keys.isEmpty()) { if (ok) { *ok = false; } return KEYTYPE(); } KEYTYPE max = *std::max_element(keys.begin(), keys.end()); if (ok) { *ok = true; } return max; } template int IDatastoreObjectList::removeObjectsWithKeys(const QList &keys) { if (keys.isEmpty()) { return 0; } if (this->container().isEmpty()) { return 0; } CONTAINER newValues; for (const OBJ &obj : ITimestampObjectList::container()) { if (keys.contains(obj.getDbKey())) { continue; } newValues.push_back(obj); } int delta = this->container().size() - newValues.size(); this->container() = newValues; return delta; } template int IDatastoreObjectList::removeObjectsWithoutDbKey() { if (this->container().isEmpty()) { return 0; } CONTAINER newValues; for (const OBJ &obj : ITimestampObjectList::container()) { if (!obj.hasValidDbKey()) { continue; } newValues.push_back(obj); } int delta = this->container().size() - newValues.size(); this->container() = newValues; return delta; } template int IDatastoreObjectList::replaceOrAddObjectsByKey(const CONTAINER &container) { if (container.isEmpty()) { return 0; } if (this->container().isEmpty()) { this->container() = container; return this->container().size(); } 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 CONTAINER IDatastoreObjectList::fromDatabaseJson(const QJsonArray &array) { CONTAINER container; for (const QJsonValue &value : array) { container.push_back(OBJ::fromDatabaseJson(value.toObject())); } return container; } template CONTAINER IDatastoreObjectList::fromDatabaseJson(const QJsonArray &array, const QString &prefix) { CONTAINER container; for (const QJsonValue &value : array) { container.push_back(OBJ::fromDatabaseJson(value.toObject(), prefix)); } return container; } // see here for the reason of thess forward instantiations // http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html template class IDatastoreObjectList; template class IDatastoreObjectList; template class IDatastoreObjectList; template class IDatastoreObjectList; template class IDatastoreObjectList; template class IDatastoreObjectList; } // namespace