Mountain
1.0.0
Simple C++ 2D Game Framework
|
Custom Mountain smart pointer. Represents both a std::shared_ptr
and a std::weak_ptr
.
More...
#include <pointer.hpp>
Public Types | |
using | Type = T |
The type of ReferenceCounter, and therefore the type this Pointer is pointing to. | |
Public Member Functions | |
Pointer ()=default | |
Creates an empty Pointer without a reference counter and pointing to nullptr . | |
Pointer (const Pointer &other, bool_t strongReference=false) | |
Creates a copy of another Pointer, specifying whether it is a weak or strong reference. | |
Pointer (Pointer &&other) noexcept | |
Creates a Pointer by moving all the values of an existing one. | |
Pointer (nullptr_t) | |
Creates a Pointer from a nullptr . | |
template<typename U > | |
Pointer (const Pointer< U > &other, bool_t strongReference=false) | |
Creates a copy of an existing Pointer of a different Type, specifying whether it is a strong reference. More... | |
template<typename U > | |
Pointer (Pointer< U > &other, bool_t strongReference=false) | |
Creates a copy of an existing Pointer of a different Type, specifying whether it is a strong reference. | |
template<typename U > | |
Pointer (Pointer< U > &&other) noexcept | |
Creates a Pointer by moving the value of another one of a different Type. | |
~Pointer () | |
Destroys this Pointer, deallocating any memory if this is the last strong reference. | |
Pointer | CreateStrongReference () const |
Creates a new strong reference to this pointer. | |
const T * | Get () const |
Gets the underlying raw pointer. More... | |
T * | Get () |
Gets the underlying raw pointer. More... | |
bool_t | IsValid () const |
Returns whether this Pointer is nullptr . | |
void | ToStrongReference () |
Converts this Pointer to a strong reference. | |
void | ToWeakReference () |
Converts this Pointer to a weak reference. | |
const ReferenceCounter< T > * | GetReferenceCounter () const |
Returns the underlying ReferenceCounter. | |
bool_t | GetIsStrongReference () const |
Returns whether this Pointer is holding a strong reference. | |
void | Reset () |
Resets this Pointer to a nullptr . More... | |
Pointer & | operator= (const Pointer &other) |
Sets this Pointer to the values of other . | |
Pointer & | operator= (Pointer &&other) noexcept |
Sets this Pointer to the values of other , moving all the data. | |
Pointer & | operator= (nullptr_t) |
Sets this Pointer to nullptr . | |
template<typename U > | |
Pointer & | operator= (const Pointer< U > &other) |
Sets this Pointer to the values of other which is a Pointer of another Type. | |
template<typename U > | |
Pointer & | operator= (Pointer< U > &&other) noexcept |
Sets this Pointer to the values of other which is a Pointer of another Type, moving all the data. | |
operator const T * () const | |
Converts this const Pointer to its underlying const raw pointer. | |
operator T* () | |
Converts this Pointer to its underlying raw pointer. | |
operator bool_t () const | |
Converts this Pointer to a bool_t the same way a raw pointer would. | |
T & | operator* () |
Dereferences this Pointer, which gives a reference to the underlying Type. | |
const T & | operator* () const |
Dereferences this const Pointer, which gives a const reference to the underlying Type. | |
T * | operator-> () |
Dereferences this Pointer, which gives a reference to the underlying Type. | |
const T * | operator-> () const |
Dereferences this const Pointer, which gives a const reference to the underlying Type. | |
Static Public Member Functions | |
template<typename... Args> | |
static Pointer | New (Args &&... args) |
Creates a Pointer, calling new with T and forwarding all given arguments to its constructor. | |
static Pointer | New () |
Creates a Pointer with a default-initialized value. | |
Custom Mountain smart pointer. Represents both a std::shared_ptr
and a std::weak_ptr
.
While using std::weak_ptr
, we realized that it was not very practical because a std::shared_ptr
needs to be constructed from the former for the pointed type to be used. The Pointer type is meant to fix this issue by being both a strong and a weak shared pointer.
The simplest way to create a Pointer to wrap your type is by using the forwarding variadic template function which allows you to do the following:
... and 7 will be forwarded as a parameter to the Type(int)
constructor.
There are 3 other ways of initializing a Pointer:
By default, creating a Pointer with constructor arguments from the pointed type allocates this type on the heap. Copying this instance of Pointer creates a new weak reference by default, meaning that the copy won't keep the raw pointer alive. When all the strong references go out of scope or are destroyed, the underlying pointed type is freed. A strong reference can still be created if needed, by calling either Pointer::CreateStrongReference(), Pointer::ToStrongReference(), or by creating a copy using the copy constructor and giving a true
value to the second argument.
T | The type to point to. Most of the time, this shouldn't be a pointer type. |
Definition at line 23 of file meta_programming.hpp.
|
explicit |
const T* Mountain::Pointer< T >::Get | ( | ) | const |
Gets the underlying raw pointer.
This is equivalent to calling Pointer::operator T*() const;
T* Mountain::Pointer< T >::Get | ( | ) |
Gets the underlying raw pointer.
This is equivalent to calling Pointer::operator T*();
void Mountain::Pointer< T >::Reset | ( | ) |
Resets this Pointer to a nullptr
.