Scarab  v2.2.3
Project 8 C++ Utility Library
cancelable.hh
Go to the documentation of this file.
1 /*
2  * cancelable.hh
3  *
4  * Created on: Jan 24, 2016
5  * Author: nsoblath
6  *
7  * Base class for thread-safe asynchronous cancellation
8  */
9 
10 #ifndef SCARAB_CANCELABLE_HH_
11 #define SCARAB_CANCELABLE_HH_
12 
13 #include <atomic>
14 
15 namespace scarab
16 {
17  class cancelable
18  {
19  public:
20  cancelable();
21  virtual ~cancelable();
22 
24  void cancel();
25 
27  void reset_cancel();
28 
30  bool is_canceled() const;
31 
32  private:
33  virtual void do_cancellation();
34  virtual void do_reset_cancellation();
35 
36  protected:
37  std::atomic< bool > f_canceled;
38  };
39 
40  inline void cancelable::cancel()
41  {
42  if( f_canceled.load() ) return;
43  f_canceled.store( true );
44  this->do_cancellation();
45  }
46 
48  {
49  if( ! f_canceled.load() ) return;
50  f_canceled.store( false );
51  this->do_reset_cancellation();
52  }
53 
54  inline bool cancelable::is_canceled() const
55  {
56  return f_canceled.load();
57  }
58 
59 } /* namespace scarab */
60 
61 #endif /* SCARAB_CANCELABLE_HH_ */
virtual void do_cancellation()
Definition: cancelable.cc:25
virtual ~cancelable()
Definition: cancelable.cc:21
bool is_canceled() const
check canceled state
Definition: cancelable.hh:54
void reset_cancel()
reset to non-canceled state
Definition: cancelable.hh:47
std::atomic< bool > f_canceled
Definition: cancelable.hh:37
virtual void do_reset_cancellation()
Definition: cancelable.cc:32
void cancel()
asynchronous cancel function
Definition: cancelable.hh:40