QuantumLibrary
quantum_future.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_FUTURE_H
17 #define BLOOMBERG_QUANTUM_FUTURE_H
18 
19 #include <exception>
20 #include <quantum/quantum_shared_state.h>
21 #include <quantum/interface/quantum_icontext.h>
22 #include <quantum/interface/quantum_ifuture.h>
23 
24 namespace Bloomberg {
25 namespace quantum {
26 
27 //==============================================================================================
28 // class Future
29 //==============================================================================================
35 template <class T>
36 class Future : public IThreadFuture<T>,
37  public ICoroFuture<T>
38 {
39 public:
40  template <class F> friend class Promise;
41  using Ptr = std::shared_ptr<Future<T>>;
42 
43  //Default constructor with empty state
44  Future() = default;
45 
46  bool valid() const final;
47 
48  //IThreadFutureBase
49  void wait() const final;
50  std::future_status waitFor(std::chrono::milliseconds timeMs) const final;
51 
52  //IThreadFuture
53  template <class V = T>
54  NonBufferRetType<V> get();
55 
56  template <class V = T>
57  const NonBufferRetType<V>& getRef() const;
58 
59  template <class V = T>
60  BufferRetType<V> pull(bool& isBufferClosed);
61 
62  //ICoroFutureBase
63  void wait(ICoroSync::Ptr sync) const final;
64  std::future_status waitFor(ICoroSync::Ptr sync, std::chrono::milliseconds timeMs) const final;
65 
66  //ICoroFuture
67  template <class V = T>
69 
70  template <class V = T>
71  const NonBufferRetType<V>& getRef(ICoroSync::Ptr sync) const;
72 
73  template <class V = T>
74  BufferRetType<V> pull(ICoroSync::Ptr sync, bool& isBufferClosed);
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(Future<T>* p);
82 
83 private:
84  explicit Future(std::shared_ptr<SharedState<T>> sharedState);
85 
86  //Members
87  std::shared_ptr<SharedState<T>> _sharedState;
88 };
89 
90 template <class T>
91 using FuturePtr = typename Future<T>::Ptr;
92 
93 }}
94 
95 #include <quantum/impl/quantum_future_impl.h>
96 
97 #endif //BLOOMBERG_QUANTUM_FUTURE_H
Definition: quantum_buffer_impl.h:22
BufferRetType< V > pull(bool &isBufferClosed)
Definition: quantum_future_impl.h:164
Shared state used between a Promise and a Future to exchange values.
Definition: quantum_shared_state.h:38
const NonBufferRetType< V > & getRef() const
Definition: quantum_future_impl.h:111
std::shared_ptr< ICoroFuture< T > > Ptr
Definition: quantum_icoro_future.h:38
Provides an interface to facilitate 'implicit' coroutine yielding within other primitives such as mut...
Definition: quantum_icoro_sync.h:34
Definition: quantum_stl_impl.h:23
std::future_status waitFor(std::chrono::milliseconds timeMs) const final
Waits for the future value up to a maximum 'timeMs' milliseconds.
Definition: quantum_future_impl.h:125
Class representing a promised value.
Definition: quantum_icoro_promise.h:77
static void deleter(Future< T > *p)
Definition: quantum_future_impl.h:191
std::enable_if_t<!Traits::IsBuffer< T >::value, typename Traits::IsBuffer< T >::Type > NonBufferRetType
Definition: quantum_traits.h:95
NonBufferRetType< V > get()
Definition: quantum_future_impl.h:103
Class representing a promised future. Can only be instantiated via a Promise object.
Definition: quantum_icoro_future.h:27
void wait() const final
Waits for the future value.
Definition: quantum_future_impl.h:118
typename Future< T >::Ptr FuturePtr
Definition: quantum_future.h:91
std::enable_if_t< Traits::IsBuffer< T >::value, typename Traits::IsBuffer< T >::Type > BufferRetType
Definition: quantum_traits.h:93
Configuration parameters for the Quantum library.
bool valid() const final
Determines if this future still has a shared state with the corresponding promise object.
Definition: quantum_future_impl.h:96
std::shared_ptr< Future< T > > Ptr
Definition: quantum_future.h:41