diff options
Diffstat (limited to 'modules/tools/c++')
-rw-r--r-- | modules/tools/c++/c_gen_iface.hpp | 146 |
1 files changed, 138 insertions, 8 deletions
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<typename Schema> - static error_or<void> append_translation_func(std::string& buff, const std::string_view& prefix, bool c_to_cpp){ + static error_or<void> 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<Schema>::apply(); + + if(with_return_type) { auto eov = lang_bind_helper::append_string(buff, "saw::error_or<void> "); if(eov.is_error()){ @@ -488,29 +490,127 @@ struct lang_bind<schema::Struct<schema::Member<V,K>...>, binding::SyncC> { using Schema = schema::Struct<schema::Member<V,K>...>; template<uint64_t i> - static error_or<void> generate_translation_func(const language_binding_config& cfg, language_binding_state& state, std::string& buff, bool c_to_cpp){ + static error_or<void> 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<i,V...>::type; static constexpr string_literal Lit = parameter_key_pack_type<i,K...>::literal; constexpr uint32_t hash = schema_hash<MT>::apply(); auto& tpe = state.tp_elements; - uint64_t id; + if constexpr (i == 0){ + auto eov = lang_bind_helper::append_translation_func<Schema>(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<i+1u>(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<uint64_t i> - static error_or<void> generate_ele(const language_binding_config& cfg, language_binding_state& state, int64_t id){ + static error_or<void> generate_ele(const language_binding_config& cfg, language_binding_state& state, uint64_t id){ using MT = typename parameter_pack_type<i,V...>::type; static constexpr string_literal Lit = parameter_key_pack_type<i,K...>::literal; @@ -621,14 +721,16 @@ struct lang_bind<schema::Struct<schema::Member<V,K>...>, 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<schema::Array<T,D>, 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<Schema>(tpe.at(id).source, cfg.prefix, true); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_translation_func<Schema>(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{}; |