tomato-testing/other/event_tooling/generate_event_c.cpp

820 lines
30 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: GPL-3.0-or-later
Squashed 'external/toxcore/c-toxcore/' changes from f1df709b87..8f0d505f9a 8f0d505f9a feat: add ngc events 9b8216e70c refactor: Make event dispatch ordered by receive time. 814c12a6f4 cleanup: Add dynamically derived array sizes to the API. 226b23be12 cleanup: Add explicit array sizes to toxencryptsave. ef33cb4de0 cleanup: Add Toxav alias for ToxAV. 1da723b34d cleanup: Make Tox_Options a typedef. b148a2afff chore: Simplify msvc build using vcpkg. 5cac6d7eb1 cleanup: Move `tox_get_system` out of the public API. c9ca4007e3 refactor: Align group message sending with other send functions. 6c6c0b1b1b cleanup: Make setters take non-const `Tox *`. a76f758d70 cleanup: Mark arrays in the tox API as `[]` instead of `*`. baf6d1f6cf cleanup: Make array params in toxav `[]` instead of `*`. 79f55bd06a cleanup: Put the size of fixed arrays into the API types. 1e73698db2 cleanup: Add typedefs for public API int identifiers. cac074c57f chore: Add fetch-sha256 script to update bootstrap node hash. 32576656bb Make the comment capitalization uniform aff4dda17c Spellcheck tox-bootstrapd 40b5fbbe9d chore: Remove settings.yml in favour of hs-github-tools. ebafd51be7 chore: Use GPL license with https. 0e42752f0f cleanup: Move all vptr-to-ptr casts to the beginning of a function. 5407384211 cleanup: Use github actions matrix to simplify CI. 82d8265688 fix: Use QueryPerformanceCounter on windows for monotonic time. 1224e656e3 chore: Add `net_(new|kill)_strerror` to cppcheck's allocators. 6a90ddfe4e cleanup: Run clang-tidy on headers, as well. bd930cc80a cleanup: Make TCP connection failures a warning instead of error. fad6e4e173 cleanup: Make all .c files include the headers they need. ef4897a898 cleanup: Upgrade to clang-tidy-17 and fix some warnings. REVERT: f1df709b87 feat: add ngc events REVERT: 1b6c907235 refactor: Make event dispatch ordered by receive time. git-subtree-dir: external/toxcore/c-toxcore git-subtree-split: 8f0d505f9a598cc41c682178e1589bcc01efe9cb
2024-01-09 16:39:05 +01:00
// Copyright © 2023-2024 The TokTok team.
// this file can be used to generate event.c files
// requires c++17
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <variant>
#include <map>
std::string str_tolower(std::string s) {
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c){ return std::tolower(c); });
return s;
}
std::string str_toupper(std::string s) {
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c){ return std::toupper(c); });
return s;
}
std::string output_folder = "out";
struct EventTypeTrivial {
std::string type; // eg uint32_t
std::string name; // eg friend_number
};
struct EventTypeByteRange {
// type is always uint8_t * for data AND uint32_t for length
std::string name_data; // eg data
std::string name_length; // eg data_length
std::string name_length_cb; // eg length // TODO: merge with normal?
};
// TODO: EventTypeByteArray
using EventType = std::variant<EventTypeTrivial, EventTypeByteRange>;
// helper type for the visitor #4
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
// explicit deduction guide (not needed as of C++20)
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
std::string bin_pack_name_from_type(const std::string& type) {
if (type == "uint64_t") {
return "bin_pack_u64";
} else if (type == "uint32_t") {
return "bin_pack_u32";
} else if (type == "uint16_t") {
return "bin_pack_u16";
} else if (type == "uint8_t") {
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 {
//std::cerr << "unknown type " << type << "\n";
//exit(1);
// assume enum -> u32
return "bin_pack_u32";
}
}
std::string bin_unpack_name_from_type(const std::string& type) {
if (type == "uint64_t") {
return "bin_unpack_u64";
} else if (type == "uint32_t") {
return "bin_unpack_u32";
} else if (type == "uint16_t") {
return "bin_unpack_u16";
} else if (type == "uint8_t") {
return "bin_unpack_u8";
} else if (type == "bool") {
return "bin_unpack_bool";
} else if (type == "Tox_User_Status") {
return "tox_user_status_unpack";
} else if (type == "Tox_Conference_Type") {
return "tox_conference_type_unpack";
} else if (type == "Tox_Message_Type") {
return "tox_message_type_unpack";
} else if (type == "Tox_File_Control") {
return "tox_file_control_unpack";
} else if (type == "Tox_Connection") {
return "tox_connection_unpack";
} else if (type == "Tox_Group_Privacy_State") {
return "tox_group_privacy_state_unpack";
} else if (type == "Tox_Group_Voice_State") {
return "tox_group_voice_state_unpack";
} else if (type == "Tox_Group_Topic_Lock") {
return "tox_group_topic_lock_unpack";
} else if (type == "Tox_Group_Join_Fail") {
return "tox_group_join_fail_unpack";
} else if (type == "Tox_Group_Mod_Event") {
return "tox_group_mod_event_unpack";
} else if (type == "Tox_Group_Exit_Type") {
return "tox_group_exit_type_unpack";
} else {
std::cerr << "unknown type " << type << "\n";
exit(1);
// assume enum -> u32
return "bin_unpack_u32";
}
}
void generate_event_impl(const std::string& event_name, const std::vector<EventType>& event_types) {
const std::string event_name_l = str_tolower(event_name);
std::string file_name = output_folder + "/" + event_name_l + ".c";
std::ofstream f(file_name);
if (!f.good()) {
std::cerr << "error opening file to write " << file_name << "\n";
exit(1);
}
Squashed 'external/toxcore/c-toxcore/' changes from f1df709b87..8f0d505f9a 8f0d505f9a feat: add ngc events 9b8216e70c refactor: Make event dispatch ordered by receive time. 814c12a6f4 cleanup: Add dynamically derived array sizes to the API. 226b23be12 cleanup: Add explicit array sizes to toxencryptsave. ef33cb4de0 cleanup: Add Toxav alias for ToxAV. 1da723b34d cleanup: Make Tox_Options a typedef. b148a2afff chore: Simplify msvc build using vcpkg. 5cac6d7eb1 cleanup: Move `tox_get_system` out of the public API. c9ca4007e3 refactor: Align group message sending with other send functions. 6c6c0b1b1b cleanup: Make setters take non-const `Tox *`. a76f758d70 cleanup: Mark arrays in the tox API as `[]` instead of `*`. baf6d1f6cf cleanup: Make array params in toxav `[]` instead of `*`. 79f55bd06a cleanup: Put the size of fixed arrays into the API types. 1e73698db2 cleanup: Add typedefs for public API int identifiers. cac074c57f chore: Add fetch-sha256 script to update bootstrap node hash. 32576656bb Make the comment capitalization uniform aff4dda17c Spellcheck tox-bootstrapd 40b5fbbe9d chore: Remove settings.yml in favour of hs-github-tools. ebafd51be7 chore: Use GPL license with https. 0e42752f0f cleanup: Move all vptr-to-ptr casts to the beginning of a function. 5407384211 cleanup: Use github actions matrix to simplify CI. 82d8265688 fix: Use QueryPerformanceCounter on windows for monotonic time. 1224e656e3 chore: Add `net_(new|kill)_strerror` to cppcheck's allocators. 6a90ddfe4e cleanup: Run clang-tidy on headers, as well. bd930cc80a cleanup: Make TCP connection failures a warning instead of error. fad6e4e173 cleanup: Make all .c files include the headers they need. ef4897a898 cleanup: Upgrade to clang-tidy-17 and fix some warnings. REVERT: f1df709b87 feat: add ngc events REVERT: 1b6c907235 refactor: Make event dispatch ordered by receive time. git-subtree-dir: external/toxcore/c-toxcore git-subtree-split: 8f0d505f9a598cc41c682178e1589bcc01efe9cb
2024-01-09 16:39:05 +01:00
bool need_stdlib_h = false;
bool need_string_h = false;
bool need_tox_unpack_h = false;
for (const auto& t : event_types) {
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
if (bin_unpack_name_from_type(t.type).rfind("tox_", 0) == 0) {
need_tox_unpack_h = true;
}
},
[&](const EventTypeByteRange&) {
need_stdlib_h = true;
need_string_h = true;
}
},
t
);
}
f << R"(/* SPDX-License-Identifier: GPL-3.0-or-later
Squashed 'external/toxcore/c-toxcore/' changes from f1df709b87..8f0d505f9a 8f0d505f9a feat: add ngc events 9b8216e70c refactor: Make event dispatch ordered by receive time. 814c12a6f4 cleanup: Add dynamically derived array sizes to the API. 226b23be12 cleanup: Add explicit array sizes to toxencryptsave. ef33cb4de0 cleanup: Add Toxav alias for ToxAV. 1da723b34d cleanup: Make Tox_Options a typedef. b148a2afff chore: Simplify msvc build using vcpkg. 5cac6d7eb1 cleanup: Move `tox_get_system` out of the public API. c9ca4007e3 refactor: Align group message sending with other send functions. 6c6c0b1b1b cleanup: Make setters take non-const `Tox *`. a76f758d70 cleanup: Mark arrays in the tox API as `[]` instead of `*`. baf6d1f6cf cleanup: Make array params in toxav `[]` instead of `*`. 79f55bd06a cleanup: Put the size of fixed arrays into the API types. 1e73698db2 cleanup: Add typedefs for public API int identifiers. cac074c57f chore: Add fetch-sha256 script to update bootstrap node hash. 32576656bb Make the comment capitalization uniform aff4dda17c Spellcheck tox-bootstrapd 40b5fbbe9d chore: Remove settings.yml in favour of hs-github-tools. ebafd51be7 chore: Use GPL license with https. 0e42752f0f cleanup: Move all vptr-to-ptr casts to the beginning of a function. 5407384211 cleanup: Use github actions matrix to simplify CI. 82d8265688 fix: Use QueryPerformanceCounter on windows for monotonic time. 1224e656e3 chore: Add `net_(new|kill)_strerror` to cppcheck's allocators. 6a90ddfe4e cleanup: Run clang-tidy on headers, as well. bd930cc80a cleanup: Make TCP connection failures a warning instead of error. fad6e4e173 cleanup: Make all .c files include the headers they need. ef4897a898 cleanup: Upgrade to clang-tidy-17 and fix some warnings. REVERT: f1df709b87 feat: add ngc events REVERT: 1b6c907235 refactor: Make event dispatch ordered by receive time. git-subtree-dir: external/toxcore/c-toxcore git-subtree-split: 8f0d505f9a598cc41c682178e1589bcc01efe9cb
2024-01-09 16:39:05 +01:00
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
Squashed 'external/toxcore/c-toxcore/' changes from f1df709b87..8f0d505f9a 8f0d505f9a feat: add ngc events 9b8216e70c refactor: Make event dispatch ordered by receive time. 814c12a6f4 cleanup: Add dynamically derived array sizes to the API. 226b23be12 cleanup: Add explicit array sizes to toxencryptsave. ef33cb4de0 cleanup: Add Toxav alias for ToxAV. 1da723b34d cleanup: Make Tox_Options a typedef. b148a2afff chore: Simplify msvc build using vcpkg. 5cac6d7eb1 cleanup: Move `tox_get_system` out of the public API. c9ca4007e3 refactor: Align group message sending with other send functions. 6c6c0b1b1b cleanup: Make setters take non-const `Tox *`. a76f758d70 cleanup: Mark arrays in the tox API as `[]` instead of `*`. baf6d1f6cf cleanup: Make array params in toxav `[]` instead of `*`. 79f55bd06a cleanup: Put the size of fixed arrays into the API types. 1e73698db2 cleanup: Add typedefs for public API int identifiers. cac074c57f chore: Add fetch-sha256 script to update bootstrap node hash. 32576656bb Make the comment capitalization uniform aff4dda17c Spellcheck tox-bootstrapd 40b5fbbe9d chore: Remove settings.yml in favour of hs-github-tools. ebafd51be7 chore: Use GPL license with https. 0e42752f0f cleanup: Move all vptr-to-ptr casts to the beginning of a function. 5407384211 cleanup: Use github actions matrix to simplify CI. 82d8265688 fix: Use QueryPerformanceCounter on windows for monotonic time. 1224e656e3 chore: Add `net_(new|kill)_strerror` to cppcheck's allocators. 6a90ddfe4e cleanup: Run clang-tidy on headers, as well. bd930cc80a cleanup: Make TCP connection failures a warning instead of error. fad6e4e173 cleanup: Make all .c files include the headers they need. ef4897a898 cleanup: Upgrade to clang-tidy-17 and fix some warnings. REVERT: f1df709b87 feat: add ngc events REVERT: 1b6c907235 refactor: Make event dispatch ordered by receive time. git-subtree-dir: external/toxcore/c-toxcore git-subtree-split: 8f0d505f9a598cc41c682178e1589bcc01efe9cb
2024-01-09 16:39:05 +01:00
#include <assert.h>)";
if (need_stdlib_h) {
f << R"(
#include <stdlib.h>)";
}
if (need_string_h) {
f << R"(
#include <string.h>)";
}
f << R"(
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
Squashed 'external/toxcore/c-toxcore/' changes from f1df709b87..8f0d505f9a 8f0d505f9a feat: add ngc events 9b8216e70c refactor: Make event dispatch ordered by receive time. 814c12a6f4 cleanup: Add dynamically derived array sizes to the API. 226b23be12 cleanup: Add explicit array sizes to toxencryptsave. ef33cb4de0 cleanup: Add Toxav alias for ToxAV. 1da723b34d cleanup: Make Tox_Options a typedef. b148a2afff chore: Simplify msvc build using vcpkg. 5cac6d7eb1 cleanup: Move `tox_get_system` out of the public API. c9ca4007e3 refactor: Align group message sending with other send functions. 6c6c0b1b1b cleanup: Make setters take non-const `Tox *`. a76f758d70 cleanup: Mark arrays in the tox API as `[]` instead of `*`. baf6d1f6cf cleanup: Make array params in toxav `[]` instead of `*`. 79f55bd06a cleanup: Put the size of fixed arrays into the API types. 1e73698db2 cleanup: Add typedefs for public API int identifiers. cac074c57f chore: Add fetch-sha256 script to update bootstrap node hash. 32576656bb Make the comment capitalization uniform aff4dda17c Spellcheck tox-bootstrapd 40b5fbbe9d chore: Remove settings.yml in favour of hs-github-tools. ebafd51be7 chore: Use GPL license with https. 0e42752f0f cleanup: Move all vptr-to-ptr casts to the beginning of a function. 5407384211 cleanup: Use github actions matrix to simplify CI. 82d8265688 fix: Use QueryPerformanceCounter on windows for monotonic time. 1224e656e3 chore: Add `net_(new|kill)_strerror` to cppcheck's allocators. 6a90ddfe4e cleanup: Run clang-tidy on headers, as well. bd930cc80a cleanup: Make TCP connection failures a warning instead of error. fad6e4e173 cleanup: Make all .c files include the headers they need. ef4897a898 cleanup: Upgrade to clang-tidy-17 and fix some warnings. REVERT: f1df709b87 feat: add ngc events REVERT: 1b6c907235 refactor: Make event dispatch ordered by receive time. git-subtree-dir: external/toxcore/c-toxcore git-subtree-split: 8f0d505f9a598cc41c682178e1589bcc01efe9cb
2024-01-09 16:39:05 +01:00
#include "../mem.h"
#include "../tox.h"
Squashed 'external/toxcore/c-toxcore/' changes from f1df709b87..8f0d505f9a 8f0d505f9a feat: add ngc events 9b8216e70c refactor: Make event dispatch ordered by receive time. 814c12a6f4 cleanup: Add dynamically derived array sizes to the API. 226b23be12 cleanup: Add explicit array sizes to toxencryptsave. ef33cb4de0 cleanup: Add Toxav alias for ToxAV. 1da723b34d cleanup: Make Tox_Options a typedef. b148a2afff chore: Simplify msvc build using vcpkg. 5cac6d7eb1 cleanup: Move `tox_get_system` out of the public API. c9ca4007e3 refactor: Align group message sending with other send functions. 6c6c0b1b1b cleanup: Make setters take non-const `Tox *`. a76f758d70 cleanup: Mark arrays in the tox API as `[]` instead of `*`. baf6d1f6cf cleanup: Make array params in toxav `[]` instead of `*`. 79f55bd06a cleanup: Put the size of fixed arrays into the API types. 1e73698db2 cleanup: Add typedefs for public API int identifiers. cac074c57f chore: Add fetch-sha256 script to update bootstrap node hash. 32576656bb Make the comment capitalization uniform aff4dda17c Spellcheck tox-bootstrapd 40b5fbbe9d chore: Remove settings.yml in favour of hs-github-tools. ebafd51be7 chore: Use GPL license with https. 0e42752f0f cleanup: Move all vptr-to-ptr casts to the beginning of a function. 5407384211 cleanup: Use github actions matrix to simplify CI. 82d8265688 fix: Use QueryPerformanceCounter on windows for monotonic time. 1224e656e3 chore: Add `net_(new|kill)_strerror` to cppcheck's allocators. 6a90ddfe4e cleanup: Run clang-tidy on headers, as well. bd930cc80a cleanup: Make TCP connection failures a warning instead of error. fad6e4e173 cleanup: Make all .c files include the headers they need. ef4897a898 cleanup: Upgrade to clang-tidy-17 and fix some warnings. REVERT: f1df709b87 feat: add ngc events REVERT: 1b6c907235 refactor: Make event dispatch ordered by receive time. git-subtree-dir: external/toxcore/c-toxcore git-subtree-split: 8f0d505f9a598cc41c682178e1589bcc01efe9cb
2024-01-09 16:39:05 +01:00
#include "../tox_events.h")";
if (need_tox_unpack_h) {
f << R"(
#include "../tox_unpack.h")";
}
f << R"(
/*****************************************************
*
* :: struct and accessors
*
*****************************************************/
)";
// gen struct
f << "struct Tox_Event_" << event_name << " {\n";
for (const auto& t : event_types) {
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
f << " " << t.type << " " << t.name << ";\n";
},
[&](const EventTypeByteRange& t) {
f << " " << "uint8_t" << " *" << t.name_data << ";\n";
f << " " << "uint32_t" << " " << t.name_length << ";\n";
}
},
t
);
}
f << "};\n\n";
// gen setters and getters
for (const auto& t : event_types) {
// setter
f << "non_null()\n";
f << "static " << (t.index() == 0 ? "void" : "bool") << " tox_event_" << event_name_l << "_set_";
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
f << t.name;
},
[&](const EventTypeByteRange& t) {
f << t.name_data;
}
},
t
);
f << "(Tox_Event_" << event_name << " *" << event_name_l << ",\n";
f << " ";
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
f << t.type << " " << t.name << ")\n";
},
[&](const EventTypeByteRange& t) {
f << "const uint8_t *" << t.name_data << ", uint32_t " << t.name_length << ")\n";
}
},
t
);
f << "{\n assert(" << event_name_l << " != nullptr);\n";
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
f << " " << event_name_l << "->" << t.name << " = " << t.name << ";\n";
},
[&](const EventTypeByteRange& t) {
f << "\n if (" << event_name_l << "->" << t.name_data << " != nullptr) {\n";
f << " free(" << event_name_l << "->" << t.name_data << ");\n";
f << " " << event_name_l << "->" << t.name_data << " = nullptr;\n";
f << " " << event_name_l << "->" << t.name_length << " = 0;\n";
f << " }\n\n";
f << " " << event_name_l << "->" << t.name_data << " = (uint8_t *)malloc(" << t.name_length << ");\n\n";
f << " if (" << event_name_l << "->" << t.name_data << " == nullptr) {\n";
f << " return false;\n }\n\n";
f << " memcpy(" << event_name_l << "->" << t.name_data << ", " << t.name_data << ", " << t.name_length << ");\n";
f << " " << event_name_l << "->" << t.name_length << " = " << t.name_length << ";\n";
f << " return true;\n";
}
},
t
);
f << "}\n";
// getter
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
//f << "non_null()\n"; // TODO: is this missing in the original?
f << t.type << " tox_event_" << event_name_l << "_get_" << t.name;
f << "(const Tox_Event_" << event_name << " *" << event_name_l << ")\n";
f << "{\n assert(" << event_name_l << " != nullptr);\n";
f << " return " << event_name_l << "->" << t.name << ";\n}\n\n";
},
[&](const EventTypeByteRange& t) {
//f << "non_null()\n"; // TODO: is this missing in the original?
f << "uint32_t tox_event_" << event_name_l << "_get_" << t.name_length;
f << "(const Tox_Event_" << event_name << " *" << event_name_l << ")\n";
f << "{\n assert(" << event_name_l << " != nullptr);\n";
f << " return " << event_name_l << "->" << t.name_length << ";\n}\n";
f << "const uint8_t *tox_event_" << event_name_l << "_get_" << t.name_data;
f << "(const Tox_Event_" << event_name << " *" << event_name_l << ")\n";
f << "{\n assert(" << event_name_l << " != nullptr);\n";
f << " return " << event_name_l << "->" << t.name_data << ";\n}\n\n";
}
},
t
);
}
// gen contruct
f << "non_null()\n";
f << "static void tox_event_" << event_name_l << "_construct(Tox_Event_" << event_name << " *" << event_name_l << ")\n{\n";
// TODO: initialize all members properly
// TODO: check if _NONE is universal
// str_toupper(
f << " *" << event_name_l << " = (Tox_Event_" << event_name << ") {\n 0\n };\n}\n";
// gen destruct
f << "non_null()\n";
f << "static void tox_event_" << event_name_l << "_destruct(Tox_Event_" << event_name << " *" << event_name_l << ", const Memory *mem)\n{\n";
size_t data_count = 0;
for (const auto& t : event_types) {
std::visit(
overloaded{
[&](const EventTypeTrivial&) {},
[&](const EventTypeByteRange& t) {
f << " free(" << event_name_l << "->" << t.name_data << ");\n";
//f << " mem->funcs->free(mem->obj, " << event_name_l << "->" << t.name_data << ");\n";
data_count++;
}
},
t
);
}
if (data_count == 0) {
f << " return;\n";
}
f << "}\n\n";
// 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";
f << " return bin_pack_array(bp, 2)\n";
f << " && bin_pack_u32(bp, TOX_EVENT_" << str_toupper(event_name) << ")";
if (event_types.size() > 1) {
f << "\n && bin_pack_array(bp, " << event_types.size() << ")";
}
for (const auto& t : event_types) {
f << "\n && ";
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
f << bin_pack_name_from_type(t.type);
f << "(bp, event->" << t.name << ")";
},
[&](const EventTypeByteRange& t) {
f << "bin_pack_bin(bp, event->" << t.name_data << ", event->" << t.name_length << ")";
}
},
t
);
}
f << ";\n}\n\n";
// unpack
f << "non_null()\n";
f << "static bool tox_event_" << event_name_l << "_unpack_into(\n";
f << " Tox_Event_" << event_name << " *event, Bin_Unpack *bu)\n{\n";
f << " assert(event != nullptr);\n";
if (event_types.size() > 1) {
f << " if (!bin_unpack_array_fixed(bu, " << event_types.size() << ", nullptr)) {\n return false;\n }\n\n";
}
bool first = true;
for (const auto& t : event_types) {
if (first) {
f << " return ";
} else {
f << "\n && ";
}
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
f << bin_unpack_name_from_type(t.type);
f << "(bu, &event->" << t.name << ")";
},
[&](const EventTypeByteRange& t) {
f << "bin_unpack_bin(bu, &event->" << t.name_data << ", &event->" << t.name_length << ")";
}
},
t
);
first = false;
}
f << ";\n}\n\n";
f << R"(
/*****************************************************
*
* :: new/free/add/get/size/unpack
*
*****************************************************/
)";
f << "const Tox_Event_" << event_name << " *tox_event_get_" << event_name_l << "(const Tox_Event *event)\n{\n";
f << " return event->type == TOX_EVENT_" << str_toupper(event_name) << " ? event->data." << event_name_l << " : nullptr;\n}\n\n";
// new
f << "Tox_Event_" << event_name << " *tox_event_" << event_name_l << "_new(const Memory *mem)\n{\n";
f << " Tox_Event_" << event_name << " *const " << event_name_l << " =\n";
f << " (Tox_Event_" << event_name << " *)mem_alloc(mem, sizeof(Tox_Event_" << event_name << "));\n\n";
f << " if (" << event_name_l << " == nullptr) {\n return nullptr;\n }\n\n";
f << " tox_event_" << event_name_l << "_construct(" << event_name_l << ");\n";
f << " return " << event_name_l << ";\n}\n\n";
// free
f << "void tox_event_" << event_name_l << "_free(Tox_Event_" << event_name << " *" << event_name_l << ", const Memory *mem)\n{\n";
f << " if (" << event_name_l << " != nullptr) {\n";
f << " tox_event_" << event_name_l << "_destruct(" << event_name_l << ", mem);\n }\n";
f << " mem_delete(mem, " << event_name_l << ");\n}\n\n";
// add
f << "non_null()\n";
f << "static Tox_Event_" << event_name << " *tox_events_add_" << event_name_l << "(Tox_Events *events, const Memory *mem)\n{\n";
f << " Tox_Event_" << event_name << " *const " << event_name_l << " = tox_event_" << event_name_l << "_new(mem);\n\n";
f << " if (" << event_name_l << " == nullptr) {\n";
f << " return nullptr;\n }\n\n";
f << " Tox_Event event;\n";
f << " event.type = TOX_EVENT_" << str_toupper(event_name) << ";\n";
f << " event.data." << event_name_l << " = " << event_name_l << ";\n\n";
f << " tox_events_add(events, &event);\n";
f << " return " << event_name_l << ";\n}\n\n";
// get
f << "const Tox_Event_" << event_name << " *tox_events_get_" << event_name_l << "(const Tox_Events *events, uint32_t index)\n{\n";
f << " uint32_t " << event_name_l << "_index = 0;\n";
f << " const uint32_t size = tox_events_get_size(events);\n\n";
f << " for (uint32_t i = 0; i < size; ++i) {\n";
f << " if (" << event_name_l << "_index > index) {\n";
f << " return nullptr;\n }\n\n";
f << " if (events->events[i].type == TOX_EVENT_" << str_toupper(event_name) << ") {\n";
f << " const Tox_Event_" << event_name << " *" << event_name_l << " = events->events[i].data." << event_name_l << ";\n";
f << " if (" << event_name_l << "_index == index) {\n";
f << " return " << event_name_l << ";\n }\n";
f << " ++" << event_name_l << "_index;\n }\n }\n\n return nullptr;\n}\n\n";
// get size
f << "uint32_t tox_events_get_" << event_name_l << "_size(const Tox_Events *events)\n{\n";
f << " uint32_t " << event_name_l << "_size = 0;\n";
f << " const uint32_t size = tox_events_get_size(events);\n\n";
f << " for (uint32_t i = 0; i < size; ++i) {\n";
f << " if (events->events[i].type == TOX_EVENT_" << str_toupper(event_name) << ") {\n";
f << " ++" << event_name_l << "_size;\n }\n }\n\n";
f << " return " << event_name_l << "_size;\n}\n\n";
// unpack
f << "bool tox_event_" << event_name_l << "_unpack(\n";
f << " Tox_Event_" << event_name << " **event, Bin_Unpack *bu, const Memory *mem)\n{\n";
f << " assert(event != nullptr);\n";
f << " *event = tox_event_" << event_name_l << "_new(mem);\n\n";
f << " if (*event == nullptr) {\n return false;\n }\n\n";
f << " return tox_event_" << event_name_l << "_unpack_into(*event, bu);\n}\n\n";
// alloc
f << "non_null()\n";
f << "static Tox_Event_" << event_name << " *tox_event_" << event_name_l << "_alloc(void *user_data)\n{\n";
f << " Tox_Events_State *state = tox_events_alloc(user_data);\n";
f << " assert(state != nullptr);\n\n";
f << " if (state->events == nullptr) {\n return nullptr;\n }\n\n";
f << " Tox_Event_" << event_name << " *" << event_name_l << " = tox_events_add_" << event_name_l << "(state->events, state->mem);\n\n";
f << " if (" << event_name_l << " == nullptr) {\n";
f << " state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;\n return nullptr;\n }\n\n";
f << " return " << event_name_l << ";\n}\n\n";
f << R"(
/*****************************************************
*
* :: event handler
*
*****************************************************/
)";
f << "void tox_events_handle_" << event_name_l << "(Tox *tox";
for (const auto& t : event_types) {
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
f << ", " << t.type << " " << t.name;
},
[&](const EventTypeByteRange& t) {
f << ", const uint8_t *" << t.name_data << ", size_t " << t.name_length_cb;
}
},
t
);
}
f << ",\n void *user_data)\n{\n";
f << " Tox_Event_" << event_name << " *" << event_name_l << " = tox_event_" << event_name_l << "_alloc(user_data);\n\n";
f << " if (" << event_name_l << " == nullptr) {\n return;\n }\n\n";
for (const auto& t : event_types) {
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
f << " tox_event_" << event_name_l << "_set_" << t.name << "(" << event_name_l << ", " << t.name << ");\n";
},
[&](const EventTypeByteRange& t) {
f << " tox_event_" << event_name_l << "_set_" << t.name_data << "(" << event_name_l << ", " << t.name_data << ", " << t.name_length_cb << ");\n";
}
},
t
);
}
f << "}\n";
}
// c++ generate_event_c.cpp -std=c++17 && ./a.out Friend_Lossy_Packet && diff --color ../../toxcore/events/friend_lossy_packet.c out/friend_lossy_packet.c
int main(int argc, char** argv) {
// TODO: json?
std::map<std::string, std::vector<EventType>> event_defs {
{
"Conference_Connected",
{
EventTypeTrivial{"uint32_t", "conference_number"},
}
},
{
"Conference_Invite",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"Tox_Conference_Type", "type"},
EventTypeByteRange{"cookie", "cookie_length", "length"}, // the latter two are ideally the same
}
},
{
"Conference_Message",
{
EventTypeTrivial{"uint32_t", "conference_number"},
EventTypeTrivial{"uint32_t", "peer_number"},
EventTypeTrivial{"Tox_Message_Type", "type"},
EventTypeByteRange{"message", "message_length", "length"}, // the latter two are ideally the same
}
},
{
"Conference_Peer_List_Changed",
{
EventTypeTrivial{"uint32_t", "conference_number"},
}
},
{
"Conference_Peer_Name",
{
EventTypeTrivial{"uint32_t", "conference_number"},
EventTypeTrivial{"uint32_t", "peer_number"},
EventTypeByteRange{"name", "name_length", "length"}, // the latter two are ideally the same
}
},
{
"Conference_Title",
{
EventTypeTrivial{"uint32_t", "conference_number"},
EventTypeTrivial{"uint32_t", "peer_number"},
EventTypeByteRange{"title", "title_length", "length"}, // the latter two are ideally the same
}
},
{
"File_Chunk_Request",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"uint32_t", "file_number"},
EventTypeTrivial{"uint64_t", "position"},
EventTypeTrivial{"uint16_t", "length"},
}
},
{
"File_Recv",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"uint32_t", "file_number"},
EventTypeTrivial{"uint32_t", "kind"},
EventTypeTrivial{"uint64_t", "file_size"},
EventTypeByteRange{"filename", "filename_length", "filename_length"},
}
},
{
"File_Recv_Chunk",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"uint32_t", "file_number"},
EventTypeTrivial{"uint64_t", "position"},
EventTypeByteRange{"data", "data_length", "length"}, // the latter two are ideally the same
}
},
{
"File_Recv_Control",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"uint32_t", "file_number"},
EventTypeTrivial{"Tox_File_Control", "control"},
}
},
{
"Friend_Connection_Status",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"Tox_Connection", "connection_status"},
}
},
{
"Friend_Lossless_Packet",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeByteRange{"data", "data_length", "length"}, // the latter two are ideally the same
}
},
{
"Friend_Lossy_Packet",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeByteRange{"data", "data_length", "length"}, // the latter two are ideally the same
}
},
{
"Friend_Message",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"Tox_Message_Type", "type"},
EventTypeByteRange{"message", "message_length", "length"}, // the latter two are ideally the same
}
},
{
"Friend_Name",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeByteRange{"name", "name_length", "length"}, // the latter two are ideally the same
}
},
{
"Friend_Read_Receipt",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"uint32_t", "message_id"},
}
},
#if 0
{
"Friend_Request",
{
//EventTypeTrivial{"uint32_t", "friend_number"}, // public_key ByteArray
EventTypeByteRange{"message", "message_length", "length"}, // the latter two are ideally the same
}
},
#endif
{
"Friend_Status",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"Tox_User_Status", "status"},
}
},
{
"Friend_Status_Message",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeByteRange{"message", "message_length", "length"}, // the latter two are ideally the same
}
},
{
"Friend_Typing",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"bool", "typing"},
}
},
{
"Self_Connection_Status",
{
EventTypeTrivial{"Tox_Connection", "connection_status"},
}
},
{
"Group_Peer_Name",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_id"},
EventTypeByteRange{"name", "name_length", "length"}, // the latter two are ideally the same
}
},
{
"Group_Peer_Status",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_id"},
EventTypeTrivial{"Tox_User_Status", "status"},
}
},
{
"Group_Topic",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_id"},
EventTypeByteRange{"topic", "topic_length", "length"}, // the latter two are ideally the same
}
},
{
"Group_Privacy_State",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"Tox_Group_Privacy_State", "privacy_state"},
}
},
{
"Group_Voice_State",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"Tox_Group_Voice_State", "voice_state"},
}
},
{
"Group_Topic_Lock",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"Tox_Group_Topic_Lock", "topic_lock"},
}
},
{
"Group_Peer_Limit",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_limit"},
}
},
{
"Group_Password",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeByteRange{"password", "password_length", "length"}, // the latter two are ideally the same
}
},
{
"Group_Message",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_id"},
EventTypeTrivial{"Tox_Message_Type", "type"},
EventTypeByteRange{"message", "message_length", "length"}, // the latter two are ideally the same
EventTypeTrivial{"uint32_t", "message_id"},
}
},
{
"Group_Private_Message",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_id"},
EventTypeTrivial{"Tox_Message_Type", "type"},
EventTypeByteRange{"message", "message_length", "length"}, // the latter two are ideally the same
}
},
{
"Group_Custom_Packet",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_id"},
EventTypeByteRange{"data", "data_length", "length"}, // the latter two are ideally the same
}
},
{
"Group_Custom_Private_Packet",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_id"},
EventTypeByteRange{"data", "data_length", "length"}, // the latter two are ideally the same
}
},
{
"Group_Invite",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeByteRange{"invite_data", "invite_data_length", "length"}, // the latter two are ideally the same
EventTypeByteRange{"group_name", "group_name_length", "group_name_length"}, // they are :)
}
},
{
"Group_Peer_Join",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_id"},
}
},
{
"Group_Peer_Exit",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_id"},
EventTypeTrivial{"Tox_Group_Exit_Type", "exit_type"},
EventTypeByteRange{"name", "name_length", "name_length"}, // they are :)
EventTypeByteRange{"part_message", "part_message_length", "part_message_length"}, // they are :)
}
},
{
"Group_Self_Join",
{
EventTypeTrivial{"uint32_t", "group_number"},
}
},
{
"Group_Join_Fail",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"Tox_Group_Join_Fail", "fail_type"},
}
},
{
"Group_Moderation",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "source_peer_id"},
EventTypeTrivial{"uint32_t", "target_peer_id"},
EventTypeTrivial{"Tox_Group_Mod_Event", "mod_type"},
}
},
};
if (argc < 2) {
for (const auto& [event, event_types] : event_defs) {
generate_event_impl(event, event_types);
}
} else {
if (event_defs.count(argv[1])) {
generate_event_impl(argv[1], event_defs[argv[1]]);
} else {
std::cerr << "error: unknown event " << argv[1] << "\n";
return 1;
}
}
return 0;
}