Implement use of Inventory API
This commit is contained in:
+20
-10
@@ -2,7 +2,9 @@
|
||||
#define UTIL_TIMER_H
|
||||
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include "Helpers.h"
|
||||
|
||||
@@ -30,18 +32,19 @@ public:
|
||||
std::string Identifier() const;
|
||||
|
||||
template <typename ...FunctionArguments, typename ...Arguments>
|
||||
void StartSingle(int interval, std::function<void(FunctionArguments...)> const & function, Arguments && ...arguments)
|
||||
void StartSingle(int milliSeconds, std::function<void(FunctionArguments...)> const & function, Arguments && ...arguments)
|
||||
{
|
||||
Start(TimerType::Single, interval, function, std::forward<Arguments>(arguments)...);
|
||||
Start(TimerType::Single, milliSeconds, function, std::forward<Arguments>(arguments)...);
|
||||
}
|
||||
|
||||
template <typename ...FunctionArguments, typename ...Arguments>
|
||||
void StartContinuous(int interval, std::function<void(FunctionArguments...)> const & function, Arguments && ...arguments)
|
||||
void StartContinuous(int milliSeconds, std::function<void(FunctionArguments...)> const & function, Arguments && ...arguments)
|
||||
{
|
||||
Start(TimerType::Continuous, interval, function, std::forward<Arguments>(arguments)...);
|
||||
Start(TimerType::Continuous, milliSeconds, function, std::forward<Arguments>(arguments)...);
|
||||
}
|
||||
|
||||
void Stop();
|
||||
void Abort();
|
||||
bool IsRunning() const noexcept;
|
||||
void Wait();
|
||||
|
||||
@@ -59,7 +62,7 @@ private:
|
||||
|
||||
private:
|
||||
template <typename ...FunctionArguments, typename ...Arguments>
|
||||
void Start(TimerType::type type, int interval, std::function<void(FunctionArguments...)> const & function, Arguments && ...arguments)
|
||||
void Start(TimerType::type type, int milliSeconds, std::function<void(FunctionArguments...)> const & function, Arguments && ...arguments)
|
||||
{
|
||||
if (type == TimerType::Unknown)
|
||||
return;
|
||||
@@ -67,17 +70,21 @@ private:
|
||||
if (m_run.load(std::memory_order_acquire))
|
||||
Stop();
|
||||
|
||||
m_thread = std::thread([this, type, interval, function, &arguments...]()
|
||||
m_thread = std::thread([this, type, milliSeconds, function, &arguments...]()
|
||||
{
|
||||
auto argumentsCopy = std::make_tuple(std::forward<Arguments>(arguments)...);
|
||||
m_run.store(true, std::memory_order_release);
|
||||
m_aborted = false;
|
||||
|
||||
while (m_run.load(std::memory_order_acquire))
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
|
||||
Helpers::execute(function, argumentsCopy);
|
||||
if (type == TimerType::Single)
|
||||
m_run.store(false, std::memory_order_release);
|
||||
std::unique_lock<std::mutex> lock(m_mutex);
|
||||
if (!m_conditionVariable.wait_for(lock, std::chrono::milliseconds(milliSeconds), [&]{return m_aborted;}))
|
||||
{
|
||||
Helpers::execute(function, argumentsCopy);
|
||||
if (type == TimerType::Single)
|
||||
m_run.store(false, std::memory_order_release);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -88,6 +95,9 @@ private:
|
||||
|
||||
private:
|
||||
std::atomic<bool> m_run;
|
||||
bool m_aborted;
|
||||
std::mutex m_mutex;
|
||||
std::condition_variable m_conditionVariable;
|
||||
std::string m_identifier;
|
||||
std::thread m_thread;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user