From 205a4dd0fa437c1bdff7305aaee297ec929f237d Mon Sep 17 00:00:00 2001
From: Benny Baumann <BenBE@geshi.org>
Date: Sat, 16 Mar 2019 16:40:13 +0100
Subject: [PATCH] fix: Code style, const correctness

---
 src/ui/display.hpp            | 41 ++++++++++++++++++
 src/ui/event.cpp              | 31 +++++++------
 src/ui/event.hpp              | 82 +++++++++++++++++------------------
 src/ui/new_child_event.cpp    |  1 -
 src/ui/progress_indicator.hpp | 54 +++++++++++------------
 src/ui/ui_context.hpp         | 12 ++---
 src/ui/view.cpp               | 32 +++++++-------
 src/ui/view.hpp               | 56 ++++++------------------
 8 files changed, 161 insertions(+), 148 deletions(-)
 create mode 100644 src/ui/display.hpp

diff --git a/src/ui/display.hpp b/src/ui/display.hpp
new file mode 100644
index 0000000..20c2072
--- /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 a344a5a..94c4005 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 d7a3ed8..057421c 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 dc62d74..cf2e74a 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 4827fb9..704b20e 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 e5e2587..b716d4d 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 cdc6908..e4f1073 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 c028824..16d197e 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();
 };
 
 }
-- 
GitLab