Skip to content

Instantly share code, notes, and snippets.

Created April 26, 2012 11:30
Show Gist options
  • Save anonymous/2498955 to your computer and use it in GitHub Desktop.
Save anonymous/2498955 to your computer and use it in GitHub Desktop.
#include <string>
#include <list>
enum DataType
{
INT,
FLOAT,
BOOL,
VEC2,
VEC3,
QUAT,
MAT3,
MAT4
};
class FunctorBase
{
virtual std::string get(DataType dataType) = 0;
};
template<class Object,class Functor>
class MemberFunctor
: public FunctorBase
{
public:
MemberFunctor(const Object& obj,const Functor &functor)
: m_Object(obj),m_Functor(functor)
{
}
std::string get(DataType dataType)
{
if (dataType == INT)
{
return intToString(m_Object->*m_Functor());
}
else if (dataType == FLOAT)
{
return floatToString(m_Object->*m_Functor());
}
..etc..
}
Object m_Object;
Functor m_Functor;
};
class TargetObject
{
public:
int testInt();
float testFloat();
};
class FunctorManager
{
public:
template<class Object,class Functor>
void AddFunctor(const Object& obj,const Functor &functor)
{
FunctorBase *pFunctorBase = new MemberFunctor<Object,Functor>(obj,functor);
m_listFunctors.push_back(pFunctorBase);
}
private:
std::list<FunctorBase*> m_listFunctors;
};
#include "Base.h"
int main()
{
TargetObject *pObject = new TargetObject;
FunctorManager *pFunctorManager = new FunctorManager;
pFunctorManager->AddFunctor(pObject,&TargetObject::testFloat);
pFunctorManager->AddFunctor(pObject,&TargetObject::testInt);
return 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment