asrt
Automated System Runtime Testing library
Loading...
Searching...
No Matches
callback.hpp
1
11#pragma once
12
13#include <concepts>
14#include <type_traits>
15
16namespace asrt
17{
18
26template < typename FnPtr >
27struct callback;
28
29template < typename R, typename... Args >
30struct callback< R ( * )( void*, Args... ) >
31{
32 using fn_type = R ( * )( void*, Args... );
33
34 fn_type fn = nullptr;
35 void* ptr = nullptr;
36
37 callback() = default;
38
39 callback( fn_type f, void* p )
40 : fn( f )
41 , ptr( p )
42 {
43 }
44
45 template < typename CB >
46 requires( !std::is_same_v< std::remove_cvref_t< CB >, callback > &&
47 std::invocable< CB&, Args... > )
48 callback( CB& cb )
49 : fn( &trampoline< CB > )
50 , ptr( &cb )
51 {
52 }
53
54 R operator()( Args... args ) const { return fn( ptr, args... ); }
55
56 explicit operator bool() const { return fn != nullptr; }
57
58private:
59 template < typename CB >
60 static R trampoline( void* p, Args... args )
61 {
62 return ( *reinterpret_cast< CB* >( p ) )( args... );
63 }
64};
65
66} // namespace asrt
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee ...
Definition: callback.hpp:17
A type-erasing wrapper for C-style callback + void* pairs.
Definition: callback.hpp:27