Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Need help with generic functors....

Reply
Thread Tools

Re: Need help with generic functors....

 
 
Rob Williscroft
Guest
Posts: n/a
 
      08-17-2003
Gordon Scott wrote in
news:mSC%a.19080$(E-Mail Removed) et:

> Hi All,
>
> I'm trying to work on a small asynchronous frame work that performs
> different jobs by having a pool of threads that pull different Task
> objects off of a queue. I would like to define a Task object that
> somehow contains different types of functor objects (or function
> pointers) and parameters for execution. This way I can have one
> thread pool performing a variety of different operations.
>
> I'm having trouble coming up with a way to do this.
> I would like to do something like:
>
> class Foo{
> void operator()(int param);
> };
> class Bar{
> void operator()(std::string param);
> }
>
> int main(){
> Foo foo;
> Bar bar;
> Task taskA(foo, 1);
> Task taskB(bar, "Hello World");
>
> Scheduler::Schedule(taskA); // add taskA to the task queue
> Scheduler::Schedule(taskB); // add taskB to the task queue
> .
> .
> .
> }
>
> defining Task like so, obviously doesn't work.


Then you need another layer of indirection:

class TaskBase
{
virtual void Run() = 0;
virtual ~TaskBase() {}
};

>
> template<class F, class T>
> class Task{


class Task: public TaskBase

> public:
> Task(F functor, T parameter){
> _functor = functor;
> _parameter = parameter;
> }
> void Run(){
> _functor(_parameter);
> }
> private:
> F _functor;
> T _parameter;
> };
>
>


Now add the function Victor showed, but now we know about Scheduler:

class Scheduler
{
// ....

std::list< TaskBase * > que;

template<class F, class P>
void Schedule(F f, P p)
{
TaskBase *tb = new Task<F,P>(f,p);
que.push_back( tb );
}

~Scheduler()
{
// ...
std::list< TaskBase * >::iterator ptr, lim;
for (ptr = que.begin(), lim = que.end(); ptr != lim; ++ptr)
{
delete *ptr;
}
}
};



Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
 
Gordon Scott
Guest
Posts: n/a
 
      08-17-2003
Thanks a bunch,

using your help and these two articles

http://www.tutok.sk/fastgl/callback.html
http://www.codeproject.com/cpp/CallbackDemo.asp

I now have the following that allows me to make functors for calling class
member functions.
Are there any major problems with this implementation, or ways to clean up?

class Task{
public:
Task() {}
virtual void Run()const {};
virtual ~Task() {}
};

template<class Functor, class Parameter>
class TaskImpl : Task{
public:
TaskImpl(Functor functor, Parameter parameter){
_functor = functor;
_parameter = parameter;
}

void Run(){
_functor(_parameter);
}

private:
Functor _functor;
Parameter _parameter;
};

template<class Callee, class Parameter1>
class Functor1{
typedef void (Callee::*MemFunc)(Parameter1 param);
public:
Functor1(): _callee(NULL), _memfunc(NULL){}

Functor1(Callee* callee, MemFunc memfunc){
_callee = callee;
_memfunc = memfunc;
}

void operator()(Parameter1 param){
if(_callee && _memfunc)
(_callee->*_memfunc)(param);
}
private:
Callee* _callee;
MemFunc _memfunc;
};


Usage:

class Bar
{
public:
Bar(void) {}
~Bar(void){}
void PrintString(std::string aString){
cout << aString.c_str() << endl;
}
};

int main(){
Bar bar;
Functor1<Bar, std::string> func(&bar, &Bar:rintString);

std::string aString = "Hello World";
TaskImpl<Functor1<Bar, std::string>, std::string> task(func, aString);
task.Run();
}


(next stop creating functors out of non-member functions...)


 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      08-17-2003
Gordon Scott wrote in
news:MCS%a.19218$(E-Mail Removed) et:

> Thanks a bunch,
>
> using your help and these two articles
>
> http://www.tutok.sk/fastgl/callback.html
> http://www.codeproject.com/cpp/CallbackDemo.asp
>
> I now have the following that allows me to make functors for calling
> class member functions.
> Are there any major problems with this implementation, or ways to
> clean up?
>


Can't see any.

[snip]

> int main(){
> Bar bar;
> Functor1<Bar, std::string> func(&bar, &Bar:rintString);
>
> std::string aString = "Hello World";
> TaskImpl<Functor1<Bar, std::string>, std::string> task(func,
> aString); task.Run();
> }
>
>
> (next stop creating functors out of non-member functions...)
>
>


void nonmember( int i )
{
std::cout << "nonmember( " << i << " )" << std::endl;
}

int main()
{
TaskImpl< void (*)(int), int> task(nonmember, 2);
task.Run();
}

Though if you do decide to create a functor for this, you may
get some benefits from inlining, but I can't see it making
much of a perfomance difference for a scheduled task.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Gordon Scott
Guest
Posts: n/a
 
      08-17-2003
Thanks for all the help.
So far my C++ experience has mostly involved legacy C code with a couple of
objects.
This is the most fun I've had in a while.

If you're interested, I followed Victor's idea and came up with

template<class Functor, class Parameter>
TaskImpl<Functor, Parameter> createTask(Functor f, Parameter p){
return TaskImpl<Functor, Parameter>(f,p);
}

template <class Callee, class Parameter>
Functor1<Callee, Parameter> createFunctor(Callee* callee,
void(Callee::*MemFunc)(Parameter))
{
return Functor1<Callee, Parameter>(callee,MemFunc);
}

Seems to work like a champ.

"Rob Williscroft" <(E-Mail Removed)> wrote in message
news:Xns93DBC8B9381ukcoREMOVEfreenetrtw@195.129.11 0.200...
> Gordon Scott wrote in
> news:MCS%a.19218$(E-Mail Removed) et:
>
> > Thanks a bunch,
> >
> > using your help and these two articles
> >
> > http://www.tutok.sk/fastgl/callback.html
> > http://www.codeproject.com/cpp/CallbackDemo.asp
> >
> > I now have the following that allows me to make functors for calling
> > class member functions.
> > Are there any major problems with this implementation, or ways to
> > clean up?
> >

>
> Can't see any.
>
> [snip]
>
> > int main(){
> > Bar bar;
> > Functor1<Bar, std::string> func(&bar, &Bar:rintString);
> >
> > std::string aString = "Hello World";
> > TaskImpl<Functor1<Bar, std::string>, std::string> task(func,
> > aString); task.Run();
> > }
> >
> >
> > (next stop creating functors out of non-member functions...)
> >
> >

>
> void nonmember( int i )
> {
> std::cout << "nonmember( " << i << " )" << std::endl;
> }
>
> int main()
> {
> TaskImpl< void (*)(int), int> task(nonmember, 2);
> task.Run();
> }
>
> Though if you do decide to create a functor for this, you may
> get some benefits from inlining, but I can't see it making
> much of a perfomance difference for a scheduled task.
>
> Rob.
> --
> http://www.victim-prime.dsl.pipex.com/



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
not just generic type programming,but also parallism generic syntaxprogramming?? minlearn C++ 2 03-13-2009 05:17 PM
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
I'm stuck with the implementation of a generic algorithm.... need some help :P StephQ C++ 8 08-01-2007 04:38 PM
Generic class in a non generic class nramnath@gmail.com Java 2 07-04-2006 07:24 AM
Need help with overriding generic in top level fastgreen2000@yahoo.com VHDL 9 02-10-2005 05:54 PM



Advertisments