Debian Codegraph @

libs/settings/MajorMinorVersion.h

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