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 +++++++++---------- 5 files changed, 110 insertions(+), 114 deletions(-) (limited to 'modules/codec/c++') 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)...}; } }; } -- cgit v1.2.3