geode/loader/include/Geode/hook-core/Handler.hpp

43 lines
1.3 KiB
C++
Raw Normal View History

2022-07-30 12:24:03 -04:00
#pragma once
#include "../meta/meta.hpp"
namespace geode::core {
2022-10-30 14:59:20 -04:00
namespace impl {
/* the handler itself */
template <auto& Det, class Ret, class... Args>
Ret handler(Args... args) {
static thread_local int counter = 0;
if constexpr (std::is_same_v<Ret, void>) {
if (counter == (int)Det->size()) counter = 0;
Det->at(counter++)(args...);
if (--counter < 0) counter = Det->size() - 1;
}
else {
if (counter == (int)Det->size()) counter = 0;
Ret ret = Det->at(counter++)(args...);
if (--counter < 0) counter = Det->size() - 1;
return ret;
}
}
template <template <class, class...> class Conv, auto& Func, class Ret, class... Args>
Ret trampoline(Args... args) {
return meta::Function<Ret(Args...), Conv>(Func)(args...);
}
}
template <template <class, class...> class Conv, auto& Det, class Ret, class... Args>
2022-11-24 16:41:02 -05:00
static inline auto handler =
2022-10-30 14:59:20 -04:00
Conv<Ret, Args...>::template get_wrapper<&impl::handler<Det, Ret, Args...>>();
template <template <class, class...> class Conv, auto& Det, class Ret, class... Args>
static constexpr inline auto trampoline = &impl::trampoline<Conv, Det, Ret, Args...>;
2022-07-30 12:24:03 -04:00
}