From 0c4126e1ab9012cb2fc27dbf0c2242130ddd03d6 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Mon, 11 Mar 2024 18:19:04 +0100 Subject: tools: Added context creation declaration --- modules/tools/c++/c_gen_iface.hpp | 126 ++++++++++++++++++++++++++++++++++---- modules/tools/tests/c_iface.cpp | 2 +- 2 files changed, 114 insertions(+), 14 deletions(-) diff --git a/modules/tools/c++/c_gen_iface.hpp b/modules/tools/c++/c_gen_iface.hpp index d671c65..a5d1de6 100644 --- a/modules/tools/c++/c_gen_iface.hpp +++ b/modules/tools/c++/c_gen_iface.hpp @@ -13,7 +13,7 @@ namespace saw { namespace binding { -struct C {}; +struct SyncC {}; } /** @@ -153,7 +153,7 @@ struct lang_bind_helper { template -struct lang_bind, binding::C> { +struct lang_bind, binding::SyncC> { using Schema = schema::Primitive; static error_or generate(buffer& buff, buffer& src, const language_binding_config& cfg, language_binding_state& state){ @@ -201,7 +201,7 @@ struct lang_bind, binding::C> { }; template -struct lang_bind, binding::C> { +struct lang_bind, binding::SyncC> { using Schema = schema::Array; static_assert(is_primitive::value, "Currently only primitive type arrays are supported"); @@ -213,13 +213,13 @@ struct lang_bind, binding::C> { auto emp = state.hashes.emplace(std::make_pair(hash, hash_type_str)); if(emp.second){ { - auto eov = lang_bind::generate(buff, src, cfg, state); + auto eov = lang_bind::generate(buff, src, cfg, state); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind::generate(buff, src, cfg, state); + auto eov = lang_bind::generate(buff, src, cfg, state); if(eov.is_error()){ return eov; } @@ -274,7 +274,7 @@ struct lang_bind, binding::C> { }; template -struct lang_bind, binding::C> { +struct lang_bind, binding::SyncC> { static error_or append_function_def(buffer& buff, const language_binding_config& cfg, language_binding_state& state, const std::string_view& f_name){ { auto eov = lang_bind_helper::append_string(buff, "int "); @@ -301,7 +301,19 @@ struct lang_bind, binding::C> { } } { - auto eov = lang_bind_helper::append_string(buff, " ( const "); + auto eov = lang_bind_helper::append_string(buff, " ( "); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, cfg.prefix); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, "_context* ctx, const "); if(eov.is_error()){ return eov; } @@ -363,13 +375,13 @@ struct lang_bind, binding::C> { constexpr uint64_t output_hash = schema_hash::apply(); { - auto eov = lang_bind::generate(buff, src, cfg, state); + auto eov = lang_bind::generate(buff, src, cfg, state); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind::generate(buff, src, cfg, state); + auto eov = lang_bind::generate(buff, src, cfg, state); if(eov.is_error()){ return eov; } @@ -476,7 +488,7 @@ struct lang_bind, binding::C> { }; template -struct lang_bind, binding::C> { +struct lang_bind, binding::SyncC> { template static error_or generate_element(buffer& buff, buffer& src, const language_binding_config& cfg, language_binding_state& state){ using Member = typename parameter_pack_type::type; @@ -484,7 +496,7 @@ struct lang_bind, binding::C> { static constexpr string_literal MKey = Member::KeyLiteral; { - auto eov = lang_bind::generate(buff, src, cfg, state, MKey.view()); + auto eov = lang_bind::generate(buff, src, cfg, state, MKey.view()); if(eov.is_error()){ return eov; } @@ -502,6 +514,94 @@ struct lang_bind, binding::C> { } language_binding_state state; + /** + * Setup context creation to bind interface to a context + */ + /** + * Context definition + */ + /// @todo Add Macro which reduces typing time ? Not really that much shorter :/ + // SAW_CALL_AND_RETURN_ON_ERROR(lang_bind_helper::append_string(buff, "struct ")); + { + auto eov = lang_bind_helper::append_string(buff, "struct "); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, cfg.prefix); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, "_context {\n\tvoid* ctx;\n};\n\n"); + if(eov.is_error()){ + return eov; + } + } + /** + * Create and destroy declaration + */ + auto ctx_func = [&](std::string_view str) -> error_or{ + { + auto eov = lang_bind_helper::append_string(buff, "int "); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, cfg.prefix); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, "_context_"); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, str); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, " ( "); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, cfg.prefix); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, "_context* ctx );\n\n"); + if(eov.is_error()){ + return eov; + } + } + + return void_t{}; + }; + + { + auto eov = ctx_func("create"); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = ctx_func("destroy"); + if(eov.is_error()){ + return eov; + } + } if constexpr (sizeof...(M) > 0){ return generate_element<0>(buff, src, cfg, state); @@ -518,9 +618,9 @@ struct language_binding { }; template -struct language_binding { +struct language_binding { static error_or generate(buffer& buff, buffer& src, const language_binding_config& cfg){ - return impl::lang_bind::generate(buff, src, cfg); + return impl::lang_bind::generate(buff, src, cfg); } }; } diff --git a/modules/tools/tests/c_iface.cpp b/modules/tools/tests/c_iface.cpp index 29cc7f9..676b5de 100644 --- a/modules/tools/tests/c_iface.cpp +++ b/modules/tools/tests/c_iface.cpp @@ -52,7 +52,7 @@ void test_generate(std::string& res, std::string& src){ ring_buffer r_src_buff{4u * 1024u * 1024u}; { - auto eov = language_binding::generate(r_buff, r_src_buff, {"prefix"}); + auto eov = language_binding::generate(r_buff, r_src_buff, {"prefix"}); SAW_EXPECT(eov.is_value(), std::string{"Couldn't generate interface info: "} + std::string{eov.get_error().get_message()}); } -- cgit v1.2.3