// =====================================================================================================================
//  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.
//
//  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_THREADING_DETAIL_THREAD_H
#define FENNEC_THREADING_DETAIL_THREAD_H

#include <fennec/containers/tuple.h>
#include <fennec/lang/function.h>
#include <fennec/memory/pointers.h>

namespace fennec::detail
{

template<typename FuncT, typename...ArgsT> using thread_proxy_t = tuple<FuncT, ArgsT...>;

template<typename FuncT, typename...ArgsT, size_t...I>
void _execute_thread(thread_proxy_t<FuncT, ArgsT...>& data, index_metasequence<I...>) {
	auto func = fennec::move(fennec::get<0>(data));
	func(fennec::move(fennec::get<I + 1>(data)) ...);
}

template<typename DataT>
void* _run_thread(void* data) {
	unique_ptr<DataT> ptr(static_cast<DataT*>(data));
	detail::_execute_thread(*ptr.get(), make_index_metasequence_t<DataT::size - 1>{});
	return nullptr;
}

}

#endif // FENNEC_THREADING_DETAIL_THREAD_H