1 /* 2 * Copyright (C) 2015 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 #pragma once 18 19 #include "Element.h" 20 #include "Stream.h" 21 #include "Strategy.h" 22 #include "Usage.h" 23 #include "InputSource.h" 24 #include <utils/Errors.h> 25 #include <system/audio.h> 26 #include <utils/Log.h> 27 #include <map> 28 #include <stdint.h> 29 #include <string> 30 31 namespace android 32 { 33 namespace audio_policy 34 { 35 36 /** 37 * Collection of policy element as a map indexed with a their UID type. 38 * 39 * @tparam Key type of the policy element indexing the collection. 40 * Policy Element supported are: 41 * - Strategy 42 * - Stream 43 * - InputSource 44 * - Usage. 45 */ 46 template <typename Key> 47 class Collection : public std::map<Key, Element<Key> *> 48 { 49 private: 50 typedef Element<Key> T; 51 typedef typename std::map<Key, T *>::iterator CollectionIterator; 52 typedef typename std::map<Key, T *>::const_iterator CollectionConstIterator; 53 54 public: 55 Collection() 56 { 57 collectionSupported(); 58 } 59 60 /** 61 * Add a policy element to the collection. Policy elements are streams, strategies, input 62 * sources, ... Compile time error generated if called with not supported collection. 63 * It also set the key as the unique identifier of the policy element. 64 * 65 * @tparam Key indexing the collection of policy element. 66 * @param[in] name of the policy element to find. 67 * @param[in] key to be used to index this new policy element. 68 * 69 * @return NO_ERROR if the policy element has been successfully added to the collection. 70 */ 71 status_t add(const std::string &name, Key key) 72 { 73 if ((*this).find(key) != (*this).end()) { 74 ALOGW("%s: element %s already added", __FUNCTION__, name.c_str()); 75 return BAD_VALUE; 76 } 77 (*this)[key] = new T(name); 78 ALOGD("%s: adding element %s to collection", __FUNCTION__, name.c_str()); 79 return (*this)[key]->setIdentifier(key); 80 } 81 82 /** 83 * Get a policy element from the collection by its key. Policy elements are streams, strategies, 84 * input sources, ... Compile time error generated if called with not supported collection. 85 * 86 * @tparam Key indexing the collection of policy element. 87 * @param[in] key of the policy element to find. 88 * 89 * @return valid pointer on policy element if found, NULL otherwise. 90 */ 91 T *get(Key key) const 92 { 93 CollectionConstIterator it = (*this).find(key); 94 return (it == (*this).end()) ? NULL : it->second; 95 } 96 97 /** 98 * Find a policy element from the collection by its name. Policy elements are streams, 99 * strategies, input sources, ... 100 * Compile time error generated if called with not supported collection. 101 * 102 * @tparam Key indexing the collection of policy element. 103 * @param[in] name of the policy element to find. 104 * @param[in] elementsMap maps of policy elements to search into. 105 * 106 * @return valid pointer on element if found, NULL otherwise. 107 */ 108 T *findByName(const std::string &name) const 109 { 110 111 CollectionConstIterator it; 112 for (it = (*this).begin(); it != (*this).end(); ++it) { 113 T *element = it->second; 114 if (element->getName() == name) { 115 return element; 116 } 117 } 118 return NULL; 119 } 120 121 /** 122 * Removes all the elements from the list and destroy them. 123 */ 124 void clear() 125 { 126 CollectionIterator it; 127 for (it = (*this).begin(); it != (*this).end(); ++it) { 128 delete it->second; 129 } 130 (*this).clear(); 131 } 132 133 private: 134 /** 135 * provide a compile time error if no specialization is provided for a given type. 136 * 137 * @tparam T: type of the policyElement. Policy Element supported are: 138 * - Strategy 139 * - Stream 140 * - InputSource 141 * - Usage. 142 */ 143 struct collectionSupported; 144 }; 145 146 template <> 147 struct Collection<audio_stream_type_t>::collectionSupported {}; 148 template <> 149 struct Collection<std::string>::collectionSupported {}; 150 template <> 151 struct Collection<audio_usage_t>::collectionSupported {}; 152 template <> 153 struct Collection<audio_source_t>::collectionSupported {}; 154 template <> 155 struct Collection<routing_strategy>::collectionSupported {}; 156 157 typedef Collection<routing_strategy> StrategyCollection; 158 typedef Collection<audio_stream_type_t> StreamCollection; 159 typedef Collection<audio_usage_t> UsageCollection; 160 typedef Collection<audio_source_t> InputSourceCollection; 161 162 } // namespace audio_policy 163 } // namespace android 164