BPackageResolvableExpression: Add Matches()

Checks if the given BPackageResolvable respectively the BPackageVersion
pair satisfies the expression.
This commit is contained in:
Ingo Weinhold 2013-08-28 00:11:11 +02:00
parent 39d0e79f2b
commit 48e17c15bc
2 changed files with 69 additions and 0 deletions

View File

@ -21,6 +21,9 @@ namespace BHPKG {
using BHPKG::BPackageResolvableExpressionData;
class BPackageResolvable;
/*
* Expresses a constraint on a specific resolvable, either just a name
* or a name plus a relational operator and a version.
@ -64,6 +67,12 @@ public:
= BPackageVersion());
void Clear();
bool Matches(const BPackageVersion& version,
const BPackageVersion& compatibleVersion)
const;
bool Matches(const BPackageResolvable& provides)
const;
public:
static const char* kOperatorNames[];

View File

@ -7,6 +7,7 @@
#include <package/PackageResolvableExpression.h>
#include <package/hpkg/PackageInfoAttributeValue.h>
#include <package/PackageResolvable.h>
namespace BPackageKit {
@ -122,4 +123,63 @@ BPackageResolvableExpression::Clear()
}
bool
BPackageResolvableExpression::Matches(const BPackageVersion& version,
const BPackageVersion& compatibleVersion) const
{
// If no particular version is required, we always match.
if (fVersion.InitCheck() != B_OK)
return true;
if (version.InitCheck() != B_OK)
return false;
int compare = version.Compare(fVersion);
bool matches = false;
switch (fOperator) {
case B_PACKAGE_RESOLVABLE_OP_LESS:
matches = compare < 0;
break;
case B_PACKAGE_RESOLVABLE_OP_LESS_EQUAL:
matches = compare <= 0;
break;
case B_PACKAGE_RESOLVABLE_OP_EQUAL:
matches = compare == 0;
break;
case B_PACKAGE_RESOLVABLE_OP_NOT_EQUAL:
matches = compare != 0;
break;
case B_PACKAGE_RESOLVABLE_OP_GREATER_EQUAL:
matches = compare >= 0;
break;
case B_PACKAGE_RESOLVABLE_OP_GREATER:
matches = compare > 0;
break;
default:
break;
}
if (!matches)
return false;
// Check compatible version. If not specified, the match must be exact.
// Otherwise fVersion must be >= compatibleVersion.
if (compatibleVersion.InitCheck() != B_OK)
return compare == 0;
// Since compatibleVersion <= version, we can save the comparison, if
// version <= fVersion.
return compare <= 0 || compatibleVersion.Compare(fVersion) <= 0;
}
bool
BPackageResolvableExpression::Matches(const BPackageResolvable& provides) const
{
if (provides.Name() != fName)
return false;
return Matches(provides.Version(), provides.CompatibleVersion());
}
} // namespace BPackageKit