Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "MatrixHal.h" |
| 18 | |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 19 | #include "MapValueIterator.h" |
| 20 | |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 21 | namespace android { |
| 22 | namespace vintf { |
| 23 | |
| 24 | bool MatrixHal::operator==(const MatrixHal &other) const { |
| 25 | if (format != other.format) |
| 26 | return false; |
| 27 | if (name != other.name) |
| 28 | return false; |
| 29 | if (versionRanges != other.versionRanges) |
| 30 | return false; |
Yifan Hong | 43e2aae | 2017-05-17 18:36:08 -0700 | [diff] [blame] | 31 | if (interfaces != other.interfaces) |
| 32 | return false; |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 33 | // do not compare optional |
| 34 | return true; |
| 35 | } |
| 36 | |
Zhuoyao Zhang | c7e75bd | 2017-10-29 17:20:19 -0700 | [diff] [blame] | 37 | bool MatrixHal::containsVersion(const Version& version) const { |
| 38 | for (VersionRange vRange : versionRanges) { |
| 39 | if (vRange.contains(version)) return true; |
| 40 | } |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | std::set<std::string> MatrixHal::getInstances(const std::string& interfaceName) const { |
| 45 | std::set<std::string> ret; |
| 46 | auto it = interfaces.find(interfaceName); |
| 47 | if (it != interfaces.end()) { |
| 48 | ret.insert(it->second.instances.begin(), it->second.instances.end()); |
| 49 | } |
| 50 | return ret; |
| 51 | } |
| 52 | |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 53 | bool MatrixHal::containsInstances(const MatrixHal& other) const { |
| 54 | for (const auto& pair : other.interfaces) { |
| 55 | const std::string& interfaceName = pair.first; |
| 56 | auto thisIt = interfaces.find(interfaceName); |
| 57 | if (thisIt == interfaces.end()) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | const std::set<std::string>& thisInstances = thisIt->second.instances; |
| 62 | const std::set<std::string>& otherInstances = pair.second.instances; |
| 63 | if (!std::includes(thisInstances.begin(), thisInstances.end(), otherInstances.begin(), |
| 64 | otherInstances.end())) { |
| 65 | return false; |
| 66 | } |
| 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 71 | bool MatrixHal::forEachInstance(const std::function<bool(const MatrixInstance&)>& func) const { |
| 72 | for (const auto& vr : versionRanges) { |
| 73 | for (const auto& intf : iterateValues(interfaces)) { |
| 74 | for (const auto& instance : intf.instances) { |
| 75 | // TODO(b/73556059): Store MatrixInstance as well to avoid creating temps |
| 76 | FqInstance fqInstance; |
| 77 | if (fqInstance.setTo(getName(), vr.majorVer, vr.minMinor, intf.name, instance)) { |
| 78 | if (!func(MatrixInstance(std::move(fqInstance), VersionRange(vr), optional))) { |
| 79 | return false; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | return true; |
| 86 | } |
| 87 | |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 88 | } // namespace vintf |
| 89 | } // namespace android |