I have the classic problem of using a C library that uses function callbacks.
I read a lot of articles, but they required a higher level of expertise than I have in order to follow the suggestions. I got to understand the problem pretty clearly - the lack of an object reference. However between boost, std::function, std::bind, typedefs and templates, there were a lot of pieces but I didn't have a single whole picture. It was clear that some of the methods did not apply because they require to change the library's calling function, and that's not an option for me, at least in the short term (the library is open-source).
These were the best articles I found:
The first article was quite comprehensive, but the examples were still abstract. The second article actually helped to demystify this topic quite a bit with a more concrete example. The solution I implemented, from the link below, may be overly complicated, but: (1) it was by far the best documented, and (2) after studying it, I was convinced I could adapt it to my code. At the bottom of the article the author provided a link to their example code that I was able to compile, run and then adapt.
After adapting it to my library, I am now getting compiler errors which I have been unable to solve.
‘MemberFunctionCallback::MemberFunctionCallback(const MemberFunctionCallback&)’
is private within this context
: handler(m), std::function<T>(h) {}
^
The example source code is long (you can see from the link) and don't see how to reduce it to a minimal code, or how decouple it from my library. After looking at the problem and reading up, I suspect my problem stems from my library's complex callback argument list. The article I read, above, has a very short argument list (two ints). My callback function's argument list is:
callback_fn(const char *osc_addrPattern, const char *osc_typeTags, lo_arg ** argv, int argc)
In any case, I suspect this block below is not correct, specifically the correct argument list to StaticInvoke.
template <int context> class DynamicHandlerCallback : public HandlerCallbackBase
{
public:
DynamicHandlerCallback()
: HandlerCallbackBase(&DynamicHandlerCallback<context>::GeneratedStaticFunction)
{
}
private:
static int GeneratedStaticFunction(const char *osc_addrPattern, const char *osc_typeTags, lo_arg ** argv, int argc)
{
return StaticInvoke(context, osc_addrPattern, osc_typeTags, argv, argc); // <= here
}
};
- Is it possible to use my argument list with the solution provided in the article? If so, what is the syntax?
- Along those lines, how would you pass such an argument list to a template class? (I wasn't able to find the answer to this online either)
- Am I approaching this wrong? Would you suggest a different approach?
The code I referenced above can be seen in the web page I provided (last link). I'll be glad to upload the full compiler output.
Thanks.