From 51e414af19ef8c60db5c162d08432f5369471898 Mon Sep 17 00:00:00 2001 From: Teus Benschop Date: Sun, 31 Dec 2023 17:34:22 +0100 Subject: [PATCH] Better typing --- bootstrap/bootstrap.cpp | 8 +++---- checks/index.cpp | 46 +++++++++++++++++++--------------------- checks/index.h | 4 ++-- menu/logic.cpp | 4 +++- resource/bb2resource.cpp | 32 +++++++++++++--------------- resource/bb2resource.h | 4 ++-- 6 files changed, 48 insertions(+), 50 deletions(-) diff --git a/bootstrap/bootstrap.cpp b/bootstrap/bootstrap.cpp index df8e3a206..a28776c81 100644 --- a/bootstrap/bootstrap.cpp +++ b/bootstrap/bootstrap.cpp @@ -432,13 +432,13 @@ void bootstrap_index (Webserver_Request& webserver_request) return; } - if ((url == resource_bible2resource_url ()) && browser_request_security_okay (webserver_request) && resource_bible2resource_acl (std::addressof(webserver_request))) { - webserver_request.reply = resource_bible2resource (std::addressof(webserver_request)); + if ((url == resource_bible2resource_url ()) && browser_request_security_okay (webserver_request) && resource_bible2resource_acl (webserver_request)) { + webserver_request.reply = resource_bible2resource (webserver_request); return; } - if ((url == checks_index_url ()) && browser_request_security_okay (webserver_request) && checks_index_acl (std::addressof(webserver_request))) { - webserver_request.reply = checks_index (std::addressof(webserver_request)); + if ((url == checks_index_url ()) && browser_request_security_okay (webserver_request) && checks_index_acl (webserver_request)) { + webserver_request.reply = checks_index (webserver_request); return; } diff --git a/checks/index.cpp b/checks/index.cpp index 5e6830d0e..14d605fd2 100644 --- a/checks/index.cpp +++ b/checks/index.cpp @@ -34,72 +34,70 @@ #include #include #include -using namespace std; -string checks_index_url () +std::string checks_index_url () { return "checks/index"; } -bool checks_index_acl (void * webserver_request) +bool checks_index_acl (Webserver_Request& webserver_request) { - return Filter_Roles::access_control (webserver_request, Filter_Roles::translator ()); + return Filter_Roles::access_control (std::addressof(webserver_request), Filter_Roles::translator ()); } -string checks_index (void * webserver_request) +std::string checks_index (Webserver_Request& webserver_request) { - Webserver_Request * request = static_cast(webserver_request); Database_Check database_check {}; - string page {}; - Assets_Header header = Assets_Header (translate("Checks"), webserver_request); + std::string page {}; + Assets_Header header = Assets_Header (translate("Checks"), std::addressof(webserver_request)); header.add_bread_crumb (menu_logic_tools_menu (), menu_logic_tools_text ()); page = header.run (); Assets_View view {}; - if (request->query.count ("approve")) { - int approve = filter::strings::convert_to_int (request->query["approve"]); + if (webserver_request.query.count ("approve")) { + const int approve = filter::strings::convert_to_int (webserver_request.query["approve"]); database_check.approve (approve); view.set_variable ("success", translate("The entry was suppressed.")); } - if (request->query.count ("delete")) { - int erase = filter::strings::convert_to_int (request->query["delete"]); + if (webserver_request.query.count ("delete")) { + const int erase = filter::strings::convert_to_int (webserver_request.query["delete"]); database_check.erase (erase); view.set_variable ("success", translate("The entry was deleted for just now.")); } // Get the Bibles the user has write-access to. - vector bibles {}; + std::vector bibles {}; { - const vector & all_bibles = request->database_bibles()->get_bibles (); - for (const auto & bible : all_bibles) { - if (access_bible::write (webserver_request, bible)) { + const std::vector & all_bibles = webserver_request.database_bibles()->get_bibles (); + for (const auto& bible : all_bibles) { + if (access_bible::write (std::addressof(webserver_request), bible)) { bibles.push_back (bible); } } } - stringstream resultblock {}; - const vector & hits = database_check.getHits (); - for (const auto & hit : hits) { - string bible = hit.bible; + std::stringstream resultblock {}; + const std::vector & hits = database_check.getHits (); + for (const auto& hit : hits) { + std::string bible = hit.bible; if (find (bibles.begin(), bibles.end (), bible) != bibles.end ()) { - int id = hit.rowid; + const int id = hit.rowid; bible = filter::strings::escape_special_xml_characters (bible); int book = hit.book; int chapter = hit.chapter; int verse = hit.verse; - string link = filter_passage_link_for_opening_editor_at (book, chapter, filter::strings::convert_to_string (verse)); - string information = filter::strings::escape_special_xml_characters (hit.data); + const std::string link = filter_passage_link_for_opening_editor_at (book, chapter, filter::strings::convert_to_string (verse)); + const std::string information = filter::strings::escape_special_xml_characters (hit.data); resultblock << "

\n"; resultblock << "\n"; resultblock << "" << filter::strings::emoji_wastebasket () << "\n"; @@ -114,7 +112,7 @@ string checks_index (void * webserver_request) view.set_variable ("resultblock", resultblock.str()); - if (checks_settings_acl (webserver_request)) { + if (checks_settings_acl (std::addressof(webserver_request))) { view.enable_zone ("can_enable"); } else { view.enable_zone ("cannot_enable"); diff --git a/checks/index.h b/checks/index.h index 72943d414..063e72f44 100644 --- a/checks/index.h +++ b/checks/index.h @@ -24,5 +24,5 @@ class Webserver_Request; std::string checks_index_url (); -bool checks_index_acl (void * webserver_request); -std::string checks_index (void * webserver_request); +bool checks_index_acl (Webserver_Request& webserver_request); +std::string checks_index (Webserver_Request& webserver_request); diff --git a/menu/logic.cpp b/menu/logic.cpp index 8c5c25c2d..bb349ecb7 100644 --- a/menu/logic.cpp +++ b/menu/logic.cpp @@ -541,6 +541,8 @@ string menu_logic_search_category (void * webserver_request, string * tooltip) string menu_logic_tools_category (void * webserver_request, string * tooltip) { + Webserver_Request * request = static_cast(webserver_request); + // The labels that may end up in the menu. string checks = translate ("Checks"); string consistency = translate ("Consistency"); @@ -575,7 +577,7 @@ string menu_logic_tools_category (void * webserver_request, string * tooltip) for (auto & label : labels) { if (label == checks) { - if (checks_index_acl (webserver_request)) { + if (checks_index_acl (*request)) { html.push_back (menu_logic_create_item (checks_index_url (), label, true, "", "")); tiplabels.push_back (label); } diff --git a/resource/bb2resource.cpp b/resource/bb2resource.cpp index d489650d9..0047a3b1d 100644 --- a/resource/bb2resource.cpp +++ b/resource/bb2resource.cpp @@ -34,54 +34,52 @@ #include #include

#include -using namespace std; -string resource_bible2resource_url () +std::string resource_bible2resource_url () { return "resource/bb2resource"; } -bool resource_bible2resource_acl (void * webserver_request) +bool resource_bible2resource_acl (Webserver_Request& webserver_request) { - return Filter_Roles::access_control (webserver_request, Filter_Roles::manager ()); + return Filter_Roles::access_control (std::addressof(webserver_request), Filter_Roles::manager ()); } -string resource_bible2resource (void * webserver_request) +std::string resource_bible2resource (Webserver_Request& webserver_request) { - Webserver_Request * request = static_cast(webserver_request); Database_UsfmResources database_usfmresources = Database_UsfmResources (); - string page; - Assets_Header header = Assets_Header (translate("Convert"), request); + std::string page; + Assets_Header header = Assets_Header (translate("Convert"), std::addressof(webserver_request)); header.add_bread_crumb (menu_logic_settings_menu (), menu_logic_settings_text ()); header.add_bread_crumb (bible_manage_url (), menu_logic_bible_manage_text ()); page = header.run (); Assets_View view; - string bible = request->query["bible"]; + const std::string bible = webserver_request.query["bible"]; view.set_variable ("bible", bible); - vector usfmResources = database_usfmresources.getResources (); - if (find (usfmResources.begin(), usfmResources.end (), bible) != usfmResources.end ()) { + const std::vector usfm_resources = database_usfmresources.getResources (); + if (find (usfm_resources.begin(), usfm_resources.end (), bible) != usfm_resources.end ()) { view.set_variable ("error", translate("A USFM Resource with this name already exists")); } - vector externalResources = resource_external_names (); - if (find (externalResources.begin(), externalResources.end (), bible) != externalResources.end ()) { + const std::vector external_resources = resource_external_names (); + if (find (external_resources.begin(), external_resources.end (), bible) != external_resources.end ()) { view.set_variable ("error", translate("An external resource with this name already exists")); } - if (request->query.count ("convert")) { - if (access_bible::write (request, bible)) { + if (webserver_request.query.count ("convert")) { + if (access_bible::write (std::addressof(webserver_request), bible)) { tasks_logic_queue (CONVERTBIBLE2RESOURCE, {bible}); - redirect_browser (request, journal_index_url ()); - return ""; + redirect_browser (std::addressof(webserver_request), journal_index_url ()); + return std::string(); } else { assets_page::error (translate("Insufficient privileges to complete operation.")); } diff --git a/resource/bb2resource.h b/resource/bb2resource.h index 1519f2c20..5c9b3ec24 100644 --- a/resource/bb2resource.h +++ b/resource/bb2resource.h @@ -24,5 +24,5 @@ class Webserver_Request; std::string resource_bible2resource_url (); -bool resource_bible2resource_acl (void * webserver_request); -std::string resource_bible2resource (void * webserver_request); +bool resource_bible2resource_acl (Webserver_Request& webserver_request); +std::string resource_bible2resource (Webserver_Request& webserver_request);