rad  5.1.0
stopToken.hpp
Go to the documentation of this file.
1 
8 #ifndef RAD_STOP_TOKEN_HPP
9 #define RAD_STOP_TOKEN_HPP
10 
11 #include <atomic>
12 #include <memory>
13 
14 namespace rad {
15 
16 namespace detail {
17 
23 struct StopState {
24  StopState() noexcept = default;
25 
26  void IncSources() noexcept;
27  void DecSources() noexcept;
28 
29  void IncTokens() noexcept;
30  void DecTokens() noexcept;
31 
32  unsigned GetNumSources() const noexcept;
33  unsigned GetNumTokens() const noexcept;
34 
35  [[nodiscard]] bool StopRequested() const noexcept;
41  bool RequestStop() noexcept;
42 
43  private:
45  std::atomic<unsigned> m_num_sources;
47  std::atomic<unsigned> m_num_tokens;
48 
49  std::atomic<bool> m_stop_requested;
50 };
51 
52 } // namespace detail
53 
54 class StopSource;
55 
62 class StopToken {
63  public:
69  StopToken() noexcept = default;
70  StopToken(StopToken const&) noexcept;
71  StopToken(StopToken&&) noexcept = default;
72  StopToken& operator=(StopToken const&);
73  ~StopToken();
74 
78  void Swap(StopToken& other) noexcept;
79 
84  [[nodiscard]] bool StopRequested() const noexcept;
85 
90  [[nodiscard]] bool StopPossible() const noexcept;
91 
95  bool operator==(StopToken const& rhs) const noexcept;
96  bool operator!=(StopToken const& rhs) const noexcept;
97 
98  private:
99  friend class StopSource;
100 
104  explicit StopToken(std::shared_ptr<detail::StopState> state) noexcept;
105 
106  std::shared_ptr<::rad::detail::StopState> m_state;
107 };
108 
112 class StopSource {
113  public:
114  StopSource() noexcept;
115  StopSource(StopSource const&) noexcept;
116  StopSource& operator=(StopSource const&);
117  StopSource(StopSource&&) noexcept = default;
118  ~StopSource();
119 
124  bool operator==(StopSource const& rhs) const noexcept;
125  bool operator!=(StopSource const& rhs) const noexcept;
126 
130  [[nodiscard]] bool StopRequested() const noexcept;
131 
136  [[nodiscard]] bool StopPossible() const noexcept;
137 
141  [[nodiscard]] StopToken GetToken() const noexcept;
142 
146  void Swap(StopSource& other) noexcept;
147 
154  bool RequestStop() noexcept;
155 
156  private:
157  std::shared_ptr<::rad::detail::StopState> m_state;
158 };
159 
160 } // namespace rad
161 
162 #endif // #define RAD_STOP_TOKEN_HPP
Definition: stopToken.hpp:112
StopSource(StopSource &&) noexcept=default
Definition: stopToken.hpp:62
StopToken() noexcept=default
Definition: actionsApp.cpp:20
Definition: errors.hpp:58
Definition: stopToken.hpp:23
void IncSources() noexcept
Definition: stopToken.cpp:9
void IncTokens() noexcept
Definition: stopToken.cpp:17
void DecTokens() noexcept
Definition: stopToken.cpp:21
unsigned GetNumSources() const noexcept
Definition: stopToken.cpp:25
bool RequestStop() noexcept
Definition: stopToken.cpp:37
StopState() noexcept=default
bool StopRequested() const noexcept
Definition: stopToken.cpp:33
void DecSources() noexcept
Definition: stopToken.cpp:13
unsigned GetNumTokens() const noexcept
Definition: stopToken.cpp:29