1 //===-- Self contained functional header ------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC___SUPPORT_CPP_FUNCTIONAL_H
10 #define LLVM_LIBC_SRC___SUPPORT_CPP_FUNCTIONAL_H
12 #include "src/__support/CPP/type_traits/enable_if.h"
13 #include "src/__support/CPP/type_traits/is_convertible.h"
14 #include "src/__support/CPP/type_traits/is_same.h"
15 #include "src/__support/CPP/type_traits/is_void.h"
16 #include "src/__support/CPP/type_traits/remove_cvref.h"
17 #include "src/__support/CPP/type_traits/remove_reference.h"
18 #include "src/__support/CPP/utility/forward.h"
19 #include "src/__support/macros/attributes.h"
23 namespace LIBC_NAMESPACE
{
26 /// A function type adapted from LLVM's function_ref.
27 /// This class does not own the callable, so it is not in general safe to
29 template <typename Fn
> class function
;
31 template <typename Ret
, typename
... Params
> class function
<Ret(Params
...)> {
32 Ret (*callback
)(intptr_t callable
, Params
... params
) = nullptr;
35 template <typename Callable
>
36 LIBC_INLINE
static Ret
callback_fn(intptr_t callable
, Params
... params
) {
37 return (*reinterpret_cast<Callable
*>(callable
))(
38 cpp::forward
<Params
>(params
)...);
42 LIBC_INLINE
function() = default;
43 LIBC_INLINE
function(decltype(nullptr)) {}
44 LIBC_INLINE
~function() = default;
46 template <typename Callable
>
49 // This is not the copy-constructor.
50 enable_if_t
<!cpp::is_same_v
<remove_cvref_t
<Callable
>, function
>> * =
52 // Functor must be callable and return a suitable type.
53 enable_if_t
<cpp::is_void_v
<Ret
> ||
54 cpp::is_convertible_v
<
55 decltype(declval
<Callable
>()(declval
<Params
>()...)), Ret
>>
57 : callback(callback_fn
<cpp::remove_reference_t
<Callable
>>),
58 callable(reinterpret_cast<intptr_t>(&callable
)) {}
60 LIBC_INLINE Ret
operator()(Params
... params
) const {
61 return callback(callable
, cpp::forward
<Params
>(params
)...);
64 LIBC_INLINE
explicit operator bool() const { return callback
; }
68 } // namespace LIBC_NAMESPACE
70 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_FUNCTIONAL_H