diff options
author | Claudius "keldu" Holeksa <mail@keldu.de> | 2023-11-27 23:03:57 +0100 |
---|---|---|
committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2023-11-27 23:03:57 +0100 |
commit | aa2ecacd2e477eb5748f060d33138e0c12c0634f (patch) | |
tree | d0360b420ecddaf6b9986c004ea3e0b962c9ca1b /c++ | |
parent | 0cf04f2d1ba5ed2a18fe9f3501f363cb3ff76c9f (diff) |
codec,core,tools: Adding dangling experimental changes
Diffstat (limited to 'c++')
-rw-r--r-- | c++/codec/args.h | 123 | ||||
-rw-r--r-- | c++/codec/forst.h | 19 | ||||
-rw-r--r-- | c++/codec/forst.tmpl.h | 7 | ||||
-rw-r--r-- | c++/core/mcts.h | 5 | ||||
-rw-r--r-- | c++/core/platonic.h | 103 | ||||
-rw-r--r-- | c++/core/string_literal.h | 2 | ||||
-rw-r--r-- | c++/core/templates.h | 6 | ||||
-rw-r--r-- | c++/tools/c_gen_iface.hpp | 22 | ||||
-rw-r--r-- | c++/tools/cli_analyzer.hpp | 114 |
9 files changed, 398 insertions, 3 deletions
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<typename InnerSchemaStruct, typename InnerSchemaTuple> +using Args = Struct< + Member<String, "program">, + Member<InnerSchemaStruct, "args">, + Member<InnerSchemaTuple, "positionals"> +>; +} + +template<typename T> +class data<T, encode::Args> { + static_assert(always_false<T>,"Not supported. Use the schema::Args alias or check how it's defined."); +}; + +template<typename... Str, string_literal... Lits, typename... Tup> +class data<schema::Args<schema::Struct<schema::Member<Str,Lits>...>, schema::Tuple<Tup...>>, 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<size_t>(argc_); + } + + std::string_view arg_view(size_t i){ + if(i < size()){ + return std::string_view{argv_[i]}; + } + return ""; + } +}; + +namespace impl { +template<typename T, typename ToDec> +struct args_decode { + static_assert(always_false<T,ToDec>, "Not supported"); +}; + +template<typename... Str, string_literal... Lits, typename... Tup, typename ToDec> +struct args_decode<schema::Args<schema::Struct<schema::Member<Str,Lits>...>,schema::Tuple<Tup...>>,ToDec> { + using Schema = schema::Args< + schema::Struct<schema::Member<Str,Lits>...>, + schema::Tuple<Tup...> + >; + + static error_or<void> decode(data<Schema, encode::Args>& from, data<Schema,ToDec>& to){ + if(from.size() == 0){ + return make_error<err::invalid_state>(); + } + + 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<err::invalid_state>(); + } + + 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<err::invalid_state>(); + } + + return void_t{}; + } +}; +} + +template<typename Schema> +class codec<Schema, encode::Args> { +public: + template<typename ToDec> + error_or<void> decode(data<Schema, encode::Args>& from, data<Schema, ToDec>& to){ + struct name_and_value { + std::string name; + std::string value; + }; + std::string program; + std::vector<name_and_value> navs; + std::vector<std::string> 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<schema::String, encode::KelForst> { +private: + own<buffer> buff_; +public: + data(own<buffer> buff): + buff_{std::move(buff)} + {} +}; +template<typename... T, string_literal... Keys> +class data<schema::Struct<schema::Member<T,Keys>...>, encode::KelForst> { +private: + own<buffer> buff_; +public: + data(own<buffer> 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<typename Prec, uint8_t N> +struct platonic_helper { + static_assert(always_false<Prec,N>, "Unsupported platonic body. Alternatively it's not a platonic body"); +}; + +template<typename Prec> +struct platonic_helper<Prec,4u> { + static constexpr surface_edges = 3u; +/* + static constexpr std::array<std::array<Prec,3u>, 4u> normals = { + {0.0, 0.0, -1.0}, // 1 + {}, // 2 + {}, // 3 + {} // 4 + }; +*/ +}; + +template<typename Prec> +struct platonic_helper<Prec,6u> { + static constexpr surface_edges = 4u; + + static constexpr std::array<std::array<Prec,3u>, 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<typename Prec> +struct platonic_helper<Prec,20u> { + static constexpr uint8_t surface_edges = 3u; +/* + static constexpr std::array<std::array<Prec,3u>, 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<typename T, typename Prec, uint8_t N> +class platonic { +private: + /** + * Storage for the surfaces + */ + std::array<T,N> surfaces_; +public: + constexpr uint8_t get_surface_edge_size() constexpr { + return platonic_helper<T,N>::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<Prec, 3>& 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 <typename T, T... Chars> -constexpr string_literal<T, sizeof...(Chars)+1u> operator""_sl() { +constexpr string_literal<T, sizeof...(Chars) + 1u> operator""_sl() { return string_literal<T, sizeof...(Chars) + 1u>{{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<T, V0, VN...> { namespace impl { template<typename T, size_t i> struct ct_convert_digits_table_helper { + static_assert(i <= 15, "Only conversion up to hex is supported"); + static constexpr std::array<T, 16> table = { '0', '1', '2', '3', '4', '5', '6', '7', @@ -119,18 +121,21 @@ template<uint64_t Num, uint64_t Base, uint64_t... Digs> struct ct_convert_digits_helper { static constexpr size_t size = ct_convert_digits_helper<Num / Base, Base, Num % Base, Digs...>::size; static constexpr std::array<uint64_t, size> value = ct_convert_digits_helper<Num / Base, Base, Num % Base, Digs...>::value; + static constexpr string_literal literal = ct_convert_digits_helper<Num / Base, Base, Num % Base, Digs...>::literal; }; template<uint64_t Base, uint64_t... Digs> struct ct_convert_digits_helper<0, Base, Digs...> { static constexpr size_t size = sizeof...(Digs); static constexpr std::array<uint64_t, size> value = {Digs...}; + static constexpr string_literal literal = {{ct_convert_digits_table_helper<char, Digs>::value..., '\0'}}; }; template<uint64_t Base> struct ct_convert_digits_helper<0, Base> { static constexpr size_t size = 0; static constexpr std::array<uint64_t, 1> 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<Num, Base>::size; static constexpr std::array<uint64_t, size> value = impl::ct_convert_digits_helper<Num, Base>::value; + static constexpr string_literal literal = impl::ct_convert_digits_helper<Num,Base>::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<schema::Array<T,Dim>> { return make_error<err::critical>(); } + 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<T>::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 <forstio/codec/schema.h> + +#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(data<Schema, Encoding>& enc_data, std::deque<std::string>& sch_path, std::string& json_data_str){ + data<Schema, encode::Native> native; + { + auto eov = encoded.decode<encode::Native>(enc_data, native); + if(eov.is_error()){ + return eov; + } + } + { + 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()); + } + + return void_t{}; + } + + error_or<void> write(data<Schema, Encoding>& enc_data, std::deque<std::string>& sch_path, 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; + } + } + { + auto eov = encoded.encode<encode::Native>(native, enc_data); + if(eov.is_error()){ + return eov; + } + }i + + return void_t{}; + + } +}; + +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> + static error_or<void> traverse(std::deque<std::string>& sch_path, std::string& json_data){ + if(sch_path.empty()){ + 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>(); + } + } else { + + } + + return void_t{}; + } +}; +} + +template<typename Schema, typename Encoding> +int modify_data_on_cli(int argc, char** argv){ + + /// @todo parse cli data + bool read_mode = true; + + std::deque<std::string> sch_path; + std::string json_data; + + if (read_mode) { + auto eov = impl::cli_modifier<Schema, Encoding>::traverse<impl::cli_mode::read>(sch_path, json_data); + if(eov.is_error()){ + return -1; + } + } else { + auto eov = impl::cli_modifier<Schema, Encoding>::traverse<impl::cli_mode::write>(sch_path, json_data); + if(eov.is_error()){ + return -1; + } + } + + return 0; +} +} |