// Copyright 2020-2024 Jeisson Hidalgo-Cespedes. ECCI-UCR. CC BY 4.0 #ifndef PRODUCER_HPP #define PRODUCER_HPP #include #include "Queue.hpp" #include "Thread.hpp" /** * @brief A template that generates abstract base classes for Producers * Producers are execution threads. They create elements that are pushed * to a queue. These elements will be popped by a consumer thread. */ template class Producer : public virtual Thread { /// Objects of this class cannot be copied DISABLE_COPY(Producer); protected: /// This thread will produce for this queue Queue* producingQueue; public: /// Constructor explicit Producer(Queue* producingQueue = nullptr) : producingQueue(producingQueue) { } /// Destructor virtual ~Producer() { } /// Get access to the queue where this thread will produce inline Queue* getProducingQueue() { return this->producingQueue; } /// Set the queue where this thread will produce elements inline void setProducingQueue(Queue* producingQueue) { this->producingQueue = producingQueue; } /// Add to the queue the produced data unit virtual void produce(const DataType& data) { assert(this->producingQueue); this->producingQueue->enqueue(data); } }; #endif // PRODUCER_HPP