CancellationToken.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 
10 
11 #include <functional>
12 #include <atomic>
13 #if !defined (BENTLEY_CONFIG_NO_THREAD_SUPPORT)
14 # include <thread>
15 #endif
16 #include <Bentley/Tasks/Tasks.h>
17 #include <Bentley/bvector.h>
18 
20 
21 /*--------------------------------------------------------------------------------------+
22 * @bsiclass Bentley Systems
23 +---------------+---------------+---------------+---------------+---------------+------*/
25  {
26  virtual ~ICancellationListener() {};
28  virtual void OnCanceled() = 0;
29  };
30 
31 /*--------------------------------------------------------------------------------------+
32 * @bsiclass Bentley Systems
33 +---------------+---------------+---------------+---------------+---------------+------*/
35  {
36  private:
37  std::function<void ()> m_onCanceled;
38 
39  public:
41  BENTLEYDLL_EXPORT SimpleCancellationListener(std::function<void ()> onCanceled);
42  BENTLEYDLL_EXPORT virtual ~SimpleCancellationListener();
43  BENTLEYDLL_EXPORT virtual void OnCanceled() override;
44  };
45 
46 /*--------------------------------------------------------------------------------------+
47 * @bsiclass Bentley Systems
48 +---------------+---------------+---------------+---------------+---------------+------*/
49 typedef std::shared_ptr<struct ICancellationToken> ICancellationTokenPtr;
51  {
52  virtual ~ICancellationToken() {};
54  virtual bool IsCanceled() = 0;
56  virtual void Register(std::weak_ptr<ICancellationListener> listener) = 0;
57  };
58 
59 /*--------------------------------------------------------------------------------------+
60 * @bsiclass Bentley Systems
61 +---------------+---------------+---------------+---------------+---------------+------*/
62 typedef std::shared_ptr<struct SimpleCancellationToken> SimpleCancellationTokenPtr;
64  {
65  private:
66  BeAtomic<bool> m_canceled;
67  bvector<std::weak_ptr<ICancellationListener>> m_listeners;
68 
69  private:
70  void OnCancelled() const;
71 
72  public:
73  explicit SimpleCancellationToken(bool canceled) : m_canceled(canceled) {};
75 
77  BENTLEYDLL_EXPORT static SimpleCancellationTokenPtr Create(bool canceled = false);
78 
80  BENTLEYDLL_EXPORT virtual bool IsCanceled() override;
82  BENTLEYDLL_EXPORT virtual void SetCanceled();
84  BENTLEYDLL_EXPORT virtual void Register(std::weak_ptr<ICancellationListener> listener) override;
85  };
86 
87 /*--------------------------------------------------------------------------------------+
88 * @bsiclass Bentley Systems
89 +---------------+---------------+---------------+---------------+---------------+------*/
90 typedef std::shared_ptr<struct MergeCancellationToken> MergeCancellationTokenPtr;
92  {
93  private:
94  bvector <ICancellationTokenPtr> m_tokens;
95 
96  public:
98  MergeCancellationToken(const bvector<ICancellationTokenPtr>& tokens);
99  virtual ~MergeCancellationToken();
100 
102  BENTLEYDLL_EXPORT static MergeCancellationTokenPtr Create(const bvector<ICancellationTokenPtr>& tokens);
105 
107  BENTLEYDLL_EXPORT virtual bool IsCanceled() override;
109  BENTLEYDLL_EXPORT virtual void Register(std::weak_ptr<ICancellationListener> listener) override;
110  };
111 
#define BENTLEYDLL_EXPORT
Definition: Bentley.h:249
Definition: CancellationToken.h:50
virtual void Register(std::weak_ptr< ICancellationListener > listener)=0
Register custom listener for cancellation event.
Definition: CancellationToken.h:91
virtual ~ICancellationToken()
Definition: CancellationToken.h:52
Definition: CancellationToken.h:24
std::shared_ptr< struct MergeCancellationToken > MergeCancellationTokenPtr
Definition: CancellationToken.h:90
#define BEGIN_BENTLEY_TASKS_NAMESPACE
Definition: Tasks.h:13
std::shared_ptr< struct ICancellationToken > ICancellationTokenPtr
Definition: CancellationToken.h:49
SimpleCancellationToken(bool canceled)
Definition: CancellationToken.h:73
Definition: CancellationToken.h:34
Definition: CancellationToken.h:63
virtual ~ICancellationListener()
Definition: CancellationToken.h:26
virtual ~SimpleCancellationToken()
Definition: CancellationToken.h:74
virtual bool IsCanceled()=0
Check if token is cancelled.
#define ct1
Definition: mscons.h:118
#define END_BENTLEY_TASKS_NAMESPACE
Definition: Tasks.h:14
#define EXPORT_VTABLE_ATTRIBUTE
Definition: Bentley.h:67
std::shared_ptr< struct SimpleCancellationToken > SimpleCancellationTokenPtr
Definition: CancellationToken.h:62

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.