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

Weak Automic Reference Count. More...

#include <arc.hpp>

Collaboration diagram for mtcore::thread::WeakArc< T >:

Public Member Functions

Result< Arc< T >, ArcErrorobtain (Allocator &alloc)
 Tries to obtain a strong reference to what's pointed at.
 
void deinit (Allocator &alloc)
 Deinitializes a weak pointer (may Result in memory cleanup if last weak counter)
 
bool valid () const
 Checks if a weak pointer is "valid" as in it's set to point to something Does NOT check if the referenced object is still alive (use obtain for that)
 
Result< WeakArc, ArcErrorcopy ()
 Tries to copy a weak reference.
 

Public Attributes

impl::RefCount< impl::RefCount< T > > * rc = nullptr
 

Detailed Description

template<typename T>
struct mtcore::thread::WeakArc< T >

Weak Automic Reference Count.

A thread-safe weak reference count (basically a shared pointer replacement) WeakArc holds weak references. References must be upgraded to strong references before dereferencing Unlike std::weak_ptr, you must copy() and denit() manually (use mtdefer)

Template Parameters
TType of data pointed to

Definition at line 141 of file arc.hpp.

Member Function Documentation

◆ copy()

template<typename T>
Result< WeakArc, ArcError > mtcore::thread::WeakArc< T >::copy ( )
inline

Tries to copy a weak reference.

Returns
Another weak reference (if can obtain)

Definition at line 180 of file arc.hpp.

180 {
181 ensure(rc, "Arc is invalid");
182 if (rc->acquire()) {
183 return *this;
184 }
186 }
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
Error< Underlying > error(Underlying err)
Creates an error.
Definition result.hpp:425
Weak Automic Reference Count.
Definition arc.hpp:141
impl::RefCount< impl::RefCount< T > > * rc
Definition arc.hpp:142
Here is the call graph for this function:

◆ deinit()

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

Deinitializes a weak pointer (may Result in memory cleanup if last weak counter)

Parameters
allocAllocator for cleanup

Definition at line 155 of file arc.hpp.

155 {
156 if (!rc) {
157 return;
158 }
159 ensure(rc, "Arc is invalid");
160 ensure(rc->value, "Arc is invalid");
161 if (rc->release(alloc)) {
162 alloc.destroy(rc->value);
163 rc->value = nullptr;
164 alloc.destroy(rc);
165 }
166 rc = nullptr;
167 }
Here is the call graph for this function:

◆ obtain()

template<typename T>
Result< Arc< T >, ArcError > mtcore::thread::WeakArc< T >::obtain ( Allocator & alloc)

Tries to obtain a strong reference to what's pointed at.

Parameters
allocAllocator to use for memory allocation/deallocation
Returns
Result with strong reference (if possible) or error

Definition at line 359 of file arc.hpp.

359 {
360 ensure(rc, "Arc is invalid");
361 if (!rc->acquire()) {
363 }
364
365 if (!rc->value->acquire()) {
366 rc->release(alloc);
368 }
369 return Arc<T>{rc};
370 }
Here is the call graph for this function:

◆ valid()

template<typename T>
bool mtcore::thread::WeakArc< T >::valid ( ) const
inlinenodiscard

Checks if a weak pointer is "valid" as in it's set to point to something Does NOT check if the referenced object is still alive (use obtain for that)

Returns
bool if RC is valid

Definition at line 174 of file arc.hpp.

174{ return rc != nullptr; }

Member Data Documentation

◆ rc

template<typename T>
impl::RefCount<impl::RefCount<T> >* mtcore::thread::WeakArc< T >::rc = nullptr

Definition at line 142 of file arc.hpp.


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