xref: /aosp_15_r20/system/libvintf/include/vintf/VersionRange.h (revision 70a7ec852fcefd15a4fb57f8f183a8b1c3aacb08)
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_RANGE_H
19 #define ANDROID_VINTF_VERSION_RANGE_H
20 
21 #include <stdint.h>
22 #include <optional>
23 #include <string>
24 #include <tuple>
25 #include <utility>
26 
27 #include "Version.h"
28 
29 namespace android {
30 namespace vintf {
31 
32 // A version range with the same major version, e.g. 2.3-7
33 struct VersionRange {
VersionRangeVersionRange34     constexpr VersionRange() : VersionRange(0u, 0u, 0u) {}
VersionRangeVersionRange35     constexpr VersionRange(size_t mjV, size_t miV) : VersionRange(mjV, miV, miV) {}
VersionRangeVersionRange36     constexpr VersionRange(size_t mjV, size_t miM, size_t mxM)
37         : majorVer(mjV), minMinor(miM), maxMinor(mxM) {}
minVerVersionRange38     constexpr inline Version minVer() const { return Version(majorVer, minMinor); }
maxVerVersionRange39     constexpr inline Version maxVer() const { return Version(majorVer, maxMinor); }
isSingleVersionVersionRange40     inline bool isSingleVersion() const { return minMinor == maxMinor; }
41 
42     inline bool operator==(const VersionRange &other) const {
43         return majorVer == other.majorVer
44             && minMinor == other.minMinor
45             && maxMinor == other.maxMinor;
46     }
47 
48     inline bool operator!=(const VersionRange& other) const { return !(*this == other); }
49 
containsVersionRange50     inline bool contains(const Version &ver) const {
51         return minVer() <= ver && ver <= maxVer();
52     }
53 
54     // If this == 2.3-7,
55     //     ver == 2.2: false
56     //     ver == 2.3: true
57     //     ver == 2.7: true
58     //     ver == 2.8: true
supportedByVersionRange59     inline bool supportedBy(const Version &ver) const {
60         return majorVer == ver.majorVer && minMinor <= ver.minorVer;
61     }
62 
63     // If a.overlaps(b) then b.overlaps(a).
64     // 1.2-4 and 2.2-4: false
65     // 1.2-4 and 1.4-5: true
66     // 1.2-4 and 1.0-1: false
overlapsVersionRange67     inline bool overlaps(const VersionRange& other) const {
68         return majorVer == other.majorVer && minMinor <= other.maxMinor &&
69                other.minMinor <= maxMinor;
70     }
71 
72     size_t majorVer;
73     size_t minMinor;
74     size_t maxMinor;
75 };
76 
77 struct SepolicyVersionRange {
78     size_t majorVer;
79     std::optional<size_t> minMinor;
80     std::optional<size_t> maxMinor;
81 
SepolicyVersionRangeSepolicyVersionRange82     constexpr SepolicyVersionRange() : SepolicyVersionRange(0u, std::nullopt, std::nullopt) {}
SepolicyVersionRangeSepolicyVersionRange83     constexpr SepolicyVersionRange(size_t mjV, std::optional<size_t> miV)
84         : SepolicyVersionRange(mjV, miV, miV) {}
SepolicyVersionRangeSepolicyVersionRange85     constexpr SepolicyVersionRange(size_t mjV, std::optional<size_t> miM, std::optional<size_t> mxM)
86         : majorVer(mjV), minMinor(miM), maxMinor(mxM) {}
minVerSepolicyVersionRange87     constexpr inline SepolicyVersion minVer() const { return SepolicyVersion(majorVer, minMinor); }
maxVerSepolicyVersionRange88     constexpr inline SepolicyVersion maxVer() const { return SepolicyVersion(majorVer, maxMinor); }
isSingleVersionSepolicyVersionRange89     inline bool isSingleVersion() const { return minMinor == maxMinor; }
90 
91     bool operator==(const SepolicyVersionRange& other) const = default;
92     bool operator!=(const SepolicyVersionRange& other) const = default;
93 
94     // If this == 2.3-7,
95     //     ver == 2.2: false
96     //     ver == 2.3: true
97     //     ver == 2.7: true
98     //     ver == 2.8: true
supportedBySepolicyVersionRange99     inline bool supportedBy(const SepolicyVersion& ver) const {
100         return majorVer == ver.majorVer && minMinor <= ver.minorVer;
101     }
102 };
103 
104 } // namespace vintf
105 } // namespace android
106 
107 #endif // ANDROID_VINTF_VERSION_RANGE_H
108