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