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 +++++++++++----------- modules/codec/tests/codec.cpp | 58 +++++++++++++------------- modules/codec/tests/csv.cpp | 2 +- 4 files changed, 100 insertions(+), 101 deletions(-) (limited to 'modules/codec') 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)...}; } }; } diff --git a/modules/codec/tests/codec.cpp b/modules/codec/tests/codec.cpp index e8a2fb3..10d9d0c 100644 --- a/modules/codec/tests/codec.cpp +++ b/modules/codec/tests/codec.cpp @@ -51,7 +51,7 @@ using TestInterface = Interface< SAW_TEST("One Dimensional Array") { using namespace saw; - data> arr{500u}; + data arr{500u}; int bar = 0; @@ -70,7 +70,7 @@ SAW_TEST("One Dimensional Array") { SAW_TEST("One dim Array Default init"){ using namespace saw; - data> arr; + data arr; SAW_EXPECT(arr.get_dim_size(0) == 0, "Dim should be size 0"); SAW_EXPECT(arr.size() == 0, "Total size should also be zero"); @@ -79,7 +79,7 @@ SAW_TEST("One dim Array Default init"){ SAW_TEST("One dimensional Array Add"){ using namespace saw; - data> arr{5u}; + data arr{5u}; int bar = 0; @@ -96,7 +96,7 @@ SAW_TEST("One dimensional Array Add"){ SAW_TEST("Two Dimensional Array") { using namespace saw; - data> arr{10,30u}; + data arr{10,30u}; int expected_sum = (300 * 301) / 2; @@ -120,7 +120,7 @@ SAW_TEST("Two Dimensional Array") { SAW_TEST("Three Dimensional Array") { using namespace saw; - data> arr{10,10u,3}; + data arr{10,10u,3}; int expected_sum = (300 * 301) / 2; int bar = 0; @@ -146,7 +146,7 @@ SAW_TEST("Three Dimensional Array") { SAW_TEST("KelSimple UInt16 write"){ using namespace saw; - data> native; + data native; data simple; codec codec; @@ -169,7 +169,7 @@ SAW_TEST("KelSimple UInt16 write"){ SAW_TEST("KelSimple UInt32 write"){ using namespace saw; - data> native; + data native; data simple; codec codec; @@ -192,7 +192,7 @@ SAW_TEST("KelSimple UInt32 write"){ SAW_TEST("KelSimple Array write and read back"){ using namespace saw; - data> native{2,3}; + data native{2,3}; data simple; codec codec; @@ -225,7 +225,7 @@ SAW_TEST("KelSimple Array write and read back"){ SAW_TEST("KelSimple Struct write and read back"){ using namespace saw; - data> native; + data native; data simple; auto& tda = native.template get<"two_dim_array">(); @@ -256,7 +256,7 @@ SAW_TEST("KelSimple Struct write and read back"){ SAW_TEST("Native Union same type compilation"){ using namespace saw; - data> native; + data native; native.template init<"two">().set(50u); @@ -269,10 +269,10 @@ SAW_TEST("Native Union same type compilation"){ SAW_TEST("KelSimple Union write and read back"){ using namespace saw; - data> native; + data native; data simple; - native.template set<"number">(data>{}); + native.template set<"number">(data{}); native.template get<"number">().set(410); codec codec; @@ -293,7 +293,7 @@ SAW_TEST("KelSimple Union write and read back"){ SAW_TEST("KelSimple Tuple write and read back"){ using namespace saw; - data> native; + data native; data simple; auto& tda = native.template get<0>(); @@ -324,7 +324,7 @@ SAW_TEST("KelSimple Tuple write and read back"){ SAW_TEST("KelSimple String write and read back"){ using namespace saw; - data> native; + data native; data simple; std::string str = "FooBananaJoe"; @@ -349,14 +349,14 @@ SAW_TEST("Function basics"){ using namespace saw; { - data> native; + data native; native.get<0>().set(5); native.get<1>().set(40); - auto func_add = function_factory>::create( - [](data> req){ - data> resp; + auto func_add = function_factory::create( + [](data req){ + data resp; resp.set(req.get<0>().get() + req.get<1>().get()); @@ -375,36 +375,36 @@ SAW_TEST("Function basics"){ SAW_TEST("Interface basics"){ using namespace saw; - data> native; + data native; auto func_add = - [](data> req){ - data> resp; + [](data req){ + data resp; resp.set(req.get<0>().get() + req.get<1>().get()); return resp; }; auto func_sub = - [](data> req){ - data> resp; + [](data req){ + data resp; resp.set(req.get<0>().get() - req.get<1>().get()); return resp; }; - auto func_multiply = [](data> req){ - data> resp; + auto func_multiply = [](data req){ + data resp; resp.set(req.get<0>().get() * req.get<1>().get()); return resp; }; - auto iface = interface_factory>::create(std::move(func_add), std::move(func_sub), std::move(func_multiply)); + auto iface = interface_factory::create(std::move(func_add), std::move(func_sub), std::move(func_multiply)); { - data> native; + data native; native.get<0>().set(5); native.get<1>().set(40); @@ -415,7 +415,7 @@ SAW_TEST("Interface basics"){ SAW_EXPECT(val.get() == 45, "Sum is incorrect"); } { - data> native; + data native; native.get<0>().set(5); native.get<1>().set(40); @@ -426,7 +426,7 @@ SAW_TEST("Interface basics"){ SAW_EXPECT(val.get() == -35, "Sum is incorrect"); } { - data> native; + data native; native.get<0>().set(5); native.get<1>().set(40); diff --git a/modules/codec/tests/csv.cpp b/modules/codec/tests/csv.cpp index 04813c5..638b864 100644 --- a/modules/codec/tests/csv.cpp +++ b/modules/codec/tests/csv.cpp @@ -22,7 +22,7 @@ SAW_TEST("Codec Csv Encode Basic"){ using namespace saw; size_t n_size = 3; - data > native_data{n_size}; + data native_data{n_size}; { auto& row = native_data.at(0); row.template get<"string">().set("foo"); -- cgit v1.2.3