Mountain  1.0.0
Simple C++ 2D Game Framework
Mountain::Pointer< T > Class Template Reference

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 TGet () const
 Gets the underlying raw pointer. More...
 
TGet ()
 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...
 
Pointeroperator= (const Pointer &other)
 Sets this Pointer to the values of other.
 
Pointeroperator= (Pointer &&other) noexcept
 Sets this Pointer to the values of other, moving all the data.
 
Pointeroperator= (nullptr_t)
 Sets this Pointer to nullptr.
 
template<typename U >
Pointeroperator= (const Pointer< U > &other)
 Sets this Pointer to the values of other which is a Pointer of another Type.
 
template<typename U >
Pointeroperator= (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.
 
Toperator* ()
 Dereferences this Pointer, which gives a reference to the underlying Type.
 
const Toperator* () const
 Dereferences this const Pointer, which gives a const reference to the underlying Type.
 
Toperator-> ()
 Dereferences this Pointer, which gives a reference to the underlying Type.
 
const Toperator-> () 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.
 

Detailed Description

template<typename T>
class Mountain::Pointer< T >

Custom Mountain smart pointer. Represents both a std::shared_ptr and a std::weak_ptr.

Reason

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.

Usage

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:

struct Type { int i; explicit Type(int i) : i(i) {} };
Pointer<Type> ptr = Pointer<Type>::New(7);

... and 7 will be forwarded as a parameter to the Type(int) constructor.

There are 3 other ways of initializing a Pointer:

// 1 - Default initialize the Pointer: this wraps a nullptr
Pointer<Type> ptr;
// 2 - Default initialize the wrapped value: this effectively calls the wrapped type's default constructor
Pointer<Type> ptr = Pointer<Type>::New();
// 3 - Manually set the Pointer to nullptr: this is actually the same as default initializing it
Pointer<Type> ptr = nullptr;

Weak and Strong References

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.

Template Parameters
TThe type to point to. Most of the time, this shouldn't be a pointer type.
See also
Smart Pointers
std::shared_ptr
std::weak_ptr

Definition at line 23 of file meta_programming.hpp.

Constructor & Destructor Documentation

◆ Pointer()

template<typename T>
template<typename U >
Mountain::Pointer< T >::Pointer ( const Pointer< U > &  other,
bool_t  strongReference = false 
)
explicit

Creates a copy of an existing Pointer of a different Type, specifying whether it is a strong reference.

Template Parameters
UThe type of the existing Pointer. This type must be convertible to Type.

Member Function Documentation

◆ Get() [1/2]

template<typename T>
const T* Mountain::Pointer< T >::Get ( ) const

Gets the underlying raw pointer.

This is equivalent to calling Pointer::operator T*() const;

◆ Get() [2/2]

template<typename T>
T* Mountain::Pointer< T >::Get ( )

Gets the underlying raw pointer.

This is equivalent to calling Pointer::operator T*();

◆ Reset()

template<typename T>
void Mountain::Pointer< T >::Reset ( )

Resets this Pointer to a nullptr.

Warning
This function is used internally and is not meant to be used except if you really know what you are doing. This doesn't free the potentially allocated memory. Use it at your own risks.

The documentation for this class was generated from the following files: