88 * does it submit to any jurisdiction.
99 */
1010
11- // / @author Baudouin Raoult
12- // / @date Jun 2020
13-
1411#include " metkit/codes/CodesContent.h"
1512
1613#include " eccodes.h"
1916#include " eckit/io/DataHandle.h"
2017#include " eckit/io/MemoryHandle.h"
2118
22- #include " eckit/memory/Zero.h"
23-
2419#include " metkit/codes/GribHandle.h"
2520
2621namespace metkit {
2722namespace codes {
2823
2924// ----------------------------------------------------------------------------------------------------------------------
3025
31- CodesContent::CodesContent (codes_handle* handle, bool delete_handle) : handle_(handle), delete_handle_(delete_handle) {
32- ASSERT (handle_);
33- }
34-
35- CodesContent::CodesContent (const codes_handle* handle) : CodesContent(const_cast <codes_handle*>(handle), false ) {}
36-
37-
38- CodesContent::~CodesContent () {
39- if (delete_handle_) {
40- codes_handle_delete (handle_);
41- }
42- }
26+ CodesContent::CodesContent (std::unique_ptr<CodesHandle> handle) : handle_(std::move(handle)) {}
4327
4428
4529// ----------------------------------------------------------------------------------------------------------------------
4630
4731size_t CodesContent::length () const {
48- size_t size;
49- const void * data;
50- CODES_CALL (codes_get_message (handle_, &data, &size));
51- return size;
32+ return handle_->messageSize ();
5233}
5334
5435
5536// ----------------------------------------------------------------------------------------------------------------------
5637
5738void CodesContent::write (eckit::DataHandle& handle) const {
58- size_t size;
59- const void * data;
60- CODES_CALL (codes_get_message (handle_, &data, &size));
61- if (handle.write (data, size) != size) {
39+ auto data = handle_->messageData ();
40+ if (handle.write (data.data (), data.size ()) != data.size ()) {
6241 std::ostringstream oss;
6342 oss << " Write error to data handle " << handle;
6443 throw eckit::WriteError (oss.str (), Here ());
@@ -69,10 +48,8 @@ void CodesContent::write(eckit::DataHandle& handle) const {
6948// ----------------------------------------------------------------------------------------------------------------------
7049
7150eckit::DataHandle* CodesContent::readHandle () const {
72- size_t size;
73- const void * data;
74- CODES_CALL (codes_get_message (handle_, &data, &size));
75- return new eckit::MemoryHandle (data, size);
51+ auto data = handle_->messageData ();
52+ return new eckit::MemoryHandle (data.data (), data.size ());
7653}
7754
7855
@@ -86,128 +63,79 @@ void CodesContent::print(std::ostream& s) const {
8663// ----------------------------------------------------------------------------------------------------------------------
8764
8865std::string CodesContent::getString (const std::string& key) const {
89- char values[10240 ];
90- size_t len = sizeof (values);
91-
92- values[0 ] = 0 ;
93-
94- CODES_CALL (codes_get_string (handle_, key.c_str (), values, &len));
95- // ASSERT(err)
96-
97- return values;
66+ return handle_->getString (key);
9867}
9968
10069// ----------------------------------------------------------------------------------------------------------------------
10170
10271long CodesContent::getLong (const std::string& key) const {
103- long v = 0 ;
104- CODES_CALL (codes_get_long (handle_, key.c_str (), &v));
105- return v;
72+ return handle_->getLong (key);
10673}
10774
10875
10976// ----------------------------------------------------------------------------------------------------------------------
11077
11178double CodesContent::getDouble (const std::string& key) const {
112- double v = 0 ;
113- CODES_CALL (codes_get_double (handle_, key.c_str (), &v));
114- return v;
79+ return handle_->getDouble (key);
11580}
11681
11782
11883// ----------------------------------------------------------------------------------------------------------------------
11984
12085void CodesContent::getDoubleArray (const std::string& key, std::vector<double >& values) const {
121- size_t size = 0 ;
122- CODES_CALL (codes_get_size (handle_, key.c_str (), &size));
123-
124- size_t count = size;
125- values.resize (count);
126- CODES_CALL (codes_get_double_array (handle_, key.c_str (), &values[0 ], &count));
127- ASSERT (count == size);
86+ values = handle_->getDoubleArray (key);
12887}
12988
13089// ----------------------------------------------------------------------------------------------------------------------
13190
13291void CodesContent::getFloatArray (const std::string& key, std::vector<float >& values) const {
133- size_t size = 0 ;
134- CODES_CALL (codes_get_size (handle_, key.c_str (), &size));
135-
136- size_t count = size;
137- values.resize (count);
138- CODES_CALL (codes_get_float_array (handle_, key.c_str (), &values[0 ], &count));
139- ASSERT (count == size);
92+ values = handle_->getFloatArray (key);
14093}
14194
14295
14396// ----------------------------------------------------------------------------------------------------------------------
14497
14598size_t CodesContent::getSize (const std::string& key) const {
146- size_t size = 0 ;
147- CODES_CALL (codes_get_size (handle_, key.c_str (), &size));
148- return size;
99+ return handle_->size (key);
149100}
150101
151102
152103// ----------------------------------------------------------------------------------------------------------------------
153104
154105void CodesContent::getDoubleArray (const std::string& key, double * data, size_t len) const {
155- size_t count = len ;
156- CODES_CALL ( codes_get_double_array (handle_, key. c_str (), data, &count ));
157- ASSERT (count == len );
106+ auto arr = handle_-> getDoubleArray (key) ;
107+ ASSERT (len == arr. size ( ));
108+ std::copy (arr. begin (), arr. end (), data );
158109}
159110
160111// ----------------------------------------------------------------------------------------------------------------------
161112
162113void CodesContent::getFloatArray (const std::string& key, float * data, size_t len) const {
163- size_t count = len ;
164- CODES_CALL ( codes_get_float_array (handle_, key. c_str (), data, &count ));
165- ASSERT (count == len );
114+ auto arr = handle_-> getFloatArray (key) ;
115+ ASSERT (len == arr. size ( ));
116+ std::copy (arr. begin (), arr. end (), data );
166117}
167118
168119
169120// ----------------------------------------------------------------------------------------------------------------------
170121
171122void CodesContent::transform (const eckit::OrderedStringDict& dict) {
172-
173- std::vector<codes_values> values;
174- values.reserve (dict.size ());
175-
176- for (auto & kv : dict) {
177- codes_values v;
178- v.name = kv.first .c_str ();
179- v.string_value = kv.second .c_str ();
180- v.type = GRIB_TYPE_STRING;
181-
182- values.push_back (v);
123+ for (auto & [key, value] : dict) {
124+ handle_->set (key, value);
183125 }
184-
185- CODES_CALL (codes_set_values (handle_, values.data (), values.size ()));
186126}
187127
188128// ----------------------------------------------------------------------------------------------------------------------
189129
190130eckit::Offset CodesContent::offset () const {
191- long pos;
192- CODES_CALL (codes_get_long (handle_, " offset" , &pos));
193- return pos;
194- }
195-
196-
197- // ----------------------------------------------------------------------------------------------------------------------
198-
199- const codes_handle* CodesContent::codesHandle () const {
200- return handle_;
131+ return handle_->getLong (" offset" );
201132}
202133
203134
204135// ----------------------------------------------------------------------------------------------------------------------
205136
206137const void * CodesContent::data () const {
207- size_t size;
208- const void * data;
209- CODES_CALL (codes_get_message (handle_, &data, &size));
210- return data;
138+ return handle_->messageData ().data ();
211139}
212140
213141
0 commit comments