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/.nix/derivation.nix | 11 +- modules/tools/SConscript | 32 --- modules/tools/SConstruct | 6 +- modules/tools/c++/SConscript | 33 +++ modules/tools/c++/c_gen_iface.hpp | 271 +++++++++++++++++++ modules/tools/c++/cli_analyzer.hpp | 183 +++++++++++++ modules/tools/c_gen_iface.cpp | 11 - modules/tools/c_gen_iface.hpp | 526 ------------------------------------- modules/tools/cli_analyzer.hpp | 183 ------------- modules/tools/tests/SConscript | 31 +++ modules/tools/tests/c_iface.cpp | 56 ++++ 11 files changed, 587 insertions(+), 756 deletions(-) delete mode 100644 modules/tools/SConscript 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 delete mode 100644 modules/tools/c_gen_iface.cpp delete mode 100644 modules/tools/c_gen_iface.hpp delete mode 100644 modules/tools/cli_analyzer.hpp create mode 100644 modules/tools/tests/SConscript create mode 100644 modules/tools/tests/c_iface.cpp diff --git a/modules/tools/.nix/derivation.nix b/modules/tools/.nix/derivation.nix index 6e3fbe1..81052f3 100644 --- a/modules/tools/.nix/derivation.nix +++ b/modules/tools/.nix/derivation.nix @@ -24,8 +24,15 @@ in stdenv.mkDerivation { forstio.core forstio.async forstio.io - forstio.codec - ]; + forstio.codec + forstio.codec-json + ]; + + doCheck = true; + checkPhase = '' + scons test + ./bin/tests + ''; outputs = ["out" "dev"]; } diff --git a/modules/tools/SConscript b/modules/tools/SConscript deleted file mode 100644 index 9b1ad89..0000000 --- a/modules/tools/SConscript +++ /dev/null @@ -1,32 +0,0 @@ -#!/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); -tools_env.c_array_example = tools_env.Program('#build/forstio-c-array-example', [objects_static]); - -# Set Alias -env.Alias('tools_c_array_example', [tools_env.c_array_example]); - -env.targets += ['tools_c_array_example']; - -# Install -env.Install('$prefix/bin/', [tools_env.c_array_example]); diff --git a/modules/tools/SConstruct b/modules/tools/SConstruct index 9a02291..20af11b 100644 --- a/modules/tools/SConstruct +++ b/modules/tools/SConstruct @@ -49,7 +49,8 @@ env=Environment(ENV=os.environ, variables=env_vars, CPPPATH=[], LIBS=[ 'forstio-core', 'forstio-async', - 'forstio-codec' + 'forstio-codec', + 'forstio-codec-json' ] ); env.__class__.add_source_files = add_kel_source_files @@ -62,7 +63,8 @@ env.headers = []; env.targets = []; Export('env') -SConscript('SConscript') +SConscript('c++/SConscript') +SConscript('tests/SConscript') env.Alias('cdb', env.cdb); env.Alias('all', [env.targets]); 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; +} +} diff --git a/modules/tools/c_gen_iface.cpp b/modules/tools/c_gen_iface.cpp deleted file mode 100644 index 996f3c3..0000000 --- a/modules/tools/c_gen_iface.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "c_gen_iface.hpp" - -int main(){ - // saw::generate_array_example(); - - auto eov = saw::generate_iface_example(); - if(eov.is_error()){ - return -1; - } - return 0; -} diff --git a/modules/tools/c_gen_iface.hpp b/modules/tools/c_gen_iface.hpp deleted file mode 100644 index 08cf2f4..0000000 --- a/modules/tools/c_gen_iface.hpp +++ /dev/null @@ -1,526 +0,0 @@ -#include -#include -#include - -#include -#include -#include - -#include - -#include - -namespace saw { - -namespace impl { -namespace schema { -using namespace saw::schema; - -using CVar = Struct< - Member, - Member, - Member ->; - -using CStruct = Struct< - Member, - Member, - Member, "members"> ->; - -using CFunction = Struct< - Member, - Member, - Member, "params"> ->; - -using CIface = Struct< - Member, - Member,"structs">, - Member,"function"> ->; -} -/** - * Type meant to provide future help if I decide to introduce more maps - */ -struct c_types { - - struct c_member { - std::string key; - std::string name; - bool is_primitive; - }; - - struct c_struct { - std::string kind; - std::string type; - std::string cpp_schema; - std::vector members; - }; - - struct c_response { - std::string key; - bool is_primitive; - }; - - struct c_func { - std::string cpp_schema; - std::string cpp_name; - c_response response; - std::vector requests; - }; - - using c_struct_map = std::map; - using c_func_map = std::map; - - struct state { - std::string interface_schema; - c_struct_map struct_map; - c_func_map func_map; - std::string prefix; - std::string postfix; - std::string encoding; - }; -}; - -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> { -}; - -using StructBindingSchema = Struct< - Member, - Member, - Member, - Member, "members"> ->; - -using FunctionBindingSchema = Struct< - Member, - Member, - Member ->; - -using BindingSchema = Struct< - Member, - Member, - Member, - Member, - Member, "structs">, - Member, "functions"> ->; -} - -/** - * 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"; -}; - -template -struct c_data_translater { - static_assert(always_false, "Not supported"); -}; - -template -struct c_data_translater> { - using StructMap = typename c_types::c_struct_map; - using Schema = schema::Primitive; - - static error_or generate(c_types::state& c_state, std::string& str){ - (void) c_state; - str += c_primitive_string::value; - return void_t{}; - } -}; - -template -struct c_data_translater> { - using Schema = schema::Array; - using StructMap = typename c_types::c_struct_map; - - static error_or generate(c_types::state& c_state, std::string& type_str){ - type_str = "array_"; - - std::string inner_type_str; - auto eov = impl::c_data_translater::generate(c_state, inner_type_str); - 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(c_state.struct_map.find(type_str) != c_state.struct_map.end()){ - return void_t{}; - } - - c_types::c_struct str; - - try { - std::stringstream iss; - schema_stringify::apply(iss); - str.cpp_schema = iss.str(); - }catch(const std::exception& e){ - return make_error(); - } - - /** - * Adding heap alloc data ptr - */ - { - c_types::c_member memb; - memb.key = inner_type_str; - memb.name = "data"; - memb.is_primitive = c_is_primitive::value; - // fn.members.emplace_back(std::move(memb)); - - str.members.emplace_back(std::move(memb)); - } - { - c_types::c_member memb; - } - - - str.type = "struct "; - str.type += type_str; - - str.type += " {\n"; - - str.type += "\t" + inner_type_str + "* data;\n"; - str.type += "\tsize_t size;\n"; - if( Dim > 1 ){ - str.type += "\tsize_t dims["+std::to_string(Dim)+"];\n"; - } - - str.type += "};\n"; - - c_state.struct_map.emplace(std::make_pair(type_str, std::move(str))); - return void_t{}; - } -}; - -template -struct c_iface_translater { - static_assert(always_false,"Not supported"); -}; - -template -struct c_iface_translater...>, Response>> { - using StructMap = typename c_types::c_struct_map; - using FuncMap = typename c_types::c_func_map; - - template - static error_or generate_request_member(c_types::state& c_state, std::array& type_arr){ - using Type = typename parameter_pack_type::type; - // constexpr string_literal lit = parameter_key_pack_type::literal; - - auto eov = c_data_translater::generate(c_state, type_arr.at(i)); - if(eov.is_error()){ - return eov; - } - - if constexpr ((i+1) < sizeof...(Request)){ - auto eov = generate_request_member(c_state, type_arr); - return eov; - } - - return void_t{}; - } - - template - static error_or generate_parameter(std::string& param_use, const std::array& req_type_arr){ - using Type = typename parameter_pack_type::type; - constexpr string_literal lit = parameter_key_pack_type::literal; - - std::string used_param; - if constexpr (c_is_primitive::value){ - used_param += req_type_arr.at(i); - } else { - used_param += "const " + req_type_arr.at(i) + "*"; - } - param_use += used_param + " " + std::string{lit.view()}; - - if constexpr ( (i+1) < sizeof...(Request) ){ - param_use += ", "; - return generate_parameter(param_use, req_type_arr); - } - - return void_t{}; - } - - static error_or generate(c_types::state& c_state, const std::string& func_name){ - std::array request_type_arr; - - if constexpr (sizeof...(Request) > 0){ - auto eov = generate_request_member<0>(c_state, request_type_arr); - if(eov.is_error()){ - return eov; - } - } - std::string response_type_str; - { - auto eov = c_data_translater::generate(c_state, response_type_str); - if(eov.is_error()){ - return eov; - } - } - - c_types::c_func fn; - fn.cpp_name = func_name; - - - c_state.func_map.emplace(std::make_pair(fn.cpp_name, std::move(fn))); - - return void_t{}; - } -}; - -template -struct c_iface_translater, Names>...>> { - using Schema = schema::Interface, Names>...>; - - template - static error_or generate_member(c_types::state& c_state){ - using Req = typename parameter_pack_type::type; - using Resp = typename parameter_pack_type::type; - constexpr string_literal lit = parameter_key_pack_type::literal; - - try{ - std::stringstream iss; - schema_stringify::apply(iss); - c_state.interface_schema = iss.str(); - }catch(const std::exception& e){ - (void) e; - return make_error(); - } - - std::string c_func_name = c_state.prefix + "_" + std::string{lit.view()}; - if(c_state.func_map.find(c_func_name) != c_state.func_map.end()){ - return make_error(); - } - - { - std::string type_str; - auto eov = c_iface_translater>::generate(c_state, std::string{lit.view()}); - if(eov.is_error()){ - return eov; - } - } - - if constexpr ((i+1) < sizeof...(Requests) ){ - return generate_member(c_state); - } - return void_t{}; - } - - static error_or generate(c_types::state& c_state){ - if constexpr ( sizeof...(Requests) > 0 ){ - return generate_member<0>(c_state); - } - - return void_t{}; - } -}; -} - -template -error_or generate_c_interface(typename impl::c_types::state& state){ - auto eov = impl::c_iface_translater::generate(state); - return eov; -} - -error_or generate_iface_example(){ - using Request1 = schema::Struct< - schema::Member, - schema::Member, - schema::Member, "baz"> - >; - using Response1 = schema::Int32; - - using Func1 = schema::Function; - - using Iface = schema::Interface< - schema::Member - >; - - using CIfaceDesc = schema::Struct< - Member, - Member - >; - - impl::c_types::state c_state; - c_state.prefix = "c_saw"; - c_state.encoding = "saw::encode::Native"; - - typename impl::c_types::c_struct_map& type_map = c_state.struct_map; - typename impl::c_types::c_func_map& func_map = c_state.func_map; - auto eov = generate_c_interface(c_state); - if(eov.is_error()){ - return eov; - } - std::cout<<"Interface: "< generate_array_example(){ - using Schema1 = schema::Array; - - using Schema2 = schema::Array; - - using Schema3 = schema::Array; - - std::string prefix = "c_saw"; - impl::c_types::state c_state; - c_state.prefix = prefix; - { - std::string type_str; - auto eov = impl::c_data_translater::generate(c_state, type_str); - if(eov.is_error()){ - return eov; - } - } - { - std::string type_str; - auto eov = impl::c_data_translater::generate(c_state, type_str); - if(eov.is_error()){ - return eov; - } - } - { - std::string type_str; - auto eov = impl::c_data_translater::generate(c_state, type_str); - if(eov.is_error()){ - return eov; - } - } - - std::cout<<"Prefix: "< - -#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; -} -} diff --git a/modules/tools/tests/SConscript b/modules/tools/tests/SConscript new file mode 100644 index 0000000..f8ffc92 --- /dev/null +++ b/modules/tools/tests/SConscript @@ -0,0 +1,31 @@ +#!/bin/false + +import os +import os.path +import glob + + +Import('env') + +dir_path = Dir('.').abspath + +# Environment for base library +test_cases_env = env.Clone(); + +test_cases_env.Append(LIBS=['forstio-test']); + +test_cases_env.sources = sorted(glob.glob(dir_path + "/*.cpp")) +test_cases_env.headers = sorted(glob.glob(dir_path + "/*.hpp")) + +env.sources += test_cases_env.sources; +env.headers += test_cases_env.headers; + +objects_static = [] +test_cases_env.add_source_files(objects_static, test_cases_env.sources, shared=False); +test_cases_env.program = test_cases_env.Program('#bin/tests', [objects_static, env.library_static]); + +# Set Alias +env.Alias('test', test_cases_env.program); +env.Alias('check', test_cases_env.program); + +env.targets += ['test','check']; diff --git a/modules/tools/tests/c_iface.cpp b/modules/tools/tests/c_iface.cpp new file mode 100644 index 0000000..121605a --- /dev/null +++ b/modules/tools/tests/c_iface.cpp @@ -0,0 +1,56 @@ +#include +#include + +#include "../c++/c_gen_iface.hpp" + +#include + +namespace { +namespace schema { +using namespace saw::schema; + +using TestEmptyInterface = Interface<>; + +using TestOneFunctionInterface = Interface< + Member, "one"> +>; +} + +template +void test_generate(std::string& res){ + using namespace saw; + + data c_iface; + { + auto eov = generate_c_interface(c_iface); + SAW_EXPECT(eov.is_value(), "Couldn't generate interface info"); + } + + data j_data; + codec j_codec; + + { + auto eov = j_codec.encode(c_iface, j_data); + SAW_EXPECT(eov.is_value(), "Couldn't encode data"); + } + + res = convert_to_string(j_data.get_buffer()); +} + +SAW_TEST("CIface Empty Interface"){ + using namespace saw; + + std::string res; + test_generate(res); + + std::cout<<"\n"<(res); + std::cout<<"\n"<