QuantumLibrary
quantum_future_state.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_STATE_H
17 #define BLOOMBERG_QUANTUM_FUTURE_STATE_H
18 
19 #include <future>
20 #include <exception>
21 #include <map>
22 
23 namespace Bloomberg {
24 namespace quantum {
25 
26 //==============================================================================================
27 // enum FutureState
28 //==============================================================================================
31 enum class FutureState
32 {
37  NoState,
40 };
41 
42 //==============================================================================================
43 // struct FutureException
44 //==============================================================================================
47 struct FutureException : public std::exception
48 {
52  _error(error)
53  {}
54 
57  const char* what() const noexcept override
58  {
59  static std::map<FutureState, const char*> msg{
60  {FutureState::PromiseNotSatisfied, "Promise not yet satisfied"},
61  {FutureState::PromiseAlreadySatisfied, "Promise already satisfied"},
62  {FutureState::BrokenPromise, "Broken promise"},
63  {FutureState::FutureAlreadyRetrieved, "Future already retrieved"},
64  {FutureState::NoState, "Invalid state"},
65  {FutureState::BufferingData, "Buffering future data"},
66  {FutureState::BufferClosed, "Buffer closed"}
67  };
68  return msg[_error];
69  }
70 
71 private:
72  //Members
73  FutureState _error;
74 };
75 
76 
77 //==============================================================================================
78 // Specialised FutureException
79 //==============================================================================================
81 {
84  {}
85 };
86 
88 {
91  {}
92 };
93 
95 {
98  {}
99 };
100 
102 {
105  {}
106 };
107 
109 {
112  {}
113 };
114 
116 {
119  {}
120 };
121 
123 {
126  {}
127 };
128 
129 inline
131 {
132  switch (state)
133  {
141  default: throw std::logic_error("Invalid future state");
142  }
143 }
144 
145 }}
146 
147 #endif //BLOOMBERG_QUANTUM_FUTURE_STATE_H
PromiseNotSatisfiedException()
Definition: quantum_future_state.h:82
Definition: quantum_buffer_impl.h:22
Definition: quantum_future_state.h:80
BrokenPromiseException()
Definition: quantum_future_state.h:96
Definition: quantum_future_state.h:115
Definition: quantum_future_state.h:108
BufferingDataException()
Definition: quantum_future_state.h:117
Definition: quantum_future_state.h:94
Exception thrown by a Future or Promise object during various errors.
Definition: quantum_future_state.h:47
FutureAlreadyRetrievedException()
Definition: quantum_future_state.h:103
Promise could not be fulfilled.
Buffered future is being streamed.
Definition: quantum_future_state.h:122
const char * what() const noexcept override
Get the encapsulated error message from the exception object.
Definition: quantum_future_state.h:57
Definition: quantum_future_state.h:101
Future value has been consumed. In the case of a buffer, no pulling is allowed.
void ThrowFutureException(FutureState state)
Definition: quantum_future_state.h:130
FutureState
Represents the internal state of a future/promise pair. Modeled after std::future_errc.
Definition: quantum_future_state.h:31
Definition: quantum_future_state.h:87
FutureException(FutureState error)
Constructor.
Definition: quantum_future_state.h:51
Buffer is closed for pushing data. Data can still be pulled.
NoStateException()
Definition: quantum_future_state.h:110
Shared state between Promise and Future is invalid.
PromiseAlreadySatisfiedException()
Definition: quantum_future_state.h:89
Future value has been set but not yet consumed.
BufferClosedException()
Definition: quantum_future_state.h:124