You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
- #pragma once
-
- #include "cancellation_token.h"
-
- namespace cppcore
- {
-
- /* cancellation_exception */
-
- const char* cancellation_exception
- ::what() const throw()
- { return "task was canceled!"; }
-
- /* data_cancellation_exception */
-
- template<typename T>
- data_cancellation_exception<T>::data_cancellation_exception(const T& d)
- : data(d)
- { }
-
- /* data_cancellation_exception<void> */
-
- template<>
- struct data_cancellation_exception<void>
- : public cancellation_exception
- { };
-
- /* cancellation_token */
-
- cancellation_token
- ::cancellation_token()
- : _is_cancellation_requested(false)
- { }
-
- bool cancellation_token
- ::is_cancellation_requested() const
- { return _is_cancellation_requested; }
-
- void cancellation_token
- ::cancel()
- { _is_cancellation_requested = true; }
-
- /* cancellation_source */
-
- template<typename T>
- void cancellation_source<T>
- ::throw_if_cancellation_requested() const
- {
- if (is_cancellation_requested())
- throw data_cancellation_exception<T>(_value);
- }
-
- template<typename T>
- void cancellation_source<T>
- ::cancel(const T& value)
- {
- cancellation_token::cancel();
- _value = value;
- }
-
- /* cancellation_source<void> */
-
- template<>
- struct cancellation_source<void>
- : public cancellation_token
- {
- public:
- inline void throw_if_cancellation_requested() const
- {
- if (is_cancellation_requested())
- throw cancellation_exception();
- }
-
- inline void cancel()
- { cancellation_token::cancel(); }
- };
-
- }
|