mini-llvm
0.1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
FunctionRef.h
Go to the documentation of this file.
1
// SPDX-License-Identifier: MIT
2
3
#pragma once
4
5
#include <functional>
6
#include <memory>
7
#include <type_traits>
8
#include <utility>
9
10
namespace
mini_llvm
{
11
12
template
<
typename
>
13
class
FunctionRef
;
14
15
template
<
typename
R,
typename
... Args>
16
class
FunctionRef
<R (Args...)> {
17
public
:
18
template
<
typename
F>
19
requires
std::is_function_v<F> &&
20
std::is_invocable_r_v<R, F &, Args...>
21
FunctionRef
(F *f) noexcept
22
: bound_(
const_cast<
void
*
>
(
reinterpret_cast<
const
void
*
>
(f))),
23
thunk_([](
void
*bound, Args &&...args) {
24
return
std::invoke_r<R>(
25
reinterpret_cast<
F *
>
(bound), std::forward<Args>(args)...); }) {}
26
27
template
<
typename
F>
28
requires
(!std::is_same_v<std::remove_cv_t<std::remove_reference_t<F>>, FunctionRef> &&
29
std::is_invocable_r_v<R, std::remove_reference_t<F> &, Args...>)
30
FunctionRef
(F &&f) noexcept
31
: bound_(
const_cast<
void
*
>
(
reinterpret_cast<
const
void
*
>
(std::addressof(f)))),
32
thunk_([](
void
*bound, Args &&...args) {
33
return
std::invoke_r<R>(
34
*
reinterpret_cast<
std::remove_reference_t<F> *
>
(bound), std::forward<Args>(args)...); }) {}
35
36
FunctionRef
(
const
FunctionRef
&) =
default
;
37
38
constexpr
FunctionRef
&
operator=
(
const
FunctionRef
&)
noexcept
=
default
;
39
40
R
operator()
(Args... args)
const
{
41
return
thunk_(bound_, std::forward<Args>(args)...);
42
}
43
44
private
:
45
void
*bound_;
46
R (*thunk_)(
void
*, Args &&...);
47
};
48
49
}
// namespace mini_llvm
mini_llvm::FunctionRef< R(Args...)>::FunctionRef
FunctionRef(const FunctionRef &)=default
mini_llvm::FunctionRef< R(Args...)>::operator()
R operator()(Args... args) const
Definition
FunctionRef.h:40
mini_llvm::FunctionRef< R(Args...)>::FunctionRef
FunctionRef(F *f) noexcept
Definition
FunctionRef.h:21
mini_llvm::FunctionRef< R(Args...)>::FunctionRef
FunctionRef(F &&f) noexcept
Definition
FunctionRef.h:30
mini_llvm::FunctionRef< R(Args...)>::operator=
constexpr FunctionRef & operator=(const FunctionRef &) noexcept=default
mini_llvm::FunctionRef
Definition
FunctionRef.h:13
mini_llvm
Definition
GraphColoringAllocator.h:13
include
mini-llvm
utils
FunctionRef.h
Generated by
1.17.0