forked from andreasbuhr/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcancellation_token.cpp
More file actions
108 lines (89 loc) · 2.21 KB
/
Copy pathcancellation_token.cpp
File metadata and controls
108 lines (89 loc) · 2.21 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
98
99
100
101
102
103
104
105
106
107
108
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/cancellation_token.hpp>
#include <cppcoro/operation_cancelled.hpp>
#include "cancellation_state.hpp"
#include <utility>
#include <cassert>
cppcoro::cancellation_token::cancellation_token() noexcept
: m_state(nullptr)
{
}
cppcoro::cancellation_token::cancellation_token(const cancellation_token& other) noexcept
: m_state(other.m_state)
{
if (m_state != nullptr)
{
m_state->add_token_ref();
}
}
cppcoro::cancellation_token::cancellation_token(cancellation_token&& other) noexcept
: m_state(other.m_state)
{
other.m_state = nullptr;
}
cppcoro::cancellation_token::~cancellation_token()
{
if (m_state != nullptr)
{
m_state->release_token_ref();
}
}
cppcoro::cancellation_token& cppcoro::cancellation_token::operator=(const cancellation_token& other) noexcept
{
if (other.m_state != m_state)
{
if (m_state != nullptr)
{
m_state->release_token_ref();
}
m_state = other.m_state;
if (m_state != nullptr)
{
m_state->add_token_ref();
}
}
return *this;
}
cppcoro::cancellation_token& cppcoro::cancellation_token::operator=(cancellation_token&& other) noexcept
{
if (this != &other)
{
if (m_state != nullptr)
{
m_state->release_token_ref();
}
m_state = other.m_state;
other.m_state = nullptr;
}
return *this;
}
void cppcoro::cancellation_token::swap(cancellation_token& other) noexcept
{
std::swap(m_state, other.m_state);
}
bool cppcoro::cancellation_token::can_be_cancelled() const noexcept
{
return m_state != nullptr && m_state->can_be_cancelled();
}
bool cppcoro::cancellation_token::is_cancellation_requested() const noexcept
{
return m_state != nullptr && m_state->is_cancellation_requested();
}
void cppcoro::cancellation_token::throw_if_cancellation_requested() const
{
if (is_cancellation_requested())
{
throw operation_cancelled{};
}
}
cppcoro::cancellation_token::cancellation_token(detail::cancellation_state* state) noexcept
: m_state(state)
{
if (m_state != nullptr)
{
m_state->add_token_ref();
}
}