summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorClaudius 'keldu' Holeksa <mail@keldu.de>2024-09-09 20:13:49 +0200
committerClaudius 'keldu' Holeksa <mail@keldu.de>2024-09-09 20:13:49 +0200
commit2edf7d384fe7c7b2fe861d91c030031ea35f1595 (patch)
tree84c79129b07ae7b4f49e9d898dc35b44b60be20f /modules
parent4a4bfa2ccf179e15a5b8e98f398c16f693ddd3e5 (diff)
wip
Diffstat (limited to 'modules')
-rw-r--r--modules/codec/c++/data_raw.hpp13
-rw-r--r--modules/tools/c++/c_gen_iface.hpp77
-rw-r--r--modules/tools/tests/c_iface.cpp2
3 files changed, 58 insertions, 34 deletions
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<schema::Array<T,Dim>, encode::NativeRaw> {
using MetaSchema = typename meta_schema<Schema>::MetaSchema;
private:
- // data<schema::FixedArray<schema::UInt64, Dim>> dims_;
+ // rawr
+ // data<schema::FixedArray<schema::UInt64, Dim>, encode::NativeRaw> dims_;
std::array<uint64_t, Dim> dims_;
std::vector<typename raw_native_array_type_helper<T>::Type> value_;
@@ -432,6 +433,16 @@ class data<schema::Array<T,Dim>, encode::NativeRaw> {
}
}
+ /**
+ * Purely internal function for working C interfacing
+ */
+ typename raw_native_array_type_helper<T>::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 <forstio/error.hpp>
#include <forstio/buffer.hpp>
#include <forstio/codec/data.hpp>
+#include <forstio/codec/data_raw.hpp>
#include <forstio/codec/schema.hpp>
#include <forstio/codec/schema_hash.hpp>
#include <forstio/codec/schema_stringify.hpp>
@@ -104,7 +105,7 @@ struct language_binding_config {
};
namespace impl {
-template<typename Schema, typename Lang>
+template<typename Schema, typename Encoding, typename Lang>
struct lang_bind {
static_assert(always_false<Schema>, "Not supported");
};
@@ -158,7 +159,7 @@ struct lang_bind_helper {
return void_t{};
}
- template<typename Schema>
+ template<typename Schema,typename Encoding = saw::encode::NativeRaw>
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();
@@ -230,6 +231,18 @@ struct lang_bind_helper {
}
}
{
+ 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()){
return eov;
@@ -313,8 +326,8 @@ struct lang_bind_helper {
};
-template<typename T, uint64_t L>
-struct lang_bind<schema::Primitive<T,L>, binding::SyncC> {
+template<typename T, uint64_t L, typename Encoding>
+struct lang_bind<schema::Primitive<T,L>, Encoding, binding::SyncC> {
using Schema = schema::Primitive<T,L>;
/*
@@ -448,7 +461,7 @@ struct lang_bind<schema::Primitive<T,L>, binding::SyncC> {
}
}
{
- auto eov = lang_bind_helper::append_translation_func<Schema>(tpe.at(id).source, cfg.prefix, true);
+ auto eov = lang_bind_helper::append_translation_func<Schema,Encoding>(tpe.at(id).source, cfg.prefix, true);
if(eov.is_error()){
return eov;
}
@@ -460,19 +473,19 @@ struct lang_bind<schema::Primitive<T,L>, 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<Schema>(tpe.at(id).source, cfg.prefix, false);
+ auto eov = lang_bind_helper::append_translation_func<Schema,Encoding>(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<schema::Primitive<T,L>, binding::SyncC> {
}
};
-template<typename... V, string_literal... K>
-struct lang_bind<schema::Struct<schema::Member<V,K>...>, binding::SyncC> {
+template<typename... V, string_literal... K,typename Encoding>
+struct lang_bind<schema::Struct<schema::Member<V,K>...>, Encoding, binding::SyncC> {
using Schema = schema::Struct<schema::Member<V,K>...>;
template<uint64_t i>
@@ -498,7 +511,7 @@ struct lang_bind<schema::Struct<schema::Member<V,K>...>, binding::SyncC> {
auto& tpe = state.tp_elements;
if constexpr (i == 0){
- auto eov = lang_bind_helper::append_translation_func<Schema>(tpe.at(id).source, cfg.prefix, c_to_cpp);
+ auto eov = lang_bind_helper::append_translation_func<Schema,Encoding>(tpe.at(id).source, cfg.prefix, c_to_cpp);
if(eov.is_error()){
return eov;
}
@@ -660,7 +673,7 @@ struct lang_bind<schema::Struct<schema::Member<V,K>...>, binding::SyncC> {
* Ensure generation of dependent types
*/
{
- auto eov = lang_bind<MT, binding::SyncC>::generate(cfg, state);
+ auto eov = lang_bind<MT, Encoding, binding::SyncC>::generate(cfg, state);
if(eov.is_error()){
return eov;
}
@@ -757,8 +770,8 @@ struct lang_bind<schema::Struct<schema::Member<V,K>...>, binding::SyncC> {
}
};
-template<typename T, uint64_t D>
-struct lang_bind<schema::Array<T,D>, binding::SyncC> {
+template<typename T, uint64_t D, typename Encoding>
+struct lang_bind<schema::Array<T,D>, Encoding, binding::SyncC> {
using Schema = schema::Array<T,D>;
static_assert(is_primitive<T>::value, "Currently only primitive type arrays are supported");
@@ -829,7 +842,7 @@ struct lang_bind<schema::Array<T,D>, binding::SyncC> {
* Guarante existance of length type
*/
{
- auto eov = lang_bind<schema::UInt64, binding::SyncC>::generate(cfg, state);
+ auto eov = lang_bind<schema::UInt64, Encoding, binding::SyncC>::generate(cfg, state);
if(eov.is_error()){
return eov;
}
@@ -838,7 +851,7 @@ struct lang_bind<schema::Array<T,D>, binding::SyncC> {
* Guarante existance of inner type
*/
{
- auto eov = lang_bind<T, binding::SyncC>::generate(cfg, state);
+ auto eov = lang_bind<T, Encoding, binding::SyncC>::generate(cfg, state);
if(eov.is_error()){
return eov;
}
@@ -859,19 +872,19 @@ struct lang_bind<schema::Array<T,D>, 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<saw::err::out_of_memory>();}\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<saw::err::out_of_memory>();}\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<Schema>(tpe.at(id).source, cfg.prefix, false);
+ auto eov = lang_bind_helper::append_translation_func<Schema, Encoding>(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<saw::err::not_implemented>();\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<schema::Array<T,D>, binding::SyncC> {
/**
* Generates the Function bindings to C
*/
-template<typename Input, typename Output>
-struct lang_bind<schema::Function<Input, Output>, binding::SyncC> {
+template<typename Input, typename Output, typename Encoding>
+struct lang_bind<schema::Function<Input, Output>, Encoding, binding::SyncC> {
using Schema = schema::Function<Input, Output>;
/**
* Appends the function declaration e.g. 'int foo(const int* a, int* b)'
@@ -999,13 +1012,13 @@ struct lang_bind<schema::Function<Input, Output>, binding::SyncC> {
* Order is not important since moving to multiple strings which get concat'ed.
*/
{
- auto eov = lang_bind<Input, binding::SyncC>::generate(cfg, state);
+ auto eov = lang_bind<Input, Encoding, binding::SyncC>::generate(cfg, state);
if(eov.is_error()){
return eov;
}
}
{
- auto eov = lang_bind<Output, binding::SyncC>::generate(cfg, state);
+ auto eov = lang_bind<Output, Encoding, binding::SyncC>::generate(cfg, state);
if(eov.is_error()){
return eov;
}
@@ -1273,8 +1286,8 @@ struct lang_bind<schema::Function<Input, Output>, binding::SyncC> {
}
};
-template<typename... M>
-struct lang_bind<schema::Interface<M...>, binding::SyncC> {
+template<typename... M, typename Encoding>
+struct lang_bind<schema::Interface<M...>, Encoding, binding::SyncC> {
using Schema = schema::Interface<M...>;
template<uint64_t i>
@@ -1285,7 +1298,7 @@ struct lang_bind<schema::Interface<M...>, binding::SyncC> {
* Guarantee
*/
{
- auto eov = lang_bind<MValue, binding::SyncC>::guarantee(cfg, state);
+ auto eov = lang_bind<MValue, Encoding, binding::SyncC>::guarantee(cfg, state);
if(eov.is_error()){
return eov;
}
@@ -1323,7 +1336,7 @@ struct lang_bind<schema::Interface<M...>, binding::SyncC> {
f_id = eoid.get_value();
}
{
- auto eov = lang_bind<MValue, binding::SyncC>::generate(cfg, state, MKey.view(), f_id);
+ auto eov = lang_bind<MValue, Encoding, binding::SyncC>::generate(cfg, state, MKey.view(), f_id);
if(eov.is_error()){
return eov;
}
@@ -1560,15 +1573,15 @@ struct lang_bind<schema::Interface<M...>, binding::SyncC> {
};
}
-template<typename IfaceSchema, typename Binding>
+template<typename IfaceSchema, typename Encoding, typename Binding>
struct language_binding {
- static_assert(always_false<IfaceSchema, Binding>, "Case not supported.");
+ static_assert(always_false<IfaceSchema, Encoding, Binding>, "Case not supported.");
};
-template<typename IfaceSchema>
-struct language_binding<IfaceSchema, binding::SyncC> {
+template<typename IfaceSchema, typename Encoding>
+struct language_binding<IfaceSchema, Encoding, binding::SyncC> {
static error_or<void> generate(buffer& buff, buffer& src, const language_binding_config& cfg){
- return impl::lang_bind<IfaceSchema, binding::SyncC>::generate(buff, src, cfg);
+ return impl::lang_bind<IfaceSchema, Encoding, binding::SyncC>::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<Schema, binding::SyncC>::generate(r_buff, r_src_buff, {"prefix"});
+ auto eov = language_binding<Schema, encode::NativeRaw, 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()});
}