Scarab  v2.4.9
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  cancelable( const cancelable& a_orig );
25  cancelable( cancelable&& a_orig );
26  virtual ~cancelable();
27 
28  cancelable& operator=( const cancelable& a_orig );
29  cancelable& operator=( cancelable&& a_orig );
30 
32  void cancel( int a_code = RETURN_SUCCESS );
33 
35  void reset_cancel();
36 
38  bool is_canceled() const;
39 
40  private:
41  virtual void do_cancellation( int a_code );
42  virtual void do_reset_cancellation();
43 
44  protected:
45  std::atomic< bool > f_canceled;
46  };
47 
48  inline void cancelable::cancel( int a_code )
49  {
50  if( f_canceled.load() ) return;
51  f_canceled.store( true );
52  this->do_cancellation( a_code );
53  }
54 
56  {
57  if( ! f_canceled.load() ) return;
58  f_canceled.store( false );
59  this->do_reset_cancellation();
60  }
61 
62  inline bool cancelable::is_canceled() const
63  {
64  return f_canceled.load();
65  }
66 
67 } /* namespace scarab */
68 
69 #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:62
void reset_cancel()
reset to non-canceled state
Definition: cancelable.hh:55
std::atomic< bool > f_canceled
Definition: cancelable.hh:45
void cancel(int a_code=0)
asynchronous cancel function
Definition: cancelable.hh:48