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 | 85103c6 | 2018-03-20 15:43:34 -0700 | [diff] [blame] | 36 | MatrixInstance::MatrixInstance(FqInstance&& fqInstance, VersionRange&& range, bool optional, |
| 37 | bool isRegex) |
| 38 | : mFqInstance(std::move(fqInstance)), |
| 39 | mRange(std::move(range)), |
| 40 | mOptional(optional), |
| 41 | mIsRegex(isRegex) {} |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 42 | |
| 43 | MatrixInstance::MatrixInstance(const FqInstance fqInstance, const VersionRange& range, |
Yifan Hong | 85103c6 | 2018-03-20 15:43:34 -0700 | [diff] [blame] | 44 | bool optional, bool isRegex) |
| 45 | : mFqInstance(fqInstance), mRange(range), mOptional(optional), mIsRegex(isRegex) {} |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 46 | |
| 47 | const std::string& MatrixInstance::package() const { |
| 48 | return mFqInstance.getPackage(); |
| 49 | } |
| 50 | |
| 51 | const VersionRange& MatrixInstance::versionRange() const { |
| 52 | return mRange; |
| 53 | } |
| 54 | |
| 55 | const std::string& MatrixInstance::interface() const { |
| 56 | return mFqInstance.getInterface(); |
| 57 | } |
| 58 | |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 59 | bool MatrixInstance::optional() const { |
| 60 | return mOptional; |
| 61 | } |
| 62 | |
Yifan Hong | 075b363 | 2018-03-14 16:03:11 -0700 | [diff] [blame] | 63 | bool MatrixInstance::isSatisfiedBy(const FqInstance& provided) const { |
| 64 | return package() == provided.getPackage() && |
| 65 | versionRange().supportedBy(provided.getVersion()) && |
Yifan Hong | 85103c6 | 2018-03-20 15:43:34 -0700 | [diff] [blame] | 66 | interface() == provided.getInterface() && matchInstance(provided.getInstance()); |
| 67 | } |
| 68 | |
| 69 | bool MatrixInstance::matchInstance(const std::string& e) const { |
| 70 | if (!isRegex()) { |
| 71 | return exactInstance() == e; |
| 72 | } |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 73 | details::Regex regex; |
| 74 | if (!regex.compile(regexPattern())) { |
| 75 | return false; |
| 76 | } |
| 77 | return regex.matches(e); |
Yifan Hong | 85103c6 | 2018-03-20 15:43:34 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | const std::string& MatrixInstance::regexPattern() const { |
| 81 | static const std::string kEmptyString; |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 82 | return isRegex() ? mFqInstance.getInstance() : kEmptyString; |
Yifan Hong | 85103c6 | 2018-03-20 15:43:34 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | const std::string& MatrixInstance::exactInstance() const { |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 86 | static const std::string kEmptyString; |
| 87 | return isRegex() ? kEmptyString : mFqInstance.getInstance(); |
Yifan Hong | 85103c6 | 2018-03-20 15:43:34 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | bool MatrixInstance::isRegex() const { |
| 91 | return mIsRegex; |
Yifan Hong | 075b363 | 2018-03-14 16:03:11 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 94 | } // namespace vintf |
| 95 | } // namespace android |