Skip to content

Commit

Permalink
feat: make helpers generic for std::chrono::duration helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ThirdEyeSqueegee committed Oct 25, 2023
1 parent b31e33c commit 5cec531
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions CommonLibSF/include/SFSE/Utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace SFSE::stl
template <class T, std::size_t Size = 5>
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<Size>(a_address, T::thunk);
}

Expand All @@ -35,24 +35,38 @@ namespace SFSE::stl
template <class T, std::size_t Size = 5>
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<Size>(a_src, T::thunk);
}

void add_thread_task(std::function<void()> a_fn, std::chrono::milliseconds a_wait_for_ms = 0ms) noexcept
namespace detail
{
template <class T>
struct is_chrono_duration : std::false_type
{};

template <class Rep, class Duration>
struct is_chrono_duration<std::chrono::duration<Rep, Duration>> : std::true_type
{};

template <typename T>
concept is_duration = is_chrono_duration<T>::value;
}

void add_thread_task(const std::function<void()> 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<void()> a_fn, std::chrono::milliseconds a_wait_for_ms = 0ms) noexcept
void add_thread_task_permanent(const std::function<void()> 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();
}
}

0 comments on commit 5cec531

Please sign in to comment.