vendredi 27 octobre 2017

How to wrap function with prefix and suffix in C

I'd like to know how to solve the wrapper problem from the Stroustrup paper but in C. I'm trying to find an efficient way to call

// prefix
GenericFunctionCallThatCouldHaveAnyNumberOfArgs();
// suffix

I've thought about creating a proxy function that takes an input a function pointer but the functions I want to wrap do not all have the same function signature.

My current solution is to create a Macro:

#define CALL(func) prefix; func; suffix;
CALL(myfunction(a, 'b', 1))

It works but it makes the code harder to understand especially when the prefix and suffix are complicated. Also the prefix and suffix are not necessarily calls to functions, they can be enclosures too. Is there a design pattern in C that does this efficiently (in terms of lines of code) while still maintaining readability.

Aucun commentaire:

Enregistrer un commentaire