[glom] Add and use find_if_exists(container, callable).
- From: Murray Cumming <murrayc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glom] Add and use find_if_exists(container, callable).
- Date: Tue, 5 Jan 2016 19:30:55 +0000 (UTC)
commit 77a19b321373481677d8e15c37906f688cf6c5c8
Author: Murray Cumming <murrayc murrayc com>
Date: Tue Jan 5 15:19:38 2016 +0100
Add and use find_if_exists(container, callable).
To replace lengthy use of
std::find_if(container.begin(), container.end(), callable) != 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/file_encodings.cc | 2 +-
glom/libglom/algorithms_utils.h | 8 ++++++++
tests/test_document_load_and_change.cc | 14 ++++++--------
tests/test_document_load_translations.cc | 27 +++++++++++----------------
4 files changed, 26 insertions(+), 25 deletions(-)
---
diff --git a/glom/import_csv/file_encodings.cc b/glom/import_csv/file_encodings.cc
index 7ddcca1..db9bfa5 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 <libglom/algorithm_utils.h>
+#include <libglom/algorithms_utils.h>
#include <glibmm/i18n.h>
namespace Glom
diff --git a/glom/libglom/algorithms_utils.h b/glom/libglom/algorithms_utils.h
index dbe81a2..a0237c4 100644
--- a/glom/libglom/algorithms_utils.h
+++ b/glom/libglom/algorithms_utils.h
@@ -37,6 +37,14 @@ 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_callable>
+bool
+find_if_exists(const T_container& container, const T_callable& callable)
+{
+ const auto end = std::end(container);
+ return std::find_if(std::begin(container), end, callable) != end;
+}
+
template<typename T_container, typename T_element>
typename T_container::iterator
diff --git a/tests/test_document_load_and_change.cc b/tests/test_document_load_and_change.cc
index 639c5cf..bc6f69b 100644
--- a/tests/test_document_load_and_change.cc
+++ b/tests/test_document_load_and_change.cc
@@ -47,14 +47,12 @@ 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 =
- Utils::find_if(container,
- [&name] (const Glom::GroupInfo& info)
- {
- return info.get_name() == name;
- }
- );
- return iter != container.end();
+ return Glom::Utils::find_if_exists(container,
+ [&name] (const Glom::GroupInfo& info)
+ {
+ return info.get_name() == name;
+ }
+ );
}
int main()
diff --git a/tests/test_document_load_translations.cc b/tests/test_document_load_translations.cc
index b40332d..875bd26 100644
--- a/tests/test_document_load_translations.cc
+++ b/tests/test_document_load_translations.cc
@@ -59,22 +59,17 @@ typename T_Container::value_type get_titled(const T_Container& container, const
template<typename T_TypeToFind>
bool contains_item_type(const Glom::Document::type_list_translatables& container)
{
- Glom::Document::type_list_translatables::const_iterator iter =
- Utils::find_if(container,
- [] (const Glom::Document::type_list_translatables::value_type& element)
- {
- std::shared_ptr<Glom::TranslatableItem> item = element.first;
- std::shared_ptr<T_TypeToFind> derived = std::dynamic_pointer_cast<T_TypeToFind>(item);
- if(derived)
- return true;
- else
- return false;
- }
- );
- if(iter != container.end())
- return true;
-
- return false;
+ return Glom::Utils::find_if_exists(container,
+ [] (const Glom::Document::type_list_translatables::value_type& element)
+ {
+ std::shared_ptr<Glom::TranslatableItem> item = element.first;
+ std::shared_ptr<T_TypeToFind> derived = std::dynamic_pointer_cast<T_TypeToFind>(item);
+ if(derived)
+ return true;
+ else
+ return false;
+ }
+ );
}
static std::shared_ptr<const Glom::LayoutItem_Field> get_field_on_layout(const Glom::Document& document,
const Glib::ustring& layout_table_name, const Glib::ustring& table_name, const Glib::ustring& field_name)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]