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.
This commit is contained in:
2026-08-27 23:04:57 +02:00
parent d98c29ecbb
commit d3c966b268
7 changed files with 43 additions and 32 deletions
+4 -22
View File
@@ -30,31 +30,13 @@ App::App(unique_ptr<ViewSession> myviewsession, unique_ptr<TimerManager> mytimer
: viewsession(std::move(myviewsession)), : viewsession(std::move(myviewsession)),
timermanager(std::move(mytimermanager)) {} timermanager(std::move(mytimermanager)) {}
//connects data with the correct sort //just pushes to App for now
shared_ptr<Steppable> make_sort(std::shared_ptr<SortInput> input, std::string sorttype) { void App::add_sort(shared_ptr<SortInput> myinput, SortBinding mysortbinding) {
if (sorttype == "bsort") { cout << "adding sort " << myinput->name << "\n";
return make_shared<SteppedBsort>(input);
}
return nullptr;
}
//creates a Sortbinding, actually does too much
void App::add_sort(shared_ptr<SortInput> myinput, string sorttype) {
cout << "adding sort " << sorttype << "\n";
//insert data //insert data
inputs.push_back(myinput); inputs.push_back(myinput);
//connect data with logic into a sort
std::shared_ptr<Steppable> mysorter = make_sort(myinput, sorttype);
//connect the sort with its invoker
unique_ptr<Invoker> myinvoker = make_unique<Invoker>(mysorter);
//move the sort into app //move the sort into app
sortbindings.push_back(SortBinding{myinput, std::move(mysorter)}); sortbindings.push_back(mysortbinding);
//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() { void App::run() {
+1 -1
View File
@@ -22,7 +22,7 @@ struct App{
App(std::unique_ptr<ViewSession> myviewsession, std::unique_ptr<TimerManager> mytimermanager); App(std::unique_ptr<ViewSession> myviewsession, std::unique_ptr<TimerManager> mytimermanager);
void add_sort(std::shared_ptr<SortInput> myinput, std::string sorttype); void add_sort(std::shared_ptr<SortInput> myinput, SortBinding mysortbinding);
void run(); void run();
}; };
+10
View File
@@ -27,3 +27,13 @@ void SteppedBsort:: step(){
is_finished=true; is_finished=true;
} }
} }
SortBinding::SortBinding(std::shared_ptr<SortInput> input, std::shared_ptr<Steppable> sort, std::shared_ptr<Invoker> invoker): input(input), sort(sort), invoker(invoker){}
//connects data with the correct sort
std::shared_ptr<Steppable> make_sort(std::shared_ptr<SortInput> input, std::string sorttype) {
if (sorttype == "bsort") {
return make_shared<SteppedBsort>(input);
}
return nullptr;
}
+6 -1
View File
@@ -41,10 +41,15 @@ public:
void step(); void step();
}; };
//working unit
struct SortBinding { struct SortBinding {
std::shared_ptr<SortInput> input; std::shared_ptr<SortInput> input;
std::shared_ptr<Steppable> sort; std::shared_ptr<Steppable> sort;
//SortBinding(std::shared_ptr<SortInput> input, std::unique_ptr<Steppable> sort, std::unique_ptr<Invoker> invoker); std::shared_ptr<Invoker> invoker;
SortBinding(std::shared_ptr<SortInput> input, std::shared_ptr<Steppable> sort, std::shared_ptr<Invoker> invoker);
}; };
std::shared_ptr<Steppable> make_sort(std::shared_ptr<SortInput> input, std::string sorttype);
#endif #endif
+14 -2
View File
@@ -1,6 +1,7 @@
// for now juist CLI, print an arr, then print each it // for now juist CLI, print an arr, then print each it
#include <memory> #include <memory>
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
#include <SFML/Graphics/Color.hpp> #include <SFML/Graphics/Color.hpp>
@@ -14,6 +15,7 @@
#include "core/app.hpp" #include "core/app.hpp"
#include "sessions.hpp" #include "sessions.hpp"
#include "sorters.hpp" #include "sorters.hpp"
#include "steppable.hpp"
#include "timer.hpp" #include "timer.hpp"
using namespace std::chrono; using namespace std::chrono;
@@ -34,8 +36,18 @@ int main() {
//create app with initialized dependencies //create app with initialized dependencies
App myapp(std::move(myviewsession),std::move(mytimermanager)); App myapp(std::move(myviewsession),std::move(mytimermanager));
//add and connect data //create binding
myapp.add_sort(myinput, "bsort"); shared_ptr<Steppable> asort = make_sort(myinput, "bsort");
shared_ptr<Invoker> myinvoker = make_shared<Invoker>(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 //actually start the app
myapp.run(); myapp.run();
+5 -3
View File
@@ -8,9 +8,9 @@
using namespace std::chrono; using namespace std::chrono;
using namespace std; using namespace std;
Timer::Timer(size_t triggeramount, std::unique_ptr<Invoker> invoker, milliseconds triggerinterval) Timer::Timer(size_t triggeramount, std::weak_ptr<Invoker> invoker, milliseconds triggerinterval)
:triggeramount(triggeramount) :triggeramount(triggeramount)
, invoker(std::move(invoker)) , invoker(invoker)
, triggerinterval(triggerinterval){} , triggerinterval(triggerinterval){}
void Timer::add_time(milliseconds deltatime) { void Timer::add_time(milliseconds deltatime) {
@@ -18,7 +18,9 @@ void Timer::add_time(milliseconds deltatime) {
if (accumulator >= triggerinterval) { if (accumulator >= triggerinterval) {
accumulator -= triggerinterval; accumulator -= triggerinterval;
cout << "request in timer\n"; cout << "request in timer\n";
invoker->request(); if (auto sp = invoker.lock()) {
sp->request();
}
triggercount++; triggercount++;
if (triggeramount != 0 && triggercount >= triggeramount){ if (triggeramount != 0 && triggercount >= triggeramount){
isexpired=true; isexpired=true;
+2 -2
View File
@@ -14,9 +14,9 @@ public:
size_t triggeramount; size_t triggeramount;
bool isexpired = false; bool isexpired = false;
size_t triggercount = 0; size_t triggercount = 0;
std::unique_ptr<Invoker> invoker; std::weak_ptr<Invoker> invoker; //if no longer exists, then should clean up timer
Timer(size_t triggeramount, std::unique_ptr<Invoker> invoker, std::chrono::milliseconds triggerinterval); Timer(size_t triggeramount, std::weak_ptr<Invoker> invoker, std::chrono::milliseconds triggerinterval);
void add_time(std::chrono::milliseconds deltatime); void add_time(std::chrono::milliseconds deltatime);
private: private: