Home | History | Annotate | Download | only in libvintf
      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 
     21 #include "Regex.h"
     22 
     23 namespace android {
     24 namespace vintf {
     25 
     26 MatrixInstance::MatrixInstance() = default;
     27 
     28 MatrixInstance::MatrixInstance(const MatrixInstance&) = default;
     29 
     30 MatrixInstance::MatrixInstance(MatrixInstance&&) = default;
     31 
     32 MatrixInstance& MatrixInstance::operator=(const MatrixInstance&) = default;
     33 
     34 MatrixInstance& MatrixInstance::operator=(MatrixInstance&&) = default;
     35 
     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) {}
     42 
     43 MatrixInstance::MatrixInstance(const FqInstance fqInstance, const VersionRange& range,
     44                                bool optional, bool isRegex)
     45     : mFqInstance(fqInstance), mRange(range), mOptional(optional), mIsRegex(isRegex) {}
     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 
     59 bool MatrixInstance::optional() const {
     60     return mOptional;
     61 }
     62 
     63 bool MatrixInstance::isSatisfiedBy(const FqInstance& provided) const {
     64     return package() == provided.getPackage() &&
     65            versionRange().supportedBy(provided.getVersion()) &&
     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     }
     73     details::Regex regex;
     74     if (!regex.compile(regexPattern())) {
     75         return false;
     76     }
     77     return regex.matches(e);
     78 }
     79 
     80 const std::string& MatrixInstance::regexPattern() const {
     81     static const std::string kEmptyString;
     82     return isRegex() ? mFqInstance.getInstance() : kEmptyString;
     83 }
     84 
     85 const std::string& MatrixInstance::exactInstance() const {
     86     static const std::string kEmptyString;
     87     return isRegex() ? kEmptyString : mFqInstance.getInstance();
     88 }
     89 
     90 bool MatrixInstance::isRegex() const {
     91     return mIsRegex;
     92 }
     93 
     94 }  // namespace vintf
     95 }  // namespace android
     96