Prechádzať zdrojové kódy

Update Timer class to accept functions with any number of parameters

JDierkse 7 rokov pred
rodič
commit
4c66aa0831
6 zmenil súbory, kde vykonal 78 pridanie a 18 odobranie
  1. 1 1
      Bluetooth/Device.cpp
  2. 1 1
      UniFi/Device.cpp
  3. 11 15
      Util/Timer.cpp
  4. 25 1
      Util/Timer.h
  5. 25 0
      Util/Util.cpp
  6. 15 0
      Util/Util.h

+ 1 - 1
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::function<void()>>(std::bind(&Device::UpdatePresentDevices, this)));
 }
 
 void Device::Stop()

+ 1 - 1
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::function<void()>>(std::bind(&Device::UpdatePresentDevices, this)));
 }
 
 void Device::Stop()

+ 11 - 15
Util/Timer.cpp

@@ -1,4 +1,5 @@
 #include <chrono>
+#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<void(void)> function)
+bool Timer::operator==(const Timer& other) const
 {
-	if (m_run.load(std::memory_order_acquire))
-		Stop();
-
-	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));
-			}
-		});
+	return m_identifier == other.Identifier();
+}
+
+std::string Timer::Identifier() const
+{
+	return m_identifier;
 }
 
 void Timer::Stop()

+ 25 - 1
Util/Timer.h

@@ -1,6 +1,8 @@
 #ifndef UTIL_TIMER_H
 #define UTIL_TIMER_H
 
+#include <iostream>
+
 #include <atomic>
 #include <functional>
 #include <thread>
@@ -15,13 +17,35 @@ public:
 	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();
 	bool IsRunning() const noexcept;
 	void Wait();
 
 private:
 	std::atomic<bool> m_run;
+	std::string m_identifier;
 	std::thread m_thread;
 };
 

+ 25 - 0
Util/Util.cpp

@@ -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 - 0
Util/Util.h

@@ -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