QuantumLibrary
quantum_future_impl.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 //NOTE: DO NOT INCLUDE DIRECTLY
17 
18 //##############################################################################################
19 //#################################### IMPLEMENTATIONS #########################################
20 //##############################################################################################
21 #include <quantum/quantum_allocator.h>
22 
23 namespace Bloomberg {
24 namespace quantum {
25 
26 #ifndef __QUANTUM_FUTURE_ALLOC_SIZE
27  #define __QUANTUM_FUTURE_ALLOC_SIZE __QUANTUM_DEFAULT_POOL_ALLOC_SIZE
28 #endif
29 #ifndef __QUANTUM_USE_DEFAULT_ALLOCATOR
30  #ifdef __QUANTUM_ALLOCATE_POOL_FROM_HEAP
31  using FutureAllocator = HeapAllocator<Future<int>>;
32  #else
33  using FutureAllocator = StackAllocator<Future<int>, __QUANTUM_FUTURE_ALLOC_SIZE>;
34  #endif
35 #else
37 #endif
38 
39 //==============================================================================================
40 // class IThreadFuture
41 //==============================================================================================
42 template <class T>
43 template <class V>
45 {
46  return static_cast<Impl*>(this)->get();
47 }
48 
49 template <class T>
50 template <class V>
52 {
53  return static_cast<const Impl*>(this)->getRef();
54 }
55 
56 template <class T>
57 template <class V>
59 {
60  return static_cast<Impl*>(this)->pull(isBufferClosed);
61 }
62 
63 //==============================================================================================
64 // class ICoroFuture
65 //==============================================================================================
66 template <class T>
67 template <class V>
69 {
70  return static_cast<Impl*>(this)->get(sync);
71 }
72 
73 template <class T>
74 template <class V>
76 {
77  return static_cast<const Impl*>(this)->getRef(sync);
78 }
79 
80 template <class T>
81 template <class V>
83 {
84  return static_cast<Impl*>(this)->pull(sync, isBufferClosed);
85 }
86 
87 //==============================================================================================
88 // class Future
89 //==============================================================================================
90 template <class T>
91 Future<T>::Future(std::shared_ptr<SharedState<T>> sharedState) :
92  _sharedState(sharedState)
93 {}
94 
95 template <class T>
96 bool Future<T>::valid() const
97 {
98  return _sharedState != nullptr;
99 }
100 
101 template <class T>
102 template <class V>
104 {
105  if (!_sharedState) ThrowFutureException(FutureState::NoState);
106  return _sharedState->get();
107 }
108 
109 template <class T>
110 template <class V>
112 {
113  if (!_sharedState) ThrowFutureException(FutureState::NoState);
114  return _sharedState->getRef();
115 }
116 
117 template <class T>
118 void Future<T>::wait() const
119 {
120  if (!_sharedState) ThrowFutureException(FutureState::NoState);
121  return _sharedState->wait();
122 }
123 
124 template <class T>
125 std::future_status Future<T>::waitFor(std::chrono::milliseconds timeMs) const
126 {
127  if (!_sharedState) ThrowFutureException(FutureState::NoState);
128  return _sharedState->waitFor(timeMs);
129 }
130 
131 template <class T>
132 template <class V>
134 {
135  if (!_sharedState) ThrowFutureException(FutureState::NoState);
136  return _sharedState->get(sync);
137 }
138 
139 template <class T>
140 template <class V>
142 {
143  if (!_sharedState) ThrowFutureException(FutureState::NoState);
144  return _sharedState->getRef(sync);
145 }
146 
147 template <class T>
149 {
150  if (!_sharedState) ThrowFutureException(FutureState::NoState);
151  return _sharedState->wait(sync);
152 }
153 
154 template <class T>
155 std::future_status Future<T>::waitFor(ICoroSync::Ptr sync,
156  std::chrono::milliseconds timeMs) const
157 {
158  if (!_sharedState) ThrowFutureException(FutureState::NoState);
159  return _sharedState->waitFor(sync, timeMs);
160 }
161 
162 template <class T>
163 template <class V>
164 BufferRetType<V> Future<T>::pull(bool& isBufferClosed)
165 {
166  if (!_sharedState) ThrowFutureException(FutureState::NoState);
167  return _sharedState->pull(isBufferClosed);
168 }
169 
170 template <class T>
171 template <class V>
173 {
174  if (!_sharedState) ThrowFutureException(FutureState::NoState);
175  return _sharedState->pull(sync, isBufferClosed);
176 }
177 
178 template <class T>
179 void* Future<T>::operator new(size_t)
180 {
182 }
183 
184 template <class T>
185 void Future<T>::operator delete(void* p)
186 {
188 }
189 
190 template <class T>
192 {
193 #ifndef __QUANTUM_USE_DEFAULT_ALLOCATOR
195 #else
196  delete p;
197 #endif
198 }
199 
200 }}
201 
202 
static AllocType & instance(std::enable_if_t<!A::default_constructor::value, uint16_t > size)
Definition: quantum_allocator.h:56
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
Definition: quantum_allocator.h:36
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
std::shared_ptr< ICoroSync > Ptr
Definition: quantum_icoro_sync.h:36
StackAllocator< Future< int >, __QUANTUM_FUTURE_ALLOC_SIZE > FutureAllocator
Definition: quantum_future_impl.h:33
static void deleter(Future< T > *p)
Definition: quantum_future_impl.h:191
BufferRetType< V > pull(bool &isBufferClosed)
Pull a single value from the future buffer.
Definition: quantum_future_impl.h:58
static size_type & futureAllocSize()
Get/set if the default size for future object pools.
Definition: quantum_allocator_traits.h:124
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
Provides a stack-based object pool to the underlying ContiguousPoolManager. The default buffer size i...
Definition: quantum_stack_allocator.h:34
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
const NonBufferRetType< V > & getRef() const
Get a reference the future value.
Definition: quantum_future_impl.h:51
void ThrowFutureException(FutureState state)
Definition: quantum_future_state.h:130
const NonBufferRetType< V > & getRef(ICoroSync::Ptr sync) const
Get a reference the future value.
Definition: quantum_future_impl.h:75
std::enable_if_t< Traits::IsBuffer< T >::value, typename Traits::IsBuffer< T >::Type > BufferRetType
Definition: quantum_traits.h:93
BufferRetType< V > pull(ICoroSync::Ptr sync, bool &isBufferClosed)
Pull a single value from the future buffer.
Definition: quantum_future_impl.h:82
Shared state between Promise and Future is invalid.
NonBufferRetType< V > get()
Get the future value.
Definition: quantum_future_impl.h:44
NonBufferRetType< V > get(ICoroSync::Ptr sync)
Get the future value.
Definition: quantum_future_impl.h:68
bool valid() const final
Determines if this future still has a shared state with the corresponding promise object.
Definition: quantum_future_impl.h:96