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 <optional> 23 #include <string> 24 #include <utility> 25 26 namespace android { 27 namespace vintf { 28 29 struct Version { 30 VersionVersion31 constexpr Version() : Version(0u, 0u) {} VersionVersion32 constexpr Version(size_t mj, size_t mi) : majorVer(mj), minorVer(mi) {} VersionVersion33 constexpr Version(const std::pair<size_t, size_t>& pair) 34 : majorVer(pair.first), minorVer(pair.second) {} 35 36 size_t majorVer; 37 size_t minorVer; 38 39 inline bool operator==(const Version &other) const { 40 return majorVer == other.majorVer && minorVer == other.minorVer; 41 } 42 inline bool operator!=(const Version &other) const { 43 return !((*this) == other); 44 } 45 inline bool operator<(const Version &other) const { 46 if (majorVer < other.majorVer) 47 return true; 48 if (majorVer > other.majorVer) 49 return false; 50 return minorVer < other.minorVer; 51 } 52 inline bool operator>(const Version &other) const { 53 return other < (*this); 54 } 55 inline bool operator<=(const Version &other) const { 56 return !((*this) > other); 57 } 58 inline bool operator>=(const Version &other) const { 59 return !((*this) < other); 60 } 61 // Version(2, 1).minorAtLeast(Version(1, 0)) == false 62 // Version(2, 1).minorAtLeast(Version(2, 0)) == true 63 // Version(2, 1).minorAtLeast(Version(2, 1)) == true 64 // Version(2, 1).minorAtLeast(Version(2, 2)) == false minorAtLeastVersion65 inline bool minorAtLeast(const Version& other) const { 66 return majorVer == other.majorVer && minorVer >= other.minorVer; 67 } 68 withMinorVersion69 inline Version withMinor(size_t mi) { return Version(majorVer, mi); } 70 }; 71 72 struct SepolicyVersion { SepolicyVersionSepolicyVersion73 constexpr SepolicyVersion() : SepolicyVersion(0u, std::nullopt) {} SepolicyVersionSepolicyVersion74 constexpr SepolicyVersion(size_t mj, std::optional<size_t> mi) : majorVer(mj), minorVer(mi) {} 75 76 size_t majorVer; 77 std::optional<size_t> minorVer; 78 79 bool operator==(const SepolicyVersion& other) const = default; 80 bool operator!=(const SepolicyVersion& other) const = default; 81 inline bool operator<(const SepolicyVersion& other) const { 82 return std::pair(majorVer, minorVer) < std::pair(other.majorVer, other.minorVer); 83 } 84 inline bool operator>(const SepolicyVersion& other) const { return other < *this; } 85 inline bool operator<=(const SepolicyVersion& other) const { return !((*this) > other); } 86 inline bool operator>=(const SepolicyVersion& other) const { return !((*this) < other); } 87 }; 88 89 struct KernelVersion { 90 KernelVersionKernelVersion91 constexpr KernelVersion() : KernelVersion(0u, 0u, 0u) {} KernelVersionKernelVersion92 constexpr KernelVersion(size_t v, size_t mj, size_t mi) : 93 version(v), majorRev(mj), minorRev(mi) {} 94 95 size_t version; 96 size_t majorRev; 97 size_t minorRev; 98 99 inline bool operator==(const KernelVersion &other) const { 100 return version == other.version 101 && majorRev == other.majorRev 102 && minorRev == other.minorRev; 103 } 104 inline bool operator!=(const KernelVersion &other) const { 105 return !((*this) == other); 106 } 107 108 inline bool operator<(const KernelVersion& other) const { 109 if (version < other.version) return true; 110 if (version > other.version) return false; 111 if (majorRev < other.majorRev) return true; 112 if (majorRev > other.majorRev) return false; 113 return minorRev < other.minorRev; 114 } 115 inline bool operator>(const KernelVersion& other) const { return other < (*this); } 116 inline bool operator<=(const KernelVersion& other) const { return !((*this) > other); } 117 inline bool operator>=(const KernelVersion& other) const { return !((*this) < other); } 118 dropMinorKernelVersion119 inline constexpr Version dropMinor() const { return Version{version, majorRev}; } 120 }; 121 122 } // namespace vintf 123 } // namespace android 124 125 #endif // ANDROID_VINTF_VERSION_H 126