diff --git a/Bluetooth/Device.cpp b/Bluetooth/Device.cpp index f1a45c5..9bcba91 100644 --- a/Bluetooth/Device.cpp +++ b/Bluetooth/Device.cpp @@ -21,7 +21,7 @@ Device::~Device() void Device::Start() { - m_timer.Start(10000, std::bind(&Device::UpdatePresentDevices, this)); + m_timer.Start(10000, static_cast>(std::bind(&Device::UpdatePresentDevices, this))); } void Device::Stop() diff --git a/UniFi/Device.cpp b/UniFi/Device.cpp index 7e89bf5..e7532d5 100644 --- a/UniFi/Device.cpp +++ b/UniFi/Device.cpp @@ -27,7 +27,7 @@ Device::~Device() void Device::Start() { - m_timer.Start(5000, std::bind(&Device::UpdatePresentDevices, this)); + m_timer.Start(5000, static_cast>(std::bind(&Device::UpdatePresentDevices, this))); } void Device::Stop() diff --git a/Util/Timer.cpp b/Util/Timer.cpp index e3e39b8..06e81e1 100644 --- a/Util/Timer.cpp +++ b/Util/Timer.cpp @@ -1,4 +1,5 @@ #include +#include "Util.h" #include "Timer.h" @@ -6,29 +7,24 @@ namespace PresenceDetection { namespace Util { Timer::Timer() : - m_run(false) + m_run(false), + m_identifier(CreateRandomString(10)) { } Timer::~Timer() { - Stop(); + Wait(); } -void Timer::Start(int interval, std::function function) +bool Timer::operator==(const Timer& other) const { - if (m_run.load(std::memory_order_acquire)) - Stop(); + return m_identifier == other.Identifier(); +} - m_run.store(true, std::memory_order_release); - m_thread = std::thread([this, interval, function]() - { - while (m_run.load(std::memory_order_acquire)) - { - function(); - std::this_thread::sleep_for(std::chrono::milliseconds(interval)); - } - }); +std::string Timer::Identifier() const +{ + return m_identifier; } void Timer::Stop() diff --git a/Util/Timer.h b/Util/Timer.h index 8f010d7..31666a8 100644 --- a/Util/Timer.h +++ b/Util/Timer.h @@ -1,6 +1,8 @@ #ifndef UTIL_TIMER_H #define UTIL_TIMER_H +#include + #include #include #include @@ -15,13 +17,35 @@ public: Timer(); ~Timer(); - void Start(int interval, std::function func); + bool operator==(const Timer& other) const; + +public: + std::string Identifier() const; + + template + void Start(int interval, std::function 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)...); + std::this_thread::sleep_for(std::chrono::milliseconds(interval)); + } + }); + } + void Stop(); bool IsRunning() const noexcept; void Wait(); private: std::atomic m_run; + std::string m_identifier; std::thread m_thread; }; diff --git a/Util/Util.cpp b/Util/Util.cpp new file mode 100644 index 0000000..21f669a --- /dev/null +++ b/Util/Util.cpp @@ -0,0 +1,25 @@ +#include +#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 diff --git a/Util/Util.h b/Util/Util.h new file mode 100644 index 0000000..3c26d86 --- /dev/null +++ b/Util/Util.h @@ -0,0 +1,15 @@ +#ifndef UTIL_UTIL_H +#define UTIL_UTIL_H + +#include + + +namespace PresenceDetection { +namespace Util { + +std::string CreateRandomString(size_t length); + +} // namespace Util +} // namespace PresenceDetection + +#endif // UTIL_UTIL_H