[gnote] Templatisize PropertyEditorBase and use move semantics



commit 1cdb3517cc377118596664b96c98e4b59b631074
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Thu Dec 31 16:08:11 2020 +0200

    Templatisize PropertyEditorBase and use move semantics

 src/sharp/propertyeditor.cpp | 24 ++++++++++--------------
 src/sharp/propertyeditor.hpp | 33 +++++++++++++++++----------------
 2 files changed, 27 insertions(+), 30 deletions(-)
---
diff --git a/src/sharp/propertyeditor.cpp b/src/sharp/propertyeditor.cpp
index 1f0df973..de71df02 100644
--- a/src/sharp/propertyeditor.cpp
+++ b/src/sharp/propertyeditor.cpp
@@ -33,28 +33,26 @@
 namespace sharp {
 
 
-  PropertyEditorBase::PropertyEditorBase(Gtk::Widget &w)
+  template <typename GetterT, typename SetterT>
+  PropertyEditorBase<GetterT, SetterT>::PropertyEditorBase(GetterT && getter, SetterT && setter, Gtk::Widget 
& w)
     : m_widget(w)
+    , m_getter(std::move(getter))
+    , m_setter(std::move(setter))
   {
     w.set_data(Glib::Quark("sharp::property-editor"), (gpointer)this,
                &PropertyEditorBase::destroy_notify);
   }
 
-  PropertyEditorBase::~PropertyEditorBase()
-  {
-  }
-
-  void PropertyEditorBase::destroy_notify(gpointer data)
+  template <typename GetterT, typename SetterT>
+  void PropertyEditorBase<GetterT, SetterT>::destroy_notify(gpointer data)
   {
     PropertyEditorBase * self = (PropertyEditorBase*)data;
     delete self;
   }
 
 
-  PropertyEditor::PropertyEditor(GetterT getter, SetterT setter, Gtk::Entry &entry)
-    : PropertyEditorBase(entry)
-    , m_getter(getter)
-    , m_setter(setter)
+  PropertyEditor::PropertyEditor(StringPropertyGetterT && getter, StringPropertySetterT && setter, 
Gtk::Entry &entry)
+    : PropertyEditorBase(std::move(getter), std::move(setter), entry)
   {
     m_connection = entry.property_text().signal_changed().connect(
       sigc::mem_fun(*this, &PropertyEditor::on_changed));
@@ -74,10 +72,8 @@ namespace sharp {
   }
 
 
-  PropertyEditorBool::PropertyEditorBool(GetterT getter, SetterT setter, Gtk::ToggleButton &button)
-    : PropertyEditorBase(button)
-    , m_getter(getter)
-    , m_setter(setter)
+  PropertyEditorBool::PropertyEditorBool(BoolPropertyGetterT && getter, BoolPropertySetterT && setter, 
Gtk::ToggleButton &button)
+    : PropertyEditorBase(std::move(getter), std::move(setter), button)
   {
     m_connection = button.property_active().signal_changed().connect(
       sigc::mem_fun(*this, &PropertyEditorBool::on_changed));
diff --git a/src/sharp/propertyeditor.hpp b/src/sharp/propertyeditor.hpp
index 0a0f1713..2f6365b3 100644
--- a/src/sharp/propertyeditor.hpp
+++ b/src/sharp/propertyeditor.hpp
@@ -38,46 +38,49 @@
 
 namespace sharp {
 
+  template <typename GetterT, typename SetterT>
   class PropertyEditorBase
   {
   public:
-    virtual ~PropertyEditorBase();
+    virtual ~PropertyEditorBase() = default;
     virtual void setup() = 0;
 
   protected:
-    explicit PropertyEditorBase(Gtk::Widget & w);
+    PropertyEditorBase(GetterT && getter, SetterT && setter, Gtk::Widget & w);
 
     Gtk::Widget &m_widget;
     sigc::connection m_connection;
+    GetterT m_getter;
+    SetterT m_setter;
   private:
     void static destroy_notify(gpointer data);
   };
 
+
+  typedef std::function<Glib::ustring()> StringPropertyGetterT;
+  typedef std::function<void(const Glib::ustring&)> StringPropertySetterT;
+
   class PropertyEditor
-      : public PropertyEditorBase
+      : public PropertyEditorBase<StringPropertyGetterT, StringPropertySetterT>
   {
   public:
-    typedef std::function<Glib::ustring()> GetterT;
-    typedef std::function<void(const Glib::ustring&)> SetterT;
-
-    PropertyEditor(GetterT getter, SetterT setter, Gtk::Entry &entry);
+    PropertyEditor(StringPropertyGetterT && getter, StringPropertySetterT && setter, Gtk::Entry &entry);
 
     virtual void setup() override;
 
   private:
     void on_changed();
-    GetterT m_getter;
-    SetterT m_setter;
   };
 
+
+  typedef std::function<bool()> BoolPropertyGetterT;
+  typedef std::function<void(bool)> BoolPropertySetterT;
+
   class PropertyEditorBool
-    : public PropertyEditorBase
+    : public PropertyEditorBase<BoolPropertyGetterT, BoolPropertySetterT>
   {
   public:
-    typedef std::function<bool()> GetterT;
-    typedef std::function<void(bool)> SetterT;
-
-    PropertyEditorBool(GetterT getter, SetterT setter, Gtk::ToggleButton &button);
+    PropertyEditorBool(BoolPropertyGetterT && getter, BoolPropertySetterT && setter, Gtk::ToggleButton 
&button);
     void add_guard(Gtk::Widget* w)
       {
         m_guarded.push_back(w);
@@ -89,8 +92,6 @@ namespace sharp {
     void guard(bool v);
     void on_changed();
     std::vector<Gtk::Widget*> m_guarded;
-    GetterT m_getter;
-    SetterT m_setter;
   };
 
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]