From 2790590996da2f6a0f4d59570de62078a5ac8ae2 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Mon, 5 Feb 2024 17:39:28 +0100 Subject: tools: Move to new module structure and moving to more schema based generation to convert more easily to json --- modules/tools/c++/SConscript | 33 +++++ modules/tools/c++/c_gen_iface.hpp | 271 +++++++++++++++++++++++++++++++++++++ modules/tools/c++/cli_analyzer.hpp | 183 +++++++++++++++++++++++++ 3 files changed, 487 insertions(+) create mode 100644 modules/tools/c++/SConscript create mode 100644 modules/tools/c++/c_gen_iface.hpp create mode 100644 modules/tools/c++/cli_analyzer.hpp (limited to 'modules/tools/c++') diff --git a/modules/tools/c++/SConscript b/modules/tools/c++/SConscript new file mode 100644 index 0000000..9ff46d0 --- /dev/null +++ b/modules/tools/c++/SConscript @@ -0,0 +1,33 @@ +#!/bin/false + +import os +import os.path +import glob + + +Import('env') + +dir_path = Dir('.').abspath + +# Environment for base library +tools_env = env.Clone(); + +tools_env.sources = sorted(glob.glob(dir_path + "/*.cpp")) +tools_env.headers = sorted(glob.glob(dir_path + "/*.hpp")) + +env.sources += tools_env.sources; +env.headers += tools_env.headers; + +## Static lib +objects_static = [] +tools_env.add_source_files(objects_static, tools_env.sources, shared=False); +env.library_static = tools_env.StaticLibrary('#build/forstio-tools', [objects_static]); + +# Set Alias +env.Alias('library_tools', [env.library_static]); + +env.targets += ['library_tools']; + +# Install +env.Install('$prefix/lib/', [env.library_static]); +env.Install('$prefix/include/forstio/tools/', [tools_env.headers]); 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 +#include +#include +#include +#include + +#include +#include +#include + +#include + +namespace saw { + +namespace schema { + +using CVar = Struct< + Member, + Member, + Member +>; + +using CStruct = Struct< + Member, + Member, + Member, "members"> +>; + +using CFunction = Struct< + Member, + Member, + Member, "params">, + Member +>; + +using CIface = Struct< + Member, + Member,"structs">, + Member,"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, "a"> + Member, "b"> +>; + +Needs to be flattened to + +template +using FlattenedSchemaElement = Struct< + Member, "path">, + Member +>; + +// Illegal, but doable with more lines of code +// Just use typename... T and +// "T..." for +// "Member,Names>...>" +// and specialize somewhere else +template +using FlattenedSchema = Struct< + Member, + Member, Names>... +>; +} + */ +template +struct schema_flattener { + static_assert(always_false, "Not supported"); +}; + +template +struct schema_flattener,schema::Member...>, Res> { +}; + +/** + * Helper to determine if we are dealing with primitive types + */ +template +struct c_is_primitive { + static constexpr bool value = false; +}; + +template +struct c_is_primitive> { + static constexpr bool value = true; +}; + +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"; +}; + +bool c_interface_function_exists(data& 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 +struct c_data_translater { + static_assert(always_false, "Not supported"); +}; + +template +struct c_data_translater> { + using Schema = schema::Primitive; + + static error_or generate(data& state, data& prim){ + /// @TODO Check if exists in CVars already + try{ + std::stringstream iss; + schema_stringify::apply(iss); + prim.template get<"schema">().set(iss.str()); + }catch(const std::exception&){ + return make_error(); + } + + return void_t{}; + } +}; + +template +struct c_data_translater> { + using Schema = schema::Function; + + static error_or generate(data& state, const std::string_view& func_name){ + if(c_interface_function_exists(state, func_name)){ + return make_error("Function already exists"); + } + + auto& funcs = state.template get<"functions">(); + data function; + function.template get<"name">().set(std::string{func_name}); + + try{ + std::stringstream iss; + schema_stringify::apply(iss); + function.template get<"schema">().set(iss.str()); + }catch(const std::exception&){ + return make_error(); + } + + { + auto& c_var = function.template get<"return">(); + c_var.template get<"name">().set("ret_val"); + auto eov = c_data_translater::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 +struct c_data_translater...>> { + using Schema = schema::Interface...>; + + template + static error_or generate_ele(data& state){ + using InnerSchema = typename parameter_pack_type::type; + constexpr string_literal Literal = parameter_key_pack_type::literal; + { + auto eov = c_data_translater::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(state); + } + return void_t{}; + } + + static error_or generate(data& state){ + try{ + std::stringstream iss; + schema_stringify::apply(iss); + state.template get<"schema">().set(iss.str()); + }catch(const std::exception&){ + return make_error(); + } + + if constexpr (sizeof...(Funcs) > 0){ + return generate_ele<0>(state); + } + return void_t{}; + } +}; + +template +error_or generate_c_interface(data& state){ + auto eov = c_data_translater::generate(state); + return eov; +} + +} diff --git a/modules/tools/c++/cli_analyzer.hpp b/modules/tools/c++/cli_analyzer.hpp new file mode 100644 index 0000000..402f03c --- /dev/null +++ b/modules/tools/c++/cli_analyzer.hpp @@ -0,0 +1,183 @@ +#pragma once + +#include + +#include + +namespace saw { +namespace impl { +struct cli_mode { + struct read {}; + struct write {}; +}; + +template +struct cli_traverser { + static_assert(always_false, "Not supported"); +}; + +template +struct cli_modifier { + codec json; + codec encoded; + + error_or read( + std::deque& sch_path, + data& enc_data, + std::string& json_data_str + ){ + data native; + if constexpr ( std::is_same_v ){ + auto eov = encoded.decode(enc_data, native); + if(eov.is_error()){ + return eov; + } + }else{ + native = enc_data; + } + { + data json_data; + auto eov = json.encode(native, json_data); + if(eov.is_error()){ + return eov; + } + + json_data_str = convert_to_string(json_data.get_buffer()); + + std::cout< write( + std::deque& sch_path, + data& enc_data, + std::string& json_data_str + ){ + data native; + { + /// @todo string to data + data json_data{ std::string_view{json_data_str} }; + auto eov = json.decode(json_data, native); + if(eov.is_error()){ + return eov; + } + } + if constexpr (std::is_same_v ){ + auto eov = encoded.encode(native, enc_data); + if(eov.is_error()){ + return eov; + } + }else{ + enc_data = native; + } + + return void_t{}; + } +}; + +template +struct cli_traverser, Encoding> { + using Schema = schema::Tuple; + + template + static error_or traverse_member(std::deque& sch_path, data& enc_data, std::string& json_data){ + using Type = typename parameter_pack_type::type; + + std::string num_str = std::to_string(i); + if( num_str == sch_path.front() ){ + sch_path.pop_front(); + return cli_traverser::traverse(sch_path, enc_data.template get(), json_data); + } + + if ( (i+1) < sizeof...(T)){ + return traverse_member(sch_path, enc_data, json_data); + } + return make_error("Didn't find tuple path element"); + } +}; + +/** + * Traverse the path until we hit the end of the provided path + */ +template +struct cli_traverser...>, Encoding> { + using Schema = schema::Struct...>; + + template + static error_or traverse_member(std::deque& sch_path, data& enc_data, std::string& json_data){ + using Type = typename parameter_pack_type::type; + constexpr string_literal Literal = parameter_key_pack_type::literal; + + if ( Literal.view() == sch_path.front() ){ + sch_path.pop_front(); + return cli_traverser::traverse(sch_path, enc_data.template get(), json_data); + } + + if constexpr ( (i+1) < sizeof...(T) ) { + return traverse_member(sch_path, enc_data, json_data); + } + return make_error("Didn't find struct path element"); + } + + template + static error_or traverse(std::deque& sch_path, data& enc_data, std::string& json_data){ + /** + * If our path is empty, then we have reached the desired destination. + */ + if(sch_path.empty()){ + /** + * Decide during this step if we are reading or not + */ + cli_modifier mod; + if constexpr (std::is_same_v){ + return mod.read(sch_path, json_data); + } else if constexpr (std::is_same_v) { + return mod.write(sch_path, json_data); + } else { + return make_error("We only support cli_mode::read and cli_mode::write"); + } + } else { + if constexpr ( sizeof...(T) > 0 ){ + return traverse_member(sch_path, json_data); + } + return make_error("No elements in struct while path isn't empty"); + } + + return void_t{}; + } +}; +} + +struct parsed_args { + bool read_mode = true; + std::string file_path; + std::deque sch_path; +}; + +template +int modify_data_on_cli(bool read_mode, const std::string& file_path, std::deque sch_path, std::string& json_data){ + /** + * Read data from file + */ + + + data data; + data native_data; + + if (read_mode) { + auto eov = impl::cli_modifier::traverse(sch_path, native_data, json_data); + if(eov.is_error()){ + return -1; + } + } else { + auto eov = impl::cli_modifier::traverse(sch_path, native_data, json_data); + if(eov.is_error()){ + return -1; + } + } + + return 0; +} +} -- cgit v1.2.3