summaryrefslogtreecommitdiff
path: root/modules/hdf5/src/cpp/H5Object.hxx
blob: 6f0d39a9af4abee6a37a74eebe88c43c082ff337 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
 *
 * This file must be used under the terms of the CeCILL.
 * This source file is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at
 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
 *
 */

#ifdef _MSC_VER
#pragma warning(disable: 4355) //disable Warning C4355: 'this' : used in base member initializer list 
#endif

#ifndef __H5OBJECT_HXX__
#define __H5OBJECT_HXX__

#include "HDF5Objects.h"

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <limits>
#include <map>
#include <set>
#include <string>

extern "C"
{
#include "MALLOC.h"
#include "Scierror.h"
#include "api_scilab.h"
#include "localization.h"
}

#include "H5VariableScope.hxx"
#include "H5Exception.hxx"

#define __H5_INDENT_LENGTH__ 3
#define __H5_LS_LENGTH__ 25

namespace org_modules_hdf5
{
class H5AttributesList;
class H5File;

class H5Object
{
    static H5Object & root;

    bool locked;
    H5Object & parent;
    int scilabId;

    friend class H5AttributesList;
    friend class H5LinkList;
    friend class H5Dataset;

public :

    enum FilterType {HARD, SOFT, EXTERNAL, DANGLING, GROUP, DATASET, TYPE, ATTRIBUTE};

    H5Object(H5Object & _parent);
    H5Object(H5Object & _parent, const std::string & _name);
    virtual ~H5Object();

    virtual void cleanup();

    virtual hid_t getH5Id() const;
    virtual H5AttributesList & getAttributes();
    virtual hsize_t getAttributesNumber() const;

    virtual H5O_info_t getInfo() const
    {
        H5O_info_t info;
        H5Oget_info(getH5Id(), &info);

        return info;
    }

    virtual bool isFile() const
    {
        return false;
    }

    virtual bool isGroup() const
    {
        return false;
    }

    virtual bool isAttribute() const
    {
        return false;
    }

    virtual bool isReference() const
    {
        return false;
    }

    virtual bool isDataspace() const
    {
        return false;
    }

    virtual bool isDataset() const
    {
        return false;
    }

    virtual bool isType() const
    {
        return false;
    }

    virtual bool isList() const
    {
        return false;
    }

    virtual bool isCompound() const
    {
        return false;
    }

    virtual bool isArray() const
    {
        return false;
    }

    virtual bool isVlen() const
    {
        return false;
    }

    virtual bool mustDelete() const
    {
        return true;
    }

    virtual haddr_t getAddr() const
    {
        return getInfo().addr;
    }

    virtual const std::string & getName() const
    {
        return name;
    }

    virtual H5Object & getData(const unsigned int size, const unsigned int * index) const
    {
        throw H5Exception(__LINE__, __FILE__, _("Cannot retrieve numeric index."));
    }

    virtual H5Object & getData(const unsigned int size, const double * index) const
    {
        unsigned int * _index = new unsigned int[size];
        for (unsigned int i = 0; i < size; i++)
        {
            _index[i] = (unsigned int)(index[i] - 1);
        }

        try
        {
            return getData(size, _index);
        }
        catch (const H5Exception & /*e*/)
        {
            delete[] _index;
            throw;
        }
    }


    virtual const std::string getBaseName() const
    {
        std::string::size_type pos = name.find_last_of('/');
        if (pos == std::string::npos)
        {
            return name;
        }
        else
        {
            return name.substr(pos + 1);
        }
    }

    virtual std::string getCompletePath() const;
    virtual std::string dump(std::map<haddr_t, std::string> & alreadyVisited, const unsigned int indentLevel = 0) const
    {
        return "";
    }

    virtual std::string ls() const
    {
        return "";
    }

    virtual void ls(std::vector<std::string> & name, std::vector<std::string> & type) const
    {

    }

    virtual void ls(std::vector<std::string> & name, FilterType type) const
    {
        getNames(*this, name, type);
    }

    virtual void printLsInfo(std::ostringstream & os) const
    {
        return;
    }

    virtual std::string toString() const
    {
        return toString(0);
    }
    virtual std::string toString(const unsigned int indentLevel) const
    {
        return "";
    }
    virtual void getAccessibleAttribute(const std::string & _name, const int pos, void * pvApiCtx) const;
    virtual void getAccessibleAttribute(const double index, const int pos, void * pvApiCtx) const
    {
        throw H5Exception(__LINE__, __FILE__, _("Invalid operation"));
    }

    virtual void setAccessibleAttribute(const std::string & name, const int pos, void * pvApiCtx) const
    {
        throw H5Exception(__LINE__, __FILE__, _("Invalid operation"));
    }

    virtual void setAccessibleAttribute(const double index, const int pos, void * pvApiCtx) const
    {
        throw H5Exception(__LINE__, __FILE__, _("Invalid operation"));
    }

    void setScilabId(const int id)
    {
        scilabId = id;
    }

    const int getScilabId() const
    {
        return scilabId;
    }

    H5Object & getParent() const
    {
        return parent;
    }
    H5File & getFile() const;

    virtual void getNames(const H5Object & obj, std::vector<std::string> & names, FilterType type) const;
    virtual void createOnScilabStack(int pos, void * pvApiCtx) const;
    virtual void createInScilabList(int * list, int stackPos, int pos, void * pvApiCtx) const;

    virtual void toScilab(void * pvApiCtx, const int lhsPosition, int * parentList = 0, const int listPosition = 0, const bool flip = true) const
    {
        if (parentList)
        {
            createInScilabList(parentList, lhsPosition, listPosition, pvApiCtx);
        }
        else
        {
            createOnScilabStack(lhsPosition, pvApiCtx);
        }
    }

    bool isRoot() const
    {
        return this == &root;
    }

    void unregisterChild(H5Object * child)
    {
        if (!locked)
        {
            children.erase(child);
        }
    }

    static std::string getIndentString(const unsigned int indentLevel)
    {
        return std::string((size_t)(__H5_INDENT_LENGTH__ * indentLevel), ' ');
    }

    static H5Object & getRoot()
    {
        return root;
    }

    static void cleanAll()
    {
        root.locked = true;
        for (std::set<H5Object *>::iterator it = root.children.begin(); it != root.children.end(); it++)
        {
            delete *it;
        }
        root.children.clear();
        root.locked = false;
        H5VariableScope::clearScope();
    }

    static void getResizedString(std::string & str)
    {
        if (str.length() < __H5_LS_LENGTH__)
        {
            str.resize(__H5_LS_LENGTH__, ' ');
        }
    }

    static H5Object & getObject(H5Object & parent, hid_t obj);
    static H5Object & getObject(H5Object & parent, const std::string & name);
    static H5Object & getObject(H5Object & parent, const std::string & name, const bool isAttr);
    static void getLinksInfo(const H5Object & obj, std::vector<std::string> & linksName, std::vector<std::string> & types, std::vector<std::string> & linksType);

    inline static hsize_t * getCumProd(const hsize_t ndims, const hsize_t * dims)
    {
        hsize_t * ret = new hsize_t[ndims];
        ret[0] = 1;
        for (unsigned int i = 1; i < ndims; i++)
        {
            ret[i] *= ret[i - 1];
        }

        return ret;
    }

    inline static bool isEmptyPath(const std::string & path)
    {
        return path.empty() || path == ".";
    }

    inline static bool isEmptyPath(const char * path)
    {
        return path[0] == '\0' || (path[0] == '.' && path[1] == '\0');
    }

protected :

    class OpDataGetLs
    {
    public:
        H5Object * parent;
        std::vector<std::string> * name;
        std::vector<std::string> * type;

        OpDataGetLs(H5Object * _parent, std::vector<std::string> * _name, std::vector<std::string> * _type) : parent(_parent), name(_name), type(_type) { }
    };

    class OpDataCount
    {
    public:
        unsigned int soft;
        unsigned int external;
        unsigned int hard;
        unsigned int dangling;
        unsigned int group;
        unsigned int dataset;
        unsigned int type;
        const bool followLink;

        OpDataCount(const bool _followLink) : soft(0), external(0), hard(0), dangling(0), group(0), dataset(0), type(0), followLink(_followLink) { }
    };

    class OpDataFilter
    {
    public:
        std::vector<std::string> * name;
        FilterType type;
        const bool followLink;

        OpDataFilter(std::vector<std::string> * _name, FilterType _type, const bool _followLink) : name(_name), type(_type), followLink(_followLink) { }
    };

    class OpDataSoftLinkFilter
    {
    public:
        std::vector<std::string> * name;
        std::vector<std::string> * value;
        FilterType type;

        OpDataSoftLinkFilter(std::vector<std::string> * _name, std::vector<std::string> * _value, FilterType _type) : name(_name), value(_value), type(_type) { }
    };

    const std::string name;
    std::set<H5Object *> children;
    void registerChild(H5Object * child)
    {
        if (!locked)
        {
            children.insert(child);
        }
    }

    static void count(const H5Object & obj, OpDataCount & opdata);
    static herr_t countIterator(hid_t g_id, const char * name, const H5L_info_t * info, void * op_data);
    static herr_t filterAttributesIterator(hid_t location_id, const char * attr_name, const H5A_info_t * ainfo, void * op_data);
    static herr_t filterIterator(hid_t g_id, const char * name, const H5L_info_t * info, void * op_data);
    static herr_t filterSoftLinkIterator(hid_t g_id, const char * name, const H5L_info_t * info, void * op_data);
    static herr_t getLsAttributes(hid_t location_id, const char * attr_name, const H5A_info_t * ainfo, void * op_data);

private :

    H5Object() : parent(*this), locked(false), scilabId(-1) { }

    class LinksInfo
    {
    public:
        std::vector<std::string> * name;
        std::vector<std::string> * type;
        std::vector<std::string> * linkType;

        LinksInfo(std::vector<std::string> * _name, std::vector<std::string> * _type, std::vector<std::string> * _linkType) : name(_name), type(_type), linkType(_linkType) { }
    };

    static herr_t iterateGetInfo(hid_t g_id, const char * name, const H5L_info_t * info, void * op_data);
};
}

#undef __H5_INDENT_LENGTH__
#undef __H5_LS_LENGTH__

#endif // __H5OBJECT_HXX__