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.
This commit is contained in:
2026-08-24 01:56:04 +02:00
parent 273ff07a00
commit d98c29ecbb
7 changed files with 32 additions and 22 deletions
+1 -2
View File
@@ -5,10 +5,9 @@
#include <memory>
class Invoker {
public:
virtual ~Invoker() = default;
std::weak_ptr<Steppable> target;
void set_target(std::shared_ptr<Steppable> t) { target = t; }
Invoker(std::shared_ptr<Steppable> t) { target = t;}
void request() {
if (auto sp = target.lock()) {
sp->step();
+11 -2
View File
@@ -39,13 +39,22 @@ shared_ptr<Steppable> make_sort(std::shared_ptr<SortInput> input, std::string so
}
//creates a Sortbinding, actually does too much
void App::add_sort(shared_ptr<SortInput> myinput, string sorttype, Invoker &invoker) {
void App::add_sort(shared_ptr<SortInput> myinput, string sorttype) {
cout << "adding sort " << sorttype << "\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)});
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() {
+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, Invoker& invoker);
void add_sort(std::shared_ptr<SortInput> myinput, std::string sorttype);
void run();
};
-1
View File
@@ -44,7 +44,6 @@ public:
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);
};
+1 -3
View File
@@ -35,9 +35,7 @@ int main() {
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();
+7 -4
View File
@@ -1,21 +1,24 @@
#include <chrono>
#include <iostream>
#include <memory>
#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> 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() {
+4 -2
View File
@@ -4,17 +4,19 @@
#include <cstddef>
#include <ctime>
#include <chrono>
#include <memory>
#include <vector>
#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> invoker;
Timer(size_t triggeramount, std::chrono::milliseconds triggerinterval);
Timer(size_t triggeramount, std::unique_ptr<Invoker> invoker, std::chrono::milliseconds triggerinterval);
void add_time(std::chrono::milliseconds deltatime);
private: