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

Automic Reference Count. More...

#include <arc.hpp>

Inheritance diagram for mtcore::thread::Arc< T >:
Collaboration diagram for mtcore::thread::Arc< T >:

Public Member Functions

T * operator-> ()
 Dereferences an Arc pointer.
 
const T * operator-> () const
 Dereferences an Arc pointer.
 
T & operator* ()
 Dereferences an Arc pointer.
 
const T & operator* () const
 Dereferences an Arc pointer.
 
bool operator== (const Arc &o) const
 Checks if an Arc is equivalent to another Arc.
 
bool operator== (T *other) const
 Checks if an Arc is equivalent to a pointer (used for nullptr checks)
 
bool operator== (const T *other) const
 Checks if an Arc is equivalent to a pointer (used for nullptr checks)
 
bool valid () const
 Checks if an Arc is valid (it points to something, and that something still exists)
 
 operator bool () const
 Checks if an Arc is valid (it points to something, and that something still exists)
 
template<typename... Args>
Result< void, AllocationErrorinit (Allocator &alloc, Args... args)
 Tries to initialize an ARC to point to a new object.
 
Result< Arc, ArcErroracquire (Allocator &alloc)
 Acquires a new Arc reference (increments ref count by 1)
 
void release (Allocator &alloc)
 Releases an Arc reference (decrements ref count by 1)
 
void deinit (Allocator &alloc)
 
Result< WeakArc< T >, ArcErrorweak ()
 Tries to get a weak pointer.
 

Public Attributes

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

Detailed Description

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

Automic Reference Count.

A thread-safe reference count (basically a shared pointer replacement) Arc holds strong references Unlike std::shared_ptr, you must acquire() and release() manually (use mtdefer)

Template Parameters
TType of data pointed to

Definition at line 190 of file arc.hpp.

Member Function Documentation

◆ acquire()

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

Acquires a new Arc reference (increments ref count by 1)

Parameters
allocAllocator for new pointer allocation
Returns
Resulting Arc or error

Definition at line 308 of file arc.hpp.

308 {
309 ensure(rc, "Arc is invalid");
310 if (!rc->acquire()) {
312 }
313 ensure(rc->value, "Arc is invalid");
314 if (!rc->value->acquire()) {
315 rc->release(alloc);
317 }
318 return *this;
319 }
#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
Automic Reference Count.
Definition arc.hpp:190
impl::RefCount< impl::RefCount< T > > * rc
Definition arc.hpp:191
Here is the call graph for this function:
Here is the caller graph for this function:

◆ deinit()

template<typename T>
void mtcore::thread::Arc< T >::deinit ( Allocator & alloc)
inline
See also
Arc<typename T>::release()

Definition at line 343 of file arc.hpp.

343{ release(alloc); }
void release(Allocator &alloc)
Releases an Arc reference (decrements ref count by 1)
Definition arc.hpp:325
Here is the call graph for this function:
Here is the caller graph for this function:

◆ init()

template<typename T>
template<typename... Args>
Result< void, AllocationError > mtcore::thread::Arc< T >::init ( Allocator & alloc,
Args... args )
inlinenodiscard

Tries to initialize an ARC to point to a new object.

Template Parameters
ArgsConstructor argument types
Parameters
allocAllocator for memory allocation
argsConstructor arguments
Returns
success or error

Definition at line 282 of file arc.hpp.

282 {
283 if (rc) {
284 release(alloc);
285 }
287 if (valRes.is_error()) {
288 return valRes.error();
289 }
290 T *val = valRes.value();
291
293 if (outerRcRes.is_error()) {
294 alloc.destroy(val);
295 return outerRcRes.error();
296 }
298 outerRc->value->value = valRes.value();
299 rc = outerRc;
300 return success();
301 }
Success< void > success()
Creates a successful void Result object.
Definition result.hpp:398
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator bool()

template<typename T>
mtcore::thread::Arc< T >::operator bool ( ) const
inlinenodiscard

Checks if an Arc is valid (it points to something, and that something still exists)

Definition at line 272 of file arc.hpp.

272{ return valid(); }
bool valid() const
Checks if an Arc is valid (it points to something, and that something still exists)
Definition arc.hpp:267
Here is the call graph for this function:

◆ operator*() [1/2]

template<typename T>
T & mtcore::thread::Arc< T >::operator* ( )
inlinenodiscard

Dereferences an Arc pointer.

Definition at line 216 of file arc.hpp.

216 {
217 ensure(rc, "Invalid Arc");
218 ensure(rc->value, "Invalid Arc");
219 ensure(rc->value->value, "Invalid Arc");
220 return *rc->value->value;
221 }

◆ operator*() [2/2]

template<typename T>
const T & mtcore::thread::Arc< T >::operator* ( ) const
inlinenodiscard

Dereferences an Arc pointer.

Definition at line 226 of file arc.hpp.

226 {
227 ensure(rc, "Invalid Arc");
228 ensure(rc->value, "Invalid Arc");
229 ensure(rc->value->value, "Invalid Arc");
230 return *rc->value->value;
231 }

◆ operator->() [1/2]

template<typename T>
T * mtcore::thread::Arc< T >::operator-> ( )
inlinenodiscard

Dereferences an Arc pointer.

Definition at line 196 of file arc.hpp.

196 {
197 ensure(rc, "Invalid Arc");
198 ensure(rc->value, "Invalid Arc");
199 ensure(rc->value->value, "Invalid Arc");
200 return rc->value->value;
201 }

◆ operator->() [2/2]

template<typename T>
const T * mtcore::thread::Arc< T >::operator-> ( ) const
inlinenodiscard

Dereferences an Arc pointer.

Definition at line 206 of file arc.hpp.

206 {
207 ensure(rc, "Invalid Arc");
208 ensure(rc->value, "Invalid Arc");
209 ensure(rc->value->value, "Invalid Arc");
210 return rc->value->value;
211 }

◆ operator==() [1/3]

template<typename T>
bool mtcore::thread::Arc< T >::operator== ( const Arc< T > & o) const
inlinenodiscard

Checks if an Arc is equivalent to another Arc.

Definition at line 236 of file arc.hpp.

236{ return o.rc == rc; }

◆ operator==() [2/3]

template<typename T>
bool mtcore::thread::Arc< T >::operator== ( const T * other) const
inlinenodiscard

Checks if an Arc is equivalent to a pointer (used for nullptr checks)

Definition at line 254 of file arc.hpp.

254 {
255 if (other == nullptr && rc == nullptr) {
256 return true;
257 }
258 ensure(rc, "Invalid Arc");
259 ensure(rc->value, "Invalid Arc");
260 ensure(rc->value->value, "Invalid Arc");
261 return rc->value->value == other;
262 }

◆ operator==() [3/3]

template<typename T>
bool mtcore::thread::Arc< T >::operator== ( T * other) const
inlinenodiscard

Checks if an Arc is equivalent to a pointer (used for nullptr checks)

Definition at line 241 of file arc.hpp.

241 {
242 if (other == nullptr && rc == nullptr) {
243 return true;
244 }
245 ensure(rc, "Invalid Arc");
246 ensure(rc->value, "Invalid Arc");
247 ensure(rc->value->value, "Invalid Arc");
248 return rc->value->value == other;
249 }

◆ release()

template<typename T>
void mtcore::thread::Arc< T >::release ( Allocator & alloc)
inline

Releases an Arc reference (decrements ref count by 1)

Parameters
allocAllocator for possible memory reclamation

Definition at line 325 of file arc.hpp.

325 {
326 if (!rc) {
327 return;
328 }
329 ensure(rc, "Arc is invalid");
330 ensure(rc->value, "Arc is invalid");
331 (void) rc->value->release(alloc);
332 if (rc->release(alloc)) {
333 alloc.destroy(rc->value);
334 rc->value = nullptr;
335 alloc.destroy(rc);
336 }
337 rc = nullptr;
338 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ valid()

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

Checks if an Arc is valid (it points to something, and that something still exists)

Definition at line 267 of file arc.hpp.

267{ return rc != nullptr && rc->value != nullptr && rc->value->value; }
Here is the caller graph for this function:

◆ weak()

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

Tries to get a weak pointer.

Returns
Weak pointer (if successfully made) or error

Definition at line 349 of file arc.hpp.

349 {
350 ensure(rc, "Invalid Arc");
351 if (!rc->acquire()) {
353 }
354 return success(WeakArc<T>{rc});
355 }
Here is the call graph for this function:

Member Data Documentation

◆ rc

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

Definition at line 191 of file arc.hpp.


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