[glom] Use Glib::WeakRef instead of Glib::RefPtr with sigc::bind().



commit d60a3ed6f2add0f22cdef872196bb91405396430
Author: Murray Cumming <murrayc murrayc com>
Date:   Fri Sep 30 09:55:38 2016 +0200

    Use Glib::WeakRef instead of Glib::RefPtr with sigc::bind().
    
    As suggested in Herb Sutter's 2016 CppCon talk, for parameters
    captured for lambdas:
    https://www.youtube.com/watch?v=JfmTagWcqoE
    
    I don't much like that this is a change in the signature of the handler,
    which isn't obvious from the code that connects the handler.

 glom/import_csv/csv_parser.cc                      |   14 +++++-
 glom/import_csv/csv_parser.h                       |    6 ++-
 .../window_relationships_overview.cc               |   41 ++++++++------------
 .../window_relationships_overview.h                |   10 ++---
 .../report_layout/dialog_layout_report.cc          |   14 +++++-
 .../report_layout/dialog_layout_report.h           |    5 +-
 glom/print_layout/canvas_print_layout.cc           |    6 ++-
 glom/print_layout/canvas_print_layout.h            |    3 +-
 8 files changed, 56 insertions(+), 43 deletions(-)
---
diff --git a/glom/import_csv/csv_parser.cc b/glom/import_csv/csv_parser.cc
index ccd43e8..97f208f 100644
--- a/glom/import_csv/csv_parser.cc
+++ b/glom/import_csv/csv_parser.cc
@@ -497,8 +497,12 @@ void CsvParser::ensure_idle_handler_connection()
   }
 }
 
-void CsvParser::on_file_read(const Glib::RefPtr<Gio::AsyncResult>& result, const Glib::RefPtr<Gio::File>& 
source)
+void CsvParser::on_file_read(const Glib::RefPtr<Gio::AsyncResult>& result, const Glib::WeakRef<Gio::File>& 
source_weak)
 {
+  const auto source = source_weak.get();
+  if (!source)
+    return;
+
   ensure_idle_handler_connection();
 
   try
@@ -549,11 +553,15 @@ void CsvParser::on_buffer_read(const Glib::RefPtr<Gio::AsyncResult>& result)
   }
 }
 
-void CsvParser::on_file_query_info(const Glib::RefPtr<Gio::AsyncResult>& result, const 
Glib::RefPtr<Gio::File>& source) const
+void CsvParser::on_file_query_info(const Glib::RefPtr<Gio::AsyncResult>& result, const 
Glib::WeakRef<Gio::File>& source_weak) const
 {
+  const auto source = source_weak.get();
+  if (!source)
+    return;
+
   try
   {
-    auto info = source->query_info_finish(result);
+    const auto info = source->query_info_finish(result);
     if(info)
       signal_have_display_name().emit(info->get_display_name());
   }
diff --git a/glom/import_csv/csv_parser.h b/glom/import_csv/csv_parser.h
index a7651c1..4dd9265 100644
--- a/glom/import_csv/csv_parser.h
+++ b/glom/import_csv/csv_parser.h
@@ -29,6 +29,7 @@
 #include <giomm/file.h>
 #include <giomm/inputstream.h>
 //#include <gtkmm/liststore.h>
+#include <glibmm/weakref.h>
 
 namespace Glom
 {
@@ -169,10 +170,11 @@ private:
 
   void ensure_idle_handler_connection();
 
-  void on_file_read(const Glib::RefPtr<Gio::AsyncResult>& result, const Glib::RefPtr<Gio::File>& source);
   void copy_buffer_and_continue_reading(gssize size);
+
+  void on_file_read(const Glib::RefPtr<Gio::AsyncResult>& result, const Glib::WeakRef<Gio::File>& source);
   void on_buffer_read(const Glib::RefPtr<Gio::AsyncResult>& result);
-  void on_file_query_info(const Glib::RefPtr<Gio::AsyncResult>& result, const Glib::RefPtr<Gio::File>& 
source) const;
+  void on_file_query_info(const Glib::RefPtr<Gio::AsyncResult>& result, const Glib::WeakRef<Gio::File>& 
source) const;
 
   void set_state(State state);
 
diff --git a/glom/mode_design/relationships_overview/window_relationships_overview.cc 
b/glom/mode_design/relationships_overview/window_relationships_overview.cc
index fde97c8..6fdb48b 100644
--- a/glom/mode_design/relationships_overview/window_relationships_overview.cc
+++ b/glom/mode_design/relationships_overview/window_relationships_overview.cc
@@ -120,16 +120,6 @@ Window_RelationshipsOverview::~Window_RelationshipsOverview()
 {
   get_size(m_last_size_x, m_last_size_y);
 
-  //Disconnect signal handlers for all current items,
-  //so sigc::bind()ed RefPtrs can be released.
-  while(m_list_table_connections.size())
-  {
-     auto iter = m_list_table_connections.begin();
-     sigc::connection the_connection = *iter;
-     the_connection.disconnect();
-     m_list_table_connections.erase(iter);
-  }
-
   //Remove all current items:
   //while(m_group_tables->get_n_children() > 0)
   //  m_group_tables->remove_child(0);
@@ -138,16 +128,6 @@ Window_RelationshipsOverview::~Window_RelationshipsOverview()
 
 void Window_RelationshipsOverview::draw_tables()
 {
-  //Disconnect signal handlers for all current items,
-  //so sigc::bind()ed RefPtrs can be released.
-  while(!m_list_table_connections.empty())
-  {
-     auto iter = m_list_table_connections.begin();
-     sigc::connection the_connection = *iter;
-     the_connection.disconnect();
-     m_list_table_connections.erase(iter);
-  }
-
   //Remove all current items:
   while(m_group_tables->get_n_children() > 0)
     m_group_tables->remove_child(0);
@@ -186,10 +166,9 @@ void Window_RelationshipsOverview::draw_tables()
       table_group->signal_moved().connect(
         sigc::mem_fun(*this, &Window_RelationshipsOverview::on_table_moved));
 
-      sigc::connection the_connection = table_group->signal_show_context().connect( sigc::bind(
+      table_group->signal_show_context().connect( sigc::bind(
         sigc::mem_fun(*this, &Window_RelationshipsOverview::on_table_show_context),
         table_group) );
-      m_list_table_connections.emplace_back(the_connection);
 
       //tv->x2 = tv->x1 + table_width;
       //tv->y2 = tv->y1 + table_height;
@@ -427,8 +406,12 @@ void Window_RelationshipsOverview::on_table_moved(const Glib::RefPtr<CanvasItemM
   draw_lines();
 }
 
-void Window_RelationshipsOverview::on_table_show_context(guint button, guint32 activate_time, 
Glib::RefPtr<CanvasGroupDbTable> table)
+void Window_RelationshipsOverview::on_table_show_context(guint button, guint32 activate_time, const 
Glib::WeakRef<CanvasGroupDbTable>& table_weak)
 {
+  const auto table = table_weak.get();
+  if (!table)
+    return;
+
   if(m_action_edit_fields)
   {
     // Disconnect the previous handler,
@@ -467,8 +450,12 @@ void Window_RelationshipsOverview::setup_context_menu()
   m_context_menu->attach_to_widget(*this);
 }
 
-void Window_RelationshipsOverview::on_context_menu_edit_fields(const Glib::VariantBase& /* parameter */, 
Glib::RefPtr<CanvasGroupDbTable> table)
+void Window_RelationshipsOverview::on_context_menu_edit_fields(const Glib::VariantBase& /* parameter */, 
const Glib::WeakRef<CanvasGroupDbTable>& table_weak)
 {
+  const auto table = table_weak.get();
+  if (!table)
+    return;
+
   auto pApp = AppWindow::get_appwindow();
   if(pApp && table)
   {
@@ -478,8 +465,12 @@ void Window_RelationshipsOverview::on_context_menu_edit_fields(const Glib::Varia
   }
 }
 
-void Window_RelationshipsOverview::on_context_menu_edit_relationships(const Glib::VariantBase& /* parameter 
*/, Glib::RefPtr<CanvasGroupDbTable> table)
+void Window_RelationshipsOverview::on_context_menu_edit_relationships(const Glib::VariantBase& /* parameter 
*/, const Glib::WeakRef<CanvasGroupDbTable>& table_weak)
 {
+  const auto table = table_weak.get();
+  if (!table)
+    return;
+
   auto pApp = AppWindow::get_appwindow();
   if(pApp && table)
   {
diff --git a/glom/mode_design/relationships_overview/window_relationships_overview.h 
b/glom/mode_design/relationships_overview/window_relationships_overview.h
index 02dbcf9..670d9c5 100644
--- a/glom/mode_design/relationships_overview/window_relationships_overview.h
+++ b/glom/mode_design/relationships_overview/window_relationships_overview.h
@@ -31,6 +31,7 @@
 #include <gtkmm/scrolledwindow.h>
 #include <gtkmm/toggleaction.h>
 #include <gtkmm/builder.h>
+#include <glibmm/weakref.h>
 #include <goocanvasmm/canvas.h>
 #include <map>
 #include <vector>
@@ -68,10 +69,10 @@ private:
   void on_menu_view_showgrid(const Glib::VariantBase& /* parameter */);
 
   void on_table_moved(const Glib::RefPtr<CanvasItemMovable>& item, double x_offset, double y_offset);
-  void on_table_show_context(guint button, guint32 activate_time, Glib::RefPtr<CanvasGroupDbTable> table);
+  void on_table_show_context(guint button, guint32 activate_time, const Glib::WeakRef<CanvasGroupDbTable>& 
table);
 
-  void on_context_menu_edit_fields(const Glib::VariantBase& parameter, Glib::RefPtr<CanvasGroupDbTable> 
table);
-  void on_context_menu_edit_relationships(const Glib::VariantBase& parameter, 
Glib::RefPtr<CanvasGroupDbTable> table);
+  void on_context_menu_edit_fields(const Glib::VariantBase& parameter, const 
Glib::WeakRef<CanvasGroupDbTable>& table);
+  void on_context_menu_edit_relationships(const Glib::VariantBase& parameter, const 
Glib::WeakRef<CanvasGroupDbTable>& table);
 
   void on_scroll_value_changed();
 
@@ -92,9 +93,6 @@ private:
   Glib::RefPtr<Goocanvas::Group> m_group_tables;
   Glib::RefPtr<Goocanvas::Group> m_group_lines;
 
-  typedef std::list<sigc::connection> type_list_connections;
-  type_list_connections m_list_table_connections;
-
   //Context menu:
   std::unique_ptr<Gtk::Menu> m_context_menu;
   Glib::RefPtr<Gio::SimpleAction> m_action_edit_fields, m_action_edit_relationships;
diff --git a/glom/mode_design/report_layout/dialog_layout_report.cc 
b/glom/mode_design/report_layout/dialog_layout_report.cc
index 09537bc..002cef3 100644
--- a/glom/mode_design/report_layout/dialog_layout_report.cc
+++ b/glom/mode_design/report_layout/dialog_layout_report.cc
@@ -944,8 +944,12 @@ void Dialog_Layout_Report::on_treeview_parts_selection_changed()
   enable_buttons();
 }
 
-void Dialog_Layout_Report::on_cell_data_part(Gtk::CellRenderer* renderer, const Gtk::TreeModel::iterator& 
iter, const Glib::RefPtr<type_model>& model)
+void Dialog_Layout_Report::on_cell_data_part(Gtk::CellRenderer* renderer, const Gtk::TreeModel::iterator& 
iter, const Glib::WeakRef<type_model>& model_weak)
 {
+  const auto model = model_weak.get();
+  if (!model)
+    return;
+
   //TODO: If we ever use this as a real layout tree, then let's add icons for each type.
 
   //Set the view's cell properties depending on the model's data:
@@ -965,9 +969,13 @@ void Dialog_Layout_Report::on_cell_data_part(Gtk::CellRenderer* renderer, const
   }
 }
 
-void Dialog_Layout_Report::on_cell_data_details(Gtk::CellRenderer* renderer, const Gtk::TreeModel::iterator& 
iter, const Glib::RefPtr<type_model>& model)
+void Dialog_Layout_Report::on_cell_data_details(Gtk::CellRenderer* renderer, const Gtk::TreeModel::iterator& 
iter, const Glib::WeakRef<type_model>& model_weak)
 {
-//Set the view's cell properties depending on the model's data:
+  const auto model = model_weak.get();
+  if(!model)
+    return;
+
+  //Set the view's cell properties depending on the model's data:
   auto renderer_text = dynamic_cast<Gtk::CellRendererText*>(renderer);
   if(renderer_text)
   {
diff --git a/glom/mode_design/report_layout/dialog_layout_report.h 
b/glom/mode_design/report_layout/dialog_layout_report.h
index 5dc8c0c..e27a17d 100644
--- a/glom/mode_design/report_layout/dialog_layout_report.h
+++ b/glom/mode_design/report_layout/dialog_layout_report.h
@@ -27,6 +27,7 @@
 #include <gtkmm/checkbutton.h>
 #include <gtkmm/label.h>
 #include <gtkmm/entry.h>
+#include <glibmm/weakref.h>
 #include "treestore_report_layout.h"
 //#include <libglom/data_structure/layout/layoutitem.h>
 //#include <libglom/sharedptr.h>
@@ -91,8 +92,8 @@ private:
   void on_treeview_parts_selection_changed();
   void on_treeview_available_parts_selection_changed();
 
-  void on_cell_data_part(Gtk::CellRenderer* renderer, const Gtk::TreeModel::iterator& iter, const 
Glib::RefPtr<type_model>& model);
-  void on_cell_data_details(Gtk::CellRenderer* renderer, const Gtk::TreeModel::iterator& iter, const 
Glib::RefPtr<type_model>& model);
+  void on_cell_data_part(Gtk::CellRenderer* renderer, const Gtk::TreeModel::iterator& iter, const 
Glib::WeakRef<type_model>& model);
+  void on_cell_data_details(Gtk::CellRenderer* renderer, const Gtk::TreeModel::iterator& iter, const 
Glib::WeakRef<type_model>& model);
 
   void on_cell_data_available_part(Gtk::CellRenderer* renderer, const Gtk::TreeModel::iterator& iter);
 
diff --git a/glom/print_layout/canvas_print_layout.cc b/glom/print_layout/canvas_print_layout.cc
index 1ec0898..b9c938d 100644
--- a/glom/print_layout/canvas_print_layout.cc
+++ b/glom/print_layout/canvas_print_layout.cc
@@ -291,8 +291,12 @@ void Canvas_PrintLayout::setup_context_menu()
 }
 
 
-void Canvas_PrintLayout::on_item_show_context_menu(guint button, guint32 activate_time, 
Glib::RefPtr<CanvasLayoutItem> item)
+void Canvas_PrintLayout::on_item_show_context_menu(guint button, guint32 activate_time, const 
Glib::WeakRef<CanvasLayoutItem>& item_weak)
 {
+  const auto item = item_weak.get();
+  if(!item)
+    return;
+
   if(!m_context_menu || !item)
     return;
 
diff --git a/glom/print_layout/canvas_print_layout.h b/glom/print_layout/canvas_print_layout.h
index e441334..45c8b24 100644
--- a/glom/print_layout/canvas_print_layout.h
+++ b/glom/print_layout/canvas_print_layout.h
@@ -29,6 +29,7 @@
 #include <giomm/simpleactiongroup.h>
 #include <gtkmm/pagesetup.h>
 #include <gtkmm/menu.h>
+#include <glibmm/weakref.h>
 
 namespace Glom
 {
@@ -119,7 +120,7 @@ private:
   std::shared_ptr<LayoutItem_Line> offer_line(const std::shared_ptr<LayoutItem_Line>& portal, Gtk::Window* 
parent);
 
   //TODO: Make the signal send the item, so we can pass it by const reference:
-  void on_item_show_context_menu(guint button, guint32 activate_time, Glib::RefPtr<CanvasLayoutItem> item);
+  void on_item_show_context_menu(guint button, guint32 activate_time, const Glib::WeakRef<CanvasLayoutItem>& 
item);
   void on_context_menu_edit();
   void on_context_menu_formatting();
   void on_context_menu_delete();


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