blob: f6eb5480905836af98b240743988f819ef398f6e [file] [log] [blame]
Yifan Hong2a90ffe2018-03-05 17:45:34 -08001/*
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 Hong643a9ef2018-03-21 14:13:55 -070021#include "Regex.h"
22
Yifan Hong2a90ffe2018-03-05 17:45:34 -080023namespace android {
24namespace vintf {
25
Yifan Hongcf231a92018-03-13 14:19:13 -070026MatrixInstance::MatrixInstance() = default;
27
28MatrixInstance::MatrixInstance(const MatrixInstance&) = default;
29
Chih-Hung Hsiehd3457062018-09-25 13:40:34 -070030MatrixInstance::MatrixInstance(MatrixInstance&&) noexcept = default;
Yifan Hongcf231a92018-03-13 14:19:13 -070031
32MatrixInstance& MatrixInstance::operator=(const MatrixInstance&) = default;
33
Chih-Hung Hsiehd3457062018-09-25 13:40:34 -070034MatrixInstance& MatrixInstance::operator=(MatrixInstance&&) noexcept = default;
Yifan Hongcf231a92018-03-13 14:19:13 -070035
Yifan Hong85103c62018-03-20 15:43:34 -070036MatrixInstance::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 Hong2a90ffe2018-03-05 17:45:34 -080042
43MatrixInstance::MatrixInstance(const FqInstance fqInstance, const VersionRange& range,
Yifan Hong85103c62018-03-20 15:43:34 -070044 bool optional, bool isRegex)
45 : mFqInstance(fqInstance), mRange(range), mOptional(optional), mIsRegex(isRegex) {}
Yifan Hong2a90ffe2018-03-05 17:45:34 -080046
47const std::string& MatrixInstance::package() const {
48 return mFqInstance.getPackage();
49}
50
51const VersionRange& MatrixInstance::versionRange() const {
52 return mRange;
53}
54
55const std::string& MatrixInstance::interface() const {
56 return mFqInstance.getInterface();
57}
58
Yifan Hong2a90ffe2018-03-05 17:45:34 -080059bool MatrixInstance::optional() const {
60 return mOptional;
61}
62
Yifan Hong075b3632018-03-14 16:03:11 -070063bool MatrixInstance::isSatisfiedBy(const FqInstance& provided) const {
64 return package() == provided.getPackage() &&
65 versionRange().supportedBy(provided.getVersion()) &&
Yifan Hong85103c62018-03-20 15:43:34 -070066 interface() == provided.getInterface() && matchInstance(provided.getInstance());
67}
68
69bool MatrixInstance::matchInstance(const std::string& e) const {
70 if (!isRegex()) {
71 return exactInstance() == e;
72 }
Yifan Hong643a9ef2018-03-21 14:13:55 -070073 details::Regex regex;
74 if (!regex.compile(regexPattern())) {
75 return false;
76 }
77 return regex.matches(e);
Yifan Hong85103c62018-03-20 15:43:34 -070078}
79
80const std::string& MatrixInstance::regexPattern() const {
81 static const std::string kEmptyString;
Yifan Hong643a9ef2018-03-21 14:13:55 -070082 return isRegex() ? mFqInstance.getInstance() : kEmptyString;
Yifan Hong85103c62018-03-20 15:43:34 -070083}
84
85const std::string& MatrixInstance::exactInstance() const {
Yifan Hong643a9ef2018-03-21 14:13:55 -070086 static const std::string kEmptyString;
87 return isRegex() ? kEmptyString : mFqInstance.getInstance();
Yifan Hong85103c62018-03-20 15:43:34 -070088}
89
90bool MatrixInstance::isRegex() const {
91 return mIsRegex;
Yifan Hong075b3632018-03-14 16:03:11 -070092}
93
Yifan Hong2a90ffe2018-03-05 17:45:34 -080094} // namespace vintf
95} // namespace android