Move timer to submodule

This commit is contained in:
2024-08-18 22:27:21 +02:00
parent 52f2ea4c0a
commit f7f5826bc1
5 changed files with 4 additions and 65 deletions
+3
View File
@@ -1,3 +1,6 @@
[submodule "external/color"] [submodule "external/color"]
path = external/color path = external/color
url = git@github.com:Sani7/cpp_color_lib.git url = git@github.com:Sani7/cpp_color_lib.git
[submodule "external/timer"]
path = external/timer
url = git@github.com:Sani7/cpp_timer.git
Vendored Submodule
+1
Submodule external/timer added at 2e0cd5f651
-16
View File
@@ -1,16 +0,0 @@
add_library(Timer STATIC)
target_compile_options(Timer PRIVATE $<$<CONFIG:Debug>:
-Wall -Wextra -pedantic-errors -Wconversion -Wsign-conversion
>)
target_sources(Timer
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/timer.cpp
)
target_include_directories(Timer
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
-34
View File
@@ -1,34 +0,0 @@
#include "timer.hpp"
Timer::Timer(void (*function)()) {
this->function = function;
}
void Timer::setTimeout(std::chrono::milliseconds delay) {
this->p_clear = false;
std::thread t([this, delay]() {
if(this->p_clear) return;
std::this_thread::sleep_for(delay);
if(this->p_clear) return;
function();
});
t.detach();
}
void Timer::setInterval(std::chrono::milliseconds interval) {
this->p_clear = false;
std::thread t([this, interval]() {
while(true) {
if(this->p_clear) return;
std::this_thread::sleep_for(interval);
if(this->p_clear) return;
function();
}
});
t.detach();
}
void Timer::stop() {
this->p_clear = true;
}
-15
View File
@@ -1,15 +0,0 @@
#pragma once
#include <chrono>
#include <thread>
class Timer {
public:
Timer(void (*function)());
void setTimeout(std::chrono::milliseconds delay);
void setInterval(std::chrono::milliseconds interval);
void stop();
private:
bool p_clear = false;
std::mutex p_clear_mutex;
void (*function)();
};