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:ublish() 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.
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:ublish() 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.