brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.5 KiB · 5c43d22 Raw
71 lines · c
1//===-- Self contained functional header ------------------------*- C++ -*-===//2//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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_FUNCTIONAL_H10#define LLVM_LIBC_SRC___SUPPORT_CPP_FUNCTIONAL_H11 12#include "hdr/stdint_proxy.h"13#include "src/__support/CPP/type_traits/enable_if.h"14#include "src/__support/CPP/type_traits/is_convertible.h"15#include "src/__support/CPP/type_traits/is_same.h"16#include "src/__support/CPP/type_traits/is_void.h"17#include "src/__support/CPP/type_traits/remove_cvref.h"18#include "src/__support/CPP/type_traits/remove_reference.h"19#include "src/__support/CPP/utility/forward.h"20#include "src/__support/macros/attributes.h"21#include "src/__support/macros/config.h"22 23namespace LIBC_NAMESPACE_DECL {24namespace cpp {25 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 to28/// store a function.29template <typename Fn> class function;30 31template <typename Ret, typename... Params> class function<Ret(Params...)> {32  Ret (*callback)(intptr_t callable, Params... params) = nullptr;33  intptr_t callable;34 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)...);39  }40 41public:42  LIBC_INLINE function() = default;43  LIBC_INLINE function(decltype(nullptr)) {}44  LIBC_INLINE ~function() = default;45 46  template <typename Callable>47  LIBC_INLINE function(48      Callable &&callable,49      // This is not the copy-constructor.50      enable_if_t<!cpp::is_same_v<remove_cvref_t<Callable>, function>> * =51          nullptr,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>>56          * = nullptr)57      : callback(callback_fn<cpp::remove_reference_t<Callable>>),58        callable(reinterpret_cast<intptr_t>(&callable)) {}59 60  LIBC_INLINE Ret operator()(Params... params) const {61    return callback(callable, cpp::forward<Params>(params)...);62  }63 64  LIBC_INLINE explicit operator bool() const { return callback; }65};66 67} // namespace cpp68} // namespace LIBC_NAMESPACE_DECL69 70#endif // LLVM_LIBC_SRC___SUPPORT_CPP_FUNCTIONAL_H71