hdf5serie  2.0.0
HDF5 Serie
interface.h
1 /* Copyright (C) 2009 Markus Friedrich
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  *
17  * Contact:
18  * friedrich.at.gc@googlemail.com
19  *
20  */
21 
22 #ifndef _HDF5SERIE_INTERFACE_H_
23 #define _HDF5SERIE_INTERFACE_H_
24 
25 #include <fmatvec/atom.h>
26 #include <hdf5.h>
27 #include <map>
28 #include <set>
29 #include <utility>
30 #include <vector>
31 
32 namespace H5 {
33 
34  #define HDF5SERIE_MAXCTORPARAMETERS 5
35 
36  class Exception : public std::exception {
37  protected:
38  std::string path;
39  std::string msg;
40  mutable std::string whatMsg;
41  public:
42  explicit Exception(std::string path_, std::string msg_);
43  ~Exception() noexcept override;
44  const char* what() const noexcept override;
45  };
46 
47  class ScopedHID {
48  protected:
49  typedef herr_t (*CloseFunc)(hid_t id);
50  hid_t id{-1};
51  CloseFunc closeFunc{nullptr};
52  public:
53  ScopedHID() = default;
54  ScopedHID(hid_t id_, CloseFunc closeFunc_) : id(id_), closeFunc(closeFunc_) {
55  if(id<0)
56  throw Exception("<unknown>", "Calling the HDF5 function failed.");
57  if(!closeFunc)
58  throw Exception("<unknown>", "No close function defined.");
59  }
60  ~ScopedHID() {
61  reset();
62  }
63  operator hid_t() {
64  return id;
65  }
66  void reset() {
67  if(id>=0)
68  closeFunc(id);
69  id=-1;
70  closeFunc=nullptr;
71  }
72  void reset(hid_t id_, CloseFunc closeFunc_) {
73  reset();
74  if(id_<0)
75  throw Exception("<unknown>", "Calling the HDF5 function failed (during reset).");
76  if(!closeFunc_)
77  throw Exception("<unknown>", "No close function defined.");
78  id=id_;
79  closeFunc=closeFunc_;
80  }
81  };
82 
83  class File;
84  class GroupBase;
85  class Attribute;
86  class Object;
87  template<class Child, class Self> class Container;
88 
89  enum ElementType {
90  simpleDatasetScalar,
91  simpleDatasetVector,
92  simpleDatasetMatrix,
93  vectorSerie,
94  simpleAttributeScalar,
95  simpleAttributeVector,
96  simpleAttributeMatrix
97  };
98 
99  class Element : virtual public fmatvec::Atom {
100  friend class Container<Attribute, Object>;
101  friend class Container<Object, GroupBase>;
102  protected:
103  ScopedHID id;
104  std::string name;
105  Element(std::string name_);
106  ~Element() override;
107  virtual void close();
108  virtual void open();
109  virtual void refresh();
110  virtual void flush();
111  public:
113  hid_t getID() { return id; }
114  std::string getName() { return name; }
115  };
116 
117  // a container of objects of type Child which have itself a parent of type Self
118  template<class Child, class Self>
119  class Container {
120  protected:
121  Container() = default;
122  ~Container() {
123  for(auto it=childs.begin(); it!=childs.end(); ++it)
124  delete it->second;
125  }
126  void close() {
127  for(auto it=childs.begin(); it!=childs.end(); ++it)
128  it->second->close();
129  }
130  void open() {
131  for(auto it=childs.begin(); it!=childs.end(); ++it)
132  it->second->open();
133  }
134  void refresh() {
135  for(auto it=childs.begin(); it!=childs.end(); ++it)
136  it->second->refresh();
137  }
138  void flush() {
139  for(auto it=childs.begin(); it!=childs.end(); ++it)
140  it->second->flush();
141  }
142  std::map<std::string, Child*> childs;
143 
144  // create a objet of class T which is derived from Child
145  template<class T>
146  class Creator {
147  protected:
148  Self *self;
149  std::string name;
150  std::map<std::string, Child*> &childs;
151  public:
152  Creator(Self *self_, std::string name_, std::map<std::string, Child*> &childs_) :
153  self(self_), name(std::move(name_)), childs(childs_) {}
154 
155  template<typename... Args>
156  T* operator()(Args&&... args) {
157  auto ret=childs.insert(std::pair<std::string, Child*>(name, NULL));
158  if(!ret.second)
159  throw Exception(self->getPath(), "A element of name "+name+" already exists.");
160  try {
161  auto* r=new T(static_cast<Self*>(self), name, std::forward<Args>(args)...);
162  ret.first->second=r;
163  return r;
164  }
165  catch(...) {
166  childs.erase(name);
167  throw;
168  }
169  }
170  };
171  template<class T>
172  Creator<T> createChild(const std::string &name_) {
173  if(name_.find_first_of('/')!=std::string::npos)
174  throw Exception(static_cast<Self*>(this)->getPath(), "Internal error: must be a relative name, not absolute or a path");
175  return Creator<T>(static_cast<Self*>(this), name_, childs);
176  }
177 
178  template<class T>
179  T* openChild(const std::string &name_) {
180  if(name_.find_first_of('/')!=std::string::npos)
181  throw Exception(static_cast<Self*>(this)->getPath(), "Internal error: must be a relative name, not absolute or a path");
182  std::pair<typename std::map<std::string, Child*>::iterator, bool> ret=childs.insert(std::pair<std::string, Child*>(name_, NULL));
183  if(!ret.second) {
184  auto *o=dynamic_cast<T*>(ret.first->second);
185  if(!o)
186  std::runtime_error("The element "+name_+" if of other type.");
187  return o;
188  }
189  try {
190  auto* r=new T(0, static_cast<Self*>(this), name_);
191  ret.first->second=r;
192  return r;
193  }
194  catch(...) {
195  childs.erase(name_);
196  throw;
197  }
198  }
199  };
200 
201  class Object : public Element, public Container<Attribute, Object> {
202  friend class Container<Object, GroupBase>;
203  protected:
204  Object(GroupBase *parent_, const std::string &name_);
205  ~Object() override;
206  void close() override;
207  void open() override;
208  void refresh() override;
209  void flush() override;
210  GroupBase *parent;
211  File *file;
212  Object *getFileAsObject(); // helper function used in openChildAttribute
213  Object *getAttrParent(const std::string &path, size_t pos); // helper function used in openChildAttribute
214  public:
215  template<class T>
216  Creator<T> createChildAttribute(const std::string &path) {
217  if(path[0]=='/') // absolute path -> call openChildAttribute from file
218  return getFileAsObject()->createChildAttribute<T>(path.substr(1));
219  // now its a relative path
220  size_t pos;
221  if((pos=path.find_last_of('/'))==std::string::npos) // no / included -> call openChild from Container
222  return createChild<T>(path);
223  // now its a relative path including at least one /
224  return getAttrParent(path, pos)->createChild<T>(path.substr(pos+1));
225  }
226 
227  template<class T>
228  T* openChildAttribute(const std::string &path) {
229  if(path[0]=='/') // absolute path -> call openChildAttribute from file
230  return getFileAsObject()->openChildAttribute<T>(path.substr(1));
231  // now its a relative path
232  size_t pos;
233  if((pos=path.find_last_of('/'))==std::string::npos) // no / included -> call openChild from Container
234  return openChild<T>(path);
235  // now its a relative path including at least one /
236  return getAttrParent(path, pos)->openChild<T>(path.substr(pos+1));
237  }
238  Attribute *openChildAttribute(const std::string &name_, ElementType *attributeType=nullptr, hid_t *type=nullptr);
239  std::set<std::string> getChildAttributeNames();
240  bool hasChildAttribute(const std::string &name_);
241  GroupBase *getParent() { return parent; }
242  File *getFile() { return file; }
243  std::string getPath();
244  };
245 
246  class Attribute : public Element {
247  friend class Container<Attribute, Object>;
248  protected:
249  Attribute(Object *parent_, const std::string &name_);
250  ~Attribute() override;
251  Object *parent;
252  File *file;
253  void close() override;
254  void open() override;
255  void refresh() override;
256  void flush() override;
257  public:
258  Object *getParent() { return parent; }
259  File *getFile() { return file; }
260  std::string getPath();
261  };
262 
263  class Dataset : public Object {
264  protected:
265  Dataset(GroupBase *parent_, const std::string &name_);
266  Dataset(int dummy, GroupBase *parent_, const std::string &name_);
267  ~Dataset() override;
268  void close() override;
269  void open() override;
270  void refresh() override;
271  void flush() override;
272  public:
273  std::vector<hsize_t> getExtentDims();
274  };
275 
276 }
277 
278 #endif
Definition: interface.h:246
Definition: interface.h:36
Definition: interface.h:146
Definition: interface.h:201
Definition: interface.h:99
Definition: interface.h:47
Definition: group.h:30
Definition: interface.h:87
Definition: interface.h:263
hid_t getID()
Note: use the returned hid_t only temporarily since its value may change, at least when File::reopenA...
Definition: interface.h:113
Definition: file.h:46