Ark Server API (ASE) - Wiki
Loading...
Searching...
No Matches
Timer.h
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <chrono>
5
6#include "API/Base.h"
7
8namespace API
9{
10 class Timer
11 {
12 public:
13 ARK_API static Timer& Get();
14
15 Timer(const Timer&) = delete;
16 Timer(Timer&&) = delete;
17 Timer& operator=(const Timer&) = delete;
18 Timer& operator=(Timer&&) = delete;
19
20 /**
21 * \brief Executes function after X seconds
22 * \tparam Func Callback function type
23 * \tparam Args Callback arguments types
24 * \param callback Callback function
25 * \param delay Delay in seconds
26 * \param args Callback arguments
27 */
28 template <typename Func, typename... Args>
29 void DelayExecute(const Func& callback, int delay, Args&&... args)
30 {
32 }
33
34 /**
35 * \brief Executes function every X seconds
36 * \tparam Func Callback function type
37 * \tparam Args Callback arguments types
38 * \param callback Callback function
39 * \param execution_interval Delay between executions in seconds
40 * \param execution_counter Amount of times to execute function, -1 for unlimited
41 * \param async If true, function will be executed in the new thread
42 * \param args Callback arguments
43 */
44 template <typename Func, typename... Args>
45 void RecurringExecute(const Func& callback, int execution_interval,
46 int execution_counter, bool async, Args&&... args)
47 {
50 }
51
52 private:
53 struct TimerFunc
54 {
55 TimerFunc(const std::chrono::time_point<std::chrono::system_clock>& next_time,
56 std::function<void()> callback,
57 bool exec_once, int execution_counter, int execution_interval)
58 : next_time(next_time),
59 callback(move(callback)),
60 exec_once(exec_once),
61 execution_counter(execution_counter),
62 execution_interval(execution_interval)
63 {
64 }
65
66 std::chrono::time_point<std::chrono::system_clock> next_time;
67 std::function<void()> callback;
71 };
72
73 Timer();
74 ~Timer();
75
76 ARK_API void DelayExecuteInternal(const std::function<void()>& callback, int delay_seconds);
77 ARK_API void RecurringExecuteInternal(const std::function<void()>& callback, int execution_interval,
78 int execution_counter, bool async);
79
80 void Update();
81
82 std::vector<std::unique_ptr<TimerFunc>> timer_funcs_;
83 };
84} // namespace API
#define ARK_API
Definition Base.h:9
virtual std::unique_ptr< ArkApi::ICommands > & GetCommands()=0
void RecurringExecute(const Func &callback, int execution_interval, int execution_counter, bool async, Args &&... args)
Executes function every X seconds.
Definition Timer.h:45
Timer(const Timer &)=delete
ARK_API void RecurringExecuteInternal(const std::function< void()> &callback, int execution_interval, int execution_counter, bool async)
Definition Timer.cpp:31
Timer & operator=(Timer &&)=delete
void Update()
Definition Timer.cpp:62
ARK_API void DelayExecuteInternal(const std::function< void()> &callback, int delay_seconds)
Definition Timer.cpp:23
static ARK_API Timer & Get()
Definition Timer.cpp:17
void DelayExecute(const Func &callback, int delay, Args &&... args)
Executes function after X seconds.
Definition Timer.h:29
Timer(Timer &&)=delete
std::vector< std::unique_ptr< TimerFunc > > timer_funcs_
Definition Timer.h:82
Timer & operator=(const Timer &)=delete
virtual bool RemoveOnTimerCallback(const FString &id)=0
Removes an on-timer callback.
virtual void AddOnTimerCallback(const FString &id, const std::function< void()> &callback)=0
Added function will be called every second.
Definition IBaseApi.h:9
std::unique_ptr< IBaseApi > game_api
Definition IBaseApi.h:25
Definition json.hpp:4518
TimerFunc(const std::chrono::time_point< std::chrono::system_clock > &next_time, std::function< void()> callback, bool exec_once, int execution_counter, int execution_interval)
Definition Timer.h:55
std::function< void()> callback
Definition Timer.h:67
std::chrono::time_point< std::chrono::system_clock > next_time
Definition Timer.h:66