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 <string>
23 #include <tuple>
24 #include <utility>
25 
26 #include "Version.h"
27 
28 namespace android {
29 namespace vintf {
30 
31 // A version range with the same major version, e.g. 2.3-7
32 struct VersionRange {
VersionRangeVersionRange33     VersionRange() : VersionRange(0u, 0u, 0u) {};
VersionRangeVersionRange34     VersionRange(size_t mjV, size_t miV)
35             : VersionRange(mjV, miV, miV) {};
VersionRangeVersionRange36     VersionRange(size_t mjV, size_t miM, size_t mxM)
37             : majorVer(mjV), minMinor(miM), maxMinor(mxM) {}
minVerVersionRange38     inline Version minVer() const { return Version(majorVer, minMinor); }
maxVerVersionRange39     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 
containsVersionRange48     inline bool contains(const Version &ver) const {
49         return minVer() <= ver && ver <= maxVer();
50     }
51 
52     // If this == 2.3-7,
53     //     ver == 2.2: false
54     //     ver == 2.3: true
55     //     ver == 2.7: true
56     //     ver == 2.8: false
supportedByVersionRange57     inline bool supportedBy(const Version &ver) const {
58         return majorVer == ver.majorVer && minMinor <= ver.minorVer;
59     }
60 
61     // If a.overlaps(b) then b.overlaps(a).
62     // 1.2-4 and 2.2-4: false
63     // 1.2-4 and 1.4-5: true
64     // 1.2-4 and 1.0-1: false
overlapsVersionRange65     inline bool overlaps(const VersionRange& other) const {
66         return majorVer == other.majorVer && minMinor <= other.maxMinor &&
67                other.minMinor <= maxMinor;
68     }
69 
70     size_t majorVer;
71     size_t minMinor;
72     size_t maxMinor;
73 };
74 
75 } // namespace vintf
76 } // namespace android
77 
78 #endif // ANDROID_VINTF_VERSION_RANGE_H
79