Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

C++
class IMetod{
	public:
		template<class RetType, class ... ParamList>
		RetType invoke(void* pObj, ParamList ... arg)
		{
			// ToDo: how i should do here.
		}

		virtual void voidInvoke(void* p, ...)
		{
			// ToDo: how i should do here.
		}
	};

	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.
Posted
Updated 15-Mar-16 18:42pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900