On Thu, Sep 01, 2011 at 03:54:03PM -0400, Paul Davis
wrote:
  On Thu, Sep 1, 2011 at 2:37 PM, Fons
Adriaensen<fons(a)linuxaudio.org>  wrote:
  That's assuming that the event's virtual
execute() has access to
 all it needs. In all cases I've encountered that is not the case:
 the event triggers something in the context where it is received
 and processing it requires access to that context's data.
 It's a problem for which I don't know a clean C++ solution. 
 depending on
the exact type of thing you're talking about, isn't this
 is place for closures, functors, etc. etc. ? 
 Yes, but
 1. I find functor syntax extremely clumsy, involving the creation of
     a specific derived functor class (from a template class) for each
     one you need.
 2. AFAIK (using the terminology of<http://www.newty.de/fpt/functor.html>,
     you can't have a TSpecificFunctor member in the event class and
     initialise it, it has to be a TSpecificFunctor*. Which in turn
     means that at the sender side you either have instances of all
     possibly required functor classes available and assign the
     TSpecificFunctor* in the event from one of them, or you have to
     use new() to allocate one. The former is extremely clumsy, and
     the latter shouldn't be done in a RT context.
 What I'm missing in C++ is a built-in 'functor' type that can
 simply be assigned from any object::method, with the user being
 responsible for the existence of the object and for supplying the
 right arguments at the time the functor is called.
 
boost::function<void(void)> serves this purpose for me in jass. To
create and pass a functor that assigns a new auditor generator to the
one in the engine, and then tells it to play i do for example:
write_blocking_command(assign(engine_.auditor_gen, p));
write_blocking_command(boost::bind(&engine::play_auditor,
boost::ref(engine_))); assign() is just a utility template to make
creating functors that do assignments easier.. boost::bind is used to
make all passed functors 0-ary (e.g for binding member functions to
their instance or binding arguments to the functor.. and
write_blocking_command is just a utility function that disables the GUI
until the acknowledgement from the engine has come back to the GUI,, The
command ringbuffer is just a ringbuffer holding
boost::fucntion<void(void)> objects..
typedef ringbuffer<boost::function<void(void)> > command_ringbuffer;
Examples from here: