Debian Codegraph @

src/atlas/io/VectorAdaptor.h

raw
1 /*
2 * (C) Copyright 2020 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #pragma once
12
13 #include "atlas/util/vector.h"
14
15 #include "atlas-io.h"
16
17 namespace atlas {
18
19 //---------------------------------------------------------------------------------------------------------------------
20
21 template <typename T>
22 void encode_data(const atlas::vector<T>& v, atlas::io::Data& out) {
23 out.assign(v.data(), v.size() * sizeof(T));
24 }
25
26 //---------------------------------------------------------------------------------------------------------------------
27
28 template <typename T>
29 size_t encode_metadata(const atlas::vector<T>& v, atlas::io::Metadata& metadata) {
30 using atlas::io::ArrayMetadata;
31 using DataType = ArrayMetadata::DataType;
32 return encode_metadata(ArrayMetadata{DataType::create<T>(), {v.size()}}, metadata);
33 }
34
35 //---------------------------------------------------------------------------------------------------------------------
36
37 template <typename T>
38 void decode(const atlas::io::Metadata& m, const atlas::io::Data& encoded, atlas::vector<T>& out) {
39 atlas::io::ArrayMetadata array(m);
40 const T* data = static_cast<const T*>(encoded.data());
41 out.assign(data, data + array.size());
42 }
43
44 //---------------------------------------------------------------------------------------------------------------------
45
46 } // end namespace atlas