// =====================================================================================================================
//  fennec, a free and open source game engine
//  Copyright © 2025 - 2026  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++ \emph{delete}
/// \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 \emph{delete} on \emph{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(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 \emph{delete} on \emph{ptr}
	/// \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 {

// Definitions =========================================================================================================
public:

	/// \name Definitions
	/// @{

	/// \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;

	/// @}


// Constructors ========================================================================================================
public:

	/// \name Constructors & Destructor
	/// @{

	///
	/// \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 \emph{ptr} with deleter \emph{del}
	/// \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 \emph{other}
	/// \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()) {
	}

	///
	/// \brief Copy Constructor
	/// \details Deleted
	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();
	}

	/// @}


// Assignment ==========================================================================================================
public:

	/// \name Assignment
	/// @{

	///
	/// \brief move constructor
	/// \param r the pointer to take ownership of
	/// \returns a reference to \emph{this}
	constexpr unique_ptr& operator=(unique_ptr&& r) noexcept {
		_delete = r._delete;
		fennec::swap(_handle, r._handle);
		return *this;
	}

	///
	/// \brief Copy Assignment
	/// \details Deleted
	/// \returns Deleted
	constexpr unique_ptr& operator=(const unique_ptr&) = delete;

	/// @}


// Properties ==========================================================================================================
public:

	/// \name Properties
	/// @{

	///
	/// \returns \emph{true} if there is not a held pointer, \emph{false} otherwise
	bool is_empty() {
		return _handle == nullptr;
	}

	///
	/// \brief implicit boolean conversion
	/// \returns \emph{true} if there is a held pointer, \emph{false} otherwise
	operator bool() const {
		return _handle != nullptr;
	}

	/// @}


// Access ==============================================================================================================
public:

	/// \name Access
	/// @{

	///
	/// \returns a reference to the held pointer
	pointer_t get() const {
		return _handle;
	}

	///
	/// \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 dereference operator
	/// \returns a reference to the held value
	element_t& operator*() const {
		return *_handle;
	}

	/// @}


// Modifiers ===========================================================================================================
public:

	/// \name Modifiers
	/// @{

	///
	/// \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;
	}

	/// @}

private:
	delete_t  _delete;
	pointer_t _handle;
};

///
/// \brief Creates a unique pointer holding an object of type \emph{TypeT}
/// \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 \emph{TypeT} constructed with arguments \emph{args}
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
