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 47 inline bool operator!=(const VersionRange& other) const { return !(*this == other); } 48 containsVersionRange49 inline bool contains(const Version &ver) const { 50 return minVer() <= ver && ver <= maxVer(); 51 } 52 53 // If this == 2.3-7, 54 // ver == 2.2: false 55 // ver == 2.3: true 56 // ver == 2.7: true 57 // ver == 2.8: false supportedByVersionRange58 inline bool supportedBy(const Version &ver) const { 59 return majorVer == ver.majorVer && minMinor <= ver.minorVer; 60 } 61 62 // If a.overlaps(b) then b.overlaps(a). 63 // 1.2-4 and 2.2-4: false 64 // 1.2-4 and 1.4-5: true 65 // 1.2-4 and 1.0-1: false overlapsVersionRange66 inline bool overlaps(const VersionRange& other) const { 67 return majorVer == other.majorVer && minMinor <= other.maxMinor && 68 other.minMinor <= maxMinor; 69 } 70 71 size_t majorVer; 72 size_t minMinor; 73 size_t maxMinor; 74 }; 75 76 } // namespace vintf 77 } // namespace android 78 79 #endif // ANDROID_VINTF_VERSION_RANGE_H 80