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/core/request_multiplexer.h"
21 #include "chre/platform/assert.h"
22
23 namespace chre {
24
25 template <typename RequestType>
addRequest(const RequestType & request,size_t * index,bool * maximalRequestChanged)26 bool RequestMultiplexer<RequestType>::addRequest(const RequestType &request,
27 size_t *index,
28 bool *maximalRequestChanged) {
29 CHRE_ASSERT_NOT_NULL(index);
30 CHRE_ASSERT(maximalRequestChanged);
31
32 bool requestStored = mRequests.push_back(request);
33 if (requestStored) {
34 *index = (mRequests.size() - 1);
35 *maximalRequestChanged = mCurrentMaximalRequest.mergeWith(request);
36 }
37
38 return requestStored;
39 }
40
41 template <typename RequestType>
addRequest(RequestType && request,size_t * index,bool * maximalRequestChanged)42 bool RequestMultiplexer<RequestType>::addRequest(RequestType &&request,
43 size_t *index,
44 bool *maximalRequestChanged) {
45 CHRE_ASSERT_NOT_NULL(index);
46 CHRE_ASSERT(maximalRequestChanged);
47
48 bool requestStored = mRequests.push_back(std::move(request));
49 if (requestStored) {
50 *index = (mRequests.size() - 1);
51 *maximalRequestChanged = mCurrentMaximalRequest.mergeWith(mRequests.back());
52 }
53
54 return requestStored;
55 }
56
57 template <typename RequestType>
updateRequest(size_t index,const RequestType & request,bool * maximalRequestChanged)58 void RequestMultiplexer<RequestType>::updateRequest(
59 size_t index, const RequestType &request, bool *maximalRequestChanged) {
60 CHRE_ASSERT(maximalRequestChanged);
61 CHRE_ASSERT(index < mRequests.size());
62
63 if (index < mRequests.size()) {
64 mRequests[index] = request;
65 updateMaximalRequest(maximalRequestChanged);
66 }
67 }
68
69 template <typename RequestType>
updateRequest(size_t index,RequestType && request,bool * maximalRequestChanged)70 void RequestMultiplexer<RequestType>::updateRequest(
71 size_t index, RequestType &&request, bool *maximalRequestChanged) {
72 CHRE_ASSERT_NOT_NULL(maximalRequestChanged);
73 CHRE_ASSERT(index < mRequests.size());
74
75 if (index < mRequests.size()) {
76 mRequests[index] = std::move(request);
77 updateMaximalRequest(maximalRequestChanged);
78 }
79 }
80
81 template <typename RequestType>
removeRequest(size_t index,bool * maximalRequestChanged)82 void RequestMultiplexer<RequestType>::removeRequest(
83 size_t index, bool *maximalRequestChanged) {
84 CHRE_ASSERT_NOT_NULL(maximalRequestChanged);
85 CHRE_ASSERT(index < mRequests.size());
86
87 if (index < mRequests.size()) {
88 mRequests.erase(index);
89 updateMaximalRequest(maximalRequestChanged);
90 }
91 }
92
93 template <typename RequestType>
removeAllRequests(bool * maximalRequestChanged)94 void RequestMultiplexer<RequestType>::removeAllRequests(
95 bool *maximalRequestChanged) {
96 CHRE_ASSERT_NOT_NULL(maximalRequestChanged);
97
98 mRequests.clear();
99 updateMaximalRequest(maximalRequestChanged);
100 }
101
102 template <typename RequestType>
getRequests()103 const DynamicVector<RequestType> &RequestMultiplexer<RequestType>::getRequests()
104 const {
105 return mRequests;
106 }
107
108 template <typename RequestType>
getCurrentMaximalRequest()109 const RequestType &RequestMultiplexer<RequestType>::getCurrentMaximalRequest()
110 const {
111 return mCurrentMaximalRequest;
112 }
113
114 template <typename RequestType>
updateMaximalRequest(bool * maximalRequestChanged)115 void RequestMultiplexer<RequestType>::updateMaximalRequest(
116 bool *maximalRequestChanged) {
117 RequestType maximalRequest;
118 for (size_t i = 0; i < mRequests.size(); i++) {
119 maximalRequest.mergeWith(mRequests[i]);
120 }
121
122 *maximalRequestChanged =
123 !mCurrentMaximalRequest.isEquivalentTo(maximalRequest);
124 if (*maximalRequestChanged) {
125 mCurrentMaximalRequest = std::move(maximalRequest);
126 }
127 }
128
129 } // namespace chre
130
131 #endif // CHRE_CORE_REQUEST_MULTIPLEXER_IMPL_H_
132