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 173 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 259 of file rc.hpp.

259 {
260 ensure(rc, "Rc is invalid");
261 if (!rc->acquireStrong()) {
263 }
264 ensure(rc->value, "Rc is invalid");
265 return *this;
266 }
#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:173
impl::ThreadLocalRefCount< T > * rc
Definition rc.hpp:174
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 295 of file rc.hpp.

295{ release(alloc); }
void release(Allocator &alloc)
Releases a reference.
Definition rc.hpp:272
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 235 of file rc.hpp.

235 {
236 if (rc) {
237 release(alloc);
238 }
240 if (valRes.is_error()) {
241 return valRes.error();
242 }
243 T *val = valRes.value();
244
246 if (rcRes.is_error()) {
247 alloc.destroy(val);
249 }
250 rc = rcRes.value();
251 rc->value = val;
252 return success();
253 }
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 225 of file rc.hpp.

225{ return valid(); }
bool valid() const
Checks if is valid.
Definition rc.hpp:222
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 177 of file rc.hpp.

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

◆ operator*() [2/2]

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

Dereferences pointer.

Definition at line 183 of file rc.hpp.

183 {
184 ensure(rc, "Invalid Rc");
185 return *rc->value;
186 }

◆ operator->() [1/2]

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

Dereferences pointer.

Definition at line 189 of file rc.hpp.

189 {
190 ensure(rc, "Invalid Rc");
191 return rc->value;
192 }

◆ operator->() [2/2]

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

Dereferences pointer.

Definition at line 195 of file rc.hpp.

195 {
196 ensure(rc, "Invalid Rc");
197 return rc->value;
198 }

◆ operator==() [1/3]

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

Compares to a pointer.

Definition at line 201 of file rc.hpp.

201{ return rc == other.rc; }

◆ operator==() [2/3]

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

Compares to a pointer.

Definition at line 213 of file rc.hpp.

213 {
214 if (other == nullptr && rc == nullptr) {
215 return true;
216 }
217 ensure(rc, "Invalid Rc");
218 return rc->value == other;
219 }

◆ operator==() [3/3]

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

Compares to a pointer.

Definition at line 204 of file rc.hpp.

204 {
205 if (other == nullptr && rc == nullptr) {
206 return true;
207 }
208 ensure(rc, "Invalid Rc");
209 return rc->value == other;
210 }

◆ 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 272 of file rc.hpp.

272 {
273 if (!rc) {
274 return;
275 }
276 ensure(rc, "Rc is invalid");
277 ensure(rc->value, "Rc is invalid");
278 switch (impl::StrongReleaseAction action = rc->releaseStrong()) {
280 break;
282 alloc.destroy(rc->value);
283 rc->value = nullptr;
284 alloc.destroy(rc);
285 break;
287 alloc.destroy(rc->value);
288 rc->value = nullptr;
289 break;
290 }
291 rc = nullptr;
292 }
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 222 of file rc.hpp.

222{ 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 300 of file rc.hpp.

300 {
301 ensure(rc, "Invalid Rc");
302 if (!rc->acquireWeak()) {
304 }
305 return success(WeakRc<T>{rc});
306 }
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 174 of file rc.hpp.


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