Path: blob/main/contrib/llvm-project/libc/src/__support/CPP/functional.h
213799 views
//===-- Self contained functional header ------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_FUNCTIONAL_H9#define LLVM_LIBC_SRC___SUPPORT_CPP_FUNCTIONAL_H1011#include "src/__support/CPP/type_traits/enable_if.h"12#include "src/__support/CPP/type_traits/is_convertible.h"13#include "src/__support/CPP/type_traits/is_same.h"14#include "src/__support/CPP/type_traits/is_void.h"15#include "src/__support/CPP/type_traits/remove_cvref.h"16#include "src/__support/CPP/type_traits/remove_reference.h"17#include "src/__support/CPP/utility/forward.h"18#include "src/__support/macros/attributes.h"19#include "src/__support/macros/config.h"2021#include <stdint.h>2223namespace LIBC_NAMESPACE_DECL {24namespace cpp {2526/// 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;3031template <typename Ret, typename... Params> class function<Ret(Params...)> {32Ret (*callback)(intptr_t callable, Params... params) = nullptr;33intptr_t callable;3435template <typename Callable>36LIBC_INLINE static Ret callback_fn(intptr_t callable, Params... params) {37return (*reinterpret_cast<Callable *>(callable))(38cpp::forward<Params>(params)...);39}4041public:42LIBC_INLINE function() = default;43LIBC_INLINE function(decltype(nullptr)) {}44LIBC_INLINE ~function() = default;4546template <typename Callable>47LIBC_INLINE function(48Callable &&callable,49// This is not the copy-constructor.50enable_if_t<!cpp::is_same_v<remove_cvref_t<Callable>, function>> * =51nullptr,52// Functor must be callable and return a suitable type.53enable_if_t<cpp::is_void_v<Ret> ||54cpp::is_convertible_v<55decltype(declval<Callable>()(declval<Params>()...)), Ret>>56* = nullptr)57: callback(callback_fn<cpp::remove_reference_t<Callable>>),58callable(reinterpret_cast<intptr_t>(&callable)) {}5960LIBC_INLINE Ret operator()(Params... params) const {61return callback(callable, cpp::forward<Params>(params)...);62}6364LIBC_INLINE explicit operator bool() const { return callback; }65};6667} // namespace cpp68} // namespace LIBC_NAMESPACE_DECL6970#endif // LLVM_LIBC_SRC___SUPPORT_CPP_FUNCTIONAL_H717273