From d765dfafc995e8f722847fb1d6ab189714af8987 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Mon, 25 Mar 2024 17:19:32 +0100 Subject: tools: Work on struct generation --- modules/tools/c++/c_gen_iface.hpp | 104 +++++++++++++++++++++++++++++++++++--- 1 file changed, 97 insertions(+), 7 deletions(-) (limited to 'modules/tools') diff --git a/modules/tools/c++/c_gen_iface.hpp b/modules/tools/c++/c_gen_iface.hpp index 1610553..7ee4613 100644 --- a/modules/tools/c++/c_gen_iface.hpp +++ b/modules/tools/c++/c_gen_iface.hpp @@ -229,7 +229,7 @@ struct lang_bind, binding::SyncC> { return void_t{}; } - static error_or generate(buffer& buff, buffer& src, const language_binding_config& cfg, language_binding_state& state){ + static error_or generate(buffer& head, buffer& src, const language_binding_config& cfg, language_binding_state& state){ constexpr uint64_t hash = schema_hash::apply(); { @@ -237,31 +237,31 @@ struct lang_bind, binding::SyncC> { auto emp = state.hashes.emplace(std::make_pair(hash, hash_type_str)); if(emp.second){ { - auto eov = lang_bind_helper::append_string(buff, "typedef "); + auto eov = lang_bind_helper::append_string(head, "typedef "); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind_helper::append_string(buff, c_primitive_string::value.view()); + auto eov = lang_bind_helper::append_string(head, c_primitive_string::value.view()); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind_helper::append_string(buff, " "); + auto eov = lang_bind_helper::append_string(head, " "); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind_helper::append_string(buff, hash_type_str); + auto eov = lang_bind_helper::append_string(head, hash_type_str); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind_helper::append_string(buff, ";\n"); + auto eov = lang_bind_helper::append_string(head, ";\n"); if(eov.is_error()){ return eov; } @@ -273,6 +273,87 @@ struct lang_bind, binding::SyncC> { } }; +template +struct lang_bind...>, binding::SyncC> { + using Schema = schema::Struct...>; + + template + static error_or generate_ele(buffer& head, buffer& src, const language_binding_config& cfg, language_binding_state& state){ + using MT = typename parameter_pack_type::type; + constexpr uint64_t hash = schema_hash::apply(); + std::string hash_type_str = cfg.prefix + "_" + std::to_string(hash) + "_t"; + auto emp = state.hashes.emplace(std::make_pair(hash, hash_type_str)); + /** + * If successful insertion did happen that means we have no struct def yet. + */ + if (emp.second) { + { + auto eov = lang_bind_helper::append_hashed_type(head, cfg.prefix, hash); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(head, "};\n"); + if(eov.is_error()){ + return eov; + } + } + } + + if constexpr ( (i+1) < sizeof...(V) ){ + return generate_ele(head, src, cfg, state); + } + return void_t{}; + } + + static error_or generate(buffer& head, buffer& src, const language_binding_config& cfg, language_binding_state& state){ + constexpr uint64_t hash = schema_hash::apply(); + + std::string hash_type_str = cfg.prefix + "_" + std::to_string(hash) + "_t"; + auto emp = state.hashes.emplace(std::make_pair(hash, hash_type_str)); + + /** + * If successful insertion did happen that means we have no struct def yet. + */ + if(emp.second){ + /** + * Generate struct type + */ + { + auto eov = lang_bind_helper::append_string(head, "struct "); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_hashed_type(head, cfg.prefix, hash); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(head, " {\n\t"); + if(eov.is_error()){ + return eov; + } + } + if constexpr ( sizeof...(V) > 0 ) { + auto eov = generate_ele<0>(head, src, cfg, state); + } + { + auto eov = lang_bind_helper::append_string(head, "\n};\n"); + if(eov.is_error()){ + return eov; + } + } + + } + + return void_t{}; + } +}; + template struct lang_bind, binding::SyncC> { using Schema = schema::Array; @@ -293,19 +374,28 @@ struct lang_bind, binding::SyncC> { std::string hash_type_str = cfg.prefix + "_" + std::to_string(hash) + "_t"; auto emp = state.hashes.emplace(std::make_pair(hash, hash_type_str)); if(emp.second){ + /** + * Guarante existance of length type + */ { auto eov = lang_bind::generate(buff, src, cfg, state); if(eov.is_error()){ return eov; } } + /** + * Guarante existance of inner type + */ { auto eov = lang_bind::generate(buff, src, cfg, state); if(eov.is_error()){ return eov; } } - + + /** + * Generate struct type + */ { auto eov = lang_bind_helper::append_string(buff, "struct "); if(eov.is_error()){ -- cgit v1.2.3