commit 055827d7cff3c1a5223e527802dd3120ac83b6a2
parent ede5960f5d9125a8af08961e2f2dd06044d041c2
Author: cfillion <cfillion@users.noreply.github.com>
Date: Thu, 5 May 2016 22:43:31 -0400
refactor platform matching code
Diffstat:
10 files changed, 234 insertions(+), 222 deletions(-)
diff --git a/src/index_v1.cpp b/src/index_v1.cpp
@@ -27,6 +27,7 @@ static void LoadMetadataV1(TiXmlElement *, Index *ri);
static void LoadCategoryV1(TiXmlElement *, Index *ri);
static void LoadPackageV1(TiXmlElement *, Category *cat);
static void LoadVersionV1(TiXmlElement *, Package *pkg);
+static void LoadSourceV1(TiXmlElement *, Version *pkg);
void Index::loadV1(TiXmlElement *root, Index *ri)
{
@@ -140,23 +141,7 @@ void LoadVersionV1(TiXmlElement *verNode, Package *pkg)
TiXmlElement *node = verNode->FirstChildElement("source");
while(node) {
- const char *platform = node->Attribute("platform");
- if(!platform) platform = "all";
-
- const char *type = node->Attribute("type");
- if(!type) type = "";
-
- const char *file = node->Attribute("file");
- if(!file) file = "";
-
- const char *url = node->GetText();
- if(!url) url = "";
-
- Source *src = new Source(file, url, ver);
- src->setPlatform(Source::getPlatform(platform));
- src->setTypeOverride(Package::getType(type));
- ver->addSource(src);
-
+ LoadSourceV1(node, ver);
node = node->NextSiblingElement("source");
}
@@ -171,3 +156,23 @@ void LoadVersionV1(TiXmlElement *verNode, Package *pkg)
ptr.release();
}
+
+void LoadSourceV1(TiXmlElement *node, Version *ver)
+{
+ const char *platform = node->Attribute("platform");
+ if(!platform) platform = "all";
+
+ const char *type = node->Attribute("type");
+ if(!type) type = "";
+
+ const char *file = node->Attribute("file");
+ if(!file) file = "";
+
+ const char *url = node->GetText();
+ if(!url) url = "";
+
+ Source *src = new Source(file, url, ver);
+ src->setPlatform(platform);
+ src->setTypeOverride(Package::getType(type));
+ ver->addSource(src);
+}
diff --git a/src/platform.cpp b/src/platform.cpp
@@ -0,0 +1,66 @@
+/* ReaPack: Package manager for REAPER
+ * Copyright (C) 2015-2016 Christian Fillion
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "platform.hpp"
+
+#include <cstring>
+
+auto Platform::parse(const char *platform) -> Enum
+{
+ if(!strcmp(platform, "all"))
+ return GenericPlatform;
+ else if(!strcmp(platform, "windows"))
+ return WindowsPlatform;
+ else if(!strcmp(platform, "win32"))
+ return Win32Platform;
+ else if(!strcmp(platform, "win64"))
+ return Win64Platform;
+ else if(!strcmp(platform, "darwin"))
+ return DarwinPlatform;
+ else if(!strcmp(platform, "darwin32"))
+ return Darwin32Platform;
+ else if(!strcmp(platform, "darwin64"))
+ return Darwin64Platform;
+ else
+ return UnknownPlatform;
+}
+
+bool Platform::test() const
+{
+ switch(m_value) {
+ case GenericPlatform:
+#ifdef __APPLE__
+ case DarwinPlatform:
+#ifdef __x86_64__
+ case Darwin64Platform:
+#else
+ case Darwin32Platform:
+#endif
+
+#elif _WIN32
+ case WindowsPlatform:
+#ifdef _WIN64
+ case Win64Platform:
+#else
+ case Win32Platform:
+#endif
+#endif
+ return true;
+ default:
+ return false;
+ }
+}
diff --git a/src/platform.hpp b/src/platform.hpp
@@ -0,0 +1,54 @@
+/* ReaPack: Package manager for REAPER
+ * Copyright (C) 2015-2016 Christian Fillion
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef REAPACK_PLATFORM_HPP
+#define REAPACK_PLATFORM_HPP
+
+class Platform {
+public:
+ enum Enum {
+ UnknownPlatform,
+ GenericPlatform,
+
+ // windows
+ WindowsPlatform,
+ Win32Platform,
+ Win64Platform,
+
+ // os x
+ DarwinPlatform,
+ Darwin32Platform,
+ Darwin64Platform,
+ };
+
+ Platform() : m_value(GenericPlatform) {}
+ Platform(Enum val) : m_value(val) {}
+ Platform(const char *str) : m_value(parse(str)) {}
+
+ Enum value() const { return m_value; }
+ bool test() const;
+
+ operator Enum() const { return m_value; }
+ Platform &operator=(Enum n) { m_value = n; return *this; }
+
+private:
+ static Enum parse(const char *);
+
+ Enum m_value;
+};
+
+#endif
diff --git a/src/source.cpp b/src/source.cpp
@@ -22,29 +22,8 @@
using namespace std;
-Source::Platform Source::getPlatform(const char *platform)
-{
- if(!strcmp(platform, "all"))
- return GenericPlatform;
- else if(!strcmp(platform, "windows"))
- return WindowsPlatform;
- else if(!strcmp(platform, "win32"))
- return Win32Platform;
- else if(!strcmp(platform, "win64"))
- return Win64Platform;
- else if(!strcmp(platform, "darwin"))
- return DarwinPlatform;
- else if(!strcmp(platform, "darwin32"))
- return Darwin32Platform;
- else if(!strcmp(platform, "darwin64"))
- return Darwin64Platform;
- else
- return UnknownPlatform;
-}
-
Source::Source(const string &file, const string &url, const Version *ver)
- : m_platform(GenericPlatform), m_type(Package::UnknownType),
- m_file(file), m_url(url), m_version(ver)
+ : m_type(Package::UnknownType), m_file(file), m_url(url), m_version(ver)
{
if(m_url.empty())
throw reapack_error("empty source url");
diff --git a/src/source.hpp b/src/source.hpp
@@ -18,9 +18,8 @@
#ifndef REAPACK_SOURCE_HPP
#define REAPACK_SOURCE_HPP
-#include <string>
-
#include "package.hpp"
+#include "platform.hpp"
class Package;
class Path;
@@ -28,23 +27,6 @@ class Version;
class Source {
public:
- enum Platform {
- UnknownPlatform,
- GenericPlatform,
-
- // windows
- WindowsPlatform,
- Win32Platform,
- Win64Platform,
-
- // os x
- DarwinPlatform,
- Darwin32Platform,
- Darwin64Platform,
- };
-
- static Platform getPlatform(const char *);
-
Source(const std::string &file, const std::string &url,
const Version * = nullptr);
diff --git a/src/version.cpp b/src/version.cpp
@@ -115,34 +115,9 @@ string Version::displayAuthor() const
void Version::addSource(Source *source)
{
- const Source::Platform p = source->platform();
-
-#ifdef __APPLE__
- if(p != Source::GenericPlatform && p < Source::DarwinPlatform)
- return;
-
-#ifdef __x86_64__
- if(p == Source::Darwin32Platform)
- return;
-#else
- if(p == Source::Darwin64Platform)
- return;
-#endif
-
-#elif _WIN32
- if(p == Source::UnknownPlatform || p > Source::Win64Platform)
- return;
-
-#ifdef _WIN64
- if(p == Source::Win32Platform)
+ if(!source->platform().test())
return;
-#else
- if(p == Source::Win64Platform)
- return;
-#endif
-#endif
-
- if(source->version() != this)
+ else if(source->version() != this)
throw reapack_error("source belongs to another version");
const Path path = source->targetPath();
diff --git a/test/index_v1.cpp b/test/index_v1.cpp
@@ -131,14 +131,14 @@ TEST_CASE("null source file", M) {
REQUIRE(pkg->version(0)->source(0)->file() == pkg->name());
}
-TEST_CASE("default platform", M) {
+TEST_CASE("default source platform", M) {
UseRootPath root(RIPATH);
IndexPtr ri = Index::load("missing_platform");
CHECK(ri->packages().size() == 1);
REQUIRE(ri->category(0)->package(0)->version(0)->source(0)->platform()
- == Source::GenericPlatform);
+ == Platform::GenericPlatform);
}
TEST_CASE("version changelog", M) {
@@ -172,7 +172,7 @@ TEST_CASE("full index", M) {
REQUIRE(ver->changelog() == "Fixed a division by zero error.");
const Source *source = ver->source(0);
- REQUIRE(source->platform() == Source::GenericPlatform);
+ REQUIRE(source->platform() == Platform::GenericPlatform);
REQUIRE(source->file() == "test.lua");
REQUIRE(source->url() == "https://google.com/");
}
diff --git a/test/platform.cpp b/test/platform.cpp
@@ -0,0 +1,79 @@
+#include <catch.hpp>
+
+#include <platform.hpp>
+
+#include <map>
+
+using namespace std;
+
+static const char *M = "[platform]";
+
+TEST_CASE("default platform", M) {
+ const Platform pl;
+ REQUIRE(pl.value() == Platform::GenericPlatform);
+ REQUIRE(pl.test());
+}
+
+TEST_CASE("platform from string", M) {
+ SECTION("unknown")
+ REQUIRE(Platform("hello") == Platform::UnknownPlatform);
+
+ SECTION("generic")
+ REQUIRE(Platform("all") == Platform::GenericPlatform);
+
+ SECTION("generic windows")
+ REQUIRE(Platform("windows") == Platform::WindowsPlatform);
+
+ SECTION("windows 32-bit")
+ REQUIRE(Platform("win32") == Platform::Win32Platform);
+
+ SECTION("windows 64-bit")
+ REQUIRE(Platform("win64") == Platform::Win64Platform);
+
+ SECTION("generic os x")
+ REQUIRE(Platform("darwin") == Platform::DarwinPlatform);
+
+ SECTION("os x 32-bit")
+ REQUIRE(Platform("darwin32") == Platform::Darwin32Platform);
+
+ SECTION("os x 64-bit")
+ REQUIRE(Platform("darwin64") == Platform::Darwin64Platform);
+}
+
+TEST_CASE("test platform", M) {
+ const map<Platform, bool> expected {
+ {Platform::GenericPlatform, true},
+
+#ifdef __APPLE__
+ {Platform::WindowsPlatform, false},
+ {Platform::Win32Platform, false},
+ {Platform::Win64Platform, false},
+
+ {Platform::DarwinPlatform, true},
+#ifdef __x86_64__
+ {Platform::Darwin32Platform, false},
+ {Platform::Darwin64Platform, true},
+#else
+ {Platform::Darwin32Platform, true},
+ {Platform::Darwin64Platform, false},
+#endif
+
+#elif _WIN32
+ {Platform::DarwinPlatform, false},
+ {Platform::Darwin32Platform, false},
+ {Platform::Darwin64Platform, false},
+
+ {Platform::WindowsPlatform, true},
+#ifdef _WIN64
+ {Platform::Win32Platform, false},
+ {Platform::Win64Platform, true},
+#else
+ {Platform::Win32Platform, true},
+ {Platform::Win64Platform, false},
+#endif
+#endif
+ };
+
+ for(const auto &it : expected)
+ REQUIRE(it.first.test() == it.second);
+}
diff --git a/test/source.cpp b/test/source.cpp
@@ -10,41 +10,15 @@ using namespace std;
static const char *M = "[source]";
-TEST_CASE("source platform from string", M) {
- SECTION("unknown")
- REQUIRE(Source::getPlatform("hello") == Source::UnknownPlatform);
-
- SECTION("generic")
- REQUIRE(Source::getPlatform("all") == Source::GenericPlatform);
-
- SECTION("generic windows")
- REQUIRE(Source::getPlatform("windows") == Source::WindowsPlatform);
-
- SECTION("windows 32-bit")
- REQUIRE(Source::getPlatform("win32") == Source::Win32Platform);
-
- SECTION("windows 64-bit")
- REQUIRE(Source::getPlatform("win64") == Source::Win64Platform);
-
- SECTION("generic os x")
- REQUIRE(Source::getPlatform("darwin") == Source::DarwinPlatform);
-
- SECTION("os x 32-bit")
- REQUIRE(Source::getPlatform("darwin32") == Source::Darwin32Platform);
-
- SECTION("os x 64-bit")
- REQUIRE(Source::getPlatform("darwin64") == Source::Darwin64Platform);
-}
-
TEST_CASE("source platform", M) {
Source src({}, "url");
- REQUIRE(src.platform() == Source::GenericPlatform);
+ REQUIRE(src.platform() == Platform::GenericPlatform);
- src.setPlatform(Source::UnknownPlatform);
- REQUIRE(src.platform() == Source::UnknownPlatform);
+ src.setPlatform(Platform::UnknownPlatform);
+ REQUIRE(src.platform() == Platform::UnknownPlatform);
- src.setPlatform(Source::WindowsPlatform);
- REQUIRE(src.platform() == Source::WindowsPlatform);
+ src.setPlatform(Platform::WindowsPlatform);
+ REQUIRE(src.platform() == Platform::WindowsPlatform);
}
TEST_CASE("source type override", M) {
diff --git a/test/version.cpp b/test/version.cpp
@@ -16,13 +16,6 @@ using namespace std;
Package pkg(Package::ScriptType, "Hello", &cat); \
Version ver("1", &pkg);
-static Source *mksource(Source::Platform p, Version *parent)
-{
- Source *src = new Source("file", "url", parent);
- src->setPlatform(p);
- return src;
-}
-
static const char *M = "[version]";
TEST_CASE("construct null version", M) {
@@ -269,7 +262,7 @@ TEST_CASE("list files", M) {
TEST_CASE("drop sources for unknown platforms", M) {
MAKE_VERSION
Source *src = new Source("a", "b", &ver);
- src->setPlatform(Source::UnknownPlatform);
+ src->setPlatform(Platform::UnknownPlatform);
ver.addSource(src);
REQUIRE(ver.sources().size() == 0);
@@ -332,98 +325,3 @@ TEST_CASE("copy version constructor", M) {
const Version copy2(original, &pkg);
REQUIRE(copy2.package() == &pkg);
}
-
-#ifdef __APPLE__
-TEST_CASE("drop windows sources on os x", M) {
- MAKE_VERSION
-
- ver.addSource(mksource(Source::WindowsPlatform, &ver));
- ver.addSource(mksource(Source::Win32Platform, &ver));
- ver.addSource(mksource(Source::Win64Platform, &ver));
-
- REQUIRE(ver.sources().size() == 0);
-}
-
-#ifdef __x86_64__
-TEST_CASE("drop 32-bit sources on os x 64-bit", M) {
- MAKE_VERSION
- ver.addSource(mksource(Source::Darwin32Platform, &ver));
-
- REQUIRE(ver.sources().size() == 0);
-}
-
-TEST_CASE("valid sources for os x 64-bit", M) {
- MAKE_VERSION
-
- ver.addSource(mksource(Source::GenericPlatform, &ver));
- ver.addSource(mksource(Source::DarwinPlatform, &ver));
- ver.addSource(mksource(Source::Darwin64Platform, &ver));
-
- REQUIRE(ver.sources().size() == 3);
-}
-#else
-TEST_CASE("drop 64-bit sources on os x 32-bit", M) {
- MAKE_VERSION
- ver.addSource(mksource(Source::Darwin64Platform, &ver));
-
- REQUIRE(ver.sources().size() == 0);
-}
-
-TEST_CASE("valid sources for os x 32-bit", M) {
- MAKE_VERSION
-
- ver.addSource(mksource(Source::GenericPlatform, &ver));
- ver.addSource(mksource(Source::DarwinPlatform, &ver));
- ver.addSource(mksource(Source::Darwin32Platform, &ver));
-
- REQUIRE(ver.sources().size() == 3);
-}
-#endif
-
-#elif _WIN32
-TEST_CASE("drop os x sources on windows", M) {
- MAKE_VERSION
-
- ver.addSource(mksource(Source::DarwinPlatform, &ver));
- ver.addSource(mksource(Source::Darwin32Platform, &ver));
- ver.addSource(mksource(Source::Darwin64Platform, &ver));
-
- REQUIRE(ver.sources().size() == 0);
-}
-
-#ifdef _WIN64
-TEST_CASE("drop 32-bit sources on windows 64-bit", M) {
- MAKE_VERSION
- ver.addSource(mksource(Source::Win32Platform, &ver));
-
- REQUIRE(ver.sources().size() == 0);
-}
-
-TEST_CASE("valid sources for windows 64-bit", M) {
- MAKE_VERSION
-
- ver.addSource(mksource(Source::GenericPlatform, &ver));
- ver.addSource(mksource(Source::WindowsPlatform, &ver));
- ver.addSource(mksource(Source::Win64Platform, &ver));
-
- REQUIRE(ver.sources().size() == 3);
-}
-#else
-TEST_CASE("drop 64-bit sources on windows 32-bit", M) {
- MAKE_VERSION
- ver.addSource(mksource(Source::Win64Platform, &ver));
-
- REQUIRE(ver.sources().size() == 0);
-}
-
-TEST_CASE("valid sources for windows 32-bit", M) {
- MAKE_VERSION
-
- ver.addSource(mksource(Source::GenericPlatform, &ver));
- ver.addSource(mksource(Source::WindowsPlatform, &ver));
- ver.addSource(mksource(Source::Win32Platform, &ver));
-
- REQUIRE(ver.sources().size() == 3);
-}
-#endif
-#endif