From c330f13bb85619a1529c8e5effd99e57e6004e3c Mon Sep 17 00:00:00 2001 From: my name Date: Thu, 27 Aug 2026 23:04:57 +0200 Subject: [PATCH] moved instantiation responsibility from App::add_sort Invoker is now an object that we attach to the sortbinding rather than the HasInvoker. we now create a binding in main, then pass it on to App. Timer now has a weak ptr to its invoker, so it could clean itself up. --- src/core/app.cpp | 26 ++++---------------------- src/core/app.hpp | 2 +- src/sorters.cpp | 14 +++++++++++++- src/sorters.hpp | 7 ++++++- src/sortui.cpp | 16 ++++++++++++++-- src/timer.cpp | 10 ++++++---- src/timer.hpp | 4 ++-- 7 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/core/app.cpp b/src/core/app.cpp index 2569210..049b210 100644 --- a/src/core/app.cpp +++ b/src/core/app.cpp @@ -30,31 +30,13 @@ App::App(unique_ptr myviewsession, unique_ptr mytimer : viewsession(std::move(myviewsession)), timermanager(std::move(mytimermanager)) {} -//connects data with the correct sort -shared_ptr make_sort(std::shared_ptr input, std::string sorttype) { - if (sorttype == "bsort") { - return make_shared(input); - } - return nullptr; -} - -//creates a Sortbinding, actually does too much -void App::add_sort(shared_ptr myinput, string sorttype) { - cout << "adding sort " << sorttype << "\n"; +//just pushes to App for now +void App::add_sort(shared_ptr myinput, SortBinding mysortbinding) { + cout << "adding sort " << myinput->name << "\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)}); - - //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)); + sortbindings.push_back(mysortbinding); } void App::run() { diff --git a/src/core/app.hpp b/src/core/app.hpp index 09bed81..834cc1e 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); + void add_sort(std::shared_ptr myinput, SortBinding mysortbinding); void run(); }; diff --git a/src/sorters.cpp b/src/sorters.cpp index 4ab62c4..3734714 100644 --- a/src/sorters.cpp +++ b/src/sorters.cpp @@ -6,7 +6,8 @@ using namespace std; -SteppedBsort::SteppedBsort(shared_ptr input): data(input) {}; +SteppedBsort::SteppedBsort(shared_ptr input) + : data(input) {}; void SteppedBsort:: step(){ cout << "step in SteppedBsort\n"; @@ -27,3 +28,14 @@ void SteppedBsort:: step(){ is_finished=true; } } + +SortBinding::SortBinding(std::shared_ptr input, std::shared_ptr sort, std::shared_ptr invoker) + : input(std::move(input)), sort(std::move(sort)), invoker(std::move(invoker)){} + +//connects data with the correct sort +std::shared_ptr make_sort(std::shared_ptr input, std::string sorttype) { + if (sorttype == "bsort") { + return make_shared(input); + } + return nullptr; +} diff --git a/src/sorters.hpp b/src/sorters.hpp index 39ecb8b..e5972d7 100644 --- a/src/sorters.hpp +++ b/src/sorters.hpp @@ -41,10 +41,15 @@ public: void step(); }; +//working unit struct SortBinding { std::shared_ptr input; std::shared_ptr sort; - //SortBinding(std::shared_ptr input, std::unique_ptr sort, std::unique_ptr invoker); + std::shared_ptr invoker; + SortBinding(std::shared_ptr input, std::shared_ptr sort, std::shared_ptr invoker); }; + +std::shared_ptr make_sort(std::shared_ptr input, std::string sorttype); + #endif diff --git a/src/sortui.cpp b/src/sortui.cpp index 59d7b9c..0ddf580 100644 --- a/src/sortui.cpp +++ b/src/sortui.cpp @@ -1,6 +1,7 @@ // for now juist CLI, print an arr, then print each it #include #include +#include #include #include @@ -14,6 +15,7 @@ #include "core/app.hpp" #include "sessions.hpp" #include "sorters.hpp" +#include "steppable.hpp" #include "timer.hpp" using namespace std::chrono; @@ -34,8 +36,18 @@ int main() { //create app with initialized dependencies App myapp(std::move(myviewsession),std::move(mytimermanager)); - //add and connect data - myapp.add_sort(myinput, "bsort"); + //create binding + shared_ptr asort = make_sort(myinput, "bsort"); + shared_ptr myinvoker = make_shared(asort); + SortBinding mysortbinding(myinput, asort, myinvoker); + + Timer timer(0, myinvoker, chrono::milliseconds(250)); + myapp.timermanager->add_timer(std::move(timer)); + + //add data and binding + myapp.add_sort(myinput, mysortbinding); + + //actually start the app myapp.run(); diff --git a/src/timer.cpp b/src/timer.cpp index 92e79ec..6082a27 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -8,17 +8,19 @@ using namespace std::chrono; using namespace std; -Timer::Timer(size_t triggeramount, std::unique_ptr invoker, milliseconds triggerinterval) +Timer::Timer(size_t triggeramount, std::weak_ptr invoker, milliseconds triggerinterval) :triggeramount(triggeramount) - , invoker(std::move(invoker)) - , triggerinterval(triggerinterval){} + , invoker(invoker) + , triggerinterval(triggerinterval){} void Timer::add_time(milliseconds deltatime) { accumulator += deltatime; if (accumulator >= triggerinterval) { accumulator -= triggerinterval; cout << "request in timer\n"; - invoker->request(); + if (auto sp = invoker.lock()) { + sp->request(); + } triggercount++; if (triggeramount != 0 && triggercount >= triggeramount){ isexpired=true; diff --git a/src/timer.hpp b/src/timer.hpp index 79e43b3..35b8e8e 100644 --- a/src/timer.hpp +++ b/src/timer.hpp @@ -14,9 +14,9 @@ public: size_t triggeramount; bool isexpired = false; size_t triggercount = 0; - std::unique_ptr invoker; + std::weak_ptr invoker; //if no longer exists, then should clean up timer - Timer(size_t triggeramount, std::unique_ptr invoker, std::chrono::milliseconds triggerinterval); + Timer(size_t triggeramount, std::weak_ptr invoker, std::chrono::milliseconds triggerinterval); void add_time(std::chrono::milliseconds deltatime); private: