From f2c255560d4ac83bdcb87c2d1a75a95ec540508c Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Sun, 10 Mar 2024 19:27:46 +0100 Subject: codec,tools: Fixed Header generation --- modules/codec/c++/schema.hpp | 3 + modules/codec/c++/schema_hash.hpp | 20 ++-- modules/tools/c++/c_gen_iface.hpp | 218 ++++++++++++++++++++++++++++++++++---- modules/tools/tests/c_iface.cpp | 18 ++-- 4 files changed, 224 insertions(+), 35 deletions(-) diff --git a/modules/codec/c++/schema.hpp b/modules/codec/c++/schema.hpp index 5348cfe..82fc0a5 100644 --- a/modules/codec/c++/schema.hpp +++ b/modules/codec/c++/schema.hpp @@ -116,6 +116,9 @@ using VarLong = VariableLengthPrimitive; template struct Function { static constexpr string_literal name = "Function"; + + using RequestT = Request; + using ResponseT = Response; }; template struct Interface { diff --git a/modules/codec/c++/schema_hash.hpp b/modules/codec/c++/schema_hash.hpp index 90ab208..5e44497 100644 --- a/modules/codec/c++/schema_hash.hpp +++ b/modules/codec/c++/schema_hash.hpp @@ -125,6 +125,18 @@ struct schema_hash_seed> { } }; +template +struct schema_hash_seed> { + using Schema = schema::Member; + + static constexpr uint32_t apply(uint32_t seed){ + seed = hash_literal::apply(seed); + seed = schema_hash_seed::apply(seed); + seed = hash_literal::apply(seed); + return seed; + } +}; + template struct schema_hash_seed...>> { using Schema = schema::Struct...>; @@ -135,9 +147,7 @@ struct schema_hash_seed...>> { constexpr string_literal Lit = parameter_key_pack_type::literal; using MemberT = typename parameter_pack_type...>::type; - seed = hash_literal::apply(seed); - seed = schema_hash_seed::apply(seed); - seed = hash_literal::apply(seed); + seed = schema_hash_seed::apply(seed); if constexpr ( (i+1) < sizeof...(V) ){ return apply_ele(seed); @@ -176,9 +186,7 @@ struct schema_hash_seed...>> { constexpr string_literal Lit = parameter_key_pack_type::literal; using MemberT = schema::Member; - seed = hash_literal::apply(seed); - seed = schema_hash_seed::apply(seed); - seed = hash_literal::apply(seed); + seed = schema_hash_seed::apply(seed); if constexpr ( (i+1) < sizeof...(T) ){ return apply_ele(seed); diff --git a/modules/tools/c++/c_gen_iface.hpp b/modules/tools/c++/c_gen_iface.hpp index 2dfa36f..7c26f80 100644 --- a/modules/tools/c++/c_gen_iface.hpp +++ b/modules/tools/c++/c_gen_iface.hpp @@ -151,6 +151,7 @@ struct lang_bind_helper { } }; + template struct lang_bind, binding::C> { using Schema = schema::Primitive; @@ -199,9 +200,84 @@ struct lang_bind, binding::C> { } }; +template +struct lang_bind, binding::C> { + using Schema = schema::Array; + + static_assert(is_primitive::value, "Currently only primitive type arrays are supported"); + + static error_or generate(buffer& buff, 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(emp.second){ + { + auto eov = lang_bind::generate(buff, cfg, state); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind::generate(buff, cfg, state); + if(eov.is_error()){ + return eov; + } + } + + { + auto eov = lang_bind_helper::append_string(buff, "struct "); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_hashed_type(buff, cfg.prefix, hash); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, " {\n\t"); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_hashed_type(buff, cfg.prefix, schema_hash::apply()); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, "* data;\n\t"); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_hashed_type(buff, cfg.prefix, schema_hash::apply()); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, " length;\n};\n"); + if(eov.is_error()){ + return eov; + } + } + } + + return void_t{}; + } +}; + template struct lang_bind, binding::C> { - static error_or generate(buffer& buff, const language_binding_config& cfg, language_binding_state& state){ + static error_or append_function_def(buffer& buff, + + static error_or generate(buffer& buff, buffer& src, const language_binding_config& cfg, language_binding_state& state, const std::string_view& f_name){ constexpr uint64_t input_hash = schema_hash::apply(); constexpr uint64_t output_hash = schema_hash::apply(); @@ -217,26 +293,66 @@ struct lang_bind, binding::C> { return eov; } } - - return void_t{}; - } -}; - -template -struct lang_bind, binding::C> { - template - static error_or generate_element(buffer& buff, const language_binding_config& cfg, language_binding_state& state){ - using Member = typename parameter_pack_type::type; - using MValue = typename Member::ValueType; - static constexpr string_literal MKey = Member::KeyLiteral; - { - auto eov = lang_bind::generate(buff, cfg, state); + auto eov = lang_bind_helper::append_string(buff, "typedef "); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_hashed_type(buff, cfg.prefix, schema_hash::apply()); + 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, "_"); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, f_name); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, "_input_t;\n"); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, "typedef "); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_hashed_type(buff, cfg.prefix, schema_hash::apply()); + 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()){ @@ -250,31 +366,86 @@ struct lang_bind, binding::C> { } } { - auto eov = lang_bind_helper::append_string(buff, MKey.view()); + auto eov = lang_bind_helper::append_string(buff, f_name); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, "_output_t;\n"); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, "int "); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind_helper::append_string(buff, "("); + auto eov = lang_bind_helper::append_string(buff, cfg.prefix); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind_helper::append_hashed_type(buff, cfg.prefix, schema_hash::apply()); + auto eov = lang_bind_helper::append_string(buff, "_"); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind_helper::append_string(buff, ","); + auto eov = lang_bind_helper::append_string(buff, f_name); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind_helper::append_string(buff, ");"); + auto eov = lang_bind_helper::append_string(buff, "( const "); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_hashed_type(buff, cfg.prefix, schema_hash::apply()); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, "* input, "); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_hashed_type(buff, cfg.prefix, schema_hash::apply()); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, "* output);\n\n"); + if(eov.is_error()){ + return eov; + } + } + + return void_t{}; + } +}; + +template +struct lang_bind, binding::C> { + template + static error_or generate_element(buffer& buff, const language_binding_config& cfg, language_binding_state& state){ + using Member = typename parameter_pack_type::type; + using MValue = typename Member::ValueType; + static constexpr string_literal MKey = Member::KeyLiteral; + + { + auto eov = lang_bind::generate(buff, cfg, state, MKey.view()); if(eov.is_error()){ return eov; } @@ -290,6 +461,7 @@ struct lang_bind, binding::C> { if(cfg.prefix.size() == 0){ return make_error("C interfaces need a prefix."); } + language_binding_state state; if constexpr (sizeof...(M) > 0){ @@ -308,8 +480,8 @@ struct language_binding { template struct language_binding { - static error_or generate(buffer& buff, const language_binding_config& cfg){ - return impl::lang_bind::generate(buff, cfg); + static error_or generate(buffer& buff, buffer& src, const language_binding_config& 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 79d2344..61668de 100644 --- a/modules/tools/tests/c_iface.cpp +++ b/modules/tools/tests/c_iface.cpp @@ -33,7 +33,14 @@ using TestStructFunctionInterface = Interface< >; using TestArrayFunctionInterface = Interface< - Member, "three"> + Member, "three"> +>; + +using TestMultiFunctionInterface = Interface< + Member, "foo">, + Member, "bar">, + Member, "baz">, + Member, "banana"> >; } @@ -44,7 +51,7 @@ void test_generate(std::string& res){ ring_buffer r_buff{4u * 1024u * 1024u}; { - auto eov = language_binding::generate(r_buff, {"kel"}); + auto eov = language_binding::generate(r_buff, {"prefix"}); SAW_EXPECT(eov.is_value(), std::string{"Couldn't generate interface info: "} + std::string{eov.get_error().get_message()}); } @@ -67,12 +74,12 @@ SAW_TEST("CIface One Function Interface"){ test_generate(res); std::cout<<"\n"<(res); + test_generate(res); std::cout<<"\n"<(res); std::cout<<"\n"<