我试图重新编码一些相当丑陋的模板编码。使用模板编码静态实例方法trampoline函数
仅供参考,原来是在这里:https://codereview.stackexchange.com/questions/69545/recode-c-c-trampoline-function-macros-using-templates
class Final : Base
{
void Foo(){...}
void Bar(){...}
static void init(){
// register_method populates a table of "extern C" function pointers.
register_method("foo", & FooHandler);
register_method("bar", & BarHandler);
}
:
// trampolines
static void FooHandler(void* pInstance) {
Final* f = reinterpret_cast<Final*>(pInstance);
f->Foo();
}
static void BarHandler(void* pInstance) {
Final* f = reinterpret_cast<Final*>(pInstance);
f->Bar();
}
}
我与CPython的(C库)代码的接口。 Python运行时看到“myInst.foo”查找“foo”的表中,并调用:
Final::FooHandler(pointer_to_myInst);
(注意有可能强制转换一个静态方法来C函数指针)
FooHandler蹦床到Final的正确实例的Foo方法。
实际上,句柄并不那么干净,并且有很多方法都需要相同的处理程序(但具有不同的函数地址)。
我试图抽象处理机制引入一个基类,像这样:
class Final : Base<Final>
{
void Foo(){...}
void Bar(){...}
static void init(){
// register_method populates a table of "extern C" function pointers.
register_method("foo", & Foo, Handler< &Foo>);
register_method("bar", & Bar, Handler< &Bar>);
}
:
}
class Base<Final>
{
typedef void (Final::*FuncSig)(void);
typedef void (Final::*HandlerSig)(void*); // takes 1 pvoid param
void register_method(std::string name, FuncSig meth, HandlerSig handler) {
...
}
// generic trampoline
template< Sig sig>
static void Handler(void* pInstance) {
Final* f = reinterpret_cast<Final*>(pInstance);
f ->* sig();
}
}
我目前陷在编译器错误(http://ideone.com/vOtbcD),所以我甚至不能确定是否技术是有效的。
有没有办法做到这一点,或者这只是其中一个真正需要宏的地方?
仅供参考,原来是在这里:https://codereview.stackexchange.com/questions/69545/recode-c-c-trampoline-function-macros-using-templates
可以看出,原来的用途,而难看的宏。
好对于初学者'Base'不是模板类,你需要使用关键字'template'。您可能还想阅读['std :: function'](http://en.cppreference。com/w/cpp/utility/functional/function)和['std :: bind'](http://en.cppreference.com/w/cpp/utility/functional/bind)。 – 2014-11-14 16:06:57