forked from andreasbuhr/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcancellation_source.cpp
More file actions
97 lines (79 loc) · 1.97 KB
/
Copy pathcancellation_source.cpp
File metadata and controls
97 lines (79 loc) · 1.97 KB
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
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/cancellation_source.hpp>
#include "cancellation_state.hpp"
#include <cassert>
cppcoro::cancellation_source::cancellation_source()
: m_state(detail::cancellation_state::create())
{
}
cppcoro::cancellation_source::cancellation_source(const cancellation_source& other) noexcept
: m_state(other.m_state)
{
if (m_state != nullptr)
{
m_state->add_source_ref();
}
}
cppcoro::cancellation_source::cancellation_source(cancellation_source&& other) noexcept
: m_state(other.m_state)
{
other.m_state = nullptr;
}
cppcoro::cancellation_source::~cancellation_source()
{
if (m_state != nullptr)
{
m_state->release_source_ref();
}
}
cppcoro::cancellation_source& cppcoro::cancellation_source::operator=(const cancellation_source& other) noexcept
{
if (m_state != other.m_state)
{
if (m_state != nullptr)
{
m_state->release_source_ref();
}
m_state = other.m_state;
if (m_state != nullptr)
{
m_state->add_source_ref();
}
}
return *this;
}
cppcoro::cancellation_source& cppcoro::cancellation_source::operator=(cancellation_source&& other) noexcept
{
if (this != &other)
{
if (m_state != nullptr)
{
m_state->release_source_ref();
}
m_state = other.m_state;
other.m_state = nullptr;
}
return *this;
}
bool cppcoro::cancellation_source::can_be_cancelled() const noexcept
{
return m_state != nullptr;
}
cppcoro::cancellation_token cppcoro::cancellation_source::token() const noexcept
{
return cancellation_token(m_state);
}
void cppcoro::cancellation_source::request_cancellation()
{
if (m_state != nullptr)
{
m_state->request_cancellation();
}
}
bool cppcoro::cancellation_source::is_cancellation_requested() const noexcept
{
return m_state != nullptr && m_state->is_cancellation_requested();
}