diff options
author | Claudius "keldu" Holeksa <mail@keldu.de> | 2024-03-11 18:19:04 +0100 |
---|---|---|
committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2024-03-11 18:19:04 +0100 |
commit | 0c4126e1ab9012cb2fc27dbf0c2242130ddd03d6 (patch) | |
tree | 108e2f8b4e693eafebaff22d7bd1c8ea5c537433 /modules/tools | |
parent | 2db5e753e2fb6b2d7333abafcd1c6994cb32682f (diff) |
tools: Added context creation declaration
Diffstat (limited to 'modules/tools')
-rw-r--r-- | modules/tools/c++/c_gen_iface.hpp | 126 | ||||
-rw-r--r-- | 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<typename T, uint64_t L> -struct lang_bind<schema::Primitive<T,L>, binding::C> { +struct lang_bind<schema::Primitive<T,L>, binding::SyncC> { using Schema = schema::Primitive<T,L>; static error_or<void> generate(buffer& buff, buffer& src, const language_binding_config& cfg, language_binding_state& state){ @@ -201,7 +201,7 @@ struct lang_bind<schema::Primitive<T,L>, binding::C> { }; template<typename T, uint64_t D> -struct lang_bind<schema::Array<T,D>, binding::C> { +struct lang_bind<schema::Array<T,D>, binding::SyncC> { using Schema = schema::Array<T,D>; static_assert(is_primitive<T>::value, "Currently only primitive type arrays are supported"); @@ -213,13 +213,13 @@ struct lang_bind<schema::Array<T,D>, binding::C> { auto emp = state.hashes.emplace(std::make_pair(hash, hash_type_str)); if(emp.second){ { - auto eov = lang_bind<schema::UInt64, binding::C>::generate(buff, src, cfg, state); + auto eov = lang_bind<schema::UInt64, binding::SyncC>::generate(buff, src, cfg, state); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind<T, binding::C>::generate(buff, src, cfg, state); + auto eov = lang_bind<T, binding::SyncC>::generate(buff, src, cfg, state); if(eov.is_error()){ return eov; } @@ -274,7 +274,7 @@ struct lang_bind<schema::Array<T,D>, binding::C> { }; template<typename Input, typename Output> -struct lang_bind<schema::Function<Input, Output>, binding::C> { +struct lang_bind<schema::Function<Input, Output>, binding::SyncC> { static error_or<void> 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<schema::Function<Input, Output>, 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<schema::Function<Input, Output>, binding::C> { constexpr uint64_t output_hash = schema_hash<Output>::apply(); { - auto eov = lang_bind<Input, binding::C>::generate(buff, src, cfg, state); + auto eov = lang_bind<Input, binding::SyncC>::generate(buff, src, cfg, state); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind<Output, binding::C>::generate(buff, src, cfg, state); + auto eov = lang_bind<Output, binding::SyncC>::generate(buff, src, cfg, state); if(eov.is_error()){ return eov; } @@ -476,7 +488,7 @@ struct lang_bind<schema::Function<Input, Output>, binding::C> { }; template<typename... M> -struct lang_bind<schema::Interface<M...>, binding::C> { +struct lang_bind<schema::Interface<M...>, binding::SyncC> { template<uint64_t i> static error_or<void> generate_element(buffer& buff, buffer& src, const language_binding_config& cfg, language_binding_state& state){ using Member = typename parameter_pack_type<i, M...>::type; @@ -484,7 +496,7 @@ struct lang_bind<schema::Interface<M...>, binding::C> { static constexpr string_literal MKey = Member::KeyLiteral; { - auto eov = lang_bind<MValue, binding::C>::generate(buff, src, cfg, state, MKey.view()); + auto eov = lang_bind<MValue, binding::SyncC>::generate(buff, src, cfg, state, MKey.view()); if(eov.is_error()){ return eov; } @@ -502,6 +514,94 @@ struct lang_bind<schema::Interface<M...>, 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<void>{ + { + 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<typename IfaceSchema> -struct language_binding<IfaceSchema, binding::C> { +struct language_binding<IfaceSchema, binding::SyncC> { static error_or<void> generate(buffer& buff, buffer& src, const language_binding_config& cfg){ - return impl::lang_bind<IfaceSchema, binding::C>::generate(buff, src, cfg); + return impl::lang_bind<IfaceSchema, binding::SyncC>::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<Schema, binding::C>::generate(r_buff, r_src_buff, {"prefix"}); + auto eov = language_binding<Schema, binding::SyncC>::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()}); } |