MatrixInstance.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (C) 2018 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. #include "MatrixInstance.h"
  17. #include <utility>
  18. #include "Regex.h"
  19. namespace android {
  20. namespace vintf {
  21. MatrixInstance::MatrixInstance() = default;
  22. MatrixInstance::MatrixInstance(const MatrixInstance&) = default;
  23. MatrixInstance::MatrixInstance(MatrixInstance&&) noexcept = default;
  24. MatrixInstance& MatrixInstance::operator=(const MatrixInstance&) = default;
  25. MatrixInstance& MatrixInstance::operator=(MatrixInstance&&) noexcept = default;
  26. MatrixInstance::MatrixInstance(FqInstance&& fqInstance, VersionRange&& range, bool optional,
  27. bool isRegex)
  28. : mFqInstance(std::move(fqInstance)),
  29. mRange(std::move(range)),
  30. mOptional(optional),
  31. mIsRegex(isRegex) {}
  32. MatrixInstance::MatrixInstance(const FqInstance fqInstance, const VersionRange& range,
  33. bool optional, bool isRegex)
  34. : mFqInstance(fqInstance), mRange(range), mOptional(optional), mIsRegex(isRegex) {}
  35. const std::string& MatrixInstance::package() const {
  36. return mFqInstance.getPackage();
  37. }
  38. const VersionRange& MatrixInstance::versionRange() const {
  39. return mRange;
  40. }
  41. const std::string& MatrixInstance::interface() const {
  42. return mFqInstance.getInterface();
  43. }
  44. bool MatrixInstance::optional() const {
  45. return mOptional;
  46. }
  47. bool MatrixInstance::isSatisfiedBy(const FqInstance& provided) const {
  48. return package() == provided.getPackage() &&
  49. versionRange().supportedBy(provided.getVersion()) &&
  50. interface() == provided.getInterface() && matchInstance(provided.getInstance());
  51. }
  52. bool MatrixInstance::matchInstance(const std::string& e) const {
  53. if (!isRegex()) {
  54. return exactInstance() == e;
  55. }
  56. details::Regex regex;
  57. if (!regex.compile(regexPattern())) {
  58. return false;
  59. }
  60. return regex.matches(e);
  61. }
  62. const std::string& MatrixInstance::regexPattern() const {
  63. static const std::string kEmptyString;
  64. return isRegex() ? mFqInstance.getInstance() : kEmptyString;
  65. }
  66. const std::string& MatrixInstance::exactInstance() const {
  67. static const std::string kEmptyString;
  68. return isRegex() ? kEmptyString : mFqInstance.getInstance();
  69. }
  70. bool MatrixInstance::isRegex() const {
  71. return mIsRegex;
  72. }
  73. } // namespace vintf
  74. } // namespace android