Since I can not compare std::function
objects and only store member functions and functions, I tried to create my own class. Where I have encountered the problem that I have to compare functions and member functions of different classes with each other. I've considered the following solution, but I'm not sure if that's valid:
template<class ClassT, class RetT, class... Args>
uint32_t getMethodAddress(RetT(ClassT::*mFunc)(Args...))
{
union Method
{
RetT(ClassT::*mFunc)(Args...);
uint32_t address;
};
return Method{ mFunc }.address;
}
Is it possible to save the address of each method by uint32_t
and can I compare this address without any nasty surprises with other functions and methods?
If anyone wonders what the class looks like:
// Function.hpp
template<class RetT, class... Args>
class Function
{
public:
explicit Function(RetT(*func)(Args...))
{
_in::Function<RetT, Args...>* pTmp{ new _in::Function<RetT, Args...>{ func } };
_pFunc = pTmp;
}
template<class ClassT>
explicit Function(ClassT* pObj, RetT(ClassT::*mFunc)(Args...))
{
_in::MFunction<ClassT, RetT, Args...>* pTmp{ new _in::MFunction<ClassT, RetT, Args...>{ pObj, mFunc } };
_pFunc = pTmp;
}
RetT operator()(Args... args)
{
return (*_pFunc)(args...);
}
bool operator==(const Function& rhs)
{
return _pFunc->addr() == rhs._pFunc->addr(); // Is that valid?
}
private:
_in::BaseFunction<RetT, Args...>* _pFunc{ nullptr };
};