Commit 792cc2ea authored by Murray Cumming's avatar Murray Cumming
Browse files

Add and use find_exists().

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.
parent 60e34dd3
......@@ -33,6 +33,7 @@
#include <glom/utils_ui.h>
#include <glom/glade_utils.h>
#include <libglom/algorithms_utils.h>
#include <libglom/db_utils.h>
#include <libglom/privs.h>
#include <glom/python_embed/python_ui_callbacks.h>
......@@ -1234,7 +1235,7 @@ void AppWindow::update_table_sensitive_ui()
bool sensitive = has_table;
const bool is_developer_item =
(std::find(m_listDeveloperActions.begin(), m_listDeveloperActions.end(), action) != m_listDeveloperActions.end());
(Utils::find_exists(m_listDeveloperActions, action));
if(is_developer_item)
sensitive = sensitive && (userlevel == AppState::userlevels::DEVELOPER);
......
......@@ -19,6 +19,7 @@
#include "config.h"
#include <glom/bakery/appwindow_withdoc.h>
#include <glom/bakery/dialog_offersave.h>
#include <libglom/algorithms_utils.h>
#include <libglom/utils.h>
#include <giomm/file.h>
#include <algorithm>
......@@ -46,7 +47,7 @@ AppWindow_WithDoc::~AppWindow_WithDoc()
//static
void AppWindow_WithDoc::add_mime_type(const Glib::ustring& mime_type)
{
if( std::find(m_mime_types.begin(), m_mime_types.end(), mime_type) == m_mime_types.end() )
if( !Glom::Utils::find_exists(m_mime_types, mime_type) )
m_mime_types.push_back(mime_type);
}
......
......@@ -20,6 +20,7 @@
#include <glom/box_reports.h>
#include <glom/appwindow.h>
#include <libglom/algorithms_utils.h>
#include <libglom/utils.h> //For bold_message()).
#include <gtkmm/alignment.h>
#include <gtkmm/dialog.h>
......@@ -194,7 +195,7 @@ void Box_Reports::save_to_document()
{
const auto report_name = m_AddDel.get_value(item, m_colReportName);
if(!report_name.empty() && std::find(listReports.begin(), listReports.end(), report_name) == listReports.end())
if(!report_name.empty() && !Utils::find_exists(listReports, report_name))
{
auto report = std::make_shared<Report>();
report->set_name(report_name);
......
/* Glom
*
* Copyright (C) 2015 Murray Cumming
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*/
#ifndef GLOM_ALGORITHMS_UTILS_H
#define GLOM_ALGORITHMS_UTILS_H
#include <algorithm>
namespace Glom
{
namespace Utils
{
template<typename T_container, typename T_element>
bool
find_exists(const T_container& container, const T_element& element)
{
const auto end = std::end(container);
return std::find(std::begin(container), end, element) != end;
}
} //namespace Utils
} //namespace Glom
#endif //GLOM_ALGORITHMS_UTILS_H
......@@ -18,9 +18,11 @@
* Boston, MA 02110-1301 USA.
*/
#include <libglom/algorithms_utils.h>
#include <libglom/libglom_config.h>
#include <libglom/connectionpool_backends/sqlite.h>
#include <libglom/utils.h>
#include <libglom/algorithms_utils.h>
#include <libglom/db_utils.h>
#include <giomm/file.h>
#include <libgdamm/metastore.h>
......@@ -205,7 +207,7 @@ bool Sqlite::recreate_table(const Glib::RefPtr<Gnome::Gda::Connection>& connecti
GdaMetaTableColumn* column = GDA_META_TABLE_COLUMN(item->data);
// Don't add if field was removed
if(std::find(fields_removed.begin(), fields_removed.end(), column->column_name) != fields_removed.end())
if(Utils::find_exists(fields_removed, column->column_name))
continue;
#if 0
{
......@@ -300,33 +302,32 @@ bool Sqlite::recreate_table(const Glib::RefPtr<Gnome::Gda::Connection>& connecti
{
// Add new fields to the table. Fields that have changed have already
// been handled above.
auto removed_iter = std::find(fields_removed.begin(), fields_removed.end(), field->get_name());
if(removed_iter == fields_removed.end())
{
add_column_to_server_operation(operation, field, i++);
if(Utils::find_exists(fields_removed, field->get_name()))
continue;
if(!trans_fields.empty())
trans_fields += ',';
const auto default_value = field->get_default_value();
if(default_value.get_value_type() != G_TYPE_NONE && !default_value.is_null())
trans_fields += field->sql(default_value, connection);
else
add_column_to_server_operation(operation, field, i++);
if(!trans_fields.empty())
trans_fields += ',';
const auto default_value = field->get_default_value();
if(default_value.get_value_type() != G_TYPE_NONE && !default_value.is_null())
trans_fields += field->sql(default_value, connection);
else
{
switch(field->get_glom_type())
{
switch(field->get_glom_type())
{
case Field::glom_field_type::NUMERIC:
trans_fields += '0';
break;
case Field::glom_field_type::BOOLEAN:
trans_fields += '0';
break;
case Field::glom_field_type::TEXT:
trans_fields += "''";
break;
default:
trans_fields += "NULL";
break;
}
case Field::glom_field_type::NUMERIC:
trans_fields += '0';
break;
case Field::glom_field_type::BOOLEAN:
trans_fields += '0';
break;
case Field::glom_field_type::TEXT:
trans_fields += "''";
break;
default:
trans_fields += "NULL";
break;
}
}
}
......
......@@ -18,6 +18,7 @@
* Boston, MA 02110-1301 USA.
*/
#include <libglom/algorithms_utils.h>
#include <libglom/data_structure/field.h>
#include <libglom/connectionpool.h>
#include <libglom/data_structure/glomconversions.h>
......@@ -736,8 +737,7 @@ bool Field::get_conversion_possible(glom_field_type field_type_src, glom_field_t
if(iterFind != m_map_conversions.end())
{
const auto list_conversions = iterFind->second;
auto iterConversionFind = std::find(list_conversions.begin(), list_conversions.end(), field_type_dest);
if(iterConversionFind != list_conversions.end())
if(Utils::find_exists(list_conversions, field_type_dest))
return true; //Success: conversion found.
}
......
......@@ -18,6 +18,7 @@
* Boston, MA 02110-1301 USA.
*/
#include <libglom/algorithms_utils.h>
#include <libglom/db_utils.h>
#include <libglom/connectionpool.h>
#include <libglom/data_structure/glomconversions.h>
......@@ -587,55 +588,56 @@ bool add_standard_groups(Document* document)
if(gda_connection->supports_feature(Gnome::Gda::CONNECTION_FEATURE_USERS))
{
const auto vecGroups = Glom::Privs::get_database_groups();
type_vec_strings::const_iterator iterFind = std::find(vecGroups.begin(), vecGroups.end(), devgroup);
if(iterFind == vecGroups.end())
if(Utils::find_exists(vecGroups, devgroup)) {
//The group already exists.
return true;
}
//TODO: Escape and quote the user and group names here?
//The "SUPERUSER" here has no effect because SUPERUSER is not "inherited" to member users.
//But let's keep it to make the purpose of this group obvious.
bool test = query_execute_string(
DbUtils::build_query_create_group(GLOM_STANDARD_GROUP_NAME_DEVELOPER, true /* superuser */));
if(!test)
{
//TODO: Escape and quote the user and group names here?
//The "SUPERUSER" here has no effect because SUPERUSER is not "inherited" to member users.
//But let's keep it to make the purpose of this group obvious.
bool test = query_execute_string(
DbUtils::build_query_create_group(GLOM_STANDARD_GROUP_NAME_DEVELOPER, true /* superuser */));
if(!test)
{
std::cerr << G_STRFUNC << ": CREATE GROUP failed when adding the developer group." << std::endl;
return false;
}
std::cerr << G_STRFUNC << ": CREATE GROUP failed when adding the developer group." << std::endl;
return false;
}
//Make sure the current user is in the developer group.
//(If he is capable of creating these groups then he is obviously a developer, and has developer rights on the postgres server.)
const auto current_user = ConnectionPool::get_instance()->get_user();
const auto strQuery = build_query_add_user_to_group(GLOM_STANDARD_GROUP_NAME_DEVELOPER, current_user);
test = query_execute_string(strQuery);
if(!test)
{
std::cerr << G_STRFUNC << ": ALTER GROUP failed when adding the user to the developer group." << std::endl;
return false;
}
//Make sure the current user is in the developer group.
//(If he is capable of creating these groups then he is obviously a developer, and has developer rights on the postgres server.)
const auto current_user = ConnectionPool::get_instance()->get_user();
const auto strQuery = build_query_add_user_to_group(GLOM_STANDARD_GROUP_NAME_DEVELOPER, current_user);
test = query_execute_string(strQuery);
if(!test)
{
std::cerr << G_STRFUNC << ": ALTER GROUP failed when adding the user to the developer group." << std::endl;
return false;
}
//std::cout << "DEBUG: Added user " << current_user << " to glom developer group on postgres server." << std::endl;
//std::cout << "DEBUG: Added user " << current_user << " to glom developer group on postgres server." << std::endl;
Privileges priv_devs;
priv_devs.m_view = true;
priv_devs.m_edit = true;
priv_devs.m_create = true;
priv_devs.m_delete = true;
Privileges priv_devs;
priv_devs.m_view = true;
priv_devs.m_edit = true;
priv_devs.m_create = true;
priv_devs.m_delete = true;
for(const auto& table_info : document->get_tables(true /* including system prefs */))
for(const auto& table_info : document->get_tables(true /* including system prefs */))
{
if(table_info)
{
if(table_info)
{
const auto table_name = table_info->get_name();
if(get_table_exists_in_database(table_name)) //Maybe the table has not been created yet.
Glom::Privs::set_table_privileges(devgroup, table_name, priv_devs, true /* developer privileges */);
}
const auto table_name = table_info->get_name();
if(get_table_exists_in_database(table_name)) //Maybe the table has not been created yet.
Glom::Privs::set_table_privileges(devgroup, table_name, priv_devs, true /* developer privileges */);
}
//Make sure that it is in the database too:
GroupInfo group_info;
group_info.set_name(GLOM_STANDARD_GROUP_NAME_DEVELOPER);
group_info.m_developer = true;
document->set_group(group_info);
}
//Make sure that it is in the database too:
GroupInfo group_info;
group_info.set_name(GLOM_STANDARD_GROUP_NAME_DEVELOPER);
group_info.m_developer = true;
document->set_group(group_info);
}
else
{
......@@ -668,8 +670,7 @@ bool add_groups_from_document(const Document* document)
//std::cout << G_STRFUNC << ": DEBUG: group=" << name << std::endl;
//See if the group exists in the database:
type_vec_strings::const_iterator iterFind = std::find(database_groups.begin(), database_groups.end(), name);
if(!name.empty() && iterFind == database_groups.end())
if(!name.empty() && !Utils::find_exists(database_groups, name))
{
if(!add_group(document, name, group.m_developer))
{
......@@ -710,8 +711,7 @@ bool set_table_privileges_groups_from_document(const Document* document)
const auto group_name = group_info.get_name();
//See if the group exists in the database:
type_vec_strings::const_iterator iterFind = std::find(database_groups.begin(), database_groups.end(), group_name);
if(!group_name.empty() && iterFind == database_groups.end())
if(!group_name.empty() && !Utils::find_exists(database_groups, group_name))
{
std::cerr << G_STRFUNC << ": group does not exist in the database. group name=" << group_name << std::endl;
result = false;
......@@ -1143,11 +1143,9 @@ bool get_table_exists_in_database(const Glib::ustring& table_name)
//TODO_Performance
const auto tables = get_table_names_from_database();
type_vec_strings::const_iterator iterFind = std::find(tables.begin(), tables.end(), table_name);
return (iterFind != tables.end());
return Utils::find_exists(tables, table_name);
}
bool create_table_with_default_fields(Document* document, const Glib::ustring& table_name)
{
if(table_name.empty())
......@@ -2198,7 +2196,7 @@ bool add_group(const Document* document, const Glib::ustring& group, bool superu
//Let them edit the autoincrements too:
//Do not fail if the autoincrements table does not yet exist, because this can happen during restoring of a backup.
if(std::find(table_list.begin(), table_list.end(), GLOM_STANDARD_TABLE_AUTOINCREMENTS_TABLE_NAME) == table_list.end())
if(!Utils::find_exists(table_list, GLOM_STANDARD_TABLE_AUTOINCREMENTS_TABLE_NAME))
return true;
if(!Privs::set_table_privileges(group, GLOM_STANDARD_TABLE_AUTOINCREMENTS_TABLE_NAME, priv))
......
......@@ -20,6 +20,7 @@
#include <libglom/document/document.h>
#include <libglom/xml_utils.h>
#include <libglom/algorithms_utils.h>
#include <libglom/utils.h>
//#include <libglom/data_structure/glomconversions.h>
#include <libglom/data_structure/layout/report_parts/layoutitem_summary.h>
......@@ -2476,8 +2477,8 @@ void Document::load_after_translations(const xmlpp::Element* element, const std:
item->set_title(translation, locale);
//Remember any new translation locales in our cached list:
if(std::find(m_translation_available_locales.begin(),
m_translation_available_locales.end(), locale) == m_translation_available_locales.end())
//TODO: Use a set instead?
if(!Utils::find_exists(m_translation_available_locales, locale))
{
m_translation_available_locales.push_back(locale);
}
......
......@@ -19,6 +19,7 @@
libglom_toplevel_headers = \
glom/libglom/appstate.h \
glom/libglom/algorithms_utils.h \
glom/libglom/init.h \
glom/libglom/libglom_config.h \
glom/libglom/sharedptr.h \
......
......@@ -19,6 +19,7 @@
*/
#include "privs.h"
#include <libglom/algorithms_utils.h>
#include <libglom/standard_table_prefs_fields.h>
#include <libglom/db_utils.h>
#include <libglom/utils.h>
......@@ -64,8 +65,7 @@ bool Privs::get_default_developer_user_exists(Document::HostingMode hosting_mode
const auto default_user = get_default_developer_user_name(default_password, hosting_mode);
const auto users = get_database_users();
type_vec_strings::const_iterator iterFind = std::find(users.begin(), users.end(), default_user);
if(iterFind != users.end())
if(Utils::find_exists(users, default_user))
return true; //We assume that the password is what it should be and that it has developer rights.
return false; //The default user is not there.
......@@ -415,8 +415,7 @@ Privs::type_vec_strings Privs::get_groups_of_user(const Glib::ustring& user)
bool Privs::get_user_is_in_group(const Glib::ustring& user, const Glib::ustring& group)
{
const auto users = get_database_users(group);
type_vec_strings::const_iterator iterFind = std::find(users.begin(), users.end(), user);
return (iterFind != users.end());
return Utils::find_exists(users, user);
}
bool Privs::on_privs_privileges_cache_timeout(const Glib::ustring& table_name)
......@@ -467,9 +466,8 @@ Privileges Privs::get_current_privs(const Glib::ustring& table_name)
//Is the user in the special developers group?
/*
type_vec_strings developers = get_database_users(GLOM_STANDARD_GROUP_NAME_DEVELOPER);
type_vec_strings::const_iterator iterFind = std::find(developers.begin(), developers.end(), current_user);
if(iterFind != developers.end())
const auto developers = get_database_users(GLOM_STANDARD_GROUP_NAME_DEVELOPER);
if(Utils::find_exists(developers, current_user))
{
result.m_developer = true;
}
......
......@@ -29,7 +29,6 @@
#include <glom/python_embed/glom_python.h>
#include <glom/python_embed/python_ui_callbacks.h>
#include <glom/appwindow.h>
#include <algorithm> //For std::find()
#include <libglom/libglom_config.h>
#include <iostream>
#include <glibmm/i18n.h>
......
......@@ -21,6 +21,7 @@
#include "config.h"
#include <glom/mode_data/box_data_details.h>
#include <glom/frame_glom.h> //For show_ok_dialog().
#include <libglom/algorithms_utils.h>
#include <libglom/data_structure/field.h>
#include <libglom/data_structure/relationship.h>
#include <libglom/data_structure/glomconversions.h>
......@@ -515,8 +516,7 @@ void Box_Data_Details::recalculate_fields_for_related_records(const Glib::ustrin
{
//Is this field triggered by this relationship?
const auto triggered_by = field->get_calculation_relationships();
Field::type_list_strings::const_iterator iterFind = std::find(triggered_by.begin(), triggered_by.end(), relationship_name);
if(iterFind != triggered_by.end()) //If it was found
if(Utils::find_exists(triggered_by, relationship_name))
{
if(field)
{
......
......@@ -23,6 +23,7 @@
#include <glom/mode_design/iso_codes.h>
#include <libxml++/libxml++.h>
//#include <libglom/document/document.h>
#include <libglom/algorithms_utils.h>
#include <libglom/utils.h>
#include <glibmm/fileutils.h>
#include <glibmm/i18n.h>
......@@ -138,7 +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(std::find(list_ids.begin(), list_ids.end(), id) == list_ids.end())
if(!Utils::find_exists(list_ids, id))
list_ids.push_back(id);
}
......
......@@ -22,6 +22,7 @@
#include <glom/appwindow.h>
#include <gtkmm/alignment.h>
#include <gtkmm/messagedialog.h>
#include <libglom/algorithms_utils.h>
#include <libglom/utils.h> //For bold_message()).
#include <glibmm/i18n.h>
......@@ -182,7 +183,7 @@ void Box_Print_Layouts::save_to_document()
{
const auto name = m_AddDel.get_value(row, m_colName);
if(!name.empty() && std::find(listItems.begin(), listItems.end(), name) == listItems.end())
if(!name.empty() && !Utils::find_exists(listItems, name))
{
auto item = std::make_shared<PrintLayout>();
item->set_name(name);
......
......@@ -20,6 +20,7 @@
#include "flowtable.h"
#include "layoutwidgetbase.h"
#include <libglom/algorithms_utils.h>
#include <iostream>
#include <gtkmm/eventbox.h>
#include <gdkmm/window.h>
......@@ -48,9 +49,7 @@ FlowTable::~FlowTable()
const Gtk::Box* FlowTable::get_parent_hbox(const Gtk::Widget* first) const
{
const type_const_list_widgets::const_iterator iter_find =
std::find(m_list_first_widgets.begin(), m_list_first_widgets.end(), first);
if(iter_find == m_list_first_widgets.end())
if(!Utils::find_exists(m_list_first_widgets, first))
{
std::cerr << G_STRFUNC << ": first was not a first widget. first=" << first << std::endl;
return nullptr; //It has no Box parent because it is not even a first widget.
......@@ -66,9 +65,7 @@ const Gtk::Box* FlowTable::get_parent_hbox(const Gtk::Widget* first) const
if(box_children.empty())
continue;
const auto iter_find_box =
std::find(box_children.begin(), box_children.end(), first);
if(iter_find_box != box_children.end())
if(Utils::find_exists(box_children, first))
return hbox;
}
......@@ -216,9 +213,7 @@ bool FlowTable::get_column_for_first_widget(const Gtk::Widget& first, guint& col
const Gtk::Widget* child = nullptr;
//Check that it is really a child widget:
const type_const_list_widgets::const_iterator iter_find =
std::find(m_list_first_widgets.begin(), m_list_first_widgets.end(), &first);
if(iter_find == m_list_first_widgets.end())
if(!Utils::find_exists(m_list_first_widgets, &first))
return false; //It is not a first widget.
child = &first;
......
......@@ -23,6 +23,7 @@
#include <glom/appwindow.h>
#include <glom/utils_ui.h>
#include <glom/glade_utils.h>
#include <libglom/algorithms_utils.h>
#include <libglom/data_structure/glomconversions.h>
#include <glom/utility_widgets/dialog_image_load_progress.h>
#include <glom/utility_widgets/dialog_image_save_progress.h>
......@@ -327,11 +328,7 @@ void ImageGlom::show_image_data()
//std::cout << "mime_type=" << mime_type << std::endl;
fill_evince_supported_mime_types();
const auto iterFind =
std::find(m_evince_supported_mime_types.begin(),
m_evince_supported_mime_types.end(),
mime_type);
if(iterFind != m_evince_supported_mime_types.end())
if(Utils::find_exists(m_evince_supported_mime_types, mime_type))
{
use_evince = true;
}
......@@ -401,11 +398,7 @@ void ImageGlom::show_image_data()
bool use_gdkpixbuf = false;
fill_gdkpixbuf_supported_mime_types();
const auto iter_find_mime_type =
std::find(m_gdkpixbuf_supported_mime_types.begin(),
m_gdkpixbuf_supported_mime_types.end(),
mime_type);
if(iter_find_mime_type != m_gdkpixbuf_supported_mime_types.end())
if(Utils::find_exists(m_gdkpixbuf_supported_mime_types, mime_type))
{
use_gdkpixbuf = true;
}
......
......@@ -19,6 +19,7 @@
*/
#include "tests/test_utils.h"
#include <libglom/algorithms_utils.h>
#include <libglom/document/document.h>
#include <libglom/init.h>
#include <libglom/db_utils.h>
......@@ -31,9 +32,7 @@
template<typename T_Container, typename T_Value>
bool contains(const T_Container& container, const T_Value& name)
{
typename T_Container::const_iterator iter =
std::find(container.begin(), container.end(), name);
return iter != container.end();
return Glom::Utils::find_exists(container, name);
}
template<typename T_Container>
......
......@@ -21,6 +21,7 @@
#include "config.h"
#include <libglom/document/document.h>
#include <libglom/algorithms_utils.h>
#include <libglom/init.h>
#include <libglom/db_utils.h>
#include <giomm/file.h>
......@@ -33,9 +34,7 @@
template<typename T_Container, typename T_Value>
bool contains(const T_Container& container, const T_Value& name)
{
typename T_Container::const_iterator iter =
std::find(container.begin(), container.end(), name);
return iter != container.end();
return Glom::Utils::find_exists(container, name);
}
template<typename T_Container>
......
......@@ -19,6 +19,7 @@
*/
#include "tests/test_selfhosting_utils.h"
#include <libglom/algorithms_utils.h>
#include <libglom/init.h>
#include <libglom/utils.h>
#include <libglom/db_utils.h>
......@@ -31,9 +32,7 @@
template<typename T_Container, typename T_Value>
bool contains(const T_Container& container, const T_Value& name)
{
typename T_Container::const_iterator iter =
std::find(container.begin(), container.end(), name);
return iter != container.end();
return Glom::Utils::find_exists(container, name);