MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mtcore::thread::FutureState< T, E > Struct Template Reference

Represents the state-setter part of a future. More...

#include <future.hpp>

Inheritance diagram for mtcore::thread::FutureState< T, E >:
Collaboration diagram for mtcore::thread::FutureState< T, E >:

Classes

struct  make_res
 

Public Member Functions

void set_val (const T &v)
 
wait ()
 Called by the future, don't call this directly.
 
void deinit (Allocator &alloc)
 

Static Public Member Functions

static Result< make_res, AllocationErrormake (Allocator &alloc, std::function< Result< Arc< E >, AllocationError >(Allocator &alloc, Arc< FutureState > &)> getExtra)
 Makes a future and associated future state.
 

Public Attributes

std::mutex mux = {}
 
std::condition_variable cv = {}
 
Optional< T > value = nullopt
 
Arc< E > extra = {}
 

Detailed Description

template<typename T, typename E>
struct mtcore::thread::FutureState< T, E >

Represents the state-setter part of a future.

When you return a future, your code that sets the future value needs to retain this part so it can actually set the future value. ONLY GET THIS USING THE make FUNCTION! That function will properly link the state to the future. Also, when calling make it will be wrapped in an Arc which you will need to deinit to clean up memory properly

Template Parameters
TFuture result type

Definition at line 141 of file future.hpp.

Member Function Documentation

◆ deinit()

template<typename T, typename E>
void mtcore::thread::FutureState< T, E >::deinit ( Allocator & alloc)
inline

Definition at line 164 of file future.hpp.

164 {
165 if (extra) {
166 extra.deinit(alloc);
167 }
168 }
Represents the state-setter part of a future.
Definition future.hpp:141

◆ make()

template<typename T, typename E>
static Result< make_res, AllocationError > mtcore::thread::FutureState< T, E >::make ( Allocator & alloc,
std::function< Result< Arc< E >, AllocationError >(Allocator &alloc, Arc< FutureState< T, E > > &)> getExtra )
inlinestatic

Makes a future and associated future state.

Will return both

Parameters
allocAllocator for heap allocations
getExtraCallback to get extra state that you may later need to properly work with the future. Don't set it directly on the future state, just return your extra state and the make function will take it from there

Definition at line 182 of file future.hpp.

183 {
185 if (auto err = state.init(alloc); err.is_error()) {
186 return err.error();
187 }
189 if (extra.is_error()) {
191 return extra.error();
192 }
193
194 state->extra = extra.value();
195 auto futureArc = state.acquire(alloc);
196 if (futureArc.is_error()) {
199 }
200 return make_res{.future = Future<T, E>{.state = futureArc.value()}, .state = state};
201 }
Error< Underlying > error(Underlying err)
Creates an error.
Definition result.hpp:425
void deinit(Allocator &alloc)
Definition future.hpp:164
Here is the call graph for this function:

◆ set_val()

template<typename T, typename E>
void mtcore::thread::FutureState< T, E >::set_val ( const T & v)
inline

Definition at line 147 of file future.hpp.

147 {
148 auto l = std::unique_lock{mux};
149 ensure(value.empty(), "CANNOT SET FUTURE VALUE MULTIPLE TIMES! CONSIDER USING A "
150 "CANNEL OR INBOX!");
151 value = v;
152 cv.notify_all();
153 }
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
std::condition_variable cv
Definition future.hpp:143

◆ wait()

template<typename T, typename E>
T mtcore::thread::FutureState< T, E >::wait ( )
inline

Called by the future, don't call this directly.

Definition at line 156 of file future.hpp.

156 {
157 auto l = std::unique_lock{mux};
158 while (value.empty()) {
159 cv.wait(l);
160 }
161 return *value;
162 }

Member Data Documentation

◆ cv

template<typename T, typename E>
std::condition_variable mtcore::thread::FutureState< T, E >::cv = {}

Definition at line 143 of file future.hpp.

143{};

◆ extra

template<typename T, typename E>
Arc<E> mtcore::thread::FutureState< T, E >::extra = {}

Definition at line 145 of file future.hpp.

145{};

◆ mux

template<typename T, typename E>
std::mutex mtcore::thread::FutureState< T, E >::mux = {}

Definition at line 142 of file future.hpp.

142{};

◆ value

template<typename T, typename E>
Optional<T> mtcore::thread::FutureState< T, E >::value = nullopt

Definition at line 144 of file future.hpp.


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