From aa2ecacd2e477eb5748f060d33138e0c12c0634f Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Mon, 27 Nov 2023 23:03:57 +0100 Subject: codec,core,tools: Adding dangling experimental changes --- c++/codec/args.h | 123 +++++++++++++++++++++++++++++++++++++++++++++ c++/codec/forst.h | 19 +++++++ c++/codec/forst.tmpl.h | 7 +++ c++/core/mcts.h | 5 ++ c++/core/platonic.h | 103 +++++++++++++++++++++++++++++++++++++ c++/core/string_literal.h | 2 +- c++/core/templates.h | 6 +++ c++/tools/c_gen_iface.hpp | 22 +++++++- c++/tools/cli_analyzer.hpp | 114 +++++++++++++++++++++++++++++++++++++++++ 9 files changed, 398 insertions(+), 3 deletions(-) create mode 100644 c++/codec/args.h create mode 100644 c++/codec/forst.tmpl.h create mode 100644 c++/core/mcts.h create mode 100644 c++/core/platonic.h create mode 100644 c++/tools/cli_analyzer.hpp (limited to 'c++') diff --git a/c++/codec/args.h b/c++/codec/args.h new file mode 100644 index 0000000..6bb75a2 --- /dev/null +++ b/c++/codec/args.h @@ -0,0 +1,123 @@ +#pragma once + +#include "schema.h" + +namespace saw { +namespace encode { +struct Args {}; +} + +namespace schema { +template +using Args = Struct< + Member, + Member, + Member +>; +} + +template +class data { + static_assert(always_false,"Not supported. Use the schema::Args alias or check how it's defined."); +}; + +template +class data...>, schema::Tuple>, encode::Args> { +private: + char** argv_; + int argc_; +public: + data(char** argv, int argc): + argv_{argv}, + argc_{argc} + {} + + size_t size() const { + if(argc_ < 0){ + return 0; + } + + static_assert(sizeof(int) <= sizeof(size_t), "size_t is smaller than int"); + + return static_cast(argc_); + } + + std::string_view arg_view(size_t i){ + if(i < size()){ + return std::string_view{argv_[i]}; + } + return ""; + } +}; + +namespace impl { +template +struct args_decode { + static_assert(always_false, "Not supported"); +}; + +template +struct args_decode...>,schema::Tuple>,ToDec> { + using Schema = schema::Args< + schema::Struct...>, + schema::Tuple + >; + + static error_or decode(data& from, data& to){ + if(from.size() == 0){ + return make_error(); + } + + to.get<"program">().set(std::string{from.arg_view(0)}); + std::size_t tuple_pos = 0; + for(size_t i = 1; i < from.size(); ++i){ + auto view = from.arg_view(i); + if(view.starts_with("--")){ + view.remove_prefix(std::min(2u, view.size())); + ++i; + + if( i >= from.size() ){ + return make_error(); + } + + auto value_view = from.arg_view(i); + + auto eov = decode_struct_member<0>(to.get<"args">(), view, value_view); + if(eov.is_error()){ + return eov; + } + } else { + auto eov = decode_tuple_member<0>(to.get<"positionals">(), view); + if(eov.is_error()){ + return eov; + } + ++tuple_pos; + } + } + + if(tuple_pos != sizeof...(Tup)){ + return make_error(); + } + + return void_t{}; + } +}; +} + +template +class codec { +public: + template + error_or decode(data& from, data& to){ + struct name_and_value { + std::string name; + std::string value; + }; + std::string program; + std::vector navs; + std::vector positionals; + + return void_t{}; + } +}; +} diff --git a/c++/codec/forst.h b/c++/codec/forst.h index cadf78e..7e8fbf0 100644 --- a/c++/codec/forst.h +++ b/c++/codec/forst.h @@ -6,8 +6,27 @@ namespace saw { namespace encode { struct KelForst {}; } +} + +#include "forst.tmpl.hpp" +namespace saw { class data { +private: + own buff_; +public: + data(own buff): + buff_{std::move(buff)} + {} +}; +template +class data...>, encode::KelForst> { +private: + own buff_; +public: + data(own buff): + buff_{std::move(buff)} + {} }; } diff --git a/c++/codec/forst.tmpl.h b/c++/codec/forst.tmpl.h new file mode 100644 index 0000000..30d18ef --- /dev/null +++ b/c++/codec/forst.tmpl.h @@ -0,0 +1,7 @@ +namespace saw { +namespace impl { +struct forst_decode { + +}; +} +} diff --git a/c++/core/mcts.h b/c++/core/mcts.h new file mode 100644 index 0000000..30eed2f --- /dev/null +++ b/c++/core/mcts.h @@ -0,0 +1,5 @@ +#pragma once + +namespace saw { + +} diff --git a/c++/core/platonic.h b/c++/core/platonic.h new file mode 100644 index 0000000..eefe99f --- /dev/null +++ b/c++/core/platonic.h @@ -0,0 +1,103 @@ +#pragma once + +#include "error.h" + +namespace saw { +namespace impl { +/** + * + */ +template +struct platonic_helper { + static_assert(always_false, "Unsupported platonic body. Alternatively it's not a platonic body"); +}; + +template +struct platonic_helper { + static constexpr surface_edges = 3u; +/* + static constexpr std::array, 4u> normals = { + {0.0, 0.0, -1.0}, // 1 + {}, // 2 + {}, // 3 + {} // 4 + }; +*/ +}; + +template +struct platonic_helper { + static constexpr surface_edges = 4u; + + static constexpr std::array, 6u> normals = { + { 1.0, 0.0, 0.0}, // 1 + {-1.0, 0.0, 0.0}, // 2 + { 0.0, 1.0, 0.0}, // 3 + { 0.0,-1.0, 0.0}, // 4 + { 0.0, 0.0, 1.0}, // 5 + { 0.0, 0.0,-1.0} // 6 + }; +}; + +template +struct platonic_helper { + static constexpr uint8_t surface_edges = 3u; +/* + static constexpr std::array, 20u> normals = { + {}, // 1 + {}, // 2 + {}, // 3 + {}, // 4 + {}, // 5 + {}, // 6 + {}, // 7 + {}, // 8 + {}, // 9 + {}, // 10 + {}, // 11 + {}, // 12 + {}, // 13 + {}, // 14 + {}, // 15 + {}, // 16 + {}, // 17 + {}, // 18 + {}, // 19 + {} // 20 + }; +*/ +}; +} +/** + * Container for describing each platonic body with + * helpers describing the orientation of each body. + */ +template +class platonic { +private: + /** + * Storage for the surfaces + */ + std::array surfaces_; +public: + constexpr uint8_t get_surface_edge_size() constexpr { + return platonic_helper::surface_edges; + } + + constexpr uint8_t get_surface_size() constexpr { + return N; + } + + T& at(uint8_t i){ + return surface_.at(i); + } + + const T& at(uint8_t i) const { + return surface_.at(i); + } + + constexpr std::array& get_surface_normal(size_t i) constexpr { + + } +}; +} diff --git a/c++/core/string_literal.h b/c++/core/string_literal.h index 7373d5c..30f62fd 100644 --- a/c++/core/string_literal.h +++ b/c++/core/string_literal.h @@ -51,7 +51,7 @@ public: }; template -constexpr string_literal operator""_sl() { +constexpr string_literal operator""_sl() { return string_literal{{Chars..., '\0'}}; } } // namespace saw diff --git a/c++/core/templates.h b/c++/core/templates.h index e2851a0..2eb0f7e 100644 --- a/c++/core/templates.h +++ b/c++/core/templates.h @@ -105,6 +105,8 @@ struct ct_multiply { namespace impl { template struct ct_convert_digits_table_helper { + static_assert(i <= 15, "Only conversion up to hex is supported"); + static constexpr std::array table = { '0', '1', '2', '3', '4', '5', '6', '7', @@ -119,18 +121,21 @@ template struct ct_convert_digits_helper { static constexpr size_t size = ct_convert_digits_helper::size; static constexpr std::array value = ct_convert_digits_helper::value; + static constexpr string_literal literal = ct_convert_digits_helper::literal; }; template struct ct_convert_digits_helper<0, Base, Digs...> { static constexpr size_t size = sizeof...(Digs); static constexpr std::array value = {Digs...}; + static constexpr string_literal literal = {{ct_convert_digits_table_helper::value..., '\0'}}; }; template struct ct_convert_digits_helper<0, Base> { static constexpr size_t size = 0; static constexpr std::array value = {0}; + static constexpr string_literal literal = "0"_sl; }; } @@ -140,5 +145,6 @@ struct ct_convert_to_digits { static constexpr size_t size = impl::ct_convert_digits_helper::size; static constexpr std::array value = impl::ct_convert_digits_helper::value; + static constexpr string_literal literal = impl::ct_convert_digits_helper::literal; }; } diff --git a/c++/tools/c_gen_iface.hpp b/c++/tools/c_gen_iface.hpp index 7e2e396..68c8779 100644 --- a/c++/tools/c_gen_iface.hpp +++ b/c++/tools/c_gen_iface.hpp @@ -20,6 +20,7 @@ struct c_types { struct c_member { std::string key; std::string name; + bool is_primitive; }; struct c_param { @@ -184,12 +185,29 @@ struct c_data_translater> { return make_error(); } + str.translate_to_c_source = "int translate_"; + str.translate_to_c_source += type_str; + str.translate_to_c_source += "(const saw::data<"; + str.translate_to_c_source += str.cpp_schema; + str.translate_to_c_source += ","; + str.translate_to_c_source += c_state.encoding; + str.translate_to_c_source += ">& cpp_data, struct "; + str.translate_to_c_source += type_str; + str.translate_to_c_source += "* c_data"; + + + str.translate_to_c_source += "int translate_"; + str.translate_to_c_source += inner_type_str; + str.translate_to_c_source += "(; + + { c_types::c_member memb; memb.key = inner_type_str; memb.name = "data"; - memb.is_primitive - fn.members.emplace_back(std::move(memb)); + memb.is_primitive = c_is_primitive::value; + // fn.members.emplace_back(std::move(memb)); + str.members.emplace_back(std::move(memb)); } str.def = "struct "; diff --git a/c++/tools/cli_analyzer.hpp b/c++/tools/cli_analyzer.hpp new file mode 100644 index 0000000..295ddf6 --- /dev/null +++ b/c++/tools/cli_analyzer.hpp @@ -0,0 +1,114 @@ +#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(data& enc_data, std::deque& sch_path, std::string& json_data_str){ + data native; + { + auto eov = encoded.decode(enc_data, native); + if(eov.is_error()){ + return eov; + } + } + { + 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()); + } + + return void_t{}; + } + + error_or write(data& enc_data, std::deque& sch_path, 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; + } + } + { + auto eov = encoded.encode(native, enc_data); + if(eov.is_error()){ + return eov; + } + }i + + return void_t{}; + + } +}; + +template +struct cli_traverser...>, Encoding> { + using Schema = schema::Struct...>; + + template + static error_or traverse(std::deque& sch_path, std::string& json_data){ + if(sch_path.empty()){ + 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(); + } + } else { + + } + + return void_t{}; + } +}; +} + +template +int modify_data_on_cli(int argc, char** argv){ + + /// @todo parse cli data + bool read_mode = true; + + std::deque sch_path; + std::string json_data; + + if (read_mode) { + auto eov = impl::cli_modifier::traverse(sch_path, json_data); + if(eov.is_error()){ + return -1; + } + } else { + auto eov = impl::cli_modifier::traverse(sch_path, json_data); + if(eov.is_error()){ + return -1; + } + } + + return 0; +} +} -- cgit v1.2.3