Skip to content

Instantly share code, notes, and snippets.

@Bueddl
Created January 6, 2017 12:53
Show Gist options
  • Save Bueddl/8ac8d0f2e62dfe2ffa08d2727c4fca24 to your computer and use it in GitHub Desktop.
Save Bueddl/8ac8d0f2e62dfe2ffa08d2727c4fca24 to your computer and use it in GitHub Desktop.
#include <iostream>
class my_class
{
public:
void test()
{
std::cout << "Hello C++" << std::endl;
}
void set_x(double x)
{
_x = x;
}
double get_x() const
{
return _x;
}
private:
double _x;
};
template<class T>
class wrapper
{
public:
template<class R, class... As>
R call(R(T::*fnp)(As...), As... args)
{
return (reinterpret_cast<T*>(this)->*fnp)(std::forward<As>(args)...);
}
template<class R, class... As>
R call(unsigned long addr, As... args)
{
union {
unsigned long u_ul;
void *u_voidp;
R(T::*u_mfnp)(As...);
} call_addr;
call_addr.u_ul = addr;
return call<R, As...>(call_addr.u_mfnp, std::forward<As>(args)...);
}
};
int main()
{
wrapper<my_class> *object = reinterpret_cast<wrapper<my_class>*>(new my_class);
object->call(&my_class::test);
std::cout << "&my_class::test = " << std::showbase << std::hex << (void*)(&my_class::test) << std::endl
<< "&my_class::set_x = " << std::showbase << std::hex << (void*)(&my_class::set_x) << std::endl
<< "&my_class::get_x = " << std::showbase << std::hex << (void*)(&my_class::get_x) << std::endl
<< std::endl;
object->call<void>(0x400cce); // test
object->call<void, double>(0x400cfa, 42.1337); // set
std::cout << "object->get_x() = " << object->call<double>(0x400d18) << std::endl; // get
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment