1
0
Fork 0

Add-ons: add UrlType enum class, QualifiedUrl class and Addon::getUrls() method

QualifiedUrl is essentially a pair containing an enum value
(addons::UrlType::homePage, addons::UrlType::download, etc.) and an
std::string for the URL per se, with adequate getters and setters.

Addon::getUrls() is for people who wish to process all non-empty URLs
occurring as part of the add-on metadata in batch.

Mailing-list discussion:

  https://sourceforge.net/p/flightgear/mailman/message/36159711/
This commit is contained in:
Florent Rougon 2017-12-16 08:33:36 +01:00
parent f5ab26bd71
commit ec349c6ac5
3 changed files with 81 additions and 0 deletions

View file

@ -25,12 +25,15 @@
#include <utility>
#include <vector>
#include <cassert>
#include <simgear/misc/sg_path.hxx>
#include <simgear/nasal/cppbind/Ghost.hxx>
#include <simgear/nasal/cppbind/NasalHash.hxx>
#include <simgear/nasal/naref.h>
#include <simgear/props/props.hxx>
#include <simgear/props/props_io.hxx>
#include <simgear/sg_inlines.h>
#include <Main/globals.hxx>
#include <Scripting/NasalSys.hxx>
@ -51,6 +54,31 @@ namespace flightgear
namespace addons
{
// ***************************************************************************
// * QualifiedUrl *
// ***************************************************************************
QualifiedUrl::QualifiedUrl(UrlType type, std::string url)
: _type(type),
_url(std::move(url))
{ }
UrlType QualifiedUrl::getType() const
{ return _type; }
void QualifiedUrl::setType(UrlType type)
{ _type = type; }
std::string QualifiedUrl::getUrl() const
{ return _url; }
void QualifiedUrl::setUrl(const std::string& url)
{ _url = url; }
// ***************************************************************************
// * Addon *
// ***************************************************************************
Addon::Addon(std::string id, AddonVersion version, SGPath basePath,
std::string minFGVersionRequired, std::string maxFGVersionRequired,
SGPropertyNode* addonNode)
@ -506,6 +534,29 @@ Addon::parseLicenseNode(const SGPath& addonPath, SGPropertyNode* addonNode)
return std::make_tuple(licenseDesignation, licenseFile, licenseUrl);
}
std::map<UrlType, QualifiedUrl> Addon::getUrls() const
{
std::map<UrlType, QualifiedUrl> res;
auto appendIfNonEmpty = [&res](UrlType type, const string& url) {
if (!url.empty()) {
auto emplaceRetval = res.emplace(type, QualifiedUrl(type, url));
// We start with an empty std::map<> and don't call this lambda more
// than once for the same type, therefore the same key can't be seen
// twice.
assert(emplaceRetval.second);
SG_UNUSED(emplaceRetval); // 'cause asserts are removed in Release builds
}
};
appendIfNonEmpty(UrlType::homePage, getHomePage());
appendIfNonEmpty(UrlType::download, getDownloadUrl());
appendIfNonEmpty(UrlType::support, getSupportUrl());
appendIfNonEmpty(UrlType::codeRepository, getCodeRepositoryUrl());
appendIfNonEmpty(UrlType::license, getLicenseUrl());
return res;
}
// Static method
void Addon::setupGhost(nasal::Hash& addonsModule)

View file

@ -20,6 +20,7 @@
#ifndef FG_ADDON_HXX
#define FG_ADDON_HXX
#include <map>
#include <ostream>
#include <string>
#include <tuple>
@ -40,6 +41,29 @@ namespace flightgear
namespace addons
{
enum class UrlType {
homePage,
download,
support,
codeRepository,
license
};
class QualifiedUrl {
public:
QualifiedUrl(UrlType type, std::string url);
UrlType getType() const;
void setType(UrlType type);
std::string getUrl() const;
void setUrl(const std::string& url);
private:
UrlType _type;
std::string _url;
};
class Addon : public SGReferenced {
public:
// Default constructor. 'minFGVersionRequired' is initialized to "2017.4.0"
@ -131,6 +155,9 @@ public:
int getLoadSequenceNumber() const;
void setLoadSequenceNumber(int num);
// Get all non-empty URLs pertaining to this add-on
std::map<UrlType, QualifiedUrl> getUrls() const;
// Simple string representation
std::string str() const;

View file

@ -34,6 +34,9 @@ class AddonManager;
class AddonVersion;
class AddonVersionSuffix;
enum class UrlType;
class QualifiedUrl;
using AddonRef = SGSharedPtr<Addon>;
using AddonVersionRef = SGSharedPtr<AddonVersion>;