fw4spl
fwRuntime/src/Version.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwRuntime/Version.hpp"
8 
9 #include "fwRuntime/RuntimeException.hpp"
10 
11 #include <boost/lexical_cast.hpp>
12 #include <boost/regex.hpp>
13 
14 #include <cassert>
15 #include <sstream>
16 
17 namespace fwRuntime
18 {
19 
21  m_major(-1),
22  m_minor(-1),
23  m_patch(-1),
24  m_defined(false)
25 {
26 }
27 
28 //------------------------------------------------------------------------------
29 
30 Version::Version(const std::string& version) :
31  m_major(-1),
32  m_minor(-1),
33  m_patch(-1),
34  m_defined(false)
35 {
36  if( version.empty() == false )
37  {
38  ::boost::regex regex("(\\d+)([-.](\\d+)){0,1}([-.](\\d+)){0,1}");
39  ::boost::smatch what;
40  if( ::boost::regex_match(version, what, regex) )
41  {
42  const std::string& major = what[1].str();
43  const std::string& minor = what[3].str();
44  const std::string& patch = what[5].str();
45 
46  assert(!major.empty());
47  if(!major.empty())
48  {
49  m_major = ::boost::lexical_cast<int>( major );
50  }
51 
52  assert(!minor.empty());
53  if(!minor.empty())
54  {
55  m_minor = ::boost::lexical_cast<int>( minor );
56  }
57 
58  if(!patch.empty())
59  {
60  m_patch = ::boost::lexical_cast<int>( patch );
61  }
62 
63  m_defined = true;
64  }
65  else
66  {
67  throw RuntimeException(version + " : invalid version.");
68  }
69  }
70 }
71 
72 //------------------------------------------------------------------------------
73 
74 Version::Version(const int major, const int minor, const int patch) :
75  m_major(major),
76  m_minor(minor),
77  m_patch(patch),
78  m_defined(true)
79 {
80 }
81 
82 //------------------------------------------------------------------------------
83 
84 const std::string Version::string() const
85 {
86  std::ostringstream os;
87  os << *this;
88  return os.str();
89 }
90 
91 //------------------------------------------------------------------------------
92 
93 bool Version::operator==(const Version& version) const
94 {
95  if( m_defined && version.m_defined )
96  {
97  return m_major == version.m_major && m_minor == version.m_minor && m_patch == version.m_patch;
98  }
99  else
100  {
101  return true;
102  }
103 }
104 
105 //------------------------------------------------------------------------------
106 
107 std::ostream& operator<<(std::ostream& os, const Version& version)
108 {
109  if(version.m_defined)
110  {
111  auto& res = os << version.m_major << "." << version.m_minor;
112 
113  if(version.m_patch != -1)
114  {
115  res << "-" << version.m_patch;
116  }
117 
118  return res;
119  }
120  else
121  {
122  return os << "version-not-defined";
123  }
124 }
125 
126 } // namespace fwRuntime
FWRUNTIME_API Version()
Constructor.
Defines the runtime exception class.
The namespace fwRuntime contains classes to manage bundle, configuration element, extension point in ...
FWRUNTIME_API friend std::ostream & operator<<(std::ostream &os, const Version &version)
Return an ostream representation of a version.
FWRUNTIME_API bool operator==(const Version &version) const
Equality test.
Holds version information for libraries and bundles.
FWRUNTIME_API const std::string string() const
String converter.