[glom] Add and use find_if(container, callable).
- From: Murray Cumming <murrayc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glom] Add and use find_if(container, callable).
- Date: Tue, 5 Jan 2016 19:30:50 +0000 (UTC)
commit a5d09e98479ca8bd7d187af861ee74dff0aaf091
Author: Murray Cumming <murrayc murrayc com>
Date: Tue Jan 5 15:12:31 2016 +0100
Add and use find_if(container, callable).
To replace lengthy use of
std::find_if(container.begin(), container.end(), callable).
It looks like something like this will be in the standard C++ library
at some point, but I don't want to wait.
glom/base_db.cc | 2 +-
glom/import_csv/file_encodings.cc | 9 ++++-----
glom/libglom/algorithms_utils.h | 15 +++++++++++++++
.../data_structure/layout/layoutitem_field.h | 3 ++-
glom/libglom/document/document.cc | 4 ++--
glom/libglom/utils.cc | 2 +-
tests/test_document_load.cc | 2 +-
tests/test_document_load_and_change.cc | 2 +-
tests/test_document_load_translations.cc | 4 ++--
9 files changed, 29 insertions(+), 14 deletions(-)
---
diff --git a/glom/base_db.cc b/glom/base_db.cc
index 97f55bf..22f5dcd 100644
--- a/glom/base_db.cc
+++ b/glom/base_db.cc
@@ -62,7 +62,7 @@ template
<typename T_Container>
auto find_if_layout_item_is_equal(T_Container& container, const typename T_Container::value_type&
layout_item) -> decltype(container.begin())
{
- return std::find_if(container.begin(), container.end(),
+ return Utils::find_if(container,
[&layout_item](const typename T_Container::value_type& element)
{
//Assume that element is a shared_ptr<>.
diff --git a/glom/import_csv/file_encodings.cc b/glom/import_csv/file_encodings.cc
index 6c8ca5d..7ddcca1 100644
--- a/glom/import_csv/file_encodings.cc
+++ b/glom/import_csv/file_encodings.cc
@@ -21,7 +21,7 @@
//#include "config.h" //For ISO_CODES_PREFIX.
#include <glom/import_csv/file_encodings.h>
-#include <algorithm>
+#include <libglom/algorithm_utils.h>
#include <glibmm/i18n.h>
namespace Glom
@@ -108,12 +108,11 @@ Glib::ustring get_name_of_charset(const Glib::ustring& charset)
//Make sure that the list is full:
get_list_of_encodings();
- type_list_encodings::const_iterator iter =
- std::find_if(list_encodings.begin(), list_encodings.end(),
+ const auto iter =
+ Utils::find_if(list_encodings,
[&charset] (const Encoding& encoding) {
return encoding.get_charset() == charset;
- }
- );
+ });
if(iter != list_encodings.end())
return iter->get_name();
diff --git a/glom/libglom/algorithms_utils.h b/glom/libglom/algorithms_utils.h
index a758f95..dbe81a2 100644
--- a/glom/libglom/algorithms_utils.h
+++ b/glom/libglom/algorithms_utils.h
@@ -52,6 +52,21 @@ find(const T_container& container, const T_element& element)
return std::find(std::begin(container), std::end(container), element);
}
+
+template<typename T_container, typename T_callable>
+typename T_container::iterator
+find_if(T_container& container, const T_callable& callable)
+{
+ return std::find_if(std::begin(container), std::end(container), callable);
+}
+
+template<typename T_container, typename T_callable>
+typename T_container::const_iterator
+find_if(const T_container& container, const T_callable& callable)
+{
+ return std::find_if(std::begin(container), std::end(container), callable);
+}
+
} //namespace Utils
} //namespace Glom
diff --git a/glom/libglom/data_structure/layout/layoutitem_field.h
b/glom/libglom/data_structure/layout/layoutitem_field.h
index 247223d..497e193 100644
--- a/glom/libglom/data_structure/layout/layoutitem_field.h
+++ b/glom/libglom/data_structure/layout/layoutitem_field.h
@@ -27,6 +27,7 @@
#include <libglom/data_structure/numeric_format.h>
#include <libglom/data_structure/relationship.h>
#include <libglom/data_structure/layout/custom_title.h>
+#include <libglom/algorithms_utils.h>
namespace Glom
{
@@ -168,7 +169,7 @@ template
<typename T_Container>
auto find_if_layout_item_field_is_same_field(T_Container& container, const std::shared_ptr<const
LayoutItem_Field>& layout_item) -> decltype(container.begin())
{
- return std::find_if(container.begin(), container.end(),
+ return Utils::find_if(container,
[&layout_item](const typename T_Container::value_type& element)
{
//Assume that element is a shared_ptr<>.
diff --git a/glom/libglom/document/document.cc b/glom/libglom/document/document.cc
index c9f6207..53da8cc 100644
--- a/glom/libglom/document/document.cc
+++ b/glom/libglom/document/document.cc
@@ -258,7 +258,7 @@ template
<typename T_Container>
auto find_if_layout(T_Container& container, const Glib::ustring& layout_name, const Glib::ustring&
layout_platform) -> decltype(container.begin())
{
- return std::find_if(container.begin(), container.end(),
+ return Utils::find_if(container,
[&layout_name, &layout_platform](const typename T_Container::value_type& element)
{
return (element.m_layout_name == layout_name) &&
@@ -4305,7 +4305,7 @@ template
<typename T_Container>
auto find_if_item_and_hint_equal(T_Container& container, const Document::pair_translatable_item_and_hint&
item_and_hint) -> decltype(container.begin())
{
- return std::find_if(container.begin(), container.end(),
+ return Utils::find_if(container,
[&item_and_hint](const typename T_Container::value_type& element)
{
if(!element.first && item_and_hint.first)
diff --git a/glom/libglom/utils.cc b/glom/libglom/utils.cc
index 133fef8..e7a8035 100644
--- a/glom/libglom/utils.cc
+++ b/glom/libglom/utils.cc
@@ -62,7 +62,7 @@ auto find_if_uses_relationship_has_relationship(T_Container& container, const st
if(first_level_only)
related_relationship_name = Glib::ustring();
- return std::find_if(container.begin(), container.end(),
+ return Utils::find_if(container,
[&relationship_name, &related_relationship_name](const typename T_Container::value_type& element)
{
//Assume that element is a shared_ptr<>.
diff --git a/tests/test_document_load.cc b/tests/test_document_load.cc
index 3d4727f..fd0e518 100644
--- a/tests/test_document_load.cc
+++ b/tests/test_document_load.cc
@@ -59,7 +59,7 @@ bool contains_value(const T_Container& container, const Glib::ustring& name)
static bool get_group_named(const Glom::Document::type_list_groups& container, const Glib::ustring& name,
Glom::GroupInfo& group_info)
{
Glom::Document::type_list_groups::const_iterator iter =
- std::find_if(container.begin(), container.end(),
+ Glom::Utils::find_if(container,
[&name] (const Glom::GroupInfo& info)
{
return info.get_name() == name;
diff --git a/tests/test_document_load_and_change.cc b/tests/test_document_load_and_change.cc
index 303613a..639c5cf 100644
--- a/tests/test_document_load_and_change.cc
+++ b/tests/test_document_load_and_change.cc
@@ -48,7 +48,7 @@ static bool field_is_on_a_layout(Glom::Document& document, const Glib::ustring&
static bool groups_contain_named(const Glom::Document::type_list_groups& container, const Glib::ustring&
name)
{
Glom::Document::type_list_groups::const_iterator iter =
- std::find_if(container.begin(), container.end(),
+ Utils::find_if(container,
[&name] (const Glom::GroupInfo& info)
{
return info.get_name() == name;
diff --git a/tests/test_document_load_translations.cc b/tests/test_document_load_translations.cc
index 6fe3e40..b40332d 100644
--- a/tests/test_document_load_translations.cc
+++ b/tests/test_document_load_translations.cc
@@ -44,7 +44,7 @@ typename T_Container::value_type get_titled(const T_Container& container, const
type_sharedptr result;
typename T_Container::const_iterator iter =
- std::find_if(container.begin(), container.end(),
+ Glom::Utils::find_if(container,
[&title] (const typename T_Container::value_type& element)
{
return (element && element->get_title_original() == title);
@@ -60,7 +60,7 @@ template<typename T_TypeToFind>
bool contains_item_type(const Glom::Document::type_list_translatables& container)
{
Glom::Document::type_list_translatables::const_iterator iter =
- std::find_if(container.begin(), container.end(),
+ Utils::find_if(container,
[] (const Glom::Document::type_list_translatables::value_type& element)
{
std::shared_ptr<Glom::TranslatableItem> item = element.first;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]