diff options
author | Claudius "keldu" Holeksa <mail@keldu.de> | 2024-02-05 17:39:28 +0100 |
---|---|---|
committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2024-02-05 17:39:28 +0100 |
commit | 2790590996da2f6a0f4d59570de62078a5ac8ae2 (patch) | |
tree | 526c188230bd29d5127cc13d10390b1cd134bebc /modules/tools/c++ | |
parent | 4e45bbc55c4011b919f0af8d43850748209f7f09 (diff) |
tools: Move to new module structure and moving to more schema based
generation to convert more easily to json
Diffstat (limited to 'modules/tools/c++')
-rw-r--r-- | modules/tools/c++/SConscript | 33 | ||||
-rw-r--r-- | modules/tools/c++/c_gen_iface.hpp | 271 | ||||
-rw-r--r-- | modules/tools/c++/cli_analyzer.hpp | 183 |
3 files changed, 487 insertions, 0 deletions
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 <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; +} + +} 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 <forstio/codec/schema.hpp> + +#include <iostream> + +namespace saw { +namespace impl { +struct cli_mode { + struct read {}; + struct write {}; +}; + +template<typename T, typename Encoding> +struct cli_traverser { + static_assert(always_false<T,Encoding>, "Not supported"); +}; + +template<typename Schema, typename Encoding> +struct cli_modifier { + codec<Schema, encode::Json> json; + codec<Schema, Encoding> encoded; + + error_or<void> read( + std::deque<std::string>& sch_path, + data<Schema, Encoding>& enc_data, + std::string& json_data_str + ){ + data<Schema, encode::Native> native; + if constexpr ( std::is_same_v<Encoding, encode::Native> ){ + auto eov = encoded.decode<encode::Native>(enc_data, native); + if(eov.is_error()){ + return eov; + } + }else{ + native = enc_data; + } + { + data<Schema, encode::Json> json_data; + auto eov = json.encode<encode::Native>(native, json_data); + if(eov.is_error()){ + return eov; + } + + json_data_str = convert_to_string(json_data.get_buffer()); + + std::cout<<json_data_str<<std::endl; + } + + return void_t{}; + } + + error_or<void> write( + std::deque<std::string>& sch_path, + data<Schema, Encoding>& enc_data, + std::string& json_data_str + ){ + data<Schema, encode::Native> native; + { + /// @todo string to data + data<Schema, encode::Json> json_data{ std::string_view{json_data_str} }; + auto eov = json.decode<encode::Native>(json_data, native); + if(eov.is_error()){ + return eov; + } + } + if constexpr (std::is_same_v<Encoding, encode::Native> ){ + auto eov = encoded.encode<encode::Native>(native, enc_data); + if(eov.is_error()){ + return eov; + } + }else{ + enc_data = native; + } + + return void_t{}; + } +}; + +template<typename... T, typename Encoding> +struct cli_traverser<schema::Tuple<T...>, Encoding> { + using Schema = schema::Tuple<T...>; + + template<typename Traversal, size_t i> + static error_or<void> traverse_member(std::deque<std::string>& sch_path, data<Schema, Encoding>& enc_data, std::string& json_data){ + using Type = typename parameter_pack_type<i,T...>::type; + + std::string num_str = std::to_string(i); + if( num_str == sch_path.front() ){ + sch_path.pop_front(); + return cli_traverser<Type, Encoding>::traverse<Traversal>(sch_path, enc_data.template get<i>(), json_data); + } + + if ( (i+1) < sizeof...(T)){ + return traverse_member<Traversal, i+1>(sch_path, enc_data, json_data); + } + return make_error<err::invalid_state>("Didn't find tuple path element"); + } +}; + +/** + * Traverse the path until we hit the end of the provided path + */ +template<typename... T, string_literal... Lits, typename Encoding> +struct cli_traverser<schema::Struct<schema::Member<T,Lits>...>, Encoding> { + using Schema = schema::Struct<schema::Member<T,Lits>...>; + + template<typename Traversal, size_t i> + static error_or<void> traverse_member(std::deque<std::string>& sch_path, data<Schema,Encoding>& enc_data, std::string& json_data){ + using Type = typename parameter_pack_type<i,T...>::type; + constexpr string_literal Literal = parameter_key_pack_type<i, Lits...>::literal; + + if ( Literal.view() == sch_path.front() ){ + sch_path.pop_front(); + return cli_traverser<Type>::traverse<Traversal>(sch_path, enc_data.template get<Literal>(), json_data); + } + + if constexpr ( (i+1) < sizeof...(T) ) { + return traverse_member<Traversal, i+1>(sch_path, enc_data, json_data); + } + return make_error<err::invalid_state>("Didn't find struct path element"); + } + + template<typename Traversal> + static error_or<void> traverse(std::deque<std::string>& sch_path, data<Schema,Encoding>& 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<Schema, Encoding> mod; + if constexpr (std::is_same_v<Traversal, cli_mode::read>){ + return mod.read(sch_path, json_data); + } else if constexpr (std::is_same_v<Traversal, cli_mode::write>) { + return mod.write(sch_path, json_data); + } else { + return make_error<err::invalid_state>("We only support cli_mode::read and cli_mode::write"); + } + } else { + if constexpr ( sizeof...(T) > 0 ){ + return traverse_member<Traversal, 0>(sch_path, json_data); + } + return make_error<err::invalid_state>("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<std::string> sch_path; +}; + +template<typename Schema, typename Encoding> +int modify_data_on_cli(bool read_mode, const std::string& file_path, std::deque<std::string> sch_path, std::string& json_data){ + /** + * Read data from file + */ + + + data<Schema, Encoding> data; + data<Schema, encode::Native> native_data; + + if (read_mode) { + auto eov = impl::cli_modifier<Schema, encode::Native>::traverse<impl::cli_mode::read>(sch_path, native_data, json_data); + if(eov.is_error()){ + return -1; + } + } else { + auto eov = impl::cli_modifier<Schema, encode::Native>::traverse<impl::cli_mode::write>(sch_path, native_data, json_data); + if(eov.is_error()){ + return -1; + } + } + + return 0; +} +} |