From e7fb9cea480b012174e0a846c0159a7b102ebdcc Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Mon, 20 Nov 2023 16:56:08 +0100 Subject: tools: Renaming tool code files --- c++/tools/c_gen_iface.hpp | 240 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 240 insertions(+) create mode 100644 c++/tools/c_gen_iface.hpp (limited to 'c++/tools/c_gen_iface.hpp') diff --git a/c++/tools/c_gen_iface.hpp b/c++/tools/c_gen_iface.hpp new file mode 100644 index 0000000..867fe60 --- /dev/null +++ b/c++/tools/c_gen_iface.hpp @@ -0,0 +1,240 @@ +#include +#include + +#include +#include +#include + +#include + +namespace saw { + +namespace impl { + +template +struct c_primitive_string { + static_assert(always_false, "Not supported"); +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "int8_t"; +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "int16_t"; +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "int32_t"; +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "int64_t"; +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "uint8_t"; +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "uint16_t"; +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "uint32_t"; +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "uint64_t"; +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "float"; +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "double"; +}; + +template +struct c_data_translater { + static_assert(always_false, "Not supported"); +}; + +template +struct c_data_translater> { + using Schema = schema::Primitive; + static error_or generate(std::map& map_str, std::string& str, const std::string& prefix){ + str += c_primitive_string::value; + return void_t{}; + } +}; + +template +struct c_data_translater> { + static error_or generate(std::map& map_str, std::string& type_str, const std::string& prefix){ + type_str = prefix + "_"; + type_str += "array_"; + + std::string inner_type_str; + auto eov = impl::c_data_translater::generate(map_str, inner_type_str, prefix); + if(eov.is_error()){ + return eov; + } + + type_str += inner_type_str; + + if(Dim > 1){ + type_str += "_"; + type_str += std::to_string(Dim); + type_str += "d"; + } + + if(map_str.find(type_str) != map_str.end()){ + return void_t{}; + } + + std::string str = "struct "; + str += type_str; + + str += " {\n"; + + str += "\t" + inner_type_str + "* data;\n"; + str += "\tsize_t size;\n"; + if( Dim > 1 ){ + str += "\tsize_t dims["+std::to_string(Dim)+"];\n"; + } + + str += "};\n"; + + map_str.insert(std::make_pair(std::move(type_str), std::move(str))); + return void_t{}; + } +}; + +template +struct c_rpc_translater { + static_assert(always_false,"Not supported"); +}; + +template +struct c_rpc_translater> { + using Map = std::map; + + static error_or generate(const std::string& prefix, Map& type_map, Map& func_map){ + + return void_t{}; + } +}; + +template +struct c_rpc_translater, Names>...>> { + using Map = std::map; + + template + static error_or generate_member(const std::string& prefix, Map& type_map, Map& func_map){ + using Req = typename parameter_pack_type::type; + using Resp = typename parameter_pack_type::type; + constexpr string_literal lit = parameter_key_pack_type::literal; + + std::string func_name{lit.view()}; + if(func_map.find(func_name) != func_map.end()){ + return make_error(); + } + + { + std::string type_str; + auto eov = c_rpc_translater>::generate(prefix, type_map, func_map); + if(eov.is_error()){ + return eov; + } + } + + if constexpr ((i+1) < sizeof...(Requests) ){ + return generate_member(prefix, type_map, func_map); + } + return void_t{}; + } + + static error_or generate(const std::string& prefix){ + Map type_map, func_map; + + if constexpr ( sizeof...(Requests) > 0 ){ + return generate_member<0>(prefix, type_map, func_map); + } + + return void_t{}; + } +}; +} + +template +error_or generate_c_rpc_interface(){ + std::stringstream iss; + auto eov = impl::c_rpc_translater::generate(iss, "c_saw"); + + if(eov.is_error()){ + return std::move(eov.get_error()); + } + + try { + return iss.str(); + }catch(const std::exception& e){ + // Do nothing for now + } + return make_error(); +} + +error_or generate_array_example(){ + using Schema = schema::Array; + + using Schema2 = schema::Array; + + using Schema3 = schema::Array; + + std::string prefix = "c_saw"; + std::map map_str; + { + std::string type_str; + auto eov = impl::c_data_translater::generate(map_str, type_str, prefix); + if(eov.is_error()){ + return eov; + } + } + { + std::string type_str; + auto eov = impl::c_data_translater::generate(map_str, type_str, prefix); + if(eov.is_error()){ + return eov; + } + } + { + std::string type_str; + auto eov = impl::c_data_translater::generate(map_str, type_str, prefix); + if(eov.is_error()){ + return eov; + } + } + + std::cout<<"Prefix: "<