1 | /* |
2 | * Copyright (C) 2016 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include <memory> |
29 | #include <wtf/FastMalloc.h> |
30 | |
31 | namespace WTF { |
32 | |
33 | namespace Detail { |
34 | |
35 | template<typename Out, typename... In> |
36 | class CallableWrapperBase { |
37 | WTF_MAKE_FAST_ALLOCATED; |
38 | public: |
39 | virtual ~CallableWrapperBase() { } |
40 | virtual Out call(In...) = 0; |
41 | }; |
42 | |
43 | template<typename, typename, typename...> class CallableWrapper; |
44 | |
45 | template<typename CallableType, typename Out, typename... In> |
46 | class CallableWrapper : public CallableWrapperBase<Out, In...> { |
47 | public: |
48 | explicit CallableWrapper(CallableType&& callable) |
49 | : m_callable(WTFMove(callable)) { } |
50 | CallableWrapper(const CallableWrapper&) = delete; |
51 | CallableWrapper& operator=(const CallableWrapper&) = delete; |
52 | Out call(In... in) final { return m_callable(std::forward<In>(in)...); } |
53 | private: |
54 | CallableType m_callable; |
55 | }; |
56 | |
57 | } // namespace Detail |
58 | |
59 | template<typename> class Function; |
60 | |
61 | template <typename Out, typename... In> |
62 | class Function<Out(In...)> { |
63 | WTF_MAKE_FAST_ALLOCATED; |
64 | public: |
65 | Function() = default; |
66 | Function(std::nullptr_t) { } |
67 | |
68 | template<typename CallableType, class = typename std::enable_if<!(std::is_pointer<CallableType>::value && std::is_function<typename std::remove_pointer<CallableType>::type>::value) && std::is_rvalue_reference<CallableType&&>::value>::type> |
69 | Function(CallableType&& callable) |
70 | : m_callableWrapper(std::make_unique<Detail::CallableWrapper<CallableType, Out, In...>>(WTFMove(callable))) { } |
71 | |
72 | template<typename FunctionType, class = typename std::enable_if<std::is_pointer<FunctionType>::value && std::is_function<typename std::remove_pointer<FunctionType>::type>::value>::type> |
73 | Function(FunctionType f) |
74 | : m_callableWrapper(std::make_unique<Detail::CallableWrapper<FunctionType, Out, In...>>(WTFMove(f))) { } |
75 | |
76 | Out operator()(In... in) const |
77 | { |
78 | ASSERT(m_callableWrapper); |
79 | return m_callableWrapper->call(std::forward<In>(in)...); |
80 | } |
81 | |
82 | explicit operator bool() const { return !!m_callableWrapper; } |
83 | |
84 | template<typename CallableType, class = typename std::enable_if<!(std::is_pointer<CallableType>::value && std::is_function<typename std::remove_pointer<CallableType>::type>::value) && std::is_rvalue_reference<CallableType&&>::value>::type> |
85 | Function& operator=(CallableType&& callable) |
86 | { |
87 | m_callableWrapper = std::make_unique<Detail::CallableWrapper<CallableType, Out, In...>>(WTFMove(callable)); |
88 | return *this; |
89 | } |
90 | |
91 | template<typename FunctionType, class = typename std::enable_if<std::is_pointer<FunctionType>::value && std::is_function<typename std::remove_pointer<FunctionType>::type>::value>::type> |
92 | Function& operator=(FunctionType f) |
93 | { |
94 | m_callableWrapper = std::make_unique<Detail::CallableWrapper<FunctionType, Out, In...>>(WTFMove(f)); |
95 | return *this; |
96 | } |
97 | |
98 | Function& operator=(std::nullptr_t) |
99 | { |
100 | m_callableWrapper = nullptr; |
101 | return *this; |
102 | } |
103 | |
104 | private: |
105 | std::unique_ptr<Detail::CallableWrapperBase<Out, In...>> m_callableWrapper; |
106 | }; |
107 | |
108 | } // namespace WTF |
109 | |
110 | using WTF::Function; |
111 | |