Skip to content

Commit

Permalink
Clearer types in code rather than void pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
teusbenschop committed Jan 6, 2024
1 parent 8e93a47 commit 8c9c13d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 54 deletions.
2 changes: 1 addition & 1 deletion changes/change.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ string changes_change (Webserver_Request& webserver_request)


// Whether there"s a live notes editor available.
bool live_notes_editor = Ipc_Notes::alive (std::addressof(webserver_request), false);
bool live_notes_editor = Ipc_Notes::alive (webserver_request, false);
if (live_notes_editor)
view.enable_zone ("alive");
else
Expand Down
29 changes: 12 additions & 17 deletions ipc/notes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,46 +28,41 @@ using namespace std;
// Deals with the consultation notes stuff.


void Ipc_Notes::open (void * webserver_request, int identifier)
void Ipc_Notes::open (Webserver_Request& webserver_request, int identifier)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
string user = request->session_logic()->currentUser ();
request->database_ipc()->storeMessage (user, "", "opennote", filter::strings::convert_to_string (identifier));
string user = webserver_request.session_logic()->currentUser ();
webserver_request.database_ipc()->storeMessage (user, "", "opennote", filter::strings::convert_to_string (identifier));
}


int Ipc_Notes::get (void * webserver_request)
int Ipc_Notes::get (Webserver_Request& webserver_request)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
Database_Ipc_Message data = request->database_ipc()->getNote ();
Database_Ipc_Message data = webserver_request.database_ipc()->getNote ();
return filter::strings::convert_to_int (data.message);
}


void Ipc_Notes::erase (void * webserver_request)
void Ipc_Notes::erase (Webserver_Request& webserver_request)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
Database_Ipc_Message data = request->database_ipc()->getNote ();
Database_Ipc_Message data = webserver_request.database_ipc()->getNote ();
int counter = 0;
while (data.id && (counter < 100)) {
int id = data.id;
request->database_ipc()->deleteMessage (id);
webserver_request.database_ipc()->deleteMessage (id);
counter++;
}
}


// If $set is true, it sets the alive status of the notes editor.
// If $set is false, it returns the alive status.
bool Ipc_Notes::alive (void * webserver_request, bool set, bool alive)
bool Ipc_Notes::alive (Webserver_Request& webserver_request, bool set, bool alive)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
string user = request->session_logic()->currentUser ();
string user = webserver_request.session_logic()->currentUser ();
if (set) {
request->database_ipc()->storeMessage (user, "", "notesalive", filter::strings::convert_to_string (alive));
webserver_request.database_ipc()->storeMessage (user, "", "notesalive", filter::strings::convert_to_string (alive));
} else {
return request->database_ipc()->getNotesAlive ();
return webserver_request.database_ipc()->getNotesAlive ();
}
return false;
}

10 changes: 6 additions & 4 deletions ipc/notes.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@

#include <config/libraries.h>

class Webserver_Request;

class Ipc_Notes
{
public:
static void open (void * webserver_request, int identifier);
static int get (void * webserver_request);
static void erase (void * webserver_request);
static bool alive (void * webserver_request, bool set, bool alive = false);
static void open (Webserver_Request& webserver_request, int identifier);
static int get (Webserver_Request& webserver_request);
static void erase (Webserver_Request& webserver_request);
static bool alive (Webserver_Request& webserver_request, bool set, bool alive = false);
private:
};
4 changes: 2 additions & 2 deletions notes/click.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ string notes_click (Webserver_Request& webserver_request)
open = filter_url_basename_web (open);
int iopen = filter::strings::convert_to_int (open);
if (database_notes.identifier_exists (iopen)) {
Ipc_Notes::open (std::addressof(webserver_request), iopen);
Ipc_Notes::open (webserver_request, iopen);
}
}

Expand All @@ -82,7 +82,7 @@ string notes_click (Webserver_Request& webserver_request)
contents += database_modifications.getNotificationNewText (inew);
Passage passage = database_modifications.getNotificationPassage (inew);
int identifier = notes_logic.createNote (bible, passage.m_book, passage.m_chapter, filter::strings::convert_to_int (passage.m_verse), summary, contents, false);
Ipc_Notes::open (std::addressof(webserver_request), identifier);
Ipc_Notes::open (webserver_request, identifier);
}


Expand Down
8 changes: 4 additions & 4 deletions notes/poll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ string notes_poll (Webserver_Request& webserver_request)
{
string action = webserver_request.query ["action"];
if (action == "alive") {
Ipc_Notes::alive (std::addressof(webserver_request), true, true);
int identifier = Ipc_Notes::get (std::addressof(webserver_request));
Ipc_Notes::alive (webserver_request, true, true);
int identifier = Ipc_Notes::get (webserver_request);
if (identifier) {
Ipc_Notes::erase (std::addressof(webserver_request));
Ipc_Notes::erase (webserver_request);
string url = "note?id=" + filter::strings::convert_to_string (identifier);
return url;
}
} else if (action == "unload") {
Ipc_Notes::alive (std::addressof(webserver_request), true, false);
Ipc_Notes::alive (webserver_request, true, false);
}
return "";
}
52 changes: 26 additions & 26 deletions unittests/ipc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,38 @@ TEST (ipc, basic)
{
// Initialize.
refresh_sandbox (false);
Webserver_Request request;
request.database_users ()->create ();
request.session_logic ()->set_username ("phpunit");
Webserver_Request webserver_request;
webserver_request.database_users ()->create ();
webserver_request.session_logic ()->set_username ("phpunit");

// There should be no note identifier.
int identifier = Ipc_Notes::get (&request);
int identifier = Ipc_Notes::get (webserver_request);
EXPECT_EQ (0, identifier);

// Test opening note.
Ipc_Notes::open (&request, 123456789);
identifier = Ipc_Notes::get (&request);
Ipc_Notes::open (webserver_request, 123456789);
identifier = Ipc_Notes::get (webserver_request);
EXPECT_EQ (123456789, identifier);

// Test trimming.
request.database_ipc()->trim ();
identifier = Ipc_Notes::get (&request);
webserver_request.database_ipc()->trim ();
identifier = Ipc_Notes::get (webserver_request);
EXPECT_EQ (123456789, identifier);

// Test deleting note once.
Ipc_Notes::open (&request, 123456789);
Ipc_Notes::erase (&request);
identifier = Ipc_Notes::get (&request);
Ipc_Notes::open (webserver_request, 123456789);
Ipc_Notes::erase (webserver_request);
identifier = Ipc_Notes::get (webserver_request);
EXPECT_EQ (0, identifier);

// Test deleting two notes.
Ipc_Notes::open (&request, 123456789);
Ipc_Notes::open (&request, 123456789);
Ipc_Notes::erase (&request);
identifier = Ipc_Notes::get (&request);
Ipc_Notes::open (webserver_request, 123456789);
Ipc_Notes::open (webserver_request, 123456789);
Ipc_Notes::erase (webserver_request);
identifier = Ipc_Notes::get (webserver_request);
EXPECT_EQ (0, identifier);
Ipc_Notes::erase (&request);
identifier = Ipc_Notes::get (&request);
Ipc_Notes::erase (webserver_request);
identifier = Ipc_Notes::get (webserver_request);
EXPECT_EQ (0, identifier);
}

Expand Down Expand Up @@ -128,11 +128,11 @@ TEST (database, ipc)
refresh_sandbox (true);
Database_Users database_users;
database_users.create ();
Webserver_Request request;
Database_Ipc database_ipc = Database_Ipc (&request);
Webserver_Request webserver_request;
Database_Ipc database_ipc (std::addressof(webserver_request));

string user = "phpunit";
request.session_logic ()->set_username (user);
webserver_request.session_logic ()->set_username (user);
string channel = "channel";
string command = "focus";

Expand All @@ -155,11 +155,11 @@ TEST (database, ipc)
refresh_sandbox (true);
Database_Users database_users;
database_users.create ();
Webserver_Request request;
Database_Ipc database_ipc = Database_Ipc (&request);
Webserver_Request webserver_request;
Database_Ipc database_ipc (std::addressof(webserver_request));

string user = "phpunit";
request.session_logic ()->set_username (user);
webserver_request.session_logic ()->set_username (user);
string channel = "channel";
string command = "opennote";

Expand All @@ -182,11 +182,11 @@ TEST (database, ipc)
refresh_sandbox (true);
Database_Users database_users;
database_users.create ();
Webserver_Request request;
Database_Ipc database_ipc = Database_Ipc (&request);
Webserver_Request webserver_request;
Database_Ipc database_ipc (std::addressof(webserver_request));

string user = "phpunit";
request.session_logic ()->set_username (user);
webserver_request.session_logic ()->set_username (user);
string channel = "channel";
string command = "notesalive";

Expand Down

0 comments on commit 8c9c13d

Please sign in to comment.