Added Logo
This commit is contained in:
@@ -19,19 +19,105 @@
|
||||
#ifndef FENNEC_LANG_POINTERS_H
|
||||
#define FENNEC_LANG_POINTERS_H
|
||||
|
||||
#include <fennec/lang/type_traits.h>
|
||||
|
||||
template<typename TypeT, class DeleteT = >
|
||||
namespace fennec
|
||||
{
|
||||
|
||||
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 { can_convert<ConvT*, TypeT*>{}.value == true; }
|
||||
constexpr default_delete(const default_delete<ConvT>&) noexcept {}
|
||||
|
||||
///
|
||||
/// \brief Function Call Operator, calls `delete` on `ptr`
|
||||
/// \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(not sizeof(TypeT) > 0, "cannot delete a pointer to an incomplete type");
|
||||
delete ptr;
|
||||
}
|
||||
};
|
||||
|
||||
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 { can_convert<ConvT(*)[], TypeT(*)[]>{}.value == true; }
|
||||
constexpr default_delete(const default_delete<ConvT(*)[]>&) noexcept {}
|
||||
|
||||
///
|
||||
/// \brief Function Call Operator, calls `delete` on `ptr`
|
||||
/// \param ptr Memory resource to delete
|
||||
template<class ArrT> requires requires { can_convert<ArrT(*)[], TypeT(*)[]>{}.value == 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(not sizeof(TypeT) > 0, "cannot delete a pointer to an incomplete type");
|
||||
delete[] ptr;
|
||||
}
|
||||
};
|
||||
|
||||
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 the deleter
|
||||
using delete_t = DeleteT;
|
||||
|
||||
///
|
||||
/// \brief Default Constructor, creates a unique_ptr that owns nothing.
|
||||
constexpr unique_ptr() : unique_ptr(nullptr) {}
|
||||
constexpr unique_ptr(pointer_t ptr) : _handle(ptr) {}
|
||||
|
||||
///
|
||||
/// \brief Nullptr Constructor, creates a unique_ptr that owns nothing.
|
||||
constexpr unique_ptr(nullptr_t) noexcept : unique_ptr(nullptr) {}
|
||||
|
||||
///
|
||||
/// \brief Pointer Constructor, creates a unique_ptr that owns `ptr` with deleter `del`
|
||||
/// \param ptr The resource to own
|
||||
/// \param del The deleter
|
||||
explicit constexpr unique_ptr(pointer_t ptr, const delete_t& del) : _delete(del), _handle(ptr) {}
|
||||
|
||||
///
|
||||
/// \brief Pointer Constructor, creates a unique_ptr that owns `ptr` with deleter `del`
|
||||
/// \param ptr The resource to own
|
||||
/// \param del The deleter
|
||||
explicit constexpr unique_ptr(pointer_t ptr, delete_t&& del) : _delete(del), _handle(ptr) {}
|
||||
|
||||
///
|
||||
/// \brief Move Constructor, transfers ownership from `other`
|
||||
/// \param other The unique_ptr to take ownership from
|
||||
constexpr unique_ptr(unique_ptr&& other) : _handle(other._handle) { other._handle = nullptr; }
|
||||
|
||||
constexpr ~unique_ptr() { if(_handle) ::operator delete(_handle); }
|
||||
// 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() { if(_handle) _delete(_handle); }
|
||||
|
||||
|
||||
constexpr unique_ptr& operator=(unique_ptr&& r) noexcept
|
||||
{ _handle = r._handle; r._handle = nullptr; return *this; }
|
||||
@@ -40,7 +126,10 @@ public:
|
||||
|
||||
|
||||
private:
|
||||
delete_t _delete;
|
||||
pointer_t _handle;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // FENNEC_LANG_POINTERS_H
|
||||
|
||||
Reference in New Issue
Block a user