diff options
Diffstat (limited to 'c++/codec-netcdf/netcdf.h')
-rw-r--r-- | c++/codec-netcdf/netcdf.h | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/c++/codec-netcdf/netcdf.h b/c++/codec-netcdf/netcdf.h new file mode 100644 index 0000000..d579b7f --- /dev/null +++ b/c++/codec-netcdf/netcdf.h @@ -0,0 +1,93 @@ +#pragma once + +#include <forstio/core/string_literal.h> +#include <forstio/core/error.h> +#include <forstio/codec/codec.h> + +#include <netcdf.h> + +namespace saw { +namespace encode { +/** + * Template type hint + */ +struct Netcdf {}; +} + +/** + * Class representing the files system netcdf file + */ +template<typename Schema> +class data<Schema, encode::Netcdf> { +private: + std::string path_; +public: + data(const std::string& path): + path_{path} + {} + + data(std::string&& path): + path_{std::move(path)} + {} + + std::string_view get_path_view() { + return path_; + } + + const std::string& get_path() const { + return path_; + } +}; + +template<typename Schema> +class codec<Schema, encode::Netcdf>{ + static_assert(always_false<Schema,encode::Netcdf>, "NetCDF only supports Structs as a root object"); +}; + +} +#include "netcdf.tmpl.h" +namespace saw { + +template<typename... Vals, string_literal... Keys> +class codec<schema::Struct<schema::Member<Vals,Keys>...>, encode::Netcdf> { +private: + using Schema = schema::Struct<schema::Member<Vals,Keys>...>; +public: + SAW_FORBID_COPY(codec); + SAW_DEFAULT_MOVE(codec); + + /** + * Default constructor + */ + codec() = default; + + /** + * Encoder function + */ + template<typename FromEncoding> + error_or<void> encode() { + return void_t{}; + } + + /** + * Decoder function + */ + template<typename ToEncoding> + error_or<void> decode(data<Schema, encode::Netcdf>& from_decode, data<Schema,ToEncoding>& to_decode) { + int ncid{}; + int rc{}; + + rc = nc_open(from_decode.get_path().c_str(), NC_NOWRITE, &ncid); + if(rc){ + // Don't know how to get the error, so fail critically. + return make_error<err::critical>(); + } + + auto eov = impl::netcdf_decode<Schema, ToEncoding>::decode(to_decode, ncid); + + nc_close(ncid); + + return eov; + } +}; +} |