MyGUI  3.2.0
MyGUI_Version.h
Go to the documentation of this file.
1 
6 /*
7  This file is part of MyGUI.
8 
9  MyGUI is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  MyGUI is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #ifndef __MYGUI_VERSION_H__
23 #define __MYGUI_VERSION_H__
24 
25 #include "MyGUI_Prerequest.h"
26 #include "MyGUI_Types.h"
27 #include "MyGUI_StringUtility.h"
28 
29 namespace MyGUI
30 {
31 
33  {
34  public:
35 
36  Version(unsigned int _major = 0, unsigned int _minor = 0, unsigned int _patch = 0) :
37  mMajor(_major),
38  mMinor(_minor),
39  mPatch(_patch)
40  {
41  }
42 
43  friend bool operator < (Version const& a, Version const& b)
44  {
45  return (a.mMajor < b.mMajor) ? true : (a.mMinor < b.mMinor);
46  }
47 
48  friend bool operator >= (Version const& a, Version const& b)
49  {
50  return !(a < b);
51  }
52 
53  friend bool operator > (Version const& a, Version const& b)
54  {
55  return (b < a);
56  }
57 
58  friend bool operator <= (Version const& a, Version const& b)
59  {
60  return !(a > b);
61  }
62 
63  friend bool operator == (Version const& a, Version const& b)
64  {
65  return !(a < b) && !(a > b);
66  }
67 
68  friend bool operator != (Version const& a, Version const& b)
69  {
70  return !(a == b);
71  }
72 
73  friend std::ostream& operator << (std::ostream& _stream, const Version& _value)
74  {
75  _stream << _value.print();
76  return _stream;
77  }
78 
79  friend std::istream& operator >> (std::istream& _stream, Version& _value)
80  {
81  std::string value;
82  _stream >> value;
83  _value = parse(value);
84  return _stream;
85  }
86 
87  unsigned int getMajor() const
88  {
89  return mMajor;
90  }
91 
92  unsigned int getMinor() const
93  {
94  return mMinor;
95  }
96 
97  unsigned int getPatch() const
98  {
99  return mPatch;
100  }
101 
102  std::string print() const
103  {
104  if (mPatch == 0)
105  return utility::toString(mMajor, ".", mMinor);
106  return utility::toString(mMajor, ".", mMinor, ".", mPatch);
107  }
108 
109  static Version parse(const std::string& _value)
110  {
111  const std::vector<std::string>& vec = utility::split(_value, ".");
112  if (vec.empty())
113  return Version();
114 
115  unsigned int major = utility::parseValue<unsigned int>(vec[0]);
116  unsigned int minor = vec.size() > 1 ? utility::parseValue<unsigned int>(vec[1]) : 0;
117  unsigned int patch = vec.size() > 2 ? utility::parseValue<unsigned int>(vec[2]) : 0;
118 
119  return Version(major, minor, patch);
120  }
121 
122  private:
123  unsigned mMajor : 8;
124  unsigned mMinor : 8;
125  unsigned mPatch : 16;
126  };
127 
128 } // namespace MyGUI
129 
130 #endif // __MYGUI_VERSION_H__