queue.hh
#ifndef __SDR_QUEUE_HH__
#define __SDR_QUEUE_HH__
#include <list>
#include <map>
#include "buffer.hh"
#include <pthread.h>
#include <iostream>
namespace sdr {
// Forward decl.
class SinkBase;
class DelegateInterface {
public:
virtual void operator() () = 0;
virtual void *instance() = 0;
};
template <class T>
class Delegate: public DelegateInterface
{
public:
Delegate(T *instance, void (T::*func)(void)) : _instance(instance), _function(func) { }
virtual ~Delegate() {}
virtual void operator() () { (_instance->*_function)(); }
virtual void *instance() { return _instance; }
protected:
T *_instance;
void (T::*_function)(void);
};
class Queue
{
public:
class Message {
public:
Message(const RawBuffer &buffer, SinkBase *sink, bool allow_overwrite)
: _buffer(buffer), _sink(sink), _allow_overwrite(allow_overwrite) { }
Message(const Message &other)
: _buffer(other._buffer), _sink(other._sink), _allow_overwrite(other._allow_overwrite) { }
const Message &operator= (const Message &other) {
_buffer = other._buffer;
_sink = other._sink;
_allow_overwrite = other._allow_overwrite;
return *this;
}
inline const RawBuffer &buffer() const { return _buffer; }
inline RawBuffer &buffer() { return _buffer; }
inline SinkBase *sink() const { return _sink; }
inline bool allowOverwrite() const { return _allow_overwrite; }
protected:
RawBuffer _buffer;
SinkBase *_sink;
bool _allow_overwrite;
};
protected:
Queue();
public:
virtual ~Queue();
static Queue &get();
void send(const RawBuffer &buffer, SinkBase *sink, bool allow_overwrite=false);
void start();
void stop();
void wait();
bool isStopped() const;
bool isRunning() const;
template <class T>
void addIdle(T *instance, void (T::*function)(void)) {
_idle.push_back(new Delegate<T>(instance, function));
}
template <class T>
void remIdle(T *instance) {
std::list<DelegateInterface *>::iterator item = _idle.begin();
while (item != _idle.end()) {
if ( (*item)->instance() == ((void *)instance)) {
item = _idle.erase(item);
} else {
item++;
}
}
}
template <class T>
void addStart(T *instance, void (T::*function)(void)) {
_onStart.push_back(new Delegate<T>(instance, function));
}
template <class T>
void remStart(T *instance) {
std::list<DelegateInterface *>::iterator item = _onStart.begin();
while (item != _onStart.end()) {
if ( (*item)->instance() == ((void *)instance)) {
item = _onStart.erase(item);
} else {
item++;
}
}
}
template <class T>
void addStop(T *instance, void (T::*function)(void)) {
_onStop.push_back(new Delegate<T>(instance, function));
}
template <class T>
void remStop(T *instance) {
std::list<DelegateInterface *>::iterator item = _onStop.begin();
while (item != _onStop.end()) {
if ( (*item)->instance() == ((void *)instance)) {
item = _onStop.erase(item);
} else {
item++;
}
}
}
protected:
void _main();
void _signalIdle();
void _signalStart();
void _signalStop();
protected:
bool _running;
pthread_t _thread;
pthread_mutex_t _queue_lock;
pthread_cond_t _queue_cond;
std::list<Message> _queue;
std::list<DelegateInterface *> _idle;
std::list<DelegateInterface *> _onStart;
std::list<DelegateInterface *> _onStop;
private:
static Queue *_instance;
static void *__thread_start(void *ptr);
};
}
#endif // __SDR_QUEUE_HH__
sdr::Queue::_onStop
std::list< DelegateInterface * > _onStop
Stop event callbacks.
Definition: queue.hh:209
sdr::Queue::Message::_sink
SinkBase * _sink
The destination.
Definition: queue.hh:85
sdr::Queue::_queue
std::list< Message > _queue
The message queue.
Definition: queue.hh:203
sdr::RawBuffer
Base class of all buffers, represents an untyped array of bytes.
Definition: buffer.hh:32
sdr::Queue::send
void send(const RawBuffer &buffer, SinkBase *sink, bool allow_overwrite=false)
Adds a buffer and its receiver to the queue.
Definition: queue.cc:36
sdr::Queue::Message
The internal used message type.
Definition: queue.hh:57
sdr::Queue::wait
void wait()
Wait for the queue to exit the queue loop.
Definition: queue.cc:70
sdr::DelegateInterface::instance
virtual void * instance()=0
Returns the instance of the delegate.
sdr::Queue::_queue_lock
pthread_mutex_t _queue_lock
The queue mutex.
Definition: queue.hh:198
sdr::Delegate
Specific delegate to a method of an object .
Definition: queue.hh:27
sdr::Queue::isStopped
bool isStopped() const
Returns true if the queue loop is stopped.
Definition: queue.cc:46
sdr::Queue::isRunning
bool isRunning() const
Returns true if the queue loop is running.
Definition: queue.cc:51
sdr::Delegate::Delegate
Delegate(T *instance, void(T::*func)(void))
Constructs a delegate to the method func of the instance instance.
Definition: queue.hh:31
sdr::Queue::_signalStop
void _signalStop()
Emits the stop signal.
Definition: queue.cc:144
sdr::Queue::_idle
std::list< DelegateInterface * > _idle
Idle event callbacks.
Definition: queue.hh:205
sdr
Definition: autocast.hh:8
sdr::Queue
Central message queue (singleton).
Definition: queue.hh:53
sdr::Delegate::_instance
T * _instance
The instance.
Definition: queue.hh:41
sdr::Queue::stop
void stop()
Signals the queue to stop processing.
Definition: queue.cc:64
sdr::Delegate::instance
virtual void * instance()
Returns the instance of the delegate.
Definition: queue.hh:37
sdr::Queue::Message::Message
Message(const Message &other)
Copy constructor.
Definition: queue.hh:63
sdr::Queue::addIdle
void addIdle(T *instance, void(T::*function)(void))
Adds a callback to the idle event.
Definition: queue.hh:127
sdr::Queue::Message::buffer
const RawBuffer & buffer() const
Returns the buffer of the message.
Definition: queue.hh:73
sdr::Queue::Message::allowOverwrite
bool allowOverwrite() const
If true, the sender allows to overwrite the content of the buffer.
Definition: queue.hh:79
sdr::Queue::_thread
pthread_t _thread
If _parallel is true, the thread of the queue loop.
Definition: queue.hh:196
sdr::Queue::addStop
void addStop(T *instance, void(T::*function)(void))
Adds a callback to the stop event.
Definition: queue.hh:165
sdr::Queue::Message::operator=
const Message & operator=(const Message &other)
Assignment operator.
Definition: queue.hh:66
sdr::Delegate::_function
void(T::* _function)(void)
The method.
Definition: queue.hh:43
sdr::Queue::~Queue
virtual ~Queue()
Destructor.
Definition: queue.cc:30
sdr::DelegateInterface
Interface of a delegate.
Definition: queue.hh:17
sdr::Queue::Message::sink
SinkBase * sink() const
Returns the destination of the message.
Definition: queue.hh:77
sdr::Queue::start
void start()
Enters the queue loop, if parallel=true was passed to get, exec will execute the queue loop in a sepa...
Definition: queue.cc:57
sdr::Queue::remStop
void remStop(T *instance)
Removes all callbacks of the given instance from the stop signal.
Definition: queue.hh:171
sdr::Delegate::~Delegate
virtual ~Delegate()
Destructor.
Definition: queue.hh:33
sdr::Queue::Message::buffer
RawBuffer & buffer()
Returns the buffer of the message.
Definition: queue.hh:75
sdr::SinkBase
Basic interface of all Sinks.
Definition: node.hh:174
sdr::Queue::remIdle
void remIdle(T *instance)
Removes all callbacks of the given instance from the idle signal.
Definition: queue.hh:133
sdr::Queue::_onStart
std::list< DelegateInterface * > _onStart
Start event callbacks.
Definition: queue.hh:207
sdr::DelegateInterface::operator()
virtual void operator()()=0
Call back interface.
sdr::Queue::_main
void _main()
The actual queue loop.
Definition: queue.cc:84
sdr::Queue::Message::Message
Message(const RawBuffer &buffer, SinkBase *sink, bool allow_overwrite)
Constructor.
Definition: queue.hh:60
sdr::Queue::_running
bool _running
While this is true, the queue loop is executed.
Definition: queue.hh:194
sdr::Queue::Message::_allow_overwrite
bool _allow_overwrite
If true, the sender allows to overwrite the buffer.
Definition: queue.hh:87
sdr::Delegate::operator()
virtual void operator()()
Callback, simply calls the method of the instance given to the constructor.
Definition: queue.hh:35
sdr::Queue::_signalIdle
void _signalIdle()
Emits the idle signal.
Definition: queue.cc:128
sdr::Queue::_queue_cond
pthread_cond_t _queue_cond
The queue condition.
Definition: queue.hh:200
sdr::Queue::_signalStart
void _signalStart()
Emits the start signal.
Definition: queue.cc:136
sdr::Queue::remStart
void remStart(T *instance)
Removes all callbacks of the given instance from the start signal.
Definition: queue.hh:152
sdr::Queue::Queue
Queue()
Hidden constructor, use get to get the singleton instance.
Definition: queue.cc:22
sdr::Queue::addStart
void addStart(T *instance, void(T::*function)(void))
Adds a callback to the start event.
Definition: queue.hh:146
sdr::Queue::Message::_buffer
RawBuffer _buffer
The buffer being send.
Definition: queue.hh:83