1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#pragma once
#include <string>
#include <regex>
#include "string/convert.h"
namespace settings
{
// Represents an application version tag with Major and Minor numbers
class MajorMinorVersion
{
private:
int _majorVersion;
int _minorVersion;
public:
// Construct the major/minor version from the given version string,
// as used in the RADIANT_VERSION pattern: "major.minor.micro[suffix]"
// Throws std::invalid_argument in case of parsing failures
MajorMinorVersion(const std::string& versionString)
{
// Major/minor version is mandatory, micro version and suffix are optional
const std::regex VersionPattern("(\\d+)\\.(\\d+)(\\.\\d+[\\w\\d_]*)?");
// Extract the version from the given string
std::smatch match;
if (!std::regex_match(versionString, match, VersionPattern))
{
throw std::invalid_argument("The input string " + versionString + " failed to parse");
}
_majorVersion = string::convert<int>(match[1].str());
_minorVersion = string::convert<int>(match[2].str());
}
int getMajorVersion() const
{
return _majorVersion;
}
int getMinorVersion() const
{
return _minorVersion;
}
// Compare this version to the other one, returning true if this is instance is smaller
bool operator<(const MajorMinorVersion& other) const
{
if (_majorVersion < other._majorVersion)
{
return true;
}
// If major version matches, minor version decides
if (_majorVersion == other._majorVersion)
{
return _minorVersion < other._minorVersion;
}
// Major version is larger
return false;
}
std::string toString() const
{
return string::to_string(_majorVersion) + "." + string::to_string(_minorVersion);
}
};
}
|