//////////////////////////////////////////////////////////////////////
// Functor.h
//////////////////////////////////////////////////////////////////////
#ifndef Functor_H
#define Functor_H
namespace Projengine
{
//////////////////////////////////////////////////////////////
// BaseFunctor Class
//////////////////////////////////////////////////////////////
class BaseFunctor
{
public:
virtual void call( std::vector< void* > arguments ) = 0;// const = 0;
};
//////////////////////////////////////////////////////////////
// Functor Class
//////////////////////////////////////////////////////////////
template< class T >
class Functor : public BaseFunctor
{
public:
typedef void ( T::*functionType )( std::vector< void* > arguments );
Functor( T* type , functionType f )
{
m_Function = f;
m_pClass = type;
}
virtual void call( std::vector< void* > arguments ) //const
{
( m_pClass->*m_Function )( arguments );
}
protected:
functionType m_Function;
T* m_pClass;
};
} // Namespace Projengine
#endif
Specifying Return Types For Functors
Hey,
Below is my functor class that I got from some tutorials from the net. What I want to do is to be able to specify the return value type for the functor. My template programing is pretty rubbish so Im having some issues doing this. I have had a few goes but give up after a while because I dont get it. Anyway, does anyone give me any help or point me to some tutorials to accomplish this ?
Thanks heaps.
Quote:
Functor( T* type , functionType f )
{
m_Function = f;
m_pClass = type;
}
What are you returning? A functionType or T?
Hey,
You use the "Functor" class Like this:
So this is all well and good, but the "sayHello" function, has to return "void" or there is trouble. I want to be able to specify the return type of the functor so I can do something like this:
To to the above, I think you need to modify my functor class to be something like this: (Note the changes at lines "template< class T , class R >" and "R* call( void* arguments ) const
I also changed "std::vector< void* > arguments" to be "void* arguments", this is irrelevant
You use the "Functor" class Like this:
class talk{ void sayHello() { // SAY HELLO // }}talk* pTalkPointer = new talk();Functor* pFunctor = new Functor< talk >( pTalkPointer , &talk::sayHello ) )// THIS CALLS "sayHello" IN CLASS "talk" //pFunctor->call();
So this is all well and good, but the "sayHello" function, has to return "void" or there is trouble. I want to be able to specify the return type of the functor so I can do something like this:
class talk{ bool sayHello() { // SAY HELLO // return true; }}talk* pTalkPointer = new talk();Functor* pFunctor = new Functor< talk , bool >( pTalkPointer , &talk::sayHello ) )// THIS CALLS "sayHello" IN CLASS "talk" //if( pFunctor->call() == true ) // CELEBRATE //
To to the above, I think you need to modify my functor class to be something like this: (Note the changes at lines "template< class T , class R >" and "R* call( void* arguments ) const
I also changed "std::vector< void* > arguments" to be "void* arguments", this is irrelevant
//////////////////////////////////////////////////////////////////////// Functor.h//////////////////////////////////////////////////////////////////////#ifndef Functor_H#define Functor_Hnamespace Projengine{ ////////////////////////////////////////////////////////////// // BaseFunctor Class ////////////////////////////////////////////////////////////// class BaseFunctor { public: virtual void call( void* arguments ) const = 0; }; ////////////////////////////////////////////////////////////// // Functor Class ////////////////////////////////////////////////////////////// template< class T , class R > class Functor : public BaseFunctor { public: typedef void ( T::*functionType )( void* arguments ); Functor( T* type , functionType f ) { m_Function = f; m_pClass = type; } R* call( void* arguments ) const { ( m_pClass->*m_Function )( arguments ); } protected: functionType m_Function; T* m_pClass; };} // Namespace Projengine#endif
I could give you the answer however there is a much better (and more efficient) solution and it comes in the form of std::tr1::function (if your compiler implements TR1) or boost::function (which std::tr1::function originates from) from boost. You may also want to look into boost.bind for your partial application needs. If you doing this for event handling then a signals and slots library such as Boost.Signals maybe a better option.
Thanks snk_kid. On first glance, boost signals and slots do all I want and more. Cheerin. While looking through the boost site, came across a shared pointer, one that you dont need to delete when you are finished with it. Is it a good idea to use those ? Should you use them for all things or just some things ?
Quote:Original post by lbt
While looking through the boost site, came across a shared pointer, one that you dont need to delete when you are finished with it. Is it a good idea to use those ?
Most definitely, as long as you understand it's purpose, usage, and when to use. boost:shared_ptr/weak_ptr are another boost component which got into C++ TR1 (therefore most likely to go into C++0x).
Quote:Original post by lbt
Should you use them for all things or just some things ?
Boost has a variety of smart pointers for different scenarios, pick the one that fits the context.
boost::shared_ptr is mainly (as it's not a hard rule) used for when you have a single instance/object not soley owned by another object and referred to by more than one pointer hence the name "shared". It's gives you a kind of garabage collection.
If go through the docs you'll get a better undestanding when and where to use them.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement