• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef META_BASE_VERSION_H
16 #define META_BASE_VERSION_H
17 
18 #include <base/containers/string.h>
19 #include <base/util/uid_util.h>
20 
21 #include <meta/base/namespace.h>
22 
META_BEGIN_NAMESPACE()23 META_BEGIN_NAMESPACE()
24 
25 /// Simple version class which supports major and minor version number
26 class Version {
27 public:
28     /// Construct Versions from major and minor versions
29     constexpr Version(uint16_t major = 0, uint16_t minor = 0) : major_(major), minor_(minor) {}
30 
31     /// Construct Version from string
32     constexpr explicit Version(BASE_NS::string_view str)
33     {
34         size_t pos = ParseInt(str, major_);
35         if (pos != 0 && pos + 1 < str.size()) {
36             ParseInt(str.substr(pos + 1), minor_);
37         }
38     }
39 
40     /// Get major version
41     constexpr uint16_t Major() const
42     {
43         return major_;
44     }
45     /// Get minor version
46     constexpr uint16_t Minor() const
47     {
48         return minor_;
49     }
50     /// Get version as string
51     BASE_NS::string ToString() const
52     {
53         return BASE_NS::string(BASE_NS::to_string(major_)) + "." + BASE_NS::string(BASE_NS::to_string(minor_));
54     }
55 
56 private:
57     constexpr size_t ParseInt(BASE_NS::string_view str, uint16_t& out)
58     {
59         size_t i = 0;
60         while (i < str.size() && str[i] >= '0' && str[i] <= '9') {
61             if (i) {
62                 out *= 10;
63             }
64             out += str[i++] - '0';
65         }
66         return i;
67     }
68 
69 private:
70     uint16_t major_ {};
71     uint16_t minor_ {};
72 };
73 
74 constexpr inline bool operator==(const Version& l, const Version& r)
75 {
76     return l.Major() == r.Major() && l.Minor() == r.Minor();
77 }
78 
79 constexpr inline bool operator!=(const Version& l, const Version& r)
80 {
81     return !(l == r);
82 }
83 
84 constexpr inline bool operator<(const Version& l, const Version& r)
85 {
86     return l.Major() < r.Major() || (l.Major() == r.Major() && l.Minor() < r.Minor());
87 }
88 
89 constexpr inline bool operator<=(const Version& l, const Version& r)
90 {
91     return l == r || l < r;
92 }
93 
94 constexpr inline bool operator>(const Version& l, const Version& r)
95 {
96     return r < l;
97 }
98 
99 constexpr inline bool operator>=(const Version& l, const Version& r)
100 {
101     return r <= l;
102 }
103 
104 META_END_NAMESPACE()
105 
106 #endif
107