OnixS C++ FIX Engine 4.14.0
Users' manual and 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#include <exception>
27
28namespace OnixS {
29
30namespace System
31{
32 class FutureSharedState;
33}
34
35namespace Threading {
36
39{
49};
50
51namespace Implementation {
52
53class FutureHelper;
54class PromiseBase;
55
56// Implementation details.
58{
59public:
60
61 virtual ~ValueBase() = default;
62 virtual ValueBase * clone() const = 0;
63};
64
67{
68public:
69
72 FutureValue(const FutureValue & other);
74
75 ValueBase * value() const { return value_; }
76
77private:
78
79 ValueBase * value_;
80};
81
83template<typename ValueType>
84class Value : public ValueBase
85{
86 typedef Value<ValueType> Type;
87
88public:
89
90 static const ValueType & value(const ValueBase * value)
91 {
92 return static_cast<const Type*>(value)->value();
93 }
94
95 static ValueBase * create(const ValueType & value)
96 {
97 return new Type(value);
98 }
99
100 ValueBase * clone() const override
101 {
102 return create(value_);
103 }
104
105private:
106
107 explicit Value(const ValueType & value) : value_(value) {}
108
109 const ValueType & value() const { return value_; }
110
111 const ValueType value_;
112};
113
116{
117public:
121 bool valid() const noexcept
122 {
123 return state_ != nullptr;
124 }
125
137
143
149
154 ONIXS_FIXENGINE_API std::exception_ptr getExceptionPtr() const;
155
156 enum { InfiniteTimeout = -1 };
157
165
166protected:
168 : state_() {}
169
171
173
175
177
178 struct moving_init_t {};
179
181 ONIXS_FIXENGINE_API FutureBase(const System::FutureSharedState * state) noexcept;
182
184 ONIXS_FIXENGINE_API FutureBase(const System::FutureSharedState * state, moving_init_t) noexcept;
185
190
191 ONIXS_FIXENGINE_API void swap(FutureBase & other) noexcept;
192
194
196
197private:
198 const System::FutureSharedState * state_;
199
200 friend class FutureHelper;
201 friend class PromiseBase;
202};
203
204template<typename T>
206{
207 typedef const T & Type;
208};
209
210template<>
211struct FutureGetReturn<void>
212{
213 typedef void Type;
214};
215
216}
217
242template <typename T>
244{
245public:
246 SharedFuture() noexcept
247 : FutureBase() {}
248
250 SharedFuture(const SharedFuture<T> & other) noexcept
251 : FutureBase(other) {}
252
254 SharedFuture<T> & operator=(const SharedFuture<T> & other) noexcept
255 {
256 FutureBase::operator=(other);
257 return *this;
258 }
259
260 SharedFuture(SharedFuture<T> && other) noexcept
261 : FutureBase(std::move(other)) {}
262
264 {
265 FutureBase::operator=(std::move(other));
266 return *this;
267 }
268
278
280 void swap(SharedFuture<T> & other) noexcept
281 {
282 FutureBase::swap(other);
283 }
284
285private:
287 SharedFuture(const System::FutureSharedState * state)
288 : FutureBase(state) {}
289
291 SharedFuture(const System::FutureSharedState * state, moving_init_t t) noexcept
292 : FutureBase(state, t) {}
293
294 friend class Implementation::FutureHelper;
296};
297
298template<>
299inline void SharedFuture<void>::get() const
300{
301 getVoid();
302}
303
304namespace Implementation {
305
308{
309public:
313 bool valid();
314
316
317protected:
319
321
322 ONIXS_FIXENGINE_API PromiseBase & operator=(const PromiseBase & other) noexcept;
323
325
326 ONIXS_FIXENGINE_API PromiseBase & operator=(PromiseBase && other) noexcept;
327
332
333 ONIXS_FIXENGINE_API void swap(PromiseBase & other) noexcept;
334
336
338
339
341
342 template<typename T>
344 {
345 return SharedFuture<T>(getFutureImpl().state_);
346 }
347
348private:
349
351 struct Impl;
352 Impl * impl_;
353};
354
355}
356
360template <typename T>
362{
363public:
364
365 Promise() noexcept
366 : PromiseBase() {}
367
369 Promise(const Promise<T> & other) noexcept
370 : PromiseBase(other) {}
371
373 Promise<T> & operator=(const Promise<T> & other) noexcept
374 {
375 PromiseBase::operator=(other);
376 return *this;
377 }
378
379 Promise(Promise<T> && other) noexcept
380 : PromiseBase(std::move(other)) {}
381
382 Promise & operator=(Promise<T> && other) noexcept
383 {
384 PromiseBase::operator=(std::move(other));
385 return *this;
386 }
387
389 void swap(Promise<T> & other) noexcept
390 {
391 PromiseBase::swap(other);
392 }
393
395 {
396 return getTypedFuture<T>();
397 }
398
400 void set(const T & value)
401 {
403 }
404
408 template<typename ExceptionType>
409 void setException(const ExceptionType & exception)
410 {
411 try
412 {
413 throw exception;
414 }
415 catch(...)
416 {
418 }
419 }
420};
421
422template<>
424{
425public:
426
427 Promise() noexcept
428 : PromiseBase() {}
429
431 Promise(const Promise<void> & other) noexcept
432 : PromiseBase(other) {}
433
435 Promise<void> & operator=(const Promise<void> & other) noexcept
436 {
437 PromiseBase::operator=(other);
438 return *this;
439 }
440
441 Promise(Promise<void> && other) noexcept
442 : PromiseBase(std::move(other)) {}
443
444 Promise & operator=(Promise<void> && other) noexcept
445 {
446 PromiseBase::operator=(std::move(other));
447 return *this;
448 }
449
451 void swap(Promise<void> & other) noexcept
452 {
453 PromiseBase::swap(other);
454 }
455
457 {
458 return getTypedFuture<void>();
459 }
460
462 void set()
463 {
464 setVoid();
465 }
466
470 template<typename ExceptionType>
471 void setException(const ExceptionType & exception)
472 {
473 try
474 {
475 throw exception;
476 }
477 catch(...)
478 {
480 }
481 }
482};
483
484}
485}
#define ONIXS_FIXENGINE_API
Definition ABI.h:45
The base implementation of the SharedFuture<T>.
Definition Future.h:116
ONIXS_FIXENGINE_API FutureBase & operator=(const FutureBase &other) noexcept
ONIXS_FIXENGINE_API ~FutureBase() noexcept
ONIXS_FIXENGINE_API bool hasException() const
ONIXS_FIXENGINE_API FutureBase(const FutureBase &other) noexcept
ONIXS_FIXENGINE_API FutureBase(FutureBase &&other) noexcept
ONIXS_FIXENGINE_API const FutureValue & getValue() const
ONIXS_FIXENGINE_API bool isReady() const
ONIXS_FIXENGINE_API FutureStatus::Enum wait(int timeoutInMs=InfiniteTimeout) const
ONIXS_FIXENGINE_API bool hasValue() const
ONIXS_FIXENGINE_API FutureBase(const System::FutureSharedState *state) noexcept
Initializes the instance with shared state.
ONIXS_FIXENGINE_API void getVoid() const
ONIXS_FIXENGINE_API FutureBase & operator=(FutureBase &&other) noexcept
ONIXS_FIXENGINE_API FutureBase(const System::FutureSharedState *state, moving_init_t) noexcept
Initializes the instance with shared state.
ONIXS_FIXENGINE_API std::exception_ptr getExceptionPtr() const
ONIXS_FIXENGINE_API void swap(FutureBase &other) noexcept
The base implementation of the PromiseFuture<T>.
Definition Future.h:308
ONIXS_FIXENGINE_API void setExceptionImpl()
ONIXS_FIXENGINE_API PromiseBase() noexcept
ONIXS_FIXENGINE_API void setValue(ValueBase *value)
SharedFuture< T > getTypedFuture() const
Definition Future.h:343
ONIXS_FIXENGINE_API SharedFuture< FutureValue > getFutureImpl() const
ONIXS_FIXENGINE_API void swap(PromiseBase &other) noexcept
virtual ValueBase * clone() const =0
Implementation details.
Definition Future.h:85
ValueBase * clone() const override
Definition Future.h:100
static const ValueType & value(const ValueBase *value)
Definition Future.h:90
static ValueBase * create(const ValueType &value)
Definition Future.h:95
Promise< void > & operator=(const Promise< void > &other) noexcept
The copy assignment.
Definition Future.h:435
SharedFuture< void > getFuture() const
Definition Future.h:456
void set()
Sets the successful result to the given shared state.
Definition Future.h:462
Promise(Promise< void > &&other) noexcept
Definition Future.h:441
void swap(Promise< void > &other) noexcept
Swaps two Promise objects.
Definition Future.h:451
Promise(const Promise< void > &other) noexcept
The copy constructor.
Definition Future.h:431
void setException(const ExceptionType &exception)
Definition Future.h:471
Promise & operator=(Promise< void > &&other) noexcept
Definition Future.h:444
void set(const T &value)
Sets the successful result value to the given shared state.
Definition Future.h:400
Promise & operator=(Promise< T > &&other) noexcept
Definition Future.h:382
Promise< T > & operator=(const Promise< T > &other) noexcept
The copy assignment.
Definition Future.h:373
void setException(const ExceptionType &exception)
Definition Future.h:409
Promise(const Promise< T > &other) noexcept
The copy constructor.
Definition Future.h:369
SharedFuture< T > getFuture() const
Definition Future.h:394
void swap(Promise< T > &other) noexcept
Swaps two Promise objects.
Definition Future.h:389
Promise(Promise< T > &&other) noexcept
Definition Future.h:379
void swap(SharedFuture< T > &other) noexcept
swaps two SharedFuture objects
Definition Future.h:280
SharedFuture(const SharedFuture< T > &other) noexcept
The copy constructor.
Definition Future.h:250
SharedFuture(SharedFuture< T > &&other) noexcept
Definition Future.h:260
FutureBase & operator=(SharedFuture< T > &&other) noexcept
Definition Future.h:263
Implementation::FutureGetReturn< T >::Type get() const
Definition Future.h:274
SharedFuture< T > & operator=(const SharedFuture< T > &other) noexcept
The copy assignment.
Definition Future.h:254
The state of a SharedFuture object (similar to std::future_status,.
Definition Future.h:39
@ timeout
The shared state did not become ready before the specified timeout duration has passed.
Definition Future.h:45
@ ready
The shared state is ready.
Definition Future.h:43
@ deferred
The shared state contains a deferred function, so the result will be computed only when explicitly re...
Definition Future.h:47