From 1fb10980808d66dfa6dec4ac87857d5c13bf7298 Mon Sep 17 00:00:00 2001 From: Claudius 'keldu' Holeksa Date: Thu, 22 Aug 2024 11:45:47 +0200 Subject: Removing storage as a parameter --- modules/codec/c++/base64.hpp | 2 +- modules/codec/c++/data.hpp | 123 ++++++++++++++++++++-------------------- modules/codec/c++/data_raw.hpp | 34 +++++------ modules/codec/c++/id_map.hpp | 8 +-- modules/codec/c++/interface.hpp | 57 +++++++++---------- modules/codec/tests/codec.cpp | 58 +++++++++---------- modules/codec/tests/csv.cpp | 2 +- 7 files changed, 140 insertions(+), 144 deletions(-) (limited to 'modules/codec') diff --git a/modules/codec/c++/base64.hpp b/modules/codec/c++/base64.hpp index 0c264be..141a63d 100644 --- a/modules/codec/c++/base64.hpp +++ b/modules/codec/c++/base64.hpp @@ -6,7 +6,7 @@ struct Base64 {}; } template<> -class data { +class data { public: using Schema = schema::String; private: diff --git a/modules/codec/c++/data.hpp b/modules/codec/c++/data.hpp index 77edf8e..5d3e4ee 100644 --- a/modules/codec/c++/data.hpp +++ b/modules/codec/c++/data.hpp @@ -17,9 +17,6 @@ #include "schema_meta.hpp" namespace saw { -namespace storage { -struct Default {}; -} namespace encode { struct Native { @@ -85,7 +82,7 @@ struct native_data_type> { using type = double; }; -template +template class data { private: static_assert(always_false, "Type not supported."); @@ -121,8 +118,8 @@ public: } }; -template -class data, encode::Native, Storage> { +template +class data, encode::Native> { public: using Schema = schema::Primitive; using MetaSchema = typename meta_schema::MetaSchema; @@ -144,29 +141,29 @@ public: typename native_data_type::type get() const {return value_;} - data operator*(const data& rhs)const{ + data operator*(const data& rhs)const{ return {get() * rhs.get()}; } - data operator/(const data& rhs)const{ + data operator/(const data& rhs)const{ return {get() / rhs.get()}; } - data operator+(const data& rhs)const{ + data operator+(const data& rhs)const{ return {get() + rhs.get()}; } - data& operator+=(const data& rhs)const{ + data& operator+=(const data& rhs)const{ value_ += rhs.get(); return *this; } - data& operator-=(const data& rhs)const{ + data& operator-=(const data& rhs)const{ value_ -= rhs.get(); return *this; } - data operator-(const data& rhs)const{ + data operator-(const data& rhs)const{ return {get() - rhs.get()}; } @@ -184,7 +181,7 @@ public: * Casts */ template - data cast_to() const { + data cast_to() const { auto raw_to = static_cast::type>(value_); return {raw_to}; } @@ -193,13 +190,13 @@ public: /** * Mixed precision class for native formats */ -template -class data, schema::Primitive>, encode::Native, Storage>{ +template +class data, schema::Primitive>, encode::Native>{ public: using Schema = schema::MixedPrecision, schema::Primitive>; using MetaSchema = typename meta_schema::MetaSchema; private: - data value_; + data value_; public: data():value_{}{} data(data):value_{}{} @@ -210,7 +207,7 @@ public: return value_.template cast_to().get(); } - data(const saw::data& val){ + data(const saw::data& val){ value_ = val.template cast_to(); } @@ -218,65 +215,65 @@ public: value_.set(static_cast(val)); } - data operator*(const data& rhs) const { + data operator*(const data& rhs) const { using CalcType = typename native_data_type::type; CalcType left = static_cast(value_.get()); CalcType right = static_cast(rhs.get()); return {left * right}; } - data operator*(const data& rhs) const { + data operator*(const data& rhs) const { using CalcType = typename native_data_type::type; CalcType left = static_cast(value_.get()); CalcType right = rhs.get(); return {left * right}; } - data operator/(const data& rhs)const{ + data operator/(const data& rhs)const{ using CalcType = typename native_data_type::type; CalcType left = static_cast(value_.get()); CalcType right = static_cast(rhs.get()); return {left / right}; } - data operator+(const data& rhs)const{ + data operator+(const data& rhs)const{ using CalcType = typename native_data_type::type; CalcType left = static_cast(value_.get()); CalcType right = static_cast(rhs.get()); return {left + right}; } - data& operator+=(const data& rhs)const{ + data& operator+=(const data& rhs)const{ *this = *this + rhs.get(); return *this; } - data operator-(const data& rhs)const{ + data operator-(const data& rhs)const{ using CalcType = typename native_data_type::type; CalcType left = static_cast(value_.get()); CalcType right = static_cast(rhs.get()); return {left - right}; } - data& operator-=(const data& rhs) const { + data& operator-=(const data& rhs) const { *this = *this - rhs.get(); return *this; } template - bool operator==(const data& rhs)const{ + bool operator==(const data& rhs)const{ return get() == rhs.get(); } template - bool operator<(const data& rhs) const { + bool operator<(const data& rhs) const { return get() < rhs.get(); } }; /* template -class data>, encode::Native, storage::Default { +class data>, encode::Native { public: using Schema = schema::Ref>; private: @@ -299,16 +296,16 @@ public: * Union type for native data classes */ template -class data...>, encode::Native, storage::Default> { +class data...>, encode::Native> { public: using Schema = schema::Union...>; using MetaSchema = typename meta_schema::MetaSchema; private: - std::variant...> value_; + std::variant...> value_; template struct init_helper { - static void apply(std::variant...>& val, data&& init){ + 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())}; @@ -332,12 +329,12 @@ public: SAW_DEFAULT_MOVE(data); template - void set(data::value, T...>::type, encode::Native, storage::Default> val){ + void set(data::value, T...>::type, encode::Native> val){ value_ = std::move(val); } template - data::value, T...>::type, encode::Native, storage::Default>& init(){ + data::value, T...>::type, encode::Native>& init(){ value_.template emplace::value>(); return get(); } @@ -348,12 +345,12 @@ public: } template - data::value, T...>::type, encode::Native, storage::Default>& get(){ + data::value, T...>::type, encode::Native>& get(){ return std::get::value>(value_); } template - const data::value, T...>::type, encode::Native, storage::Default>& get() const{ + const data::value, T...>::type, encode::Native>& get() const{ return std::get::value>(value_); } }; @@ -362,7 +359,7 @@ public: * Data class which represents a struct in the native format. */ template -class data...>, encode::Native, storage::Default> { +class data...>, encode::Native> { public: using Schema = schema::Struct...>; using MetaSchema = typename meta_schema::MetaSchema; @@ -370,11 +367,11 @@ private: /** * Tuple storing the member values. */ - std::tuple...> value_; + std::tuple...> value_; template struct init_helper { - static void apply(std::tuple...>& val, data&& init){ + static void apply(std::tuple...>& val, data&& init){ auto& val_i = val.template get(); auto& init_i = init.template get(); @@ -409,7 +406,7 @@ public: literal, literals... >::value , T...>::type - , encode::Native, storage::Default>& get(){ + , encode::Native>& get(){ return std::get::value>(value_); } @@ -423,7 +420,7 @@ public: literal, literals... >::value , T...>::type - , encode::Native, storage::Default>& get() const { + , encode::Native>& get() const { return std::get::value>(value_); } @@ -436,16 +433,16 @@ public: }; template -class data, encode::Native, storage::Default> { +class data, encode::Native> { public: using Schema = schema::Tuple; using MetaSchema = typename meta_schema::MetaSchema; private: - std::tuple...> value_; + std::tuple...> value_; template struct init_helper { - static void apply(std::tuple...>& val, data&& init){ + static void apply(std::tuple...>& val, data&& init){ auto& val_i = val.template get(); auto& init_i = init.template get(); @@ -468,12 +465,12 @@ public: SAW_DEFAULT_MOVE(data); template - data::type, encode::Native, storage::Default>& get(){ + data::type, encode::Native>& get(){ return std::get(value_); } template - const data::type, encode::Native, storage::Default>& get() const{ + const data::type, encode::Native>& get() const{ return std::get(value_); } @@ -524,14 +521,14 @@ public: }; template -class data, encode::Native, storage::Default> { +class data, encode::Native> { public: using Schema = schema::Array; using MetaSchema = typename meta_schema::MetaSchema; private: // data> dims_; std::array dims_; - std::vector> value_; + std::vector> value_; uint64_t get_full_size() const { uint64_t s = 1; @@ -570,7 +567,7 @@ class data, encode::Native, storage::Default> { } 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. @@ -605,29 +602,29 @@ class data, encode::Native, storage::Default> { 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)); } @@ -675,14 +672,14 @@ private: }; template -class data, encode::Native, storage::Default> { +class data, encode::Native> { public: using Schema = schema::FixedArray; using MetaSchema = typename meta_schema::MetaSchema; 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: @@ -692,29 +689,29 @@ public: value_{value__} {} - 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)); } @@ -748,7 +745,7 @@ private: * Data type representing string. */ template<> -class data { +class data { public: using Schema = schema::String; using MetaSchema = typename meta_schema::MetaSchema; diff --git a/modules/codec/c++/data_raw.hpp b/modules/codec/c++/data_raw.hpp index 40751a3..de5e7f0 100644 --- a/modules/codec/c++/data_raw.hpp +++ b/modules/codec/c++/data_raw.hpp @@ -211,7 +211,7 @@ public: * Data class which represents a struct in the native format. */ template -class data...>, encode::NativeRaw, storage::Default> { +class data...>, encode::NativeRaw> { public: using Schema = schema::Struct...>; using MetaSchema = typename meta_schema::MetaSchema; @@ -219,7 +219,7 @@ private: /** * Tuple storing the member values. */ - std::tuple...> value_; + std::tuple...> value_; template struct init_helper { @@ -258,7 +258,7 @@ public: literal, literals... >::value , T...>::type - , encode::NativeRaw, storage::Default>& get(){ + , encode::NativeRaw>& get(){ return std::get::value>(value_); } @@ -272,7 +272,7 @@ public: literal, literals... >::value , T...>::type - , encode::NativeRaw, storage::Default>& get() const { + , encode::NativeRaw>& get() const { return std::get::value>(value_); } @@ -285,12 +285,12 @@ public: }; template -class data, encode::NativeRaw, storage::Default> { +class data, encode::NativeRaw> { public: using Schema = schema::Tuple; using MetaSchema = typename meta_schema::MetaSchema; private: - std::tuple...> value_; + std::tuple...> value_; template struct init_helper { @@ -317,12 +317,12 @@ public: SAW_DEFAULT_MOVE(data); template - data::type, encode::NativeRaw, storage::Default>& get(){ + data::type, encode::NativeRaw>& get(){ return std::get(value_); } template - const data::type, encode::NativeRaw, storage::Default>& get() const{ + const data::type, encode::NativeRaw>& get() const{ return std::get(value_); } @@ -332,7 +332,7 @@ public: }; template -class data, encode::NativeRaw, storage::Default> { +class data, encode::NativeRaw> { public: using Schema = schema::Array; using MetaSchema = typename meta_schema::MetaSchema; @@ -348,7 +348,7 @@ class data, encode::NativeRaw, storage::Default> { std::conditional< is_primitive::value, typename native_data_type::type, - data + data > > value_; @@ -390,7 +390,7 @@ class data, encode::NativeRaw, storage::Default> { } 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. @@ -425,29 +425,29 @@ class data, encode::NativeRaw, storage::Default> { 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)); } diff --git a/modules/codec/c++/id_map.hpp b/modules/codec/c++/id_map.hpp index 18a331f..56becf0 100644 --- a/modules/codec/c++/id_map.hpp +++ b/modules/codec/c++/id_map.hpp @@ -19,7 +19,7 @@ private: /** * Container which stores the primary data */ - std::vector> data_; + std::vector> data_; /** * Container which tracks free'd/fragmented elements within the * main container @@ -65,7 +65,7 @@ public: * Inserts an element into the container and returns either an id on success * or an error on failure. */ - error_or> insert(data val) noexcept { + error_or> insert(data val) noexcept { /// @todo Fix size_t and id base type if(free_ids_.empty()){ try { @@ -90,7 +90,7 @@ public: /** * Insert as data with associated id. This can fail when it doesn't adhere to the standard approach. */ - error_or insert_as(data val, id id) noexcept { + error_or insert_as(data val, id id) noexcept { if(free_ids_.empty()){ if( id.get_value() != data_.size() ){ return make_error("Can't insert_as with provided ID. Doesn't match."); @@ -178,7 +178,7 @@ public: * Returns an error on failure and returns * a value pointer on success. */ - error_or*> find(const id& val){ + error_or*> find(const id& val){ if(val.get_value() >= data_.size()){ return make_error("ID is too large"); } diff --git a/modules/codec/c++/interface.hpp b/modules/codec/c++/interface.hpp index e1c9a12..f31b778 100644 --- a/modules/codec/c++/interface.hpp +++ b/modules/codec/c++/interface.hpp @@ -8,44 +8,44 @@ #include "data.hpp" namespace saw { -template +template class function; namespace impl { -template +template struct FuncReturnTypeHelper { - using Type = data; + using Type = data; }; -template -struct FuncReturnTypeHelper { +template +struct FuncReturnTypeHelper { using Type = void; }; -template +template struct FuncTypeHelper { - using Type = std::function::Type>(data&, Ctx)>; + using Type = std::function::Type>(data&, Ctx)>; }; -template -struct FuncTypeHelper { - using Type = std::function::Type>(data&)>; +template +struct FuncTypeHelper { + using Type = std::function::Type>(data&)>; }; } -template -class function, Encode, Storage, Context> { +template +class function, Encode, Context> { private: - typename impl::FuncTypeHelper::Type func_; - using ResponseDataType = typename impl::FuncReturnTypeHelper::Type; + typename impl::FuncTypeHelper::Type func_; + using ResponseDataType = typename impl::FuncReturnTypeHelper::Type; 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_(req); @@ -55,17 +55,17 @@ public: } }; -template +template class interface; -template -class interface, Names>...>, Encode, Storage, Context> { +template +class interface, Names>...>, Encode, Context> { public: using Schema = schema::Interface,Names>...>; private: - std::tuple, Encode, Storage, Context>...> funcs_; + std::tuple, Encode, Context>...> funcs_; public: - interface(function, Encode, Storage, Context>... funcs): + interface(function, Encode, Context>... funcs): funcs_{std::move(funcs)...} {} @@ -88,7 +88,6 @@ public: , Responses...>::type > , Encode - , Storage , Context >& get(){ return std::get::value>(funcs_); @@ -102,14 +101,14 @@ public: Lit, Names... >::value , Responses...>::type - , Encode, Storage>::Type > call( + , Encode>::Type > call( data< typename parameter_pack_type< parameter_key_pack_index< Lit, Names... >::value , Requests...>::type - , Encode, Storage>& req, + , Encode>& req, Context ctx = {} ){ if constexpr (std::is_same_v) { @@ -121,19 +120,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 1bec214..7ad1c89 100644 --- a/modules/codec/tests/codec.cpp +++ b/modules/codec/tests/codec.cpp @@ -54,7 +54,7 @@ using TestInterface = Interface< SAW_TEST("One Dimensional Array") { using namespace saw; - data arr{500u}; + data arr{500u}; int bar = 0; @@ -73,7 +73,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"); @@ -82,7 +82,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; @@ -99,7 +99,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; @@ -123,7 +123,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; @@ -149,7 +149,7 @@ SAW_TEST("Three Dimensional Array") { SAW_TEST("KelSimple UInt16 write"){ using namespace saw; - data native; + data native; data simple; codec codec; @@ -172,7 +172,7 @@ SAW_TEST("KelSimple UInt16 write"){ SAW_TEST("KelSimple UInt32 write"){ using namespace saw; - data native; + data native; data simple; codec codec; @@ -195,7 +195,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; @@ -228,7 +228,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">(); @@ -259,7 +259,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); @@ -272,10 +272,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; @@ -296,7 +296,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>(); @@ -327,7 +327,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"; @@ -352,14 +352,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()); @@ -378,26 +378,26 @@ 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()); @@ -409,10 +409,10 @@ SAW_TEST("Interface basics"){ return void_t{}; }; - auto iface = interface_factory::create(std::move(func_add), std::move(func_sub), std::move(func_multiply), std::move(func_void)); + auto iface = interface_factory::create(std::move(func_add), std::move(func_sub), std::move(func_multiply), std::move(func_void)); { - data native; + data native; native.get<0>().set(5); native.get<1>().set(40); @@ -423,7 +423,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); @@ -434,7 +434,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 638b864..ac394ce 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