VersionRange.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_RANGE_H
  17. #define ANDROID_VINTF_VERSION_RANGE_H
  18. #include <stdint.h>
  19. #include <string>
  20. #include <tuple>
  21. #include <utility>
  22. #include "Version.h"
  23. namespace android {
  24. namespace vintf {
  25. // A version range with the same major version, e.g. 2.3-7
  26. struct VersionRange {
  27. VersionRange() : VersionRange(0u, 0u, 0u) {}
  28. VersionRange(size_t mjV, size_t miV) : VersionRange(mjV, miV, miV) {}
  29. VersionRange(size_t mjV, size_t miM, size_t mxM)
  30. : majorVer(mjV), minMinor(miM), maxMinor(mxM) {}
  31. inline Version minVer() const { return Version(majorVer, minMinor); }
  32. inline Version maxVer() const { return Version(majorVer, maxMinor); }
  33. inline bool isSingleVersion() const { return minMinor == maxMinor; }
  34. inline bool operator==(const VersionRange &other) const {
  35. return majorVer == other.majorVer
  36. && minMinor == other.minMinor
  37. && maxMinor == other.maxMinor;
  38. }
  39. inline bool contains(const Version &ver) const {
  40. return minVer() <= ver && ver <= maxVer();
  41. }
  42. // If this == 2.3-7,
  43. // ver == 2.2: false
  44. // ver == 2.3: true
  45. // ver == 2.7: true
  46. // ver == 2.8: false
  47. inline bool supportedBy(const Version &ver) const {
  48. return majorVer == ver.majorVer && minMinor <= ver.minorVer;
  49. }
  50. // If a.overlaps(b) then b.overlaps(a).
  51. // 1.2-4 and 2.2-4: false
  52. // 1.2-4 and 1.4-5: true
  53. // 1.2-4 and 1.0-1: false
  54. inline bool overlaps(const VersionRange& other) const {
  55. return majorVer == other.majorVer && minMinor <= other.maxMinor &&
  56. other.minMinor <= maxMinor;
  57. }
  58. size_t majorVer;
  59. size_t minMinor;
  60. size_t maxMinor;
  61. };
  62. } // namespace vintf
  63. } // namespace android
  64. #endif // ANDROID_VINTF_VERSION_RANGE_H