CNORXZ
Container with Native Operation Routines and Expressions
Loading...
Searching...
No Matches
h5_group.cc
Go to the documentation of this file.
1// -*- C++ -*-
12#include "h5_group.h"
13#include "h5_table.h"
14#include "h5_dataset.h"
15
16namespace CNORXZ
17{
18 namespace hdf5
19 {
23
25 {
26 this->close();
27 }
28
30 {
31 return ContentType::GROUP;
32 }
33
34 bool Group::ro() const
35 {
36 if(mParent){
37 return mParent->ro();
38 }
39 return false;
40 }
41
43 {
44 if(not isOpen()){
45 if(this->exists()){
46 mId = H5Gopen( mParent->id(), mName.c_str(), H5P_DEFAULT );
47 }
48 else {
49 mId = H5Gcreate( mParent->id(), mName.c_str(),
51 }
52 this->mkCont();
53 }
54 return *this;
55 }
56
58 {
59 if(mId != 0){
61 }
62 mId = 0;
63 return *this;
64 }
65
67 {
68 if(mParent){
69 return mParent->path() + "/" + this->name();
70 }
71 return this->name();
72 }
73
75 {
76 if(mParent){
77 return mParent->filename();
78 }
79 return String();
80 }
81
82 bool Group::exists() const
83 {
84 return H5Lexists(mParent->id(), mName.c_str(), H5P_DEFAULT) > 0;
85 }
86
87 const ContentPtr& Group::get(const String& name) const
88 {
89 const String delim = "/";
90 const SizeT delimpos = name.find(delim);
91 const String thisname = name.substr(0, delimpos);
92 if(delimpos != String::npos and delimpos+1 < name.size()){
93 const String next = name.substr(delimpos+1);
94 auto g = getGroup(thisname);
95 g->open();
96 return g->get(next);
97 }
98 auto i = this->getIndexTo(thisname);
99 return *i;
100 }
101
103 {
104 auto group = this->get(name);
106 "element '" << name << "' is not of type GROUP");
107 return std::dynamic_pointer_cast<Group>( group );
108 }
109
111 {
112 auto table = this->get(name);
114 "element '" << name << "' is not of type TABLE");
115 return std::dynamic_pointer_cast<Table>( table );
116 }
117
119 {
120 auto dset = this->get(name);
122 "element '" << name << "' is not of type DSET");
123 return std::dynamic_pointer_cast<Dataset>( dset );
124 }
125
127 {
128 CXZ_ASSERT(this->isOpen(), "tried to get content of closed group");
129 return mCont;
130 }
131
133 {
134 CXZ_ASSERT(this->isOpen(), "tried to extend closed group");
137 auto extr = URangeFactory<String>( nvec ).create();
139 auto ii = mCont.begin();
140 ii.at(dvec); // 'at' returns YIndex&, so cannot use it inline...
141 *ii = std::make_shared<Group>(name, this);
142 (*ii)->open(); // create new group
143 return *this;
144 }
145
147 struct InitContData
148 {
149 const ContentBase* parent;
150 BIndex<ContentPtr> index;
151 };
154 static herr_t addName(hid_t id, const char* name, const H5L_info_t* info, void* x)
155 {
156 Vector<String>* names = reinterpret_cast<Vector<String>*>(x);
157 names->push_back(String(name));
158 return 0;
159 }
160
161 static bool isTable(hid_t loc_id, const char* name)
162 {
163 const hid_t id = H5Dopen(loc_id, name, H5P_DEFAULT);
164 if(not H5Aexists(id, "CLASS")){
165 return false;
166 H5Dclose(id);
167 }
168 hid_t attrid = H5Aopen(id, "CLASS", H5P_DEFAULT);
169 const hid_t atype = H5Aget_type(attrid);
170 const SizeT asize = H5Tget_size(atype);
172 const herr_t ret = H5Aread(attrid, atype, buff.data());
175 H5Dclose(id);
176 if(ret != 0){
177 return false;
178 }
179 else {
180 return String(buff.data()) == "TABLE";
181 }
182 }
183
184 static herr_t initCont(hid_t id, const char* name, const H5L_info_t* info, void* x)
185 {
186 const String sname(name);
187 InitContData* icd = reinterpret_cast<InitContData*>(x);
188 BIndex<ContentPtr>& index = icd->index;
189 UIndex<String>& ui = std::dynamic_pointer_cast<XIndex<UIndex<String>,String>>
190 (index.pack()[0])->get();
191 ui.at(sname);
192 index();
194#if H5_VERS_MINOR > 10
196#else
198#endif
199 switch (oinfo.type) {
200 case H5O_TYPE_GROUP: {
201 *index = std::make_shared<Group>(sname, icd->parent);
202 break;
203 }
204 case H5O_TYPE_DATASET: {
205 if(isTable(id, name)){
206 *index = std::make_shared<Table>(sname, icd->parent);
207 }
208 else {
209 *index = std::make_shared<Dataset>(sname, icd->parent);
210 }
211 break;
212 }
213 default:
214 return 1;
215 }
216 return 0;
217 }
218
220 {
223 addName, reinterpret_cast<void*>(&names) );
224 mCont.init( URangeFactory<String>( names ).create() );
225
227 icd.index = mCont.begin();
228 icd.parent = this;
230 initCont, reinterpret_cast<void*>(&icd) );
231 }
232
234 {
235 CXZ_ASSERT(this->isOpen(), "tried to get content of closed group");
236 auto dvec = [](const String& n) { return Vector<DType>({DType(n)}); };
237 auto i = mCont.begin();
238 i.at(dvec(name));
239 return i;
240 }
241
243 {
244 CXZ_ASSERT(this->isOpen(), "tried to get content of closed group");
245 auto dvec = [](const String& n) { return Vector<DType>({DType(n)}); };
246 auto i = mCont.begin();
247 i.at(dvec(name));
248 return i;
249 }
250
251 }
252}
#define CXZ_ASSERT(statement, errmsg)
Definition assert.h:40
virtual iterator begin()
MArray & init(const RangePtr &range)
Definition marray.cc.h:39
MArray & extend(const RangePtr &range)
Definition marray.cc.h:47
const DPack & pack() const
Definition yrange.cc:375
YIndex & at(const Vector< DType > &meta)
Definition yrange.cc:345
virtual bool ro() const =0
const String & name() const
virtual String path() const =0
const ContentBase * mParent
virtual String filename() const =0
const MArray< ContentPtr > & get() const
Definition h5_group.cc:126
virtual String filename() const override
Definition h5_group.cc:74
Sptr< Group > getGroup(const String &name) const
Definition h5_group.cc:102
Sptr< Dataset > getDataset(const String &name) const
Definition h5_group.cc:118
MArray< ContentPtr > mCont
Definition h5_group.h:148
Sptr< Table > getTable(const String &name) const
Definition h5_group.cc:110
virtual Group & close() override
Definition h5_group.cc:57
Group & addGroup(const String &name)
Definition h5_group.cc:132
virtual Group & open() override
Definition h5_group.cc:42
virtual bool exists() const override
Definition h5_group.cc:82
virtual ContentType type() const override
Definition h5_group.cc:29
virtual bool ro() const override
Definition h5_group.cc:34
AIndex< ContentPtr > getIndexTo(const String &name) const
Definition h5_group.cc:233
virtual String path() const override
Definition h5_group.cc:66
Group(const String &gname, const ContentBase *_parent)
Definition h5_group.cc:20
Dataset declaration.
Group declaration.
Table declaration.
static bool isTable(hid_t loc_id, const char *name)
Definition h5_group.cc:161
static herr_t initCont(hid_t id, const char *name, const H5L_info_t *info, void *x)
Definition h5_group.cc:184
Sptr< ContentBase > ContentPtr
static herr_t addName(hid_t id, const char *name, const H5L_info_t *info, void *x)
Definition h5_group.cc:154
std::string String
Definition types.h:42
uint64_t SizeT
Definition types.h:38
std::vector< T, Allocator< T > > Vector
Definition types.h:310
Sptr< Range > rangeCast(const RangePtr r)
std::shared_ptr< T > Sptr
Definition types.h:48