blob: fb1cb33950a1437be655bf2ca73a25fa33b18ed0 [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 Hong219000b2019-09-10 19:23:19 -070036MatrixInstance::MatrixInstance(HalFormat format, FqInstance&& fqInstance, VersionRange&& range,
37 bool optional, bool isRegex)
38 : mFormat(format),
39 mFqInstance(std::move(fqInstance)),
Yifan Hong85103c62018-03-20 15:43:34 -070040 mRange(std::move(range)),
41 mOptional(optional),
42 mIsRegex(isRegex) {}
Yifan Hong2a90ffe2018-03-05 17:45:34 -080043
Yifan Hong219000b2019-09-10 19:23:19 -070044MatrixInstance::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 Hong2a90ffe2018-03-05 17:45:34 -080051
52const std::string& MatrixInstance::package() const {
53 return mFqInstance.getPackage();
54}
55
56const VersionRange& MatrixInstance::versionRange() const {
57 return mRange;
58}
59
60const std::string& MatrixInstance::interface() const {
61 return mFqInstance.getInterface();
62}
63
Yifan Hong219000b2019-09-10 19:23:19 -070064HalFormat MatrixInstance::format() const {
65 return mFormat;
66}
67
Yifan Hong2a90ffe2018-03-05 17:45:34 -080068bool MatrixInstance::optional() const {
69 return mOptional;
70}
71
Yifan Hong075b3632018-03-14 16:03:11 -070072bool MatrixInstance::isSatisfiedBy(const FqInstance& provided) const {
73 return package() == provided.getPackage() &&
74 versionRange().supportedBy(provided.getVersion()) &&
Yifan Hong85103c62018-03-20 15:43:34 -070075 interface() == provided.getInterface() && matchInstance(provided.getInstance());
76}
77
78bool MatrixInstance::matchInstance(const std::string& e) const {
79 if (!isRegex()) {
80 return exactInstance() == e;
81 }
Yifan Hong643a9ef2018-03-21 14:13:55 -070082 details::Regex regex;
83 if (!regex.compile(regexPattern())) {
84 return false;
85 }
86 return regex.matches(e);
Yifan Hong85103c62018-03-20 15:43:34 -070087}
88
89const std::string& MatrixInstance::regexPattern() const {
90 static const std::string kEmptyString;
Yifan Hong643a9ef2018-03-21 14:13:55 -070091 return isRegex() ? mFqInstance.getInstance() : kEmptyString;
Yifan Hong85103c62018-03-20 15:43:34 -070092}
93
94const std::string& MatrixInstance::exactInstance() const {
Yifan Hong643a9ef2018-03-21 14:13:55 -070095 static const std::string kEmptyString;
96 return isRegex() ? kEmptyString : mFqInstance.getInstance();
Yifan Hong85103c62018-03-20 15:43:34 -070097}
98
99bool MatrixInstance::isRegex() const {
100 return mIsRegex;
Yifan Hong075b3632018-03-14 16:03:11 -0700101}
102
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800103} // namespace vintf
104} // namespace android