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
31namespace WTF {
32
33template<typename> class Function;
34
35template <typename Out, typename... In>
36class Function<Out(In...)> {
37 WTF_MAKE_FAST_ALLOCATED;
38public:
39 Function() = default;
40 Function(std::nullptr_t) { }
41
42 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>
43 Function(CallableType&& callable)
44 : m_callableWrapper(std::make_unique<CallableWrapper<CallableType>>(WTFMove(callable)))
45 {
46 }
47
48 template<typename FunctionType, class = typename std::enable_if<std::is_pointer<FunctionType>::value && std::is_function<typename std::remove_pointer<FunctionType>::type>::value>::type>
49 Function(FunctionType f)
50 : m_callableWrapper(std::make_unique<CallableWrapper<FunctionType>>(WTFMove(f)))
51 {
52 }
53
54 Out operator()(In... in) const
55 {
56 ASSERT(m_callableWrapper);
57 return m_callableWrapper->call(std::forward<In>(in)...);
58 }
59
60 explicit operator bool() const { return !!m_callableWrapper; }
61
62 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>
63 Function& operator=(CallableType&& callable)
64 {
65 m_callableWrapper = std::make_unique<CallableWrapper<CallableType>>(WTFMove(callable));
66 return *this;
67 }
68
69 template<typename FunctionType, class = typename std::enable_if<std::is_pointer<FunctionType>::value && std::is_function<typename std::remove_pointer<FunctionType>::type>::value>::type>
70 Function& operator=(FunctionType f)
71 {
72 m_callableWrapper = std::make_unique<CallableWrapper<FunctionType>>(WTFMove(f));
73 return *this;
74 }
75
76 Function& operator=(std::nullptr_t)
77 {
78 m_callableWrapper = nullptr;
79 return *this;
80 }
81
82private:
83 class CallableWrapperBase {
84 WTF_MAKE_FAST_ALLOCATED;
85 public:
86 virtual ~CallableWrapperBase() { }
87
88 virtual Out call(In...) = 0;
89 };
90
91 template<typename CallableType>
92 class CallableWrapper : public CallableWrapperBase {
93 public:
94 explicit CallableWrapper(CallableType&& callable)
95 : m_callable(WTFMove(callable))
96 {
97 }
98
99 CallableWrapper(const CallableWrapper&) = delete;
100 CallableWrapper& operator=(const CallableWrapper&) = delete;
101
102 Out call(In... in) final { return m_callable(std::forward<In>(in)...); }
103
104 private:
105 CallableType m_callable;
106 };
107
108 std::unique_ptr<CallableWrapperBase> m_callableWrapper;
109};
110
111} // namespace WTF
112
113using WTF::Function;
114