1/*
2 * Copyright (C) 2015-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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <wtf/ForbidHeapAllocation.h>
29
30namespace WTF {
31
32// You can use ScopedLambda to efficiently pass lambdas without allocating memory or requiring
33// template specialization of the callee. The callee should be declared as:
34//
35// void foo(const ScopedLambda<MyThings* (int, Stuff&)>&);
36//
37// The caller just does:
38//
39// void foo(scopedLambda<MyThings* (int, Stuff&)>([&] (int x, Stuff& y) -> MyThings* { blah }));
40//
41// Note that this relies on foo() not escaping the lambda. The lambda is only valid while foo() is
42// on the stack - hence the name ScopedLambda.
43
44template<typename FunctionType> class ScopedLambda;
45template<typename ResultType, typename... ArgumentTypes>
46class ScopedLambda<ResultType (ArgumentTypes...)> {
47 WTF_FORBID_HEAP_ALLOCATION;
48public:
49 ScopedLambda(ResultType (*impl)(void* arg, ArgumentTypes...) = nullptr, void* arg = nullptr)
50 : m_impl(impl)
51 , m_arg(arg)
52 {
53 }
54
55 template<typename... PassedArgumentTypes>
56 ResultType operator()(PassedArgumentTypes&&... arguments) const
57 {
58 return m_impl(m_arg, std::forward<PassedArgumentTypes>(arguments)...);
59 }
60
61private:
62 ResultType (*m_impl)(void* arg, ArgumentTypes...);
63 void *m_arg;
64};
65
66template<typename FunctionType, typename Functor> class ScopedLambdaFunctor;
67template<typename ResultType, typename... ArgumentTypes, typename Functor>
68class ScopedLambdaFunctor<ResultType (ArgumentTypes...), Functor> : public ScopedLambda<ResultType (ArgumentTypes...)> {
69public:
70 template<typename PassedFunctor>
71 ScopedLambdaFunctor(PassedFunctor&& functor)
72 : ScopedLambda<ResultType (ArgumentTypes...)>(implFunction, this)
73 , m_functor(std::forward<PassedFunctor>(functor))
74 {
75 }
76
77 // We need to make sure that copying and moving ScopedLambdaFunctor results in a ScopedLambdaFunctor
78 // whose ScopedLambda supertype still points to this rather than other.
79 ScopedLambdaFunctor(const ScopedLambdaFunctor& other)
80 : ScopedLambda<ResultType (ArgumentTypes...)>(implFunction, this)
81 , m_functor(other.m_functor)
82 {
83 }
84
85 ScopedLambdaFunctor(ScopedLambdaFunctor&& other)
86 : ScopedLambda<ResultType (ArgumentTypes...)>(implFunction, this)
87 , m_functor(WTFMove(other.m_functor))
88 {
89 }
90
91 ScopedLambdaFunctor& operator=(const ScopedLambdaFunctor& other)
92 {
93 m_functor = other.m_functor;
94 return *this;
95 }
96
97 ScopedLambdaFunctor& operator=(ScopedLambdaFunctor&& other)
98 {
99 m_functor = WTFMove(other.m_functor);
100 return *this;
101 }
102
103private:
104 static ResultType implFunction(void* argument, ArgumentTypes... arguments)
105 {
106 return static_cast<ScopedLambdaFunctor*>(argument)->m_functor(arguments...);
107 }
108
109 Functor m_functor;
110};
111
112// Can't simply rely on perfect forwarding because then the ScopedLambdaFunctor would point to the functor
113// by const reference. This would be surprising in situations like:
114//
115// auto scopedLambda = scopedLambda<Foo(Bar)>([&] (Bar) -> Foo { ... });
116//
117// We expected scopedLambda to be valid for its entire lifetime, but if it computed the lambda by reference
118// then it would be immediately invalid.
119template<typename FunctionType, typename Functor>
120ScopedLambdaFunctor<FunctionType, Functor> scopedLambda(const Functor& functor)
121{
122 return ScopedLambdaFunctor<FunctionType, Functor>(functor);
123}
124
125template<typename FunctionType, typename Functor>
126ScopedLambdaFunctor<FunctionType, Functor> scopedLambda(Functor&& functor)
127{
128 return ScopedLambdaFunctor<FunctionType, Functor>(WTFMove(functor));
129}
130
131template<typename FunctionType, typename Functor> class ScopedLambdaRefFunctor;
132template<typename ResultType, typename... ArgumentTypes, typename Functor>
133class ScopedLambdaRefFunctor<ResultType (ArgumentTypes...), Functor> : public ScopedLambda<ResultType (ArgumentTypes...)> {
134public:
135 ScopedLambdaRefFunctor(const Functor& functor)
136 : ScopedLambda<ResultType (ArgumentTypes...)>(implFunction, this)
137 , m_functor(&functor)
138 {
139 }
140
141 // We need to make sure that copying and moving ScopedLambdaRefFunctor results in a
142 // ScopedLambdaRefFunctor whose ScopedLambda supertype still points to this rather than
143 // other.
144 ScopedLambdaRefFunctor(const ScopedLambdaRefFunctor& other)
145 : ScopedLambda<ResultType (ArgumentTypes...)>(implFunction, this)
146 , m_functor(other.m_functor)
147 {
148 }
149
150 ScopedLambdaRefFunctor(ScopedLambdaRefFunctor&& other)
151 : ScopedLambda<ResultType (ArgumentTypes...)>(implFunction, this)
152 , m_functor(other.m_functor)
153 {
154 }
155
156 ScopedLambdaRefFunctor& operator=(const ScopedLambdaRefFunctor& other)
157 {
158 m_functor = other.m_functor;
159 return *this;
160 }
161
162 ScopedLambdaRefFunctor& operator=(ScopedLambdaRefFunctor&& other)
163 {
164 m_functor = other.m_functor;
165 return *this;
166 }
167
168private:
169 static ResultType implFunction(void* argument, ArgumentTypes... arguments)
170 {
171 return (*static_cast<ScopedLambdaRefFunctor*>(argument)->m_functor)(arguments...);
172 }
173
174 const Functor* m_functor;
175};
176
177// This is for when you already refer to a functor by reference, and you know its lifetime is
178// good. This just creates a ScopedLambda that points to your functor.
179//
180// Note that this is always wrong:
181//
182// auto ref = scopedLambdaRef([...] (...) {...});
183//
184// Because the scopedLambdaRef will refer to the lambda by reference, and the lambda will die after the
185// semicolon. Use scopedLambda() in that case.
186template<typename FunctionType, typename Functor>
187ScopedLambdaRefFunctor<FunctionType, Functor> scopedLambdaRef(const Functor& functor)
188{
189 return ScopedLambdaRefFunctor<FunctionType, Functor>(functor);
190}
191
192} // namespace WTF
193
194using WTF::ScopedLambda;
195using WTF::scopedLambda;
196using WTF::scopedLambdaRef;
197