Files
fennec/include/fennec/memory/pointers.h
Medusa Slockbower 7c2f89b331 - A few Vulkan wrapper structs
- Framework for Vulkan context
 - Fixed a bug with dynarray where if `resize()` shrinks the array, destructors are not called.
 - Fixed grammar issues with the containers library and added property tables to existing data structures.
2026-01-02 15:38:03 -05:00

246 lines
6.7 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) {
}
///
/// \param other The unique_ptr to take ownership from
constexpr unique_ptr(unique_ptr&& other)
: _handle(other._handle) {
other._handle = nullptr;
}
///
/// \brief Move Constructor, transfers ownership from \f$other\f$
/// \param other The unique_ptr to take ownership from
template<typename DerivedT> requires(is_base_of_v<TypeT, DerivedT>)
constexpr unique_ptr(unique_ptr<DerivedT>&& other)
: _handle(other.release()) {
}
// 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;
fennec::swap(_handle, r._handle);
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 is_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;
}
///
/// \brief dereference operator
/// \returns a reference to the held value
element_t& operator*() const {
return *_handle;
}
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)...));
}
template<typename TypeT>
unique_ptr<TypeT> make_unique(TypeT* ptr) {
return unique_ptr<TypeT>(ptr);
}
}
#endif // FENNEC_MEMORY_POINTERS_H