|
6 | 6 | * LICENSE file in the root directory of this source tree. |
7 | 7 | */ |
8 | 8 |
|
9 | | -//===- llvm/ADT/STLFunctionalExtras.h - Extras for <functional> -*- C++ -*-===// |
10 | | -// |
11 | | -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
12 | | -// See https://llvm.org/LICENSE.txt for license information. |
13 | | -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
14 | | -// |
15 | | -//===----------------------------------------------------------------------===// |
16 | | -// |
17 | | -// This file contains some extension to <functional>. |
18 | | -// |
19 | | -// No library is required when using these functions. |
20 | | -// |
21 | | -//===----------------------------------------------------------------------===// |
22 | | -// Extra additions to <functional> |
23 | | -//===----------------------------------------------------------------------===// |
24 | | - |
25 | | -/// An efficient, type-erasing, non-owning reference to a callable. This is |
26 | | -/// intended for use as the type of a function parameter that is not used |
27 | | -/// after the function in question returns. |
28 | | -/// |
29 | | -/// This class does not own the callable, so it is not in general safe to store |
30 | | -/// a FunctionRef. |
31 | | - |
32 | | -// torch::executor: modified from llvm::function_ref |
33 | | -// - renamed to FunctionRef |
34 | | -// - removed LLVM_GSL_POINTER and LLVM_LIFETIME_BOUND macro uses |
35 | | -// - use namespaced internal::remove_cvref_t |
36 | | - |
37 | | - |
38 | 9 | #pragma once |
39 | 10 |
|
40 | | -#include <cstdint> |
41 | | -#include <type_traits> |
42 | | -#include <utility> |
43 | | - |
44 | | -namespace executorch { |
45 | | -namespace extension { |
46 | | -namespace pytree { |
47 | | - |
48 | | -//===----------------------------------------------------------------------===// |
49 | | -// Features from C++20 |
50 | | -//===----------------------------------------------------------------------===// |
51 | | - |
52 | | -namespace internal { |
53 | | - |
54 | | -template <typename T> |
55 | | -struct remove_cvref { |
56 | | - using type = |
57 | | - typename std::remove_cv<typename std::remove_reference<T>::type>::type; |
58 | | -}; |
59 | | - |
60 | | -template <typename T> |
61 | | -using remove_cvref_t = typename remove_cvref<T>::type; |
62 | | - |
63 | | -} // namespace internal |
64 | | - |
65 | | -template <typename Fn> |
66 | | -class FunctionRef; |
67 | | - |
68 | | -template <typename Ret, typename... Params> |
69 | | -class FunctionRef<Ret(Params...)> { |
70 | | - Ret (*callback)(intptr_t callable, Params ...params) = nullptr; |
71 | | - intptr_t callable; |
72 | | - |
73 | | - template<typename Callable> |
74 | | - static Ret callback_fn(intptr_t callable, Params ...params) { |
75 | | - return (*reinterpret_cast<Callable*>(callable))( |
76 | | - std::forward<Params>(params)...); |
77 | | - } |
78 | | - |
79 | | -public: |
80 | | - FunctionRef() = default; |
81 | | - FunctionRef(std::nullptr_t) {} |
82 | | - |
83 | | - template <typename Callable> |
84 | | - FunctionRef( |
85 | | - Callable &&callable, |
86 | | - // This is not the copy-constructor. |
87 | | - std::enable_if_t<!std::is_same<internal::remove_cvref_t<Callable>, |
88 | | - FunctionRef>::value> * = nullptr, |
89 | | - // Functor must be callable and return a suitable type. |
90 | | - std::enable_if_t<std::is_void<Ret>::value || |
91 | | - std::is_convertible<decltype(std::declval<Callable>()( |
92 | | - std::declval<Params>()...)), |
93 | | - Ret>::value> * = nullptr) |
94 | | - : callback(callback_fn<std::remove_reference_t<Callable>>), |
95 | | - callable(reinterpret_cast<intptr_t>(&callable)) {} |
96 | | - |
97 | | - Ret operator()(Params ...params) const { |
98 | | - return callback(callable, std::forward<Params>(params)...); |
99 | | - } |
| 11 | +#include <executorch/runtime/core/function_ref.h> |
100 | 12 |
|
101 | | - explicit operator bool() const { return callback; } |
| 13 | +/// This header is DEPRECATED; use executorch/runtime/core/function_ref.h directly instead. |
102 | 14 |
|
103 | | - bool operator==(const FunctionRef<Ret(Params...)> &Other) const { |
104 | | - return callable == Other.callable; |
105 | | - } |
106 | | -}; |
107 | | -} // namespace pytree |
108 | | -} // namespace extension |
109 | | -} // namespace executorch |
| 15 | +namespace executorch::extension::pytree { |
| 16 | +using executorch::runtime::FunctionRef; |
| 17 | +} // namespace executorch::extension::pytree |
110 | 18 |
|
111 | | -namespace torch { |
112 | | -namespace executor { |
113 | | -namespace pytree { |
| 19 | +namespace torch::executor::pytree { |
114 | 20 | // TODO(T197294990): Remove these deprecated aliases once all users have moved |
115 | 21 | // to the new `::executorch` namespaces. |
116 | 22 | using ::executorch::extension::pytree::FunctionRef; |
117 | | -} // namespace pytree |
118 | | -} // namespace executor |
119 | | -} // namespace torch |
| 23 | +} // namespace torch::executor::pytree |
0 commit comments