Boost logo

Boost :

From: Rene Rivera (grafik.list_at_[hidden])
Date: 2006-06-11 22:45:10


Dean Michael Berris wrote:
> On 6/8/06, Peter Dimov <pdimov_at_[hidden]> wrote:
>> Dean Michael Berris wrote:
>>
>>> //set up a registry
>>> dispatch::dispatcher<void (), int> callbacks;
>>>
>>> // register a nullary free function f to index 0
>>> callbacks[0] = f;
>>>
>>> // bind arguments to a single argument function p to index 1
>>> callbacks[1] = boost::bind(p, 1);
>>>
>>> // get user input then execute appropriate callback
>>> int i;
>>> cin >> i;
>>> callbacks[i]();
>> This can be done with map< int, function<void()> >.
>
> Yes, this is just one of the things that the dispatcher can do. The
> current implementation (lacking documentation) can be downloaded from
> http://tinyurl.com/px33v --

Well documentation would be what most of use would really like to see :-)

Some days ago I rewrote a message dispatching system that has similar
interface to what you present, and implemented on top of Boost.Signals.
Some examples:

struct is_equal_message : message<
   is_equal_message, boost::tuple<int,bool>
   bool (int, double)
> {};
namespace {
   is_equal_message cosnt & is_equal = is_equal_message::get(); }
struct is_even_message : message<
   is_even_message, boost::tuple<int>
   bool (int)
> {};
namespace {
   is_even_message cosnt & is_even = is_even_message::get(); }

bool my_equal(int a, double b) {
   return double(a) == b;
}
bool my_even(int a) {
   return a%2 == 0;
}

int main (int arc, char** argv) {
   dispatcher d;
   d
     << is_equal[1,true].connect(boost::bind(my_equal,_2,_3))
     << is_equal[1,false].connect(boost::bind(my_equal,_3,_2))
     << is_even[1].connect(boost::bind(my_even,_2));
   std::cout
     << ( d[is_equal,1,true](12,12) ? "T" : "F" )
     << ( d[is_equal,1,false](12,3.14) ? "T" : "F" )
     << ( d[is_even,1](2) ? "T" : "F" )
     << ( d[is_even,1](1) ? "T" : "F" )
     << std::endl;
   return 0;
}

Does your code support such varied registration and dispatching?

> there are a few other uses, including a
> means for defining a strategy for index validation. An example of this
> usage is shown below (and can also be seen from the unit test):
[...]
> typedef dispatch::dispatcher<int(), int, require_even_strategy,
> std::vector> dispatcher_t;

Hm, now that's an interesting idea, contract based dispatch interfaces.

-- 
-- Grafik - Don't Assume Anything
-- Redshift Software, Inc. - http://redshift-software.com
-- rrivera/acm.org - grafik/redshift-software.com
-- 102708583/icq - grafikrobot/aim - grafikrobot/yahoo

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk