Template Function rclcpp::detail::cpp_callback_trampoline

Function Documentation

template<typename UserDataRealT, typename UserDataT, typename ...Args, typename ReturnT = void>
ReturnT rclcpp::detail::cpp_callback_trampoline(UserDataT user_data, Args... args) noexcept

Trampoline pattern for wrapping std::function into C-style callbacks.

A common pattern in C is for a function to take a function pointer and a void pointer for “user data” which is passed to the function pointer when it is called from within C.

It works by using the user data pointer to store a pointer to a std::function instance. So when called from C, this function will cast the user data to the right std::function type and call it.

This should allow you to use free functions, lambdas with and without captures, and various kinds of std::bind instances.

The interior of this function is likely to be executed within a C runtime, so no exceptions should be thrown at this point, and doing so results in undefined behavior.

Template Parameters:
  • UserDataRealT – Declared type of the passed function

  • UserDataT – Deduced type based on what is passed for user data, usually this type is either void * or const void *.

  • Args – the arguments being passed to the callback

  • ReturnT – the return type of this function and the callback, default void

Parameters:
  • user_data – the function pointer, possibly type erased

  • args – the arguments to be forwarded to the callback

Returns:

whatever the callback returns, if anything