22#ifndef _HDF5SERIE_INTERFACE_H_
23#define _HDF5SERIE_INTERFACE_H_
25#include <fmatvec/atom.h>
48 mutable std::string whatMsg;
50 explicit Exception(std::string path_, std::string msg_);
52 const char* what()
const noexcept override;
57 using CloseFunc = herr_t (*)(hid_t);
59 CloseFunc closeFunc{
nullptr};
62 ScopedHID(hid_t id_, CloseFunc closeFunc_) : id(id_), closeFunc(closeFunc_) {
64 throw Exception(
"<unknown>",
"Calling the HDF5 function failed.");
66 throw Exception(
"<unknown>",
"No close function defined.");
73 std::cerr<<
"Error in ScopedHID::dtor: "<<ex.what()<<std::endl;
82 throw Exception(
"<unknown>",
"Close function of ScopedHID failed.");
86 void reset(hid_t id_, CloseFunc closeFunc_) {
89 throw Exception(
"<unknown>",
"Calling the HDF5 function failed (during reset).");
91 throw Exception(
"<unknown>",
"No close function defined.");
97 inline void checkCall(herr_t err) {
99 throw Exception(
"<unknown>",
"A HDF5 call failed.");
106 template<
class Child,
class Self>
class Container;
113 simpleAttributeScalar,
114 simpleAttributeVector,
115 simpleAttributeMatrix
118 class Element :
virtual public fmatvec::Atom {
126 virtual void close();
127 virtual void refresh();
128 virtual void flush();
129 virtual void enableSWMR();
133 std::string getName() {
return name; }
137 template<
class Child,
class Self>
142 for(
auto it=childs.begin(); it!=childs.end(); ++it)
146 for(
auto it=childs.begin(); it!=childs.end(); ++it)
150 for(
auto it=childs.begin(); it!=childs.end(); ++it)
151 it->second->refresh();
154 for(
auto it=childs.begin(); it!=childs.end(); ++it)
158 if constexpr (std::is_same_v<Child, Attribute>) {
160 for(
auto it=childs.begin(); it!=childs.end(); ++it)
166 for(
auto it=childs.begin(); it!=childs.end(); ++it)
168 it->second->enableSWMR();
172 std::map<std::string, Child*> childs;
180 std::map<std::string, Child*> &childs;
182 Creator(Self *self_, std::string name_, std::map<std::string, Child*> &childs_) :
183 self(self_), name(std::move(name_)), childs(childs_) {}
185 template<
typename... Args>
186 T* operator()(Args&&... args) {
187 auto ret=childs.insert(std::pair<std::string, Child*>(name, NULL));
189 throw Exception(self->getPath(),
"A element of name "+name+
" already exists.");
191 auto* r=
new T(
static_cast<Self*
>(self), name, std::forward<Args>(args)...);
202 Creator<T> createChild(
const std::string &name_) {
203 if(name_.find_first_of(
'/')!=std::string::npos)
204 throw Exception(
static_cast<Self*
>(
this)->getPath(),
"Internal error: must be a relative name, not absolute or a path");
205 return Creator<T>(
static_cast<Self*
>(
this), name_, childs);
209 T* openChild(
const std::string &name_) {
210 if(name_.find_first_of(
'/')!=std::string::npos)
211 throw Exception(
static_cast<Self*
>(
this)->getPath(),
"Internal error: must be a relative name, not absolute or a path");
212 std::pair<typename std::map<std::string, Child*>::iterator,
bool> ret=childs.insert(std::pair<std::string, Child*>(name_, NULL));
214 auto *o=
dynamic_cast<T*
>(ret.first->second);
216 std::runtime_error(
"The element "+name_+
" if of other type.");
220 auto* r=
new T(0,
static_cast<Self*
>(
this), name_);
236 void close()
override;
237 void refresh()
override;
238 void flush()
override;
239 void enableSWMR()
override;
242 Object *getFileAsObject();
243 Object *getAttrParent(
const std::string &path,
size_t pos);
246 Creator<T> createChildAttribute(
const std::string &path) {
248 return getFileAsObject()->createChildAttribute<T>(path.substr(1));
251 if((pos=path.find_last_of(
'/'))==std::string::npos)
252 return createChild<T>(path);
254 return getAttrParent(path, pos)->createChild<T>(path.substr(pos+1));
258 T* openChildAttribute(
const std::string &path) {
260 return getFileAsObject()->openChildAttribute<T>(path.substr(1));
263 if((pos=path.find_last_of(
'/'))==std::string::npos)
264 return openChild<T>(path);
266 return getAttrParent(path, pos)->openChild<T>(path.substr(pos+1));
268 Attribute *openChildAttribute(
const std::string &name_, ElementType *attributeType=
nullptr, hid_t *type=
nullptr);
269 std::set<std::string> getChildAttributeNames();
270 bool hasChildAttribute(
const std::string &name_);
271 GroupBase *getParent() {
return parent; }
272 File *getFile() {
return file; }
273 std::string getPath();
283 void close()
override;
284 void refresh()
override;
285 void flush()
override;
287 Object *getParent() {
return parent; }
288 File *getFile() {
return file; }
289 std::string getPath();
297 void close()
override;
298 void refresh()
override;
299 void flush()
override;
300 void enableSWMR()
override;
302 std::vector<hsize_t> getExtentDims();
Definition: interface.h:276
Definition: interface.h:176
Definition: interface.h:138
Definition: interface.h:292
Definition: interface.h:118
hid_t getID()
Note: use the returned hid_t only temporarily since it may get invalid, at least when File::enableSWM...
Definition: interface.h:132
Definition: interface.h:44
Definition: interface.h:231
Definition: interface.h:55