Flexible templated function class and related utilities that avoid dynamic memory allocation without limiting functionality

Dependents:   SimpleHTTPExample

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);

Changes

RevisionDateWhoCommit message
18:a0fde14b6c39 2016-04-17 Christopher Haster Increase to 5 args default tip
17:079c5ad807fb 2016-04-20 Christopher Haster Add support for x86_64
16:c88d591ae0b4 2016-04-20 Christopher Haster Fix executable restrictions on multiarch support for x86
15:0c3d1c4a050b 2016-04-17 Christopher Haster Reversed template arguments to Chainer to better match array declaration
14:79be4e700cc9 2016-04-17 Christopher Haster Removed funcptr namespace
13:4d8a50d4967e 2016-04-17 Christopher Haster Added funcptr namespace
12:a005f8ca5d36 2016-04-17 Christopher Haster Move trampoline generation into separate C-compatible file
11:035e0728d927 2016-04-17 Christopher Haster Rearranged FuncPtr to take better advantage of struct alignment
10:086f34a27a8d 2016-04-17 Christopher Haster Slight doc change
9:89aeb1297779 2016-04-17 Christopher Haster Added Thunker class for generating static thunks to bound functions
8:71037a47492d 2016-04-17 Christopher Haster Standardized empty argument methods
7:9a62ba2d3d68 2016-04-17 Christopher Haster Add Chainer class for chaining functions
6:b5f0551273f6 2016-04-17 Christopher Haster Fixed issue with boolean casts
5:5c91c61f50b6 2016-04-17 Christopher Haster Added Composer class for static function composition
4:627e19790dd9 2016-04-17 Christopher Haster Removed unecessary static casts
3:84b61e1b050c 2016-04-17 Christopher Haster Added Binder class for static function binding
2:56e1906f7103 2016-04-17 Christopher Haster Small comment changes
1:9c28068feddc 2016-04-17 Christopher Haster Added thunk function for better interoperability with C
0:176c74479de2 2016-04-17 Christopher Haster Added FuncPtr class