Arch.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_ARCH_H
  17. #define ANDROID_VINTF_ARCH_H
  18. #include <stdint.h>
  19. #include <string>
  20. #include <array>
  21. namespace android {
  22. namespace vintf {
  23. enum class Arch : size_t {
  24. ARCH_EMPTY = 0,
  25. ARCH_32,
  26. ARCH_64,
  27. ARCH_32_64
  28. };
  29. static const std::array<std::string, 4> gArchStrings = {
  30. {
  31. "",
  32. "32",
  33. "64",
  34. "32+64",
  35. }
  36. };
  37. inline bool has32(Arch arch) {
  38. return arch == Arch::ARCH_32 || arch == Arch::ARCH_32_64;
  39. }
  40. inline bool has64(Arch arch) {
  41. return arch == Arch::ARCH_64 || arch == Arch::ARCH_32_64;
  42. }
  43. inline constexpr Arch operator|(Arch lft, Arch rgt) {
  44. return static_cast<Arch>(static_cast<size_t>(lft) | static_cast<size_t>(rgt));
  45. }
  46. static_assert((Arch::ARCH_32 | Arch::ARCH_64) == Arch::ARCH_32_64, "bad Arch::operator|");
  47. inline Arch& operator|=(Arch& lft, Arch rgt) {
  48. return (lft = lft | rgt);
  49. }
  50. // Returns true if lft defines all bitnesses in rgt, otherwise false.
  51. inline constexpr bool contains(Arch lft, Arch rgt) {
  52. return !(~static_cast<size_t>(lft) & static_cast<size_t>(rgt));
  53. }
  54. static_assert(contains(Arch::ARCH_32_64, Arch::ARCH_32_64), "bad contains(Arch, Arch)");
  55. static_assert(contains(Arch::ARCH_32_64, Arch::ARCH_64), "bad contains(Arch, Arch)");
  56. static_assert(contains(Arch::ARCH_32_64, Arch::ARCH_32), "bad contains(Arch, Arch)");
  57. static_assert(contains(Arch::ARCH_32_64, Arch::ARCH_EMPTY), "bad contains(Arch, Arch)");
  58. static_assert(contains(Arch::ARCH_32, Arch::ARCH_EMPTY), "bad contains(Arch, Arch)");
  59. static_assert(contains(Arch::ARCH_64, Arch::ARCH_EMPTY), "bad contains(Arch, Arch)");
  60. static_assert(!contains(Arch::ARCH_32, Arch::ARCH_32_64), "bad contains(Arch, Arch)");
  61. static_assert(!contains(Arch::ARCH_64, Arch::ARCH_32_64), "bad contains(Arch, Arch)");
  62. static_assert(!contains(Arch::ARCH_32, Arch::ARCH_64), "bad contains(Arch, Arch)");
  63. static_assert(!contains(Arch::ARCH_64, Arch::ARCH_32), "bad contains(Arch, Arch)");
  64. static_assert(!contains(Arch::ARCH_EMPTY, Arch::ARCH_32), "bad contains(Arch, Arch)");
  65. static_assert(!contains(Arch::ARCH_EMPTY, Arch::ARCH_64), "bad contains(Arch, Arch)");
  66. } // namespace vintf
  67. } // namespace android
  68. #endif // ANDROID_VINTF_ARCH_H