From d98c29ecbbbb3623c71219c4f3c9eef7209150ef Mon Sep 17 00:00:00 2001 From: my name Date: Mon, 24 Aug 2026 01:56:04 +0200 Subject: [PATCH] Timer now contains an invoker instead of inheriting the idea is that an invoker holds a weak_ptr to a steppable. and anything with an invoker can advance a steppable app::add_sort has become too heavy because of the way timer and invoker are initialized now. --- src/Invoker.hpp | 15 +++++++-------- src/core/app.cpp | 13 +++++++++++-- src/core/app.hpp | 2 +- src/sorters.hpp | 1 - src/sortui.cpp | 6 ++---- src/timer.cpp | 11 +++++++---- src/timer.hpp | 6 ++++-- 7 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/Invoker.hpp b/src/Invoker.hpp index 3f3a1ce..d2c496a 100644 --- a/src/Invoker.hpp +++ b/src/Invoker.hpp @@ -5,15 +5,14 @@ #include class Invoker { public: - virtual ~Invoker() = default; - std::weak_ptr target; + std::weak_ptr target; - void set_target(std::shared_ptr t) { target = t; } - void request() { - if (auto sp = target.lock()) { - sp->step(); - } - } + Invoker(std::shared_ptr t) { target = t;} + void request() { + if (auto sp = target.lock()) { + sp->step(); + } + } }; #endif diff --git a/src/core/app.cpp b/src/core/app.cpp index f6d6ee3..2569210 100644 --- a/src/core/app.cpp +++ b/src/core/app.cpp @@ -39,13 +39,22 @@ shared_ptr make_sort(std::shared_ptr input, std::string so } //creates a Sortbinding, actually does too much -void App::add_sort(shared_ptr myinput, string sorttype, Invoker &invoker) { +void App::add_sort(shared_ptr myinput, string sorttype) { cout << "adding sort " << sorttype << "\n"; + //insert data inputs.push_back(myinput); + //connect data with logic into a sort std::shared_ptr mysorter = make_sort(myinput, sorttype); + //connect the sort with its invoker + unique_ptr myinvoker = make_unique(mysorter); + + //move the sort into app sortbindings.push_back(SortBinding{myinput, std::move(mysorter)}); - invoker.set_target(sortbindings.back().sort); + + //create the timer with the invoker and add it to app + Timer timer(0, std::move(myinvoker), chrono::milliseconds(250)); + timermanager->add_timer(std::move(timer)); } void App::run() { diff --git a/src/core/app.hpp b/src/core/app.hpp index ae72470..09bed81 100644 --- a/src/core/app.hpp +++ b/src/core/app.hpp @@ -22,7 +22,7 @@ struct App{ App(std::unique_ptr myviewsession, std::unique_ptr mytimermanager); - void add_sort(std::shared_ptr myinput, std::string sorttype, Invoker& invoker); + void add_sort(std::shared_ptr myinput, std::string sorttype); void run(); }; diff --git a/src/sorters.hpp b/src/sorters.hpp index 8e3bfc8..39ecb8b 100644 --- a/src/sorters.hpp +++ b/src/sorters.hpp @@ -44,7 +44,6 @@ public: struct SortBinding { std::shared_ptr input; std::shared_ptr sort; - //SortBinding(std::shared_ptr input, std::unique_ptr sort, std::unique_ptr invoker); }; diff --git a/src/sortui.cpp b/src/sortui.cpp index 4cea0e9..59d7b9c 100644 --- a/src/sortui.cpp +++ b/src/sortui.cpp @@ -30,14 +30,12 @@ int main() { auto myviewsession = make_unique(); auto mytimermanager = make_unique(); - + //create app with initialized dependencies App myapp(std::move(myviewsession),std::move(mytimermanager)); //add and connect data - Timer timer(0, chrono::milliseconds(250)); - myapp.timermanager->timers.push_back(timer); - myapp.add_sort(myinput, "bsort", myapp.timermanager->timers.back()); + myapp.add_sort(myinput, "bsort"); //actually start the app myapp.run(); diff --git a/src/timer.cpp b/src/timer.cpp index b75b2b1..92e79ec 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -1,21 +1,24 @@ #include #include +#include #include "timer.hpp" +#include "Invoker.hpp" using namespace std::chrono; using namespace std; -Timer::Timer(size_t triggeramount, milliseconds triggerinterval) +Timer::Timer(size_t triggeramount, std::unique_ptr invoker, milliseconds triggerinterval) :triggeramount(triggeramount) - , triggerinterval(triggerinterval) {} + , invoker(std::move(invoker)) + , triggerinterval(triggerinterval){} void Timer::add_time(milliseconds deltatime) { accumulator += deltatime; if (accumulator >= triggerinterval) { accumulator -= triggerinterval; cout << "request in timer\n"; - request(); + invoker->request(); triggercount++; if (triggeramount != 0 && triggercount >= triggeramount){ isexpired=true; @@ -24,7 +27,7 @@ void Timer::add_time(milliseconds deltatime) { } void TimerManager::add_timer(Timer timer){ - timers.push_back(timer); + timers.push_back(std::move(timer)); } void TimerManager::update_timers() { diff --git a/src/timer.hpp b/src/timer.hpp index 7ed0b1a..79e43b3 100644 --- a/src/timer.hpp +++ b/src/timer.hpp @@ -4,17 +4,19 @@ #include #include #include +#include #include #include "Invoker.hpp" //check if the timer is still valid, check if enough time has passed and if so, actuate action -class Timer: public Invoker { +class Timer { public: size_t triggeramount; bool isexpired = false; size_t triggercount = 0; + std::unique_ptr invoker; - Timer(size_t triggeramount, std::chrono::milliseconds triggerinterval); + Timer(size_t triggeramount, std::unique_ptr invoker, std::chrono::milliseconds triggerinterval); void add_time(std::chrono::milliseconds deltatime); private: