diff options
Diffstat (limited to 'modules/tools/c++/c_gen_iface.hpp')
-rw-r--r-- | modules/tools/c++/c_gen_iface.hpp | 271 |
1 files changed, 271 insertions, 0 deletions
diff --git a/modules/tools/c++/c_gen_iface.hpp b/modules/tools/c++/c_gen_iface.hpp new file mode 100644 index 0000000..b25e5eb --- /dev/null +++ b/modules/tools/c++/c_gen_iface.hpp @@ -0,0 +1,271 @@ +#include <forstio/error.hpp> +#include <forstio/codec/data.hpp> +#include <forstio/codec/schema.hpp> +#include <forstio/codec/schema_stringify.hpp> +#include <forstio/templates.hpp> + +#include <string> +#include <sstream> +#include <map> + +#include <iostream> + +namespace saw { + +namespace schema { + +using CVar = Struct< + Member<schema::String, "schema">, + Member<schema::String, "type">, + Member<schema::String, "name"> +>; + +using CStruct = Struct< + Member<schema::String, "schema">, + Member<schema::String, "name">, + Member<Array<CVar>, "members"> +>; + +using CFunction = Struct< + Member<schema::String, "schema">, + Member<schema::String, "name">, + Member<Array<CVar>, "params">, + Member<CVar, "return"> +>; + +using CIface = Struct< + Member<schema::String, "schema">, + Member<Array<CStruct>,"structs">, + Member<Array<CFunction>,"functions"> +>; +} +/** + * Type meant to provide future help if I decide to introduce more maps + */ +/** +namespace schema { +using namespace saw::schema; +Pseudo flattened +using Foo = Struct< + Member<Array<Int32>, "a"> + Member<Array<Float32>, "b"> +>; + +Needs to be flattened to + +template<typename InnerSchema> +using FlattenedSchemaElement = Struct< + Member<Array<String>, "path">, + Member<InnerSchema, "inner_schema"> +>; + +// Illegal, but doable with more lines of code +// Just use typename... T and +// "T..." for +// "Member<FlattenedSchemaElement<Ele>,Names>...>" +// and specialize somewhere else +template<typename... Ele, string_literal... Names> +using FlattenedSchema = Struct< + Member<String, "top_schema">, + Member<FlattenedSchemaElement<Ele>, Names>... +>; +} + */ +template<typename T, typename Res> +struct schema_flattener { + static_assert(always_false<T>, "Not supported"); +}; + +template<typename T0, string_literal Name0, typename... T, string_literal... Names, typename Res> +struct schema_flattener<schema::Struct<schema::Member<T0,Name0>,schema::Member<T,Names>...>, Res> { +}; + +/** + * Helper to determine if we are dealing with primitive types + */ +template<typename Schema> +struct c_is_primitive { + static constexpr bool value = false; +}; + +template<typename T, size_t N> +struct c_is_primitive<schema::Primitive<T,N>> { + static constexpr bool value = true; +}; + +template<typename Schema> +struct c_primitive_string { + static_assert(always_false<Schema>, "Not supported"); +}; + +template<> +struct c_primitive_string<schema::Int8> { + static constexpr std::string_view value = "int8_t"; +}; + +template<> +struct c_primitive_string<schema::Int16> { + static constexpr std::string_view value = "int16_t"; +}; + +template<> +struct c_primitive_string<schema::Int32> { + static constexpr std::string_view value = "int32_t"; +}; + +template<> +struct c_primitive_string<schema::Int64> { + static constexpr std::string_view value = "int64_t"; +}; + +template<> +struct c_primitive_string<schema::UInt8> { + static constexpr std::string_view value = "uint8_t"; +}; + +template<> +struct c_primitive_string<schema::UInt16> { + static constexpr std::string_view value = "uint16_t"; +}; + +template<> +struct c_primitive_string<schema::UInt32> { + static constexpr std::string_view value = "uint32_t"; +}; + +template<> +struct c_primitive_string<schema::UInt64> { + static constexpr std::string_view value = "uint64_t"; +}; + +template<> +struct c_primitive_string<schema::Float32> { + static constexpr std::string_view value = "float"; +}; + +template<> +struct c_primitive_string<schema::Float64> { + static constexpr std::string_view value = "double"; +}; + +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){ + if(funcs.at(i).get<"name">() == name){ + return true; + } + } + return false; +} + +template<typename Schema> +struct c_data_translater { + static_assert(always_false<Schema>, "Not supported"); +}; + +template<typename T, uint64_t N> +struct c_data_translater<schema::Primitive<T,N>> { + using Schema = schema::Primitive<T,N>; + + static error_or<void> generate(data<schema::CIface>& state, data<schema::CVar>& prim){ + /// @TODO Check if exists in CVars already + try{ + std::stringstream iss; + schema_stringify<Schema>::apply(iss); + prim.template get<"schema">().set(iss.str()); + }catch(const std::exception&){ + return make_error<err::out_of_memory>(); + } + + return void_t{}; + } +}; + +template<typename Req, typename Ret> +struct c_data_translater<schema::Function<Req,Ret>> { + using Schema = schema::Function<Req,Ret>; + + static error_or<void> generate(data<schema::CIface>& state, const std::string_view& func_name){ + if(c_interface_function_exists(state, func_name)){ + return make_error<err::invalid_state>("Function already exists"); + } + + auto& funcs = state.template get<"functions">(); + data<schema::CFunction> function; + function.template get<"name">().set(std::string{func_name}); + + try{ + std::stringstream iss; + schema_stringify<Schema>::apply(iss); + function.template get<"schema">().set(iss.str()); + }catch(const std::exception&){ + return make_error<err::out_of_memory>(); + } + + { + auto& c_var = function.template get<"return">(); + c_var.template get<"name">().set("ret_val"); + auto eov = c_data_translater<Ret>::generate(state, c_var); + if(eov.is_error()){ + return eov; + } + } + + { + auto eov = funcs.add(std::move(function)); + if(eov.is_error()){ + return eov; + } + } + + return void_t{}; + } +}; + +template<typename... Funcs, string_literal... Names> +struct c_data_translater<schema::Interface<schema::Member<Funcs,Names>...>> { + using Schema = schema::Interface<schema::Member<Funcs, Names>...>; + + template<std::size_t i> + static error_or<void> generate_ele(data<schema::CIface>& state){ + using InnerSchema = typename parameter_pack_type<i, Funcs...>::type; + constexpr string_literal Literal = parameter_key_pack_type<i, Names...>::literal; + { + auto eov = c_data_translater<InnerSchema>::generate(state, Literal.view()); + if(eov.is_error()){ + return eov; + } + } + + /** + * Continue if elements remain or finish + */ + if constexpr ( (i+1) < sizeof...(Funcs) ){ + return generate_ele<i+1>(state); + } + return void_t{}; + } + + static error_or<void> generate(data<schema::CIface>& state){ + try{ + std::stringstream iss; + schema_stringify<Schema>::apply(iss); + state.template get<"schema">().set(iss.str()); + }catch(const std::exception&){ + return make_error<err::out_of_memory>(); + } + + if constexpr (sizeof...(Funcs) > 0){ + return generate_ele<0>(state); + } + return void_t{}; + } +}; + +template<typename Interface> +error_or<void> generate_c_interface(data<schema::CIface>& state){ + auto eov = c_data_translater<Interface>::generate(state); + return eov; +} + +} |