229 lines
6.3 KiB
C++
229 lines
6.3 KiB
C++
// =====================================================================================================================
|
|
// fennec, a free and open source game engine
|
|
// Copyright © 2025 Medusa Slockbower
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//2
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
// =====================================================================================================================
|
|
|
|
#ifndef FENNEC_MEMORY_POINTERS_H
|
|
#define FENNEC_MEMORY_POINTERS_H
|
|
|
|
#include <fennec/lang/type_traits.h>
|
|
#include <fennec/lang/utility.h>
|
|
|
|
namespace fennec
|
|
{
|
|
|
|
///
|
|
/// \brief Struct for wrapping C++ \f$delete\f$
|
|
/// \tparam TypeT The type of the buffer to be deleted
|
|
template<typename TypeT>
|
|
struct default_delete
|
|
{
|
|
///
|
|
/// \brief Default constructor
|
|
constexpr default_delete() noexcept = default;
|
|
|
|
///
|
|
/// \brief Conversion Constructor
|
|
/// \tparam ConvT of other deleter
|
|
template<class ConvT> requires requires { is_convertible<ConvT*, TypeT*>{}.value == true; }
|
|
constexpr default_delete(const default_delete<ConvT>&) noexcept {}
|
|
|
|
///
|
|
/// \brief Function Call Operator, calls \f$delete\f$ on \f$ptr\f$
|
|
/// \param ptr Memory resource to delete
|
|
constexpr void operator()(TypeT* ptr) const noexcept {
|
|
static_assert(not is_void_v<TypeT>, "cannot delete a pointer to an incomplete type");
|
|
static_assert(is_complete_v<TypeT>, "cannot delete a pointer to an incomplete type");
|
|
delete ptr;
|
|
}
|
|
};
|
|
|
|
///
|
|
/// \tparam TypeT The type of the buffer to be deleted
|
|
template<typename TypeT>
|
|
struct default_delete<TypeT[]>
|
|
{
|
|
///
|
|
/// \brief Default constructor
|
|
constexpr default_delete() noexcept = default;
|
|
|
|
///
|
|
/// \brief Conversion Constructor
|
|
/// \tparam ConvT of other deleter
|
|
template<class ConvT> requires requires { is_convertible_v<ConvT(*)[], TypeT(*)[]> == true; }
|
|
constexpr default_delete(const default_delete<ConvT(*)[]>&) noexcept {}
|
|
|
|
///
|
|
/// \brief Function Call Operator, calls \f$delete\f$ on \f$ptr\f$
|
|
/// \param ptr Memory resource to delete
|
|
template<class ArrT> requires requires { is_convertible_v<ArrT(*)[], TypeT(*)[]> == true; }
|
|
constexpr void operator()(TypeT* ptr) const noexcept {
|
|
static_assert(not is_void_v<TypeT>, "cannot delete a pointer to an incomplete type");
|
|
static_assert(is_complete_v<TypeT>, "cannot delete a pointer to an incomplete type");
|
|
delete[] ptr;
|
|
}
|
|
};
|
|
|
|
|
|
|
|
///
|
|
/// \brief
|
|
/// \tparam TypeT
|
|
/// \tparam DeleteT
|
|
template<typename TypeT, class DeleteT = default_delete<TypeT>>
|
|
class unique_ptr
|
|
{
|
|
public:
|
|
/// \brief the element type
|
|
using element_t = TypeT;
|
|
|
|
/// \brief pointer to element type
|
|
using pointer_t = element_t*;
|
|
|
|
/// \brief pointer to element type
|
|
using const_pointer_t = const element_t*;
|
|
|
|
/// \brief the deleter
|
|
using delete_t = DeleteT;
|
|
|
|
///
|
|
/// \brief Default Constructor, creates a unique_ptr that owns nothing.
|
|
constexpr unique_ptr() : unique_ptr(nullptr) {}
|
|
|
|
///
|
|
/// \brief Nullptr Constructor, creates a unique_ptr that owns nothing.
|
|
constexpr unique_ptr(nullptr_t) noexcept : unique_ptr(nullptr, delete_t()) {}
|
|
|
|
///
|
|
/// \brief Pointer Constructor, creates a unique_ptr that owns \f$ptr\f$ with deleter \f$del\f$
|
|
/// \param ptr The resource to own
|
|
/// \param del The deleter
|
|
explicit constexpr unique_ptr(pointer_t ptr, const delete_t& del = delete_t())
|
|
: _delete(del)
|
|
, _handle(ptr) {
|
|
}
|
|
|
|
///
|
|
/// \brief Move Constructor, transfers ownership from \f$other\f$
|
|
/// \param other The unique_ptr to take ownership from
|
|
constexpr unique_ptr(unique_ptr&& other)
|
|
: _handle(other._handle) {
|
|
other._handle = nullptr;
|
|
}
|
|
|
|
// Delete copy constructor
|
|
constexpr unique_ptr(const unique_ptr&) = delete;
|
|
|
|
///
|
|
/// \brief Default Constructor, if it owns a resource, it deletes it using `delete_t`
|
|
constexpr ~unique_ptr() {
|
|
reset();
|
|
}
|
|
|
|
constexpr unique_ptr& operator=(const unique_ptr&) = delete;
|
|
|
|
///
|
|
/// \brief move constructor
|
|
/// \param r the pointer to take ownership of
|
|
/// \returns a reference to self
|
|
constexpr unique_ptr& operator=(unique_ptr&& r) noexcept {
|
|
_delete = r._delete;
|
|
_handle = r._handle;
|
|
r._handle = nullptr;
|
|
return *this;
|
|
}
|
|
|
|
///
|
|
/// \brief reset the pointer, destroying the held object
|
|
/// \param ptr the new pointer to own
|
|
void reset(pointer_t ptr) {
|
|
if(_handle) {
|
|
_delete(_handle);
|
|
}
|
|
_handle = ptr;
|
|
}
|
|
|
|
///
|
|
/// \brief reset the pointer, destroying the held object
|
|
void reset(nullptr_t = nullptr) {
|
|
if(_handle) {
|
|
_delete(_handle);
|
|
_handle = nullptr;
|
|
}
|
|
}
|
|
|
|
///
|
|
/// \brief releases the held pointer, returning it
|
|
/// \returns the released pointer
|
|
pointer_t release() {
|
|
pointer_t retval = _handle;
|
|
_handle = nullptr;
|
|
return retval;
|
|
}
|
|
|
|
///
|
|
/// \returns a reference to the held pointer
|
|
pointer_t get() const {
|
|
return _handle;
|
|
}
|
|
|
|
///
|
|
/// \returns \f$true\f$ if there is not a held pointer, \f$false\f$ otherwise
|
|
bool empty() {
|
|
return _handle == nullptr;
|
|
}
|
|
|
|
///
|
|
/// \returns the held object for access
|
|
pointer_t operator->() {
|
|
return _handle;
|
|
}
|
|
|
|
///
|
|
/// \brief access operator
|
|
/// \returns the held object for access
|
|
const_pointer_t operator->() const {
|
|
return _handle;
|
|
}
|
|
|
|
///
|
|
/// \brief implicit boolean conversion
|
|
/// \returns \f$true\f$ if there is a held pointer, \f$false\f$ otherwise
|
|
operator bool() const {
|
|
return _handle != nullptr;
|
|
}
|
|
|
|
|
|
private:
|
|
delete_t _delete;
|
|
pointer_t _handle;
|
|
};
|
|
|
|
///
|
|
/// \brief Creates a unique pointer holding an object of type \f$TypeT\f$
|
|
/// \tparam TypeT The type
|
|
/// \tparam ArgsT The constructor arguments, automatically deduced
|
|
/// \param args The constructor arguments
|
|
/// \returns A unique pointer holding a heap allocated object of type \f$TypeT\f$ constructed with arguments \f$args\f$
|
|
template<typename TypeT, typename...ArgsT>
|
|
unique_ptr<TypeT> make_unique(ArgsT&&...args) {
|
|
return unique_ptr<TypeT>(new TypeT(fennec::forward<ArgsT>(args)...));
|
|
}
|
|
|
|
}
|
|
|
|
#endif // FENNEC_MEMORY_POINTERS_H
|