TRIQS/h5 1.3.0
C++ interface to HDF5
Loading...
Searching...
No Matches
serialization.hpp
Go to the documentation of this file.
1// Copyright (c) 2019-2024 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Thomas Hahn, Olivier Parcollet, Nils Wentzell
16
22#ifndef LIBH5_SERIALIZATION_HPP
23#define LIBH5_SERIALIZATION_HPP
24
25#include "./file.hpp"
26#include "./generic.hpp"
27
28#include <cstddef>
29#include <vector>
30
31namespace h5 {
32
47 template <typename T>
48 [[nodiscard]] std::vector<std::byte> serialize(T const &x) {
49 file f{};
50 h5_write(f, "object", x);
51 return f.as_buffer();
52 }
53
63 template <typename T>
64 [[nodiscard]] T deserialize(std::vector<std::byte> const &buf) {
65 file f{buf};
66 return h5_read<T>(f, "object");
67 }
68
71} // namespace h5
72
73#endif // LIBH5_SERIALIZATION_HPP
A handle to an HDF5 file.
Definition file.hpp:43
Provides a handle to an HDF5 file.
Provides a generic interface for reading/writing data from/to various HDF5 objects.
T h5_read(group g, std::string const &key)
Generic implementation for reading from an HDF5 dataset/subgroup.
Definition generic.hpp:51
void h5_write(group g, std::string const &name, T const &x) H5_REQUIRES(std
Write a scalar to an HDF5 dataset.
Definition scalar.hpp:70
T deserialize(std::vector< std::byte > const &buf)
Deserialize an object from a byte buffer.
std::vector< std::byte > serialize(T const &x)
Serialize an object to a byte buffer.