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.hpp155
1 files changed, 139 insertions, 16 deletions
diff --git a/modules/tools/c++/c_gen_iface.hpp b/modules/tools/c++/c_gen_iface.hpp
index 1b1e14d..ceb6fef 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/codec/data.hpp>
#include <forstio/codec/schema.hpp>
+#include <forstio/codec/schema_hash.hpp>
#include <forstio/codec/schema_stringify.hpp>
#include <forstio/templates.hpp>
@@ -16,19 +17,23 @@ namespace schema {
using CVar = Struct<
Member<schema::String, "schema">,
+ Member<schema::UInt32, "crc32">,
Member<schema::String, "type">,
+ Member<schema::String, "type_name">,
Member<schema::String, "name">,
Member<schema::Int8, "is_primitive">
>;
using CStruct = Struct<
Member<schema::String, "schema">,
- Member<schema::String, "name">,
+ Member<schema::UInt32, "crc32">,
+ Member<schema::String, "type">,
Member<Array<CVar>, "members">
>;
using CFunction = Struct<
Member<schema::String, "schema">,
+ Member<schema::UInt32, "crc32">,
Member<schema::String, "name">,
Member<Array<CVar>, "params">,
Member<CVar, "return">
@@ -36,6 +41,7 @@ using CFunction = Struct<
using CIface = Struct<
Member<schema::String, "schema">,
+ Member<schema::UInt32, "crc32">,
Member<Array<CStruct>,"structs">,
Member<Array<CFunction>,"functions">
>;
@@ -71,7 +77,8 @@ using FlattenedSchema = Struct<
Member<FlattenedSchemaElement<Ele>, Names>...
>;
}
- */
+*/
+
template<typename T, typename Res>
struct schema_flattener {
static_assert(always_false<T>, "Not supported");
@@ -101,54 +108,84 @@ struct c_primitive_string {
template<>
struct c_primitive_string<schema::Int8> {
- static constexpr std::string_view value = "int8_t";
+ static constexpr string_literal name = "int8";
+ static constexpr string_literal value = "int8_t";
};
template<>
struct c_primitive_string<schema::Int16> {
- static constexpr std::string_view value = "int16_t";
+ static constexpr string_literal name = "int16";
+ static constexpr string_literal value = "int16_t";
};
template<>
struct c_primitive_string<schema::Int32> {
- static constexpr std::string_view value = "int32_t";
+ static constexpr string_literal name = "int32";
+ static constexpr string_literal value = "int32_t";
};
template<>
struct c_primitive_string<schema::Int64> {
- static constexpr std::string_view value = "int64_t";
+ static constexpr string_literal name = "int64";
+ static constexpr string_literal value = "int64_t";
};
template<>
struct c_primitive_string<schema::UInt8> {
- static constexpr std::string_view value = "uint8_t";
+ static constexpr string_literal name = "uint8";
+ static constexpr string_literal value = "uint8_t";
};
template<>
struct c_primitive_string<schema::UInt16> {
- static constexpr std::string_view value = "uint16_t";
+ static constexpr string_literal name = "uint16";
+ static constexpr string_literal value = "uint16_t";
};
template<>
struct c_primitive_string<schema::UInt32> {
- static constexpr std::string_view value = "uint32_t";
+ static constexpr string_literal name = "uint32";
+ static constexpr string_literal value = "uint32_t";
};
template<>
struct c_primitive_string<schema::UInt64> {
- static constexpr std::string_view value = "uint64_t";
+ static constexpr string_literal name = "uint64";
+ static constexpr string_literal value = "uint64_t";
};
template<>
struct c_primitive_string<schema::Float32> {
- static constexpr std::string_view value = "float";
+ static constexpr string_literal name = "float32";
+ static constexpr string_literal value = "float";
};
template<>
struct c_primitive_string<schema::Float64> {
- static constexpr std::string_view value = "double";
+ static constexpr string_literal name = "float64";
+ static constexpr string_literal value = "double";
};
+error_or<data<schema::UInt64>> c_interface_find_struct_by_crc32(const data<schema::CIface>& state, const data<schema::UInt32>& id){
+ const auto& strs = state.template get<"structs">();
+ for(uint64_t i = 0; i < strs.size(); ++i){
+ const auto& str = strs.at(i);
+ if(str.template get<"crc32">() == id){
+ return data<schema::UInt64>{i};
+ }
+ }
+ return make_error<err::not_found>();
+}
+
+error_or<void> c_interface_add_struct(data<schema::CIface>& state, data<schema::CStruct>&& val){
+ auto& strs = state.template get<"structs">();
+ auto eov = strs.add(std::move(val));
+ if(eov.is_error()){
+ return std::move(eov.get_error());
+ }
+ return void_t{};
+}
+
bool c_interface_function_exists(data<schema::CIface>& state, const std::string_view& name){
auto& funcs = state.template get<"functions">();
for(uint64_t i = 0; i < funcs.size(); ++i){
@@ -167,14 +204,17 @@ struct c_data_translater {
template<typename T, uint64_t N>
struct c_data_translater<schema::Primitive<T,N>> {
using Schema = schema::Primitive<T,N>;
+ static constexpr string_literal c_type_name = c_primitive_string<Schema>::name;
+ static constexpr string_literal c_type = c_primitive_string<Schema>::value;
static error_or<void> generate(data<schema::CIface>& state, data<schema::CVar>& prim){
try{
std::stringstream iss;
schema_stringify<Schema>::apply(iss);
prim.template get<"schema">().set(iss.str());
+ prim.template get<"crc32">().set(schema_hash<Schema>::apply());
prim.template get<"is_primitive">().set(1);
- prim.template get<"type">().set(std::string{c_primitive_string<Schema>::value});
+ prim.template get<"type">().set(std::string{c_primitive_string<Schema>::value.view()});
}catch(const std::exception&){
return make_error<err::out_of_memory>();
}
@@ -185,23 +225,104 @@ struct c_data_translater<schema::Primitive<T,N>> {
template<typename T, uint64_t N>
struct c_data_translater<schema::Array<T,N>> {
+ static_assert(N==1, "Doesn't support more than one dimension currently");
+
using Schema = schema::Array<T,N>;
+ static constexpr string_literal c_type_name = "array_"_sl + c_data_translater<T>::c_type_name + "_1d"_sl;
+ static constexpr string_literal c_type = c_type_name + "_t"_sl;
- static error_or<void> generate(data<schema::CIface>& state){
+ static error_or<void> generate(data<schema::CIface>& state, data<schema::CVar>& arr){
+ try{
+ {
+ std::stringstream iss;
+ schema_stringify<Schema>::apply(iss);
+ arr.template get<"schema">().set(iss.str());
+ arr.template get<"crc32">().set(schema_hash<Schema>::apply());
+ arr.template get<"is_primitive">().set(0);
+ }
+
+ /// @todo change this !!!!!
+ {
+ auto eov = c_interface_find_struct_by_crc32(state, arr.template get<"crc32">());
+ if(eov.is_error()){
+ if(false){
+ return std::move(eov.get_error());
+ }
+ data<schema::CStruct> str;
+ str.template get<"schema">() = arr.template get<"schema">();
+ str.template get<"crc32">() = arr.template get<"crc32">();
+ str.template get<"type">().set(std::string{c_type.view()});
+ auto& membs = str.template get<"members">();
+ {
+ data<schema::CVar> c_var;
+ std::stringstream iss;
+ schema_stringify<T>::apply(iss);
+ c_var.template get<"schema">().set(iss.str());
+ c_var.template get<"crc32">().set(schema_hash<T>::apply());
+ if constexpr (is_primitive<T>::value){
+ c_var.template get<"type">().set(std::string{c_primitive_string<T>::value.view()} + "*");
+ c_var.template get<"name">().set(std::string{"data"});
+ c_var.template get<"is_primitive">().set(1);
+ }else{
+ return make_error<err::not_implemented>();
+ }
+
+ auto eov_add = membs.add(std::move(c_var));
+ if(eov_add.is_error()){
+ return std::move(eov_add.get_error());
+ }
+ }
+ {
+ data<schema::CVar> c_var;
+ std::stringstream iss;
+ schema_stringify<schema::UInt64>::apply(iss);
+ c_var.template get<"schema">().set(iss.str());
+ c_var.template get<"crc32">().set(schema_hash<schema::UInt64>::apply());
+ if constexpr (is_primitive<T>::value){
+ c_var.template get<"type">().set(std::string{c_primitive_string<schema::UInt64>::value.view()});
+ c_var.template get<"name">().set(std::string{"size"});
+ c_var.template get<"is_primitive">().set(1);
+ }else{
+ return make_error<err::not_implemented>();
+ }
+
+ auto eov_add = membs.add(std::move(c_var));
+ if(eov_add.is_error()){
+ return std::move(eov_add.get_error());
+ }
+ }
+
+ /**
+ * Add the struct to the struct array
+ */
+ {
+ auto eov_add = c_interface_add_struct(state, std::move(str));
+ if(eov_add.is_error()){
+ return std::move(eov_add.get_error());
+ }
+ }
+ }
+ }
+ arr.template get<"type">().set(std::string{c_type.view()});
+ }catch(const std::exception&){
+ return make_error<err::out_of_memory>();
+ }
return void_t{};
}
};
+/*
template<typename... Members>
struct c_data_translater<schema::Struct<Members...>> {
using Schema = schema::Struct<Members...>;
static error_or<void> generate(data<schema::CIface>& state){
-
return void_t{};
}
};
+*/
+
template<typename Str>
struct c_parameter_translater {
static_assert(always_false<Str>, "Type not supported");
@@ -218,7 +339,7 @@ struct c_parameter_translater<schema::Struct<schema::Member<V,K>...>> {
auto c_var = data<schema::CVar>{};
c_var.template get<"name">().set(std::string{Literal.view()});
{
- auto eov = c_data_translater<Type>::generate(state, c_var);
+ auto eov = c_data_translater<Type>::generate(state,c_var);
if(eov.is_error()){
return eov;
}
@@ -266,6 +387,7 @@ struct c_data_translater<schema::Function<Req,Ret>> {
std::stringstream iss;
schema_stringify<Schema>::apply(iss);
function.template get<"schema">().set(iss.str());
+ function.template get<"crc32">().set(schema_hash<Schema>::apply());
}catch(const std::exception&){
return make_error<err::out_of_memory>();
}
@@ -344,6 +466,7 @@ struct c_data_translater<schema::Interface<schema::Member<Funcs,Names>...>> {
std::stringstream iss;
schema_stringify<Schema>::apply(iss);
state.template get<"schema">().set(iss.str());
+ state.template get<"crc32">().set(schema_hash<Schema>::apply());
}catch(const std::exception&){
return make_error<err::out_of_memory>();
}