#pragma once /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ /** * @file * @copyright MPLv2 */ #include #include #include #include #include namespace BC{ /** * @class BasicMessageQueue * * This class encapsulates shared storage which is a queue and protect it * with a mutex. In Addition there is a condition variable to notify other * threads if there is new data in the queue. */ class BasicMessageQueue { private: std::queue> q; std::mutex mx; std::condition_variable condVar; public: /** * @brief default constructor for class BasicMessageQueue */ BasicMessageQueue(); /** * @brief appends given message to queue * * The storage is protected by a mutex. Makes a notify_one call to inform * in waiting thread that there is a new message in the queue. * * @param msg - incoming message */ void appendMessage( std::unique_ptr msg ); /** * @brief gets the fron message from the queue * * This method gets the front message in the queue and deletes it from the * queue. The storage is protected by a mutex. If the queue is empty the * function throws an exception. * * @result msg - returns the front message from the queue * @throws length_error - if queue is empty this method throws length_error exception */ std::unique_ptr getMessage(); /** * @brief waits for new message to be added * * If the queue is empty this method waits for a notfication on the condition variable. * It returns the front message and deletes it from the queue. This action is protected * by a mutex. * * @result msg - returns the first/front message in the queue * @throws length_error - if queue is empty this method throws length_error exception */ std::unique_ptr waitForNewMessage(); /** * @brief method size * * @result size - current size of the message queue */ unsigned int size(); }; //BasicMessageQueue }; // namespace BC