utils.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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_UTILS_H
  17. #define ANDROID_VINTF_UTILS_H
  18. #include <memory>
  19. #include <mutex>
  20. #include <utils/Errors.h>
  21. #include <vintf/FileSystem.h>
  22. #include <vintf/PropertyFetcher.h>
  23. #include <vintf/RuntimeInfo.h>
  24. #include <vintf/parse_xml.h>
  25. namespace android {
  26. namespace vintf {
  27. namespace details {
  28. template <typename T>
  29. status_t fetchAllInformation(const FileSystem* fileSystem, const std::string& path,
  30. const XmlConverter<T>& converter, T* outObject, std::string* error) {
  31. std::string info;
  32. status_t result = fileSystem->fetch(path, &info, error);
  33. if (result != OK) {
  34. return result;
  35. }
  36. bool success = converter(outObject, info, error);
  37. if (!success) {
  38. if (error) {
  39. *error = "Illformed file: " + path + ": " + *error;
  40. }
  41. return BAD_VALUE;
  42. }
  43. return OK;
  44. }
  45. // TODO(b/70628538): Do not infer from Shipping API level.
  46. inline Level convertFromApiLevel(size_t apiLevel) {
  47. if (apiLevel < 26) {
  48. return Level::LEGACY;
  49. } else if (apiLevel == 26) {
  50. return Level::O;
  51. } else if (apiLevel == 27) {
  52. return Level::O_MR1;
  53. } else {
  54. return Level::UNSPECIFIED;
  55. }
  56. }
  57. class PropertyFetcherImpl : public PropertyFetcher {
  58. public:
  59. virtual std::string getProperty(const std::string& key,
  60. const std::string& defaultValue = "") const;
  61. virtual uint64_t getUintProperty(const std::string& key, uint64_t defaultValue,
  62. uint64_t max = UINT64_MAX) const;
  63. virtual bool getBoolProperty(const std::string& key, bool defaultValue) const;
  64. };
  65. class PropertyFetcherNoOp : public PropertyFetcher {
  66. public:
  67. virtual std::string getProperty(const std::string& key,
  68. const std::string& defaultValue = "") const override;
  69. virtual uint64_t getUintProperty(const std::string& key, uint64_t defaultValue,
  70. uint64_t max = UINT64_MAX) const override;
  71. virtual bool getBoolProperty(const std::string& key, bool defaultValue) const override;
  72. };
  73. // Merge src into dst.
  74. // postcondition (if successful): *src == empty.
  75. template <typename T>
  76. static bool mergeField(T* dst, T* src, const T& empty = T{}) {
  77. if (*dst == *src) {
  78. *src = empty;
  79. return true; // no conflict
  80. }
  81. if (*src == empty) {
  82. return true;
  83. }
  84. if (*dst == empty) {
  85. *dst = std::move(*src);
  86. *src = empty;
  87. return true;
  88. }
  89. return false;
  90. }
  91. } // namespace details
  92. } // namespace vintf
  93. } // namespace android
  94. #endif