[glom] Add and use find_if_same_name_exist().



commit 624e61166f412d2a73e8d802f11eafe74579dbcc
Author: Murray Cumming <murrayc murrayc com>
Date:   Tue Jan 5 15:48:15 2016 +0100

    Add and use find_if_same_name_exist().
    
    To replace use of find_if_same_name() != container.end().

 glom/frame_glom.cc                             |    3 +--
 glom/libglom/data_structure/field.h            |   11 +++++++++++
 glom/libglom/db_utils.cc                       |   11 ++++-------
 glom/libglom/document/document.cc              |    6 +++---
 glom/mode_design/box_db_table_relationships.cc |    2 +-
 tests/test_document_load.cc                    |    4 +---
 6 files changed, 21 insertions(+), 16 deletions(-)
---
diff --git a/glom/frame_glom.cc b/glom/frame_glom.cc
index 391db4d..2efd4c5 100644
--- a/glom/frame_glom.cc
+++ b/glom/frame_glom.cc
@@ -1492,8 +1492,7 @@ void Frame_Glom::update_table_in_document_from_database()
       for(const auto& field : fieldsDocument)
       {
         //Check whether it's in the database:
-        auto iterFindDatabase = find_if_same_name(fieldsDatabase, field->get_name());
-        if(iterFindDatabase != fieldsDatabase.end()) //If it was found
+        if(find_if_same_name_exists(fieldsDatabase, field->get_name())) //If it was found
         {
           fieldsActual.push_back(field);
         }
diff --git a/glom/libglom/data_structure/field.h b/glom/libglom/data_structure/field.h
index d966ff5..8a660e8 100644
--- a/glom/libglom/data_structure/field.h
+++ b/glom/libglom/data_structure/field.h
@@ -48,6 +48,17 @@ auto find_if_same_name(T_Container& container, const Glib::ustring& name) -> dec
   );
 }
 
+/**
+ * Find if there is an element in the container whose name is the same as @name.
+ * This assumes that the element is a shared_ptr<>.
+ */
+template
+<typename T_Container>
+bool find_if_same_name_exists(T_Container& container, const Glib::ustring& name)
+{
+  return find_if_same_name(container, name) != container.end();
+}
+
 //Field info, such as Name, Title, definitions, and, sometimes, contents.
 class Field : public TranslatableItem
 {
diff --git a/glom/libglom/db_utils.cc b/glom/libglom/db_utils.cc
index bbaeef3..ddcb77d 100644
--- a/glom/libglom/db_utils.cc
+++ b/glom/libglom/db_utils.cc
@@ -800,9 +800,8 @@ void handle_error(const std::exception& ex)
 
 bool get_field_exists_in_database(const Glib::ustring& table_name, const Glib::ustring& field_name)
 {
-  const type_vec_fields vecFields = get_fields_for_table_from_database(table_name);
-  auto iterFind = find_if_same_name(vecFields, field_name);
-  return iterFind != vecFields.end();
+  const auto vecFields = get_fields_for_table_from_database(table_name);
+  return find_if_same_name_exists(vecFields, field_name);
 }
 
 type_vec_fields get_fields_for_table_from_database(const Glib::ustring& table_name, bool /* 
including_system_fields */)
@@ -1025,10 +1024,8 @@ type_vec_fields get_fields_for_table(const Document* document, const Glib::ustri
     const auto field_name = (*iter)->get_name();
 
     //Look in the result so far:
-    type_vec_fields::const_iterator iterFind = find_if_same_name(result, field_name);
-
     //Add it if it is not there:
-    if(iterFind == result.end() )
+    if(!find_if_same_name_exists(result, field_name))
       result.push_back(*iter);
   }
   */
@@ -1223,7 +1220,7 @@ bool create_table(Document::HostingMode hosting_mode, const std::shared_ptr<cons
 
   //Create the standard field too:
   //(We don't actually use this yet)
-  if(find_if_same_name(fields, GLOM_STANDARD_FIELD_LOCK) == fields.end())
+  if(find_if_same_name_exists(fields, GLOM_STANDARD_FIELD_LOCK))
   {
     std::shared_ptr<Field> field = std::make_shared<Field>();
     field->set_name(GLOM_STANDARD_FIELD_LOCK);
diff --git a/glom/libglom/document/document.cc b/glom/libglom/document/document.cc
index 53da8cc..f885ffd 100644
--- a/glom/libglom/document/document.cc
+++ b/glom/libglom/document/document.cc
@@ -635,7 +635,7 @@ Document::type_vec_relationships Document::get_relationships(const Glib::ustring
   //Add the system properties if necessary:
   if(plus_system_prefs)
   {
-    if(find_if_same_name(result, GLOM_RELATIONSHIP_NAME_SYSTEM_PROPERTIES) == result.end())
+    if(find_if_same_name_exists(result, GLOM_RELATIONSHIP_NAME_SYSTEM_PROPERTIES))
     {
       result.push_back(create_relationship_system_preferences(table_name));
     }
@@ -1162,7 +1162,7 @@ Document::type_listConstTableInfo Document::get_tables(bool plus_system_prefs) c
   //Add the system properties if necessary:
   if(plus_system_prefs)
   {
-    if(find_if_same_name(result, GLOM_STANDARD_TABLE_PREFS_TABLE_NAME) == result.end())
+    if(find_if_same_name_exists(result, GLOM_STANDARD_TABLE_PREFS_TABLE_NAME))
       result.push_back(create_table_system_preferences());
   }
 
@@ -1185,7 +1185,7 @@ Document::type_listTableInfo Document::get_tables(bool plus_system_prefs)
   //Add the system properties if necessary:
   if(plus_system_prefs)
   {
-    if(find_if_same_name(result, GLOM_STANDARD_TABLE_PREFS_TABLE_NAME) == result.end())
+    if(find_if_same_name_exists(result, GLOM_STANDARD_TABLE_PREFS_TABLE_NAME))
       result.push_back(create_table_system_preferences());
   }
 
diff --git a/glom/mode_design/box_db_table_relationships.cc b/glom/mode_design/box_db_table_relationships.cc
index df9eb16..329b3b5 100644
--- a/glom/mode_design/box_db_table_relationships.cc
+++ b/glom/mode_design/box_db_table_relationships.cc
@@ -151,7 +151,7 @@ void Box_DB_Table_Relationships::save_to_document()
       if(!old_name.empty() && (old_name != name))
         get_document()->change_relationship_name(m_table_name, old_name, name); //Update layouts and reports.
 
-      if(find_if_same_name(vecRelationships, name) == vecRelationships.end()) //Don't add 2 relationships 
with the same name.
+      if(find_if_same_name_exists(vecRelationships, name)) //Don't add 2 relationships with the same name.
       {
         std::shared_ptr<Relationship> relationship = document->get_relationship(m_table_name, name); 
//Preserve other information, such as translations.
         if(!relationship)
diff --git a/tests/test_document_load.cc b/tests/test_document_load.cc
index fd0e518..e1881c6 100644
--- a/tests/test_document_load.cc
+++ b/tests/test_document_load.cc
@@ -38,9 +38,7 @@ bool contains(const T_Container& container, const T_Value& name)
 template<typename T_Container>
 bool contains_named(const T_Container& container, const Glib::ustring& name)
 {
-  typename T_Container::const_iterator iter =
-    Glom::find_if_same_name(container, name);
-  return iter != container.end();
+  return Glom::find_if_same_name_exists(container, name);
 }
 
 template<typename T_Container>


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