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
26namespace orc_rt {
27
28namespace unique_function_detail {
29
30template <typename RetT, typename... ArgTs> class Callable {
31public:
32 virtual ~Callable() = default;
33 virtual RetT call(ArgTs &&...Args) = 0;
34};
35
36template <typename CallableT, typename RetT, typename... ArgTs>
37class CallableImpl : public Callable<RetT, ArgTs...> {
38public:
39 CallableImpl(CallableT &&Callable) : Callable(std::move(Callable)) {}
40 RetT call(ArgTs &&...Args) override {
41 return Callable(std::forward<ArgTs>(Args)...);
42 }
43
44private:
45 CallableT Callable;
46};
47
48} // namespace unique_function_detail
49
50template <typename FnT> class unique_function;
51
52template <typename RetT, typename... ArgTs>
53class unique_function<RetT(ArgTs...)> {
54public:
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
74private:
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

source code of compiler-rt/lib/orc/unique_function.h