Lely core libraries 2.3.4
stop.hpp
Go to the documentation of this file.
1
24#ifndef LELY_UTIL_STOP_HPP_
25#define LELY_UTIL_STOP_HPP_
26
27#include <lely/util/stop.h>
28#include <lely/util/error.hpp>
29
30#include <utility>
31
32namespace lely {
33namespace util {
34
40class StopToken {
41 public:
49
50 explicit StopToken(stop_token_t* token) noexcept : token_(token) {}
51
61 : token_(stop_token_acquire(other.token_)) {}
62
70 StopToken(StopToken&& other) noexcept : token_(other.token_) {
71 other.token_ = nullptr;
72 }
73
82 operator=(const StopToken& other) noexcept {
83 if (token_ != other.token_) {
84 stop_token_release(token_);
85 token_ = stop_token_acquire(other.token_);
86 }
87 return *this;
88 }
89
99 using ::std::swap;
100 swap(token_, other.token_);
101 return *this;
102 }
103
111
112 operator stop_token_t*() const noexcept { return token_; }
113
115 explicit operator bool() const noexcept { return token_ != nullptr; }
116
123 bool
125 return *this && stop_token_stop_requested(*this) != 0;
126 }
127
135 bool
137 return *this && stop_token_stop_possible(*this) != 0;
138 }
139
141 void
142 swap(StopToken& other) noexcept {
143 ::std::swap(token_, other.token_);
144 }
145
150 friend bool
151 operator==(const StopToken& lhs, const StopToken& rhs) noexcept {
152 return lhs.token_ == rhs.token_;
153 }
154
159 friend bool
160 operator!=(const StopToken& lhs, const StopToken& rhs) noexcept {
161 return !(lhs == rhs);
162 }
163
165 friend void
167 lhs.swap(rhs);
168 }
169
170 private:
171 stop_token_t* token_{nullptr};
172};
173
180 public:
187 if (!source_) util::throw_errc("StopSource");
188 }
189
196
206 : source_(stop_source_acquire(other.source_)) {}
207
215 StopSource(StopSource&& other) noexcept : source_(other.source_) {
216 other.source_ = nullptr;
217 }
218
227 operator=(const StopSource& other) noexcept {
228 if (source_ != other.source_) {
229 stop_source_release(source_);
230 source_ = stop_source_acquire(other.source_);
231 }
232 return *this;
233 }
234
244 StopSource(::std::move(other)).swap(*this);
245 return *this;
246 }
247
255
256 operator stop_source_t*() const noexcept { return source_; }
257
259 explicit operator bool() const noexcept { return source_ != nullptr; }
260
277 bool
279 return *this && stop_source_request_stop(*this) != 0;
280 }
281
288 bool
290 return *this && stop_source_stop_requested(*this) != 0;
291 }
292
294 bool
296 return *this;
297 }
298
300 void
301 swap(StopSource& other) noexcept {
302 ::std::swap(source_, other.source_);
303 }
304
313
318 friend bool
319 operator==(const StopSource& lhs, const StopSource& rhs) noexcept {
320 return lhs.source_ == rhs.source_;
321 }
322
327 friend bool
328 operator!=(const StopSource& lhs, const StopSource& rhs) noexcept {
329 return !(lhs == rhs);
330 }
331
333 friend void
335 lhs.swap(rhs);
336 }
337
338 private:
339 stop_source_t* source_{nullptr};
340};
341
348template <class Callback>
349class StopCallback : public stop_func {
350 public:
351 using callback_type = Callback;
352
361 template <class C>
362 explicit StopCallback(const StopToken& token, C&& cb)
363 : stop_func STOP_FUNC_INIT(&func_), cb_(::std::forward<C>(cb)) {
364 if (token && !stop_token_insert(token, this)) token_ = token;
365 }
366
375 template <class C>
376 explicit StopCallback(StopToken&& token, C&& cb)
377 : stop_func STOP_FUNC_INIT(&func_), cb_(::std::forward<C>(cb)) {
378 if (token && !stop_token_insert(token, this)) token_ = ::std::move(token);
379 }
380
381 StopCallback(const StopCallback&) = delete;
382 StopCallback(StopCallback&&) = delete;
383
384 StopCallback& operator=(const StopCallback&) = delete;
385 StopCallback& operator=(StopCallback&&) = delete;
386
399 if (token_) stop_token_remove(token_, this);
400 }
401
402 private:
403 static void
404 func_(stop_func* func) noexcept {
405 Callback& cb_ = static_cast<StopCallback*>(func)->cb_;
406 ::std::forward<Callback>(cb_)();
407 }
408
409 StopToken token_;
410 Callback cb_;
411};
412
413} // namespace util
414} // namespace lely
415
416#endif // !LELY_UTIL_STOP_HPP_
A CANopen value.
Definition val.hpp:42
A RAII object type that registers a callback function with a lely::util::StopToken object.
Definition stop.hpp:349
StopCallback(const StopToken &token, C &&cb)
Copies the token stop token, saves the cb callback function and registers with with token.
Definition stop.hpp:362
~StopCallback()
If *this has a lely::util::StopToken with associated stop-state, deregisters the saved callback funct...
Definition stop.hpp:398
StopCallback(StopToken &&token, C &&cb)
Moves the token stop token, saves the cb callback function and registers with with token.
Definition stop.hpp:376
An object providing the means to issue a stop request.
Definition stop.hpp:179
friend void swap(StopSource &lhs, StopSource &rhs) noexcept
Exchanges the stop-state of lhs with rhs.
Definition stop.hpp:334
StopSource(StopSource &&other) noexcept
Constructs a stop source whose associated stop-state is the same as that if other; other is left as v...
Definition stop.hpp:215
StopToken get_token() const noexcept
Returns a stop token associated with the stop-state of *this, if it has any, and a default-constructe...
Definition stop.hpp:310
~StopSource()
Destroys a stop source.
Definition stop.hpp:254
StopSource(const StopSource &other) noexcept
Constructs a stop source whose associated stop-state is the same as that if other.
Definition stop.hpp:205
StopSource & operator=(const StopSource &other) noexcept
Copy-assigns the stop-state of other to *this.
Definition stop.hpp:227
StopSource(stop_source_t *source) noexcept
Constructs an empty stop source with no assiociated stop-state.
Definition stop.hpp:195
StopSource()
Constructs a stop source with a new stop-state.
Definition stop.hpp:186
friend bool operator==(const StopSource &lhs, const StopSource &rhs) noexcept
Returns true if lhs and rhs have the same stop-state, and false otherwise.
Definition stop.hpp:319
bool stop_possible() const noexcept
Returns true if *this has a stop-state, and false otherwise.
Definition stop.hpp:295
friend bool operator!=(const StopSource &lhs, const StopSource &rhs) noexcept
Returns false if lhs and rhs have the same stop-state, and true otherwise.
Definition stop.hpp:328
StopSource & operator=(StopSource &&other) noexcept
Move-assigns the stop-state of other to *this.
Definition stop.hpp:243
bool stop_requested() const noexcept
Returns true if *this has a stop-state and it has received a stop request, and false otherwise.
Definition stop.hpp:289
void swap(StopSource &other) noexcept
Exchanges the stop-state of *this and other/.
Definition stop.hpp:301
bool request_stop() noexcept
Issues a stop requests to the stop-state, if *this has a stop-state` and it has not already received ...
Definition stop.hpp:278
An object providing the means to check if a stop request has been made for its associated lely::util:...
Definition stop.hpp:40
StopToken(const StopToken &other) noexcept
Constructs a stop token whose associated stop-state is the same as that if other.
Definition stop.hpp:60
StopToken & operator=(const StopToken &other) noexcept
Copy-assigns the stop-state of other to *this.
Definition stop.hpp:82
~StopToken()
Destroys a stop token.
Definition stop.hpp:110
friend void swap(StopToken &lhs, StopToken &rhs) noexcept
Exchanges the stop-state of lhs with rhs.
Definition stop.hpp:166
friend bool operator==(const StopToken &lhs, const StopToken &rhs) noexcept
Returns true if lhs and rhs have the same stop-state, and false otherwise.
Definition stop.hpp:151
StopToken(StopToken &&other) noexcept
Constructs a stop token whose associated stop-state is the same as that if other; other is left as va...
Definition stop.hpp:70
bool stop_possible() const noexcept
Returns true if *this has a stop-state and it has received a stop request, or if it has an associated...
Definition stop.hpp:136
void swap(StopToken &other) noexcept
Exchanges the stop-state of *this and other/.
Definition stop.hpp:142
friend bool operator!=(const StopToken &lhs, const StopToken &rhs) noexcept
Returns false if lhs and rhs have the same stop-state, and true otherwise.
Definition stop.hpp:160
bool stop_requested() const noexcept
Returns true if *this has a stop-state and it has received a stop request, and false otherwise.
Definition stop.hpp:124
StopToken() noexcept=default
Constructs an empty stop token with no assiociated stop-state.
StopToken & operator=(StopToken &&other) noexcept
Move-assigns the stop-state of other to *this.
Definition stop.hpp:98
This header file is part of the utilities library; it contains C++ convenience functions for creating...
STL namespace.
This header file is part of the utilities library; it contains the stop token declarations.
int stop_token_insert(stop_token_t *token, struct stop_func *func)
Registers a callback function with the specified stop token.
Definition stop.c:163
#define STOP_FUNC_INIT(func)
The static initializer for stop_func.
Definition stop.h:59
void stop_token_release(stop_token_t *token)
Releases a reference to a stop token.
Definition stop.c:121
int stop_source_request_stop(stop_source_t *source)
Issues a stop request to the stop-state associated with the specified stop source,...
Definition stop.c:291
stop_token_t * stop_source_get_token(stop_source_t *source)
Returns (a reference to) a stop token associated with the specified stop source's stop-state.
Definition stop.c:381
stop_source_t * stop_source_acquire(stop_source_t *source)
Acquires a reference to a stop source.
Definition stop.c:254
void stop_source_release(stop_source_t *source)
Releases a reference to a stop source.
Definition stop.c:273
int stop_source_stop_requested(const stop_source_t *source)
Checks if the stop-state associated with the specified stop source has received a stop request.
Definition stop.c:366
void stop_token_remove(stop_token_t *token, struct stop_func *func)
Deregisters a callback function from the specified stop token.
Definition stop.c:199
int stop_token_stop_possible(const stop_token_t *token)
Checks if the stop-state associated with the specified stop token has received a stop request,...
Definition stop.c:149
stop_source_t * stop_source_create(void)
Creates a stop source with a new stop-state.
Definition stop.c:227
int stop_token_stop_requested(const stop_token_t *token)
Checks if the stop-state associated with the specified stop token has received a stop request.
Definition stop.c:143
stop_token_t * stop_token_acquire(stop_token_t *token)
Acquires a reference to a stop token.
Definition stop.c:104
An object providing the means to register a callback function with an stop_token_t object.
Definition stop.h:51
void(* func)(struct stop_func *func)
The function to be invoked when a stop request is issued.
Definition stop.h:53
A stop source.
Definition stop.c:71
A stop token.
Definition stop.c:40