#ifndef QUEUE_HPP__
#define QUEUE_HPP__

#include <queue>
#include <mutex>
#include <condition_variable>

template <typename T>
class SharedQueue {
public:
    SharedQueue() = default;

    void set_push_callback(std::function<void()> callback) 
    {
        std::lock_guard<std::mutex> lock(mutex_);
        push_callback_ = callback;
    }

    void push(const T& item) {
        std::lock_guard<std::mutex> lock(mutex_);
        queue_.push(item);
        cond_var_.notify_one();
        if (push_callback_) {  // 触发回调
            push_callback_();
        }
    }

    void push(T&& item) {
        std::lock_guard<std::mutex> lock(mutex_);
        queue_.push(std::move(item));
        cond_var_.notify_one();
        if (push_callback_) {  // 触发回调
            push_callback_();
        }
    }

    bool try_pop(T& item) {
        std::lock_guard<std::mutex> lock(mutex_);
        if (queue_.empty()) {
            return false;
        }
        item = std::move(queue_.front());
        queue_.pop();
        return true;
    }

    T wait_pop() {
        std::unique_lock<std::mutex> lock(mutex_);
        cond_var_.wait(lock, [this] { return !queue_.empty(); });
        T item = std::move(queue_.front());
        queue_.pop();
        return item;
    }

    bool empty() const {
        std::lock_guard<std::mutex> lock(mutex_);
        return queue_.empty();
    }

    size_t size() const {
        std::lock_guard<std::mutex> lock(mutex_);
        return queue_.size();
    }

private:
    mutable std::mutex mutex_;
    std::queue<T> queue_;
    std::condition_variable cond_var_;
    // 回调函数，用于通知节点的条件变量
    std::function<void()> push_callback_;
};

#endif  // QUEUE_HPP__