HDF5Serie

Project Page
/home/user/MBSimWindows/hdf5serie/hdf5serie/hdf5serie/interface.h
00001 /* Copyright (C) 2009 Markus Friedrich
00002  *
00003  * This library is free software; you can redistribute it and/or
00004  * modify it under the terms of the GNU Lesser General Public
00005  * License as published by the Free Software Foundation; either
00006  * version 2.1 of the License, or (at your option) any later version.
00007  *
00008  * This library is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * Lesser General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public
00014  * License along with this library; if not, write to the Free Software
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
00016  *
00017  * Contact:
00018  *   friedrich.at.gc@googlemail.com
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 path;
00061       std::string msg;
00062       mutable std::string whatMsg;
00063     public:
00064       explicit Exception(const std::string &path_, const std::string &msg_);
00065       ~Exception() throw();
00066       const char* what() const throw();
00067   };
00068 
00069   class File;
00070   class GroupBase;
00071   class Attribute;
00072   class Object;
00073   template<class Child, class Self> class Container;
00074 
00075   enum ElementType {
00076     simpleDatasetScalar,
00077     simpleDatasetVector,
00078     simpleDatasetMatrix,
00079     vectorSerie,
00080     simpleAttributeScalar,
00081     simpleAttributeVector,
00082     simpleAttributeMatrix
00083   };
00084 
00085   class Element : virtual public fmatvec::Atom {
00086     friend class Container<Attribute, Object>;
00087     friend class Container<Object, GroupBase>;
00088     protected:
00089       ScopedHID id;
00090       std::string name;
00091       Element(const std::string &name_);
00092       virtual ~Element();
00093       virtual void close();
00094       virtual void open();
00095       virtual void refresh();
00096       virtual void flush();
00097     public:
00099       hid_t getID() { return id; }
00100       std::string getName() { return name; }
00101   };
00102 
00103   // a container of objects of type Child which have itself a parent of type Self
00104   template<class Child, class Self>
00105   class Container {
00106     protected:
00107       Container() {
00108       }
00109       ~Container() {
00110         for(typename std::map<std::string, Child*>::iterator it=childs.begin(); it!=childs.end(); ++it)
00111           delete it->second;
00112       }
00113       void close() {
00114         for(typename std::map<std::string, Child*>::iterator it=childs.begin(); it!=childs.end(); ++it)
00115           it->second->close();
00116       }
00117       void open() {
00118         for(typename std::map<std::string, Child*>::iterator it=childs.begin(); it!=childs.end(); ++it)
00119           it->second->open();
00120       }
00121       void refresh() {
00122         for(typename std::map<std::string, Child*>::iterator it=childs.begin(); it!=childs.end(); ++it)
00123           it->second->refresh();
00124       }
00125       void flush() {
00126         for(typename std::map<std::string, Child*>::iterator it=childs.begin(); it!=childs.end(); ++it)
00127           it->second->flush();
00128       }
00129       std::map<std::string, Child*> childs;
00130 
00131       // create a objet of class T which is derived from Child
00132       template<class T>
00133       class Creator {
00134         protected:
00135           Self *self;
00136           std::string name;
00137           std::map<std::string, Child*> &childs;
00138         public:
00139           Creator(Self *self_, const std::string &name_, std::map<std::string, Child*> &childs_) :
00140             self(self_), name(name_), childs(childs_) {}
00141 
00142           // call iteration for function "T* operator()(P1 p1, P2 p2, ...)"
00143           #define BOOST_PP_ITERATION_PARAMS_1 (3, (0, HDF5SERIE_MAXCTORPARAMETERS, "hdf5serie/interface_creatoroperator_iter.h"))
00144           #include BOOST_PP_ITERATE()
00145       };
00146       template<class T>
00147       Creator<T> createChild(const std::string &name_) {
00148         if(name_.find_first_of('/')!=std::string::npos)
00149           throw Exception(static_cast<Self*>(this)->getPath(), "Internal error: must be a relative name, not absolute or a path");
00150         return Creator<T>(static_cast<Self*>(this), name_, childs);
00151       }
00152 
00153       template<class T>
00154       T* openChild(const std::string &name_) {
00155         if(name_.find_first_of('/')!=std::string::npos)
00156           throw Exception(static_cast<Self*>(this)->getPath(), "Internal error: must be a relative name, not absolute or a path");
00157         std::pair<typename std::map<std::string, Child*>::iterator, bool> ret=childs.insert(std::pair<std::string, Child*>(name_, NULL));
00158         if(!ret.second) {
00159           T *o=dynamic_cast<T*>(ret.first->second);
00160           if(!o)
00161             std::runtime_error("The element "+name_+" if of other type.");
00162           return o;
00163         }
00164         try {
00165           T* r=new T(0, static_cast<Self*>(this), name_);
00166           ret.first->second=r;
00167           return r;
00168         }
00169         catch(...) {
00170           childs.erase(name_);
00171           throw;
00172         }
00173       }
00174   };
00175 
00176   class Object : public Element, public Container<Attribute, Object> {
00177     friend class Container<Object, GroupBase>;
00178     protected:
00179       Object(GroupBase *parent_, const std::string &name_);
00180       ~Object();
00181       void close();
00182       void open();
00183       void refresh();
00184       void flush();
00185       GroupBase *parent;
00186       File *file;
00187       Object *getFileAsObject(); // helper function used in openChildAttribute
00188       Object *getAttrParent(const std::string &path, size_t pos); // helper function used in openChildAttribute
00189     public:
00190       template<class T>
00191       Creator<T> createChildAttribute(const std::string &path) {
00192         if(path[0]=='/') // absolute path -> call openChildAttribute from file
00193           return getFileAsObject()->createChildAttribute<T>(path.substr(1));
00194         // now its a relative path
00195         size_t pos;
00196         if((pos=path.find_last_of('/'))==std::string::npos) // no / included -> call openChild from Container
00197           return createChild<T>(path);
00198         // now its a relative path including at least one /
00199         return getAttrParent(path, pos)->createChild<T>(path.substr(pos+1));
00200       }
00201 
00202       template<class T>
00203       T* openChildAttribute(const std::string &path) {
00204         if(path[0]=='/') // absolute path -> call openChildAttribute from file
00205           return getFileAsObject()->openChildAttribute<T>(path.substr(1));
00206         // now its a relative path
00207         size_t pos;
00208         if((pos=path.find_last_of('/'))==std::string::npos) // no / included -> call openChild from Container
00209           return openChild<T>(path);
00210         // now its a relative path including at least one /
00211         return getAttrParent(path, pos)->openChild<T>(path.substr(pos+1));
00212       }
00213       Attribute *openChildAttribute(const std::string &name_, ElementType *objectType=NULL, hid_t *type=NULL);
00214       std::set<std::string> getChildAttributeNames();
00215       bool hasChildAttribute(const std::string &name_);
00216       GroupBase *getParent() { return parent; }
00217       File *getFile() { return file; }
00218       std::string getPath();
00219   };
00220 
00221   class Attribute : public Element {
00222     friend class Container<Attribute, Object>;
00223     protected:
00224       Attribute(Object *parent_, const std::string &name_);
00225       ~Attribute();
00226       Object *parent;
00227       File *file;
00228       void close();
00229       void open();
00230       void refresh();
00231       void flush();
00232     public:
00233       Object *getParent() { return parent; }
00234       File *getFile() { return file; }
00235       std::string getPath();
00236   };
00237 
00238   class Dataset : public Object {
00239     protected:
00240       Dataset(GroupBase *parent_, const std::string &name_);
00241       Dataset(int dummy, GroupBase *parent_, const std::string &name_);
00242       ~Dataset();
00243       void close();
00244       void open();
00245       void refresh();
00246       void flush();
00247     public:
00248       std::vector<hsize_t> getExtentDims();
00249   };
00250 
00251 }
00252 
00253 #endif