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:
+7
-8
@@ -5,15 +5,14 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
class Invoker {
|
class Invoker {
|
||||||
public:
|
public:
|
||||||
virtual ~Invoker() = default;
|
std::weak_ptr<Steppable> target;
|
||||||
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() {
|
void request() {
|
||||||
if (auto sp = target.lock()) {
|
if (auto sp = target.lock()) {
|
||||||
sp->step();
|
sp->step();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+11
-2
@@ -39,13 +39,22 @@ shared_ptr<Steppable> make_sort(std::shared_ptr<SortInput> input, std::string so
|
|||||||
}
|
}
|
||||||
|
|
||||||
//creates a Sortbinding, actually does too much
|
//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";
|
cout << "adding sort " << sorttype << "\n";
|
||||||
|
//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);
|
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)});
|
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() {
|
void App::run() {
|
||||||
|
|||||||
+1
-1
@@ -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, Invoker& invoker);
|
void add_sort(std::shared_ptr<SortInput> myinput, std::string sorttype);
|
||||||
void run();
|
void run();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -44,7 +44,6 @@ public:
|
|||||||
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);
|
//SortBinding(std::shared_ptr<SortInput> input, std::unique_ptr<Steppable> sort, std::unique_ptr<Invoker> invoker);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
+1
-3
@@ -35,9 +35,7 @@ int main() {
|
|||||||
App myapp(std::move(myviewsession),std::move(mytimermanager));
|
App myapp(std::move(myviewsession),std::move(mytimermanager));
|
||||||
|
|
||||||
//add and connect data
|
//add and connect data
|
||||||
Timer timer(0, chrono::milliseconds(250));
|
myapp.add_sort(myinput, "bsort");
|
||||||
myapp.timermanager->timers.push_back(timer);
|
|
||||||
myapp.add_sort(myinput, "bsort", myapp.timermanager->timers.back());
|
|
||||||
|
|
||||||
//actually start the app
|
//actually start the app
|
||||||
myapp.run();
|
myapp.run();
|
||||||
|
|||||||
+7
-4
@@ -1,21 +1,24 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include "timer.hpp"
|
#include "timer.hpp"
|
||||||
|
#include "Invoker.hpp"
|
||||||
|
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
Timer::Timer(size_t triggeramount, milliseconds triggerinterval)
|
Timer::Timer(size_t triggeramount, std::unique_ptr<Invoker> invoker, milliseconds triggerinterval)
|
||||||
:triggeramount(triggeramount)
|
:triggeramount(triggeramount)
|
||||||
, triggerinterval(triggerinterval) {}
|
, invoker(std::move(invoker))
|
||||||
|
, triggerinterval(triggerinterval){}
|
||||||
|
|
||||||
void Timer::add_time(milliseconds deltatime) {
|
void Timer::add_time(milliseconds deltatime) {
|
||||||
accumulator += deltatime;
|
accumulator += deltatime;
|
||||||
if (accumulator >= triggerinterval) {
|
if (accumulator >= triggerinterval) {
|
||||||
accumulator -= triggerinterval;
|
accumulator -= triggerinterval;
|
||||||
cout << "request in timer\n";
|
cout << "request in timer\n";
|
||||||
request();
|
invoker->request();
|
||||||
triggercount++;
|
triggercount++;
|
||||||
if (triggeramount != 0 && triggercount >= triggeramount){
|
if (triggeramount != 0 && triggercount >= triggeramount){
|
||||||
isexpired=true;
|
isexpired=true;
|
||||||
@@ -24,7 +27,7 @@ void Timer::add_time(milliseconds deltatime) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TimerManager::add_timer(Timer timer){
|
void TimerManager::add_timer(Timer timer){
|
||||||
timers.push_back(timer);
|
timers.push_back(std::move(timer));
|
||||||
}
|
}
|
||||||
|
|
||||||
void TimerManager::update_timers() {
|
void TimerManager::update_timers() {
|
||||||
|
|||||||
+4
-2
@@ -4,17 +4,19 @@
|
|||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "Invoker.hpp"
|
#include "Invoker.hpp"
|
||||||
//check if the timer is still valid, check if enough time has passed and if so, actuate action
|
//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:
|
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;
|
||||||
|
|
||||||
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);
|
void add_time(std::chrono::milliseconds deltatime);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
Reference in New Issue
Block a user