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 #ifndef ANDROID_VINTF_HAL_GROUP_H 18 #define ANDROID_VINTF_HAL_GROUP_H 19 20 #include <map> 21 22 #include "MapValueIterator.h" 23 24 namespace android { 25 namespace vintf { 26 27 // A HalGroup is a wrapped multimap from name to Hal. 28 // Hal.getName() must return a string indicating the name. 29 template <typename Hal> 30 struct HalGroup { 31 public: ~HalGroupHalGroup32 virtual ~HalGroup() {} 33 // Move all hals from another HalGroup to this. addAllHalGroup34 bool addAll(HalGroup&& other) { 35 for (auto& pair : other.mHals) { 36 if (!add(std::move(pair.second))) { 37 return false; 38 } 39 } 40 return true; 41 } 42 43 // Add an hal to this HalGroup so that it can be constructed programatically. addHalGroup44 bool add(Hal&& hal) { 45 if (!shouldAdd(hal)) { 46 return false; 47 } 48 std::string name = hal.getName(); 49 mHals.emplace(std::move(name), std::move(hal)); // always succeed 50 return true; 51 } 52 53 protected: 54 // sorted map from component name to the component. 55 // The component name looks like: android.hardware.foo 56 std::multimap<std::string, Hal> mHals; 57 58 // override this to filter for add. shouldAddHalGroup59 virtual bool shouldAdd(const Hal&) const { return true; } 60 61 // Return an iterable to all ManifestHal objects. Call it as follows: 62 // for (const auto& e : vm.getHals()) { } getHalsHalGroup63 ConstMultiMapValueIterable<std::string, Hal> getHals() const { 64 return ConstMultiMapValueIterable<std::string, Hal>(mHals); 65 } 66 67 // Get any HAL component based on the component name. Return any one 68 // if multiple. Return nullptr if the component does not exist. This is only 69 // for creating objects programatically. 70 // The component name looks like: 71 // android.hardware.foo getAnyHalHalGroup72 Hal* getAnyHal(const std::string& name) { 73 auto it = mHals.find(name); 74 if (it == mHals.end()) { 75 return nullptr; 76 } 77 return &(it->second); 78 } 79 }; 80 81 } // namespace vintf 82 } // namespace android 83 84 #endif // ANDROID_VINTF_HAL_GROUP_H 85