[glom/no_raw_loops2: 1/4] Add and use add_unique() to simplify code.



commit 314a73a5e4e1ad99cbd265d8617adabbb4269010
Author: Murray Cumming <murrayc murrayc com>
Date:   Wed Feb 10 10:54:25 2016 +0100

    Add and use add_unique() to simplify code.

 glom/bakery/appwindow_withdoc.cc  |    4 ++--
 glom/libglom/algorithms_utils.h   |    9 +++++++++
 glom/libglom/document/document.cc |    5 +----
 glom/mode_design/iso_codes.cc     |    3 +--
 4 files changed, 13 insertions(+), 8 deletions(-)
---
diff --git a/glom/bakery/appwindow_withdoc.cc b/glom/bakery/appwindow_withdoc.cc
index 13b4b32..d5c7246 100644
--- a/glom/bakery/appwindow_withdoc.cc
+++ b/glom/bakery/appwindow_withdoc.cc
@@ -21,6 +21,7 @@
 #include <glom/bakery/dialog_offersave.h>
 #include <libglom/algorithms_utils.h>
 #include <libglom/utils.h>
+#include <libglom/algorithms_utils.h>
 #include <giomm/file.h>
 #include <algorithm>
 #include <glibmm/i18n-lib.h>
@@ -45,8 +46,7 @@ AppWindow_WithDoc::~AppWindow_WithDoc()
 //static
 void AppWindow_WithDoc::add_mime_type(const Glib::ustring& mime_type)
 {
-  if( !Glom::Utils::find_exists(m_mime_types, mime_type) )
-    m_mime_types.emplace_back(mime_type);
+  Glom::Utils::add_unique(m_mime_types, mime_type);
 }
 
 void AppWindow_WithDoc::on_menu_file_close()
diff --git a/glom/libglom/algorithms_utils.h b/glom/libglom/algorithms_utils.h
index a0237c4..5bf9895 100644
--- a/glom/libglom/algorithms_utils.h
+++ b/glom/libglom/algorithms_utils.h
@@ -75,6 +75,15 @@ find_if(const T_container& container, const T_callable& callable)
   return std::find_if(std::begin(container), std::end(container), callable);
 }
 
+template<typename T_container, typename T_element>
+void
+add_unique(T_container& container, T_element&& element)
+{
+  if(!find_exists(container, element))
+    container.emplace_back(std::forward<T_element>(element));
+}
+
+
 } //namespace Utils
 
 } //namespace Glom
diff --git a/glom/libglom/document/document.cc b/glom/libglom/document/document.cc
index d92f948..bcd2b21 100644
--- a/glom/libglom/document/document.cc
+++ b/glom/libglom/document/document.cc
@@ -2474,10 +2474,7 @@ void Document::load_after_translations(const xmlpp::Element* element, const std:
 
         //Remember any new translation locales in our cached list:
         //TODO: Use a set instead?
-        if(!Utils::find_exists(m_translation_available_locales, locale))
-        {
-          m_translation_available_locales.emplace_back(locale);
-        }
+        Utils::add_unique(m_translation_available_locales, locale);
       }
     }
   }
diff --git a/glom/mode_design/iso_codes.cc b/glom/mode_design/iso_codes.cc
index 5079aeb..10f037c 100644
--- a/glom/mode_design/iso_codes.cc
+++ b/glom/mode_design/iso_codes.cc
@@ -139,8 +139,7 @@ Glib::ustring get_locale_name(const Glib::ustring& locale_id)
     //Add the non-specific locales:
     for(const auto& id : list_ids_simple)
     {
-      if(!Utils::find_exists(list_ids, id))
-        list_ids.emplace_back(id);
+      Utils::add_unique(list_ids, id);
     }
 
     //Get the (translated) language names:


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