|
Boost Users : |
Subject: Re: [Boost-users] Auto dispatch metaprogramming tricks
From: strasser_at_[hidden]
Date: 2010-05-21 09:30:21
Zitat von Alexander Lamaison <awl03_at_[hidden]>:
> I would like to dispatch messages to a class based simply on the *presence*
> of a message handler method.
>
> Currently, the classes maintain an MPL vector of the messages they handle
> and must implement a on(message<MESSAGE_ID>) method for each one. The
> dispatcher uses this compile-time list to build the dispatching code. This
> means the information is maintained twice and may fall out of sync (e.g.
> adding the handler but forgetting to update the message vector.
>
> Are there any template metaprogramming tricks I can employ to dispatch the
> message to a handler method if it exists and the default handler otherwise?
> All this information is available at compile time. The question is are
> templates are powerful enough to make use of it?
templates are not, and there is no portable way to detect the presence
of a member function.
you can however use ADL for this type of thing, if you don't need the
information whether the message handler is present for anything other
than calling it:
class A{
friend void message(A &this_,message msg){
...
}
};
namespace detail{
template<class T,class Msg>
void message(T &this_,Msg msg){
//default handler
}
template<class T,class Msg>
void dispatch(T &this_,Msg msg){
message(this_,msg);
}
}
A a;
B b;
detail::dispatch(a,msg); //calls message handler of A
detail::dispatch(b,msg); //calls default handler
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net