Home | History | Annotate | Download | only in core
      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_H_
     18 #define CHRE_CORE_REQUEST_MULTIPLEXER_H_
     19 
     20 #include "chre/util/dynamic_vector.h"
     21 #include "chre/util/non_copyable.h"
     22 
     23 namespace chre {
     24 
     25 /**
     26  * This class multiplexes multiple generic requests into one maximal request.
     27  * This is a templated class and the template type is required to implement the
     28  * following API:
     29  *
     30  * 1. RequestType();
     31  *
     32  *     RequestTypes must be default constructable and constructed to a state
     33  *     that is the lowest possible priority for a request. The lowest priority
     34  *     state must be equivalent to being restored to the initial state for the
     35  *     RequestType.
     36  *
     37  * 2. bool isEquivalentTo(const RequestType& request) const;
     38  *
     39  *     Perform a comparison to another request to determine if they are
     40  *     equivalent. This is different from equality in that the request object
     41  *     may have other internal state that makes two requests different, but
     42  *     their net request is considered to be equal. This function returns true
     43  *     if the requests are equivalent.
     44  *
     45  * 3. bool mergeWith(const RequestType& request);
     46  *
     47  *     Merges a request with the current request. This method must set the
     48  *     attributes of the current request to the highest priority attributes of
     49  *     both the current and other. The method returns true if the current
     50  *     request has changed.
     51  */
     52 template<typename RequestType>
     53 class RequestMultiplexer : public NonCopyable {
     54  public:
     55   /**
     56    * Adds a request to the list of requests being managed by this multiplexer.
     57    *
     58    * @param request The request to add to the list.
     59    * @param index A non-null pointer to an index that is populated with the
     60    *              location that the request was added.
     61    * @param maximalRequestChanged A non-null pointer to a bool that is set to
     62    *        true if current maximal request has changed. The user of this API
     63    *        must query the getCurrentMaximalRequest method to get the new
     64    *        maximal request.
     65    * @return Returns false if the request cannot be inserted into the
     66    *         multiplexer.
     67    */
     68   bool addRequest(const RequestType& request, size_t *index,
     69                   bool *maximalRequestChanged);
     70 
     71   /**
     72    * Updates a request in the list of requests being managed by this
     73    * multiplexer.
     74    *
     75    * @param index The index of the request to be updated. This param must fall
     76    *        in the range of indices provided by getRequests().
     77    * @param request The request to update to.
     78    * @param maximalRequestChanged A non-null pointer to a bool that is set to
     79    *        true if the current maximal request has changed. The user of this
     80    *        API must query the getCurrentMaximalRequest() method to get the new
     81    *        maximal request.
     82    */
     83   void updateRequest(size_t index, const RequestType& request,
     84                      bool *maximalRequestChanged);
     85 
     86   /**
     87    * Removes a request from the list of requests being managed by this
     88    * multiplexer.
     89    *
     90    * @param index The index of the request to be removed. This index must fall
     91    *        in the range of indices provided by getRequests().
     92    * @param maximalRequestChanged A non-null pointer to a bool that is set to
     93    *        true if the current maximal request has changed. The user of this
     94    *        API must query the getCurrentMaximalRequest method to get the new
     95    *        maximal request.
     96    */
     97   void removeRequest(size_t index, bool *maximalRequestChanged);
     98 
     99   /*
    100    * Removes all requests managed by this multiplexer. The maximal request will
    101    * change if the multiplexer is not empty.
    102    *
    103    * @param maximalRequestChanged A non-null pointer to a bool that is set to
    104    *        true if the current maximal request has changed.
    105    */
    106   void removeAllRequests(bool *maximalRequestChanged);
    107 
    108   /**
    109    * @return The list of requests managed by this multiplexer.
    110    */
    111   const DynamicVector<RequestType>& getRequests() const;
    112 
    113   /**
    114    * @return Returns the current maximal request.
    115    */
    116   const RequestType& getCurrentMaximalRequest() const;
    117 
    118  private:
    119   //! The list of requests to track.
    120   DynamicVector<RequestType> mRequests;
    121 
    122   //! The current maximal request as generated by this multiplexer.
    123   RequestType mCurrentMaximalRequest;
    124 
    125   /**
    126    * Iterates over all tracked requests and updates the current maximal request
    127    * if it has changed.
    128    *
    129    * @param maximalRequestChanged A non-null pointer to a bool that is set to
    130    *        true if the current maximal request has changed.
    131    */
    132   void updateMaximalRequest(bool *maximalRequestChanged);
    133 };
    134 
    135 }  // namespace chre
    136 
    137 #include "chre/core/request_multiplexer_impl.h"
    138 
    139 #endif  // CHRE_CORE_REQUEST_MULTIPLEXER_H_
    140