22#ifndef _HDF5SERIE_INTERFACE_H_
23#define _HDF5SERIE_INTERFACE_H_
25#include <fmatvec/atom.h>
34 #define HDF5SERIE_MAXCTORPARAMETERS 5
40 mutable std::string whatMsg;
42 explicit Exception(std::string path_, std::string msg_);
44 const char* what()
const noexcept override;
49 using CloseFunc = herr_t (*)(hid_t);
51 CloseFunc closeFunc{
nullptr};
54 ScopedHID(hid_t id_, CloseFunc closeFunc_) : id(id_), closeFunc(closeFunc_) {
56 throw Exception(
"<unknown>",
"Calling the HDF5 function failed.");
58 throw Exception(
"<unknown>",
"No close function defined.");
65 std::cerr<<
"Error in ScopedHID::dtor: "<<ex.what()<<std::endl;
74 throw Exception(
"<unknown>",
"Close function of ScopedHID failed.");
78 void reset(hid_t id_, CloseFunc closeFunc_) {
81 throw Exception(
"<unknown>",
"Calling the HDF5 function failed (during reset).");
83 throw Exception(
"<unknown>",
"No close function defined.");
89 inline void checkCall(herr_t err) {
91 throw Exception(
"<unknown>",
"A HDF5 call failed.");
98 template<
class Child,
class Self>
class Container;
105 simpleAttributeScalar,
106 simpleAttributeVector,
107 simpleAttributeMatrix
110 class Element :
virtual public fmatvec::Atom {
118 virtual void close();
119 virtual void refresh();
120 virtual void flush();
121 virtual void enableSWMR();
125 std::string getName() {
return name; }
129 template<
class Child,
class Self>
134 for(
auto it=childs.begin(); it!=childs.end(); ++it)
138 for(
auto it=childs.begin(); it!=childs.end(); ++it)
142 for(
auto it=childs.begin(); it!=childs.end(); ++it)
143 it->second->refresh();
146 for(
auto it=childs.begin(); it!=childs.end(); ++it)
150 if constexpr (std::is_same_v<Child, Attribute>) {
152 for(
auto it=childs.begin(); it!=childs.end(); ++it)
158 for(
auto it=childs.begin(); it!=childs.end(); ++it)
160 it->second->enableSWMR();
164 std::map<std::string, Child*> childs;
172 std::map<std::string, Child*> &childs;
174 Creator(Self *self_, std::string name_, std::map<std::string, Child*> &childs_) :
175 self(self_), name(std::move(name_)), childs(childs_) {}
177 template<
typename... Args>
178 T* operator()(Args&&... args) {
179 auto ret=childs.insert(std::pair<std::string, Child*>(name, NULL));
181 throw Exception(self->getPath(),
"A element of name "+name+
" already exists.");
183 auto* r=
new T(
static_cast<Self*
>(self), name, std::forward<Args>(args)...);
194 Creator<T> createChild(
const std::string &name_) {
195 if(name_.find_first_of(
'/')!=std::string::npos)
196 throw Exception(
static_cast<Self*
>(
this)->getPath(),
"Internal error: must be a relative name, not absolute or a path");
197 return Creator<T>(
static_cast<Self*
>(
this), name_, childs);
201 T* openChild(
const std::string &name_) {
202 if(name_.find_first_of(
'/')!=std::string::npos)
203 throw Exception(
static_cast<Self*
>(
this)->getPath(),
"Internal error: must be a relative name, not absolute or a path");
204 std::pair<typename std::map<std::string, Child*>::iterator,
bool> ret=childs.insert(std::pair<std::string, Child*>(name_, NULL));
206 auto *o=
dynamic_cast<T*
>(ret.first->second);
208 std::runtime_error(
"The element "+name_+
" if of other type.");
212 auto* r=
new T(0,
static_cast<Self*
>(
this), name_);
228 void close()
override;
229 void refresh()
override;
230 void flush()
override;
231 void enableSWMR()
override;
234 Object *getFileAsObject();
235 Object *getAttrParent(
const std::string &path,
size_t pos);
238 Creator<T> createChildAttribute(
const std::string &path) {
240 return getFileAsObject()->createChildAttribute<T>(path.substr(1));
243 if((pos=path.find_last_of(
'/'))==std::string::npos)
244 return createChild<T>(path);
246 return getAttrParent(path, pos)->createChild<T>(path.substr(pos+1));
250 T* openChildAttribute(
const std::string &path) {
252 return getFileAsObject()->openChildAttribute<T>(path.substr(1));
255 if((pos=path.find_last_of(
'/'))==std::string::npos)
256 return openChild<T>(path);
258 return getAttrParent(path, pos)->openChild<T>(path.substr(pos+1));
260 Attribute *openChildAttribute(
const std::string &name_, ElementType *attributeType=
nullptr, hid_t *type=
nullptr);
261 std::set<std::string> getChildAttributeNames();
262 bool hasChildAttribute(
const std::string &name_);
263 GroupBase *getParent() {
return parent; }
264 File *getFile() {
return file; }
265 std::string getPath();
275 void close()
override;
276 void refresh()
override;
277 void flush()
override;
279 Object *getParent() {
return parent; }
280 File *getFile() {
return file; }
281 std::string getPath();
289 void close()
override;
290 void refresh()
override;
291 void flush()
override;
292 void enableSWMR()
override;
294 std::vector<hsize_t> getExtentDims();
Definition: interface.h:268
Definition: interface.h:168
Definition: interface.h:130
Definition: interface.h:284
Definition: interface.h:110
hid_t getID()
Note: use the returned hid_t only temporarily since it may get invalid, at least when File::enableSWM...
Definition: interface.h:124
Definition: interface.h:36
Definition: interface.h:223
Definition: interface.h:47