All Classes Namespaces Functions Typedefs Enumerations Pages
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 <vector>
30 #include <boost/preprocessor/iteration/iterate.hpp>
31 
32 namespace H5 {
33 
34  #define HDF5SERIE_MAXCTORPARAMETERS 5
35 
36  class ScopedHID {
37  protected:
38  typedef herr_t (*CloseFunc)(hid_t id);
39  hid_t id;
40  CloseFunc closeFunc;
41  public:
42  ScopedHID() : id(-1), closeFunc(NULL) {}
43  ScopedHID(hid_t id_, CloseFunc closeFunc_) : id(id_), closeFunc(closeFunc_) {}
44  ~ScopedHID() {
45  reset();
46  }
47  operator hid_t() {
48  return id;
49  }
50  void reset(hid_t id_=-1, CloseFunc closeFunc_=NULL) {
51  if(id>=0)
52  closeFunc(id);
53  id=id_;
54  closeFunc=closeFunc_;
55  }
56  };
57 
58  class Exception : public std::exception {
59  protected:
60  std::string path;
61  std::string msg;
62  mutable std::string whatMsg;
63  public:
64  explicit Exception(const std::string &path_, const std::string &msg_);
65  ~Exception() throw();
66  const char* what() const throw();
67  };
68 
69  class File;
70  class GroupBase;
71  class Attribute;
72  class Object;
73  template<class Child, class Self> class Container;
74 
75  enum ElementType {
76  simpleDatasetScalar,
77  simpleDatasetVector,
78  simpleDatasetMatrix,
79  vectorSerie,
80  simpleAttributeScalar,
81  simpleAttributeVector,
82  simpleAttributeMatrix
83  };
84 
85  class Element : virtual public fmatvec::Atom {
86  friend class Container<Attribute, Object>;
87  friend class Container<Object, GroupBase>;
88  protected:
89  ScopedHID id;
90  std::string name;
91  Element(const std::string &name_);
92  virtual ~Element();
93  virtual void close();
94  virtual void open();
95  virtual void refresh();
96  virtual void flush();
97  public:
99  hid_t getID() { return id; }
100  std::string getName() { return name; }
101  };
102 
103  // a container of objects of type Child which have itself a parent of type Self
104  template<class Child, class Self>
105  class Container {
106  protected:
107  Container() {
108  }
109  ~Container() {
110  for(typename std::map<std::string, Child*>::iterator it=childs.begin(); it!=childs.end(); ++it)
111  delete it->second;
112  }
113  void close() {
114  for(typename std::map<std::string, Child*>::iterator it=childs.begin(); it!=childs.end(); ++it)
115  it->second->close();
116  }
117  void open() {
118  for(typename std::map<std::string, Child*>::iterator it=childs.begin(); it!=childs.end(); ++it)
119  it->second->open();
120  }
121  void refresh() {
122  for(typename std::map<std::string, Child*>::iterator it=childs.begin(); it!=childs.end(); ++it)
123  it->second->refresh();
124  }
125  void flush() {
126  for(typename std::map<std::string, Child*>::iterator it=childs.begin(); it!=childs.end(); ++it)
127  it->second->flush();
128  }
129  std::map<std::string, Child*> childs;
130 
131  // create a objet of class T which is derived from Child
132  template<class T>
133  class Creator {
134  protected:
135  Self *self;
136  std::string name;
137  std::map<std::string, Child*> &childs;
138  public:
139  Creator(Self *self_, const std::string &name_, std::map<std::string, Child*> &childs_) :
140  self(self_), name(name_), childs(childs_) {}
141 
142  // call iteration for function "T* operator()(P1 p1, P2 p2, ...)"
143  #define BOOST_PP_ITERATION_PARAMS_1 (3, (0, HDF5SERIE_MAXCTORPARAMETERS, "hdf5serie/interface_creatoroperator_iter.h"))
144  #include BOOST_PP_ITERATE()
145  };
146  template<class T>
147  Creator<T> createChild(const std::string &name_) {
148  if(name_.find_first_of('/')!=std::string::npos)
149  throw Exception(static_cast<Self*>(this)->getPath(), "Internal error: must be a relative name, not absolute or a path");
150  return Creator<T>(static_cast<Self*>(this), name_, childs);
151  }
152 
153  template<class T>
154  T* openChild(const std::string &name_) {
155  if(name_.find_first_of('/')!=std::string::npos)
156  throw Exception(static_cast<Self*>(this)->getPath(), "Internal error: must be a relative name, not absolute or a path");
157  std::pair<typename std::map<std::string, Child*>::iterator, bool> ret=childs.insert(std::pair<std::string, Child*>(name_, NULL));
158  if(!ret.second) {
159  T *o=dynamic_cast<T*>(ret.first->second);
160  if(!o)
161  std::runtime_error("The element "+name_+" if of other type.");
162  return o;
163  }
164  try {
165  T* r=new T(0, static_cast<Self*>(this), name_);
166  ret.first->second=r;
167  return r;
168  }
169  catch(...) {
170  childs.erase(name_);
171  throw;
172  }
173  }
174  };
175 
176  class Object : public Element, public Container<Attribute, Object> {
177  friend class Container<Object, GroupBase>;
178  protected:
179  Object(GroupBase *parent_, const std::string &name_);
180  ~Object();
181  void close();
182  void open();
183  void refresh();
184  void flush();
185  GroupBase *parent;
186  File *file;
187  Object *getFileAsObject(); // helper function used in openChildAttribute
188  Object *getAttrParent(const std::string &path, size_t pos); // helper function used in openChildAttribute
189  public:
190  template<class T>
191  Creator<T> createChildAttribute(const std::string &path) {
192  if(path[0]=='/') // absolute path -> call openChildAttribute from file
193  return getFileAsObject()->createChildAttribute<T>(path.substr(1));
194  // now its a relative path
195  size_t pos;
196  if((pos=path.find_last_of('/'))==std::string::npos) // no / included -> call openChild from Container
197  return createChild<T>(path);
198  // now its a relative path including at least one /
199  return getAttrParent(path, pos)->createChild<T>(path.substr(pos+1));
200  }
201 
202  template<class T>
203  T* openChildAttribute(const std::string &path) {
204  if(path[0]=='/') // absolute path -> call openChildAttribute from file
205  return getFileAsObject()->openChildAttribute<T>(path.substr(1));
206  // now its a relative path
207  size_t pos;
208  if((pos=path.find_last_of('/'))==std::string::npos) // no / included -> call openChild from Container
209  return openChild<T>(path);
210  // now its a relative path including at least one /
211  return getAttrParent(path, pos)->openChild<T>(path.substr(pos+1));
212  }
213  Attribute *openChildAttribute(const std::string &name_, ElementType *objectType=NULL, hid_t *type=NULL);
214  std::set<std::string> getChildAttributeNames();
215  bool hasChildAttribute(const std::string &name_);
216  GroupBase *getParent() { return parent; }
217  File *getFile() { return file; }
218  std::string getPath();
219  };
220 
221  class Attribute : public Element {
222  friend class Container<Attribute, Object>;
223  protected:
224  Attribute(Object *parent_, const std::string &name_);
225  ~Attribute();
226  Object *parent;
227  File *file;
228  void close();
229  void open();
230  void refresh();
231  void flush();
232  public:
233  Object *getParent() { return parent; }
234  File *getFile() { return file; }
235  std::string getPath();
236  };
237 
238  class Dataset : public Object {
239  protected:
240  Dataset(GroupBase *parent_, const std::string &name_);
241  Dataset(int dummy, GroupBase *parent_, const std::string &name_);
242  ~Dataset();
243  void close();
244  void open();
245  void refresh();
246  void flush();
247  public:
248  std::vector<hsize_t> getExtentDims();
249  };
250 
251 }
252 
253 #endif
Definition: interface.h:221
Definition: interface.h:58
Definition: interface.h:133
Definition: interface.h:176
Definition: interface.h:85
Definition: interface.h:36
Definition: group.h:30
Definition: interface.h:73
Definition: interface.h:238
hid_t getID()
Note: use the returned hid_t only temporarily since its value may change, at least when File::reopenA...
Definition: interface.h:99
Definition: file.h:47

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML