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

Thread-local reference counted pointer Not thread safe, don't share Reference counting for cleaning up memory Supports weak references as well. More...

#include <rc.hpp>

Public Member Functions

T & operator* ()
 Dereferences pointer.
 
const T & operator* () const
 Dereferences pointer.
 
T * operator-> ()
 Dereferences pointer.
 
const T * operator-> () const
 Dereferences pointer.
 
bool operator== (const Rc &other) const
 Compares to a pointer.
 
bool operator== (T *other) const
 Compares to a pointer.
 
bool operator== (const T *other) const
 Compares to a pointer.
 
bool valid () const
 Checks if is valid.
 
 operator bool () const
 Checks if is valid.
 
template<typename... Args>
Result< void, AllocationErrorinit (Allocator &alloc, Args... args)
 Initializes a reference counter to point to a new entity.
 
Result< Rc, RcErroracquire ()
 Tries to acquire another strong reference Will return either another strong reference or an error.
 
void release (Allocator &alloc)
 Releases a reference.
 
void deinit (Allocator &alloc)
 Same as release.
 
Result< WeakRc< T >, RcErrorweak ()
 Attempts to get a weak reference.
 

Public Attributes

impl::ThreadLocalRefCount< T > * rc = nullptr
 

Detailed Description

template<typename T>
struct mtcore::Rc< T >

Thread-local reference counted pointer Not thread safe, don't share Reference counting for cleaning up memory Supports weak references as well.

Template Parameters
TType pointed to

Definition at line 155 of file rc.hpp.

Member Function Documentation

◆ acquire()

template<typename T>
Result< Rc, RcError > mtcore::Rc< T >::acquire ( )
inlinenodiscard

Tries to acquire another strong reference Will return either another strong reference or an error.

Definition at line 243 of file rc.hpp.

243 {
244 ensure(rc, "Rc is invalid");
245 if (!rc->acquireStrong()) {
247 }
248 ensure(rc->value, "Rc is invalid");
249 return *this;
250 }
#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
Thread-local reference counted pointer Not thread safe, don't share Reference counting for cleaning u...
Definition rc.hpp:155
impl::ThreadLocalRefCount< T > * rc
Definition rc.hpp:156
Here is the call graph for this function:

◆ deinit()

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

Same as release.

See also
release

Definition at line 279 of file rc.hpp.

279 {
280 release(alloc);
281 }
void release(Allocator &alloc)
Releases a reference.
Definition rc.hpp:256
Here is the call graph for this function:

◆ init()

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

Initializes a reference counter to point to a new entity.

Template Parameters
ArgsTypes of constructor arguments
Parameters
allocAllocator for initial allocation
argsArguments to use for construction
Returns
Success or allocation error

Definition at line 219 of file rc.hpp.

219 {
220 if (rc) {
221 release(alloc);
222 }
224 if (valRes.is_error()) {
225 return valRes.error();
226 }
227 T* val = valRes.value();
228
230 if (rcRes.is_error()) {
231 alloc.destroy(val);
233 }
234 rc = rcRes.value();
235 rc->value = val;
236 return success();
237 }
Success< void > success()
Creates a successful void Result object.
Definition result.hpp:398
Here is the call graph for this function:

◆ operator bool()

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

Checks if is valid.

Definition at line 209 of file rc.hpp.

209{ return valid(); }
bool valid() const
Checks if is valid.
Definition rc.hpp:206
Here is the call graph for this function:

◆ operator*() [1/2]

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

Dereferences pointer.

Definition at line 159 of file rc.hpp.

159 {
160 ensure(rc, "Invalid Rc");
161 return *rc->value;
162 }

◆ operator*() [2/2]

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

Dereferences pointer.

Definition at line 165 of file rc.hpp.

165 {
166 ensure(rc, "Invalid Rc");
167 return *rc->value;
168 }

◆ operator->() [1/2]

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

Dereferences pointer.

Definition at line 171 of file rc.hpp.

171 {
172 ensure(rc, "Invalid Rc");
173 return rc->value;
174 }

◆ operator->() [2/2]

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

Dereferences pointer.

Definition at line 177 of file rc.hpp.

177 {
178 ensure(rc, "Invalid Rc");
179 return rc->value;
180 }

◆ operator==() [1/3]

template<typename T>
bool mtcore::Rc< T >::operator== ( const Rc< T > & other) const
inlinenodiscard

Compares to a pointer.

Definition at line 183 of file rc.hpp.

183 {
184 return rc == other.rc;
185 }

◆ operator==() [2/3]

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

Compares to a pointer.

Definition at line 197 of file rc.hpp.

197 {
198 if (other == nullptr && rc == nullptr) {
199 return true;
200 }
201 ensure(rc, "Invalid Rc");
202 return rc->value == other;
203 }

◆ operator==() [3/3]

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

Compares to a pointer.

Definition at line 188 of file rc.hpp.

188 {
189 if (other == nullptr && rc == nullptr) {
190 return true;
191 }
192 ensure(rc, "Invalid Rc");
193 return rc->value == other;
194 }

◆ release()

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

Releases a reference.

May clean up object

Parameters
allocAllocator to use for potential cleanup

Definition at line 256 of file rc.hpp.

256 {
257 if (!rc) {
258 return;
259 }
260 ensure(rc, "Rc is invalid");
261 ensure(rc->value, "Rc is invalid");
262 switch (impl::StrongReleaseAction action = rc->releaseStrong()) {
264 break;
266 alloc.destroy(rc->value);
267 rc->value = nullptr;
268 alloc.destroy(rc);
269 break;
271 alloc.destroy(rc->value);
272 rc->value = nullptr;
273 break;
274 }
275 rc = nullptr;
276 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ valid()

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

Checks if is valid.

Definition at line 206 of file rc.hpp.

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

◆ weak()

template<typename T>
Result< WeakRc< T >, RcError > mtcore::Rc< T >::weak ( )
inline

Attempts to get a weak reference.

Definition at line 286 of file rc.hpp.

286 {
287 ensure(rc, "Invalid Rc");
288 if (!rc->acquireWeak()) {
290 }
291 return success(WeakRc<T>{rc});
292 }
Here is the call graph for this function:

Member Data Documentation

◆ rc

template<typename T>
impl::ThreadLocalRefCount<T>* mtcore::Rc< T >::rc = nullptr

Definition at line 156 of file rc.hpp.


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