diff options
author | Claudius "keldu" Holeksa <mail@keldu.de> | 2025-04-11 12:54:00 +0200 |
---|---|---|
committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2025-04-11 12:54:00 +0200 |
commit | 5c65123b44cd8e9761ff8c812b141a496a649019 (patch) | |
tree | 7b956d22e382ed59c260d7c8de9174eeb5579c6a | |
parent | 0457249dcfea285c4a3685d4806bbb9b39037091 (diff) |
Moving towards more forstio data types
-rw-r--r-- | modules/codec-json/c++/json.tmpl.hpp | 31 | ||||
-rw-r--r-- | modules/codec-json/tests/codec-json.cpp | 16 | ||||
-rw-r--r-- | modules/codec-netcdf/c++/netcdf.tmpl.hpp | 14 | ||||
-rw-r--r-- | modules/codec-netcdf/tests/codec-netcdf.cpp | 2 | ||||
-rw-r--r-- | modules/codec/c++/data.hpp | 30 | ||||
-rw-r--r-- | modules/codec/c++/simple.hpp | 24 | ||||
-rw-r--r-- | modules/codec/tests/transport.cpp | 16 |
7 files changed, 75 insertions, 58 deletions
diff --git a/modules/codec-json/c++/json.tmpl.hpp b/modules/codec-json/c++/json.tmpl.hpp index 1eb46bc..e51d456 100644 --- a/modules/codec-json/c++/json.tmpl.hpp +++ b/modules/codec-json/c++/json.tmpl.hpp @@ -199,7 +199,7 @@ struct json_encode<schema::Array<T,D>, FromEncode> { using Schema = schema::Array<T,D>; template<size_t Level> - static error_or<void> encode_level(const data<Schema, FromEncode>& from, buffer& to, std::array<std::size_t, D>& index, uint64_t depth, bool pretty){ + static error_or<void> encode_level(const data<Schema, FromEncode>& from, buffer& to, data<schema::FixedArray<schema::UInt64,D>, FromEncode>& index, uint64_t depth, bool pretty){ if constexpr (Level == D){ if(pretty){ auto eov = json_helper::print_pretty_indent(to, depth); @@ -232,7 +232,7 @@ struct json_encode<schema::Array<T,D>, FromEncode> { } } { - index[Level] = i; + index.at(Level) = i; auto eov = encode_level<Level+1>(from, to, index, depth+1u, pretty); if(eov.is_error()){ return eov; @@ -256,7 +256,7 @@ struct json_encode<schema::Array<T,D>, FromEncode> { } static error_or<void> encode(const data<Schema, FromEncode>& from, buffer& to, uint64_t depth, bool pretty) { - std::array<std::size_t, D> index; + data<schema::FixedArray<schema::UInt64,D>, FromEncode> index; return encode_level<0>(from, to, index, depth+1u, pretty); } }; @@ -289,7 +289,7 @@ struct json_encode<schema::FixedArray<T,D...>, FromEncode> { } } { - index[Level] = i; + index.at(Level) = i; if constexpr (sizeof...(DimPack) > 0){ auto eov = encode_level<Level+1, DimPack...>(from, to, index, depth, pretty); if(eov.is_error()){ @@ -837,7 +837,7 @@ struct json_decode<schema::Array<T,D>, ToDecode> { using Schema = schema::Array<T,D>; template<size_t Level> - static error_or<void> decode_flat_level(buffer_view& buff, std::vector<data<T, encode::Native>>& to, std::array<std::size_t, D>& index, std::array<std::size_t, D>& dims, bool log_dim){ + static error_or<void> decode_flat_level(buffer_view& buff, std::vector<data<T, encode::Native>>& to, data<schema::FixedArray<schema::UInt64,D>, ToDecode>& index, data<schema::FixedArray<schema::UInt64,D>, ToDecode>& dims, bool log_dim){ if constexpr (Level == D) { json_helper::skip_whitespace(buff); @@ -867,10 +867,10 @@ struct json_decode<schema::Array<T,D>, ToDecode> { is_empty = true; } - index[Level] = 0; + index.at(Level) = 0; for(;!is_empty;){ // We should have an element right now - auto eov = decode_flat_level<Level+1>(buff,to,index,dims, index[Level] == 0 && log_dim); + auto eov = decode_flat_level<Level+1>(buff,to,index,dims, index.at(Level).get() == 0 && log_dim); if(eov.is_error()){ return eov; } @@ -879,7 +879,7 @@ struct json_decode<schema::Array<T,D>, ToDecode> { return make_error<err::buffer_exhausted>(); } - ++index[Level]; + ++index.at(Level); if(buff.read() == ','){ buff.read_advance(1); } else if(buff.read() == ']'){ @@ -894,8 +894,8 @@ struct json_decode<schema::Array<T,D>, ToDecode> { } } if(log_dim){ - dims[Level] = index[Level]; - }else if (dims[Level] != index[Level]){ + dims.at(Level) = index.at(Level); + }else if (dims.at(Level) != index.at(Level)){ return make_error<err::invalid_state>("Not matching Array endings"); } } @@ -903,14 +903,14 @@ struct json_decode<schema::Array<T,D>, ToDecode> { } template<std::size_t Level> - static error_or<void> decode_unflat_level(std::vector<data<T,encode::Native>>& flat, data<schema::Array<T,D>, ToDecode>& to, std::array<std::size_t, D>& index, std::size_t& flat_index) { + static error_or<void> decode_unflat_level(std::vector<data<T,encode::Native>>& flat, data<schema::Array<T,D>, ToDecode>& to, data<schema::FixedArray<schema::UInt64,D>, ToDecode>& index, std::size_t& flat_index) { if constexpr ( Level == D ){ - auto& flat_data = flat.at(flat_index); + auto& flat_data = flat.at({flat_index}); to.at(index) = std::move(flat_data); ++flat_index; }else { const std::size_t dim_size = to.get_dim_size(Level); - for(index[Level] = 0; index[Level] < dim_size; ++index[Level]){ + for(index.at(Level) = 0; index.at(Level).get() < dim_size; ++index.at(Level)){ auto eov = decode_unflat_level<Level+1>(flat, to, index, flat_index); if(eov.is_error()){ @@ -922,9 +922,8 @@ struct json_decode<schema::Array<T,D>, ToDecode> { } static error_or<void> decode(buffer_view& buff, data<Schema, ToDecode>& to){ - std::array<std::size_t, D> index; - std::array<std::size_t, D> dims; - std::fill(dims.begin(), dims.end(), 0); + data<schema::FixedArray<schema::UInt64,D>, ToDecode> index; + data<schema::FixedArray<schema::UInt64,D>, ToDecode> dims; std::vector<data<T,encode::Native>> flat_array; auto eov = decode_flat_level<0>(buff, flat_array, index, dims, true); if(eov.is_error()){ diff --git a/modules/codec-json/tests/codec-json.cpp b/modules/codec-json/tests/codec-json.cpp index a181dc0..c5f08ec 100644 --- a/modules/codec-json/tests/codec-json.cpp +++ b/modules/codec-json/tests/codec-json.cpp @@ -273,10 +273,10 @@ SAW_TEST("Three Dim Array write and read"){ data<schema::TestMultiArray, encode::Native> native{2,1,2}; data<schema::TestMultiArray, encode::Json> json; - native.at({0,0,0}).set("multi"); - native.at({0,0,1}).set("baz"); - native.at({1,0,0}).set("foo"); - native.at({1,0,1}).set("bar"); + native.at({{0,0,0}}).set("multi"); + native.at({{0,0,1}}).set("baz"); + native.at({{1,0,0}}).set("foo"); + native.at({{1,0,1}}).set("bar"); codec<schema::TestMultiArray, encode::Json> codec; @@ -291,10 +291,10 @@ SAW_TEST("Three Dim Array write and read"){ native = {}; eov = codec.decode(json, native); SAW_EXPECT(eov.is_value(), "Decoding error"); - SAW_EXPECT(native.at({0,0,0}) == "multi", "Invalid Value at 0,0,0"); - SAW_EXPECT(native.at({0,0,1}) == "baz", "Invalid Value at 0,0,1"); - SAW_EXPECT(native.at({1,0,0}) == "foo", "Invalid Value at 1,0,0"); - SAW_EXPECT(native.at({1,0,1}) == "bar", "Invalid Value at 1,0,1"); + SAW_EXPECT(native.at({{0,0,0}}) == "multi", "Invalid Value at 0,0,0"); + SAW_EXPECT(native.at({{0,0,1}}) == "baz", "Invalid Value at 0,0,1"); + SAW_EXPECT(native.at({{1,0,0}}) == "foo", "Invalid Value at 1,0,0"); + SAW_EXPECT(native.at({{1,0,1}}) == "bar", "Invalid Value at 1,0,1"); } diff --git a/modules/codec-netcdf/c++/netcdf.tmpl.hpp b/modules/codec-netcdf/c++/netcdf.tmpl.hpp index bf257e4..ba357a5 100644 --- a/modules/codec-netcdf/c++/netcdf.tmpl.hpp +++ b/modules/codec-netcdf/c++/netcdf.tmpl.hpp @@ -229,18 +229,18 @@ struct netcdf_decode<schema::Array<T,Dim>, ToDecode> { using Schema = schema::Array<T,Dim>; template<std::size_t Level> - static error_or<void> decode_level(data<Schema, ToDecode>& to, int from, int nc_varid, std::array<std::size_t, Dim>& index, const std::array<size_t,Dim>& count){ + static error_or<void> decode_level(data<Schema, ToDecode>& to, int from, int nc_varid, data<schema::FixedArray<schema::UInt64,Dim>, ToDecode>& index, const std::array<size_t,Dim>& count){ if constexpr ( Level == Dim ){ int rc{}; typename native_data_type<T>::type val; - rc = nc_get_vara(from, nc_varid, index.data(), count.data(), &val); + rc = nc_get_vara(from, nc_varid, convert_to_stl<schema::FixedArray<schema::UInt64,Dim>, ToDecode>{}(index).data(), count.data(), &val); if(rc != NC_NOERR){ return make_error<err::critical>(); } to.at(index).set(val); }else{ const std::size_t dim_size = to.get_dim_size(Level); - for(index[Level] = 0; index[Level] < dim_size; ++index[Level]){ + for(index.at(Level) = 0; index.at(Level).get() < dim_size; ++index.at(Level)){ auto eov = decode_level<Level+1>(to, from, nc_varid, index, count); if(eov.is_error()){ return eov; @@ -270,18 +270,20 @@ struct netcdf_decode<schema::Array<T,Dim>, ToDecode> { return make_error<err::critical>(); } - std::array<std::size_t, Dim> dims; + data<schema::FixedArray<schema::UInt64,Dim>, ToDecode> dims; std::array<size_t, Dim> count; for(std::size_t i = 0; i < Dim; ++i){ - rc = nc_inq_dim(from, nc_dimids[i], nullptr, &dims[i]); + uint64_t dim_i = dims.at({i}).get(); + rc = nc_inq_dim(from, nc_dimids[i], nullptr, &dim_i); if(rc != NC_NOERR){ return make_error<err::critical>(); } + dims.at(i).set(dim_i); count[i] = 1; } to = {dims}; - std::array<std::size_t, Dim> index; + data<schema::FixedArray<schema::UInt64,Dim>, ToDecode> index; return decode_level<0>(to, from, nc_varid, index, count); } diff --git a/modules/codec-netcdf/tests/codec-netcdf.cpp b/modules/codec-netcdf/tests/codec-netcdf.cpp index 540f60e..6595851 100644 --- a/modules/codec-netcdf/tests/codec-netcdf.cpp +++ b/modules/codec-netcdf/tests/codec-netcdf.cpp @@ -97,7 +97,7 @@ SAW_TEST("NetCDF Struct Array read"){ for(std::size_t i = 0; i < 5; ++i){ for(std::size_t j = 0; j < 3; ++j){ int64_t exp_val = i * 3 + j; - SAW_EXPECT(arr.at({i,j}).get() == exp_val, "Incorrect value"); + SAW_EXPECT(arr.at({{i,j}}).get() == exp_val, "Incorrect value"); } } } diff --git a/modules/codec/c++/data.hpp b/modules/codec/c++/data.hpp index 9f65d08..ce57967 100644 --- a/modules/codec/c++/data.hpp +++ b/modules/codec/c++/data.hpp @@ -319,14 +319,6 @@ public: value_{value__} {} - constexpr data<T, encode::Native>& at(const std::array<uint64_t, sizeof...(D)>& ind){ - return value_.at(this->get_flat_index(ind)); - } - - constexpr const data<T, encode::Native>& at(const std::array<uint64_t, sizeof...(D)>& ind) const { - return value_.at(this->get_flat_index(ind)); - } - constexpr data<T, encode::Native>& at(data<schema::UInt64, encode::Native> i) { return value_.at(this->get_flat_index({i.get()})); } @@ -861,5 +853,27 @@ public: } }; +/** + * I need some basic support to be able to have some STL guarantees for now. + * I probably should to a trait check on the encoding to have some guarantees regarding the layout instead. + */ +template<typename Schema, typename Encode> +struct convert_to_stl { + static_assert(always_false<Schema,Encode>, "Encode or Schema not supported for stl conversion"); +}; + +template<typename T, uint64_t Dim> +struct convert_to_stl<schema::FixedArray<T,Dim>, encode::Native> { + using Schema = schema::FixedArray<T,Dim>; + static_assert(is_primitive<T>::value, "Only supports primitives"); + + std::array<typename native_data_type<T>::type, Dim> operator()(const data<Schema,encode::Native>& inp){ + std::array<typename native_data_type<T>::type,Dim> conv; + for(uint64_t i = 0u; i < Dim; ++i){ + conv[i] = inp.at({i}).get(); + } + return conv; + } +}; } diff --git a/modules/codec/c++/simple.hpp b/modules/codec/c++/simple.hpp index de35c3e..0e5ef6d 100644 --- a/modules/codec/c++/simple.hpp +++ b/modules/codec/c++/simple.hpp @@ -48,12 +48,12 @@ struct kelsimple_encode<schema::Primitive<T,N>, FromEnc> { template<typename T, size_t Dim, typename FromEnc> struct kelsimple_encode<schema::Array<T,Dim>, FromEnc> { template<std::size_t Level> - static error_or<void> encode_level(const data<schema::Array<T,Dim>, FromEnc>& from, buffer& to, std::array<std::size_t, Dim>& index){ + static error_or<void> encode_level(const data<schema::Array<T,Dim>, FromEnc>& from, buffer& to, data<schema::FixedArray<schema::UInt64, Dim>, FromEnc>& index){ if constexpr (Dim == Level){ return kelsimple_encode<T,FromEnc>::encode(from.at(index), to); } else { const std::size_t dim_size = from.get_dim_size(Level); - for(index[Level] = 0; (index.at(Level) < dim_size); ++index[Level]){ + for(index.at(Level) = 0; (index.at(Level).get() < dim_size); ++index.at(Level)){ auto eov = encode_level<Level+1>(from, to, index); if(eov.is_error()){ return eov; @@ -73,8 +73,10 @@ struct kelsimple_encode<schema::Array<T,Dim>, FromEnc> { } } { - std::array<std::size_t, Dim> index; - std::fill(index.begin(), index.end(), 0); + data<schema::FixedArray<schema::UInt64, Dim>,FromEnc> index; + for(data<schema::UInt64,FromEnc> i{0u}; i.get() < Dim; ++i){ + index.at(i) = 0u; + } return encode_level<0>(from, to, index); } @@ -210,12 +212,12 @@ struct kelsimple_decode<schema::Primitive<T,N>, FromEnc> { template<typename T, size_t Dim, typename FromEnc> struct kelsimple_decode<schema::Array<T,Dim>, FromEnc> { template<std::size_t Level> - static error_or<void> decode_level(buffer& from, data<schema::Array<T,Dim>, FromEnc>& to, std::array<std::size_t, Dim>& index){ + static error_or<void> decode_level(buffer& from, data<schema::Array<T,Dim>, FromEnc>& to, data<schema::FixedArray<schema::UInt64,Dim>, FromEnc>& index){ if constexpr (Level == Dim){ - return kelsimple_decode<T, FromEnc>::decode(from, {to.at(index)}); + return kelsimple_decode<T, FromEnc>::decode(from, to.at(index)); }else{ - const std::size_t dim_size = to.get_dim_size(Level); - for(index[Level] = 0; index[Level] < dim_size; ++index[Level]){ + const auto dim_size = to.get_dim_size(Level); + for(index.at(Level) = 0; index.at(Level).get() < dim_size; ++index.at(Level)){ auto eov = decode_level<Level+1>(from, to, index); if(eov.is_error()){ return eov; @@ -227,19 +229,19 @@ struct kelsimple_decode<schema::Array<T,Dim>, FromEnc> { static error_or<void> decode(buffer& from, data<schema::Array<T,Dim>, FromEnc>& to){ { - std::array<std::size_t, Dim> dims{}; + data<schema::FixedArray<schema::UInt64, Dim>, FromEnc> dims{}; for(std::size_t i = 0; i < Dim; ++i){ uint64_t val{}; auto eov = stream_value<schema::UInt64>::decode(val, from); if(eov.is_error()){ return eov; } - dims.at(i) = static_cast<std::size_t>(val); + dims.at({i}) = {static_cast<std::size_t>(val)}; } to = data<schema::Array<T,Dim>,FromEnc>{dims}; } { - std::array<std::size_t, Dim> index{}; + data<schema::FixedArray<schema::UInt64, Dim>, FromEnc> index{}; return decode_level<0>(from, to, index); } return void_t{}; diff --git a/modules/codec/tests/transport.cpp b/modules/codec/tests/transport.cpp index e0e105f..515092c 100644 --- a/modules/codec/tests/transport.cpp +++ b/modules/codec/tests/transport.cpp @@ -33,8 +33,8 @@ SAW_TEST("Transport FixedLen Struct write and slice"){ auto& tda = native.template get<"two_dim_array">(); tda = {1,2}; - tda.at({0,0}).set(5); - tda.at({0,1}).set(3); + tda.at({{0,0}}).set(5); + tda.at({{0,1}}).set(3); native.template get<"number">().set(410); } codec<schema::TestStruct, encode::KelSimple> codec; @@ -67,8 +67,8 @@ SAW_TEST("Transport FixedLen Struct write and slice"){ auto& tda = native.template get<"two_dim_array">(); tda = {1,2}; - tda.at({0,0}).set(2); - tda.at({0,1}).set(4); + tda.at({{0,0}}).set(2); + tda.at({{0,1}}).set(4); native.template get<"number">().set(709); } { @@ -118,8 +118,8 @@ SAW_TEST("Transport FixedLen Struct write and slice"){ { auto& tda = native.template get<"two_dim_array">(); - SAW_EXPECT(tda.at({0,0}).get() == 5, "Decoded value (0,0) wrong."); - SAW_EXPECT(tda.at({0,1}).get() == 3, "Decoded value (0,1) wrong."); + SAW_EXPECT(tda.at({{0,0}}).get() == 5, "Decoded value (0,0) wrong."); + SAW_EXPECT(tda.at({{0,1}}).get() == 3, "Decoded value (0,1) wrong."); SAW_EXPECT(native.template get<"number">().get() == 410, "Decoded value number wrong."); } } @@ -148,8 +148,8 @@ SAW_TEST("Transport FixedLen Struct write and slice"){ { auto& tda = native.template get<"two_dim_array">(); - SAW_EXPECT(tda.at({0,0}).get() == 2, "Decoded value (0,0) wrong."); - SAW_EXPECT(tda.at({0,1}).get() == 4, "Decoded value (0,1) wrong."); + SAW_EXPECT(tda.at({{0,0}}).get() == 2, "Decoded value (0,0) wrong."); + SAW_EXPECT(tda.at({{0,1}}).get() == 4, "Decoded value (0,1) wrong."); SAW_EXPECT(native.template get<"number">().get() == 709, "Decoded value number wrong."); } } |