Skip to content

Commit

Permalink
refactor: Use enum-specific pack functions for enum values.
Browse files Browse the repository at this point in the history
It's more obvious this way.
  • Loading branch information
iphydf committed Jan 15, 2024
1 parent 6caa7ce commit a840cbd
Show file tree
Hide file tree
Showing 43 changed files with 150 additions and 108 deletions.
45 changes: 34 additions & 11 deletions other/event_tooling/generate_event_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,31 @@ std::string bin_pack_name_from_type(const std::string& type) {
return "bin_pack_u8";
} else if (type == "bool") {
return "bin_pack_bool";
// only unpack is special TODO(Green-Sky): should we change that?
//} else if (type == "Tox_User_Status") {
//return "tox_pack_user_status";
//} else if (type == "Tox_Conference_Type") {
//return "tox_pack_conference_type";
} else if (type == "Tox_User_Status") {
return "tox_user_status_pack";
} else if (type == "Tox_Conference_Type") {
return "tox_conference_type_pack";
} else if (type == "Tox_Message_Type") {
return "tox_message_type_pack";
} else if (type == "Tox_File_Control") {
return "tox_file_control_pack";
} else if (type == "Tox_Connection") {
return "tox_connection_pack";
} else if (type == "Tox_Group_Privacy_State") {
return "tox_group_privacy_state_pack";
} else if (type == "Tox_Group_Voice_State") {
return "tox_group_voice_state_pack";
} else if (type == "Tox_Group_Topic_Lock") {
return "tox_group_topic_lock_pack";
} else if (type == "Tox_Group_Join_Fail") {
return "tox_group_join_fail_pack";
} else if (type == "Tox_Group_Mod_Event") {
return "tox_group_mod_event_pack";
} else if (type == "Tox_Group_Exit_Type") {
return "tox_group_exit_type_pack";
} else {
//std::cerr << "unknown type " << type << "\n";
//exit(1);
// assume enum -> u32
std::cerr << "unknown type " << type << "\n";
exit(1);
return "bin_pack_u32";
}
}
Expand Down Expand Up @@ -310,7 +326,6 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
// pack
f << "bool tox_event_" << event_name_l << "_pack(\n";
f << " const Tox_Event_" << event_name << " *event, Bin_Pack *bp)\n{\n";
f << " assert(event != nullptr);\n";

bool return_started = false;

Expand All @@ -330,7 +345,11 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
overloaded{
[&](const EventTypeTrivial& t) {
f << bin_pack_name_from_type(t.type);
f << "(bp, event->" << t.name << ")";
if (t.type.rfind("Tox_", 0) == 0) {
f << "(event->" << t.name << ", bp)";
} else {
f << "(bp, event->" << t.name << ")";
}
},
[&](const EventTypeByteRange& t) {
f << "bin_pack_bin(bp, event->" << t.name_data << ", event->" << t.name_length << ")";
Expand Down Expand Up @@ -361,7 +380,11 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
overloaded{
[&](const EventTypeTrivial& t) {
f << bin_unpack_name_from_type(t.type);
f << "(bu, &event->" << t.name << ")";
if (t.type.rfind("Tox_", 0) == 0) {
f << "(&event->" << t.name << ", bu)";
} else {
f << "(bu, &event->" << t.name << ")";
}
},
[&](const EventTypeByteRange& t) {
f << "bin_unpack_bin(bu, &event->" << t.name_data << ", &event->" << t.name_length << ")";
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/conference_connected.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ static void tox_event_conference_connected_destruct(Tox_Event_Conference_Connect
bool tox_event_conference_connected_pack(
const Tox_Event_Conference_Connected *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_u32(bp, event->conference_number);
}

Expand Down
5 changes: 2 additions & 3 deletions toxcore/events/conference_invite.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,9 @@ static void tox_event_conference_invite_destruct(Tox_Event_Conference_Invite *co
bool tox_event_conference_invite_pack(
const Tox_Event_Conference_Invite *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->type)
&& tox_conference_type_pack(event->type, bp)
&& bin_pack_bin(bp, event->cookie, event->cookie_length);
}

Expand All @@ -124,7 +123,7 @@ static bool tox_event_conference_invite_unpack_into(
}

return bin_unpack_u32(bu, &event->friend_number)
&& tox_conference_type_unpack(bu, &event->type)
&& tox_conference_type_unpack(&event->type, bu)
&& bin_unpack_bin(bu, &event->cookie, &event->cookie_length);
}

Expand Down
5 changes: 2 additions & 3 deletions toxcore/events/conference_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,10 @@ static void tox_event_conference_message_destruct(Tox_Event_Conference_Message *
bool tox_event_conference_message_pack(
const Tox_Event_Conference_Message *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 4)
&& bin_pack_u32(bp, event->conference_number)
&& bin_pack_u32(bp, event->peer_number)
&& bin_pack_u32(bp, event->type)
&& tox_message_type_pack(event->type, bp)
&& bin_pack_bin(bp, event->message, event->message_length);
}

Expand All @@ -140,7 +139,7 @@ static bool tox_event_conference_message_unpack_into(

return bin_unpack_u32(bu, &event->conference_number)
&& bin_unpack_u32(bu, &event->peer_number)
&& tox_message_type_unpack(bu, &event->type)
&& tox_message_type_unpack(&event->type, bu)
&& bin_unpack_bin(bu, &event->message, &event->message_length);
}

Expand Down
1 change: 0 additions & 1 deletion toxcore/events/conference_peer_list_changed.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ static void tox_event_conference_peer_list_changed_destruct(Tox_Event_Conference
bool tox_event_conference_peer_list_changed_pack(
const Tox_Event_Conference_Peer_List_Changed *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_u32(bp, event->conference_number);
}

Expand Down
1 change: 0 additions & 1 deletion toxcore/events/conference_peer_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ static void tox_event_conference_peer_name_destruct(Tox_Event_Conference_Peer_Na
bool tox_event_conference_peer_name_pack(
const Tox_Event_Conference_Peer_Name *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->conference_number)
&& bin_pack_u32(bp, event->peer_number)
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/conference_title.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ static void tox_event_conference_title_destruct(Tox_Event_Conference_Title *conf
bool tox_event_conference_title_pack(
const Tox_Event_Conference_Title *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->conference_number)
&& bin_pack_u32(bp, event->peer_number)
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/file_chunk_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ static void tox_event_file_chunk_request_destruct(Tox_Event_File_Chunk_Request *
bool tox_event_file_chunk_request_pack(
const Tox_Event_File_Chunk_Request *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 4)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->file_number)
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/file_recv.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ static void tox_event_file_recv_destruct(Tox_Event_File_Recv *file_recv, const M
bool tox_event_file_recv_pack(
const Tox_Event_File_Recv *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 5)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->file_number)
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/file_recv_chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ static void tox_event_file_recv_chunk_destruct(Tox_Event_File_Recv_Chunk *file_r
bool tox_event_file_recv_chunk_pack(
const Tox_Event_File_Recv_Chunk *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 4)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->file_number)
Expand Down
5 changes: 2 additions & 3 deletions toxcore/events/file_recv_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,10 @@ static void tox_event_file_recv_control_destruct(Tox_Event_File_Recv_Control *fi
bool tox_event_file_recv_control_pack(
const Tox_Event_File_Recv_Control *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->file_number)
&& bin_pack_u32(bp, event->control);
&& tox_file_control_pack(event->control, bp);
}

non_null()
Expand All @@ -101,7 +100,7 @@ static bool tox_event_file_recv_control_unpack_into(

return bin_unpack_u32(bu, &event->friend_number)
&& bin_unpack_u32(bu, &event->file_number)
&& tox_file_control_unpack(bu, &event->control);
&& tox_file_control_unpack(&event->control, bu);
}


Expand Down
5 changes: 2 additions & 3 deletions toxcore/events/friend_connection_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ static void tox_event_friend_connection_status_destruct(Tox_Event_Friend_Connect
bool tox_event_friend_connection_status_pack(
const Tox_Event_Friend_Connection_Status *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->connection_status);
&& tox_connection_pack(event->connection_status, bp);
}

non_null()
Expand All @@ -85,7 +84,7 @@ static bool tox_event_friend_connection_status_unpack_into(
}

return bin_unpack_u32(bu, &event->friend_number)
&& tox_connection_unpack(bu, &event->connection_status);
&& tox_connection_unpack(&event->connection_status, bu);
}


Expand Down
1 change: 0 additions & 1 deletion toxcore/events/friend_lossless_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ static void tox_event_friend_lossless_packet_destruct(Tox_Event_Friend_Lossless_
bool tox_event_friend_lossless_packet_pack(
const Tox_Event_Friend_Lossless_Packet *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_bin(bp, event->data, event->data_length);
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/friend_lossy_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ static void tox_event_friend_lossy_packet_destruct(Tox_Event_Friend_Lossy_Packet
bool tox_event_friend_lossy_packet_pack(
const Tox_Event_Friend_Lossy_Packet *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_bin(bp, event->data, event->data_length);
Expand Down
5 changes: 2 additions & 3 deletions toxcore/events/friend_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,9 @@ static void tox_event_friend_message_destruct(Tox_Event_Friend_Message *friend_m
bool tox_event_friend_message_pack(
const Tox_Event_Friend_Message *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->type)
&& tox_message_type_pack(event->type, bp)
&& bin_pack_bin(bp, event->message, event->message_length);
}

Expand All @@ -124,7 +123,7 @@ static bool tox_event_friend_message_unpack_into(
}

return bin_unpack_u32(bu, &event->friend_number)
&& tox_message_type_unpack(bu, &event->type)
&& tox_message_type_unpack(&event->type, bu)
&& bin_unpack_bin(bu, &event->message, &event->message_length);
}

Expand Down
1 change: 0 additions & 1 deletion toxcore/events/friend_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ static void tox_event_friend_name_destruct(Tox_Event_Friend_Name *friend_name, c
bool tox_event_friend_name_pack(
const Tox_Event_Friend_Name *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_bin(bp, event->name, event->name_length);
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/friend_read_receipt.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ static void tox_event_friend_read_receipt_destruct(Tox_Event_Friend_Read_Receipt
bool tox_event_friend_read_receipt_pack(
const Tox_Event_Friend_Read_Receipt *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->message_id);
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/friend_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ static void tox_event_friend_request_destruct(Tox_Event_Friend_Request *friend_r
bool tox_event_friend_request_pack(
const Tox_Event_Friend_Request *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_bin(bp, event->public_key, TOX_PUBLIC_KEY_SIZE)
&& bin_pack_bin(bp, event->message, event->message_length);
Expand Down
5 changes: 2 additions & 3 deletions toxcore/events/friend_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ static void tox_event_friend_status_destruct(Tox_Event_Friend_Status *friend_sta
bool tox_event_friend_status_pack(
const Tox_Event_Friend_Status *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->status);
&& tox_user_status_pack(event->status, bp);
}

non_null()
Expand All @@ -85,7 +84,7 @@ static bool tox_event_friend_status_unpack_into(
}

return bin_unpack_u32(bu, &event->friend_number)
&& tox_user_status_unpack(bu, &event->status);
&& tox_user_status_unpack(&event->status, bu);
}


Expand Down
1 change: 0 additions & 1 deletion toxcore/events/friend_status_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ static void tox_event_friend_status_message_destruct(Tox_Event_Friend_Status_Mes
bool tox_event_friend_status_message_pack(
const Tox_Event_Friend_Status_Message *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_bin(bp, event->message, event->message_length);
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/friend_typing.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ static void tox_event_friend_typing_destruct(Tox_Event_Friend_Typing *friend_typ
bool tox_event_friend_typing_pack(
const Tox_Event_Friend_Typing *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_bool(bp, event->typing);
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/group_custom_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ static void tox_event_group_custom_packet_destruct(Tox_Event_Group_Custom_Packet
bool tox_event_group_custom_packet_pack(
const Tox_Event_Group_Custom_Packet *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->peer_id)
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/group_custom_private_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ static void tox_event_group_custom_private_packet_destruct(Tox_Event_Group_Custo
bool tox_event_group_custom_private_packet_pack(
const Tox_Event_Group_Custom_Private_Packet *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->peer_id)
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/group_invite.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ static void tox_event_group_invite_destruct(Tox_Event_Group_Invite *group_invite
bool tox_event_group_invite_pack(
const Tox_Event_Group_Invite *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_bin(bp, event->invite_data, event->invite_data_length)
Expand Down
5 changes: 2 additions & 3 deletions toxcore/events/group_join_fail.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ static void tox_event_group_join_fail_destruct(Tox_Event_Group_Join_Fail *group_
bool tox_event_group_join_fail_pack(
const Tox_Event_Group_Join_Fail *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->fail_type);
&& tox_group_join_fail_pack(event->fail_type, bp);
}

non_null()
Expand All @@ -85,7 +84,7 @@ static bool tox_event_group_join_fail_unpack_into(
}

return bin_unpack_u32(bu, &event->group_number)
&& tox_group_join_fail_unpack(bu, &event->fail_type);
&& tox_group_join_fail_unpack(&event->fail_type, bu);
}


Expand Down
5 changes: 2 additions & 3 deletions toxcore/events/group_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,10 @@ static void tox_event_group_message_destruct(Tox_Event_Group_Message *group_mess
bool tox_event_group_message_pack(
const Tox_Event_Group_Message *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 5)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->peer_id)
&& bin_pack_u32(bp, event->type)
&& tox_message_type_pack(event->type, bp)
&& bin_pack_bin(bp, event->message, event->message_length)
&& bin_pack_u32(bp, event->message_id);
}
Expand All @@ -155,7 +154,7 @@ static bool tox_event_group_message_unpack_into(

return bin_unpack_u32(bu, &event->group_number)
&& bin_unpack_u32(bu, &event->peer_id)
&& tox_message_type_unpack(bu, &event->type)
&& tox_message_type_unpack(&event->type, bu)
&& bin_unpack_bin(bu, &event->message, &event->message_length)
&& bin_unpack_u32(bu, &event->message_id);
}
Expand Down
Loading

0 comments on commit a840cbd

Please sign in to comment.