From 3213bef6aa2b87cf8ea207e53ddf1b064539b46a Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Tue, 2 Apr 2024 16:27:41 +0200 Subject: core, tools: Fixed include bug and some other behaviours --- modules/core/c++/templates.hpp | 1 + modules/tools/c++/cli_analyzer.hpp | 60 ++++++++++++++++---------------------- modules/tools/examples/cli_mod.cpp | 60 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 35 deletions(-) (limited to 'modules') diff --git a/modules/core/c++/templates.hpp b/modules/core/c++/templates.hpp index 05b5a56..acbaeb0 100644 --- a/modules/core/c++/templates.hpp +++ b/modules/core/c++/templates.hpp @@ -1,5 +1,6 @@ #pragma once +#include "common.hpp" #include "string_literal.hpp" namespace saw { diff --git a/modules/tools/c++/cli_analyzer.hpp b/modules/tools/c++/cli_analyzer.hpp index a1dc731..4aa3d6c 100644 --- a/modules/tools/c++/cli_analyzer.hpp +++ b/modules/tools/c++/cli_analyzer.hpp @@ -1,7 +1,11 @@ #pragma once +#include #include +#include +#include +#include #include namespace saw { @@ -11,33 +15,28 @@ struct cli_mode { struct write {}; }; -template -struct cli_traverser { - static_assert(always_false, "Not supported"); +template +struct cli_traverser { + template + static error_or traverse(std::deque& sch_path, data& enc_data, std::string& json_data){ + return make_error("Exhausted path or structure is not supported as of this moment"); + } }; template struct cli_modifier { codec json; - codec encoded; + static_assert( std::is_same_v, "Not supported" ); 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); + auto eov = json.template encode(enc_data, json_data); if(eov.is_error()){ return eov; } @@ -55,24 +54,14 @@ struct cli_modifier { 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); + auto eov = json.template decode(json_data, enc_data); 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{}; } }; @@ -88,7 +77,7 @@ struct cli_traverser, Encoding> { 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); + return cli_traverser::template traverse(sch_path, enc_data.template get(), json_data); } if ( (i+1) < sizeof...(T)){ @@ -112,7 +101,7 @@ struct cli_traverser...>, Encoding> { if ( Literal.view() == sch_path.front() ){ sch_path.pop_front(); - return cli_traverser::traverse(sch_path, enc_data.template get(), json_data); + return cli_traverser::template traverse(sch_path, enc_data.template get(), json_data); } if constexpr ( (i+1) < sizeof...(T) ) { @@ -132,15 +121,15 @@ struct cli_traverser...>, Encoding> { */ cli_modifier mod; if constexpr (std::is_same_v){ - return mod.read(sch_path, json_data); + return mod.read(sch_path, enc_data, json_data); } else if constexpr (std::is_same_v) { - return mod.write(sch_path, json_data); + return mod.write(sch_path, enc_data, 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 traverse_member(sch_path, enc_data, json_data); } return make_error("No elements in struct while path isn't empty"); } @@ -157,21 +146,22 @@ struct parsed_args { }; template -int modify_data_on_cli(bool read_mode, const std::string& file_path, std::deque sch_path, std::string& json_data){ +int modify_data_on_cli(bool read_mode, const std::string_view& file_path, std::deque sch_path, std::string& json_data){ /** * Read data from file */ + - data data; + data enc_data; data native_data; if (read_mode) { - auto eov = impl::cli_modifier::traverse(sch_path, native_data, json_data); + auto eov = impl::cli_traverser::template 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); + auto eov = impl::cli_traverser::template traverse(sch_path, native_data, json_data); if(eov.is_error()){ return -1; } diff --git a/modules/tools/examples/cli_mod.cpp b/modules/tools/examples/cli_mod.cpp index 84b3dc6..42d4370 100644 --- a/modules/tools/examples/cli_mod.cpp +++ b/modules/tools/examples/cli_mod.cpp @@ -1,6 +1,66 @@ +#include "../c++/cli_analyzer.hpp" +#include +#include + +#include + +namespace schema { +using namespace saw::schema; + +using AnalyzeTest = Struct< + Member +>; +} int main(int argc, char** argv){ + /** + * Basic checking and wrapping args into an array of string_view + */ + int min_amount_args = 2; + + if( argc < (min_amount_args + 1) ) { + std::cerr<<"Not enough arguments"< 32){ + std::cerr<<"Too many arguments"< args_view; + for(int i = 0; i < argc; ++i){ + args_view.at(i) = {argv[i]}; + } + + uint64_t args_size = [&]() -> uint64_t{ + if(args_view.at(1) == "r"){ + return min_amount_args; + }else if (args_view.at(1) == "w"){ + return min_amount_args + 1u; + } + return 0u; + }(); + + if(args_size < min_amount_args){ + std::cerr<<"Invalid first argument. Must either be 'r' or 'w'"< bool { + if(args_view.at(1) == "w"){ + return false; + } + return true; + }(); + + std::string json_data; + std::deque sch_path; + int rc = saw::modify_data_on_cli(is_read_mode, args_view.at(2), sch_path, json_data); + + for(int i = 0; i < argc; ++i){ + std::cout<