summaryrefslogtreecommitdiff
path: root/boost/beast/experimental/core/detail/timeout_work_guard.hpp
blob: 463567ae7860db8d3cc4324c47ecdfac5bb9f8d2 (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
//
// Copyright (c) 2018 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_DETAIL_TIMEOUT_WORK_GUARD_HPP
#define BOOST_BEAST_CORE_DETAIL_TIMEOUT_WORK_GUARD_HPP

#include <boost/beast/experimental/core/detail/timeout_service.hpp>
#include <boost/assert.hpp>
#include <boost/core/exchange.hpp>

namespace boost {
namespace beast {
namespace detail {

class timeout_work_guard
{
    timeout_object* obj_;

public:
    timeout_work_guard(timeout_work_guard const&) = delete;
    timeout_work_guard& operator=(timeout_work_guard&&) = delete;
    timeout_work_guard& operator=(timeout_work_guard const&) = delete;

    ~timeout_work_guard()
    {
        reset();
    }

    timeout_work_guard(timeout_work_guard&& other)
        : obj_(boost::exchange(other.obj_, nullptr))
    {
    }

    explicit
    timeout_work_guard(timeout_object& obj)
        : obj_(&obj)
    {
        obj_->service().on_work_started(*obj_);
    }

    bool
    owns_work() const
    {
        return obj_ != nullptr;
    }

    void
    reset()
    {
        if(obj_)
            obj_->service().on_work_stopped(*obj_);
    }

    void
    complete()
    {
        BOOST_ASSERT(obj_ != nullptr);
        obj_->service().on_work_complete(*obj_);
        obj_ = nullptr;
    }
};

} // detail
} // beast
} // boost

#endif