Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I finally have a working trampoline mechanism. Would anyone care to critique it?

Ideone

#include <iostream>
using namespace std;
#include <map>
template<typename T>
class Base
{
public: 
 typedef void (T::*PFunc)(void);
 typedef void (*PHandler)(void*);
 using method_map_t = map< string, PHandler >;
 static method_map_t& methods( ) {
 static method_map_t* map_of_methods{};
 if( ! map_of_methods ) map_of_methods = new method_map_t;
 return *map_of_methods;
 }
 static void register_method( string name, PHandler handler ) {
 methods()[name] = handler;
 }
 // generic trampoline
 template<PFunc sig>
 static void Handler( void* pInstance ) {
 T* f = reinterpret_cast<T*>(pInstance);
 (f ->* sig)();
 }
};

...

class Final : Base<Final>
{
public:
 void Foo(){cout<<"got foo";}
 void Bar(){cout<<"got bar";}
 
 static void init(){
 // register_method populates a table of "extern C" function pointers.
 register_method( "foo", static_cast<PHandler>( &Handler<&Final::Foo> ) );
 register_method( "bar", static_cast<PHandler>( &Handler<&Final::Bar> ) );
 }
};
void InvokeFromC(void* inst, string name) {
 Base<Final>::PHandler h = Base<Final>::methods()[name];
 (*h)(inst);
}
int main() {
 Final* f = new Final{};
 f->init();
 // simulate invoking from C 
 InvokeFromC( f, "foo" );
 
 // your code goes here
 return 0;
}

This question follows from here here.

I finally have a working trampoline mechanism. Would anyone care to critique it?

Ideone

#include <iostream>
using namespace std;
#include <map>
template<typename T>
class Base
{
public: 
 typedef void (T::*PFunc)(void);
 typedef void (*PHandler)(void*);
 using method_map_t = map< string, PHandler >;
 static method_map_t& methods( ) {
 static method_map_t* map_of_methods{};
 if( ! map_of_methods ) map_of_methods = new method_map_t;
 return *map_of_methods;
 }
 static void register_method( string name, PHandler handler ) {
 methods()[name] = handler;
 }
 // generic trampoline
 template<PFunc sig>
 static void Handler( void* pInstance ) {
 T* f = reinterpret_cast<T*>(pInstance);
 (f ->* sig)();
 }
};

...

class Final : Base<Final>
{
public:
 void Foo(){cout<<"got foo";}
 void Bar(){cout<<"got bar";}
 
 static void init(){
 // register_method populates a table of "extern C" function pointers.
 register_method( "foo", static_cast<PHandler>( &Handler<&Final::Foo> ) );
 register_method( "bar", static_cast<PHandler>( &Handler<&Final::Bar> ) );
 }
};
void InvokeFromC(void* inst, string name) {
 Base<Final>::PHandler h = Base<Final>::methods()[name];
 (*h)(inst);
}
int main() {
 Final* f = new Final{};
 f->init();
 // simulate invoking from C 
 InvokeFromC( f, "foo" );
 
 // your code goes here
 return 0;
}

This question follows from here.

I finally have a working trampoline mechanism. Would anyone care to critique it?

Ideone

#include <iostream>
using namespace std;
#include <map>
template<typename T>
class Base
{
public: 
 typedef void (T::*PFunc)(void);
 typedef void (*PHandler)(void*);
 using method_map_t = map< string, PHandler >;
 static method_map_t& methods( ) {
 static method_map_t* map_of_methods{};
 if( ! map_of_methods ) map_of_methods = new method_map_t;
 return *map_of_methods;
 }
 static void register_method( string name, PHandler handler ) {
 methods()[name] = handler;
 }
 // generic trampoline
 template<PFunc sig>
 static void Handler( void* pInstance ) {
 T* f = reinterpret_cast<T*>(pInstance);
 (f ->* sig)();
 }
};

...

class Final : Base<Final>
{
public:
 void Foo(){cout<<"got foo";}
 void Bar(){cout<<"got bar";}
 
 static void init(){
 // register_method populates a table of "extern C" function pointers.
 register_method( "foo", static_cast<PHandler>( &Handler<&Final::Foo> ) );
 register_method( "bar", static_cast<PHandler>( &Handler<&Final::Bar> ) );
 }
};
void InvokeFromC(void* inst, string name) {
 Base<Final>::PHandler h = Base<Final>::methods()[name];
 (*h)(inst);
}
int main() {
 Final* f = new Final{};
 f->init();
 // simulate invoking from C 
 InvokeFromC( f, "foo" );
 
 // your code goes here
 return 0;
}

This question follows from here.

added 11 characters in body; edited tags
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I finally have a working trampoline mechanism:

. Would anyone care to critique it?

http://ideone.com/vOtbcDIdeone

or:

#include <iostream>
using namespace std;
#include <map>
template<typename T>
class Base
{
public: 
 typedef void (T::*PFunc)(void);
 typedef void (*PHandler)(void*);
 using method_map_t = map< string, PHandler >;
 static method_map_t& methods( ) {
 static method_map_t* map_of_methods{};
 if( ! map_of_methods ) map_of_methods = new method_map_t;
 return *map_of_methods;
 }
 static void register_method( string name, PHandler handler ) {
 methods()[name] = handler;
 }
 // generic trampoline
 template<PFunc sig>
 static void Handler( void* pInstance ) {
 T* f = reinterpret_cast<T*>(pInstance);
 (f ->* sig)();
 }
};

...

class Final : Base<Final>
{
public:
 void Foo(){cout<<"got foo";}
 void Bar(){cout<<"got bar";}
 
 static void init(){
 // register_method populates a table of "extern C" function pointers.
 register_method( "foo", static_cast<PHandler>( &Handler<&Final::Foo> ) );
 register_method( "bar", static_cast<PHandler>( &Handler<&Final::Bar> ) );
 }
};
void InvokeFromC(void* inst, string name) {
 Base<Final>::PHandler h = Base<Final>::methods()[name];
 (*h)(inst);
}
int main() {
 Final* f = new Final{};
 f->init();
 // simulate invoking from C 
 InvokeFromC( f, "foo" );
 
 // your code goes here
 return 0;
}

PS This question follows from: http://stackoverflow.com/questions/26934036/coding-static-to-instance-method-trampoline-function-with-templates/here .

I finally have a working trampoline mechanism:

Would anyone care to critique it?

http://ideone.com/vOtbcD

or:

#include <iostream>
using namespace std;
#include <map>
template<typename T>
class Base
{
public: 
 typedef void (T::*PFunc)(void);
 typedef void (*PHandler)(void*);
 using method_map_t = map< string, PHandler >;
 static method_map_t& methods( ) {
 static method_map_t* map_of_methods{};
 if( ! map_of_methods ) map_of_methods = new method_map_t;
 return *map_of_methods;
 }
 static void register_method( string name, PHandler handler ) {
 methods()[name] = handler;
 }
 // generic trampoline
 template<PFunc sig>
 static void Handler( void* pInstance ) {
 T* f = reinterpret_cast<T*>(pInstance);
 (f ->* sig)();
 }
};

...

class Final : Base<Final>
{
public:
 void Foo(){cout<<"got foo";}
 void Bar(){cout<<"got bar";}
 
 static void init(){
 // register_method populates a table of "extern C" function pointers.
 register_method( "foo", static_cast<PHandler>( &Handler<&Final::Foo> ) );
 register_method( "bar", static_cast<PHandler>( &Handler<&Final::Bar> ) );
 }
};
void InvokeFromC(void* inst, string name) {
 Base<Final>::PHandler h = Base<Final>::methods()[name];
 (*h)(inst);
}
int main() {
 Final* f = new Final{};
 f->init();
 // simulate invoking from C 
 InvokeFromC( f, "foo" );
 
 // your code goes here
 return 0;
}

PS This question follows from: http://stackoverflow.com/questions/26934036/coding-static-to-instance-method-trampoline-function-with-templates/

I finally have a working trampoline mechanism. Would anyone care to critique it?

Ideone

#include <iostream>
using namespace std;
#include <map>
template<typename T>
class Base
{
public: 
 typedef void (T::*PFunc)(void);
 typedef void (*PHandler)(void*);
 using method_map_t = map< string, PHandler >;
 static method_map_t& methods( ) {
 static method_map_t* map_of_methods{};
 if( ! map_of_methods ) map_of_methods = new method_map_t;
 return *map_of_methods;
 }
 static void register_method( string name, PHandler handler ) {
 methods()[name] = handler;
 }
 // generic trampoline
 template<PFunc sig>
 static void Handler( void* pInstance ) {
 T* f = reinterpret_cast<T*>(pInstance);
 (f ->* sig)();
 }
};

...

class Final : Base<Final>
{
public:
 void Foo(){cout<<"got foo";}
 void Bar(){cout<<"got bar";}
 
 static void init(){
 // register_method populates a table of "extern C" function pointers.
 register_method( "foo", static_cast<PHandler>( &Handler<&Final::Foo> ) );
 register_method( "bar", static_cast<PHandler>( &Handler<&Final::Bar> ) );
 }
};
void InvokeFromC(void* inst, string name) {
 Base<Final>::PHandler h = Base<Final>::methods()[name];
 (*h)(inst);
}
int main() {
 Final* f = new Final{};
 f->init();
 // simulate invoking from C 
 InvokeFromC( f, "foo" );
 
 // your code goes here
 return 0;
}

This question follows fromhere .

Source Link
P i
  • 639
  • 5
  • 13

Static to Instance method trampolining with templates

I finally have a working trampoline mechanism:

Would anyone care to critique it?

http://ideone.com/vOtbcD

or:

#include <iostream>
using namespace std;
#include <map>
template<typename T>
class Base
{
public: 
 typedef void (T::*PFunc)(void);
 typedef void (*PHandler)(void*);
 using method_map_t = map< string, PHandler >;
 static method_map_t& methods( ) {
 static method_map_t* map_of_methods{};
 if( ! map_of_methods ) map_of_methods = new method_map_t;
 return *map_of_methods;
 }
 static void register_method( string name, PHandler handler ) {
 methods()[name] = handler;
 }
 // generic trampoline
 template<PFunc sig>
 static void Handler( void* pInstance ) {
 T* f = reinterpret_cast<T*>(pInstance);
 (f ->* sig)();
 }
};

...

class Final : Base<Final>
{
public:
 void Foo(){cout<<"got foo";}
 void Bar(){cout<<"got bar";}
 
 static void init(){
 // register_method populates a table of "extern C" function pointers.
 register_method( "foo", static_cast<PHandler>( &Handler<&Final::Foo> ) );
 register_method( "bar", static_cast<PHandler>( &Handler<&Final::Bar> ) );
 }
};
void InvokeFromC(void* inst, string name) {
 Base<Final>::PHandler h = Base<Final>::methods()[name];
 (*h)(inst);
}
int main() {
 Final* f = new Final{};
 f->init();
 // simulate invoking from C 
 InvokeFromC( f, "foo" );
 
 // your code goes here
 return 0;
}

PS This question follows from: http://stackoverflow.com/questions/26934036/coding-static-to-instance-method-trampoline-function-with-templates/

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /