* Copyright (c) 2025-2026 IB-Robot Group & openEuler Embedded SIG & openharmony-robot sig_RoboFrame.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <condition_variable>
#include <mutex>
#include <optional>
#include <queue>
#include <stdexcept>
namespace ibmw::common::util {
class BlockQueueStoppedException : public std::runtime_error {
public:
BlockQueueStoppedException() : std::runtime_error("BlockQueue is stopped") {}
};
template <class T>
class BlockQueue {
public:
BlockQueue() = default;
~BlockQueue() { Stop(); }
BlockQueue(const BlockQueue&) = delete;
BlockQueue& operator=(const BlockQueue&) = delete;
void Enqueue(const T& item)
{
std::unique_lock<std::mutex> lck(mutex_);
if (!running_flag_) {
throw BlockQueueStoppedException();
}
queue_.emplace(item);
cond_.notify_one();
}
void Enqueue(T&& item)
{
std::unique_lock<std::mutex> lck(mutex_);
if (!running_flag_) {
throw BlockQueueStoppedException();
}
queue_.emplace(std::move(item));
cond_.notify_one();
}
T Dequeue()
{
std::unique_lock<std::mutex> lck(mutex_);
cond_.wait(lck, [this] { return !queue_.empty() || !running_flag_; });
if (queue_.empty()) {
throw BlockQueueStoppedException();
}
T item = std::move(queue_.front());
queue_.pop();
return item;
}
std::optional<T> TryDequeue()
{
std::lock_guard<std::mutex> lck(mutex_);
if (queue_.empty()) [[unlikely]] {
return std::nullopt;
}
T item = std::move(queue_.front());
queue_.pop();
return item;
}
void Stop()
{
std::unique_lock<std::mutex> lck(mutex_);
running_flag_ = false;
cond_.notify_all();
}
size_t Size() const
{
std::lock_guard<std::mutex> lck(mutex_);
return queue_.size();
}
bool IsRunning() const
{
std::lock_guard<std::mutex> lck(mutex_);
return running_flag_;
}
protected:
mutable std::mutex mutex_;
std::condition_variable cond_;
std::queue<T> queue_;
bool running_flag_ = true;
};
}