1818
1919#include <string>
2020#include <sstream>
21+ #include <iostream>
22+
23+ using namespace std ;
2124
2225namespace o2 ::quality_control ::core {
2326
2427/// Represents a software package version.
25- /// Inspired from https://sourcey.com/articles/comparing-version-strings-in-cpp
28+ /// Version numbers can go up to 999.
2629class Version
2730{
2831 public :
@@ -31,10 +34,10 @@ class Version
3134 /// @param version The version in the form X.Y.Z. If minor or patch is missing, it is replaced by 0.
3235 Version (std ::string version )
3336 {
34- std ::sscanf (version .c_str (), "%d.%d.%d " , & mMajor , & mMinor , & mPatch );
37+ std ::sscanf (version .c_str (), "%u.%u.%u " , & mMajor , & mMinor , & mPatch );
3538 }
3639
37- Version (int major , int minor , int patch ): mMajor (major ), mMinor (minor ), mPatch (patch )
40+ Version (unsigned int major , unsigned int minor , unsigned int patch ): mMajor (major ), mMinor (minor ), mPatch (patch )
3841 {
3942 }
4043
@@ -49,57 +52,77 @@ class Version
4952 return qcVersion ;
5053 }
5154
52- int getMajor () const
55+ unsigned int getMajor () const
5356 {
5457 return mMajor ;
5558 }
5659
57- int getMinor () const
60+ unsigned int getMinor () const
5861 {
5962 return mMinor ;
6063 }
6164
62- int getPatch () const
65+ unsigned int getPatch () const
6366 {
6467 return mPatch ;
6568 }
6669
6770 bool operator < (const Version & other )
6871 {
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;
72+ return getIntegerRepresentation () < other .getIntegerRepresentation ();
73+ }
74+
75+ bool operator >=(const Version & other )
76+ {
77+ return getIntegerRepresentation () >= other .getIntegerRepresentation ();
78+ }
79+
80+ bool operator > (const Version & other )
81+ {
82+ return getIntegerRepresentation () > other .getIntegerRepresentation ();
83+ }
84+
85+ bool operator <=(const Version & other )
86+ {
87+ return getIntegerRepresentation () <= other .getIntegerRepresentation ();
7988 }
8089
8190 bool operator == (const Version & other )
8291 {
83- return getMajor () == other .getMajor ()
84- && getMinor () == other .getMinor ()
85- && getPatch () == other .getPatch ();
92+ return getIntegerRepresentation () == other .getIntegerRepresentation ();
93+ }
94+
95+ bool operator != (const Version & other )
96+ {
97+ return getIntegerRepresentation () != other .getIntegerRepresentation ();
8698 }
8799
88100 friend std ::ostream & operator <<(std ::ostream & stream , const Version & ver )
89101 {
90- stream << ver .getMajor () << '.' << ver . getMinor () << '.' << ver . getPatch ();
102+ stream << ver .getString ();
91103 return stream ;
92104 }
93105
94- std ::string getString ()
106+ std ::string getString () const
95107 {
96- std ::ostringstream version ;
97- version << this ;
98- return version .str ();
108+ std ::stringstream result ;
109+ result << getMajor () << '.' << getMinor () << '.' << getPatch ();
110+ return result .str ();
111+ }
112+
113+ unsigned int getIntegerRepresentation () const
114+ {
115+ return getMajor () * (getMaxVersion () * getMaxVersion ()) + getMinor () * getMaxVersion () + getPatch () * 1 ;
116+ }
117+
118+ unsigned int getMaxVersion () const {
119+ return mMaxVersion ;
99120 }
100121
101122 private :
102- int mMajor = 0 , mMinor = 0 , mPatch = 0 ;
123+ unsigned int mMajor = 0 , mMinor = 0 , mPatch = 0 ;
124+ unsigned int mMaxVersion = 1000 ;
125+
103126};
104127
105128} // namespace o2::quality_control::core
0 commit comments