Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 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 | |
| 17 | #include "MatrixInstance.h" |
| 18 | |
| 19 | #include <utility> |
| 20 | |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 21 | #include "Regex.h" |
| 22 | |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 23 | namespace android { |
| 24 | namespace vintf { |
| 25 | |
Yifan Hong | cf231a9 | 2018-03-13 14:19:13 -0700 | [diff] [blame] | 26 | MatrixInstance::MatrixInstance() = default; |
| 27 | |
| 28 | MatrixInstance::MatrixInstance(const MatrixInstance&) = default; |
| 29 | |
Chih-Hung Hsieh | d345706 | 2018-09-25 13:40:34 -0700 | [diff] [blame] | 30 | MatrixInstance::MatrixInstance(MatrixInstance&&) noexcept = default; |
Yifan Hong | cf231a9 | 2018-03-13 14:19:13 -0700 | [diff] [blame] | 31 | |
| 32 | MatrixInstance& MatrixInstance::operator=(const MatrixInstance&) = default; |
| 33 | |
Chih-Hung Hsieh | d345706 | 2018-09-25 13:40:34 -0700 | [diff] [blame] | 34 | MatrixInstance& MatrixInstance::operator=(MatrixInstance&&) noexcept = default; |
Yifan Hong | cf231a9 | 2018-03-13 14:19:13 -0700 | [diff] [blame] | 35 | |
Yifan Hong | 219000b | 2019-09-10 19:23:19 -0700 | [diff] [blame^] | 36 | MatrixInstance::MatrixInstance(HalFormat format, FqInstance&& fqInstance, VersionRange&& range, |
| 37 | bool optional, bool isRegex) |
| 38 | : mFormat(format), |
| 39 | mFqInstance(std::move(fqInstance)), |
Yifan Hong | 85103c6 | 2018-03-20 15:43:34 -0700 | [diff] [blame] | 40 | mRange(std::move(range)), |
| 41 | mOptional(optional), |
| 42 | mIsRegex(isRegex) {} |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 43 | |
Yifan Hong | 219000b | 2019-09-10 19:23:19 -0700 | [diff] [blame^] | 44 | MatrixInstance::MatrixInstance(HalFormat format, const FqInstance fqInstance, |
| 45 | const VersionRange& range, bool optional, bool isRegex) |
| 46 | : mFormat(format), |
| 47 | mFqInstance(fqInstance), |
| 48 | mRange(range), |
| 49 | mOptional(optional), |
| 50 | mIsRegex(isRegex) {} |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 51 | |
| 52 | const std::string& MatrixInstance::package() const { |
| 53 | return mFqInstance.getPackage(); |
| 54 | } |
| 55 | |
| 56 | const VersionRange& MatrixInstance::versionRange() const { |
| 57 | return mRange; |
| 58 | } |
| 59 | |
| 60 | const std::string& MatrixInstance::interface() const { |
| 61 | return mFqInstance.getInterface(); |
| 62 | } |
| 63 | |
Yifan Hong | 219000b | 2019-09-10 19:23:19 -0700 | [diff] [blame^] | 64 | HalFormat MatrixInstance::format() const { |
| 65 | return mFormat; |
| 66 | } |
| 67 | |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 68 | bool MatrixInstance::optional() const { |
| 69 | return mOptional; |
| 70 | } |
| 71 | |
Yifan Hong | 075b363 | 2018-03-14 16:03:11 -0700 | [diff] [blame] | 72 | bool MatrixInstance::isSatisfiedBy(const FqInstance& provided) const { |
| 73 | return package() == provided.getPackage() && |
| 74 | versionRange().supportedBy(provided.getVersion()) && |
Yifan Hong | 85103c6 | 2018-03-20 15:43:34 -0700 | [diff] [blame] | 75 | interface() == provided.getInterface() && matchInstance(provided.getInstance()); |
| 76 | } |
| 77 | |
| 78 | bool MatrixInstance::matchInstance(const std::string& e) const { |
| 79 | if (!isRegex()) { |
| 80 | return exactInstance() == e; |
| 81 | } |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 82 | details::Regex regex; |
| 83 | if (!regex.compile(regexPattern())) { |
| 84 | return false; |
| 85 | } |
| 86 | return regex.matches(e); |
Yifan Hong | 85103c6 | 2018-03-20 15:43:34 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | const std::string& MatrixInstance::regexPattern() const { |
| 90 | static const std::string kEmptyString; |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 91 | return isRegex() ? mFqInstance.getInstance() : kEmptyString; |
Yifan Hong | 85103c6 | 2018-03-20 15:43:34 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | const std::string& MatrixInstance::exactInstance() const { |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 95 | static const std::string kEmptyString; |
| 96 | return isRegex() ? kEmptyString : mFqInstance.getInstance(); |
Yifan Hong | 85103c6 | 2018-03-20 15:43:34 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool MatrixInstance::isRegex() const { |
| 100 | return mIsRegex; |
Yifan Hong | 075b363 | 2018-03-14 16:03:11 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 103 | } // namespace vintf |
| 104 | } // namespace android |