00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _HDF5SERIE_INTERFACE_H_
00023 #define _HDF5SERIE_INTERFACE_H_
00024
00025 #include <fmatvec/atom.h>
00026 #include <hdf5.h>
00027 #include <map>
00028 #include <set>
00029 #include <vector>
00030 #include <boost/preprocessor/iteration/iterate.hpp>
00031
00032 namespace H5 {
00033
00034 #define HDF5SERIE_MAXCTORPARAMETERS 5
00035
00036 class ScopedHID {
00037 protected:
00038 typedef herr_t (*CloseFunc)(hid_t id);
00039 hid_t id;
00040 CloseFunc closeFunc;
00041 public:
00042 ScopedHID() : id(-1), closeFunc(NULL) {}
00043 ScopedHID(hid_t id_, CloseFunc closeFunc_) : id(id_), closeFunc(closeFunc_) {}
00044 ~ScopedHID() {
00045 reset();
00046 }
00047 operator hid_t() {
00048 return id;
00049 }
00050 void reset(hid_t id_=-1, CloseFunc closeFunc_=NULL) {
00051 if(id>=0)
00052 closeFunc(id);
00053 id=id_;
00054 closeFunc=closeFunc_;
00055 }
00056 };
00057
00058 class Exception : public std::exception {
00059 protected:
00060 std::string msg;
00061 public:
00062 explicit Exception(const std::string &msg_);
00063 ~Exception() throw();
00064 const char* what() const throw();
00065 };
00066
00067 class File;
00068 class GroupBase;
00069 class Attribute;
00070 class Object;
00071 template<class Child, class Self> class Container;
00072
00073 enum ElementType {
00074 simpleDatasetScalar,
00075 simpleDatasetVector,
00076 simpleDatasetMatrix,
00077 vectorSerie,
00078 simpleAttributeScalar,
00079 simpleAttributeVector,
00080 simpleAttributeMatrix
00081 };
00082
00083 class Element : virtual public fmatvec::Atom {
00084 friend class Container<Attribute, Object>;
00085 friend class Container<Object, GroupBase>;
00086 protected:
00087 ScopedHID id;
00088 std::string name;
00089 Element(const std::string &name_);
00090 virtual ~Element();
00091 virtual void close();
00092 virtual void open();
00093 virtual void refresh();
00094 virtual void flush();
00095 public:
00097 hid_t getID() { return id; }
00098 std::string getName() { return name; }
00099 };
00100
00101
00102 template<class Child, class Self>
00103 class Container {
00104 protected:
00105 Container() {
00106 }
00107 ~Container() {
00108 for(typename std::map<std::string, Child*>::iterator it=childs.begin(); it!=childs.end(); ++it)
00109 delete it->second;
00110 }
00111 void close() {
00112 for(typename std::map<std::string, Child*>::iterator it=childs.begin(); it!=childs.end(); ++it)
00113 it->second->close();
00114 }
00115 void open() {
00116 for(typename std::map<std::string, Child*>::iterator it=childs.begin(); it!=childs.end(); ++it)
00117 it->second->open();
00118 }
00119 void refresh() {
00120 for(typename std::map<std::string, Child*>::iterator it=childs.begin(); it!=childs.end(); ++it)
00121 it->second->refresh();
00122 }
00123 void flush() {
00124 for(typename std::map<std::string, Child*>::iterator it=childs.begin(); it!=childs.end(); ++it)
00125 it->second->flush();
00126 }
00127 std::map<std::string, Child*> childs;
00128
00129
00130 template<class T>
00131 class Creator {
00132 protected:
00133 Self *self;
00134 std::string name;
00135 std::map<std::string, Child*> &childs;
00136 public:
00137 Creator(Self *self_, const std::string &name_, std::map<std::string, Child*> &childs_) :
00138 self(self_), name(name_), childs(childs_) {}
00139
00140
00141 #define BOOST_PP_ITERATION_PARAMS_1 (3, (0, HDF5SERIE_MAXCTORPARAMETERS, "hdf5serie/interface_creatoroperator_iter.h"))
00142 #include BOOST_PP_ITERATE()
00143 };
00144 template<class T>
00145 Creator<T> createChild(const std::string &name_) {
00146 if(name_.find_first_of('/')!=std::string::npos)
00147 throw std::runtime_error("Internal error: must be a relative name, not absolute or a path");
00148 return Creator<T>(static_cast<Self*>(this), name_, childs);
00149 }
00150
00151 template<class T>
00152 T* openChild(const std::string &name_) {
00153 if(name_.find_first_of('/')!=std::string::npos)
00154 throw std::runtime_error("Internal error: must be a relative name, not absolute or a path");
00155 std::pair<typename std::map<std::string, Child*>::iterator, bool> ret=childs.insert(std::pair<std::string, Child*>(name_, NULL));
00156 if(!ret.second) {
00157 T *o=dynamic_cast<T*>(ret.first->second);
00158 if(!o)
00159 std::runtime_error("The element "+name_+" if of other type.");
00160 return o;
00161 }
00162 try {
00163 T* r=new T(0, static_cast<Self*>(this), name_);
00164 ret.first->second=r;
00165 return r;
00166 }
00167 catch(...) {
00168 childs.erase(name_);
00169 throw;
00170 }
00171 }
00172 };
00173
00174 class Object : public Element, public Container<Attribute, Object> {
00175 friend class Container<Object, GroupBase>;
00176 protected:
00177 Object(GroupBase *parent_, const std::string &name_);
00178 ~Object();
00179 void close();
00180 void open();
00181 void refresh();
00182 void flush();
00183 GroupBase *parent;
00184 File *file;
00185 Object *getFileAsObject();
00186 Object *getAttrParent(const std::string &path, size_t pos);
00187 public:
00188 template<class T>
00189 Creator<T> createChildAttribute(const std::string &path) {
00190 if(path[0]=='/')
00191 return getFileAsObject()->createChildAttribute<T>(path.substr(1));
00192
00193 size_t pos;
00194 if((pos=path.find_last_of('/'))==std::string::npos)
00195 return createChild<T>(path);
00196
00197 return getAttrParent(path, pos)->createChild<T>(path.substr(pos+1));
00198 }
00199
00200 template<class T>
00201 T* openChildAttribute(const std::string &path) {
00202 if(path[0]=='/')
00203 return getFileAsObject()->openChildAttribute<T>(path.substr(1));
00204
00205 size_t pos;
00206 if((pos=path.find_last_of('/'))==std::string::npos)
00207 return openChild<T>(path);
00208
00209 return getAttrParent(path, pos)->openChild<T>(path.substr(pos+1));
00210 }
00211 Attribute *openChildAttribute(const std::string &name_, ElementType *objectType=NULL, hid_t *type=NULL);
00212 std::set<std::string> getChildAttributeNames();
00213 bool hasChildAttribute(const std::string &name_);
00214 GroupBase *getParent() { return parent; }
00215 File *getFile() { return file; }
00216 };
00217
00218 class Attribute : public Element {
00219 friend class Container<Attribute, Object>;
00220 protected:
00221 Attribute(Object *parent_, const std::string &name_);
00222 ~Attribute();
00223 Object *parent;
00224 File *file;
00225 void close();
00226 void open();
00227 void refresh();
00228 void flush();
00229 public:
00230 Object *getParent() { return parent; }
00231 File *getFile() { return file; }
00232 };
00233
00234 class Dataset : public Object {
00235 protected:
00236 Dataset(GroupBase *parent_, const std::string &name_);
00237 Dataset(int dummy, GroupBase *parent_, const std::string &name_);
00238 ~Dataset();
00239 void close();
00240 void open();
00241 void refresh();
00242 void flush();
00243 public:
00244 std::vector<hsize_t> getExtentDims();
00245 };
00246
00247 }
00248
00249 #endif