From 2edf7d384fe7c7b2fe861d91c030031ea35f1595 Mon Sep 17 00:00:00 2001 From: Claudius 'keldu' Holeksa Date: Mon, 9 Sep 2024 20:13:49 +0200 Subject: wip --- modules/codec/c++/data_raw.hpp | 13 ++++++- modules/tools/c++/c_gen_iface.hpp | 77 +++++++++++++++++++++++---------------- modules/tools/tests/c_iface.cpp | 2 +- 3 files changed, 58 insertions(+), 34 deletions(-) (limited to 'modules') diff --git a/modules/codec/c++/data_raw.hpp b/modules/codec/c++/data_raw.hpp index 37e56d9..fabf77a 100644 --- a/modules/codec/c++/data_raw.hpp +++ b/modules/codec/c++/data_raw.hpp @@ -409,7 +409,8 @@ class data, encode::NativeRaw> { using MetaSchema = typename meta_schema::MetaSchema; private: - // data> dims_; + // rawr + // data, encode::NativeRaw> dims_; std::array dims_; std::vector::Type> value_; @@ -432,6 +433,16 @@ class data, encode::NativeRaw> { } } + /** + * Purely internal function for working C interfacing + */ + typename raw_native_array_type_helper::Type* get_raw_data() { + if(value_.size() == 0u){ + return nullptr; + } + return &(value_[0]); + } + SAW_DEFAULT_COPY(data); SAW_DEFAULT_MOVE(data); diff --git a/modules/tools/c++/c_gen_iface.hpp b/modules/tools/c++/c_gen_iface.hpp index 590ee3f..1881cf8 100644 --- a/modules/tools/c++/c_gen_iface.hpp +++ b/modules/tools/c++/c_gen_iface.hpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -104,7 +105,7 @@ struct language_binding_config { }; namespace impl { -template +template struct lang_bind { static_assert(always_false, "Not supported"); }; @@ -158,7 +159,7 @@ struct lang_bind_helper { return void_t{}; } - template + template 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(); @@ -229,6 +230,18 @@ struct lang_bind_helper { return make_error(); } } + { + auto eov = lang_bind_helper::append_string(buff, ","); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, std::string{Encoding::name.view()}); + if(eov.is_error()){ + return eov; + } + } { auto eov = lang_bind_helper::append_string(buff, ">& cpp_val"); if(eov.is_error()){ @@ -313,8 +326,8 @@ struct lang_bind_helper { }; -template -struct lang_bind, binding::SyncC> { +template +struct lang_bind, Encoding, binding::SyncC> { using Schema = schema::Primitive; /* @@ -448,7 +461,7 @@ struct lang_bind, binding::SyncC> { } } { - auto eov = lang_bind_helper::append_translation_func(tpe.at(id).source, cfg.prefix, true); + auto eov = lang_bind_helper::append_translation_func(tpe.at(id).source, cfg.prefix, true); if(eov.is_error()){ return eov; } @@ -460,19 +473,19 @@ struct lang_bind, binding::SyncC> { } } { - 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, "\treturn saw::make_void();\n}\n"); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind_helper::append_translation_func(tpe.at(id).source, cfg.prefix, false); + 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\t*c_output = cpp_output.get();\n\treturn void_t{};\n}\n}"); + auto eov = lang_bind_helper::append_string(tpe.at(id).source, " {\n\t*c_output = cpp_output.get();\n\treturn saw::make_void();\n}\n}"); if(eov.is_error()){ return eov; } @@ -485,8 +498,8 @@ struct lang_bind, binding::SyncC> { } }; -template -struct lang_bind...>, binding::SyncC> { +template +struct lang_bind...>, Encoding, binding::SyncC> { using Schema = schema::Struct...>; template @@ -498,7 +511,7 @@ struct lang_bind...>, binding::SyncC> { auto& tpe = state.tp_elements; if constexpr (i == 0){ - auto eov = lang_bind_helper::append_translation_func(tpe.at(id).source, cfg.prefix, c_to_cpp); + auto eov = lang_bind_helper::append_translation_func(tpe.at(id).source, cfg.prefix, c_to_cpp); if(eov.is_error()){ return eov; } @@ -660,7 +673,7 @@ struct lang_bind...>, binding::SyncC> { * Ensure generation of dependent types */ { - auto eov = lang_bind::generate(cfg, state); + auto eov = lang_bind::generate(cfg, state); if(eov.is_error()){ return eov; } @@ -757,8 +770,8 @@ struct lang_bind...>, binding::SyncC> { } }; -template -struct lang_bind, binding::SyncC> { +template +struct lang_bind, Encoding, binding::SyncC> { using Schema = schema::Array; static_assert(is_primitive::value, "Currently only primitive type arrays are supported"); @@ -829,7 +842,7 @@ struct lang_bind, binding::SyncC> { * Guarante existance of length type */ { - auto eov = lang_bind::generate(cfg, state); + auto eov = lang_bind::generate(cfg, state); if(eov.is_error()){ return eov; } @@ -838,7 +851,7 @@ struct lang_bind, binding::SyncC> { * Guarante existance of inner type */ { - auto eov = lang_bind::generate(cfg, state); + auto eov = lang_bind::generate(cfg, state); if(eov.is_error()){ return eov; } @@ -859,19 +872,19 @@ struct lang_bind, binding::SyncC> { } } { - auto eov = lang_bind_helper::append_string(tpe.at(id).source, " {\n\ttry{\n\t\tcpp_val = {c_val->length};\n\t}catch(const std::exception&){return saw::make_error();}\n\n\tfor(uint64_t i = 0u; i < cpp_val.size(); ++i){\n\t\tcpp_val.at(i).set(c_val[i]);\n\t}\n\n\treturn void_t{};\n}\n"); + auto eov = lang_bind_helper::append_string(tpe.at(id).source, " {\n\ttry{\n\t\tcpp_val = {c_val->length};\n\t}catch(const std::exception&){return saw::make_error();}\n\n\tfor(uint64_t i = 0u; i < cpp_val.size(); ++i){\n\t\tcpp_val.at(i).set(c_val[i]);\n\t}\n\n\treturn saw::make_void();\n}\n"); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind_helper::append_translation_func(tpe.at(id).source, cfg.prefix, false); + 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\treturn saw::make_error();\n}\n"); + auto eov = lang_bind_helper::append_string(tpe.at(id).source, " {\n\tc_val->length = cpp_val.size();\n\tc_val->data = cpp_val.get_raw_data();\n\n\treturn saw::make_void();\n}\n"); } { auto eov = lang_bind_helper::append_string(tpe.at(id).source, "\n}\n"); @@ -889,8 +902,8 @@ struct lang_bind, binding::SyncC> { /** * Generates the Function bindings to C */ -template -struct lang_bind, binding::SyncC> { +template +struct lang_bind, Encoding, binding::SyncC> { using Schema = schema::Function; /** * Appends the function declaration e.g. 'int foo(const int* a, int* b)' @@ -999,13 +1012,13 @@ struct lang_bind, binding::SyncC> { * Order is not important since moving to multiple strings which get concat'ed. */ { - auto eov = lang_bind::generate(cfg, state); + auto eov = lang_bind::generate(cfg, state); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind::generate(cfg, state); + auto eov = lang_bind::generate(cfg, state); if(eov.is_error()){ return eov; } @@ -1273,8 +1286,8 @@ struct lang_bind, binding::SyncC> { } }; -template -struct lang_bind, binding::SyncC> { +template +struct lang_bind, Encoding, binding::SyncC> { using Schema = schema::Interface; template @@ -1285,7 +1298,7 @@ struct lang_bind, binding::SyncC> { * Guarantee */ { - auto eov = lang_bind::guarantee(cfg, state); + auto eov = lang_bind::guarantee(cfg, state); if(eov.is_error()){ return eov; } @@ -1323,7 +1336,7 @@ struct lang_bind, binding::SyncC> { f_id = eoid.get_value(); } { - auto eov = lang_bind::generate(cfg, state, MKey.view(), f_id); + auto eov = lang_bind::generate(cfg, state, MKey.view(), f_id); if(eov.is_error()){ return eov; } @@ -1560,15 +1573,15 @@ struct lang_bind, binding::SyncC> { }; } -template +template struct language_binding { - static_assert(always_false, "Case not supported."); + static_assert(always_false, "Case not supported."); }; -template -struct language_binding { +template +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 25a5d04..d27c45a 100644 --- a/modules/tools/tests/c_iface.cpp +++ b/modules/tools/tests/c_iface.cpp @@ -65,7 +65,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