Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
database::async::async_result< T > Class Template Reference

Template class for asynchronous operation results. More...

#include <async_operations.h>

Collaboration diagram for database::async::async_result< T >:
Collaboration graph

Public Member Functions

 async_result (std::future< T > future)
 
get ()
 
get_for (std::chrono::milliseconds timeout)
 
bool is_ready () const
 
std::future_status wait_for (std::chrono::milliseconds timeout) const
 
template<concepts::VoidCallable< T > Callback>
void then (Callback &&callback)
 
template<concepts::ErrorHandler Handler>
void on_error (Handler &&error_handler)
 
void then (std::function< void(T)> callback)
 
void on_error (std::function< void(const std::exception &)> error_handler)
 

Private Attributes

std::future< T > future_
 
std::mutex callback_mutex_
 
std::function< void(T)> success_callback_
 
std::function< void(const std::exception &)> error_callback_
 

Detailed Description

template<typename T>
class database::async::async_result< T >

Template class for asynchronous operation results.

Thread-safety: Callback registration methods (then, on_error) are thread-safe. get() and get_for() should only be called once as they consume the future.

Definition at line 90 of file async_operations.h.

Constructor & Destructor Documentation

◆ async_result()

template<typename T >
database::async::async_result< T >::async_result ( std::future< T > future)
Examples
/home/runner/work/database_system/database_system/database/async/async_operations.h.

Definition at line 125 of file async_operations.h.

126 : future_(std::move(future))
127 {
128 }

Member Function Documentation

◆ get()

template<typename T >
T database::async::async_result< T >::get ( )
Examples
/home/runner/work/database_system/database_system/database/async/async_operations.h.

Definition at line 131 of file async_operations.h.

132 {
133 std::function<void(T)> on_success;
134 std::function<void(const std::exception&)> on_error_cb;
135 {
136 std::lock_guard<std::mutex> lock(callback_mutex_);
137 on_success = success_callback_;
138 on_error_cb = error_callback_;
139 }
140
141 try {
142 T value = future_.get();
143 if (on_success) {
144 on_success(value);
145 }
146 return value;
147 } catch (const std::exception& e) {
148 if (on_error_cb) {
149 on_error_cb(e);
150 }
151 throw;
152 }
153 }
std::function< void(T)> success_callback_
std::function< void(const std::exception &)> error_callback_

Referenced by TEST_F(), and database::async::transaction_coordinator::two_phase_commit().

Here is the caller graph for this function:

◆ get_for()

template<typename T >
T database::async::async_result< T >::get_for ( std::chrono::milliseconds timeout)
Examples
/home/runner/work/database_system/database_system/database/async/async_operations.h.

Definition at line 156 of file async_operations.h.

157 {
158 auto status = future_.wait_for(timeout);
159 if (status == std::future_status::timeout) {
160 throw std::runtime_error("async_result::get_for timed out");
161 }
162 return get();
163 }

References database::timeout.

Referenced by TEST_F().

Here is the caller graph for this function:

◆ is_ready()

template<typename T >
bool database::async::async_result< T >::is_ready ( ) const
Examples
/home/runner/work/database_system/database_system/database/async/async_operations.h.

Definition at line 166 of file async_operations.h.

167 {
168 return future_.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready;
169 }

Referenced by TEST_F(), and TEST_F().

Here is the caller graph for this function:

◆ on_error() [1/2]

template<typename T >
template<concepts::ErrorHandler Handler>
void database::async::async_result< T >::on_error ( Handler && error_handler)
Examples
/home/runner/work/database_system/database_system/database/async/async_operations.h.

Definition at line 187 of file async_operations.h.

188 {
189 std::lock_guard<std::mutex> lock(callback_mutex_);
190 error_callback_ = std::forward<Handler>(error_handler);
191 }

◆ on_error() [2/2]

template<typename T >
void database::async::async_result< T >::on_error ( std::function< void(const std::exception &)> error_handler)

Definition at line 201 of file async_operations.h.

202 {
203 std::lock_guard<std::mutex> lock(callback_mutex_);
204 error_callback_ = std::move(error_handler);
205 }

◆ then() [1/2]

template<typename T >
template<concepts::VoidCallable< T > Callback>
void database::async::async_result< T >::then ( Callback && callback)
Examples
/home/runner/work/database_system/database_system/database/async/async_operations.h.

Definition at line 179 of file async_operations.h.

180 {
181 std::lock_guard<std::mutex> lock(callback_mutex_);
182 success_callback_ = std::forward<Callback>(callback);
183 }

◆ then() [2/2]

template<typename T >
void database::async::async_result< T >::then ( std::function< void(T)> callback)

Definition at line 194 of file async_operations.h.

195 {
196 std::lock_guard<std::mutex> lock(callback_mutex_);
197 success_callback_ = std::move(callback);
198 }

◆ wait_for()

template<typename T >
std::future_status database::async::async_result< T >::wait_for ( std::chrono::milliseconds timeout) const
Examples
/home/runner/work/database_system/database_system/database/async/async_operations.h.

Definition at line 172 of file async_operations.h.

173 {
174 return future_.wait_for(timeout);
175 }

References database::timeout.

Referenced by TEST_F().

Here is the caller graph for this function:

Member Data Documentation

◆ callback_mutex_

template<typename T >
std::mutex database::async::async_result< T >::callback_mutex_
mutableprivate

◆ error_callback_

template<typename T >
std::function<void(const std::exception&)> database::async::async_result< T >::error_callback_
private

◆ future_

template<typename T >
std::future<T> database::async::async_result< T >::future_
private

◆ success_callback_

template<typename T >
std::function<void(T)> database::async::async_result< T >::success_callback_
private

The documentation for this class was generated from the following file: