commonlib
threadexceptionadapter.h
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include "threading.h"
13 #include <mutex>
15 
16 BEGIN_NAMESPACE_THREADING
17 
23 class ThreadExceptionAdapter : public Synchronization::RecursiveLocking
24 {
25 public:
29  void reset()
30  {
31  set(std::exception_ptr());
32  }
33 
38  void set(std::exception_ptr exception)
39  {
40  std::lock_guard guard(lock);
41  this->exceptionPtr = exception;
42  }
43 
48  void operator=(std::exception_ptr exception)
49  {
50  set(exception);
51  }
52 
57  const std::exception_ptr& get() const
58  {
59  std::lock_guard guard(lock);
60  return exceptionPtr;
61  }
62 
67  const std::exception_ptr& operator*() const
68  {
69  return get();
70  }
71 
76  bool operator!() const
77  {
78  return !isSet();
79  }
80 
85  bool exceptionOccurred() const
86  {
87  return isSet();
88  };
89 
90 protected:
95  bool isSet() const
96  {
97  std::lock_guard guard(lock);
98  return (this->exceptionPtr != nullptr);
99  };
100 
101 private:
102  std::exception_ptr exceptionPtr{nullptr};
103 };
104 
105 END_NAMESPACE_THREADING
void operator=(std::exception_ptr exception)
Sets exception pointer.
Definition: threadexceptionadapter.h:48
Interface for easy use of basic locking objects.
Wraps exception_ptr to deliver exception propagation from thread to the main thread.
Definition: threadexceptionadapter.h:23
const std::exception_ptr & operator*() const
Returns exception pointer.
Definition: threadexceptionadapter.h:67
bool operator!() const
Is exception pointer unset.
Definition: threadexceptionadapter.h:76
void reset()
Resets exception pointer.
Definition: threadexceptionadapter.h:29
bool isSet() const
Is exception pointer set?
Definition: threadexceptionadapter.h:95
bool exceptionOccurred() const
Did we catch an exception? Has exception occurred? (expressional)
Definition: threadexceptionadapter.h:85