Flexible templated function class and related utilities that avoid dynamic memory allocation without limiting functionality
FuncPtr provides a flexible templated function class and related utilities while avoiding dynamic memory allocation and avoiding limited functionality.
FuncPtr provides an intuitive template interface:
FuncPtr<void(const char *)> func(puts); func("hello!\n"); // prints hello!
Several function types are supported by FuncPtr:
// Simple C functions void func(); FuncPtr<void()> fp(func); // C++ Methods struct Thing { void func(); }; Thing thing; FuncPtr<void()> fp(&thing, &Thing::func); // C functions with context void func(Thing *); FuncPtr<void()> fp(&thing, func); // Function objects struct Thing { void operator()(); }; Thing thing; FuncPtr<void()> fp(&thing);
There is no dynamic memory allocation, managing memory is placed entirely on the user. More advanced function manipulation can be accomplished with statically allocated classes:
// Function binding Binder<void(const char *), const char *> bind(putc, "hi!"); bind(); // prints hi! // Function composition Composer<int(const char *), const char *(int)> comp(puts, itoa); comp(10); // prints 10 // Function chaining Chainer<void(const char *), 2> chain; chain.attach(puts); chain.attach(puts); chain("hi!\n"); // prints hi! twice
FuncPtr allows easy support of a large range of function types in C++ APIs with very few lines of code:
class Thing { public: // The following two methods are sufficient for supporting // every supported function type void attach(FuncPtr<void()> func) { _func.attach(func); } template<typename T, typename M> void attach(T *obj, M method) { attach(FuncPtr<void()>(obj, method)); } private: FuncPtr<void()> _func; }
Additionally, FuncPtrs have several utilities for easy integration with C APIs:
// C style callbacks void register_callback(void (*callback)(void *), void *data); register_callback(&FuncPtr<void()>::thunk, &func); // C style functions without context void register_callback(void (*callback)()); Thunker thunk(func); register_callback(thunk); // mbed style callbacks void register_callback(T *obj, void (T::*M)()); register_callback(&func, &FuncPtr<void()>::call);
History
Increase to 5 args
2016-04-17, by Christopher Haster [Sun, 17 Apr 2016 23:38:04 -0500] rev 18
Increase to 5 args
Add support for x86_64
2016-04-20, by Christopher Haster [Wed, 20 Apr 2016 02:43:14 -0500] rev 17
Add support for x86_64
Fix executable restrictions on multiarch support for x86
2016-04-20, by Christopher Haster [Wed, 20 Apr 2016 02:17:49 -0500] rev 16
Fix executable restrictions on multiarch support for x86
Reversed template arguments to Chainer to better match array declaration
2016-04-17, by Christopher Haster [Sun, 17 Apr 2016 22:01:07 -0500] rev 15
Reversed template arguments to Chainer to better match array declaration
Removed funcptr namespace
2016-04-17, by Christopher Haster [Sun, 17 Apr 2016 21:25:00 -0500] rev 14
Removed funcptr namespace
Added funcptr namespace
2016-04-17, by Christopher Haster [Sun, 17 Apr 2016 21:15:30 -0500] rev 13
Added funcptr namespace
Move trampoline generation into separate C-compatible file
2016-04-17, by Christopher Haster [Sun, 17 Apr 2016 21:11:07 -0500] rev 12
Move trampoline generation into separate C-compatible file
Rearranged FuncPtr to take better advantage of struct alignment
2016-04-17, by Christopher Haster [Sun, 17 Apr 2016 20:51:55 -0500] rev 11
Rearranged FuncPtr to take better advantage of struct alignment
Slight doc change
2016-04-17, by Christopher Haster [Sun, 17 Apr 2016 20:40:41 -0500] rev 10
Slight doc change
Added Thunker class for generating static thunks to bound functions
2016-04-17, by Christopher Haster [Sun, 17 Apr 2016 20:38:53 -0500] rev 9
Added Thunker class for generating static thunks to bound functions