2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Jan Olszak <j.olszak@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
21 * @author Jan Olszak (j.olszak@samsung.com)
22 * @brief Managing the queue of messages carrying any kind of data
25 #ifndef COMMON_IPC_INTERNALS_MESSAGE_QUEUE_HPP
26 #define COMMON_IPC_INTERNALS_MESSAGE_QUEUE_HPP
28 #include "ipc/exception.hpp"
29 #include "ipc/internals/eventfd.hpp"
30 #include "logger/logger.hpp"
40 * Class for managing a queue of Requests carrying any data
42 template<typename RequestIdType>
45 RequestQueue() = default;
47 RequestQueue(const RequestQueue&) = delete;
48 RequestQueue& operator=(const RequestQueue&) = delete;
51 Request(const Request& other) = delete;
52 Request& operator=(const Request&) = delete;
54 Request(Request&&) = default;
55 Request(const RequestIdType requestID, const std::shared_ptr<void>& data)
56 : requestID(requestID),
60 template<typename DataType>
61 std::shared_ptr<DataType> get()
63 return std::static_pointer_cast<DataType>(data);
66 RequestIdType requestID;
67 std::shared_ptr<void> data;
71 * @return event's file descriptor
76 * @return is the queue empty
81 * Push data to the queue
83 * @param requestID request type
84 * @param data data corresponding to the request
86 void push(const RequestIdType requestID,
87 const std::shared_ptr<void>& data = nullptr);
90 * @return get the data from the next request
95 * Remove elements from the queue when the predicate returns true
97 * @param predicate condition
98 * @return was anything removed
100 template<typename Predicate>
101 bool removeIf(Predicate predicate);
104 std::list<Request> mRequests;
108 template<typename RequestIdType>
109 int RequestQueue<RequestIdType>::getFD() const
111 return mEventFD.getFD();
114 template<typename RequestIdType>
115 bool RequestQueue<RequestIdType>::isEmpty() const
117 return mRequests.empty();
120 template<typename RequestIdType>
121 void RequestQueue<RequestIdType>::push(const RequestIdType requestID,
122 const std::shared_ptr<void>& data)
124 Request request(requestID, data);
125 mRequests.push_back(std::move(request));
129 template<typename RequestIdType>
130 typename RequestQueue<RequestIdType>::Request RequestQueue<RequestIdType>::pop()
133 if (mRequests.empty()) {
134 LOGE("Request queue is empty");
135 throw IPCException("Request queue is empty");
137 Request request = std::move(mRequests.front());
138 mRequests.pop_front();
142 template<typename RequestIdType>
143 template<typename Predicate>
144 bool RequestQueue<RequestIdType>::removeIf(Predicate predicate)
146 auto it = std::find_if(mRequests.begin(), mRequests.end(), predicate);
147 if (it == mRequests.end()) {
152 it = mRequests.erase(it);
153 it = std::find_if(it, mRequests.end(), predicate);
154 } while (it != mRequests.end());
161 #endif // COMMON_IPC_INTERNALS_MESSAGE_QUEUE_HPP