HDF5Serie

Project Page
/home/user/MBSimWindows/hdf5serie/hdf5serie/hdf5serie/group.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_GROUP_H_
00023 #define _HDF5SERIE_GROUP_H_
00024 
00025 #include <hdf5serie/interface.h>
00026 #include <boost/filesystem.hpp>
00027 
00028 namespace H5 {
00029 
00030   class GroupBase : public Object, public Container<Object, GroupBase> {
00031     protected:
00032       GroupBase(int dummy, GroupBase *parent_, const std::string &name_);
00033       GroupBase(GroupBase *parent_, const std::string &name_);
00034       ~GroupBase();
00035       void close();
00036       void open();
00037       void refresh();
00038       void flush();
00039       Dataset *openChildDataset(const std::string &name_, ElementType *objectType, hid_t *type);
00040       void handleExternalLink(const std::string &name_);
00041       GroupBase *getFileAsGroup();
00042     public:
00043       template<class T>
00044       Container<Object, GroupBase>::Creator<T> createChildObject(const std::string &path) {
00045         if(path[0]=='/') // absolute path -> call createChildObject from file
00046           return getFileAsGroup()->createChildObject<T>(path.substr(1));
00047         // now its a relative path
00048         size_t pos;
00049         if((pos=path.find_last_of('/'))==std::string::npos) // no / included -> call createChild from Container<Object, GroupBase>
00050           return Container<Object, GroupBase>::createChild<T>(path);
00051         // now its a relative path including at least one /
00052         GroupBase *group=dynamic_cast<GroupBase*>(openChildObject(path.substr(0, pos)));
00053         if(!group)
00054           throw std::runtime_error("Got a path (including /) but this object is not a group");
00055         return group->createChildObject<T>(path.substr(pos+1));
00056       }
00057 
00058       template<class T>
00059       T* openChildObject(const std::string &path) {
00060         handleExternalLink(path);
00061         if(path[0]=='/') // absolute path -> call openChildObject from file
00062           return getFileAsGroup()->openChildObject<T>(path.substr(1));
00063         // now its a relative path
00064         size_t pos;
00065         if((pos=path.find_first_of('/'))==std::string::npos) // no / included -> call openChild from Container<Object, GroupBase>
00066           return Container<Object, GroupBase>::openChild<T>(path);
00067         // now its a relative path including at least one /
00068         GroupBase *group=dynamic_cast<GroupBase*>(openChildObject(path.substr(0, pos)));
00069         if(!group)
00070           throw std::runtime_error("Got a path (including /) but this object is not a group");
00071         return group->openChildObject<T>(path.substr(pos+1));
00072       }
00073       Object *openChildObject(const std::string &name_, ElementType *objectType=NULL, hid_t *type=NULL);
00074       std::set<std::string> getChildObjectNames();
00075       bool hasChildObject(const std::string &name_);
00076 
00077       bool isExternalLink(const std::string &name_);
00078       std::pair<boost::filesystem::path, std::string> getExternalLink(const std::string &name_);
00079       void createExternalLink(const std::string &name_, const std::pair<boost::filesystem::path, std::string> &target);
00080       void createSoftLink(const std::string &name_, const std::string &target);
00081   };
00082 
00083   class Group : public GroupBase {
00084     friend class Container<Object, GroupBase>;
00085     protected:
00086       Group(int dummy, GroupBase *parent_, const std::string &name_);
00087       Group(GroupBase *parent_, const std::string &name_);
00088       ~Group();
00089       void close();
00090       void open();
00091       void refresh();
00092       void flush();
00093   };
00094 
00095 }
00096 
00097 #endif