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 constexpr VersionRange() : VersionRange(0u, 0u, 0u) {} VersionRangeVersionRange34 constexpr VersionRange(size_t mjV, size_t miV) : VersionRange(mjV, miV, miV) {} VersionRangeVersionRange35 constexpr VersionRange(size_t mjV, size_t miM, size_t mxM) 36 : majorVer(mjV), minMinor(miM), maxMinor(mxM) {} minVerVersionRange37 constexpr inline Version minVer() const { return Version(majorVer, minMinor); } maxVerVersionRange38 constexpr inline Version maxVer() const { return Version(majorVer, maxMinor); } isSingleVersionVersionRange39 inline bool isSingleVersion() const { return minMinor == maxMinor; } 40 41 inline bool operator==(const VersionRange &other) const { 42 return majorVer == other.majorVer 43 && minMinor == other.minMinor 44 && maxMinor == other.maxMinor; 45 } 46 containsVersionRange47 inline bool contains(const Version &ver) const { 48 return minVer() <= ver && ver <= maxVer(); 49 } 50 51 // If this == 2.3-7, 52 // ver == 2.2: false 53 // ver == 2.3: true 54 // ver == 2.7: true 55 // ver == 2.8: false supportedByVersionRange56 inline bool supportedBy(const Version &ver) const { 57 return majorVer == ver.majorVer && minMinor <= ver.minorVer; 58 } 59 60 // If a.overlaps(b) then b.overlaps(a). 61 // 1.2-4 and 2.2-4: false 62 // 1.2-4 and 1.4-5: true 63 // 1.2-4 and 1.0-1: false overlapsVersionRange64 inline bool overlaps(const VersionRange& other) const { 65 return majorVer == other.majorVer && minMinor <= other.maxMinor && 66 other.minMinor <= maxMinor; 67 } 68 69 size_t majorVer; 70 size_t minMinor; 71 size_t maxMinor; 72 }; 73 74 } // namespace vintf 75 } // namespace android 76 77 #endif // ANDROID_VINTF_VERSION_RANGE_H 78