summaryrefslogtreecommitdiff
path: root/modules/tools/c++
diff options
context:
space:
mode:
Diffstat (limited to 'modules/tools/c++')
-rw-r--r--modules/tools/c++/c_gen_iface.hpp77
1 files changed, 45 insertions, 32 deletions
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);
}
};
}