|
Boost Users : |
From: Douglas Gregor (gregod_at_[hidden])
Date: 2002-12-10 08:56:34
On Tuesday 10 December 2002 04:48 am, Malcolm Smith wrote:
> I read Boost.Function info and it sounds like part of the solution.
>
> Is there a way of associating a key with a container of *different*
> boost.Function's ?
Not directly. How are you planning to invoke functions in this map? You gave
the example:
// I know this is not valid code but it explains what I'm after
typedef std::map<int, *pf(...)> StateFunctions;
void Method1(void);
int Method2(TCustomClass &MyClass);
StateFunctions[1] = Method1;
StateFunctions[2] = Method2;
My question is: do you really want the parameter lists to be different for
StateFunctions[1] and StateFunctions[2], meaning that when calling you might
need something like:
foo = StateFunctions[x];
if (foo has arity zero)
foo();
else if (foo has arity one with type TCustomClass)
foo(herClass);
else if (...)
etc. etc.
?
Or are all the parameters to the functors known when they are put into the
StateFunctions map? Then what you really want is something that achieves the
desired effect:
StateFunctions[1] = Method1;
StateFunctions[2] = something that calls Method2 with parameter herClass
If it's the latter case, you've run into the classic Boost.Bind/Boost.Function
(or Boost.Lambda/Boost.Function, if your compiler can handle it) scenario :)
Then you want:
typedef std::map<int, boost::function<void()> > StateFunctions;
StateFunctions[1] = &Method1;
StateFunctions[2] = boost::bind(&Method2, boost::ref(&herClass));
int x = something();
StateFunctions[x](); // call the appropriate state function
If it's the former case (different parameter lists), then there's no direct
"Boost way" to do this that I know of.
Doug
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