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(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>
updateRequest(size_t index,const RequestType & request,bool * maximalRequestChanged)42 void RequestMultiplexer<RequestType>::updateRequest(
43     size_t index, const RequestType& request, bool *maximalRequestChanged) {
44   CHRE_ASSERT(maximalRequestChanged);
45   CHRE_ASSERT(index < mRequests.size());
46 
47   if (index < mRequests.size()) {
48     mRequests[index] = request;
49     updateMaximalRequest(maximalRequestChanged);
50   }
51 }
52 
53 template<typename RequestType>
removeRequest(size_t index,bool * maximalRequestChanged)54 void RequestMultiplexer<RequestType>::removeRequest(
55     size_t index, bool *maximalRequestChanged) {
56   CHRE_ASSERT(maximalRequestChanged);
57   CHRE_ASSERT(index < mRequests.size());
58 
59   if (index < mRequests.size()) {
60     mRequests.erase(index);
61     updateMaximalRequest(maximalRequestChanged);
62   }
63 }
64 
65 template<typename RequestType>
removeAllRequests(bool * maximalRequestChanged)66 void RequestMultiplexer<RequestType>::removeAllRequests(
67     bool *maximalRequestChanged) {
68   CHRE_ASSERT(maximalRequestChanged);
69 
70   mRequests.clear();
71   updateMaximalRequest(maximalRequestChanged);
72 }
73 
74 template<typename RequestType>
75 const DynamicVector<RequestType>&
getRequests()76     RequestMultiplexer<RequestType>::getRequests() const {
77   return mRequests;
78 }
79 
80 template<typename RequestType>
getCurrentMaximalRequest()81 const RequestType& RequestMultiplexer<RequestType>::getCurrentMaximalRequest()
82     const {
83   return mCurrentMaximalRequest;
84 }
85 
86 template<typename RequestType>
updateMaximalRequest(bool * maximalRequestChanged)87 void RequestMultiplexer<RequestType>::updateMaximalRequest(
88     bool *maximalRequestChanged) {
89   RequestType maximalRequest;
90   for (size_t i = 0; i < mRequests.size(); i++) {
91     maximalRequest.mergeWith(mRequests[i]);
92   }
93 
94   *maximalRequestChanged = !mCurrentMaximalRequest.isEquivalentTo(
95       maximalRequest);
96   if (*maximalRequestChanged) {
97     mCurrentMaximalRequest = maximalRequest;
98   }
99 }
100 
101 }  // namespace chre
102 
103 #endif  // CHRE_CORE_REQUEST_MULTIPLEXER_IMPL_H_
104