PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
Loading...
Searching...
No Matches
Archive.h
Go to the documentation of this file.
1
8
9#ifndef PBAT_IO_ARCHIVE_H
10#define PBAT_IO_ARCHIVE_H
11
12#include "PhysicsBasedAnimationToolkitExport.h"
13
14#include <filesystem>
15#include <highfive/H5File.hpp>
16#include <highfive/H5Group.hpp>
17#include <highfive/eigen.hpp>
18#include <optional>
19#include <string>
20#include <type_traits>
21#include <variant>
22
23namespace pbat::io {
24
29{
30 public:
37 PBAT_API Archive(
38 std::filesystem::path const& filepath,
39 HighFive::File::AccessMode flags = HighFive::File::OpenOrCreate);
44 PBAT_API bool IsUsable() const;
52 PBAT_API Archive GetOrCreateGroup(std::string const& path);
60 PBAT_API Archive operator[](std::string const& path);
67 PBAT_API Archive operator[](std::string const& path) const;
73 PBAT_API bool HasGroup(std::string const& path) const;
81 template <class T>
82 void WriteData(std::string const& path, T const& data);
90 template <class T>
91 void WriteMetaData(std::string const& key, T const& value);
99 template <class T>
100 T ReadData(std::string const& path) const;
108 template <class T>
109 T ReadMetaData(std::string const& key) const;
115 PBAT_API void Unlink(std::string const& path);
120 PBAT_API std::optional<std::string> GetPath() const;
121
122 protected:
123 using Object =
124 std::variant<std::monostate, HighFive::File, HighFive::Group>;
125
130 Archive(Object obj);
131
132 private:
133 Object mHdf5Object;
134};
135
136template <class T>
137inline void Archive::WriteData(std::string const& path, T const& data)
138{
139 std::visit(
140 [&](auto&& arg) {
141 using U = std::decay_t<decltype(arg)>;
142 if constexpr (not std::is_same_v<U, std::monostate>)
143 {
144 if (arg.exist(path) and (arg.getObjectType(path) == HighFive::ObjectType::Dataset))
145 {
146 arg.getDataSet(path).write(data);
147 }
148 else
149 {
150 arg.createDataSet(path, data);
151 }
152 }
153 },
154 mHdf5Object);
155}
156
157template <class T>
158inline void Archive::WriteMetaData(std::string const& key, T const& value)
159{
160 std::visit(
161 [&](auto&& arg) {
162 using U = std::decay_t<decltype(arg)>;
163 if constexpr (not std::is_same_v<U, std::monostate>)
164 {
165 if (arg.hasAttribute(key))
166 {
167 arg.getAttribute(key).write(value);
168 }
169 else
170 {
171 arg.createAttribute(key, value);
172 }
173 }
174 },
175 mHdf5Object);
176}
177
178template <class T>
179inline T Archive::ReadData(std::string const& path) const
180{
181 T data;
182 std::visit(
183 [&](auto&& arg) {
184 using U = std::decay_t<decltype(arg)>;
185 if constexpr (not std::is_same_v<U, std::monostate>)
186 {
187 data = arg.getDataSet(path).template read<T>();
188 }
189 },
190 mHdf5Object);
191 return std::move(data);
192}
193
194template <class T>
195inline T Archive::ReadMetaData(std::string const& key) const
196{
197 T data;
198 std::visit(
199 [&](auto&& arg) {
200 using U = std::decay_t<decltype(arg)>;
201 if constexpr (not std::is_same_v<U, std::monostate>)
202 {
203 data = arg.getAttribute(key).template read<T>();
204 }
205 },
206 mHdf5Object);
207 return data;
208}
209
210} // namespace pbat::io
211
212#endif // PBAT_IO_ARCHIVE_H
PBAT_API bool HasGroup(std::string const &path) const
Check if a group exists at the given path.
Definition Archive.cpp:86
PBAT_API Archive GetOrCreateGroup(std::string const &path)
Get or create a group at the given path.
Definition Archive.cpp:29
T ReadData(std::string const &path) const
Read data from the archive.
Definition Archive.h:179
PBAT_API void Unlink(std::string const &path)
Unlink a dataset or group from the archive.
Definition Archive.cpp:102
std::variant< std::monostate, HighFive::File, HighFive::Group > Object
HDF5 object type.
Definition Archive.h:123
PBAT_API bool IsUsable() const
Check if the archive is usable.
Definition Archive.cpp:14
void WriteMetaData(std::string const &key, T const &value)
Write metadata to the archive.
Definition Archive.h:158
PBAT_API Archive operator[](std::string const &path)
Get a group or create it if it does not exist.
Definition Archive.cpp:63
PBAT_API std::optional< std::string > GetPath() const
Get the path of the current HDF5 object.
Definition Archive.cpp:118
PBAT_API Archive(std::filesystem::path const &filepath, HighFive::File::AccessMode flags=HighFive::File::OpenOrCreate)
Construct a new Archive object from a filepath to an HDF5 file.
Definition Archive.cpp:9
void WriteData(std::string const &path, T const &data)
Write data to the archive.
Definition Archive.h:137
T ReadMetaData(std::string const &key) const
Read metadata from the archive.
Definition Archive.h:195
Namespace for I/O.
Definition Archive.cpp:7