[glom] Add and use find(container, element).



commit fce6b6e8da42bdf23d931ab0acf765a39355ef69
Author: Murray Cumming <murrayc murrayc com>
Date:   Tue Jan 5 15:00:28 2016 +0100

    Add and use find(container, element).
    
    To replace lengthy use of
    std::find(container.begin(), container.end(), element) != container.end()).
    
    It looks like something like this will be in the standard C++ library
    at some point, but I don't want to wait.

 glom/import_csv/dialog_import_csv.cc               |    2 +-
 glom/libglom/algorithms_utils.h                    |   15 +++++++++++++++
 glom/libglom/data_structure/layout/layoutgroup.cc  |    7 ++++---
 glom/libglom/document/bakery/view/view_composite.h |    3 ++-
 glom/utility_widgets/flowtable.cc                  |    3 +--
 5 files changed, 23 insertions(+), 7 deletions(-)
---
diff --git a/glom/import_csv/dialog_import_csv.cc b/glom/import_csv/dialog_import_csv.cc
index ff7486b..7505113 100644
--- a/glom/import_csv/dialog_import_csv.cc
+++ b/glom/import_csv/dialog_import_csv.cc
@@ -649,7 +649,7 @@ void Dialog_Import_CSV::on_field_edited(const Glib::ustring& path, const Glib::u
     {
       std::shared_ptr<Field> field = (*field_iter)[m_field_columns.m_col_field];
       // Check whether another column is already using that field
-      auto vec_field_iter = std::find(m_fields.begin(), m_fields.end(), field);
+      auto vec_field_iter = Utils::find(m_fields, field);
       // Reset the old column since two different columns cannot be imported into the same field
       if(vec_field_iter != m_fields.end()) *vec_field_iter = std::shared_ptr<Field>();
 
diff --git a/glom/libglom/algorithms_utils.h b/glom/libglom/algorithms_utils.h
index 9b16cdd..a758f95 100644
--- a/glom/libglom/algorithms_utils.h
+++ b/glom/libglom/algorithms_utils.h
@@ -37,6 +37,21 @@ find_exists(const T_container& container, const T_element& element)
   return std::find(std::begin(container), end, element) != end;
 }
 
+
+template<typename T_container, typename T_element>
+typename T_container::iterator
+find(T_container& container, const T_element& element)
+{
+  return std::find(std::begin(container), std::end(container), element);
+}
+
+template<typename T_container, typename T_element>
+typename T_container::const_iterator
+find(const T_container& container, const T_element& element)
+{
+  return std::find(std::begin(container), std::end(container), element);
+}
+
 } //namespace Utils
 
 } //namespace Glom
diff --git a/glom/libglom/data_structure/layout/layoutgroup.cc 
b/glom/libglom/data_structure/layout/layoutgroup.cc
index 16d29b0..c33e24a 100644
--- a/glom/libglom/data_structure/layout/layoutgroup.cc
+++ b/glom/libglom/data_structure/layout/layoutgroup.cc
@@ -21,6 +21,7 @@
 #include <libglom/data_structure/layout/layoutgroup.h>
 #include <libglom/data_structure/layout/layoutitem_field.h>
 #include <libglom/data_structure/layout/layoutitem_portal.h>
+#include <libglom/algorithms_utils.h>
 #include <glibmm/i18n.h>
 #include <iostream> 
 
@@ -147,7 +148,7 @@ void LayoutGroup::add_item(const std::shared_ptr<LayoutItem>& item, const std::s
 {
   //Find the position of the item.
   auto unconst = std::const_pointer_cast<LayoutItem>(position);
-  auto iter = std::find(m_list_items.begin(), m_list_items.end(), unconst);
+  auto iter = Utils::find(m_list_items, unconst);
 
   //std::vector::insert() adds before rather than after:
   // jhs: We want to add after rather than before - at least for dnd
@@ -156,10 +157,10 @@ void LayoutGroup::add_item(const std::shared_ptr<LayoutItem>& item, const std::s
   m_list_items.insert(iter, item);
 }
 
-void LayoutGroup::remove_item (const std::shared_ptr<LayoutItem>& item)
+void LayoutGroup::remove_item(const std::shared_ptr<LayoutItem>& item)
 {
   auto unconst = std::const_pointer_cast<LayoutItem>(item);
-  auto iter = std::find(m_list_items.begin(), m_list_items.end(), unconst);
+  auto iter = Utils::find(m_list_items, unconst);
   m_list_items.erase(iter);
 }
 
diff --git a/glom/libglom/document/bakery/view/view_composite.h 
b/glom/libglom/document/bakery/view/view_composite.h
index c6a6fdf..effe71e 100644
--- a/glom/libglom/document/bakery/view/view_composite.h
+++ b/glom/libglom/document/bakery/view/view_composite.h
@@ -20,6 +20,7 @@
 #define GLOM_BAKERY_VIEW_COMPOSITE_H
 
 #include <libglom/document/bakery/view/view.h>
+#include <libglom/algorithms_utils.h>
 #include <vector>
 #include <algorithm> //For std::find
 
@@ -58,7 +59,7 @@ public:
 
   virtual void remove_view(type_view* pView)
   {
-    auto iter = std::find(m_vecViews.begin(), m_vecViews.end(), pView);
+    auto iter = Glom::Utils::find(m_vecViews, pView);
     if(iter != m_vecViews.end())
       m_vecViews.erase(iter);
   }
diff --git a/glom/utility_widgets/flowtable.cc b/glom/utility_widgets/flowtable.cc
index 6a79a55..784971d 100644
--- a/glom/utility_widgets/flowtable.cc
+++ b/glom/utility_widgets/flowtable.cc
@@ -89,8 +89,7 @@ void FlowTable::delete_and_forget_hbox(Gtk::Box* hbox)
   //if(hbox->get_parent() == this)
   
   //Check that it is in our list of hboxes:
-  const auto iter = std::find(
-    m_list_hboxes.begin(), m_list_hboxes.end(), hbox);
+  const auto iter = Utils::find(m_list_hboxes, hbox);
   if(iter == m_list_hboxes.end())
   {
     std::cerr << G_STRFUNC << ": hbox=" << hbox << " is not in our list of hboxes." << std::endl;


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