Packages: support a type enum

Work towards other package types, conceptually. (Not actually
implemented yet)
This commit is contained in:
James Turner 2021-03-16 20:35:32 +00:00
parent a02353a280
commit 224b557573
9 changed files with 147 additions and 79 deletions

View File

@ -7,6 +7,7 @@ set(HEADERS
Install.hxx
Root.hxx
Delegate.hxx
PackageCommon.hxx
)
set(SOURCES

View File

@ -295,21 +295,27 @@ bool Catalog::removeDirectory()
return d.remove(true /* recursive */);
}
PackageList const&
Catalog::packages() const
PackageList
Catalog::packages(Type ty) const
{
return m_packages;
PackageList r;
std::copy_if(m_packages.begin(), m_packages.end(),
std::back_inserter(r),
[ty](const PackageRef& p) {
return p->type() == ty;
});
return r;
}
PackageList
Catalog::packagesMatching(const SGPropertyNode* aFilter) const
{
PackageList r;
for (auto p : m_packages) {
if (p->matches(aFilter)) {
r.push_back(p);
}
}
std::copy_if(m_packages.begin(), m_packages.end(),
std::back_inserter(r),
[aFilter](const PackageRef& p) {
return p->matches(aFilter);
});
return r;
}
@ -317,27 +323,23 @@ PackageList
Catalog::packagesNeedingUpdate() const
{
PackageList r;
for (auto p : m_packages) {
if (!p->isInstalled()) {
continue;
}
if (p->install()->hasUpdate()) {
r.push_back(p);
}
}
std::copy_if(m_packages.begin(), m_packages.end(),
std::back_inserter(r),
[](const PackageRef& p) {
return p->isInstalled() && p->install()->hasUpdate();
});
return r;
}
PackageList
Catalog::installedPackages() const
Catalog::installedPackages(Type ty) const
{
PackageList r;
for (auto p : m_packages) {
if (p->isInstalled()) {
r.push_back(p);
}
}
std::copy_if(m_packages.begin(), m_packages.end(),
std::back_inserter(r),
[ty](const PackageRef& p) {
return p->isInstalled() && p->type() == ty;
});
return r;
}

View File

@ -32,6 +32,7 @@
#include <simgear/io/HTTPRequest.hxx>
#include <simgear/package/Delegate.hxx>
#include <simgear/package/PackageCommon.hxx>
namespace simgear
{
@ -40,20 +41,6 @@ namespace HTTP { class Client; }
namespace pkg
{
// forward decls
class Package;
class Catalog;
class Root;
class Install;
typedef SGSharedPtr<Package> PackageRef;
typedef SGSharedPtr<Catalog> CatalogRef;
typedef SGSharedPtr<Install> InstallRef;
typedef std::vector<PackageRef> PackageList;
typedef std::vector<CatalogRef> CatalogList;
class Catalog : public SGReferenced
{
public:
@ -79,7 +66,7 @@ public:
/**
* Get all packages in this catalog.
*/
PackageList const& packages() const;
PackageList packages(Type ty = AircraftPackage) const;
/**
* retrieve packages in this catalog matching a filter.
@ -90,7 +77,7 @@ public:
/**
* packages which are locally installed
*/
PackageList installedPackages() const;
PackageList installedPackages(Type ty = AircraftPackage) const;
/**
* retrieve all the packages in the catalog which are installed

View File

@ -20,9 +20,10 @@
#include <vector>
#include <simgear/misc/sg_path.hxx>
#include <simgear/misc/sg_dir.hxx>
#include <simgear/misc/sg_path.hxx>
#include <simgear/package/Delegate.hxx>
#include <simgear/package/PackageCommon.hxx>
#include <simgear/structure/function_list.hxx>
#include <simgear/structure/SGReferenced.hxx>
@ -35,15 +36,6 @@ namespace simgear
namespace pkg
{
// forward decls
class Package;
class Catalog;
class Install;
typedef SGSharedPtr<Package> PackageRef;
typedef SGSharedPtr<Catalog> CatalogRef;
typedef SGSharedPtr<Install> InstallRef;
/**
*
*/

View File

@ -29,10 +29,30 @@
#include <simgear/package/Install.hxx>
#include <simgear/package/Root.hxx>
namespace {
const string_list static_typeNames = {
"aircraft",
"ai-model",
"add-on"};
}
namespace simgear {
namespace pkg {
Type typeFromString(const std::string& s)
{
const auto l = strutils::lowercase(s);
const auto it = std::find(static_typeNames.begin(), static_typeNames.end(), l);
if (it != static_typeNames.end()) {
return static_cast<Type>(std::distance(static_typeNames.begin(), it));
}
return AircraftPackage; // for backwards-compat
}
Package::Package(const SGPropertyNode* aProps, CatalogRef aCatalog) :
m_catalog(aCatalog.get())
{
@ -53,6 +73,12 @@ void Package::initWithProps(const SGPropertyNode* aProps)
for (auto var : m_props->getChildren("variant")) {
m_variants.push_back(var->getStringValue("id"));
}
if (aProps->hasChild("type")) {
m_type = typeFromString(m_props->getStringValue("type"));
} else {
m_type = AircraftPackage;
}
}
void Package::updateFromProps(const SGPropertyNode* aProps)
@ -133,6 +159,12 @@ bool Package::matches(const SGPropertyNode* aFilter) const
}
}
if (filter_name == "type") {
handled = true;
const auto n = strutils::lowercase(aFilter->getStringValue());
return (n == static_typeNames.at(static_cast<int>(m_type)));
}
if (!handled) {
SG_LOG(SG_GENERAL, SG_WARN, "unknown filter term:" << filter_name);
}
@ -186,10 +218,23 @@ bool Package::isInstalled() const
return pathOnDisk().exists();
}
std::string Package::directoryForType(Type type)
{
switch (type) {
case AircraftPackage:
default:
return "Aircraft";
case AIModelPackage:
return "AI/Aircraft";
case AddOnPackage:
return "Add-On";
}
}
SGPath Package::pathOnDisk() const
{
SGPath p(m_catalog->installRoot());
p.append("Aircraft");
p.append(directoryForType(m_type));
p.append(dirName());
return p;
}
@ -585,6 +630,10 @@ bool Package::validate() const
return true;
}
Type Package::type() const
{
return m_type;
}
} // of namespace pkg

View File

@ -28,6 +28,8 @@
#include <simgear/structure/SGReferenced.hxx>
#include <simgear/structure/SGSharedPtr.hxx>
#include <simgear/package/PackageCommon.hxx>
typedef std::set<std::string> string_set;
namespace simgear
@ -35,19 +37,7 @@ namespace simgear
namespace pkg
{
// forward decls
class Install;
class Catalog;
class Package;
typedef SGSharedPtr<Package> PackageRef;
typedef SGSharedPtr<Catalog> CatalogRef;
typedef SGSharedPtr<Install> InstallRef;
typedef std::vector<PackageRef> PackageList;
class Package : public SGReferenced
class Package : public SGReferenced
{
public:
@ -214,6 +204,11 @@ public:
* (parents and children)
*/
std::string parentIdForVariant(unsigned int variantIndex) const;
Type type() const;
static std::string directoryForType(Type type);
private:
SGPath pathOnDisk() const;
@ -240,6 +235,7 @@ private:
bool matchesDescription(const std::string &search) const;
SGPropertyNode_ptr m_props;
Type m_type;
std::string m_id;
string_set m_tags;
Catalog* m_catalog = nullptr; // non-owning ref, explicitly

View File

@ -0,0 +1,52 @@
// Copyright (C) 2021 James Turner - james@flightgear.org
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
#pragma once
#include <vector>
#include <simgear/structure/SGSharedPtr.hxx>
namespace simgear {
namespace pkg {
enum Type {
AircraftPackage = 0,
AIModelPackage,
AddOnPackage
// if you extend this enum, extend the static_typeNames string array
// in Package.cxx file as well.
};
// forward decls
class Install;
class Catalog;
class Package;
class Root;
typedef SGSharedPtr<Package> PackageRef;
typedef SGSharedPtr<Catalog> CatalogRef;
typedef SGSharedPtr<Install> InstallRef;
typedef std::vector<PackageRef> PackageList;
typedef std::vector<CatalogRef> CatalogList;
} // namespace pkg
} // namespace simgear

View File

@ -534,13 +534,13 @@ CatalogList Root::allCatalogs() const
}
PackageList
Root::allPackages() const
Root::allPackages(Type ty) const
{
PackageList r;
CatalogDict::const_iterator it = d->catalogs.begin();
for (; it != d->catalogs.end(); ++it) {
const PackageList& r2(it->second->packages());
const PackageList& r2(it->second->packages(ty));
r.insert(r.end(), r2.begin(), r2.end());
}

View File

@ -23,6 +23,7 @@
#include <simgear/misc/sg_path.hxx>
#include <simgear/package/Delegate.hxx>
#include <simgear/package/PackageCommon.hxx>
#include <simgear/structure/SGReferenced.hxx>
#include <simgear/structure/SGSharedPtr.hxx>
@ -40,18 +41,6 @@ namespace HTTP {
namespace pkg
{
// forward decls
class Package;
class Catalog;
class Install;
typedef SGSharedPtr<Package> PackageRef;
typedef SGSharedPtr<Catalog> CatalogRef;
typedef SGSharedPtr<Install> InstallRef;
typedef std::vector<PackageRef> PackageList;
typedef std::vector<CatalogRef> CatalogList;
class Root : public SGReferenced
{
public:
@ -114,7 +103,7 @@ public:
/**
*
*/
PackageList allPackages() const;
PackageList allPackages(Type ty = AircraftPackage) const;
/**
* retrieve packages matching a filter.