Scarab  v2.4.8
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 "macros.hh"
14 #include "scarab_api.hh"
15 
16 #include <atomic>
17 
18 namespace scarab
19 {
21  {
22  public:
23  cancelable();
24  virtual ~cancelable();
25 
27  void cancel( int a_code = RETURN_SUCCESS );
28 
30  void reset_cancel();
31 
33  bool is_canceled() const;
34 
35  private:
36  virtual void do_cancellation( int a_code );
37  virtual void do_reset_cancellation();
38 
39  protected:
40  std::atomic< bool > f_canceled;
41  };
42 
43  inline void cancelable::cancel( int a_code )
44  {
45  if( f_canceled.load() ) return;
46  f_canceled.store( true );
47  this->do_cancellation( a_code );
48  }
49 
51  {
52  if( ! f_canceled.load() ) return;
53  f_canceled.store( false );
54  this->do_reset_cancellation();
55  }
56 
57  inline bool cancelable::is_canceled() const
58  {
59  return f_canceled.load();
60  }
61 
62 } /* namespace scarab */
63 
64 #endif /* SCARAB_CANCELABLE_HH_ */
#define RETURN_SUCCESS
Definition: macros.hh:12
#define SCARAB_API
Definition: scarab_api.hh:24
bool is_canceled() const
check canceled state
Definition: cancelable.hh:57
void reset_cancel()
reset to non-canceled state
Definition: cancelable.hh:50
std::atomic< bool > f_canceled
Definition: cancelable.hh:40
void cancel(int a_code=0)
asynchronous cancel function
Definition: cancelable.hh:43