mini-llvm 0.1.0
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
10namespace mini_llvm {
11
12template <typename>
14
15template <typename R, typename... Args>
16class FunctionRef<R (Args...)> {
17public:
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
44private:
45 void *bound_;
46 R (*thunk_)(void *, Args &&...);
47};
48
49} // namespace mini_llvm
FunctionRef(const FunctionRef &)=default
R operator()(Args... args) const
Definition FunctionRef.h:40
FunctionRef(F *f) noexcept
Definition FunctionRef.h:21
FunctionRef(F &&f) noexcept
Definition FunctionRef.h:30
constexpr FunctionRef & operator=(const FunctionRef &) noexcept=default
Definition FunctionRef.h:13
Definition GraphColoringAllocator.h:13