1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifndef ANDROID_VINTF_VERSION_H
19 #define ANDROID_VINTF_VERSION_H
20 
21 #include <stdint.h>
22 #include <string>
23 #include <utility>
24 
25 namespace android {
26 namespace vintf {
27 
28 struct Version {
29 
30     constexpr Version() : Version(0u, 0u) {}
31     constexpr Version(size_t mj, size_t mi) : majorVer(mj), minorVer(mi) {}
32     constexpr Version(const std::pair<size_t, size_t>& pair)
33         : majorVer(pair.first), minorVer(pair.second) {}
34 
35     size_t majorVer;
36     size_t minorVer;
37 
38     inline bool operator==(const Version &other) const {
39         return majorVer == other.majorVer && minorVer == other.minorVer;
40     }
41     inline bool operator!=(const Version &other) const {
42         return !((*this) == other);
43     }
44     inline bool operator<(const Version &other) const {
45         if (majorVer < other.majorVer)
46             return true;
47         if (majorVer > other.majorVer)
48             return false;
49         return minorVer < other.minorVer;
50     }
51     inline bool operator>(const Version &other) const {
52         return other < (*this);
53     }
54     inline bool operator<=(const Version &other) const {
55         return !((*this) > other);
56     }
57     inline bool operator>=(const Version &other) const {
58         return !((*this) < other);
59     }
60     // Version(2, 1).minorAtLeast(Version(1, 0)) == false
61     // Version(2, 1).minorAtLeast(Version(2, 0)) == true
62     // Version(2, 1).minorAtLeast(Version(2, 1)) == true
63     // Version(2, 1).minorAtLeast(Version(2, 2)) == false
64     inline bool minorAtLeast(const Version& other) const {
65         return majorVer == other.majorVer && minorVer >= other.minorVer;
66     }
67 };
68 
69 struct KernelVersion {
70 
71     constexpr KernelVersion() : KernelVersion(0u, 0u, 0u) {}
72     constexpr KernelVersion(size_t v, size_t mj, size_t mi) :
73             version(v), majorRev(mj), minorRev(mi) {}
74 
75     size_t version;
76     size_t majorRev;
77     size_t minorRev;
78 
79     inline bool operator==(const KernelVersion &other) const {
80         return version == other.version
81             && majorRev == other.majorRev
82             && minorRev == other.minorRev;
83     }
84     inline bool operator!=(const KernelVersion &other) const {
85         return !((*this) == other);
86     }
87 
88     inline bool operator<(const KernelVersion& other) const {
89         if (version < other.version) return true;
90         if (version > other.version) return false;
91         if (majorRev < other.majorRev) return true;
92         if (majorRev > other.majorRev) return false;
93         return minorRev < other.minorRev;
94     }
95 
96     inline constexpr Version dropMinor() const { return Version{version, majorRev}; }
97 };
98 
99 } // namespace vintf
100 } // namespace android
101 
102 #endif // ANDROID_VINTF_VERSION_H
103