diff --git a/src/Invoker.hpp b/src/Invoker.hpp index d2c496a..d5cb70b 100644 --- a/src/Invoker.hpp +++ b/src/Invoker.hpp @@ -4,15 +4,15 @@ #include "steppable.hpp" #include class Invoker { -public: - std::weak_ptr target; + public: + std::weak_ptr target; - Invoker(std::shared_ptr t) { target = t;} - void request() { - if (auto sp = target.lock()) { - sp->step(); - } - } + Invoker(std::shared_ptr t) { target = t; } + void request() { + if (auto sp = target.lock()) { + sp->step(); + } + } }; #endif diff --git a/src/core/app.cpp b/src/core/app.cpp index 049b210..b56765e 100644 --- a/src/core/app.cpp +++ b/src/core/app.cpp @@ -9,56 +9,55 @@ using namespace std; bool displaySortInput(shared_ptr input) { - cout << "name: " + input->name + " values: "; - for (size_t i = 0; i < input->values.size(); ++i) { - printf("%d | ", input->values[i]); - } - printf("\n"); - return false; + cout << "name: " + input->name + " values: "; + + for (size_t i = 0; i < input->values.size(); ++i) { + printf("%d | ", input->values[i]); + } + printf("\n"); + return false; } string arraytostr(shared_ptr input) { - string myoutput; - ostringstream stringrep; - for (size_t i = 0; i < input->values.size(); ++i) { - stringrep << input->values[i] << " | "; - } - return stringrep.str(); + string myoutput; + ostringstream stringrep; + for (size_t i = 0; i < input->values.size(); ++i) { + stringrep << input->values[i] << " | "; + } + return stringrep.str(); } App::App(unique_ptr myviewsession, unique_ptr mytimermanager) - : viewsession(std::move(myviewsession)), - timermanager(std::move(mytimermanager)) {} + : viewsession(std::move(myviewsession)), timermanager(std::move(mytimermanager)) {} -//just pushes to App for now +// just pushes to App for now void App::add_sort(shared_ptr myinput, SortBinding mysortbinding) { - cout << "adding sort " << myinput->name << "\n"; - //insert data - inputs.push_back(myinput); - //move the sort into app - sortbindings.push_back(mysortbinding); + cout << "adding sort " << myinput->name << "\n"; + // insert data + inputs.push_back(myinput); + // move the sort into app + sortbindings.push_back(mysortbinding); } void App::run() { - // set up ui - while (viewsession->mywindow.isOpen()) { - while (const optional event = viewsession->mywindow.pollEvent()) { - if (event->is()) - viewsession->mywindow.close(); - } + // set up ui + while (viewsession->mywindow.isOpen()) { + while (const optional event = viewsession->mywindow.pollEvent()) { + if (event->is()) viewsession->mywindow.close(); + } - // draw - viewsession->mywindow.clear(sf::Color::Black); - viewsession->mywindow.draw(viewsession->text); - viewsession->text.setString(arraytostr(inputs[0])); + // draw + viewsession->mywindow.clear(sf::Color::Black); + viewsession->mywindow.draw(viewsession->text); + viewsession->text.setString(arraytostr(inputs[0])); - for (shared_ptr input : inputs) { - displaySortInput(input); - } - viewsession->mywindow.display(); + for (shared_ptr input : inputs) { + displaySortInput(input); + } + viewsession->mywindow.display(); - // update logic - timermanager->update_timers(); - this_thread::sleep_for(std::chrono::milliseconds(250)); - } + // update logic + timermanager->update_timers(); + this_thread::sleep_for(std::chrono::milliseconds(250)); + } } diff --git a/src/core/app.hpp b/src/core/app.hpp index 834cc1e..3e5d092 100644 --- a/src/core/app.hpp +++ b/src/core/app.hpp @@ -1,29 +1,28 @@ #ifndef APP_HPP #define APP_HPP - #include #include #include #include #include -#include "../timer.hpp" -#include "../sorters.hpp" #include "../sessions.hpp" +#include "../sorters.hpp" +#include "../timer.hpp" -//should connect the different parts -//for now data, mutation, and rendering -struct App{ - std::vector> inputs; - std::vector sortbindings; - std::unique_ptr viewsession; - std::unique_ptr timermanager; +// should connect the different parts +// for now data, mutation, and rendering +struct App { + std::vector> inputs; + std::vector sortbindings; + std::unique_ptr viewsession; + std::unique_ptr timermanager; - App(std::unique_ptr myviewsession, std::unique_ptr mytimermanager); + App(std::unique_ptr myviewsession, std::unique_ptr mytimermanager); - void add_sort(std::shared_ptr myinput, SortBinding mysortbinding); - void run(); + void add_sort(std::shared_ptr myinput, SortBinding mysortbinding); + void run(); }; #endif diff --git a/src/sessions.cpp b/src/sessions.cpp index 375f5b5..e082e3a 100644 --- a/src/sessions.cpp +++ b/src/sessions.cpp @@ -2,19 +2,19 @@ using namespace std; -ViewSession::ViewSession(): mywindow(sf::VideoMode({800,600}), "mywindow"), myfont(load_font("assets/NS-R.ttf")), text(myfont) -{ - text.setFont(myfont); - text.setString("Hello world"); - text.setCharacterSize(24); - text.setFillColor(sf::Color::Red); +ViewSession::ViewSession() + : mywindow(sf::VideoMode({800, 600}), "mywindow"), myfont(load_font("assets/NS-R.ttf")), text(myfont) { + text.setFont(myfont); + text.setString("Hello world"); + text.setCharacterSize(24); + text.setFillColor(sf::Color::Red); }; -sf::Font ViewSession::load_font(const std::string &filename){ - sf::Font font; - if (!font.openFromFile(filename)){ - fprintf(stderr, "failed to load font"); - std::exit(1); - } - return font; +sf::Font ViewSession::load_font(const std::string &filename) { + sf::Font font; + if (!font.openFromFile(filename)) { + fprintf(stderr, "failed to load font"); + std::exit(1); + } + return font; } diff --git a/src/sessions.hpp b/src/sessions.hpp index c6acaab..a2377b7 100644 --- a/src/sessions.hpp +++ b/src/sessions.hpp @@ -8,16 +8,16 @@ #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 -//sfml/render -struct ViewSession{ - sf::RenderWindow mywindow; - sf::Font myfont; - sf::Text text; +// sfml/render +struct ViewSession { + sf::RenderWindow mywindow; + sf::Font myfont; + sf::Text text; - ViewSession(); - sf::Font load_font(const std::string &filename); + ViewSession(); + sf::Font load_font(const std::string &filename); }; #endif diff --git a/src/sorters.cpp b/src/sorters.cpp index 3734714..fb39663 100644 --- a/src/sorters.cpp +++ b/src/sorters.cpp @@ -1,41 +1,39 @@ +#include "sorters.hpp" #include #include #include #include -#include "sorters.hpp" using namespace std; +SteppedBsort::SteppedBsort(shared_ptr input) : data(input) {}; -SteppedBsort::SteppedBsort(shared_ptr input) - : data(input) {}; +void SteppedBsort::step() { + cout << "step in SteppedBsort\n"; + if (is_finished) { + printf("finished"); + } + if (data->values[j] > data->values[j + 1]) { + swap(data->values[j], data->values[j + 1]); + } + j++; + if (j >= data->values.size() - 1 - i) { + j = 0; + i++; + } -void SteppedBsort:: step(){ - cout << "step in SteppedBsort\n"; - if(is_finished) { - printf("finished"); - } - if(data->values[j]>data->values[j+1]) - { - swap(data->values[j], data->values[j+1]); - } - j++; - if(j>=data->values.size()-1 -i){ - j=0; - i++; - } - - if (i==data->values.size()-1){ - is_finished=true; - } + if (i == data->values.size() - 1) { + is_finished = true; + } } -SortBinding::SortBinding(std::shared_ptr input, std::shared_ptr sort, std::shared_ptr invoker) - : input(std::move(input)), sort(std::move(sort)), invoker(std::move(invoker)){} +SortBinding::SortBinding( + std::shared_ptr input, std::shared_ptr sort, std::shared_ptr invoker) + : input(std::move(input)), sort(std::move(sort)), invoker(std::move(invoker)) {} -//connects data with the correct sort +// connects data with the correct sort std::shared_ptr make_sort(std::shared_ptr input, std::string sorttype) { - if (sorttype == "bsort") { - return make_shared(input); - } - return nullptr; + if (sorttype == "bsort") { + return make_shared(input); + } + return nullptr; } diff --git a/src/sorters.hpp b/src/sorters.hpp index e5972d7..e751896 100644 --- a/src/sorters.hpp +++ b/src/sorters.hpp @@ -9,45 +9,43 @@ #include "Invoker.hpp" #include "steppable.hpp" -//ground truth domain data +// ground truth domain data struct SortInput { - std::string name; - std::vector values; + std::string name; + std::vector values; - SortInput(std::string name, std::vector data) - : name(name), values(data) {} + SortInput(std::string name, std::vector data) : name(name), values(data) {} }; -//ro rendering of data +// ro rendering of data struct SortView { - std::shared_ptr values; - size_t currenttarget = 0; - int x,y; - int color; - SortView(std::shared_ptr in): values(in) {} + std::shared_ptr values; + size_t currenttarget = 0; + int x, y; + int color; + SortView(std::shared_ptr in) : values(in) {} }; -//mutates the domain data -class SteppedBsort: public Steppable { -public: - std::shared_ptr data; - size_t stepcount = 0; - size_t i = 0; - size_t j = 0; - bool is_finished = false; +// mutates the domain data +class SteppedBsort : public Steppable { + public: + std::shared_ptr data; + size_t stepcount = 0; + size_t i = 0; + size_t j = 0; + bool is_finished = false; - SteppedBsort(std::shared_ptr data); + SteppedBsort(std::shared_ptr data); - void step(); + void step(); }; -//working unit +// working unit struct SortBinding { - std::shared_ptr input; - std::shared_ptr sort; - std::shared_ptr invoker; - SortBinding(std::shared_ptr input, std::shared_ptr sort, std::shared_ptr invoker); - + std::shared_ptr input; + std::shared_ptr sort; + std::shared_ptr invoker; + SortBinding(std::shared_ptr input, std::shared_ptr sort, std::shared_ptr invoker); }; std::shared_ptr make_sort(std::shared_ptr input, std::string sorttype); diff --git a/src/sortui.cpp b/src/sortui.cpp index 0ddf580..463f6cd 100644 --- a/src/sortui.cpp +++ b/src/sortui.cpp @@ -4,13 +4,13 @@ #include #include +#include #include #include #include #include -#include #include -#include +#include #include "core/app.hpp" #include "sessions.hpp" @@ -21,34 +21,30 @@ using namespace std::chrono; using namespace std; -vector dataprovider(){ - return vector {9,8,1,2,7,3,6,4,5}; -} +vector dataprovider() { return vector{9, 8, 1, 2, 7, 3, 6, 4, 5}; } int main() { - //create data - auto myinput = make_shared("arr1", dataprovider()); - auto myinput2 = make_shared("arr2", dataprovider()); + // create data + auto myinput = make_shared("arr1", dataprovider()); + auto myinput2 = make_shared("arr2", dataprovider()); - auto myviewsession = make_unique(); - auto mytimermanager = make_unique(); + auto myviewsession = make_unique(); + auto mytimermanager = make_unique(); - //create app with initialized dependencies - App myapp(std::move(myviewsession),std::move(mytimermanager)); + // create app with initialized dependencies + App myapp(std::move(myviewsession), std::move(mytimermanager)); - //create binding - shared_ptr asort = make_sort(myinput, "bsort"); - shared_ptr myinvoker = make_shared(asort); - SortBinding mysortbinding(myinput, asort, myinvoker); + // create binding + shared_ptr asort = make_sort(myinput, "bsort"); + shared_ptr myinvoker = make_shared(asort); + SortBinding mysortbinding(myinput, asort, myinvoker); - Timer timer(0, myinvoker, chrono::milliseconds(250)); - myapp.timermanager->add_timer(std::move(timer)); + Timer timer(0, myinvoker, chrono::milliseconds(250)); + myapp.timermanager->add_timer(std::move(timer)); - //add data and binding - myapp.add_sort(myinput, mysortbinding); + // add data and binding + myapp.add_sort(myinput, mysortbinding); - - - //actually start the app - myapp.run(); + // actually start the app + myapp.run(); } diff --git a/src/steppable.hpp b/src/steppable.hpp index 27c67bc..20cbef0 100644 --- a/src/steppable.hpp +++ b/src/steppable.hpp @@ -4,11 +4,11 @@ #include class Steppable { -public: - virtual ~Steppable() = default; - bool is_finished = 0; - size_t stepcount = 0; - virtual void step() = 0; + public: + virtual ~Steppable() = default; + bool is_finished = 0; + size_t stepcount = 0; + virtual void step() = 0; }; #endif diff --git a/src/timer.cpp b/src/timer.cpp index 6082a27..6bd2b05 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -2,42 +2,37 @@ #include #include -#include "timer.hpp" #include "Invoker.hpp" +#include "timer.hpp" using namespace std::chrono; using namespace std; Timer::Timer(size_t triggeramount, std::weak_ptr invoker, milliseconds triggerinterval) - :triggeramount(triggeramount) - , invoker(invoker) - , triggerinterval(triggerinterval){} + : triggeramount(triggeramount), invoker(invoker), triggerinterval(triggerinterval) {} void Timer::add_time(milliseconds deltatime) { - accumulator += deltatime; - if (accumulator >= triggerinterval) { - accumulator -= triggerinterval; - cout << "request in timer\n"; - if (auto sp = invoker.lock()) { - sp->request(); - } - triggercount++; - if (triggeramount != 0 && triggercount >= triggeramount){ - isexpired=true; - } - } + accumulator += deltatime; + if (accumulator >= triggerinterval) { + accumulator -= triggerinterval; + cout << "request in timer\n"; + if (auto sp = invoker.lock()) { + sp->request(); + } + triggercount++; + if (triggeramount != 0 && triggercount >= triggeramount) { + isexpired = true; + } + } } -void TimerManager::add_timer(Timer timer){ - timers.push_back(std::move(timer)); -} +void TimerManager::add_timer(Timer timer) { timers.push_back(std::move(timer)); } void TimerManager::update_timers() { - steady_clock::time_point currenttime = - steady_clock::now(); - milliseconds deltatime = duration_cast(currenttime-previoustime); - for(Timer &mytimer: timers){ - mytimer.add_time(deltatime); - } - previoustime = currenttime; + steady_clock::time_point currenttime = steady_clock::now(); + milliseconds deltatime = duration_cast(currenttime - previoustime); + for (Timer &mytimer : timers) { + mytimer.add_time(deltatime); + } + previoustime = currenttime; } diff --git a/src/timer.hpp b/src/timer.hpp index 35b8e8e..cd37126 100644 --- a/src/timer.hpp +++ b/src/timer.hpp @@ -1,42 +1,41 @@ #ifndef TIMER_HPP #define TIMER_HPP +#include #include #include -#include #include #include #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: - size_t triggeramount; - bool isexpired = false; - size_t triggercount = 0; - std::weak_ptr invoker; //if no longer exists, then should clean up timer + public: + size_t triggeramount; + bool isexpired = false; + size_t triggercount = 0; + std::weak_ptr invoker; // if no longer exists, then should clean up timer - Timer(size_t triggeramount, std::weak_ptr invoker, std::chrono::milliseconds triggerinterval); - void add_time(std::chrono::milliseconds deltatime); + Timer(size_t triggeramount, std::weak_ptr invoker, std::chrono::milliseconds triggerinterval); + void add_time(std::chrono::milliseconds deltatime); -private: - std::chrono::milliseconds accumulator = std::chrono::milliseconds(0); - std::chrono::milliseconds triggerinterval = std::chrono::milliseconds(1000); //default to 1 sec + private: + std::chrono::milliseconds accumulator = std::chrono::milliseconds(0); + std::chrono::milliseconds triggerinterval = std::chrono::milliseconds(1000); // default to 1 sec }; - -//CRUD timers and give them dt on update +// CRUD timers and give them dt on update class TimerManager { -public: - std::vector timers; - TimerManager(){previoustime = std::chrono::steady_clock::now();} - //void add_timer(size_t triggeramount, std::function myaction, std::chrono::milliseconds triggerinterval); - void add_timer(Timer timer); - void delete_timer(); - void update_timers(); + public: + std::vector timers; + TimerManager() { previoustime = std::chrono::steady_clock::now(); } + // void add_timer(size_t triggeramount, std::function myaction, std::chrono::milliseconds triggerinterval); + void add_timer(Timer timer); + void delete_timer(); + void update_timers(); -private: - std::chrono::steady_clock::time_point previoustime; + private: + std::chrono::steady_clock::time_point previoustime; }; #endif