// =====================================================================================================================
//  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/>.
// =====================================================================================================================

///
/// \file fennec/memory/new.h
/// \brief This header contains functions related to analyzing, modifying or copying buffers interpreted as bytes
///
///
/// \details
/// \author Medusa Slockbower
///
/// \copyright Copyright © 2025 - 2026 Medusa Slockbower ([GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html))
///
///


#ifndef FENNEC_MEMORY_NEW_H
#define FENNEC_MEMORY_NEW_H

#include <fennec/lang/types.h>
#include <fennec/lang/utility.h>

namespace fennec
{

///
/// \brief Alignment class to handle conflicting definitions
enum class align_t : size_t;

///
/// \brief Type to handle explicit nothrow definitions
struct nothrow_t
{
	explicit nothrow_t() noexcept { }
};

///
/// \returns the page size for the current environment
size_t pagesize();

///
/// \brief Default construct the object of type \emph{TypeT} at \emph{ptr}
/// \tparam TypeT the type to construct
/// \param ptr the pointer to the object to construct
template<typename TypeT> void construct(TypeT* ptr) {
	new(ptr) TypeT();
}

///
/// \brief Copy construct the object of type \emph{TypeT} at \emph{ptr}
/// \tparam TypeT the type to construct
/// \param ptr the pointer to the object to construct
/// \param val the value to copy
template<typename TypeT> void construct(TypeT* ptr, const TypeT& val) {
	new(ptr) TypeT(val);
}

///
/// \brief Move construct the object of type \emph{TypeT} at \emph{ptr}
/// \tparam TypeT the type to construct
/// \param ptr the pointer to the object to construct
/// \param val the value to take ownership of
template<typename TypeT> void construct(TypeT* ptr, TypeT&& val) {
	new(ptr) TypeT(fennec::forward<TypeT>(val));
}

///
/// \brief Variadic construct the object of type \emph{TypeT} at \emph{ptr}
/// \tparam TypeT the type to construct
/// \tparam ArgsT the argument types
/// \param ptr the pointer to the object to construct
/// \param args the argument values
template<typename TypeT, typename...ArgsT> void construct(TypeT* ptr, ArgsT&&...args) {
	new(ptr) TypeT(fennec::forward<ArgsT>(args)...);
}

///
/// \brief Destruct the object of type \emph{TypeT} at \emph{ptr}
/// \tparam TypeT the type to destruct
/// \param ptr the pointer to the object to destruct
template<typename TypeT> void destruct(TypeT* ptr) {
	ptr->~TypeT();
}

}

#ifndef FENNEC_DOXYGEN
void* operator new  (fennec::size_t size);
void* operator new[](fennec::size_t size);
void* operator new  (fennec::size_t size, const fennec::nothrow_t&);
void* operator new[](fennec::size_t size, const fennec::nothrow_t&);
void* operator new  (fennec::size_t size, fennec::align_t align);
void* operator new[](fennec::size_t size, fennec::align_t align);
void* operator new  (fennec::size_t size, fennec::align_t align, const fennec::nothrow_t&);
void* operator new[](fennec::size_t size, fennec::align_t align, const fennec::nothrow_t&);


void operator delete  (void* ptr) noexcept;
void operator delete[](void* ptr) noexcept;
void operator delete  (void* ptr, fennec::size_t) noexcept;
void operator delete[](void* ptr, fennec::size_t) noexcept;
void operator delete  (void* ptr, const fennec::nothrow_t&) noexcept;
void operator delete[](void* ptr, const fennec::nothrow_t&) noexcept;
void operator delete  (void* ptr, fennec::size_t, const fennec::nothrow_t&) noexcept;
void operator delete[](void* ptr, fennec::size_t, const fennec::nothrow_t&) noexcept;

void operator delete  (void* ptr, fennec::align_t) noexcept;
void operator delete[](void* ptr, fennec::align_t) noexcept;
void operator delete  (void* ptr, fennec::size_t, fennec::align_t) noexcept;
void operator delete[](void* ptr, fennec::size_t, fennec::align_t) noexcept;
void operator delete  (void* ptr, fennec::align_t, const fennec::nothrow_t&) noexcept;
void operator delete[](void* ptr, fennec::align_t, const fennec::nothrow_t&) noexcept;

template<typename TypeT> constexpr void* operator new  (fennec::size_t, TypeT* ptr) { return ptr; }
template<typename TypeT> constexpr void* operator new[](fennec::size_t, TypeT* ptr) { return ptr; }
#endif


#endif // FENNEC_MEMORY_NEW_H
