MyGUI  3.0.1
MyGUI_Version.h
Go to the documentation of this file.
1 
7 /*
8  This file is part of MyGUI.
9 
10  MyGUI is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  MyGUI is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #ifndef __MYGUI_VERSION_H__
24 #define __MYGUI_VERSION_H__
25 
26 #include "MyGUI_Prerequest.h"
27 #include "MyGUI_Types.h"
28 #include "MyGUI_Utility.h"
29 
30 namespace MyGUI
31 {
32 
34  {
35  public:
36  Version() : value(0) { }
37  Version(uint8 _major, uint8 _minor, uint16 _patch) : value((uint32(_major) << 24) + (uint32(_minor) << 16) + uint32(_patch)) { }
38  Version(uint8 _major, uint8 _minor) : value((uint32(_major) << 24) + (uint32(_minor) << 16)) { }
39  explicit Version(const std::string& _value) : value(parse(_value).value) { }
40 
41  friend bool operator < (Version const& a, Version const& b) { return a.getPoorVersion() < b.getPoorVersion(); }
42  friend bool operator >= (Version const& a, Version const& b) { return !(a < b); }
43  friend bool operator > (Version const& a, Version const& b) { return (b < a); }
44  friend bool operator <= (Version const& a, Version const& b) { return !(a > b); }
45 
46  friend bool operator == (Version const& a, Version const& b) { return !(a < b) && !(a > b); }
47  friend bool operator != (Version const& a, Version const& b) { return !(a == b); }
48 
49  friend std::ostream& operator << ( std::ostream& _stream, const Version& _value )
50  {
51  _stream << _value.print();
52  return _stream;
53  }
54 
55  friend std::istream& operator >> ( std::istream& _stream, Version& _value )
56  {
57  std::string value;
58  _stream >> value;
59  _value = Version::parse(value);
60  return _stream;
61  }
62 
63  uint8 getMajor() const { return uint8((value & 0xFF000000) >> 24); }
64  uint8 getMinor() const { return uint8((value & 0x00FF0000) >> 16); }
65  uint16 getPatch() const { return uint16(value & 0x0000FFFF); }
66 
67  uint32 getPoorVersion() const { return value & 0xFFFF0000; }
68  uint32 getFullVersion() const { return value; }
69 
70  std::string print() const
71  {
72  if (getPatch() == 0) return utility::toString(getMajor(), ".", getMinor());
73  return utility::toString(getMajor(), ".", getMinor(), ".", getPatch());
74  }
75 
76  static Version parse(const std::string& _value)
77  {
78  const std::vector<std::string>& vec = utility::split(_value, ".");
79  if (vec.empty()) return Version();
80  uint8 major = (uint8)utility::parseValue<int>(vec[0]);
81  uint8 minor = vec.size() > 1 ? (uint8)utility::parseValue<int>(vec[1]) : uint8(0);
82  uint16 patch = vec.size() > 2 ? (uint16)utility::parseValue<int>(vec[2]) : uint16(0);
83  return Version(major, minor, patch);
84  }
85 
86  private:
87  union
88  {
90  uint8 value_data[4];
91  };
92  };
93 
94 } // namespace MyGUI
95 
96 #endif // __MYGUI_VERSION_H__