commit 20a4ced5ae6876ffa18313a8e8ac7a3b3d0ff104
parent 297d534f3075ceae411657329bcd96c34275edb8
Author: cfillion <cfillion@users.noreply.github.com>
Date: Fri, 2 Jun 2017 18:15:51 -0400
api: alias Registry::Entry to PackageEntry and simplify CompareVersions
Diffstat:
2 files changed, 19 insertions(+), 27 deletions(-)
diff --git a/src/api.cpp b/src/api.cpp
@@ -37,7 +37,8 @@ using namespace std;
ReaPack *API::reapack = nullptr;
-static set<Registry::Entry *> s_entries;
+typedef Registry::Entry PackageEntry;
+static set<PackageEntry *> s_entries;
APIDef::APIDef(const APIFunc *func)
: m_func(func)
@@ -90,7 +91,7 @@ void APIDef::unregister(const char *key, void *ptr)
"APIdef_" API_PREFIX #name, (void *)API_##name::definition, \
}
-DEFINE_API(bool, AboutInstalledPackage, ((Registry::Entry*, entry)), R"(
+DEFINE_API(bool, AboutInstalledPackage, ((PackageEntry*, entry)), R"(
Show the about dialog of the given package entry.
The repository index is downloaded asynchronously if the cached copy doesn't exist or is older than one week.
)", {
@@ -98,7 +99,7 @@ DEFINE_API(bool, AboutInstalledPackage, ((Registry::Entry*, entry)), R"(
return false;
// the one given by the user may be deleted while we download the idnex
- const Registry::Entry entryCopy = *entry;
+ const PackageEntry entryCopy = *entry;
const Remote &repo = reapack->remote(entry->remote);
if(!repo)
@@ -137,27 +138,23 @@ DEFINE_API(bool, AboutRepository, ((const char*, repoName)), R"(
return false;
});
-DEFINE_API(bool, CompareVersions, ((const char*, ver1))((const char*, ver2))
- ((int*, resultOut))((char*, errorOut))((int, errorOut_sz)), R"(
+DEFINE_API(int, CompareVersions, ((const char*, ver1))((const char*, ver2))
+ ((char*, errorOut))((int, errorOut_sz)), R"(
Returns 0 if both versions are equal, a positive value if ver1 is higher than ver2 and a negative value otherwise.
)", {
VersionName a, b;
string error;
- if(a.tryParse(ver1, &error) && b.tryParse(ver2, &error)) {
- *resultOut = a.compare(b);
- return true;
- }
+ b.tryParse(ver2, &error);
+ a.tryParse(ver1, &error);
if(errorOut)
snprintf(errorOut, errorOut_sz, "%s", error.c_str());
- *resultOut = 0;
-
- return false;
+ return a.compare(b);
});
-DEFINE_API(Registry::Entry*, GetOwner, ((const char*, fn))((char*, errorOut))((int, errorOut_sz)), R"(
+DEFINE_API(PackageEntry*, GetOwner, ((const char*, fn))((char*, errorOut))((int, errorOut_sz)), R"(
Returns the package entry owning the given file.
Delete the returned object from memory after use with <a href="#ReaPack_FreeEntry">ReaPack_FreeEntry</a>.
)", {
@@ -173,7 +170,7 @@ DEFINE_API(Registry::Entry*, GetOwner, ((const char*, fn))((char*, errorOut))((i
const auto &owner = reg.getOwner(path);
if(owner) {
- auto entry = new Registry::Entry(owner);
+ auto entry = new PackageEntry(owner);
s_entries.insert(entry);
return entry;
}
@@ -191,7 +188,7 @@ DEFINE_API(Registry::Entry*, GetOwner, ((const char*, fn))((char*, errorOut))((i
}
});
-DEFINE_API(void, FreeEntry, ((Registry::Entry*, entry)), R"(
+DEFINE_API(void, FreeEntry, ((PackageEntry*, entry)), R"(
Free resources allocated for the given package entry.
)", {
if(s_entries.count(entry)) {
diff --git a/test/api.cpp b/test/api.cpp
@@ -9,36 +9,31 @@ using namespace std;
static const char *M = "[api]";
TEST_CASE("CompareVersions", M) {
- auto CompareVersions = (bool (*)(const char *, const char *,
- int*, char *, int))API::CompareVersions.cImpl;
+ const auto CompareVersions = (int (*)(const char *, const char *,
+ char *, int))API::CompareVersions.cImpl;
- int result;
char error[255] = {};
SECTION("equal") {
- REQUIRE(CompareVersions("1.0", "1.0", &result, error, sizeof(error)));
- REQUIRE(result == 0);
+ REQUIRE(CompareVersions("1.0", "1.0", error, sizeof(error)) == 0);
REQUIRE(strcmp(error, "") == 0);
}
SECTION("lower") {
- REQUIRE(CompareVersions("1.0", "2.0", &result, error, sizeof(error)));
- REQUIRE(result < 0);
+ REQUIRE(CompareVersions("1.0", "2.0", error, sizeof(error)) < 0);
REQUIRE(strcmp(error, "") == 0);
}
SECTION("higher") {
- REQUIRE(CompareVersions("2.0", "1.0", &result, error, sizeof(error)));
- REQUIRE(result > 0);
+ REQUIRE(CompareVersions("2.0", "1.0", error, sizeof(error)) > 0);
REQUIRE(strcmp(error, "") == 0);
}
SECTION("invalid") {
- REQUIRE_FALSE(CompareVersions("abc", "def", &result, error, sizeof(error)));
- REQUIRE(result == 0);
+ REQUIRE(CompareVersions("abc", "def", error, sizeof(error)) == 0);
REQUIRE(strcmp(error, "invalid version name 'abc'") == 0);
}
SECTION("invalid no error buffer")
- REQUIRE_FALSE(CompareVersions("abc", "def", &result, nullptr, 0));
+ CompareVersions("abc", "def", nullptr, 0); // no crash
}