Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
usModuleVersion.cpp
Go to the documentation of this file.
1 /*=============================================================================
2 
3  Library: CppMicroServices
4 
5  Copyright (c) German Cancer Research Center,
6  Division of Medical and Biological Informatics
7 
8  Licensed under the Apache License, Version 2.0 (the "License");
9  you may not use this file except in compliance with the License.
10  You may obtain a copy of the License at
11 
12  http://www.apache.org/licenses/LICENSE-2.0
13 
14  Unless required by applicable law or agreed to in writing, software
15  distributed under the License is distributed on an "AS IS" BASIS,
16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  See the License for the specific language governing permissions and
18  limitations under the License.
19 
20 =============================================================================*/
21 
22 #include "usModuleVersion.h"
23 
24 #include <vector>
25 #include <sstream>
26 #include <stdexcept>
27 #include <algorithm>
28 #include <cctype>
29 
30 US_BEGIN_NAMESPACE
31 
32 const char ModuleVersion::SEPARATOR = '.';
33 
34 bool IsInvalidQualifier(char c)
35 {
36  return !(std::isalnum(c) || c == '_' || c == '-');
37 }
38 
39 ModuleVersion ModuleVersion::EmptyVersion()
40 {
41  static ModuleVersion emptyV(false);
42  return emptyV;
43 }
44 
45 ModuleVersion ModuleVersion::UndefinedVersion()
46 {
47  static ModuleVersion undefinedV(true);
48  return undefinedV;
49 }
50 
51 ModuleVersion& ModuleVersion::operator=(const ModuleVersion& v)
52 {
53  majorVersion = v.majorVersion;
54  minorVersion = v.minorVersion;
55  microVersion = v.microVersion;
56  qualifier = v.qualifier;
57  undefined = v.undefined;
58  return *this;
59 }
60 
61 ModuleVersion::ModuleVersion(bool undefined)
62  : majorVersion(0), minorVersion(0), microVersion(0), qualifier(""), undefined(undefined)
63 {
64 
65 }
66 
67 void ModuleVersion::Validate()
68 {
69  if (std::find_if(qualifier.begin(), qualifier.end(), IsInvalidQualifier) != qualifier.end())
70  throw std::invalid_argument(std::string("invalid qualifier: ") + qualifier);
71 
72  undefined = false;
73 }
74 
75 ModuleVersion::ModuleVersion(unsigned int majorVersion, unsigned int minorVersion, unsigned int microVersion)
76  : majorVersion(majorVersion), minorVersion(minorVersion), microVersion(microVersion), qualifier(""),
77  undefined(false)
78 {
79 
80 }
81 
82 ModuleVersion::ModuleVersion(unsigned int majorVersion, unsigned int minorVersion, unsigned int microVersion, const std::string& qualifier)
83  : majorVersion(majorVersion), minorVersion(minorVersion), microVersion(microVersion), qualifier(qualifier),
84  undefined(true)
85 {
86  this->Validate();
87 }
88 
89 ModuleVersion::ModuleVersion(const std::string& version)
90  : majorVersion(0), minorVersion(0), microVersion(0), undefined(true)
91 {
92  unsigned int maj = 0;
93  unsigned int min = 0;
94  unsigned int mic = 0;
95  std::string qual("");
96 
97  std::vector<std::string> st;
98  std::stringstream ss(version);
99  std::string token;
100  while(std::getline(ss, token, SEPARATOR))
101  {
102  st.push_back(token);
103  }
104 
105  if (st.empty()) return;
106 
107  bool ok = true;
108  ss.clear();
109  ss.str(st[0]);
110  ss >> maj;
111  ok = !ss.fail();
112 
113  if (st.size() > 1)
114  {
115  ss.clear();
116  ss.str(st[1]);
117  ss >> min;
118  ok = !ss.fail();
119  if (st.size() > 2)
120  {
121  ss.clear();
122  ss.str(st[2]);
123  ss >> mic;
124  ok = !ss.fail();
125  if (st.size() > 3)
126  {
127  qual = st[3];
128  if (st.size() > 4)
129  {
130  ok = false;
131  }
132  }
133  }
134  }
135 
136  if (!ok) throw std::invalid_argument("invalid format");
137 
138  majorVersion = maj;
139  minorVersion = min;
140  microVersion = mic;
141  qualifier = qual;
142  this->Validate();
143 }
144 
145 ModuleVersion::ModuleVersion(const ModuleVersion& version)
146 : majorVersion(version.majorVersion), minorVersion(version.minorVersion),
147  microVersion(version.microVersion), qualifier(version.qualifier),
148  undefined(version.undefined)
149 {
150 
151 }
152 
153 ModuleVersion ModuleVersion::ParseVersion(const std::string& version)
154 {
155  if (version.empty())
156  {
157  return EmptyVersion();
158  }
159 
160  std::string version2(version);
161  version2.erase(0, version2.find_first_not_of(' '));
162  version2.erase(version2.find_last_not_of(' ')+1);
163  if (version2.empty())
164  {
165  return EmptyVersion();
166  }
167 
168  return ModuleVersion(version2);
169 }
170 
172 {
173  return undefined;
174 }
175 
176 unsigned int ModuleVersion::GetMajor() const
177 {
178  if (undefined) throw std::logic_error("Version undefined");
179  return majorVersion;
180 }
181 
182 unsigned int ModuleVersion::GetMinor() const
183 {
184  if (undefined) throw std::logic_error("Version undefined");
185  return minorVersion;
186 }
187 
188 unsigned int ModuleVersion::GetMicro() const
189 {
190  if (undefined) throw std::logic_error("Version undefined");
191  return microVersion;
192 }
193 
194 std::string ModuleVersion::GetQualifier() const
195 {
196  if (undefined) throw std::logic_error("Version undefined");
197  return qualifier;
198 }
199 
200 std::string ModuleVersion::ToString() const
201 {
202  if (undefined) return "undefined";
203 
204  std::stringstream ss;
205  ss << majorVersion << SEPARATOR << minorVersion << SEPARATOR << microVersion;
206  if (!qualifier.empty())
207  {
208  ss << SEPARATOR << qualifier;
209  }
210  return ss.str();
211 }
212 
213 bool ModuleVersion::operator==(const ModuleVersion& other) const
214 {
215  if (&other == this)
216  { // quicktest
217  return true;
218  }
219 
220  if (other.undefined && this->undefined) return true;
221  if (this->undefined) throw std::logic_error("Version undefined");
222  if (other.undefined) return false;
223 
224  return (majorVersion == other.majorVersion) && (minorVersion == other.minorVersion) && (microVersion
225  == other.microVersion) && qualifier == other.qualifier;
226 }
227 
228 int ModuleVersion::Compare(const ModuleVersion& other) const
229 {
230  if (&other == this)
231  { // quicktest
232  return 0;
233  }
234 
235  if (this->undefined || other.undefined)
236  throw std::logic_error("Cannot compare undefined version");
237 
238  if (majorVersion < other.majorVersion)
239  {
240  return -1;
241  }
242 
243  if (majorVersion == other.majorVersion)
244  {
245 
246  if (minorVersion < other.minorVersion)
247  {
248  return -1;
249  }
250 
251  if (minorVersion == other.minorVersion)
252  {
253 
254  if (microVersion < other.microVersion)
255  {
256  return -1;
257  }
258 
259  if (microVersion == other.microVersion)
260  {
261  return qualifier.compare(other.qualifier);
262  }
263  }
264  }
265  return 1;
266 }
267 
268 US_END_NAMESPACE
269 
270 US_USE_NAMESPACE
271 
272 std::ostream& operator<<(std::ostream& os, const ModuleVersion& v)
273 {
274  return os << v.ToString();
275 }
int Compare(const ModuleVersion &object) const
unsigned int GetMicro() const
bool operator==(const ModuleVersion &object) const
unsigned int GetMinor() const
US_Core_EXPORT std::ostream & operator<<(std::ostream &os, ModuleEvent::Type eventType)
bool IsUndefined() const
static T min(T x, T y)
Definition: svm.cpp:67
bool IsInvalidQualifier(char c)
std::string ToString() const
std::string GetQualifier() const
static ModuleVersion EmptyVersion()
unsigned int GetMajor() const
static ModuleVersion ParseVersion(const std::string &version)