1 | //===----- unique_function.h - moveable type-erasing function ---*- 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-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | /// |
9 | /// unique_function works like std::function, but supports move-only callable |
10 | /// objects. |
11 | /// |
12 | /// TODO: Use LLVM's unique_function (llvm/include/llvm/ADT/FunctionExtras.h), |
13 | /// which uses some extra inline storage to avoid heap allocations for |
14 | /// small objects. Using LLVM's unique_function will require first |
15 | /// porting some other utilities like PointerIntPair, PointerUnion, and |
16 | /// PointerLikeTypeTraits. (These are likely to be independently useful |
17 | /// in the orc runtime, so porting will have additional benefits). |
18 | /// |
19 | //===----------------------------------------------------------------------===// |
20 | |
21 | #ifndef ORC_RT_UNIQUE_FUNCTION_H |
22 | #define ORC_RT_UNIQUE_FUNCTION_H |
23 | |
24 | #include <memory> |
25 | |
26 | namespace orc_rt { |
27 | |
28 | namespace unique_function_detail { |
29 | |
30 | template <typename RetT, typename... ArgTs> class Callable { |
31 | public: |
32 | virtual ~Callable() = default; |
33 | virtual RetT call(ArgTs &&...Args) = 0; |
34 | }; |
35 | |
36 | template <typename CallableT, typename RetT, typename... ArgTs> |
37 | class CallableImpl : public Callable<RetT, ArgTs...> { |
38 | public: |
39 | CallableImpl(CallableT &&Callable) : Callable(std::move(Callable)) {} |
40 | RetT call(ArgTs &&...Args) override { |
41 | return Callable(std::forward<ArgTs>(Args)...); |
42 | } |
43 | |
44 | private: |
45 | CallableT Callable; |
46 | }; |
47 | |
48 | } // namespace unique_function_detail |
49 | |
50 | template <typename FnT> class unique_function; |
51 | |
52 | template <typename RetT, typename... ArgTs> |
53 | class unique_function<RetT(ArgTs...)> { |
54 | public: |
55 | unique_function() = default; |
56 | unique_function(std::nullptr_t) {} |
57 | unique_function(unique_function &&) = default; |
58 | unique_function(const unique_function &&) = delete; |
59 | unique_function &operator=(unique_function &&) = default; |
60 | unique_function &operator=(const unique_function &&) = delete; |
61 | |
62 | template <typename CallableT> |
63 | unique_function(CallableT &&Callable) |
64 | : C(std::make_unique< |
65 | unique_function_detail::CallableImpl<CallableT, RetT, ArgTs...>>( |
66 | std::forward<CallableT>(Callable))) {} |
67 | |
68 | RetT operator()(ArgTs... Params) { |
69 | return C->call(std::forward<ArgTs>(Params)...); |
70 | } |
71 | |
72 | explicit operator bool() const { return !!C; } |
73 | |
74 | private: |
75 | std::unique_ptr<unique_function_detail::Callable<RetT, ArgTs...>> C; |
76 | }; |
77 | |
78 | } // namespace orc_rt |
79 | |
80 | #endif // ORC_RT_UNIQUE_FUNCTION_H |
81 | |