1/*
2 * Copyright (C) 2017 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 <wtf/Function.h>
29
30namespace WTF {
31
32template<typename> class CompletionHandler;
33
34// Wraps a Function to make sure it is always called once and only once.
35template <typename Out, typename... In>
36class CompletionHandler<Out(In...)> {
37public:
38 CompletionHandler() = default;
39
40 template<typename CallableType, class = typename std::enable_if<std::is_rvalue_reference<CallableType&&>::value>::type>
41 CompletionHandler(CallableType&& callable)
42 : m_function(WTFMove(callable))
43 {
44 }
45
46 CompletionHandler(CompletionHandler&&) = default;
47 CompletionHandler& operator=(CompletionHandler&&) = default;
48
49 ~CompletionHandler()
50 {
51 ASSERT_WITH_MESSAGE(!m_function, "Completion handler should always be called");
52 }
53
54 explicit operator bool() const { return !!m_function; }
55
56 Out operator()(In... in)
57 {
58 ASSERT_WITH_MESSAGE(m_function, "Completion handler should not be called more than once");
59 return std::exchange(m_function, nullptr)(std::forward<In>(in)...);
60 }
61
62private:
63 Function<Out(In...)> m_function;
64};
65
66namespace Detail {
67
68template<typename Out, typename... In>
69class CallableWrapper<CompletionHandler<Out(In...)>, Out, In...> : public CallableWrapperBase<Out, In...> {
70public:
71 explicit CallableWrapper(CompletionHandler<Out(In...)>&& completionHandler)
72 : m_completionHandler(WTFMove(completionHandler))
73 {
74 RELEASE_ASSERT(m_completionHandler);
75 }
76 Out call(In... in) final { return m_completionHandler(std::forward<In>(in)...); }
77private:
78 CompletionHandler<Out(In...)> m_completionHandler;
79};
80
81} // namespace Detail
82
83class CompletionHandlerCallingScope {
84public:
85 CompletionHandlerCallingScope() = default;
86
87 CompletionHandlerCallingScope(CompletionHandler<void()>&& completionHandler)
88 : m_completionHandler(WTFMove(completionHandler))
89 { }
90
91 ~CompletionHandlerCallingScope()
92 {
93 if (m_completionHandler)
94 m_completionHandler();
95 }
96
97 CompletionHandlerCallingScope(CompletionHandlerCallingScope&&) = default;
98 CompletionHandlerCallingScope& operator=(CompletionHandlerCallingScope&&) = default;
99
100 CompletionHandler<void()> release() { return WTFMove(m_completionHandler); }
101
102private:
103 CompletionHandler<void()> m_completionHandler;
104};
105
106} // namespace WTF
107
108using WTF::CompletionHandler;
109using WTF::CompletionHandlerCallingScope;
110