// =====================================================================================================================
//  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_DETAIL_PTR_TRAITS_H
#define FENNEC_MEMORY_DETAIL_PTR_TRAITS_H

#include <fennec/lang/conditional_types.h>
#include <fennec/lang/types.h>
#include <fennec/lang/type_traits.h>
#include <fennec/lang/type_sequences.h>

namespace fennec
{

namespace detail
{

// helper to get the element type of the pointer class
template<typename ClassT, typename = void>
struct _ptr_get_element : first_element<ClassT> { }; // Default case, return the first template parameter

// overload for types that have a member `ClassT::element_t`
template<typename ClassT> requires requires { typename ClassT::element_t; }
struct _ptr_get_element<ClassT, ClassT> { using type = typename ClassT::element_t; };

// helper for generating `pointer_to`
template<typename PtrT, typename ElemT, bool_t = is_void_v<ElemT>>
struct _ptr_traits_ptr_to
{
	using pointer_t = PtrT;
	using element_t = ElemT;

	static constexpr pointer_t pointer_to(element_t& obj) {
		return pointer_t::pointer_to(obj);
	}
};

// overload for C style pointers
template<typename ElemT>
struct _ptr_traits_ptr_to<ElemT*, ElemT, false>
{
	using pointer_t = ElemT*;
	using element_t = ElemT;

	static constexpr pointer_t pointer_to(element_t& obj) {
		return addressof(obj);
	}
};

// underlying implementation for classes that behave like pointers
template<typename ClassT, typename ElemT, bool_t = is_void_v<ElemT>>
struct _ptr_traits_impl : _ptr_traits_ptr_to<ClassT, ElemT>
{
private:
	template<typename TypeT>
	using _diff_t = typename TypeT::diff_t; // Helper to prevent substitution issues with detecting the difference type

	template<typename BaseT, typename OElemT> // Helper to prevent substitution issues with detecting a defined rebind
	using _rebind_helper = type_identity<typename BaseT::template rebind<OElemT>>;

public:
	using pointer_t = ClassT;
	using element_t = _ptr_get_element<ClassT>;
	using diff_t    = _diff_t<ClassT>;

	template<class U> using rebind = detect_t<replace_first_element<ClassT, U>, _rebind_helper, ClassT, ElemT>;
};

// overload for an undefined type
template<typename ClassT> struct _ptr_traits_impl<ClassT, undefined_t> {};

// overload for void element
template<typename PtrT, typename ElemT> struct _ptr_traits_ptr_to<PtrT, ElemT, true> { };

}

}

#endif // FENNEC_MEMORY_DETAIL_PTR_TRAITS_H
