Skip to content

Commit e2c01e4

Browse files
committed
refactor: Use enum-specific pack functions for enum values.
It's more obvious this way.
1 parent afc4724 commit e2c01e4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+207
-109
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@ set(toxcore_SOURCES
338338
toxcore/tox.h
339339
toxcore/tox_private.c
340340
toxcore/tox_private.h
341+
toxcore/tox_pack.c
342+
toxcore/tox_pack.h
341343
toxcore/tox_unpack.c
342344
toxcore/tox_unpack.h
343345
toxcore/util.c
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
50123098ff16637a19253e4ea24c0b7bc7d1ef35f6e7c9f2783bddd303a6c6f3 /usr/local/bin/tox-bootstrapd
1+
0b904988d79b9576bb88c6c7316d107b5a61bd6119a0992ebd7c1fa43db70abf /usr/local/bin/tox-bootstrapd

other/event_tooling/generate_event_c.cpp

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,31 @@ std::string bin_pack_name_from_type(const std::string& type) {
5656
return "bin_pack_u8";
5757
} else if (type == "bool") {
5858
return "bin_pack_bool";
59-
// only unpack is special TODO(Green-Sky): should we change that?
60-
//} else if (type == "Tox_User_Status") {
61-
//return "tox_pack_user_status";
62-
//} else if (type == "Tox_Conference_Type") {
63-
//return "tox_pack_conference_type";
59+
} else if (type == "Tox_User_Status") {
60+
return "tox_user_status_pack";
61+
} else if (type == "Tox_Conference_Type") {
62+
return "tox_conference_type_pack";
63+
} else if (type == "Tox_Message_Type") {
64+
return "tox_message_type_pack";
65+
} else if (type == "Tox_File_Control") {
66+
return "tox_file_control_pack";
67+
} else if (type == "Tox_Connection") {
68+
return "tox_connection_pack";
69+
} else if (type == "Tox_Group_Privacy_State") {
70+
return "tox_group_privacy_state_pack";
71+
} else if (type == "Tox_Group_Voice_State") {
72+
return "tox_group_voice_state_pack";
73+
} else if (type == "Tox_Group_Topic_Lock") {
74+
return "tox_group_topic_lock_pack";
75+
} else if (type == "Tox_Group_Join_Fail") {
76+
return "tox_group_join_fail_pack";
77+
} else if (type == "Tox_Group_Mod_Event") {
78+
return "tox_group_mod_event_pack";
79+
} else if (type == "Tox_Group_Exit_Type") {
80+
return "tox_group_exit_type_pack";
6481
} else {
65-
//std::cerr << "unknown type " << type << "\n";
66-
//exit(1);
67-
// assume enum -> u32
82+
std::cerr << "unknown type " << type << "\n";
83+
exit(1);
6884
return "bin_pack_u32";
6985
}
7086
}
@@ -164,6 +180,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
164180
#include "../tox_events.h")";
165181
if (need_tox_unpack_h) {
166182
f << R"(
183+
#include "../tox_pack.h"
167184
#include "../tox_unpack.h")";
168185
}
169186
f << R"(
@@ -310,7 +327,6 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
310327
// pack
311328
f << "bool tox_event_" << event_name_l << "_pack(\n";
312329
f << " const Tox_Event_" << event_name << " *event, Bin_Pack *bp)\n{\n";
313-
f << " assert(event != nullptr);\n";
314330

315331
bool return_started = false;
316332

@@ -330,7 +346,11 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
330346
overloaded{
331347
[&](const EventTypeTrivial& t) {
332348
f << bin_pack_name_from_type(t.type);
333-
f << "(bp, event->" << t.name << ")";
349+
if (t.type.rfind("Tox_", 0) == 0) {
350+
f << "(event->" << t.name << ", bp)";
351+
} else {
352+
f << "(bp, event->" << t.name << ")";
353+
}
334354
},
335355
[&](const EventTypeByteRange& t) {
336356
f << "bin_pack_bin(bp, event->" << t.name_data << ", event->" << t.name_length << ")";
@@ -361,7 +381,11 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
361381
overloaded{
362382
[&](const EventTypeTrivial& t) {
363383
f << bin_unpack_name_from_type(t.type);
364-
f << "(bu, &event->" << t.name << ")";
384+
if (t.type.rfind("Tox_", 0) == 0) {
385+
f << "(&event->" << t.name << ", bu)";
386+
} else {
387+
f << "(bu, &event->" << t.name << ")";
388+
}
365389
},
366390
[&](const EventTypeByteRange& t) {
367391
f << "bin_unpack_bin(bu, &event->" << t.name_data << ", &event->" << t.name_length << ")";

toxcore/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,19 @@ cc_test(
892892
],
893893
)
894894

895+
cc_library(
896+
name = "tox_pack",
897+
srcs = ["tox_pack.c"],
898+
hdrs = ["tox_pack.h"],
899+
visibility = ["//c-toxcore:__subpackages__"],
900+
deps = [
901+
":attributes",
902+
":bin_pack",
903+
":ccompat",
904+
":tox",
905+
],
906+
)
907+
895908
cc_library(
896909
name = "tox_unpack",
897910
srcs = ["tox_unpack.c"],
@@ -926,6 +939,7 @@ cc_library(
926939
":ccompat",
927940
":mem",
928941
":tox",
942+
":tox_pack",
929943
":tox_unpack",
930944
"//c-toxcore/third_party:cmp",
931945
],

toxcore/Makefile.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \
9494
../toxcore/tox_event.c \
9595
../toxcore/tox_events.h \
9696
../toxcore/tox_events.c \
97+
../toxcore/tox_pack.h \
98+
../toxcore/tox_pack.c \
9799
../toxcore/tox_unpack.h \
98100
../toxcore/tox_unpack.c \
99101
../toxcore/tox_private.c \

toxcore/events/conference_connected.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ static void tox_event_conference_connected_destruct(Tox_Event_Conference_Connect
5454
bool tox_event_conference_connected_pack(
5555
const Tox_Event_Conference_Connected *event, Bin_Pack *bp)
5656
{
57-
assert(event != nullptr);
5857
return bin_pack_u32(bp, event->conference_number);
5958
}
6059

toxcore/events/conference_invite.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "../mem.h"
1515
#include "../tox.h"
1616
#include "../tox_events.h"
17+
#include "../tox_pack.h"
1718
#include "../tox_unpack.h"
1819

1920

@@ -107,10 +108,9 @@ static void tox_event_conference_invite_destruct(Tox_Event_Conference_Invite *co
107108
bool tox_event_conference_invite_pack(
108109
const Tox_Event_Conference_Invite *event, Bin_Pack *bp)
109110
{
110-
assert(event != nullptr);
111111
return bin_pack_array(bp, 3)
112112
&& bin_pack_u32(bp, event->friend_number)
113-
&& bin_pack_u32(bp, event->type)
113+
&& tox_conference_type_pack(event->type, bp)
114114
&& bin_pack_bin(bp, event->cookie, event->cookie_length);
115115
}
116116

@@ -124,7 +124,7 @@ static bool tox_event_conference_invite_unpack_into(
124124
}
125125

126126
return bin_unpack_u32(bu, &event->friend_number)
127-
&& tox_conference_type_unpack(bu, &event->type)
127+
&& tox_conference_type_unpack(&event->type, bu)
128128
&& bin_unpack_bin(bu, &event->cookie, &event->cookie_length);
129129
}
130130

toxcore/events/conference_message.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "../mem.h"
1515
#include "../tox.h"
1616
#include "../tox_events.h"
17+
#include "../tox_pack.h"
1718
#include "../tox_unpack.h"
1819

1920

@@ -121,11 +122,10 @@ static void tox_event_conference_message_destruct(Tox_Event_Conference_Message *
121122
bool tox_event_conference_message_pack(
122123
const Tox_Event_Conference_Message *event, Bin_Pack *bp)
123124
{
124-
assert(event != nullptr);
125125
return bin_pack_array(bp, 4)
126126
&& bin_pack_u32(bp, event->conference_number)
127127
&& bin_pack_u32(bp, event->peer_number)
128-
&& bin_pack_u32(bp, event->type)
128+
&& tox_message_type_pack(event->type, bp)
129129
&& bin_pack_bin(bp, event->message, event->message_length);
130130
}
131131

@@ -140,7 +140,7 @@ static bool tox_event_conference_message_unpack_into(
140140

141141
return bin_unpack_u32(bu, &event->conference_number)
142142
&& bin_unpack_u32(bu, &event->peer_number)
143-
&& tox_message_type_unpack(bu, &event->type)
143+
&& tox_message_type_unpack(&event->type, bu)
144144
&& bin_unpack_bin(bu, &event->message, &event->message_length);
145145
}
146146

toxcore/events/conference_peer_list_changed.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ static void tox_event_conference_peer_list_changed_destruct(Tox_Event_Conference
5454
bool tox_event_conference_peer_list_changed_pack(
5555
const Tox_Event_Conference_Peer_List_Changed *event, Bin_Pack *bp)
5656
{
57-
assert(event != nullptr);
5857
return bin_pack_u32(bp, event->conference_number);
5958
}
6059

toxcore/events/conference_peer_name.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ static void tox_event_conference_peer_name_destruct(Tox_Event_Conference_Peer_Na
106106
bool tox_event_conference_peer_name_pack(
107107
const Tox_Event_Conference_Peer_Name *event, Bin_Pack *bp)
108108
{
109-
assert(event != nullptr);
110109
return bin_pack_array(bp, 3)
111110
&& bin_pack_u32(bp, event->conference_number)
112111
&& bin_pack_u32(bp, event->peer_number)

0 commit comments

Comments
 (0)