Collection.h revision 65c3781db3443531deacecfbda5c7e7e82868a34
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
31namespace android
32{
33namespace 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 */
46template <typename Key>
47class Collection : public std::map<Key, Element<Key> *>
48{
49private:
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
54public:
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
133private:
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
146template <>
147struct Collection<audio_stream_type_t>::collectionSupported {};
148template <>
149struct Collection<std::string>::collectionSupported {};
150template <>
151struct Collection<audio_usage_t>::collectionSupported {};
152template <>
153struct Collection<audio_source_t>::collectionSupported {};
154template <>
155struct Collection<routing_strategy>::collectionSupported {};
156
157typedef Collection<routing_strategy> StrategyCollection;
158typedef Collection<audio_stream_type_t> StreamCollection;
159typedef Collection<audio_usage_t> UsageCollection;
160typedef Collection<audio_source_t> InputSourceCollection;
161
162} // namespace audio_policy
163} // namespace android
164