aboutsummaryrefslogtreecommitdiff
path: root/src/common/Asio/ExpectedCompletionHandler.h
blob: 6379b9682e89f553425df2f372c87d0418d7a512 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef TRINITYCORE_EXPECTED_COMPLETION_HANDLER_H
#define TRINITYCORE_EXPECTED_COMPLETION_HANDLER_H

#include <boost/asio/associated_executor.hpp>
#include <boost/asio/async_result.hpp>
#include <boost/asio/detail/handler_cont_helpers.hpp>
#include <boost/outcome/result.hpp>
#include <boost/preprocessor/empty.hpp>
#include <type_traits>

namespace Trinity::Asio
{
/// The AsExpected class is used to indicate that any arguments to the
/// completion handler should be combined and passed as a single @c boost::outcome_v2::result argument.
/// The arguments are first moved into a @c boost::outcome_v2::result and that is then
/// passed to the completion handler.
template <typename CompletionToken>
class AsExpected
{
public:
    // First dummy argument is used to prevent the "default" constructor from being used for implicit conversions
    constexpr AsExpected(std::type_identity<void> = {}, CompletionToken token = {})
        : token_(std::forward<CompletionToken>(token))
    {
    }

    template <typename T> requires (!std::same_as<T, AsExpected>)
    constexpr explicit AsExpected(T&& completion_token)
        : token_(std::forward<T>(completion_token))
    {
    }

    /// Adapts an executor to add the @c AsExpected completion token as the
    /// default.
    template <typename InnerExecutor>
    struct executor_with_default : InnerExecutor
    {
        /// Specify @c AsExpected as the default completion token type.
        typedef AsExpected default_completion_token_type;

        /// Construct the adapted executor from the inner executor type.
        template <typename InnerExecutor1>
        executor_with_default(InnerExecutor1 const& ex,
            std::enable_if_t<
                std::conditional_t<
                    !std::is_same_v<InnerExecutor1, executor_with_default>,
                    std::is_convertible<InnerExecutor1, InnerExecutor>,
                    std::false_type
                >::value,
                int
            > = 0) noexcept
            : InnerExecutor(ex)
        {
        }
    };

    /// Type alias to adapt an I/O object to use @c AsExpected as its
    /// default completion token type.
    template <typename T>
    using as_default_on_t = typename T::template rebind_executor<executor_with_default<typename T::executor_type>>::other;

    /// Function helper to adapt an I/O object to use @c AsExpected as its
    /// default completion token type.
    template <typename T>
    static auto as_default_on(T&& object)
    {
        return as_default_on_t<std::decay_t<T>>(static_cast<T&&>(object));
    }

    CompletionToken token_;
};

struct AsExpectedFn
{
    /// Adapt a @ref completion_token to specify that the completion handler
    /// arguments should be combined into a single @c boost::outcome_v2::result argument.
    template <typename CompletionToken>
    [[nodiscard]] inline constexpr AsExpected<std::decay_t<CompletionToken>> operator()(CompletionToken&& completion_token) const
    {
        return AsExpected<std::decay_t<CompletionToken>>(static_cast<CompletionToken&&>(completion_token));
    }
};

/// A function object that adapts a @ref completion_token to specify that the
/// completion handler arguments should be combined into a single @c boost::outcome_v2::result
/// argument.
///
/// May also be used directly as a completion token, in which case it adapts the
/// asynchronous operation's default completion token (or @ref boost::asio::deferred
/// if no default is available).
inline constexpr AsExpectedFn as_expected;

namespace Impl
{
template <typename T>
concept CompletionTokenError = std::same_as<std::remove_cvref_t<T>, boost::system::error_code>
    || std::same_as<std::remove_cvref_t<T>, std::exception_ptr>;

template <typename Handler>
class AsExpectedHandler
{
public:
    typedef void result_type;

    template <typename CompletionToken>
    AsExpectedHandler(AsExpected<CompletionToken> e) : handler_(static_cast<CompletionToken&&>(e.token_)) { }

    template <typename RedirectedHandler> requires (!std::same_as<RedirectedHandler, AsExpectedHandler>)
    AsExpectedHandler(RedirectedHandler&& h) : handler_(std::forward<RedirectedHandler>(h)) { }

    template <CompletionTokenError Error>
    inline void operator()(Error&& e)
    {
        using return_type = boost::outcome_v2::result<void, std::remove_cvref_t<Error>>;

        if (e)
            static_cast<Handler&&>(handler_)(return_type(boost::outcome_v2::failure(std::forward<Error>(e))));
        else
            static_cast<Handler&&>(handler_)(return_type(boost::outcome_v2::success()));
    }

    template <CompletionTokenError Error, typename Arg>
    inline void operator()(Error&& e, Arg&& value)
    {
        using return_type = boost::outcome_v2::result<std::decay_t<Arg>, std::remove_cvref_t<Error>>;

        if (e)
            static_cast<Handler&&>(handler_)(return_type(boost::outcome_v2::failure(std::forward<Error>(e))));
        else
            static_cast<Handler&&>(handler_)(return_type(boost::outcome_v2::success(std::forward<Arg>(value))));
    }

    template <CompletionTokenError Error, typename Arg, typename... Args>
    inline void operator()(Error&& e, Arg&& first, Args&&... rest)
    {
        using return_type = boost::outcome_v2::result<std::tuple<std::decay_t<Arg>, std::decay_t<Args>...>, std::remove_cvref_t<Error>>;

        if (e)
            static_cast<Handler&&>(handler_)(return_type(boost::outcome_v2::failure(std::forward<Error>(e))));
        else
            static_cast<Handler&&>(handler_)(return_type(boost::outcome_v2::success(std::make_tuple(std::forward<Arg>(first), std::forward<Args>(rest)...))));
    }

    Handler handler_;
};

template <typename Handler>
inline bool asio_handler_is_continuation(AsExpectedHandler<Handler>* this_handler)
{
    return boost_asio_handler_cont_helpers::is_continuation(this_handler->handler_);
}

template <typename Signature>
struct AsExpectedSignature;

#define STAMP_AS_EXPECTED_SIGNATURE(qualifier) \
    template <typename R, CompletionTokenError Error> \
    struct AsExpectedSignature<R(Error) qualifier> \
    { \
        using type = R(boost::outcome_v2::result<void, std::remove_cvref_t<Error>>) qualifier; \
    }; \
    template <typename R, CompletionTokenError Error, typename Arg> \
    struct AsExpectedSignature<R(Error, Arg) qualifier> \
    { \
        using type = R(boost::outcome_v2::result<std::decay_t<Arg>, std::remove_cvref_t<Error>>) qualifier; \
    }; \
    template <typename R, CompletionTokenError Error, typename Arg, typename... Args> \
    struct AsExpectedSignature<R(Error, Arg, Args...) qualifier> \
    { \
        using type = R(boost::outcome_v2::result<std::tuple<std::decay_t<Arg>, std::decay_t<Args>...>, std::remove_cvref_t<Error>>) qualifier; \
    };

STAMP_AS_EXPECTED_SIGNATURE(BOOST_PP_EMPTY());
STAMP_AS_EXPECTED_SIGNATURE(&);
STAMP_AS_EXPECTED_SIGNATURE(&&);
STAMP_AS_EXPECTED_SIGNATURE(noexcept);
STAMP_AS_EXPECTED_SIGNATURE(& noexcept);
STAMP_AS_EXPECTED_SIGNATURE(&& noexcept);

} // namespace Impl
}

namespace boost::asio
{
#if BOOST_VERSION >= 107700
template <typename CompletionToken, typename... Signatures>
class async_result<Trinity::Asio::AsExpected<CompletionToken>, Signatures...> : async_result<CompletionToken, typename Trinity::Asio::Impl::AsExpectedSignature<Signatures>::type...>
{
    template <typename Initiation>
    struct init_wrapper
    {
        explicit init_wrapper(Initiation const& initiation) : initiation_(initiation) { }
        explicit init_wrapper(Initiation&& initiation) : initiation_(std::move(initiation)) { }

        template <typename Handler, typename... Args>
        inline void operator()(Handler&& handler, Args&&... args) &&
        {
            static_cast<Initiation&&>(initiation_)(Trinity::Asio::Impl::AsExpectedHandler<std::decay_t<Handler>>(std::forward<Handler>(handler)), std::forward<Args>(args)...);
        }

        template <typename Handler, typename... Args>
        inline void operator()(Handler&& handler, Args&&... args) const &
        {
            static_cast<Initiation const&>(initiation_)(Trinity::Asio::Impl::AsExpectedHandler<std::decay_t<Handler>>(std::forward<Handler>(handler)), std::forward<Args>(args)...);
        }

        Initiation initiation_;
    };

public:
    template <typename Initiation, typename RawCompletionToken, typename... Args>
    static inline auto initiate(Initiation&& initiation, RawCompletionToken&& token, Args&&... args)
    {
        return async_initiate<
            conditional_t<
            is_const<remove_reference_t<RawCompletionToken>>::value,
            CompletionToken const, CompletionToken>,
            typename Trinity::Asio::Impl::AsExpectedSignature<Signatures>::type...>(
                init_wrapper<std::decay_t<Initiation>>(
                    std::forward<Initiation>(initiation)),
                token.token_, std::forward<Args>(args)...);
    }
};

template <template <typename, typename> class Associator, typename Handler, typename DefaultCandidate, typename _>
struct associator;

template <template <typename, typename> class Associator, typename Handler, typename DefaultCandidate>
struct associator<Associator, Trinity::Asio::Impl::AsExpectedHandler<Handler>, DefaultCandidate, void> : Associator<Handler, DefaultCandidate>
{
    static inline auto get(Trinity::Asio::Impl::AsExpectedHandler<Handler> const& h) noexcept
    {
        return Associator<Handler, DefaultCandidate>::get(h.handler_);
    }

    static inline auto get(Trinity::Asio::Impl::AsExpectedHandler<Handler> const& h, DefaultCandidate const& c) noexcept
    {
        return Associator<Handler, DefaultCandidate>::get(h.handler_, c);
    }
};

template <typename... Signatures>
class async_result<Trinity::Asio::AsExpectedFn, Signatures...>
{
public:
    template <typename Initiation, typename RawCompletionToken, typename... Args>
    static inline auto initiate(Initiation&& initiation, RawCompletionToken&&, Args&&... args)
    {
        return async_initiate<Signatures...>(
            std::forward<Initiation>(initiation),
            Trinity::Asio::AsExpected<
            default_completion_token_t<associated_executor_t<Initiation>>>{},
            std::forward<Args>(args)...);
    }
};
#else
template <typename CompletionToken, typename Signature>
class async_result<Trinity::Asio::AsExpected<CompletionToken>, Signature> : async_result<CompletionToken, typename Trinity::Asio::Impl::AsExpectedSignature<Signature>::type>
{
    template <typename Initiation>
    struct init_wrapper
    {
        explicit init_wrapper(Initiation const& initiation) : initiation_(initiation) { }
        explicit init_wrapper(Initiation&& initiation) : initiation_(std::move(initiation)) { }

        template <typename Handler, typename... Args>
        inline void operator()(Handler&& handler, Args&&... args) &&
        {
            static_cast<Initiation&&>(initiation_)(Trinity::Asio::Impl::AsExpectedHandler<std::decay_t<Handler>>(std::forward<Handler>(handler)), std::forward<Args>(args)...);
        }

        template <typename Handler, typename... Args>
        inline void operator()(Handler&& handler, Args&&... args) const &
        {
            static_cast<Initiation const&>(initiation_)(Trinity::Asio::Impl::AsExpectedHandler<std::decay_t<Handler>>(std::forward<Handler>(handler)), std::forward<Args>(args)...);
        }

        Initiation initiation_;
    };

public:
    template <typename Initiation, typename RawCompletionToken, typename... Args>
    static inline auto initiate(Initiation&& initiation, RawCompletionToken&& token, Args&&... args)
    {
        return async_initiate<
            conditional_t<
            is_const<remove_reference_t<RawCompletionToken>>::value,
            CompletionToken const, CompletionToken>,
            typename Trinity::Asio::Impl::AsExpectedSignature<Signature>::type>(
                init_wrapper<std::decay_t<Initiation>>(
                    std::forward<Initiation>(initiation)),
                token.token_, std::forward<Args>(args)...);
    }
};
#endif
} // namespace boost::asio

#endif // TRINITYCORE_EXPECTED_COMPLETION_HANDLER_H