From 273ff07a00b30397a377915c6fb10de9ed4336cb Mon Sep 17 00:00:00 2001 From: my name Date: Sun, 23 Aug 2026 23:25:04 +0200 Subject: [PATCH] cleanup, timermanager in App is a dependency that takes timers again. App now adds already constructed Timers Timer is now an Invoker that takes a weak_ptr to a steppable with a request()rather than a bool callback. should eventually be changed to composition --- src/Invoker.hpp | 19 +++++++++++++++++++ src/core/app.cpp | 37 +++++++++++++++++++++---------------- src/core/app.hpp | 9 +++++---- src/sessions.hpp | 13 ------------- src/sorters.cpp | 9 ++++----- src/sorters.hpp | 9 +++++---- src/sortui.cpp | 12 +++++++++--- src/steppable.hpp | 2 +- src/timer.cpp | 20 +++++++------------- src/timer.hpp | 12 +++--------- 10 files changed, 74 insertions(+), 68 deletions(-) create mode 100644 src/Invoker.hpp diff --git a/src/Invoker.hpp b/src/Invoker.hpp new file mode 100644 index 0000000..3f3a1ce --- /dev/null +++ b/src/Invoker.hpp @@ -0,0 +1,19 @@ +#ifndef INVOKER_HPP +#define INVOKER_HPP + +#include "steppable.hpp" +#include +class Invoker { +public: + virtual ~Invoker() = default; + std::weak_ptr target; + + void set_target(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 78a1545..f6d6ee3 100644 --- a/src/core/app.cpp +++ b/src/core/app.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -25,22 +26,26 @@ string arraytostr(shared_ptr input) { return stringrep.str(); } -App::App(unique_ptr myviewsession) - : viewsession(std::move(myviewsession)) {} +App::App(unique_ptr myviewsession, unique_ptr mytimermanager) + : viewsession(std::move(myviewsession)), + timermanager(std::move(mytimermanager)) {} -//connects data with logic and invoker -void App::setup_session(shared_ptr myinput, string sorttype) { - cout << "adding sort " << sorttype << "\n"; - //push data to app - inputs.push_back(myinput); - //connect data to logic +//connects data with the correct sort +shared_ptr make_sort(std::shared_ptr input, std::string sorttype) { if (sorttype == "bsort") { - sortsession.add_session(myinput, sorttype); + return make_shared(input); } - //connect logic to timer (should not be hardcoded to timers) - auto prs = sortsession.sortsessions.back().get(); - timermanager.add_timer( - 0, [prs]() { return prs->step(); }, std::chrono::milliseconds(250)); + return nullptr; +} + +//creates a Sortbinding, actually does too much +void App::add_sort(shared_ptr myinput, string sorttype, Invoker &invoker) { + cout << "adding sort " << sorttype << "\n"; + inputs.push_back(myinput); + std::shared_ptr mysorter = make_sort(myinput, sorttype); + + sortbindings.push_back(SortBinding{myinput, std::move(mysorter)}); + invoker.set_target(sortbindings.back().sort); } void App::run() { @@ -51,7 +56,7 @@ void App::run() { viewsession->mywindow.close(); } - //draw + // draw viewsession->mywindow.clear(sf::Color::Black); viewsession->mywindow.draw(viewsession->text); viewsession->text.setString(arraytostr(inputs[0])); @@ -61,8 +66,8 @@ void App::run() { } viewsession->mywindow.display(); - //update logic - timermanager.update_timers(); + // update logic + timermanager->update_timers(); this_thread::sleep_for(std::chrono::milliseconds(250)); } } diff --git a/src/core/app.hpp b/src/core/app.hpp index 7bc2d5f..ae72470 100644 --- a/src/core/app.hpp +++ b/src/core/app.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "../timer.hpp" #include "../sorters.hpp" @@ -15,13 +16,13 @@ //for now data, mutation, and rendering struct App{ std::vector> inputs; - SortSession sortsession; + std::vector sortbindings; std::unique_ptr viewsession; - TimerManager timermanager; + std::unique_ptr timermanager; - App(std::unique_ptr myviewsession); + App(std::unique_ptr myviewsession, std::unique_ptr mytimermanager); - void setup_session(std::shared_ptr myinput, std::string sorttype); + void add_sort(std::shared_ptr myinput, std::string sorttype, Invoker& invoker); void run(); }; diff --git a/src/sessions.hpp b/src/sessions.hpp index 436bf90..c6acaab 100644 --- a/src/sessions.hpp +++ b/src/sessions.hpp @@ -9,19 +9,6 @@ #include "sorters.hpp" //owns and manages the sorters that mutate data, but does not own the data itself -struct SortSession { - std::vector> sortsessions; - - SortSession(){ - } - - //connects data with a sorter and controls it through a timer - void add_session(std::shared_ptr input, std::string sorttype){ - if(sorttype == "bsort"){ - sortsessions.push_back(make_unique(input)); - }; - } -}; //sfml/render struct ViewSession{ diff --git a/src/sorters.cpp b/src/sorters.cpp index f73722f..4ab62c4 100644 --- a/src/sorters.cpp +++ b/src/sorters.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include "sorters.hpp" @@ -7,10 +8,10 @@ using namespace std; SteppedBsort::SteppedBsort(shared_ptr input): data(input) {}; -bool SteppedBsort:: step(){ - if(is_finished==true) { +void SteppedBsort:: step(){ + cout << "step in SteppedBsort\n"; + if(is_finished) { printf("finished"); - return true; } if(data->values[j]>data->values[j+1]) { @@ -25,6 +26,4 @@ bool SteppedBsort:: step(){ if (i==data->values.size()-1){ is_finished=true; } - - return false; } diff --git a/src/sorters.hpp b/src/sorters.hpp index 6da03eb..8e3bfc8 100644 --- a/src/sorters.hpp +++ b/src/sorters.hpp @@ -38,13 +38,14 @@ public: SteppedBsort(std::shared_ptr data); - bool step(); + void step(); }; struct SortBinding { std::shared_ptr input; - std::unique_ptr sort; - std::unique_ptr invoker; -}; + std::shared_ptr sort; + //SortBinding(std::shared_ptr input, std::unique_ptr sort, std::unique_ptr invoker); + +}; #endif diff --git a/src/sortui.cpp b/src/sortui.cpp index d6f414f..4cea0e9 100644 --- a/src/sortui.cpp +++ b/src/sortui.cpp @@ -14,6 +14,7 @@ #include "core/app.hpp" #include "sessions.hpp" #include "sorters.hpp" +#include "timer.hpp" using namespace std::chrono; using namespace std; @@ -28,11 +29,16 @@ int main() { auto myinput2 = make_shared("arr2", dataprovider()); auto myviewsession = make_unique(); - + auto mytimermanager = make_unique(); + //create app with initialized dependencies - App myapp(std::move(myviewsession)); + App myapp(std::move(myviewsession),std::move(mytimermanager)); + //add and connect data - myapp.setup_session(myinput, "bsort"); + Timer timer(0, chrono::milliseconds(250)); + myapp.timermanager->timers.push_back(timer); + myapp.add_sort(myinput, "bsort", myapp.timermanager->timers.back()); + //actually start the app myapp.run(); } diff --git a/src/steppable.hpp b/src/steppable.hpp index 8371fa9..27c67bc 100644 --- a/src/steppable.hpp +++ b/src/steppable.hpp @@ -8,7 +8,7 @@ public: virtual ~Steppable() = default; bool is_finished = 0; size_t stepcount = 0; - virtual bool step() = 0; + virtual void step() = 0; }; #endif diff --git a/src/timer.cpp b/src/timer.cpp index d06d30e..b75b2b1 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -1,37 +1,31 @@ #include -#include -#include +#include #include "timer.hpp" using namespace std::chrono; using namespace std; -Timer::Timer(size_t triggeramount, function myaction, milliseconds triggerinterval) +Timer::Timer(size_t triggeramount, milliseconds triggerinterval) :triggeramount(triggeramount) - , callback(myaction) , triggerinterval(triggerinterval) {} void Timer::add_time(milliseconds deltatime) { accumulator += deltatime; if (accumulator >= triggerinterval) { accumulator -= triggerinterval; + cout << "request in timer\n"; request(); triggercount++; - if (!isinfinite && triggercount >= triggeramount){ + if (triggeramount != 0 && triggercount >= triggeramount){ isexpired=true; } } -}; - -void Timer::request() { - callback(); } -void TimerManager::add_timer(size_t triggeramount, function myaction, std::chrono::milliseconds triggerinterval) { - Timer mynewtimer(triggeramount, myaction, triggerinterval); - timers.push_back(std::move(mynewtimer)); -}; +void TimerManager::add_timer(Timer timer){ + timers.push_back(timer); +} void TimerManager::update_timers() { steady_clock::time_point currenttime = diff --git a/src/timer.hpp b/src/timer.hpp index 72a2e6d..7ed0b1a 100644 --- a/src/timer.hpp +++ b/src/timer.hpp @@ -3,7 +3,6 @@ #include #include -#include #include #include @@ -13,17 +12,11 @@ class Timer: public Invoker { public: size_t triggeramount; bool isexpired = false; - bool isinfinite = false; size_t triggercount = 0; - std::function callback; - - Timer(size_t triggeramount, std::function myaction, std::chrono::milliseconds triggerinterval); - + Timer(size_t triggeramount, std::chrono::milliseconds triggerinterval); void add_time(std::chrono::milliseconds deltatime); - void request(); - private: std::chrono::milliseconds accumulator = std::chrono::milliseconds(0); std::chrono::milliseconds triggerinterval = std::chrono::milliseconds(1000); //default to 1 sec @@ -35,7 +28,8 @@ class TimerManager { public: std::vector timers; TimerManager(){previoustime = std::chrono::steady_clock::now();} - void add_timer(size_t triggeramount, std::function myaction, std::chrono::milliseconds triggerinterval); + //void add_timer(size_t triggeramount, std::function myaction, std::chrono::milliseconds triggerinterval); + void add_timer(Timer timer); void delete_timer(); void update_timers();