 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
hydrajak@yahoo.com Guest
|
Posted: Thu Sep 22, 2005 8:54 am Post subject: How to store Loki Functors in a generic container? |
|
|
Hello group,
This is a pretty basic question....
I'm using Loki Functors and I want to store them in a generic container
(like vector or map). The problem is that each kind of functor is-a
different type because of the template paramaters. So I had the bright
idea to hack Loki and make Functor inheret from a
class FunctorInterface{};
so now I can store a functor in a vector as a FunctorInterface*. Sweet.
However, I can't USE a FunctorInterface because it is an empty class,
so I need to convert the FunctorInterface* back to the right kind of
Functor.
Here is how it is getting used....
Larry l;
Functor<void, TYPELIST_1(string) > fun2(&l,Larry::foo);
CommandManager<LocalExecutionPolicy> cMgr;
cMgr.registerCommand("Larry's Command",fun2);
FunctorInterface* f = cMgr.retreiveCommand("Larry's Command");
int someInt;
cMgr.triggerCmd<void>(f,someInt);
Now as we can see, that last line is a clear error. fun2 takes a string
not an int. if you tried:
fun2(someInt);
you would get a compile error.
Without going into too much detail, here is the policy that is
responsible for executing the functor in CommandManager:
template<typename R,typename P1>
R LocalExecutionPolicy::triggerCmd(Loki::FunctorInterface* f,P1 param1)
{
Loki::Functor<R,TYPELIST_1(P1)>* fun = static_cast<
Loki::Functor( f );
return (*fun)(param1);
}
So my problem is that the static_cast happly converts the f* into a
Functor, but with a TYPELIST_1( int ) instead of a TYPELIST_1( string
) like I want. Now.... I COULD add a virtual descrutor to
FunctorInterface and use a dynamic_cast but:
1) That is slow
2) That is run time error checking, not compile time.
I need some way when I call CommandManager::registerCommand() to save
the type of the TList so that I can compare it later to the
CommandManager::Trigger() call. I would like this all to happen at
compile time so that bad Trigger calls simply won't compile. I have
stared at Chapter 2 of Modern C++ Designs over and over and over and I
think I'm missing the obvious.
Thanks,
Larry E. Ramey
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Andreas Magnusson Guest
|
Posted: Sat Sep 24, 2005 2:06 am Post subject: Re: How to store Loki Functors in a generic container? |
|
|
Haha, this is funny, I recently posted here about an article I have
written that describes just how to do what you want. Except I don't use
Loki::Functor, but it's possible you can change that if you really want
to. I have also not had the time to publish the whole source code to
the article but I hope to do so soon.
My article describes how to store functions with different types and
how to invoke them by deriving all their parameters using one
super-parameter (this is since you can't invoke a function if you don't
give it the correct parameters).
Anyway, here's a link to my article:
http://amag.rotd.org/tutorials.php
Cheers,
Andreas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
msalters Guest
|
Posted: Sat Sep 24, 2005 2:20 am Post subject: Re: How to store Loki Functors in a generic container? |
|
|
hydra... (AT) yahoo (DOT) com schreef:
| Quote: | Hello group,
This is a pretty basic question....
I'm using Loki Functors and I want to store them in a generic container
(like vector or map). The problem is that each kind of functor is-a
different type because of the template paramaters. So I had the bright
idea to hack Loki and make Functor inheret from a
class FunctorInterface{};
so now I can store a functor in a vector as a FunctorInterface*. Sweet.
|
Of course, while you know that vector[0] is callable, you don't know
its arguments. Furthermore, even if you knew, vector[1] will need
different arguments.
| Quote: | Now.... I COULD add a virtual descrutor to
FunctorInterface and use a dynamic_cast but:
1) That is slow
2) That is run time error checking, not compile time.
|
Well, you gave up compile-time checking when you mixed the two types
Be glad you can unscramble eggs, even slowly.
HTH,
Michiel Salters
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|