Update Timer class to accept functions with any number of parameters

This commit is contained in:
2019-01-05 13:27:47 +01:00
parent 7ad040c6c1
commit 4c66aa0831
6 changed files with 77 additions and 17 deletions
+1 -1
View File
@@ -21,7 +21,7 @@ Device::~Device()
void Device::Start() void Device::Start()
{ {
m_timer.Start(10000, std::bind(&Device::UpdatePresentDevices, this)); m_timer.Start(10000, static_cast<std::function<void()>>(std::bind(&Device::UpdatePresentDevices, this)));
} }
void Device::Stop() void Device::Stop()
+1 -1
View File
@@ -27,7 +27,7 @@ Device::~Device()
void Device::Start() void Device::Start()
{ {
m_timer.Start(5000, std::bind(&Device::UpdatePresentDevices, this)); m_timer.Start(5000, static_cast<std::function<void()>>(std::bind(&Device::UpdatePresentDevices, this)));
} }
void Device::Stop() void Device::Stop()
+10 -14
View File
@@ -1,4 +1,5 @@
#include <chrono> #include <chrono>
#include "Util.h"
#include "Timer.h" #include "Timer.h"
@@ -6,29 +7,24 @@ namespace PresenceDetection {
namespace Util { namespace Util {
Timer::Timer() : Timer::Timer() :
m_run(false) m_run(false),
m_identifier(CreateRandomString(10))
{ {
} }
Timer::~Timer() Timer::~Timer()
{ {
Stop(); Wait();
} }
void Timer::Start(int interval, std::function<void(void)> function) bool Timer::operator==(const Timer& other) const
{ {
if (m_run.load(std::memory_order_acquire)) return m_identifier == other.Identifier();
Stop(); }
m_run.store(true, std::memory_order_release); std::string Timer::Identifier() const
m_thread = std::thread([this, interval, function]() {
{ return m_identifier;
while (m_run.load(std::memory_order_acquire))
{
function();
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
}
});
} }
void Timer::Stop() void Timer::Stop()
+25 -1
View File
@@ -1,6 +1,8 @@
#ifndef UTIL_TIMER_H #ifndef UTIL_TIMER_H
#define UTIL_TIMER_H #define UTIL_TIMER_H
#include <iostream>
#include <atomic> #include <atomic>
#include <functional> #include <functional>
#include <thread> #include <thread>
@@ -15,13 +17,35 @@ public:
Timer(); Timer();
~Timer(); ~Timer();
void Start(int interval, std::function<void(void)> func); bool operator==(const Timer& other) const;
public:
std::string Identifier() const;
template <typename ...FunctionArguments, typename ...Arguments>
void Start(int interval, std::function<void(FunctionArguments...)> const & function, Arguments && ...arguments)
{
if (m_run.load(std::memory_order_acquire))
Stop();
m_run.store(true, std::memory_order_release);
m_thread = std::thread([this, interval, function, arguments ...]()
{
while (m_run.load(std::memory_order_acquire))
{
function(std::forward<Arguments>(arguments)...);
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
}
});
}
void Stop(); void Stop();
bool IsRunning() const noexcept; bool IsRunning() const noexcept;
void Wait(); void Wait();
private: private:
std::atomic<bool> m_run; std::atomic<bool> m_run;
std::string m_identifier;
std::thread m_thread; std::thread m_thread;
}; };
+25
View File
@@ -0,0 +1,25 @@
#include <algorithm>
#include "Util.h"
namespace PresenceDetection {
namespace Util {
std::string CreateRandomString(size_t length)
{
auto randchar = []() -> char
{
const char charset[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
const size_t max_index = (sizeof(charset) - 1);
return charset[ rand() % max_index ];
};
std::string str(length,0);
std::generate_n(str.begin(), length, randchar);
return str;
}
} // namespace Util
} // namespace PresenceDetection
+15
View File
@@ -0,0 +1,15 @@
#ifndef UTIL_UTIL_H
#define UTIL_UTIL_H
#include <string>
namespace PresenceDetection {
namespace Util {
std::string CreateRandomString(size_t length);
} // namespace Util
} // namespace PresenceDetection
#endif // UTIL_UTIL_H