Version.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #ifndef ANDROID_VINTF_VERSION_H
  17. #define ANDROID_VINTF_VERSION_H
  18. #include <stdint.h>
  19. #include <string>
  20. #include <utility>
  21. namespace android {
  22. namespace vintf {
  23. struct Version {
  24. constexpr Version() : Version(0u, 0u) {}
  25. constexpr Version(size_t mj, size_t mi) : majorVer(mj), minorVer(mi) {}
  26. constexpr Version(const std::pair<size_t, size_t>& pair)
  27. : majorVer(pair.first), minorVer(pair.second) {}
  28. size_t majorVer;
  29. size_t minorVer;
  30. inline bool operator==(const Version &other) const {
  31. return majorVer == other.majorVer && minorVer == other.minorVer;
  32. }
  33. inline bool operator!=(const Version &other) const {
  34. return !((*this) == other);
  35. }
  36. inline bool operator<(const Version &other) const {
  37. if (majorVer < other.majorVer)
  38. return true;
  39. if (majorVer > other.majorVer)
  40. return false;
  41. return minorVer < other.minorVer;
  42. }
  43. inline bool operator>(const Version &other) const {
  44. return other < (*this);
  45. }
  46. inline bool operator<=(const Version &other) const {
  47. return !((*this) > other);
  48. }
  49. inline bool operator>=(const Version &other) const {
  50. return !((*this) < other);
  51. }
  52. // Version(2, 1).minorAtLeast(Version(1, 0)) == false
  53. // Version(2, 1).minorAtLeast(Version(2, 0)) == true
  54. // Version(2, 1).minorAtLeast(Version(2, 1)) == true
  55. // Version(2, 1).minorAtLeast(Version(2, 2)) == false
  56. inline bool minorAtLeast(const Version& other) const {
  57. return majorVer == other.majorVer && minorVer >= other.minorVer;
  58. }
  59. };
  60. struct KernelVersion {
  61. constexpr KernelVersion() : KernelVersion(0u, 0u, 0u) {}
  62. constexpr KernelVersion(size_t v, size_t mj, size_t mi) :
  63. version(v), majorRev(mj), minorRev(mi) {}
  64. size_t version;
  65. size_t majorRev;
  66. size_t minorRev;
  67. inline bool operator==(const KernelVersion &other) const {
  68. return version == other.version
  69. && majorRev == other.majorRev
  70. && minorRev == other.minorRev;
  71. }
  72. inline bool operator!=(const KernelVersion &other) const {
  73. return !((*this) == other);
  74. }
  75. inline bool operator<(const KernelVersion& other) const {
  76. if (version < other.version) return true;
  77. if (version > other.version) return false;
  78. if (majorRev < other.majorRev) return true;
  79. if (majorRev > other.majorRev) return false;
  80. return minorRev < other.minorRev;
  81. }
  82. };
  83. } // namespace vintf
  84. } // namespace android
  85. #endif // ANDROID_VINTF_VERSION_H