1 /*
2 * Copyright (C) 2016 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 CHRE_CORE_REQUEST_MULTIPLEXER_IMPL_H_
18 #define CHRE_CORE_REQUEST_MULTIPLEXER_IMPL_H_
19
20 #include "chre/platform/assert.h"
21
22 namespace chre {
23
24 template<typename RequestType>
addRequest(const RequestType & request,size_t * index,bool * maximalRequestChanged)25 bool RequestMultiplexer<RequestType>::addRequest(const RequestType& request,
26 size_t *index,
27 bool *maximalRequestChanged) {
28 CHRE_ASSERT(index);
29 CHRE_ASSERT(maximalRequestChanged);
30
31 bool requestStored = mRequests.push_back(request);
32 if (requestStored) {
33 *index = (mRequests.size() - 1);
34 *maximalRequestChanged = mCurrentMaximalRequest.mergeWith(request);
35 }
36
37 return requestStored;
38 }
39
40 template<typename RequestType>
updateRequest(size_t index,const RequestType & request,bool * maximalRequestChanged)41 void RequestMultiplexer<RequestType>::updateRequest(
42 size_t index, const RequestType& request, bool *maximalRequestChanged) {
43 CHRE_ASSERT(maximalRequestChanged);
44 CHRE_ASSERT(index < mRequests.size());
45
46 if (index < mRequests.size()) {
47 mRequests[index] = request;
48 updateMaximalRequest(maximalRequestChanged);
49 }
50 }
51
52 template<typename RequestType>
removeRequest(size_t index,bool * maximalRequestChanged)53 void RequestMultiplexer<RequestType>::removeRequest(
54 size_t index, bool *maximalRequestChanged) {
55 CHRE_ASSERT(maximalRequestChanged);
56 CHRE_ASSERT(index < mRequests.size());
57
58 if (index < mRequests.size()) {
59 mRequests.erase(index);
60 updateMaximalRequest(maximalRequestChanged);
61 }
62 }
63
64 template<typename RequestType>
removeAllRequests(bool * maximalRequestChanged)65 void RequestMultiplexer<RequestType>::removeAllRequests(
66 bool *maximalRequestChanged) {
67 CHRE_ASSERT(maximalRequestChanged);
68
69 mRequests.clear();
70 updateMaximalRequest(maximalRequestChanged);
71 }
72
73 template<typename RequestType>
74 const DynamicVector<RequestType>&
getRequests()75 RequestMultiplexer<RequestType>::getRequests() const {
76 return mRequests;
77 }
78
79 template<typename RequestType>
getCurrentMaximalRequest()80 RequestType RequestMultiplexer<RequestType>::getCurrentMaximalRequest() const {
81 return mCurrentMaximalRequest;
82 }
83
84 template<typename RequestType>
updateMaximalRequest(bool * maximalRequestChanged)85 void RequestMultiplexer<RequestType>::updateMaximalRequest(
86 bool *maximalRequestChanged) {
87 RequestType maximalRequest;
88 for (size_t i = 0; i < mRequests.size(); i++) {
89 maximalRequest.mergeWith(mRequests[i]);
90 }
91
92 *maximalRequestChanged = !mCurrentMaximalRequest.isEquivalentTo(
93 maximalRequest);
94 if (*maximalRequestChanged) {
95 mCurrentMaximalRequest = maximalRequest;
96 }
97 }
98
99 } // namespace chre
100
101 #endif // CHRE_CORE_REQUEST_MULTIPLEXER_IMPL_H_
102