Factory for objects w/o default constructor (tmp?)

S

Shawn McGrath

I'm writing a generic message handler system with two basic classes:
template <typename T>
struct MessageHandler //singleton-ized elsewhere
{
...
void publish(...);
void subscribe(MessageInterface<T> *);
void unsubscribe(MessageInterface<T> *);
private:
std::vector<MessageInterface<T> *> vec;
};

and obviously:
template <typename T>
struct MessageInterface
{
...
void receiveMessage(const T *msg);
};

right now MessageHandler::publish() looks like:
publish(T *msg) { std::for_each(vec... receiveMessage...);

This works fine, however it's up to the caller of publish() to create
the message. I'd rather put message creation inside the MessageHandler
class. The difficulty with doing that is not all T's will have a
default constructor.

I was looking into how boost::signals work, but that defines the
arguments at the time of handler class creation, and it's overall quite
confusing - is there a simpler way?

If not, can I apply the boost::signal method to make publish() a
template member function - if yes, is there a way to automatically
infer the template parameters based off the arguments? (ie so I don't
have to do:

message_handler.publish<int>( x );

and just have it know that I'm using publish<int> by the type of the
argument passed to publish())

Thanks a lot.
-Shawn.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top