|
10 | 10 |
|
11 | 11 | /// |
12 | 12 | /// \file Version.h |
13 | | -/// \brief Report the version for this package. |
14 | | -/// \author bvonhall |
| 13 | +/// \author Barthelemy von Haller |
15 | 14 | /// |
16 | 15 |
|
17 | 16 | #ifndef QC_CORE_VERSION_H |
|
20 | 19 | #include <string> |
21 | 20 | #include <sstream> |
22 | 21 |
|
23 | | -namespace o2::quality_control::core |
24 | | -{ |
25 | | - |
26 | | -/// The current major version. |
27 | | -#define QUALITYCONTROL_VERSION_MAJOR @PROJECT_VERSION_MAJOR@ |
28 | | - |
29 | | -/// The current minor version. |
30 | | -#define QUALITYCONTROL_VERSION_MINOR @PROJECT_VERSION_MINOR@ |
31 | | - |
32 | | -/// The current patch level. |
33 | | -#define QUALITYCONTROL_VERSION_PATCH @PROJECT_VERSION_PATCH@ |
34 | | - |
35 | | -/// True if the current version is newer than the given one. |
36 | | -#define QUALITYCONTROL_VERSION_GT(MAJOR, MINOR, PATCH) \ |
37 | | - ((QUALITYCONTROL_VERSION_MAJOR > MAJOR) || \ |
38 | | - (QUALITYCONTROL_VERSION_MAJOR == \ |
39 | | - MAJOR&&(QUALITYCONTROL_VERSION_MINOR > MINOR || (QUALITYCONTROL_VERSION_MINOR == MINOR&& QUALITYCONTROL_VERSION_PATCH > PATCH)))) |
40 | | - |
41 | | -/// True if the current version is equal or newer to the given. |
42 | | -#define QUALITYCONTROL_VERSION_GE(MAJOR, MINOR, PATCH) \ |
43 | | - ((QUALITYCONTROL_VERSION_MAJOR > MAJOR) || \ |
44 | | - (QUALITYCONTROL_VERSION_MAJOR == \ |
45 | | - MAJOR&&(QUALITYCONTROL_VERSION_MINOR > MINOR || (QUALITYCONTROL_VERSION_MINOR == MINOR&& QUALITYCONTROL_VERSION_PATCH >= PATCH)))) |
46 | | - |
47 | | -/// True if the current version is older than the given one. |
48 | | -#define QUALITYCONTROL_VERSION_LT(MAJOR, MINOR, PATCH) \ |
49 | | - ((QUALITYCONTROL_VERSION_MAJOR < MAJOR) || \ |
50 | | - (QUALITYCONTROL_VERSION_MAJOR == \ |
51 | | - MAJOR&&(QUALITYCONTROL_VERSION_MINOR < MINOR || (QUALITYCONTROL_VERSION_MINOR == MINOR&& QUALITYCONTROL_VERSION_PATCH < PATCH)))) |
52 | | - |
53 | | -/// True if the current version is older or equal to the given. |
54 | | -#define QUALITYCONTROL_VERSION_LE(MAJOR, MINOR, PATCH) \ |
55 | | - ((QUALITYCONTROL_VERSION_MAJOR < MAJOR) || \ |
56 | | - (QUALITYCONTROL_VERSION_MAJOR == \ |
57 | | - MAJOR&&(QUALITYCONTROL_VERSION_MINOR < MINOR || (QUALITYCONTROL_VERSION_MINOR == MINOR&& QUALITYCONTROL_VERSION_PATCH <= PATCH)))) |
58 | | - |
59 | | -/// Information about the current QualityControl version. |
60 | | -class Version { |
61 | | -public: |
62 | | - /// @return the current major version of QualityControl. |
63 | | - static int getMajor() |
64 | | - { |
65 | | - return QUALITYCONTROL_VERSION_MAJOR; |
66 | | - } |
67 | | - |
68 | | - /// @return the current minor version of QualityControl. |
69 | | - static int getMinor() |
70 | | - { |
71 | | - return QUALITYCONTROL_VERSION_MINOR; |
72 | | - } |
73 | | - |
74 | | - /// @return the current patch level of QualityControl. |
75 | | - static int getPatch() |
76 | | - { |
77 | | - return QUALITYCONTROL_VERSION_PATCH; |
78 | | - } |
79 | | - |
80 | | - /// @return the current QualityControl version (MM.mm.pp). |
81 | | - static std::string getString() |
82 | | - { |
83 | | - std::ostringstream version; |
84 | | - version << QUALITYCONTROL_VERSION_MAJOR << '.' << QUALITYCONTROL_VERSION_MINOR << '.' << QUALITYCONTROL_VERSION_PATCH; |
85 | | - return version.str(); |
86 | | - } |
| 22 | +namespace o2::quality_control::core { |
87 | 23 |
|
| 24 | +/// Represents a software package version. |
| 25 | +/// Inspired from https://sourcey.com/articles/comparing-version-strings-in-cpp |
| 26 | +class Version |
| 27 | +{ |
| 28 | + public: |
| 29 | + |
| 30 | + /// Create a version from a string. |
| 31 | + /// @param version The version in the form X.Y.Z. If minor or patch is missing, it is replaced by 0. |
| 32 | + Version(std::string version) |
| 33 | + { |
| 34 | + std::sscanf(version.c_str(), "%d.%d.%d", &mMajor, &mMinor, &mPatch); |
| 35 | + } |
| 36 | + |
| 37 | + Version(int major, int minor, int patch): mMajor(major), mMinor(minor), mPatch(patch) |
| 38 | + { |
| 39 | + } |
| 40 | + |
| 41 | + ~Version() = default; |
| 42 | + |
| 43 | + /// \brief Returns the version of the QC framework. |
| 44 | + /// Returns the version of the QC framework as found in CMake. |
| 45 | + static Version& GetQcVersion() |
| 46 | + { |
| 47 | + // Guaranteed to be destroyed. Instantiated on first use |
| 48 | + static Version qcVersion{@PROJECT_VERSION_MAJOR@, @PROJECT_VERSION_MINOR@, @PROJECT_VERSION_PATCH@}; |
| 49 | + return qcVersion; |
| 50 | + } |
| 51 | + |
| 52 | + int getMajor() const |
| 53 | + { |
| 54 | + return mMajor; |
| 55 | + } |
| 56 | + |
| 57 | + int getMinor() const |
| 58 | + { |
| 59 | + return mMinor; |
| 60 | + } |
| 61 | + |
| 62 | + int getPatch() const |
| 63 | + { |
| 64 | + return mPatch; |
| 65 | + } |
| 66 | + |
| 67 | + bool operator<(const Version &other) |
| 68 | + { |
| 69 | + if (getMajor() < other.getMajor()) { |
| 70 | + return true; |
| 71 | + } |
| 72 | + if (getMinor() < other.getMinor()) { |
| 73 | + return true; |
| 74 | + } |
| 75 | + if (getPatch() < other.getPatch()) { |
| 76 | + return true; |
| 77 | + } |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + bool operator==(const Version &other) |
| 82 | + { |
| 83 | + return getMajor() == other.getMajor() |
| 84 | + && getMinor() == other.getMinor() |
| 85 | + && getPatch() == other.getPatch(); |
| 86 | + } |
| 87 | + |
| 88 | + friend std::ostream &operator<<(std::ostream &stream, const Version &ver) |
| 89 | + { |
| 90 | + stream << ver.getMajor() << '.' << ver.getMinor() << '.' << ver.getPatch(); |
| 91 | + return stream; |
| 92 | + } |
| 93 | + |
| 94 | + std::string getString() |
| 95 | + { |
| 96 | + std::ostringstream version; |
| 97 | + version << this; |
| 98 | + return version.str(); |
| 99 | + } |
| 100 | + |
| 101 | + private: |
| 102 | + int mMajor = 0, mMinor = 0, mPatch = 0; |
88 | 103 | }; |
89 | 104 |
|
90 | 105 | } // namespace o2::quality_control::core |
|
0 commit comments