OnixS C++ FIX Engine 4.13.0
API Documentation
Loading...
Searching...
No Matches
Future.h
Go to the documentation of this file.
1/*
2* Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3*
4* This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5* and international copyright treaties.
6*
7* Access to and use of the software is governed by the terms of the applicable OnixS Software
8* Services Agreement (the Agreement) and Customer end user license agreements granting
9* a non-assignable, non-transferable and non-exclusive license to use the software
10* for it's own data processing purposes under the terms defined in the Agreement.
11*
12* Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13* of this source code or associated reference material to any other location for further reproduction
14* or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15*
16* Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17* the terms of the Agreement is a violation of copyright law.
18*/
19
20#pragma once
21
22#include <OnixS/FIXEngine/ABI.h>
25
26#ifdef ONIXS_FIXENGINE_CXX11
27#include <exception>
28#endif
29
30namespace OnixS {
31
32namespace System
33{
34 class FutureSharedState;
35}
36
37namespace Threading {
38
41{
51};
52
53namespace Implementation {
54
55class FutureHelper;
56class PromiseBase;
57
58// Implementation details.
60{
61public:
62
64 virtual ValueBase * clone() const = 0;
65};
66
69{
70public:
71
74 FutureValue(const FutureValue & other);
76
77 ValueBase * value() const { return value_; }
78
79private:
80
81 ValueBase * value_;
82};
83
85template<typename ValueType>
86class Value : public ValueBase
87{
88 typedef Value<ValueType> Type;
89
90public:
91
92 static const ValueType & value(const ValueBase * value)
93 {
94 return static_cast<const Type*>(value)->value();
95 }
96
97 static ValueBase * create(const ValueType & value)
98 {
99 return new Type(value);
100 }
101
103 {
104 return create(value_);
105 }
106
107private:
108
109 explicit Value(const ValueType & value) : value_(value) {}
110
111 const ValueType & value() const { return value_; }
112
113 const ValueType value_;
114};
115
118{
119public:
124 {
125 return state_ != ONIXS_FIXENGINE_NULLPTR;
126 }
127
139
145
151
152#ifdef ONIXS_FIXENGINE_CXX11
153
158 ONIXS_FIXENGINE_API std::exception_ptr getExceptionPtr() const;
159
160#endif
161
162 enum { InfiniteTimeout = -1 };
163
171
172protected:
174 : state_() {}
175
177
179
180#ifdef ONIXS_FIXENGINE_CXX11
181
183
185
186#endif
187
188 struct moving_init_t {};
189
191 ONIXS_FIXENGINE_API FutureBase(const System::FutureSharedState * state) ONIXS_FIXENGINE_NOTHROW;
192
194 ONIXS_FIXENGINE_API FutureBase(const System::FutureSharedState * state, moving_init_t) ONIXS_FIXENGINE_NOTHROW;
195
200
202
204
206
207private:
208 const System::FutureSharedState * state_;
209
210 friend class FutureHelper;
211 friend class PromiseBase;
212};
213
214template<typename T>
216{
217 typedef const T & Type;
218};
219
220template<>
221struct FutureGetReturn<void>
222{
223 typedef void Type;
224};
225
226}
227
252template <typename T>
254{
255public:
258
262
265 {
266 FutureBase::operator=(other);
267 return *this;
268 }
269
270#ifdef ONIXS_FIXENGINE_CXX11
271
273 : FutureBase(std::move(other)) {}
274
275 FutureBase & operator=(SharedFuture<T> && other) ONIXS_FIXENGINE_NOTHROW
276 {
277 FutureBase::operator=(std::move(other));
278 return *this;
279 }
280
281#endif
282
292
295 {
296 FutureBase::swap(other);
297 }
298
299private:
301 SharedFuture(const System::FutureSharedState * state)
302 : FutureBase(state) {}
303
305 SharedFuture(const System::FutureSharedState * state, moving_init_t t) ONIXS_FIXENGINE_NOTHROW
306 : FutureBase(state, t) {}
307
308 friend class Implementation::FutureHelper;
310};
311
312template<>
313inline void SharedFuture<void>::get() const
314{
315 getVoid();
316}
317
318namespace Implementation {
319
372
373}
374
378template <typename T>
380{
381public:
382
385
389
392 {
393 PromiseBase::operator=(other);
394 return *this;
395 }
396
397#ifdef ONIXS_FIXENGINE_CXX11
398
400 : PromiseBase(std::move(other)) {}
401
402 Promise & operator=(Promise<T> && other) ONIXS_FIXENGINE_NOTHROW
403 {
404 PromiseBase::operator=(std::move(other));
405 return *this;
406 }
407
408#endif
409
412 {
413 PromiseBase::swap(other);
414 }
415
417 {
418 return getTypedFuture<T>();
419 }
420
422 void set(const T & value)
423 {
425 }
426
430 template<typename ExceptionType>
431 void setException(const ExceptionType & exception)
432 {
433 try
434 {
435 throw exception;
436 }
437 catch(...)
438 {
440 }
441 }
442};
443
444template<>
446{
447public:
448
451
455
458 {
459 PromiseBase::operator=(other);
460 return *this;
461 }
462
463#ifdef ONIXS_FIXENGINE_CXX11
464
466 : PromiseBase(std::move(other)) {}
467
468 Promise & operator=(Promise<void> && other) ONIXS_FIXENGINE_NOTHROW
469 {
470 PromiseBase::operator=(std::move(other));
471 return *this;
472 }
473
474#endif
475
478 {
479 PromiseBase::swap(other);
480 }
481
483 {
484 return getTypedFuture<void>();
485 }
486
488 void set()
489 {
490 setVoid();
491 }
492
496 template<typename ExceptionType>
497 void setException(const ExceptionType & exception)
498 {
499 try
500 {
501 throw exception;
502 }
503 catch(...)
504 {
506 }
507 }
508};
509
510}
511}
#define ONIXS_FIXENGINE_API
Definition ABI.h:45
#define ONIXS_FIXENGINE_DEFAULT
Definition Compiler.h:194
#define ONIXS_FIXENGINE_NULLPTR
Definition Compiler.h:200
#define ONIXS_FIXENGINE_NOTHROW
Definition Compiler.h:186
#define ONIXS_FIXENGINE_OVERRIDE
Definition Compiler.h:191
The base implementation of the SharedFuture<T>.
Definition Future.h:118
ONIXS_FIXENGINE_API void swap(FutureBase &other) ONIXS_FIXENGINE_NOTHROW
ONIXS_FIXENGINE_API FutureBase & operator=(const FutureBase &other) ONIXS_FIXENGINE_NOTHROW
ONIXS_FIXENGINE_API FutureBase(const FutureBase &other) ONIXS_FIXENGINE_NOTHROW
ONIXS_FIXENGINE_API bool hasException() const
Returns true if the asynchronous result associated with this Future has a stored exception,...
ONIXS_FIXENGINE_API ~FutureBase() ONIXS_FIXENGINE_NOTHROW
Destroys a future object.
ONIXS_FIXENGINE_API const FutureValue & getValue() const
ONIXS_FIXENGINE_API bool isReady() const
Returns true if the asynchronous result associated with this Future is ready (has a value or an excep...
ONIXS_FIXENGINE_API FutureStatus::Enum wait(int timeoutInMs=InfiniteTimeout) const
Waits for the result to become available during the timeout.
ONIXS_FIXENGINE_API bool hasValue() const
Returns true if the asynchronous result associated with this Future has a stored value,...
bool valid() const ONIXS_FIXENGINE_NOTHROW
Check if a future instance is associated with an asynchronous result.
Definition Future.h:123
ONIXS_FIXENGINE_API void getVoid() const
ONIXS_FIXENGINE_API FutureBase(const System::FutureSharedState *state) ONIXS_FIXENGINE_NOTHROW
Initializes the instance with shared state.
ONIXS_FIXENGINE_API FutureBase(const System::FutureSharedState *state, moving_init_t) ONIXS_FIXENGINE_NOTHROW
Initializes the instance with shared state.
The base implementation of the PromiseFuture<T>.
Definition Future.h:322
bool valid()
Check if a future instance is associated with an asynchronous result.
ONIXS_FIXENGINE_API void setExceptionImpl()
ONIXS_FIXENGINE_API void setValue(ValueBase *value)
ONIXS_FIXENGINE_API PromiseBase() ONIXS_FIXENGINE_NOTHROW
ONIXS_FIXENGINE_API ~PromiseBase() ONIXS_FIXENGINE_NOTHROW
Destroys a future object.
ONIXS_FIXENGINE_API void swap(PromiseBase &other) ONIXS_FIXENGINE_NOTHROW
SharedFuture< T > getTypedFuture() const
Definition Future.h:361
ONIXS_FIXENGINE_API SharedFuture< FutureValue > getFutureImpl() const
ONIXS_FIXENGINE_API PromiseBase & operator=(const PromiseBase &other) ONIXS_FIXENGINE_NOTHROW
virtual ValueBase * clone() const =0
virtual ~ValueBase() ONIXS_FIXENGINE_DEFAULT
Implementation details.
Definition Future.h:87
static const ValueType & value(const ValueBase *value)
Definition Future.h:92
static ValueBase * create(const ValueType &value)
Definition Future.h:97
ValueBase * clone() const ONIXS_FIXENGINE_OVERRIDE
Definition Future.h:102
Promise() ONIXS_FIXENGINE_NOTHROW
Definition Future.h:449
Promise(const Promise< void > &other) ONIXS_FIXENGINE_NOTHROW
The copy constructor.
Definition Future.h:453
Promise< void > & operator=(const Promise< void > &other) ONIXS_FIXENGINE_NOTHROW
The copy assignment.
Definition Future.h:457
SharedFuture< void > getFuture() const
Definition Future.h:482
void set()
Sets the successful result to the given shared state.
Definition Future.h:488
void setException(const ExceptionType &exception)
Sets the result to indicate an exception.
Definition Future.h:497
void swap(Promise< void > &other) ONIXS_FIXENGINE_NOTHROW
Swaps two Promise objects.
Definition Future.h:477
Provides a facility to store a value or an exception that can be acquired asynchronously via a Shared...
Definition Future.h:380
void set(const T &value)
Sets the successful result value to the given shared state.
Definition Future.h:422
Promise() ONIXS_FIXENGINE_NOTHROW
Definition Future.h:383
Promise(const Promise< T > &other) ONIXS_FIXENGINE_NOTHROW
The copy constructor.
Definition Future.h:387
void swap(Promise< T > &other) ONIXS_FIXENGINE_NOTHROW
Swaps two Promise objects.
Definition Future.h:411
Promise< T > & operator=(const Promise< T > &other) ONIXS_FIXENGINE_NOTHROW
The copy assignment.
Definition Future.h:391
void setException(const ExceptionType &exception)
Sets the result to indicate an exception.
Definition Future.h:431
SharedFuture< T > getFuture() const
Definition Future.h:416
Represents a future result of an asynchronous operation - a result that will eventually appear in the...
Definition Future.h:254
SharedFuture() ONIXS_FIXENGINE_NOTHROW
Definition Future.h:256
void swap(SharedFuture< T > &other) ONIXS_FIXENGINE_NOTHROW
swaps two SharedFuture objects
Definition Future.h:294
SharedFuture< T > & operator=(const SharedFuture< T > &other) ONIXS_FIXENGINE_NOTHROW
The copy assignment.
Definition Future.h:264
SharedFuture(const SharedFuture< T > &other) ONIXS_FIXENGINE_NOTHROW
The copy constructor.
Definition Future.h:260
Implementation::FutureGetReturn< T >::Type get() const
Returns the result.
Definition Future.h:288
The state of a SharedFuture object (similar to std::future_status,.
Definition Future.h:41
@ timeout
The shared state did not become ready before the specified timeout duration has passed.
Definition Future.h:47
@ ready
The shared state is ready.
Definition Future.h:45
@ deferred
The shared state contains a deferred function, so the result will be computed only when explicitly re...
Definition Future.h:49