Version.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2015 Imperial College London
5  * Copyright 2013-2015 Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef MIRTK_Version_H
21 #define MIRTK_Version_H
22 
23 #include "mirtk/CommonExport.h"
24 #include "mirtk/VersionInfo.h"
25 
26 #include "mirtk/Object.h"
27 
28 
29 namespace mirtk {
30 
31 
32 // -----------------------------------------------------------------------------
33 /// Software version object
34 class Version : public Object
35 {
36  mirtkObjectMacro(Version);
37 
38  mirtkPublicAttributeMacro(unsigned int, Major); ///< Major version number
39  mirtkPublicAttributeMacro(unsigned int, Minor); ///< Minor version number
40  mirtkPublicAttributeMacro(unsigned int, Patch); ///< Patch number
41 
42 public:
43 
44  /// Constructor
45  Version(unsigned int major = 0u,
46  unsigned int minor = 0u,
47  unsigned int patch = 0u);
48 
49  /// Constructor
50  Version(int major, int minor = 0, int patch = 0);
51 
52  /// Constructor
53  Version(const char *);
54 
55  /// Copy constructor
56  Version(const Version &);
57 
58  /// Assignment operator
59  Version &operator =(const Version &);
60 
61  /// Whether this version is valid
62  operator bool() const;
63 
64  /// Equality operator
65  bool operator ==(const Version &) const;
66 
67  /// Inequality operator
68  bool operator !=(const Version &) const;
69 
70  /// Less operator
71  bool operator <(const Version &) const;
72 
73  /// Greater operator
74  bool operator >(const Version &) const;
75 
76  /// Less or equal operator
77  bool operator <=(const Version &) const;
78 
79  /// Greater or equal operator
80  bool operator >=(const Version &) const;
81 
82  /// Get version information as string
83  string ToString() const;
84 
85 };
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 // Global variables
89 ////////////////////////////////////////////////////////////////////////////////
90 
91 /// Current software version
92 const Version current_version(MIRTK_VERSION_MAJOR,
93  MIRTK_VERSION_MINOR,
94  MIRTK_VERSION_PATCH);
95 
96 /// Version to emulate
97 MIRTK_Common_EXPORT extern Version version;
98 
99 /// Print software revision number (or version if not available) only
100 void PrintRevision(ostream &);
101 
102 /// Print build time stamp as version string
103 void PrintVersion(ostream &, const char* = NULL);
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 // Inline definitions
107 ////////////////////////////////////////////////////////////////////////////////
108 
109 // -----------------------------------------------------------------------------
110 /// Write version to output stream
111 inline ostream &operator <<(ostream &os, const Version &version)
112 {
113  os << version.Major() << "." << version.Minor();
114  if (version.Patch() != 0u) os << "." << version.Patch();
115  return os;
116 }
117 
118 // -----------------------------------------------------------------------------
119 /// Read version from input stream
120 inline istream &operator >>(istream &is, Version &version)
121 {
122  string token;
123  char c;
124  while (is.get(c) && (isdigit(c) || c == '.')) token.push_back(c);
125  if (is.bad()) return is;
126  if (is.eof()) is.clear(ios::eofbit); // reset failbit
127  istringstream ss(token);
128  unsigned int number[3] = {0u, 0u, 0u};
129  int i = -1;
130  while (getline(ss, token, '.')) {
131  if (++i == 3 || !FromString(token.c_str(), number[i])) {
132  i = -1;
133  break;
134  }
135  }
136  if (i == -1) is.setstate(ios::failbit);
137  else {
138  version.Major(number[0]);
139  version.Minor(number[1]);
140  version.Patch(number[2]);
141  }
142  return is;
143 }
144 
145 // -----------------------------------------------------------------------------
146 inline string Version::ToString() const
147 {
148  ostringstream ss;
149  ss << (*this);
150  return ss.str();
151 }
152 
153 // -----------------------------------------------------------------------------
154 inline Version::Version(unsigned int major,
155  unsigned int minor,
156  unsigned int patch)
157 :
158  _Major(major), _Minor(minor), _Patch(patch)
159 {
160 }
161 
162 // -----------------------------------------------------------------------------
163 inline Version::Version(int major, int minor, int patch)
164 :
165  _Major(major >= 0 ? static_cast<unsigned int>(major) : 0u),
166  _Minor(minor >= 0 ? static_cast<unsigned int>(minor) : 0u),
167  _Patch(patch >= 0 ? static_cast<unsigned int>(patch) : 0u)
168 {
169 }
170 
171 // -----------------------------------------------------------------------------
172 inline Version::Version(const char *str)
173 :
174  _Major(0u), _Minor(0u), _Patch(0u)
175 {
176  FromString(str, *this);
177 }
178 
179 // -----------------------------------------------------------------------------
180 inline Version::Version(const Version &other)
181 :
182  _Major(other._Major), _Minor(other._Minor), _Patch(other._Patch)
183 {
184 }
185 
186 // -----------------------------------------------------------------------------
187 inline Version &Version::operator =(const Version &other)
188 {
189  if (this != &other) {
190  _Major = other._Major;
191  _Minor = other._Minor;
192  _Patch = other._Patch;
193  }
194  return *this;
195 }
196 
197 // -----------------------------------------------------------------------------
198 inline Version::operator bool() const
199 {
200  return (_Major != 0u || _Minor != 0u || _Patch != 0u);
201 }
202 
203 // -----------------------------------------------------------------------------
204 inline bool Version::operator ==(const Version &rhs) const
205 {
206  return _Major == rhs._Major && _Minor == rhs._Minor && _Patch == rhs._Patch;
207 }
208 
209 // -----------------------------------------------------------------------------
210 inline bool Version::operator !=(const Version &rhs) const
211 {
212  return !(*this == rhs);
213 }
214 
215 // -----------------------------------------------------------------------------
216 // Note: Invalid/development branch version 0.0.0 is greater than any other
217 inline bool Version::operator <(const Version &rhs) const
218 {
219  return bool(*this) && (!rhs || (_Major < rhs._Major || (_Major == rhs._Major && (_Minor < rhs._Minor || (_Minor == rhs._Minor && _Patch < rhs._Patch)))));
220 }
221 
222 // -----------------------------------------------------------------------------
223 inline bool Version::operator <=(const Version &rhs) const
224 {
225  return (*this == rhs) || (*this < rhs);
226 }
227 
228 // -----------------------------------------------------------------------------
229 // Note: Invalid/development branch version 0.0.0 is greater than any other
230 inline bool Version::operator >(const Version &rhs) const
231 {
232  return !(*this <= rhs);
233 }
234 
235 // -----------------------------------------------------------------------------
236 inline bool Version::operator >=(const Version &rhs) const
237 {
238  return (*this == rhs) || (*this > rhs);
239 }
240 
241 
242 } // namespace mirtk
243 
244 #endif // MIRTK_Version_H
bool operator==(const Version &) const
Equality operator.
Definition: Version.h:204
Software version object.
Definition: Version.h:34
bool operator>=(const Version &) const
Greater or equal operator.
Definition: Version.h:236
bool operator<(const Version &) const
Less operator.
Definition: Version.h:217
Version(unsigned int major=0u, unsigned int minor=0u, unsigned int patch=0u)
Constructor.
Definition: Version.h:154
MIRTK_Common_EXPORT Version version
Version to emulate.
bool operator!=(const Version &) const
Inequality operator.
Definition: Version.h:210
Definition: IOConfig.h:41
bool operator<=(const Version &) const
Less or equal operator.
Definition: Version.h:223
void PrintVersion(ostream &, const char *=NULL)
Print build time stamp as version string.
bool FromString(const char *str, EnergyMeasure &value)
Convert energy measure string to enumeration value.
void PrintRevision(ostream &)
Print software revision number (or version if not available) only.
Version & operator=(const Version &)
Assignment operator.
Definition: Version.h:187
const Version current_version(MIRTK_VERSION_MAJOR, MIRTK_VERSION_MINOR, MIRTK_VERSION_PATCH)
Current software version.
bool operator>(const Version &) const
Greater operator.
Definition: Version.h:230
string ToString() const
Get version information as string.
Definition: Version.h:146