9 #include "Arp/System/Core/Exception.hpp"
10 #include "Arp/System/Core/TypeName.hxx"
15 namespace Arp {
namespace System {
namespace Nm {
namespace Internal
31 class IExceptionWrapper
34 virtual ~IExceptionWrapper() =
default;
35 virtual void ReThrowException() = 0;
41 template<
typename ExceptionType>
42 class ExceptionWrapper :
public IExceptionWrapper
45 explicit ExceptionWrapper(
const ExceptionType& e) : exception(e)
49 ~ExceptionWrapper()
override =
default;
51 void ReThrowException()
override
53 throw this->exception;
57 ExceptionType exception;
65 bool isCreatedByPromise =
false;
66 std::atomic<bool> valueHasBeenSet;
67 std::unique_ptr<IExceptionWrapper> exception;
89 virtual ImplBase& GetPImpl() = 0;
90 virtual const ImplBase& GetPImpl()
const = 0;
91 void CheckForValueAndException()
const;
93 template<
typename ExceptionType>
94 void SetException(
const ExceptionType& e);
96 void Clear(
bool isValid);
100 template<
typename ExceptionType>
101 void FutureBase::SetException(
const ExceptionType& e)
103 this->GetPImpl().exception = std::make_unique<ExceptionWrapper<ExceptionType>>(e);
104 this->GetPImpl().valueHasBeenSet.store(
true);
116 friend Internal::Promise<T>;
118 struct Impl :
public ImplBase
123 static Future CreateByPromise();
132 ~
Future()
override =
default;
143 void SetValue(
const T& value);
145 Impl& GetPImpl()
override
147 return *(this->pImpl);
150 const Impl& GetPImpl()
const override
152 return *(this->pImpl);
156 std::shared_ptr<Impl> pImpl{std::make_shared<Impl>()};
161 Future<T> Future<T>::CreateByPromise()
164 result.pImpl->isCreatedByPromise =
true;
172 this->CheckForValueAndException();
173 return this->GetPImpl().value;
180 this->GetPImpl().value = value;
181 this->GetPImpl().valueHasBeenSet.store(
true);
193 friend Internal::Promise<void>;
194 using Impl = ImplBase;
196 static Future CreateByPromise();
206 ~
Future()
override =
default;
220 Impl& GetPImpl()
override;
221 const Impl& GetPImpl()
const override;
224 std::shared_ptr<Impl> pImpl;
Base class with common behavior for Future<T> and Future<void>
Definition: Future.hpp:27
const bool HasValue() const
Returns true, if a value or an exception has been set
const bool HasException() const
Returns true, if an exception has been set
const bool IsValid() const
Returns true, if this Future has an assiciated Promise
Future()
Constructs an invalid Future
void GetValue() const
Gets the void result of the asynchronous function call
Future object as proxy for return value an asynchronous function call
Definition: Future.hpp:114
const T GetValue() const
Returns the value set by the associated Promise
Definition: Future.hpp:170
Future()=default
Constructs an invalid Future
@ System
System components used by the System, Device, Plc or Io domains.
Root namespace for the PLCnext API