ic working example
setup with working timers and timermanager
This commit is contained in:
+1477
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,21 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.24)
|
||||||
|
project(cppout)
|
||||||
|
|
||||||
|
file(GLOB SOURCE_FILES "*.cpp")
|
||||||
|
|
||||||
|
find_package(Curses REQUIRED)
|
||||||
|
include_directories(${CURSES_INCLUDE_DIR})
|
||||||
|
|
||||||
|
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
|
||||||
|
|
||||||
|
if(MSVC)
|
||||||
|
target_compile_option(${PROJECT_NAME} PRIVATE /W4)
|
||||||
|
else()
|
||||||
|
target_compile_options(${PROJECT_NAME} PRIVATE
|
||||||
|
-Wall
|
||||||
|
-Wextra
|
||||||
|
-Wpedantic
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
target_link_libraries(${PROJECT_NAME} ${CURSES_LIBRARIES})
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
|
||||||
|
// for now juist CLI, print an arr, then print each it
|
||||||
|
#include <chrono>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
#include <vector>
|
||||||
|
#include "timer.hpp"
|
||||||
|
|
||||||
|
struct SortingData {
|
||||||
|
std::string name;
|
||||||
|
int target;
|
||||||
|
std::vector<int> data;
|
||||||
|
|
||||||
|
SortingData(std::string name, std::vector<int> data)
|
||||||
|
: name(name), data(data) {}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
void displaynumbers(SortingData in_arr) {
|
||||||
|
for (size_t i = 0; i < in_arr.data.size(); ++i) {
|
||||||
|
printf("array:%d ", in_arr.data[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void stepbubblesort(std::vector<int> &data, size_t step) {
|
||||||
|
data[step%data.size()]++;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
std::vector<int> dummydata = {9,8,1,2,7,3,6,4,5};
|
||||||
|
SortingData mysd("arr1", dummydata);
|
||||||
|
std::unique_ptr<Timer> mytimer = std::make_unique<Timer>(
|
||||||
|
"sort1", 0,
|
||||||
|
[&mysd](Timer &t) { stepbubblesort(mysd.data, t.triggercounter); },
|
||||||
|
std::chrono::milliseconds(1000));
|
||||||
|
std::unique_ptr<Timer> mydisplaytimer = std::make_unique<Timer>("display", 0, [&mysd](Timer &t){displaynumbers(mysd);}, std::chrono::milliseconds(1000));
|
||||||
|
|
||||||
|
TimerManager mytimermanager;
|
||||||
|
|
||||||
|
|
||||||
|
mytimermanager.add_timer(std::move(mytimer));
|
||||||
|
mytimermanager.add_timer(std::move(mydisplaytimer));
|
||||||
|
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
mytimermanager.update_timers();
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
#include <chrono>
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
#include "timer.hpp"
|
||||||
|
Timer::Timer(std::string name, int triggercount, std::function<void(Timer &t)> myaction, std::chrono::milliseconds triggerinterval)
|
||||||
|
: name(name), triggeramount(triggercount), timeraction(myaction), triggerinterval(triggerinterval) {}
|
||||||
|
|
||||||
|
void Timer::add_time(std::chrono::milliseconds deltatime) {
|
||||||
|
accumulator += deltatime;
|
||||||
|
if (accumulator >= triggerinterval) {
|
||||||
|
accumulator -= triggerinterval;
|
||||||
|
timeraction(*this);
|
||||||
|
triggercounter++;
|
||||||
|
if (!isinfinite && triggercounter >= triggeramount){
|
||||||
|
isexpired=true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void TimerManager::add_timer(std::unique_ptr<Timer> newtimer) {
|
||||||
|
timers.push_back(std::move(newtimer));
|
||||||
|
};
|
||||||
|
|
||||||
|
void TimerManager::update_timers() {
|
||||||
|
std::chrono::steady_clock::time_point currenttime =
|
||||||
|
std::chrono::steady_clock::now();
|
||||||
|
std::chrono::milliseconds deltatime = std::chrono::duration_cast<std::chrono::milliseconds>(currenttime-previoustime);
|
||||||
|
for(std::unique_ptr<Timer> &mytimer: timers){
|
||||||
|
mytimer->add_time(deltatime);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <ctime>
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <chrono>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class Timer {
|
||||||
|
public:
|
||||||
|
std::string name;
|
||||||
|
std::size_t triggeramount;
|
||||||
|
|
||||||
|
bool isexpired;
|
||||||
|
bool isinfinite;
|
||||||
|
std::size_t triggercounter = 0;
|
||||||
|
|
||||||
|
Timer(std::string name, int triggercount, std::function<void(Timer &t)> myaction, std::chrono::milliseconds triggerinterval);
|
||||||
|
void add_time(std::chrono::milliseconds deltatime);
|
||||||
|
std::function<void(Timer&)> timeraction;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::chrono::milliseconds accumulator = std::chrono::milliseconds(0);
|
||||||
|
std::chrono::milliseconds triggerinterval = std::chrono::milliseconds(1000); //default to 1 sec
|
||||||
|
};
|
||||||
|
|
||||||
|
class TimerManager {
|
||||||
|
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::vector<std::unique_ptr<Timer>> timers;
|
||||||
|
TimerManager() { previoustime = std::chrono::steady_clock::now(); }
|
||||||
|
void add_timer(std::unique_ptr<Timer> newtimer);
|
||||||
|
void delete_timer();
|
||||||
|
void update_timers();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::chrono::steady_clock::time_point previoustime;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user