I am designing two classes which encapsulate a producer and consumer. One class does single-thread processing, the other (not shown here) two threads.
The producer and consumer are passed to each 'threading class' by template parameters.
*this is passed to the producer to enable a callback upon each message:
template<class PRODUCER, class CONSUMER>
class SingleThread
{
SingleThread() : _prod(*this){}
void processMessage(const char* message, int len)
{
int d = _cons.doSomething(message, len);
// Code omitted
}
PRODUCER<SingleThread<>> _prod; // Cyclical dependency
CONSUMER _cons;
};
Each producer receives the threading class as a callback via template parameter:
template<class THREADING_CALLBACK>
class Producer
{
Producer(THREADING_CALLBACK& cb) : _cb(cb){}
// A lot of code omitted
void receiveMessage(const char* message, int len)
{
_cb.processMessage(message, len);
}
THREADING_CALLBACK& _cb;
};
but I have a problem with circular template dependency. SingleThread takes Producer as a template parameter but Producer requires SingleThread:
SingleThread<Producer<SingleThread<Producer<....>, Consumer>, Consumer>
How should I (re)structure this design?