From 6ca91987ec91621577468ebf68fd68fc5433284a Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Sun, 21 Jul 2024 22:23:59 +0200 Subject: wip --- modules/codec/c++/data.hpp | 105 +++++++++++++++++++++++++++++++++++++- modules/codec/c++/schema_meta.hpp | 68 ++++++++++++++++++++++++ 2 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 modules/codec/c++/schema_meta.hpp (limited to 'modules/codec/c++') diff --git a/modules/codec/c++/data.hpp b/modules/codec/c++/data.hpp index b3e4fe2..b1e9401 100644 --- a/modules/codec/c++/data.hpp +++ b/modules/codec/c++/data.hpp @@ -13,6 +13,7 @@ #include #include "schema.hpp" +#include "schema_meta.hpp" namespace saw { namespace storage { @@ -83,17 +84,26 @@ struct native_data_type> { template class data { private: - static_assert(always_false, "Type not supported"); + static_assert(always_false, "Type not supported."); +}; + +template<> +class data { +public: + using Schema = schema::Void; + using MetaSchema = schema::Void; }; template class data, encode::Native, Storage> { public: using Schema = schema::Primitive; + using MetaSchema = typename meta_schema::MetaSchema; private: typename native_data_type::type value_; public: data():value_{}{} + data(data):value_{}{} SAW_DEFAULT_COPY(data); SAW_DEFAULT_MOVE(data); @@ -160,10 +170,12 @@ template class data, schema::Primitive>, encode::Native, Storage>{ public: using Schema = schema::MixedPrecision, schema::Primitive>; + using MetaSchema = typename meta_schema::MetaSchema; private: data value_; public: data():value_{}{} + data(data):value_{}{} data(typename saw::native_data_type::type val__):value_{static_cast::type>(val__)}{} @@ -261,10 +273,33 @@ public: */ template class data...>, encode::Native, storage::Default> { +public: + using Schema = schema::Union...>; + using MetaSchema = typename meta_schema::MetaSchema; private: std::variant...> value_; + + template + struct init_helper { + static void apply(std::variant...>& val, data&& init){ + if( init.index() == i ){ + auto& val_i = val.template init(); + val_i = {std::move(init.template get())}; + return; + } + + if constexpr ( (i+1u) < sizeof...(T) ){ + init_helper::apply(val, init); + } + } + }; public: data() = default; + data(data init){ + if constexpr ( 0u < sizeof...(T) ){ + init_helper<0u>::apply(value_, std::move(init)); + } + } SAW_DEFAULT_COPY(data); SAW_DEFAULT_MOVE(data); @@ -301,16 +336,39 @@ public: */ template class data...>, encode::Native, storage::Default> { +public: + using Schema = schema::Struct...>; + using MetaSchema = typename meta_schema::MetaSchema; private: /** * Tuple storing the member values. */ std::tuple...> value_; + + template + struct init_helper { + static void apply(std::tuple...>& val, data&& init){ + auto& val_i = val.template get(); + auto& init_i = init.template get(); + + val_i = {std::move(init_i)}; + + if constexpr ( (i+1u) < sizeof...(T) ){ + init_helper::apply(val, init); + } + } + }; public: /** * Default constructor. */ data() = default; + data(data init){ + if constexpr ( 0u < sizeof...(T) ){ + init_helper<0u>::apply(value_, std::move(init)); + } + } + SAW_DEFAULT_COPY(data); SAW_DEFAULT_MOVE(data); @@ -352,10 +410,33 @@ public: template class data, encode::Native, storage::Default> { +public: + using Schema = schema::Tuple; + using MetaSchema = typename meta_schema::MetaSchema; private: std::tuple...> value_; + + template + struct init_helper { + static void apply(std::tuple...>& val, data&& init){ + auto& val_i = val.template get(); + auto& init_i = init.template get(); + + val_i = {std::move(init_i)}; + + if constexpr ( (i+1u) < sizeof...(T) ){ + init_helper::apply(val, init); + } + } + }; public: data() = default; + data(data init){ + if constexpr ( 0u < sizeof...(T) ){ + init_helper<0u>::apply(value_, std::move(init)); + } + } + SAW_DEFAULT_COPY(data); SAW_DEFAULT_MOVE(data); @@ -376,6 +457,9 @@ public: template class data, encode::Native, storage::Default> { + public: + using Schema = schema::Array; + using MetaSchema = typename meta_schema::MetaSchema; private: // data> dims_; std::array dims_; @@ -410,6 +494,15 @@ class data, encode::Native, storage::Default> { value_.resize(get_full_size()); } + data(data init) + { + for(uint64_t i = 0; i < Dim; ++i){ + dims_.at(i) = init.at(i).get(); + } + value_.resize(get_full_size()); + } + + template error_or add(saw::data data){ /** @todo @@ -517,6 +610,9 @@ private: template class data, encode::Native, storage::Default> { +public: + using Schema = schema::FixedArray; + using MetaSchema = typename meta_schema::MetaSchema; private: //using inner_type = std::array, multiply_helper::value>; //std::unique_ptr value_; @@ -525,6 +621,7 @@ private: public: data() = default; + data(data){} data& at(const std::array& ind){ return value_.at(this->get_flat_index(ind)); @@ -583,6 +680,9 @@ private: */ template<> class data { +public: + using Schema = schema::String; + using MetaSchema = typename meta_schema::MetaSchema; private: /** * The native way to represent strings. @@ -593,6 +693,9 @@ public: data(uint64_t size__){ value_.resize(size__); } + data(data init){ + value_.resize(init.get()); + } SAW_DEFAULT_COPY(data); SAW_DEFAULT_MOVE(data); diff --git a/modules/codec/c++/schema_meta.hpp b/modules/codec/c++/schema_meta.hpp new file mode 100644 index 0000000..a37197a --- /dev/null +++ b/modules/codec/c++/schema_meta.hpp @@ -0,0 +1,68 @@ +#pragma once + +#include "schema.hpp" + +namespace saw { + +template +struct meta_schema { + static_assert(always_false, "Not supported Schema."); +}; + +template<> +struct meta_schema { + using Schema = schema::Void; + using MetaSchema = schema::Void; +}; + +template +struct meta_schema> { + using MetaSchema = schema::Void; + using Schema = schema::Primitive; +}; + +template +struct meta_schema, schema::Primitive >> { + using Schema = schema::MixedPrecision, schema::Primitive>; + using MetaSchema = schema::Void; +}; + +template +struct meta_schema...>> { + using MetaSchema = schema::Struct::MetaSchema,Lit>...>; + using Schema = schema::Struct...>; +}; + +template +struct meta_schema...>> { + using MetaSchema = schema::Union::MetaSchema,Lit>...>; + using Schema = schema::Union...>; +}; + +template +struct meta_schema> { + using MetaSchema = schema::Tuple::MetaSchema...>; + using Schema = schema::Tuple; +}; + +template<> +struct meta_schema { + using MetaSchema = schema::UInt64; + using Schema = schema::String; +}; + +/** + * Technically we also can distinguish if T is primitive. + */ +template +struct meta_schema> { + using MetaSchema = schema::FixedArray; + using Schema = schema::Array; +}; + +template +struct meta_schema> { + using MetaSchema = schema::Void; + using Schema = schema::FixedArray; +}; +} -- cgit v1.2.3