liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
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
//
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//
 
#ifndef BOOST_BEAST_CORE_SAVED_HANDLER_HPP
#define BOOST_BEAST_CORE_SAVED_HANDLER_HPP
 
#include <boost/beast/core/detail/config.hpp>
 
namespace boost {
namespace beast {
 
/** An invocable, nullary function object which holds a completion handler.
 
    This container can hold a type-erased instance of any completion
    handler, or it can be empty. When the container holds a value,
    the implementation maintains an instance of `net::executor_work_guard`
    for the handler's associated executor. Memory is dynamically allocated
    to store the completion handler, and the allocator may optionally
    be specified. Otherwise, the implementation uses the handler's
    associated allocator.
*/
class saved_handler
{
    class base;
 
    template<class, class>
    class impl;
 
    base* p_ = nullptr;
 
public:
    /// Default Constructor
    saved_handler() = default;
 
    /// Copy Constructor (deleted)
    saved_handler(saved_handler const&) = delete;
 
    /// Copy Assignment (deleted)
    saved_handler& operator=(saved_handler const&) = delete;
 
    /// Destructor
    BOOST_BEAST_DECL
    ~saved_handler();
 
    /// Move Constructor
    BOOST_BEAST_DECL
    saved_handler(saved_handler&& other) noexcept;
 
    /// Move Assignment
    BOOST_BEAST_DECL
    saved_handler&
    operator=(saved_handler&& other) noexcept;
 
    /// Returns `true` if `*this` contains a completion handler.
    bool
    has_value() const noexcept
    {
        return p_ != nullptr;
    }
 
    /** Store a completion handler in the container.
 
        Requires `this->has_value() == false`.
 
        @param handler The completion handler to store.
        The implementation takes ownership of the handler by performing a decay-copy.
 
        @param alloc The allocator to use.
    */
    template<class Handler, class Allocator>
    void
    emplace(Handler&& handler, Allocator const& alloc);
 
    /** Store a completion handler in the container.
 
        Requires `this->has_value() == false`. The
        implementation will use the handler's associated
        allocator to obtian storage.
 
        @param handler The completion handler to store.
        The implementation takes ownership of the handler by performing a decay-copy.
    */
    template<class Handler>
    void
    emplace(Handler&& handler);
 
    /** Discard the saved handler, if one exists.
 
        If `*this` contains an object, it is destroyed.
 
        @returns `true` if an object was destroyed.
    */
    BOOST_BEAST_DECL
    bool
    reset() noexcept;
 
    /** Unconditionally invoke the stored completion handler.
 
        Requires `this->has_value() == true`. Any dynamic memory
        used is deallocated before the stored completion handler
        is invoked. The executor work guard is also reset before
        the invocation.
    */
    BOOST_BEAST_DECL
    void
    invoke();
 
    /** Conditionally invoke the stored completion handler.
 
        Invokes the stored completion handler if
        `this->has_value() == true`, otherwise does nothing. Any
        dynamic memory used is deallocated before the stored completion
        handler is invoked. The executor work guard is also reset before
        the invocation.
 
        @return `true` if the invocation took place.
    */
    BOOST_BEAST_DECL
    bool
    maybe_invoke();
};
 
} // beast
} // boost
 
#include <boost/beast/core/impl/saved_handler.hpp>
#ifdef BOOST_BEAST_HEADER_ONLY
#include <boost/beast/core/impl/saved_handler.ipp>
#endif
 
#endif