[Development] Adding support for version number comparisons

Thiago Macieira thiago.macieira at intel.com
Sat May 31 21:00:45 CEST 2014


Em sex 09 maio 2014, às 11:36:08, Keith Gardner escreveu:
> I have been working on adding a class to QtCore (QVersion) to support
> storing version numbers, convert to/from QString, and having comparison
> operators.  My goal was to provide an API to assist in the following use
> cases:
> 
>    - Plugin loading where there are multiple versions on the same system.
>    - File format validation.
>    - Executing an already installed command line application where the
>    behavior is dependent on the called application's version.
>    - Performing software installations and updates.
>    - QMake support for version number comparisons.
> 
> Please share other potential use cases where this might be useful.

Ok, since we can't seem to agree, let's settle on the maximum common 
denominator: QVersion will only compare the numeric part of a version number. 
By default, two versions with the same numeric part will compare equally, 
regardless of any suffixes that may be present.

Therefore, by default, given:
	QVersion a("5.3.0");
	QVersion b("5.3.0pl1");
	QVersion c("5.3.0beta1");

	a != b != c;		// since they aren't equal

	QVersion::compare(a, b) == 0;
	QVersion::compare(b, c) == 0;

We will also provide an overload for the QVersion::compare function taking a 
functor:

	template <typename SuffixCompare> static inline int
	compare(const QVersion &a, const QVersion &b, SuffixCompare suffixCompare)
	{
		int cmp = compare(a, b);
		if (cmp)
			return cmp;
		return suffixCompare(a.suffix(), b.suffix());
	}

Thus, if you want to implement a comparison like OpenSSL's, you can do:

	QVersion::compare(a, b, 
	    [](auto a, auto b) { 
		return QString::compare(a, b, Qt::CaseInsensitive); 
	    });

If you want a more complex comparison like I had wanted, you'd do:

static int priorityOrder(const QString &suffix)
{
	if (suffix.isEmpty())
		return 0;
	return suffix.startsWith("alpha") || suffix.startsWith("beta") ||
		suffix.startsWith("rc") ? -1 : 1;
}

static int myCompare(const QString &a, const QString &b)
{
	int a_prio = priorityOrder(a);
	int b_prio = priorityOrder(b);
	if (a_prio != b_prio || a_prio == 0)
		return a_prio - b_prio;
	return QString::compare(a, b, Qt::CaseInsensitive);
}

And then you'd use: QVersion::compare(a, b, myCompare);
-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center




More information about the Development mailing list