22#ifndef _HDF5SERIE_INTERFACE_H_
23#define _HDF5SERIE_INTERFACE_H_
25#include <fmatvec/atom.h>
45 ErrorInfo(hid_t cls_id_, hid_t maj_num_, hid_t min_num_,
unsigned line_,
46 std::string func_name_, std::string file_name_, std::string desc_) :
51 func_name(std::move(func_name_)),
52 file_name(std::move(file_name_)),
53 desc (std::move(desc_))
59 std::string func_name;
60 std::string file_name;
62 bool operator==(
const ErrorInfo &b)
const;
69 mutable std::string whatMsg;
70 std::vector<ErrorInfo> errorStack;
72 explicit Exception(std::string path_, std::string msg_,
const std::vector<ErrorInfo> &errorStack_={});
74 const char* what()
const noexcept override;
75 const std::vector<ErrorInfo>& getErrorStack()
const {
return errorStack; }
80 using CloseFunc = herr_t (*)(hid_t);
82 CloseFunc closeFunc{
nullptr};
85 ScopedHID(hid_t id_, CloseFunc closeFunc_) : id(id_), closeFunc(closeFunc_) {
87 throw Exception({},
"A call of a HDF5 function failed.");
89 throw Exception({},
"Internal erorr: no close function defined in ScopedHID.");
94 closeFunc = src.closeFunc;
96 src.closeFunc =
nullptr;
102 closeFunc = src.closeFunc;
104 src.closeFunc =
nullptr;
112 std::cerr<<
"Error in ScopedHID::dtor: "<<ex.what()<<std::endl;
121 throw Exception({},
"Close function of ScopedHID failed.");
125 void reset(hid_t id_, CloseFunc closeFunc_) {
128 throw Exception({},
"A call to a HDF5 function failed.");
130 throw Exception({},
"No close function defined.");
132 closeFunc=closeFunc_;
136 inline void checkCall(herr_t err) {
138 throw Exception({},
"A call to a HDF5 function failed.");
145 template<
class Child,
class Self>
class Container;
152 simpleAttributeScalar,
153 simpleAttributeVector,
154 simpleAttributeMatrix
157 class Element :
virtual public fmatvec::Atom {
165 virtual void close();
166 virtual void refresh();
167 virtual void flush();
168 virtual void enableSWMR();
172 std::string getName() {
return name; }
176 template<
class Child,
class Self>
181 for(
auto it=childs.begin(); it!=childs.end(); ++it)
185 for(
auto it=childs.begin(); it!=childs.end(); ++it)
189 for(
auto it=childs.begin(); it!=childs.end(); ++it)
190 it->second->refresh();
193 for(
auto it=childs.begin(); it!=childs.end(); ++it)
197 if constexpr (std::is_same_v<Child, Attribute>) {
199 for(
auto it=childs.begin(); it!=childs.end(); ++it)
205 for(
auto it=childs.begin(); it!=childs.end(); ++it)
207 it->second->enableSWMR();
211 std::map<std::string, Child*> childs;
219 std::map<std::string, Child*> &childs;
221 Creator(Self *self_, std::string name_, std::map<std::string, Child*> &childs_) :
222 self(self_), name(std::move(name_)), childs(childs_) {}
224 template<
typename... Args>
225 T* operator()(Args&&... args) {
226 auto ret=childs.insert(std::pair<std::string, Child*>(name, NULL));
228 throw Exception(self->getPath(),
"A element of name "+name+
" already exists.");
230 auto* r=
new T(
static_cast<Self*
>(self), name, std::forward<Args>(args)...);
241 Creator<T> createChild(
const std::string &name_) {
242 if(name_.find_first_of(
'/')!=std::string::npos)
243 throw Exception(
static_cast<Self*
>(
this)->getPath(),
"Internal error: must be a relative name, not absolute or a path");
244 return Creator<T>(
static_cast<Self*
>(
this), name_, childs);
248 T* openChild(
const std::string &name_) {
249 if(name_.find_first_of(
'/')!=std::string::npos)
250 throw Exception(
static_cast<Self*
>(
this)->getPath(),
"Internal error: must be a relative name, not absolute or a path");
251 std::pair<typename std::map<std::string, Child*>::iterator,
bool> ret=childs.insert(std::pair<std::string, Child*>(name_, NULL));
253 auto *o=
dynamic_cast<T*
>(ret.first->second);
255 std::runtime_error(
"The element "+name_+
" if of other type.");
259 auto* r=
new T(0,
static_cast<Self*
>(
this), name_);
275 void close()
override;
276 void refresh()
override;
277 void flush()
override;
278 void enableSWMR()
override;
281 Object *getFileAsObject();
282 Object *getAttrParent(
const std::string &path,
size_t pos);
285 Creator<T> createChildAttribute(
const std::string &path) {
287 return getFileAsObject()->createChildAttribute<T>(path.substr(1));
290 if((pos=path.find_last_of(
'/'))==std::string::npos)
291 return createChild<T>(path);
293 return getAttrParent(path, pos)->createChild<T>(path.substr(pos+1));
297 T* openChildAttribute(
const std::string &path) {
299 return getFileAsObject()->openChildAttribute<T>(path.substr(1));
302 if((pos=path.find_last_of(
'/'))==std::string::npos)
303 return openChild<T>(path);
305 return getAttrParent(path, pos)->openChild<T>(path.substr(pos+1));
307 Attribute *openChildAttribute(
const std::string &name_, ElementType *attributeType=
nullptr,
ScopedHID *type=
nullptr);
308 std::set<std::string> getChildAttributeNames();
309 bool hasChildAttribute(
const std::string &name_);
310 GroupBase *getParent() {
return parent; }
311 File *getFile() {
return file; }
312 std::string getPath();
322 void close()
override;
323 void refresh()
override;
324 void flush()
override;
326 Object *getParent() {
return parent; }
327 File *getFile() {
return file; }
328 std::string getPath();
336 void close()
override;
337 void refresh()
override;
338 void enableSWMR()
override;
340 void flush()
override;
341 std::vector<hsize_t> getExtentDims();
Definition: interface.h:315
Definition: interface.h:215
Definition: interface.h:177
Definition: interface.h:331
Definition: interface.h:157
hid_t getID()
Note: use the returned hid_t only temporarily since it may get invalid, at least when File::enableSWM...
Definition: interface.h:171
Definition: interface.h:65
Definition: interface.h:270
Definition: interface.h:78
Definition: interface.h:44