I want to use C++11 to make it possible to use reflection just as do in Java. So I make the template class named CMethod to save the member function, and make the CMethod derived from the IMethod to make it a abstract implement. But C++11 do not support the virtual template member function, so How can i do to make what I want to do possible?
class IMetod{
public:
template<class RetType, class ... ParamList>
RetType invoke(void* pObj, ParamList ... arg)
{
}
virtual void voidInvoke(void* p, ...)
{
}
};
template<class Clazz>
class CMethod:public IMetod
{
public:
template<class RetType, class ... ParamList>
struct SMethod{
RetType(__thiscall Clazz::*pMethod)(ParamList ... arg);
};
template<class RetType, class ... ParamList>
void operator=(RetType (Clazz::*m)(ParamList ... arg));
template<class RetType, class ... ParamList>
RetType invoke(Clazz* pObj, ParamList ... arg);
template<class RetType, class ... ParamList>
RetType invoke(Clazz& pObj, ParamList ... arg);
template<class RetType, class ... ParamList>
RetType invoke(void* p, ParamList ... arg)
{
Clazz* pObj = (Clazz*)p;
return this->invoke(pObj, arg...);
}
const char* name() const;
public:
CMethod();
template<class RetType, class ... ParamList>
CMethod(const char* name, RetType(Clazz::*m)(ParamList ...), CMethod<Clazz>* next = NULL, int* paramsTypeHashCode = NULL);
~CMethod();
private:
template<class FirstParam = void, class ... OtherParams>
bool recodeParamType(int index)
{
if (m_paramsSize > 0 && index <= 0)
{
if (m_paramsTypeHashCode) delete[] m_paramsTypeHashCode;
m_paramsTypeHashCode = NULL;
m_paramsTypeHashCode = new int[m_paramsSize];
memset(m_paramsTypeHashCode, 0, sizeof(int)*m_paramsSize);
}
const char* type = typeid(FirstParam).name();
if (typeid(FirstParam) == typeid(void)) return true;
else
{
if (index >= m_paramsSize)
{
throw runtime_error("Too many parameters to invoke the method!");
return false;
}
m_paramsTypeHashCode[index] = typeid(FirstParam).hash_code();
return recodeParamType<OtherParams ...>(index+1);
}
}
private:
void* m_method;
int m_returnTypeHashCode;
int m_paramsSize;
int* m_paramsTypeHashCode;
public:
CMethod<Clazz>* m_next;
const char* m_name;
char* m_paramList;
};
What I have tried:
I use Viusal Studio 2013 to carry out this project. So i want it run at least on Windows7 or more.