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 c330f13bb8
7 changed files with 46 additions and 33 deletions
+4 -22
View File
@@ -30,31 +30,13 @@ App::App(unique_ptr<ViewSession> myviewsession, unique_ptr<TimerManager> mytimer
: viewsession(std::move(myviewsession)),
timermanager(std::move(mytimermanager)) {}
//connects data with the correct sort
shared_ptr<Steppable> make_sort(std::shared_ptr<SortInput> input, std::string sorttype) {
if (sorttype == "bsort") {
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";
//just pushes to App for now
void App::add_sort(shared_ptr<SortInput> 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<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
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() {
+1 -1
View File
@@ -22,7 +22,7 @@ struct App{
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();
};
+13 -1
View File
@@ -6,7 +6,8 @@
using namespace std;
SteppedBsort::SteppedBsort(shared_ptr<SortInput> input): data(input) {};
SteppedBsort::SteppedBsort(shared_ptr<SortInput> 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<SortInput> input, std::shared_ptr<Steppable> sort, std::shared_ptr<Invoker> invoker)
: input(std::move(input)), sort(std::move(sort)), invoker(std::move(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();
};
//working unit
struct SortBinding {
std::shared_ptr<SortInput> input;
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
+14 -2
View File
@@ -1,6 +1,7 @@
// for now juist CLI, print an arr, then print each it
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <SFML/Graphics/Color.hpp>
@@ -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<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
myapp.run();
+6 -4
View File
@@ -8,17 +8,19 @@
using namespace std::chrono;
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)
, 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;
+2 -2
View File
@@ -14,9 +14,9 @@ public:
size_t triggeramount;
bool isexpired = false;
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);
private: