Pigweed
 
Loading...
Searching...
No Matches
pointer.h File Reference
#include <utility>
#include "pw_function/internal/static_invoker.h"

Go to the source code of this file.

Namespaces

namespace  pw
 Provides basic helpers for reading and writing UTF-8 encoded strings.
 

Functions

template<typename FunctionType >
constexpr auto pw::function::GetFunctionPointer ()
 
template<typename FunctionType >
constexpr auto pw::function::GetFunctionPointer (const FunctionType &)
 
template<typename FunctionType >
constexpr auto pw::function::GetFunctionPointerContextFirst ()
 
template<typename FunctionType >
constexpr auto pw::function::GetFunctionPointerContextFirst (const FunctionType &)
 

Detailed Description

Traditional callback APIs often use a function pointer and void* context argument. The context argument makes it possible to use the callback function with non-global data. For example, the qsort_s and bsearch_s functions take a pointer to a comparison function that has void* context as its last parameter.

embed:rst:inline :cpp:type:`pw::Function` 

does not naturally work with these kinds of APIs.

The functions below make it simple to adapt a

embed:rst:inline :cpp:type:`pw::Function` 

for use with APIs that accept a function pointer and void* context argument.

Function Documentation

◆ GetFunctionPointer() [1/2]

template<typename FunctionType >
constexpr auto pw::function::GetFunctionPointer ( )
constexpr

Returns a function pointer that invokes a pw::Function, lambda, or other callable object from a void* context argument. This makes it possible to use C++ callables with C-style APIs that take a function pointer and void* context.

The returned function pointer has the same return type and arguments as the pw::Function or pw::Callback, except that the last parameter is a void*. GetFunctionPointerContextFirst places the void* context parameter first.

The following example adapts a C++ lambda function for use with C-style API that takes an int (*)(int, void*) function and a void* context.

void TakesAFunctionPointer(int (*function)(int, void*), void* context);
void UseFunctionPointerApiWithPwFunction() {
// Declare a callable object so a void* pointer can be obtained for it.
auto my_function = [captures](int value) {
// ...
return value + captures;
};
// Invoke the API with the function pointer and callable pointer.
TakesAFunctionPointer(pw::function::GetFunctionPointer(my_function),
&my_function);
}

The function returned from this must ONLY be used with the exact type for which it was created! Function pointer / context APIs are not type safe.

◆ GetFunctionPointer() [2/2]

template<typename FunctionType >
constexpr auto pw::function::GetFunctionPointer ( const FunctionType &  )
constexpr

GetFunctionPointer overload that uses the type of the function passed to this call.

◆ GetFunctionPointerContextFirst() [1/2]

template<typename FunctionType >
constexpr auto pw::function::GetFunctionPointerContextFirst ( )
constexpr

Same as GetFunctionPointer, but the context argument is passed first. Returns a void(void*, int) function for a pw::Function<void(int)>.

◆ GetFunctionPointerContextFirst() [2/2]

template<typename FunctionType >
constexpr auto pw::function::GetFunctionPointerContextFirst ( const FunctionType &  )
constexpr

GetFunctionPointerContextFirst overload that uses the type of the function passed to this call.