From e4e49a117702945066e3e279fa0f005200400cb7 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Wed, 12 Jun 2024 15:04:42 +0200 Subject: Separated Encoding and Storage approaches --- modules/codec/c++/data.hpp | 91 ++++++++++++++++++++--------------------- modules/codec/c++/interface.hpp | 50 +++++++++++----------- 2 files changed, 70 insertions(+), 71 deletions(-) (limited to 'modules/codec/c++') diff --git a/modules/codec/c++/data.hpp b/modules/codec/c++/data.hpp index 2a5f082..79f5264 100644 --- a/modules/codec/c++/data.hpp +++ b/modules/codec/c++/data.hpp @@ -20,10 +20,9 @@ struct Default {}; } namespace encode { -template struct Native {}; } -template +template class codec; /* * Helper for the basic message container, so the class doesn't have to be @@ -81,14 +80,14 @@ struct native_data_type> { using type = double; }; -template> +template class data { private: static_assert(always_false, "Type not supported"); }; -template -class data, encode::Native> { +template +class data, encode::Native, storage::Default> { private: typename native_data_type>::type value_; public: @@ -106,19 +105,19 @@ public: typename native_data_type>::type get() const {return value_;} - data, encode::Native> operator*(const data, encode::Native>& rhs)const{ + data, encode::Native, storage::Default> operator*(const data, encode::Native, storage::Default>& rhs)const{ return {get() * rhs.get()}; } - data, encode::Native> operator/(const data, encode::Native>& rhs)const{ + data, encode::Native, storage::Default> operator/(const data, encode::Native, storage::Default>& rhs)const{ return {get() / rhs.get()}; } - data, encode::Native> operator+(const data, encode::Native>& rhs)const{ + data, encode::Native, storage::Default> operator+(const data, encode::Native, storage::Default>& rhs)const{ return {get() + rhs.get()}; } - data, encode::Native> operator-(const data, encode::Native>& rhs)const{ + data, encode::Native, storage::Default> operator-(const data, encode::Native, storage::Default>& rhs)const{ return {get() - rhs.get()}; } @@ -136,7 +135,7 @@ public: * Casts */ template - data> cast_to(){ + data cast_to(){ auto raw_to = static_cast::type>(value_); return {raw_to}; } @@ -145,12 +144,12 @@ public: /** * Mixed precision class for native formats */ -template -class data, schema::Primitive>, encode::Native>{ +template +class data, schema::Primitive>, encode::Native, storage::Default>{ public: using Schema = schema::MixedPrecision, schema::Primitive>; private: - data> value_; + data value_; public: data():value_{}{} @@ -169,9 +168,9 @@ public: * Union type for native data classes */ template -class data...>, encode::Native> { +class data...>, encode::Native, storage::Default> { private: - std::variant>...> value_; + std::variant...> value_; public: data() = default; @@ -179,12 +178,12 @@ public: SAW_DEFAULT_MOVE(data); template - void set(data::value, T...>::type, encode::Native> val){ + void set(data::value, T...>::type, encode::Native, storage::Default> val){ value_ = std::move(val); } template - data::value, T...>::type, encode::Native>& init(){ + data::value, T...>::type, encode::Native, storage::Default>& init(){ value_.template emplace::value>(); return get(); } @@ -195,12 +194,12 @@ public: } template - data::value, T...>::type, encode::Native>& get(){ + data::value, T...>::type, encode::Native, storage::Default>& get(){ return std::get::value>(value_); } template - const data::value, T...>::type, encode::Native>& get() const{ + const data::value, T...>::type, encode::Native, storage::Default>& get() const{ return std::get::value>(value_); } }; @@ -209,12 +208,12 @@ public: * Data class which represents a struct in the native format. */ template -class data...>, encode::Native> { +class data...>, encode::Native, storage::Default> { private: /** * Tuple storing the member values. */ - std::tuple>...> value_; + std::tuple...> value_; public: /** * Default constructor. @@ -233,7 +232,7 @@ public: literal, literals... >::value , T...>::type - , encode::Native>& get(){ + , encode::Native, storage::Default>& get(){ return std::get::value>(value_); } @@ -247,7 +246,7 @@ public: literal, literals... >::value , T...>::type - , encode::Native>& get() const { + , encode::Native, storage::Default>& get() const { return std::get::value>(value_); } @@ -260,21 +259,21 @@ public: }; template -class data, encode::Native> { +class data, encode::Native, storage::Default> { private: - std::tuple>...> value_; + std::tuple...> value_; public: data() = default; SAW_DEFAULT_COPY(data); SAW_DEFAULT_MOVE(data); template - data::type, encode::Native>& get(){ + data::type, encode::Native, storage::Default>& get(){ return std::get(value_); } template - const data::type, encode::Native>& get() const{ + const data::type, encode::Native, storage::Default>& get() const{ return std::get(value_); } @@ -284,11 +283,11 @@ public: }; template -class data, encode::Native> { +class data, encode::Native, storage::Default> { private: // data> dims_; std::array dims_; - std::vector>> value_; + std::vector> value_; uint64_t get_full_size() const { uint64_t s = 1; @@ -320,7 +319,7 @@ class data, encode::Native> { } template - error_or add(saw::data> data){ + error_or add(saw::data data){ /** @todo * Generally the last dimension can always accept a element so to say. * Changing the others would require moving data due to the stride changing. @@ -355,29 +354,29 @@ class data, encode::Native> { static_assert(sizeof...(Dims)==Dim, "Argument size must be equal to the Dimension"); } - data>& at(const std::array& ind){ + data& at(const std::array& ind){ return value_.at(this->get_flat_index(ind)); } - const data>& at(const std::array& ind) const { + const data& at(const std::array& ind) const { return value_.at(this->get_flat_index(ind)); } template - data>& at(Dims... i){ + data& at(Dims... i){ return value_.at(this->get_flat_index(std::array{static_cast(i)...})); } template - const data>& at(Dims... i) const { + const data& at(Dims... i) const { return value_.at(this->get_flat_index(std::array{static_cast(i)...})); } - data>& at(const data>& i){ + data& at(const data>& i){ return value_.at(this->get_flat_index(i)); } - const data>& at(const data>& i)const{ + const data& at(const data>& i)const{ return value_.at(this->get_flat_index(i)); } @@ -425,39 +424,39 @@ private: }; template -class data, encode::Native> { +class data, encode::Native, storage::Default> { private: - //using inner_type = std::array>, multiply_helper::value>; + //using inner_type = std::array, multiply_helper::value>; //std::unique_ptr value_; - using ArrayT = std::array>, ct_multiply::value>; + using ArrayT = std::array, ct_multiply::value>; ArrayT value_; public: data() = default; - data>& at(const std::array& ind){ + data& at(const std::array& ind){ return value_.at(this->get_flat_index(ind)); } - const data>& at(const std::array& ind) const { + const data& at(const std::array& ind) const { return value_.at(this->get_flat_index(ind)); } template - data>& at(Dims... i) { + data& at(Dims... i) { return value_.at(this->get_flat_index({i...})); } template - const data>& at(Dims... i) const { + const data& at(Dims... i) const { return value_.at(this->get_flat_index({i...})); } - data>& at(const data>& i){ + data& at(const data>& i){ return value_.at(this->get_flat_index(i)); } - const data>& at(const data>& i)const{ + const data& at(const data>& i)const{ return value_.at(this->get_flat_index(i)); } @@ -491,7 +490,7 @@ private: * Data type representing string. */ template<> -class data> { +class data { private: /** * The native way to represent strings. diff --git a/modules/codec/c++/interface.hpp b/modules/codec/c++/interface.hpp index 8c6808b..d55b415 100644 --- a/modules/codec/c++/interface.hpp +++ b/modules/codec/c++/interface.hpp @@ -8,32 +8,32 @@ #include "data.hpp" namespace saw { -template +template class function; namespace impl { -template +template struct FuncTypeHelper { - using Type = std::function(data, Ctx)>; + using Type = std::function(data, Ctx)>; }; -template -struct FuncTypeHelper { - using Type = std::function(data)>; +template +struct FuncTypeHelper { + using Type = std::function(data)>; }; } -template -class function, Encode, Context> { +template +class function, Encode, Storage, Context> { private: - typename impl::FuncTypeHelper::Type func_; + typename impl::FuncTypeHelper::Type func_; public: template function(Func func): func_{std::move(func)} {} - error_or> call(data req, Context ctx = {}){ + error_or> call(data req, Context ctx = {}){ if constexpr (std::is_same_v){ (void) ctx; return func_(std::move(req)); @@ -43,17 +43,17 @@ public: } }; -template +template class interface; -template -class interface, Names>...>, Encode, Context> { +template +class interface, Names>...>, Encode, Storage, Context> { public: using Schema = schema::Interface,Names>...>; private: - std::tuple, Encode, Context>...> funcs_; + std::tuple, Encode, Storage, Context>...> funcs_; public: - interface(function, Encode, Context>... funcs): + interface(function, Encode, Storage, Context>... funcs): funcs_{std::move(funcs)...} {} @@ -75,8 +75,8 @@ public: >::value , Responses...>::type > - , - Encode + , Encode + , Storage , Context >& get(){ return std::get::value>(funcs_); @@ -90,14 +90,14 @@ public: Lit, Names... >::value , Responses...>::type - , Encode>> call( + , Encode, Storage>> call( data< typename parameter_pack_type< parameter_key_pack_index< Lit, Names... >::value , Requests...>::type - , Encode> req, + , Encode, Storage> req, Context ctx = {} ){ if constexpr (std::is_same_v) { @@ -109,19 +109,19 @@ public: } }; -template +template struct function_factory { template - static function create(Func func){ - return function {std::move(func)}; + static function create(Func func){ + return function {std::move(func)}; } }; -template +template struct interface_factory { template - static interface create(Func... func){ - return interface{std::move(func)...}; + static interface create(Func... func){ + return interface{std::move(func)...}; } }; } -- cgit v1.2.3