From 0302bb7d3b1ab2ac936911a4466a2530650c7b83 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Thu, 9 Nov 2023 21:56:18 +0100 Subject: codec-netcdf: Basic struct and primitive write in working order --- c++/codec-netcdf/netcdf.tmpl.h | 156 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 139 insertions(+), 17 deletions(-) (limited to 'c++/codec-netcdf/netcdf.tmpl.h') diff --git a/c++/codec-netcdf/netcdf.tmpl.h b/c++/codec-netcdf/netcdf.tmpl.h index f3b36b8..00bfbf4 100644 --- a/c++/codec-netcdf/netcdf.tmpl.h +++ b/c++/codec-netcdf/netcdf.tmpl.h @@ -48,6 +48,125 @@ struct netcdf_is_group...>> { }; template +struct netcdf_encode; + +template +struct netcdf_encode, ToEncode> { + using Schema = schema::Primitive; + + static error_or encode(const data& from, int ncid, int ncvarid){ + int rc{}; + + typename native_data_type::type val = from.get(); + + rc = nc_put_var(ncid, ncvarid, &val); + if(rc != NC_NOERR) { + return make_error(); + } + + return void_t{}; + } + + static error_or encode_meta(const data& from, int ncid, int ncvarid){ + (void) from; + (void) ncid; + (void) ncvarid; + return void_t{}; + } +}; + +template +struct netcdf_encode...>, ToEncode> { + using Schema = schema::Struct...>; + + template + static error_or encode_member(const data& from, int ncid){ + using Type = typename parameter_pack_type::type; + constexpr string_literal Literal = parameter_key_pack_type::literal; + { + /** + * We have to ask for the internal id of the netcdf structure + */ + if constexpr (netcdf_is_group::value){ + return make_error(); + }else{ + int nc_varid{}; + int rc {}; + rc = nc_inq_varid(ncid, Literal.data.data(), &nc_varid); + if(rc != NC_NOERR) { + return make_error(); + } + auto eov = netcdf_encode::encode(from.template get(), ncid, nc_varid); + if(eov.is_error()){ + return eov; + } + } + + } + if constexpr ((i+1) < sizeof...(T)){ + auto eov = encode_member(from, ncid); + if(eov.is_error()){ + return eov; + } + } + + return void_t{}; + } + + static error_or encode(const data& from, int ncid){ + if constexpr (sizeof...(T) > 0){ + auto eov = encode_member<0>(from, ncid); + return eov; + } + + return void_t{}; + } + + template + static error_or encode_meta_member(const data& from, int ncid){ + using Type = typename parameter_pack_type::type; + constexpr string_literal Literal = parameter_key_pack_type::literal; + { + /** + * We have to ask for the internal id of the netcdf structure + */ + if constexpr (netcdf_is_group::value){ + return make_error(); + }else{ + int nc_varid{}; + int rc {}; + rc = nc_def_var(ncid, Literal.data.data(), netcdf_primitive_id::value, 0, nullptr, &nc_varid); + if(rc != NC_NOERR) { + return make_error(); + } + auto eov = netcdf_encode::encode_meta(from.template get(), ncid, nc_varid); + if(eov.is_error()){ + return eov; + } + } + + } + if constexpr ((i+1) < sizeof...(T)){ + auto eov = encode_meta_member(from, ncid); + if(eov.is_error()){ + return eov; + } + } + + return void_t{}; + } + + static error_or encode_meta(const data& from, int ncid){ + if constexpr (sizeof...(T) > 0){ + auto eov = encode_meta_member<0>(from, ncid); + return eov; + } + + return void_t{}; + } +}; + +template struct netcdf_decode; template @@ -160,24 +279,27 @@ struct netcdf_decode...>, ToDecode> { * We have to ask for the internal id of the netcdf structure */ if constexpr (netcdf_is_group::value){ - int nc_group_id{}; - int rc {}; - rc = nc_inq_ncid(from, Literal.data.data(), &nc_group_id); - if(rc != NC_NOERR) { - return make_error(); - } - auto eov = netcdf_decode::decode(to.template get(), nc_group_id); - if(eov.is_error()){ - return eov; - } + int nc_group_id{}; + int rc {}; + rc = nc_inq_ncid(from, Literal.data.data(), &nc_group_id); + if(rc != NC_NOERR) { + return make_error(); + } + auto eov = netcdf_decode::decode(to.template get(), nc_group_id); + if(eov.is_error()){ + return eov; + } }else{ - int nc_varid{}; - int rc {}; - rc = nc_inq_varid(from, Literal.data.data(), &nc_varid); - if(rc != NC_NOERR) { - return make_error(); - } - auto eov = netcdf_decode::decode(to.template get(), from, nc_varid); + int nc_varid{}; + int rc {}; + rc = nc_inq_varid(from, Literal.data.data(), &nc_varid); + if(rc != NC_NOERR) { + return make_error(); + } + auto eov = netcdf_decode::decode(to.template get(), from, nc_varid); + if(eov.is_error()){ + return eov; + } } } -- cgit v1.2.3