diff --git a/src/ui/display.hpp b/src/ui/display.hpp new file mode 100644 index 0000000000000000000000000000000000000000..20c20729a49d9e5787c8a45efe1585eee6159e5d --- /dev/null +++ b/src/ui/display.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include <functional> +#include <memory> +#include <shared_mutex> + +#include "progress_indicator.hpp" +#include "ui_context.hpp" +#include "event.hpp" + +namespace rmrf::ui { + +/** + * This class is designed to be the first level adapter to curses. + * It implements some basic information handling and a registry for + * views. + */ +class display : private std::enable_shared_from_this<display> { +public: + typedef display self_type; + typedef std::shared_ptr<self_type> ptr_type; + +private: + typedef std::shared_mutex mutex_type; + typedef std::lock_guard<mutex_type> lock_type; + mutable mutex_type m; + +public: + display(); + virtual ~display(); + +public: + template<typename F, typename ...Args> + decltype(auto) sync(F &&f, Args &&... args) { + lock_type lock(m); + + return std::forward<F>(f)(shared_from_this(), std::forward<Args>(args)...); + } +}; + +} diff --git a/src/ui/event.cpp b/src/ui/event.cpp index a344a5a4dbe9482694a234f544335e1cb2b64b8a..94c40058c444b37380a38134c6bad4591b1441b9 100644 --- a/src/ui/event.cpp +++ b/src/ui/event.cpp @@ -1,24 +1,23 @@ #include "ui/event.hpp" namespace rmrf::ui { - event::event(std::shared_ptr<ui_context> sender) { - this->event_sender = sender; - } - event::~event() { - this->event_sender = nullptr; - this->handled = false; - } +event::event(const std::shared_ptr<ui_context> &sender) : event_sender(sender), handled(false) { +} + +event::~event() { +} - std::shared_ptr<ui_context> event::get_sender() { - return this->event_sender; - } +std::shared_ptr<ui_context> event::get_sender() const { + return this->event_sender; +} - bool event::has_been_handled() { - return this->handled; - } +bool event::has_been_handled() const { + return this->handled; +} + +void event::set_handled() { + this->handled = true; +} - void event::set_handled() { - this->handled = true; - } } diff --git a/src/ui/event.hpp b/src/ui/event.hpp index d7a3ed819dad61382adb2eefc361cf22808206d7..057421c2cad38095e9f247e11f12fd920511da12 100644 --- a/src/ui/event.hpp +++ b/src/ui/event.hpp @@ -9,51 +9,51 @@ namespace rmrf::ui { /** - * This class is used to share publish event updates. + * This class is used to share publish event updates. */ class event { private: - std::shared_ptr<ui_context> event_sender; - bool handled; + std::shared_ptr<ui_context> event_sender; + bool handled; public: - /** - * In order to construct an event one needs at least to specify - * the sender. - * - * @param sender The source of the event. - */ - event(std::shared_ptr<ui_context> sender); - /** - * A class implementing an event also needs to make sure that - * the special payload of the event is beeing taken care of. - */ - ~event(); - /** - * Use this methos in order to obtain the sender of this event. - * - * @return The sender - */ - std::shared_ptr<ui_context> get_sender(); - /** - * Use this function in order to get a human readable description - * of the event. - * - * This does not need to be translated due to its purpose beeing - * debugging. - * - * @return A pointer to the description string - */ - std::shared_ptr<std::string> get_event_description(); - /** - * Use this function in order to check if the event has been handled yet. - * - * @return true if the event was already dealt with or otherwise false. - */ - bool has_been_handled(); - /** - * Call this function once the purpose of the event has been taken care of. - */ - void set_handled(); + /** + * In order to construct an event one needs at least to specify + * the sender. + * + * @param sender The source of the event. + */ + explicit event(const std::shared_ptr<ui_context> &sender); + /** + * A class implementing an event also needs to make sure that + * the special payload of the event is beeing taken care of. + */ + virtual ~event(); + /** + * Use this methos in order to obtain the sender of this event. + * + * @return The sender + */ + std::shared_ptr<ui_context> get_sender() const; + /** + * Use this function in order to get a human readable description + * of the event. + * + * This does not need to be translated due to its purpose beeing + * debugging. + * + * @return A pointer to the description string + */ + std::shared_ptr<std::string> get_event_description() const; + /** + * Use this function in order to check if the event has been handled yet. + * + * @return true if the event was already dealt with or otherwise false. + */ + bool has_been_handled() const; + /** + * Call this function once the purpose of the event has been taken care of. + */ + void set_handled(); }; } diff --git a/src/ui/new_child_event.cpp b/src/ui/new_child_event.cpp index dc62d740257461cc888b6e7d4b6b5caa9121a905..cf2e74a47b45736f205822a0850da989a7200b96 100644 --- a/src/ui/new_child_event.cpp +++ b/src/ui/new_child_event.cpp @@ -2,5 +2,4 @@ namespace rmrf::ui { - } diff --git a/src/ui/progress_indicator.hpp b/src/ui/progress_indicator.hpp index 4827fb92729cd08e989adbb15fd3acbfaceee5af..704b20e972ad2a06e4843b7e83e31bdecb5906de 100644 --- a/src/ui/progress_indicator.hpp +++ b/src/ui/progress_indicator.hpp @@ -6,36 +6,36 @@ namespace rmrf::ui { /** - * This class is used to share progress information on running tasks. + * This class is used to share progress information on running tasks. */ class progress_indicator { public: - virtual ~progress_indicator(); - /** - * Query the progress state - * @return The current progress in percent - */ - virtual int get_progress(); - /** - * This method shall be used in order to obtain - * the total amount of steps to be done. - */ - virtual int get_total_work(); - /** - * This method shall be used to retrieve the - * current progress as a number of finished jobs. - */ - virtual int get_current_work(); - /** - * This method shall be used in order to obtain the - * description of the total operation. - */ - virtual std::shared_ptr<std::string> get_operation_description(); - /** - * This method shall be used in order to retrieve a - * description of the current step. - */ - virtual std::shared_ptr<std::string> get_current_job_description(); + virtual ~progress_indicator(); + /** + * Query the progress state + * @return The current progress in percent + */ + virtual int get_progress() const; + /** + * This method shall be used in order to obtain + * the total amount of steps to be done. + */ + virtual int get_total_work() const; + /** + * This method shall be used to retrieve the + * current progress as a number of finished jobs. + */ + virtual int get_current_work() const; + /** + * This method shall be used in order to obtain the + * description of the total operation. + */ + virtual std::shared_ptr<std::string> get_operation_description() const; + /** + * This method shall be used in order to retrieve a + * description of the current step. + */ + virtual std::shared_ptr<std::string> get_current_job_description() const; }; } diff --git a/src/ui/ui_context.hpp b/src/ui/ui_context.hpp index e5e25876391e5a5a4f79cad6452c42c0f6da040f..b716d4d64e1846f7fee57a32499694372b555a8d 100644 --- a/src/ui/ui_context.hpp +++ b/src/ui/ui_context.hpp @@ -11,12 +11,12 @@ namespace rmrf::ui { */ class ui_context { public: - virtual ~ui_context(); - /** - * Use this method in order to get a fully qualified debug name - * of the context. - */ - virtual std::shared_ptr<std::string> get_name(); + virtual ~ui_context(); + /** + * Use this method in order to get a fully qualified debug name + * of the context. + */ + virtual std::shared_ptr<std::string> get_name() const; }; } diff --git a/src/ui/view.cpp b/src/ui/view.cpp index cdc69087c2fe6f3b4d10db88fb6be5feefdd4b06..e4f10730a352935e1c70677823809b018de73b7e 100644 --- a/src/ui/view.cpp +++ b/src/ui/view.cpp @@ -5,30 +5,32 @@ namespace rmrf::ui { -view::view(std::shared_ptr<view> parent) : parent_view{parent} { - if(this->parent_view != nullptr) { - this->parent_view->add_child(this); - } +view::view(const std::shared_ptr<view> &parent) : parent_view{parent}, child_views{} { + if (this->parent_view) { + this->parent_view->add_child(this->shared_from_this()); + } } view::~view() { - if(this->parent_view != nullptr) { - this->parent_view->remove_child(this); - } - // Delete all childs that still exist - this->child_views.clear(); + // Delete all childs that still exist + this->child_views.clear(); + + // Notify our parent about us being destructed + if (this->parent_view) { + this->parent_view->remove_child(this->shared_from_this()); + } } -std::shared_ptr<view> view::get_parent() { - return this->parent_view; +std::shared_ptr<view> view::get_parent() const { + return this->parent_view; } -void view::add_child(std::unique_ptr<view> child) { - this->child_views.push_back(child); +void view::add_child(const std::shared_ptr<view> &child) { + this->child_views.push_back(child); } -void view::remove_child(std::unique_ptr<view> child) { - this->child_views.remove(child); +void view::remove_child(const std::shared_ptr<view> &child) { + this->child_views.remove(child); } } diff --git a/src/ui/view.hpp b/src/ui/view.hpp index c028824510e38baac954a469ab51ecb15c456783..16d197e40f802808ff85074aafd1997b1e884a19 100644 --- a/src/ui/view.hpp +++ b/src/ui/view.hpp @@ -4,51 +4,23 @@ #include <memory> #include <shared_mutex> -#include "progress_indicator.hpp" -#include "ui_context.hpp" -#include "event.hpp" +#include "ui/display.hpp" +#include "ui/event.hpp" +#include "ui/progress_indicator.hpp" +#include "ui/ui_context.hpp" namespace rmrf::ui { -/** - * This class is designed to be the first level adapter to curses. - * It implements some basic information handling and a registry for - * views. - */ -class display : public std::enable_shared_from_this<display> { -public: - typedef display self_type; - typedef std::shared_ptr<self_type> ptr_type; - -private: - typedef std::shared_mutex mutex_type; - typedef std::lock_guard<mutex_type> lock_type; - mutable mutex_type m; - -public: - display(); - ~display(); - -public: - template<typename F, typename ...Args> - decltype(auto) sync(F &&f, Args &&... args) { - lock_type lock(m); - - return std::forward<F>(f)(shared_from_this(), std::forward<Args>(args)...); - } - -}; - /** * This abstract class implements a view page. */ -class view : public ui_context { +class view : public ui_context, private std::enable_shared_from_this<view> { private: std::shared_ptr<view> parent_view; - std::list<std::unique_ptr<view>> child_views; + std::list<std::shared_ptr<view>> child_views; private: - void add_child(std::unique_ptr<view> child); - void remove_child(std::unique_ptr<view> child); + void add_child(const std::shared_ptr<view> &child); + void remove_child(const std::shared_ptr<view> &child); public: /** * This method will be called when an operation is taking place. It may add @@ -56,7 +28,7 @@ public: * * @param progress The progress_indicator from the new running task to add */ - virtual void add_progress_indicator(std::shared_ptr<progress_indicator> progress); + virtual void add_progress_indicator(const std::shared_ptr<progress_indicator> &progress); /** * This method will be called on a regular basis when the view needs to be updated @@ -68,27 +40,27 @@ public: * @param event The event that caused the update. * @return True if rerendering is required or otherwise false. */ - virtual bool update(std::shared_ptr<display> display, std::shared_ptr<event> event); + virtual bool update(const std::shared_ptr<display> &display, const std::shared_ptr<event> &event); /** * This method gets called when events need to be processed that do not * necessarily come from the UI thread. * * @param event The event that caused the update. */ - virtual void schedule_update(std::shared_ptr<event> event); + virtual void schedule_update(const std::shared_ptr<event> &event); /** * Use this method in order to retrieve the parent of this view. * @warn Keep in mind that this might be null. * @return The parent */ - std::shared_ptr<view> get_parent(); + std::shared_ptr<view> get_parent() const; /** * This constructor shall be capable of creating the view. * * @param parent The parent view of this view. This may be null if there is none. */ - view(std::shared_ptr<view> parent); - ~view(); + explicit view(const std::shared_ptr<view> &parent); + virtual ~view(); }; }