MyGUI 3.5.3
MyGUI_Version.h
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#ifndef MYGUI_VERSION_H_
8#define MYGUI_VERSION_H_
9
10#include "MyGUI_Prerequest.h"
11#include "MyGUI_Types.h"
12#include "MyGUI_StringUtility.h"
13
14namespace MyGUI
15{
16
17 class MYGUI_EXPORT Version
18 {
19 public:
20 Version(uint8_t _major = 0, uint8_t _minor = 0, uint16_t _patch = 0) :
21 mMajor(_major),
22 mMinor(_minor),
23 mPatch(_patch)
24 {
25 }
26
27 friend bool operator<(Version const& a, Version const& b)
28 {
29 if (a.mMajor != b.mMajor)
30 return a.mMajor < b.mMajor;
31 if (a.mMinor != b.mMinor)
32 return a.mMinor < b.mMinor;
33 return a.mPatch < b.mPatch;
34 }
35
36 friend bool operator>=(Version const& a, Version const& b)
37 {
38 return !(a < b);
39 }
40
41 friend bool operator>(Version const& a, Version const& b)
42 {
43 return (b < a);
44 }
45
46 friend bool operator<=(Version const& a, Version const& b)
47 {
48 return !(a > b);
49 }
50
51 friend bool operator==(Version const& a, Version const& b)
52 {
53 return !(a < b) && !(a > b);
54 }
55
56 friend bool operator!=(Version const& a, Version const& b)
57 {
58 return !(a == b);
59 }
60
61 friend std::ostream& operator<<(std::ostream& _stream, const Version& _value)
62 {
63 _stream << _value.print();
64 return _stream;
65 }
66
67 friend std::istream& operator>>(std::istream& _stream, Version& _value)
68 {
69 std::string value;
70 _stream >> value;
71 _value = parse(value);
72 return _stream;
73 }
74
75 uint8_t getMajor() const
76 {
77 return mMajor;
78 }
79
80 uint8_t getMinor() const
81 {
82 return mMinor;
83 }
84
85 uint16_t getPatch() const
86 {
87 return mPatch;
88 }
89
90 std::string print() const
91 {
92 if (mPatch == 0)
93 return utility::toString(mMajor, ".", mMinor);
94 return utility::toString(mMajor, ".", mMinor, ".", mPatch);
95 }
96
97 static Version parse(std::string_view _value)
98 {
99 const std::vector<std::string>& vec = utility::split(_value, ".");
100 if (vec.empty())
101 return {};
102
103 uint8_t major = utility::parseValue<uint8_t>(vec[0]);
104 uint8_t minor = vec.size() > 1 ? utility::parseValue<uint8_t>(vec[1]) : 0;
105 uint16_t patch = vec.size() > 2 ? utility::parseValue<uint16_t>(vec[2]) : 0;
106
107 return {major, minor, patch};
108 }
109
110 private:
111 uint8_t mMajor;
112 uint8_t mMinor;
113 uint16_t mPatch;
114 };
115
116} // namespace MyGUI
117
118#endif // MYGUI_VERSION_H_
#define MYGUI_EXPORT