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