template<typename T>
owning_ptr class
Heap allocated owned value.
Contents
This is basically an implementation of Rusts Box. It behaves exactly like T
but lives on the heap.
In other words this is a std::unique_ptr
that also support copying but can't contain a nullptr
.
If this type is copied it will make a new heap allocation and copy the value.
The default constructor of this class will call the default constructor of T
instead of using a nullptr (like std::unique_ptr
does).
In case any of the copy/move/swap functions throw an exception you should expect the owning_
owning_T
is. These special member functions are always defined but can only be used if the type T
also has them.
Supports equality comparsion if T
does.
Constructors, destructors, conversion operators
- owning_ptr()
- Default constructor.
- owning_ptr(T* value) explicit
- Creates an owning_
ptr from the given pointer. -
owning_ptr(const owning_
ptr<T>& other) - Copy constructor.
-
owning_ptr(owning_
ptr<T>&& other) - Move constructor.
Public functions
-
auto operator=(const owning_
ptr<T>& other) -> owning_ptr< T > & -> auto - Copy assignment operator.
-
auto operator=(owning_
ptr<T>&& other) -> owning_ptr< T > & -> auto - Move assignment operator.
- auto get() const noexcept -> typename std::unique_ptr< T >::pointer -> auto
- Pointer to the heap value.
- auto operator*() const -> T & -> auto
- Derefernce to the owned type.
- auto operator->() const noexcept -> typename std::unique_ptr< T >::pointer -> auto
- Derefernce to the owned type.
Friends
-
void swap(owning_
ptr<T>& lhs, owning_ ptr<T>& rhs) - Swap function.
-
auto operator==(const owning_
ptr<T>& lhs, const owning_ ptr<T>& rhs) -> bool -> auto - Equality comparison.
-
auto operator!=(const owning_
ptr<T>& lhs, const owning_ ptr<T>& rhs) -> bool -> auto - Unequality comparison.
-
auto operator<<(std::ostream& os,
const owning_
ptr<T>& self) -> std::ostream & -> auto - Print operator.
Function documentation
template<typename T>
minilua:: owning_ptr<T>:: owning_ptr()
Default constructor.
Only available if T
is default constructible.
template<typename T>
minilua:: owning_ptr<T>:: owning_ptr(T* value) explicit
Creates an owning_
Will throw std::invalid_argument
exception if the value is nullptr
.
template<typename T>
minilua:: owning_ptr<T>:: owning_ptr(const owning_ ptr<T>& other)
Copy constructor.
This will make a new heap allocation.
template<typename T>
minilua:: owning_ptr<T>:: owning_ptr(owning_ ptr<T>&& other)
Move constructor.
This will make a new heap allocation and move the old value instead of just moving the pointer. This is done so if you keep any pointers to the old value around they will still point to the (now modified) old value.
template<typename T>
auto minilua:: owning_ptr<T>:: operator=(const owning_ ptr<T>& other) -> owning_ptr< T > &
Copy assignment operator.
This will make a new heap allocation.
template<typename T>
auto minilua:: owning_ptr<T>:: operator*() const -> T &
Derefernce to the owned type.
Returns a reference to the owned type.
template<typename T>
auto minilua:: owning_ptr<T>:: operator->() const noexcept -> typename std::unique_ptr< T >::pointer
Derefernce to the owned type.
Returns a pointer to the owned type.
template<typename T>
auto operator<<(std::ostream& os,
const owning_ ptr<T>& self) -> std::ostream &
Print operator.
Can always be printed. If T
is not printable this will print the pointer value (as a void*
).