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/Expected.h>
29
30namespace WebCore {
31
32enum class CallbackResultType {
33 Success,
34 ExceptionThrown,
35 UnableToExecute
36};
37
38template<typename ReturnType> class CallbackResult {
39public:
40 CallbackResult(CallbackResultType);
41 CallbackResult(ReturnType&&);
42
43 CallbackResultType type() const;
44 ReturnType&& releaseReturnValue();
45
46private:
47 Expected<ReturnType, CallbackResultType> m_value;
48};
49
50template<> class CallbackResult<void> {
51public:
52 CallbackResult() = default;
53 CallbackResult(CallbackResultType);
54
55 CallbackResultType type() const;
56
57private:
58 CallbackResultType m_type = CallbackResultType::Success;
59};
60
61
62template<typename ReturnType> inline CallbackResult<ReturnType>::CallbackResult(CallbackResultType type)
63 : m_value(makeUnexpected(type))
64{
65}
66
67template<typename ReturnType> inline CallbackResult<ReturnType>::CallbackResult(ReturnType&& returnValue)
68 : m_value(WTFMove(returnValue))
69{
70}
71
72template<typename ReturnType> inline CallbackResultType CallbackResult<ReturnType>::type() const
73{
74 return m_value.has_value() ? CallbackResultType::Success : m_value.error();
75}
76
77template<typename ReturnType> inline auto CallbackResult<ReturnType>::releaseReturnValue() -> ReturnType&&
78{
79 ASSERT(m_value.has_value());
80 return WTFMove(m_value.value());
81}
82
83
84// Void specialization
85
86inline CallbackResult<void>::CallbackResult(CallbackResultType type)
87 : m_type(type)
88{
89}
90
91inline CallbackResultType CallbackResult<void>::type() const
92{
93 return m_type;
94}
95
96}
97