From ea306799624d0390074f6afa5d38644cce076c9f Mon Sep 17 00:00:00 2001 From: Claudius 'keldu' Holeksa Date: Wed, 24 Jul 2024 11:09:55 +0200 Subject: wip --- modules/tools/c++/c_gen_iface.hpp | 146 +++++++++++++++++++++++++++++++++++--- modules/tools/tests/c_iface.cpp | 3 +- 2 files changed, 140 insertions(+), 9 deletions(-) (limited to 'modules/tools') diff --git a/modules/tools/c++/c_gen_iface.hpp b/modules/tools/c++/c_gen_iface.hpp index 3a648a1..4fa881c 100644 --- a/modules/tools/c++/c_gen_iface.hpp +++ b/modules/tools/c++/c_gen_iface.hpp @@ -159,8 +159,10 @@ struct lang_bind_helper { } template - static error_or append_translation_func(std::string& buff, const std::string_view& prefix, bool c_to_cpp){ + static error_or append_translation_func(std::string& buff, const std::string_view& prefix, bool c_to_cpp, bool with_return_type = true){ constexpr uint32_t hash = schema_hash::apply(); + + if(with_return_type) { auto eov = lang_bind_helper::append_string(buff, "saw::error_or "); if(eov.is_error()){ @@ -488,29 +490,127 @@ struct lang_bind...>, binding::SyncC> { using Schema = schema::Struct...>; template - static error_or generate_translation_func(const language_binding_config& cfg, language_binding_state& state, std::string& buff, bool c_to_cpp){ + static error_or generate_translation_func(const language_binding_config& cfg, language_binding_state& state, bool c_to_cpp, uint64_t id){ using MT = typename parameter_pack_type::type; static constexpr string_literal Lit = parameter_key_pack_type::literal; constexpr uint32_t hash = schema_hash::apply(); auto& tpe = state.tp_elements; - uint64_t id; + if constexpr (i == 0){ + auto eov = lang_bind_helper::append_translation_func(tpe.at(id).source, cfg.prefix, c_to_cpp); + if(eov.is_error()){ + return eov; + } + } + if constexpr (i == 0) { - std::string hash_type_str = cfg.prefix + "_" + std::to_string(hash) + "_t"; + auto eov = lang_bind_helper::append_string(tpe.at(id).source, "{\n\t"); + if(eov.is_error()){ + return eov; + } } { - auto eov = lang_bind_helper::append_string(tpe.at(id).source, "\treturn void_t{};\n}\n"); + auto eov = lang_bind_helper::append_string(tpe.at(id).source, "{\n\t\tauto eov = "); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_hashed_type(tpe.at(id).source, cfg.prefix, hash); + if(eov.is_error()){ + return eov; + } + } + { + std::string_view trans = c_to_cpp ? "_translate_c_to_cpp (" : "_translate_cpp_to_c ("; + auto eov = lang_bind_helper::append_string(tpe.at(id).source, trans); + if(eov.is_error()){ + return eov; + } + } + if(c_to_cpp){ + auto eov = lang_bind_helper::append_string(tpe.at(id).source, "&((*c_val)."); + if(eov.is_error()){ + return eov; + } + } + if(c_to_cpp){ + auto eov = lang_bind_helper::append_string(tpe.at(id).source, std::string{Lit.view()}); + if(eov.is_error()){ + return eov; + } + } + if(c_to_cpp){ + auto eov = lang_bind_helper::append_string(tpe.at(id).source, "), cpp_val.template get<\""); + if(eov.is_error()){ + return eov; + } + } + if(c_to_cpp){ + auto eov = lang_bind_helper::append_string(tpe.at(id).source, std::string{Lit.view()}); + if(eov.is_error()){ + return eov; + } + } + if(c_to_cpp){ + auto eov = lang_bind_helper::append_string(tpe.at(id).source, "\">()"); + if(eov.is_error()){ + return eov; + } + } + if(!c_to_cpp){ + auto eov = lang_bind_helper::append_string(tpe.at(id).source, " cpp_val.template get<\""); + if(eov.is_error()){ + return eov; + } + } + if(!c_to_cpp){ + auto eov = lang_bind_helper::append_string(tpe.at(id).source, std::string{Lit.view()}); + if(eov.is_error()){ + return eov; + } + } + if(!c_to_cpp){ + auto eov = lang_bind_helper::append_string(tpe.at(id).source, "\">(), &((*c_val)."); + if(eov.is_error()){ + return eov; + } + } + if(!c_to_cpp){ + auto eov = lang_bind_helper::append_string(tpe.at(id).source, std::string{Lit.view()}); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(tpe.at(id).source, ") );\n\t\t");//cpp_output.set(*c_input);\n"); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(tpe.at(id).source, "if(eov.is_error()) return eov.get_error();\n\t}\n\t");//cpp_output.set(*c_input);\n"); if(eov.is_error()){ return eov; } } + if constexpr ( (i+1) < sizeof...(V) ){ + return generate_translation_func(cfg, state, c_to_cpp, id); + } + + { + auto eov = lang_bind_helper::append_string(tpe.at(id).source, "return void_t{};\n}\n"); + if(eov.is_error()){ + return eov; + } + } return void_t{}; } template - static error_or generate_ele(const language_binding_config& cfg, language_binding_state& state, int64_t id){ + static error_or generate_ele(const language_binding_config& cfg, language_binding_state& state, uint64_t id){ using MT = typename parameter_pack_type::type; static constexpr string_literal Lit = parameter_key_pack_type::literal; @@ -621,14 +721,16 @@ struct lang_bind...>, binding::SyncC> { return eov; } } + if constexpr ( sizeof...(V) > 0 ) { - auto eov = generate_translation_func<0>(tpe.at(id).source, cfg.prefix, true); + auto eov = generate_translation_func<0>(cfg, state, true, id); if(eov.is_error()){ return eov; } } + if constexpr ( sizeof...(V) > 0 ) { - auto eov = generate_translation_func<0>(tpe.at(id).source, cfg.prefix, false); + auto eov = generate_translation_func<0>(cfg, state, false, id); if(eov.is_error()){ return eov; } @@ -741,6 +843,34 @@ struct lang_bind, binding::SyncC> { return eov; } } + /** + * Source + */ + { + auto eov = lang_bind_helper::append_string(tpe.at(id).source, "namespace {\n"); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_translation_func(tpe.at(id).source, cfg.prefix, true); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_translation_func(tpe.at(id).source, cfg.prefix, false); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(tpe.at(id).source, "\n}\n"); + if(eov.is_error()){ + return eov; + } + } + } return void_t{}; diff --git a/modules/tools/tests/c_iface.cpp b/modules/tools/tests/c_iface.cpp index 35591f8..25a5d04 100644 --- a/modules/tools/tests/c_iface.cpp +++ b/modules/tools/tests/c_iface.cpp @@ -19,7 +19,8 @@ using TestArray = Array< >; using TestStructArray = Struct< - Member + Member, + Member >; using TestStructMore = Struct< -- cgit v1.2.3