QuantumLibrary
quantum_promise.h
1 /*
2 ** Copyright 2018 Bloomberg Finance L.P.
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16 #ifndef BLOOMBERG_QUANTUM_PROMISE_H
17 #define BLOOMBERG_QUANTUM_PROMISE_H
18 
19 #include <quantum/quantum_future.h>
20 #include <quantum/interface/quantum_ipromise.h>
21 
22 namespace Bloomberg {
23 namespace quantum {
24 
25 //==============================================================================================
26 // class Promise
27 //==============================================================================================
32 template <class T>
33 class Promise : public IPromiseBase,
34  public IThreadPromise<Promise, T>,
35  public ICoroPromise<Promise, T>
36 {
37 public:
38  using Ptr = std::shared_ptr<Promise<T>>;
39 
40  //Constructor
41  Promise();
42 
43  //Destructor
44  ~Promise();
45 
46  //Future interface accessors
48  ICoroFutureBase::Ptr getICoroFutureBase() const final;
50  CoroFuturePtr<T> getICoroFuture() const;
51 
52  //ITerminate
53  void terminate() final;
54 
55  //IPromiseBase
56  bool valid() const final;
57  int setException(std::exception_ptr ex) final;
58 
59  //IThreadPromise
60  template <class V, class = NonBufferType<T,V>>
61  int set(V&& value);
62 
63  template <class V, class = BufferType<T,V>>
64  void push(V&& value);
65 
66  //ICoroPromise
67  template <class V, class = NonBufferType<T,V>>
68  int set(ICoroSync::Ptr sync, V&& value);
69 
70  template <class V, class = BufferType<T,V>>
71  void push(ICoroSync::Ptr sync, V&& value);
72 
73  template <class V = T, class = BufferRetType<V>>
74  int closeBuffer();
75 
76  //===================================
77  // NEW / DELETE
78  //===================================
79  static void* operator new(size_t size);
80  static void operator delete(void* p);
81  static void deleter(Promise<T>* p);
82 
83 private:
84  std::shared_ptr<SharedState<T>> _sharedState;
85  std::atomic_flag _terminated;
86 };
87 
88 template <class T>
89 using PromisePtr = typename Promise<T>::Ptr;
90 
91 }}
92 
93 #include <quantum/impl/quantum_promise_impl.h>
94 
95 #endif //BLOOMBERG_QUANTUM_PROMISE_H
Exposes methods to access a coroutine-compatible future.
Definition: quantum_icoro_future_base.h:32
Definition: quantum_buffer_impl.h:22
std::enable_if_t< Traits::IsBuffer< T >::value &&!std::is_same< std::decay_t< V >, T >::value &&std::is_convertible< std::decay_t< V >, typename Traits::IsBuffer< T >::Type >::value > BufferType
Definition: quantum_traits.h:89
typename ICoroFuture< T >::Ptr CoroFuturePtr
Definition: quantum_icoro_future.h:72
Shared state used between a Promise and a Future to exchange values.
Definition: quantum_shared_state.h:38
std::enable_if_t<!Traits::IsBuffer< T >::value &&std::is_convertible< std::decay_t< V >, T >::value > NonBufferType
Definition: quantum_traits.h:91
std::shared_ptr< IPromiseBase > Ptr
Definition: quantum_ipromise_base.h:34
typename Promise< T >::Ptr PromisePtr
Definition: quantum_promise.h:89
void push(V &&value)
Definition: quantum_promise_impl.h:172
ThreadFuturePtr< T > getIThreadFuture() const
Get the associated thread future.
Definition: quantum_promise_impl.h:149
int setException(std::exception_ptr ex) final
Set an exception in this promise.
Definition: quantum_promise_impl.h:120
Provides an interface to facilitate 'implicit' coroutine yielding within other primitives such as mut...
Definition: quantum_icoro_sync.h:34
bool valid() const final
Determines if this promise still has a shared state with the corresponding future object.
Definition: quantum_promise_impl.h:114
Definition: quantum_stl_impl.h:23
int set(V &&value)
Definition: quantum_promise_impl.h:142
static void deleter(Promise< T > *p)
Definition: quantum_promise_impl.h:207
Class representing a promised value.
Definition: quantum_icoro_promise.h:77
ICoroFutureBase::Ptr getICoroFutureBase() const final
Get a coroutine-compatible interface used to access the associated future.
Definition: quantum_promise_impl.h:134
Promise()
Definition: quantum_promise_impl.h:91
std::shared_ptr< Promise< T > > Ptr
Definition: quantum_promise.h:38
std::shared_ptr< IThreadFutureBase > Ptr
Definition: quantum_ithread_future_base.h:34
void terminate() final
Terminates the object.
Definition: quantum_promise_impl.h:105
typename IThreadFuture< T >::Ptr ThreadFuturePtr
Definition: quantum_ithread_future.h:69
std::enable_if_t< Traits::IsBuffer< T >::value, typename Traits::IsBuffer< T >::Type > BufferRetType
Definition: quantum_traits.h:93
int closeBuffer()
Definition: quantum_promise_impl.h:188
IThreadFutureBase::Ptr getIThreadFutureBase() const final
Get a thread-compatible interface used to access the associated future.
Definition: quantum_promise_impl.h:127
Configuration parameters for the Quantum library.
CoroFuturePtr< T > getICoroFuture() const
Get the associated coroutine future.
Definition: quantum_promise_impl.h:164