Boost logo

Boost :

From: Dean Michael Berris (mikhailberis_at_[hidden])
Date: 2006-10-19 12:54:35


Hi Sohail!

On 10/20/06, Sohail Somani <s.somani_at_[hidden]> wrote:
> >
> > If you need to call multiple methods, you can use the invoker
> > interface to the dispatcher -- but still using single dispatch. You
> > can even use Boost.Signals to register a set of methods and register
> > it to the dispatcher on a single index.
>
> Cool! Can it do Common Lisp-like dispatch where it calls one function if
> 0 <= x < 5 and another if 5 <= x < 10?

Actually, yes! :D

The following (incomplete) example below should be sufficient:

struct function_chooser {
  int operator() (int i) const {
    if ((i >= 0) && (i < 5))
      return 0;
    if ((i >= 5) && (i < 10))
      return 5;
    return 11;
  };
};

typedef boost::dispatch::dispatcher<void(), int,
  boost::dispatch::always_true_validator<int>,
  function_chooser> dispatcher_t ;
dispatcher_t d;
d[0] = &function_1;
d[5] = &function_2;
d[11] = &default_function;

int input;
std::cin >> input;
d[input];

HTH! :)

-- 
Dean Michael C. Berris
C++ Software Architect
Orange and Bronze Software Labs, Ltd. Co.
web: http://software.orangeandbronze.com/
email: dean_at_[hidden]
mobile: +63 928 7291459
phone: +63 2 8943415
other: +1 408 4049532
blogs: http://mikhailberis.blogspot.com http://3w-agility.blogspot.com
http://cplusplus-soup.blogspot.com

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