From 814e74bea3e814486e08b2fb529f24d310a63267 Mon Sep 17 00:00:00 2001 From: Teus Benschop Date: Sun, 7 Jan 2024 14:36:21 +0100 Subject: [PATCH] Clearer types in code rather than void pointers https://github.com/bibledit/cloud/issues/926 --- database/ipc.cpp | 13 +++++-------- database/ipc.h | 7 +++++-- database/maintenance.cpp | 2 +- unittests/ipc.cpp | 17 +++++++++-------- webserver/request.cpp | 3 ++- 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/database/ipc.cpp b/database/ipc.cpp index e8bf83d69..4b8bdeeb1 100644 --- a/database/ipc.cpp +++ b/database/ipc.cpp @@ -30,9 +30,9 @@ using namespace std; // Database resilience: Stored in plain file system. -Database_Ipc::Database_Ipc (void * webserver_request_in) +Database_Ipc::Database_Ipc (Webserver_Request& webserver_request): +m_webserver_request (webserver_request) { - webserver_request = webserver_request_in; } @@ -143,8 +143,7 @@ void Database_Ipc::deleteMessage (int id) string Database_Ipc::getFocus () { - Webserver_Request * request = static_cast(webserver_request); - string user = request->session_logic ()->currentUser (); + string user = m_webserver_request.session_logic ()->currentUser (); int highestId = 0; string hitMessage = ""; @@ -171,8 +170,7 @@ string Database_Ipc::getFocus () Database_Ipc_Message Database_Ipc::getNote () { - Webserver_Request * request = static_cast(webserver_request); - string user = request->session_logic ()->currentUser (); + string user = m_webserver_request.session_logic ()->currentUser (); int highestId = 0; string hitMessage = ""; @@ -203,8 +201,7 @@ Database_Ipc_Message Database_Ipc::getNote () bool Database_Ipc::getNotesAlive () { - Webserver_Request * request = static_cast(webserver_request); - string user = request->session_logic ()->currentUser (); + string user = m_webserver_request.session_logic ()->currentUser (); int highestId = 0; string hitMessage = ""; diff --git a/database/ipc.h b/database/ipc.h index 0ec9911a6..ec6641155 100644 --- a/database/ipc.h +++ b/database/ipc.h @@ -23,6 +23,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include +class Webserver_Request; + + struct Database_Ipc_Item { std::string file {}; @@ -45,7 +48,7 @@ struct Database_Ipc_Message class Database_Ipc { public: - Database_Ipc (void * webserver_request_in); + Database_Ipc (Webserver_Request& webserver_request); void trim (); void storeMessage (std::string user, std::string channel, std::string command, std::string message); Database_Ipc_Message retrieveMessage (int id, std::string user, std::string channel, std::string command); @@ -54,7 +57,7 @@ class Database_Ipc Database_Ipc_Message getNote (); bool getNotesAlive (); private: - void * webserver_request {nullptr}; + Webserver_Request& m_webserver_request; std::string folder (); std::string file (std::string file); std::vector readData (); diff --git a/database/maintenance.cpp b/database/maintenance.cpp index ce3981df0..f71e56ee7 100644 --- a/database/maintenance.cpp +++ b/database/maintenance.cpp @@ -91,7 +91,7 @@ void database_maintenance () database_bibles.optimize (); - Database_Ipc database_ipc = Database_Ipc (&webserver_request); + Database_Ipc database_ipc (webserver_request); database_ipc.trim (); diff --git a/unittests/ipc.cpp b/unittests/ipc.cpp index 5a130859d..0eb9c2977 100644 --- a/unittests/ipc.cpp +++ b/unittests/ipc.cpp @@ -72,15 +72,16 @@ TEST (database, ipc) // Test trim. { refresh_sandbox (false); - Database_Ipc database_ipc = Database_Ipc (nullptr); + Webserver_Request webserver_request; + Database_Ipc database_ipc (webserver_request); database_ipc.trim (); } // Test store retrieve { refresh_sandbox (true); - Webserver_Request request; - Database_Ipc database_ipc = Database_Ipc (&request); + Webserver_Request webserver_request; + Database_Ipc database_ipc (webserver_request); int id = 1; string user = "phpunit"; @@ -103,8 +104,8 @@ TEST (database, ipc) // Test delete { refresh_sandbox (true); - Webserver_Request request; - Database_Ipc database_ipc = Database_Ipc (&request); + Webserver_Request webserver_request; + Database_Ipc database_ipc (webserver_request); int id = 1; string user = "phpunit"; @@ -129,7 +130,7 @@ TEST (database, ipc) Database_Users database_users; database_users.create (); Webserver_Request webserver_request; - Database_Ipc database_ipc (std::addressof(webserver_request)); + Database_Ipc database_ipc (webserver_request); string user = "phpunit"; webserver_request.session_logic ()->set_username (user); @@ -156,7 +157,7 @@ TEST (database, ipc) Database_Users database_users; database_users.create (); Webserver_Request webserver_request; - Database_Ipc database_ipc (std::addressof(webserver_request)); + Database_Ipc database_ipc (webserver_request); string user = "phpunit"; webserver_request.session_logic ()->set_username (user); @@ -183,7 +184,7 @@ TEST (database, ipc) Database_Users database_users; database_users.create (); Webserver_Request webserver_request; - Database_Ipc database_ipc (std::addressof(webserver_request)); + Database_Ipc database_ipc (webserver_request); string user = "phpunit"; webserver_request.session_logic ()->set_username (user); diff --git a/webserver/request.cpp b/webserver/request.cpp index aa3bb0c3a..024fb15ae 100644 --- a/webserver/request.cpp +++ b/webserver/request.cpp @@ -95,7 +95,8 @@ Database_Check * Webserver_Request::database_check () Database_Ipc * Webserver_Request::database_ipc () { - if (!database_ipc_instance) database_ipc_instance = new Database_Ipc (this); + if (!database_ipc_instance) + database_ipc_instance = new Database_Ipc (*this); return database_ipc_instance; }