Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d98c29ecbb | |||
| 273ff07a00 | |||
| 54faab2a28 |
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef INVOKER_HPP
|
||||||
|
#define INVOKER_HPP
|
||||||
|
|
||||||
|
#include "steppable.hpp"
|
||||||
|
#include <memory>
|
||||||
|
class Invoker {
|
||||||
|
public:
|
||||||
|
std::weak_ptr<Steppable> target;
|
||||||
|
|
||||||
|
Invoker(std::shared_ptr<Steppable> t) { target = t;}
|
||||||
|
void request() {
|
||||||
|
if (auto sp = target.lock()) {
|
||||||
|
sp->step();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
+28
-14
@@ -1,4 +1,5 @@
|
|||||||
#include <SFML/Graphics/Font.hpp>
|
#include <SFML/Graphics/Font.hpp>
|
||||||
|
#include <chrono>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
@@ -25,22 +26,35 @@ string arraytostr(shared_ptr<SortInput> input) {
|
|||||||
return stringrep.str();
|
return stringrep.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
App::App(unique_ptr<ViewSession> myviewsession)
|
App::App(unique_ptr<ViewSession> myviewsession, unique_ptr<TimerManager> mytimermanager)
|
||||||
: viewsession(std::move(myviewsession)) {}
|
: viewsession(std::move(myviewsession)),
|
||||||
|
timermanager(std::move(mytimermanager)) {}
|
||||||
|
|
||||||
//connects data with logic and invoker
|
//connects data with the correct sort
|
||||||
void App::setup_session(shared_ptr<SortInput> myinput, string sorttype) {
|
shared_ptr<Steppable> make_sort(std::shared_ptr<SortInput> input, std::string sorttype) {
|
||||||
cout << "adding sort " << sorttype << "\n";
|
|
||||||
//push data to app
|
|
||||||
inputs.push_back(myinput);
|
|
||||||
//connect data to logic
|
|
||||||
if (sorttype == "bsort") {
|
if (sorttype == "bsort") {
|
||||||
sortsession.add_session(myinput, sorttype);
|
return make_shared<SteppedBsort>(input);
|
||||||
}
|
}
|
||||||
//connect logic to timer (should not be hardcoded to timers)
|
return nullptr;
|
||||||
auto prs = sortsession.sortsessions.back().get();
|
}
|
||||||
timermanager.add_timer(
|
|
||||||
0, [prs]() { return prs->step(); }, std::chrono::milliseconds(250));
|
//creates a Sortbinding, actually does too much
|
||||||
|
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)});
|
||||||
|
|
||||||
|
//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() {
|
||||||
@@ -62,7 +76,7 @@ void App::run() {
|
|||||||
viewsession->mywindow.display();
|
viewsession->mywindow.display();
|
||||||
|
|
||||||
// update logic
|
// update logic
|
||||||
timermanager.update_timers();
|
timermanager->update_timers();
|
||||||
this_thread::sleep_for(std::chrono::milliseconds(250));
|
this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-4
@@ -6,6 +6,7 @@
|
|||||||
#include <SFML/Graphics/RenderWindow.hpp>
|
#include <SFML/Graphics/RenderWindow.hpp>
|
||||||
#include <SFML/Graphics/Text.hpp>
|
#include <SFML/Graphics/Text.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "../timer.hpp"
|
#include "../timer.hpp"
|
||||||
#include "../sorters.hpp"
|
#include "../sorters.hpp"
|
||||||
@@ -15,13 +16,13 @@
|
|||||||
//for now data, mutation, and rendering
|
//for now data, mutation, and rendering
|
||||||
struct App{
|
struct App{
|
||||||
std::vector<std::shared_ptr<SortInput>> inputs;
|
std::vector<std::shared_ptr<SortInput>> inputs;
|
||||||
SortSession sortsession;
|
std::vector<SortBinding> sortbindings;
|
||||||
std::unique_ptr<ViewSession> viewsession;
|
std::unique_ptr<ViewSession> viewsession;
|
||||||
TimerManager timermanager;
|
std::unique_ptr<TimerManager> timermanager;
|
||||||
|
|
||||||
App(std::unique_ptr<ViewSession> myviewsession);
|
App(std::unique_ptr<ViewSession> myviewsession, std::unique_ptr<TimerManager> mytimermanager);
|
||||||
|
|
||||||
void setup_session(std::shared_ptr<SortInput> myinput, std::string sorttype);
|
void add_sort(std::shared_ptr<SortInput> myinput, std::string sorttype);
|
||||||
void run();
|
void run();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -9,19 +9,6 @@
|
|||||||
#include "sorters.hpp"
|
#include "sorters.hpp"
|
||||||
|
|
||||||
//owns and manages the sorters that mutate data, but does not own the data itself
|
//owns and manages the sorters that mutate data, but does not own the data itself
|
||||||
struct SortSession {
|
|
||||||
std::vector<std::unique_ptr<Steppable>> sortsessions;
|
|
||||||
|
|
||||||
SortSession(){
|
|
||||||
}
|
|
||||||
|
|
||||||
//connects data with a sorter and controls it through a timer
|
|
||||||
void add_session(std::shared_ptr<SortInput> input, std::string sorttype){
|
|
||||||
if(sorttype == "bsort"){
|
|
||||||
sortsessions.push_back(make_unique<SteppedBsort>(input));
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
//sfml/render
|
//sfml/render
|
||||||
struct ViewSession{
|
struct ViewSession{
|
||||||
|
|||||||
+4
-5
@@ -1,4 +1,5 @@
|
|||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include "sorters.hpp"
|
#include "sorters.hpp"
|
||||||
@@ -7,10 +8,10 @@ using namespace std;
|
|||||||
|
|
||||||
SteppedBsort::SteppedBsort(shared_ptr<SortInput> input): data(input) {};
|
SteppedBsort::SteppedBsort(shared_ptr<SortInput> input): data(input) {};
|
||||||
|
|
||||||
bool SteppedBsort:: step(){
|
void SteppedBsort:: step(){
|
||||||
if(is_finished==true) {
|
cout << "step in SteppedBsort\n";
|
||||||
|
if(is_finished) {
|
||||||
printf("finished");
|
printf("finished");
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
if(data->values[j]>data->values[j+1])
|
if(data->values[j]>data->values[j+1])
|
||||||
{
|
{
|
||||||
@@ -25,6 +26,4 @@ bool SteppedBsort:: step(){
|
|||||||
if (i==data->values.size()-1){
|
if (i==data->values.size()-1){
|
||||||
is_finished=true;
|
is_finished=true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-1
@@ -6,6 +6,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "Invoker.hpp"
|
||||||
#include "steppable.hpp"
|
#include "steppable.hpp"
|
||||||
|
|
||||||
//ground truth domain data
|
//ground truth domain data
|
||||||
@@ -37,7 +38,13 @@ public:
|
|||||||
|
|
||||||
SteppedBsort(std::shared_ptr<SortInput> data);
|
SteppedBsort(std::shared_ptr<SortInput> data);
|
||||||
|
|
||||||
bool step();
|
void step();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+6
-2
@@ -14,6 +14,7 @@
|
|||||||
#include "core/app.hpp"
|
#include "core/app.hpp"
|
||||||
#include "sessions.hpp"
|
#include "sessions.hpp"
|
||||||
#include "sorters.hpp"
|
#include "sorters.hpp"
|
||||||
|
#include "timer.hpp"
|
||||||
|
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@@ -28,11 +29,14 @@ int main() {
|
|||||||
auto myinput2 = make_shared<SortInput>("arr2", dataprovider());
|
auto myinput2 = make_shared<SortInput>("arr2", dataprovider());
|
||||||
|
|
||||||
auto myviewsession = make_unique<ViewSession>();
|
auto myviewsession = make_unique<ViewSession>();
|
||||||
|
auto mytimermanager = make_unique<TimerManager>();
|
||||||
|
|
||||||
//create app with initialized dependencies
|
//create app with initialized dependencies
|
||||||
App myapp(std::move(myviewsession));
|
App myapp(std::move(myviewsession),std::move(mytimermanager));
|
||||||
|
|
||||||
//add and connect data
|
//add and connect data
|
||||||
myapp.setup_session(myinput, "bsort");
|
myapp.add_sort(myinput, "bsort");
|
||||||
|
|
||||||
//actually start the app
|
//actually start the app
|
||||||
myapp.run();
|
myapp.run();
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -8,7 +8,7 @@ public:
|
|||||||
virtual ~Steppable() = default;
|
virtual ~Steppable() = default;
|
||||||
bool is_finished = 0;
|
bool is_finished = 0;
|
||||||
size_t stepcount = 0;
|
size_t stepcount = 0;
|
||||||
virtual bool step() = 0;
|
virtual void step() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+12
-11
@@ -1,33 +1,34 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <functional>
|
#include <iostream>
|
||||||
#include <utility>
|
#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, function<bool()> myaction, milliseconds triggerinterval)
|
Timer::Timer(size_t triggeramount, std::unique_ptr<Invoker> invoker, milliseconds triggerinterval)
|
||||||
:triggeramount(triggeramount)
|
:triggeramount(triggeramount)
|
||||||
, callback(myaction)
|
, invoker(std::move(invoker))
|
||||||
, triggerinterval(triggerinterval){}
|
, 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;
|
||||||
callback();
|
cout << "request in timer\n";
|
||||||
|
invoker->request();
|
||||||
triggercount++;
|
triggercount++;
|
||||||
if (!isinfinite && triggercount >= triggeramount){
|
if (triggeramount != 0 && triggercount >= triggeramount){
|
||||||
isexpired=true;
|
isexpired=true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
void TimerManager::add_timer(size_t triggeramount, function<bool()> myaction, std::chrono::milliseconds triggerinterval) {
|
void TimerManager::add_timer(Timer timer){
|
||||||
Timer mynewtimer(triggeramount, myaction, triggerinterval);
|
timers.push_back(std::move(timer));
|
||||||
timers.push_back(std::move(mynewtimer));
|
}
|
||||||
};
|
|
||||||
|
|
||||||
void TimerManager::update_timers() {
|
void TimerManager::update_timers() {
|
||||||
steady_clock::time_point currenttime =
|
steady_clock::time_point currenttime =
|
||||||
|
|||||||
+6
-7
@@ -3,22 +3,20 @@
|
|||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <functional>
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#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 {
|
class Timer {
|
||||||
public:
|
public:
|
||||||
size_t triggeramount;
|
size_t triggeramount;
|
||||||
bool isexpired = false;
|
bool isexpired = false;
|
||||||
bool isinfinite = false;
|
|
||||||
size_t triggercount = 0;
|
size_t triggercount = 0;
|
||||||
|
std::unique_ptr<Invoker> invoker;
|
||||||
|
|
||||||
std::function<bool()> callback;
|
Timer(size_t triggeramount, std::unique_ptr<Invoker> invoker, std::chrono::milliseconds triggerinterval);
|
||||||
|
|
||||||
Timer(size_t triggeramount, std::function<bool()> myaction, std::chrono::milliseconds triggerinterval);
|
|
||||||
|
|
||||||
void add_time(std::chrono::milliseconds deltatime);
|
void add_time(std::chrono::milliseconds deltatime);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -32,7 +30,8 @@ class TimerManager {
|
|||||||
public:
|
public:
|
||||||
std::vector<Timer> timers;
|
std::vector<Timer> timers;
|
||||||
TimerManager(){previoustime = std::chrono::steady_clock::now();}
|
TimerManager(){previoustime = std::chrono::steady_clock::now();}
|
||||||
void add_timer(size_t triggeramount, std::function<bool()> myaction, std::chrono::milliseconds triggerinterval);
|
//void add_timer(size_t triggeramount, std::function<bool()> myaction, std::chrono::milliseconds triggerinterval);
|
||||||
|
void add_timer(Timer timer);
|
||||||
void delete_timer();
|
void delete_timer();
|
||||||
void update_timers();
|
void update_timers();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user