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

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

#include <future.hpp>

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

Classes

struct  make_res
 

Public Member Functions

void set_val (const T &v)
 Sets the value of the associated future.
 
wait ()
 Called by the future, don't call this directly.
 
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)
 Makes a future and associated future state.
 
static Result< make_res, AllocationErrormake (Allocator &alloc, std::function< Result< Arc< void >, 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
 
std::mutex mux
 
std::condition_variable cv
 
Optional< T > value
 
Arc< void > extra
 

Detailed Description

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

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 87 of file future.hpp.

Member Function Documentation

◆ deinit()

void mtcore::thread::FutureState< T, void >::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() [1/2]

template<typename T>
static Result< make_res, AllocationError > mtcore::thread::FutureState< T, void >::make ( Allocator & alloc)
inlinestatic

Makes a future and associated future state.

Will return both

Parameters
allocAllocator for heap allocations

Definition at line 118 of file future.hpp.

118 {
120 if (auto err = state.init(alloc); err.is_error()) {
121 return err.error();
122 }
123 auto futureArc = state.acquire(alloc);
124 if (futureArc.is_error()) {
127 }
128 return make_res{.future = Future<T>{.state = futureArc.value()}, .state = state};
129 }
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:

◆ make() [2/2]

static Result< make_res, AllocationError > mtcore::thread::FutureState< T, void >::make ( Allocator & alloc,
std::function< Result< Arc< void >, AllocationError >(Allocator &alloc, Arc< FutureState< T, void > > &)> 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 }

◆ set_val() [1/2]

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

Sets the value of the associated future.

Definition at line 93 of file future.hpp.

93 {
94 auto l = std::unique_lock{mux};
95 ensure(value.empty(), "CANNOT SET FUTURE VALUE MULTIPLE TIMES! CONSIDER USING A CANNEL OR INBOX!");
96 value = v;
97 cv.notify_all();
98 }
#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:89

◆ set_val() [2/2]

void mtcore::thread::FutureState< T, void >::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 }

◆ wait() [1/2]

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

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

Definition at line 101 of file future.hpp.

101 {
102 auto l = std::unique_lock{mux};
103 while (value.empty()) {
104 cv.wait(l);
105 }
106 return *value;
107 }

◆ wait() [2/2]

T mtcore::thread::FutureState< T, void >::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 [1/2]

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

Definition at line 89 of file future.hpp.

89{};

◆ cv [2/2]

std::condition_variable mtcore::thread::FutureState< T, void >::cv

Definition at line 143 of file future.hpp.

143{};

◆ extra

Arc<void> mtcore::thread::FutureState< T, void >::extra

Definition at line 145 of file future.hpp.

145{};

◆ mux [1/2]

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

Definition at line 88 of file future.hpp.

88{};

◆ mux [2/2]

std::mutex mtcore::thread::FutureState< T, void >::mux

Definition at line 142 of file future.hpp.

142{};

◆ value [1/2]

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

Definition at line 90 of file future.hpp.

◆ value [2/2]

Optional<T> mtcore::thread::FutureState< T, void >::value

Definition at line 144 of file future.hpp.


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