I've been researching how to use lambdas as callbacks, but I do not seem to be getting my syntax correct. I am working in C++ via Visual Studio 2013 Community, & I am pretty sure this type of lambda use is supported. I am working on a video library which is based on the FFMPEG libav libraries.
The basic issue is the logging callback provided to library clients of FFMPEG's libav. It has this calling structure:
void logging_callback( void *ptr, int level, const char *fmt, va_list vargs );
This is fine for C or fine for C++ when the logging callback is a static member function, but I want to install a class member function, which needs the this parameter per the C++ nature of calling method functions.
What I have so far looks like: (unrelated portions removed)
class my_libav
{
// logging messages callback for use by library client, any log messages generated
// by the library will be sent thru this to the lib client's logic:
typedef void(*STREAM_LOGGING_CALLBACK_CB) (std::string msg, void* p_object);
// my lib's logger has a void* param so clients can redirect to member functs easier
void SetStreamLoggingCallback(STREAM_LOGGING_CALLBACK_CB p_stream_logger, void* p_object);
// member function to be installed as FFMPEG's av_log_set_callback():
void RedirectLoggingOutputs( void *ptr, int level, const char *fmt, va_list vargs );
STREAM_LOGGING_CALLBACK_CB mp_stream_logging_callback; // client's callback
void* mp_stream_logging_object; // client's data
};
void my_libav::SetStreamLoggingCallback(STREAM_LOGGING_CALLBACK_CB p_stream_logger, void* p_object)
{
mp_stream_logging_callback = p_stream_logger;
mp_stream_logging_object = p_object;
}
void my_libav::RedirectLoggingOutputs(void *ptr, int level, const char *fmt, va_list vargs )
{
// logic that resolves the message and sends through client's callback
// installed int mp_stream_logging_callback
}
my_libav::my_libav()
{
// this is the old static member function I deleted:
// av_log_set_callback( &my_libav::logging_callback );
// I tried using std::bind & placeholders, but this syntax is wrong too:
// namespace ph = std::placeholders;
// auto callback = std::bind( &my_libav::RedirectLoggingOutputs, this, ph::_1, ph::_2, ph::_3, ph::_4 );
//
// av_log_set_callback( callback ); // <- 'callback' is not the correct type
// this is my try at a lambda. I know that the *this* cannot be
// in the capture, but I don't know the right syntax to work around
// this issue:
std::function<void(void *ptr, int level, const char *fmt, va_list vargs )>
f2 = [this](void *ptr, int level, const char *fmt, va_list vargs ){
RedirectLoggingOutputs( ptr, level, fmt, vargs ); };
av_log_set_callback( f2 ); // <- 'f2' is not the correct type, how to fix?
The syntax I am trying to fix is in the last 4 lines. What is the correct form?