MeshLib C++ Docs
Loading...
Searching...
No Matches
MRFunctional.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <functional>
6#include <utility>
7
8namespace MR
9{
12
13
14template <typename F>
16
22template <typename R, typename... Args>
23class FunctionRef<R ( Args... )>
24{
25public:
26 constexpr FunctionRef() noexcept = delete;
27
28 constexpr FunctionRef( const FunctionRef& rhs ) noexcept = default;
29
30 template <
31 typename F,
32 std::enable_if_t<
33 std::is_invocable_r_v<R, F&&, Args...>
34 > * = nullptr
35 >
36 constexpr FunctionRef( F&& f ) noexcept
37 {
38 obj_ = reinterpret_cast<void *>( std::addressof( f ) );
39 callback_ = [] ( void *obj, Args... args ) -> R
40 {
41 return std::invoke(
42 *reinterpret_cast<std::add_pointer_t<F>>( obj ),
43 std::forward<Args>( args )...
44 );
45 };
46 }
47
48 constexpr FunctionRef& operator =( const FunctionRef& rhs ) noexcept = default;
49
50 template <
51 typename F,
52 std::enable_if_t<
53 std::is_invocable_r_v<R, F&&, Args...>
54 >* = nullptr
55 >
56 constexpr FunctionRef& operator =( F&& f ) noexcept
57 {
58 obj_ = reinterpret_cast<void *>( std::addressof( f ) );
59 callback_ = [] ( void *obj, Args... args ) -> R
60 {
61 return std::invoke(
62 *reinterpret_cast<std::add_pointer_t<F>>( obj ),
63 std::forward<Args>( args )...
64 );
65 };
66 return *this;
67 }
68
69 constexpr void swap( FunctionRef& rhs ) noexcept
70 {
71 std::swap( obj_, rhs.obj_ );
72 std::swap( callback_, rhs.callback_ );
73 }
74
75 R operator ()( Args... args ) const
76 {
77 return callback_( obj_, std::forward<Args>( args )... );
78 }
79
80private:
81 void *obj_ = nullptr;
82 R (*callback_)( void*, Args... ) = nullptr;
83};
84
85template <typename R, typename... Args>
86constexpr void swap( FunctionRef<R ( Args... )>& lhs, FunctionRef<R ( Args... )>& rhs ) noexcept
87{
88 lhs.swap( rhs );
89}
90
91}
Definition MRFunctional.h:15
constexpr FunctionRef() noexcept=delete
constexpr void swap(FunctionRef< R(Args...)> &lhs, FunctionRef< R(Args...)> &rhs) noexcept
Definition MRFunctional.h:86
constexpr void swap(FunctionRef &rhs) noexcept
Definition MRFunctional.h:69
only for bindings generation
Definition MRCameraOrientationPlugin.h:8