summaryrefslogtreecommitdiff
path: root/c++/codec-netcdf/netcdf.h
blob: 72e76144705aa8676ef95eeb396ebef12565fe2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once

#include <forstio/core/string_literal.h>
#include <forstio/core/error.h>
#include <forstio/codec/data.h>

#include <netcdf.h>
#include <netcdf_mem.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::vector<uint8_t> buff_;
public:
		data(std::vector<uint8_t> buff):
				buff_{std::move(buff)}
		{}

		template<size_t N>
		data(const std::array<uint8_t, N>& arr):
			buff_{arr.begin(), arr.end()}
		{}

		std::vector<uint8_t>& get_data() {
				return buff_;
		}

		const std::vector<uint8_t>& get_data() const {
				return buff_;
		}
};

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 make_error<err::not_implemented>();
		}

		/**
		 * 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_mem("forstio_internal_memory", NC_NOWRITE, from_decode.get_data().size(), &from_decode.get_data()[0], &ncid);
				if(rc != NC_NOERR){
						// 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;
		}
};
}