From 5cec531dc3b892c89af084b6dc18d5d77f9eea48 Mon Sep 17 00:00:00 2001 From: Angad <66992519+ThirdEyeSqueegee@users.noreply.github.com> Date: Wed, 25 Oct 2023 10:00:08 -0700 Subject: [PATCH] feat: make helpers generic for std::chrono::duration helpers --- CommonLibSF/include/SFSE/Utilities.h | 34 ++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/CommonLibSF/include/SFSE/Utilities.h b/CommonLibSF/include/SFSE/Utilities.h index 034b2b75..43d8bb26 100644 --- a/CommonLibSF/include/SFSE/Utilities.h +++ b/CommonLibSF/include/SFSE/Utilities.h @@ -8,8 +8,8 @@ namespace SFSE::stl template constexpr void write_thunk_call(const std::uintptr_t a_address) noexcept { - SFSE::AllocTrampoline(14); - auto& trampoline = SFSE::GetTrampoline(); + AllocTrampoline(14); + auto& trampoline = GetTrampoline(); T::func = trampoline.write_call(a_address, T::thunk); } @@ -35,24 +35,38 @@ namespace SFSE::stl template constexpr void write_thunk_jump(const std::uintptr_t a_src) noexcept { - SFSE::AllocTrampoline(14); - auto& trampoline = SFSE::GetTrampoline(); + AllocTrampoline(14); + auto& trampoline = GetTrampoline(); T::func = trampoline.write_branch(a_src, T::thunk); } - void add_thread_task(std::function a_fn, std::chrono::milliseconds a_wait_for_ms = 0ms) noexcept + namespace detail + { + template + struct is_chrono_duration : std::false_type + {}; + + template + struct is_chrono_duration> : std::true_type + {}; + + template + concept is_duration = is_chrono_duration::value; + } + + void add_thread_task(const std::function a_fn, detail::is_duration auto a_wait_for = 0ms) noexcept { std::jthread([=] { - std::this_thread::sleep_for(a_wait_for_ms); - SFSE::GetTaskInterface()->AddTask(a_fn); + std::this_thread::sleep_for(a_wait_for); + GetTaskInterface()->AddTask(a_fn); }).detach(); } - void add_thread_task_permanent(std::function a_fn, std::chrono::milliseconds a_wait_for_ms = 0ms) noexcept + void add_thread_task_permanent(const std::function a_fn, detail::is_duration auto a_wait_for = 0ms) noexcept { std::jthread([=] { - std::this_thread::sleep_for(a_wait_for_ms); - SFSE::GetTaskInterface()->AddPermanentTask(a_fn); + std::this_thread::sleep_for(a_wait_for); + GetTaskInterface()->AddPermanentTask(a_fn); }).detach(); } }