summaryrefslogtreecommitdiff
path: root/modules/hdf5/src/cpp/H5Dataspace.hxx
blob: 90eeb532a8307eb6c8f80116253ffbe991e82726 (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
/*
 * 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
 *
 */

#ifndef __H5DATASPACE_HXX__
#define __H5DATASPACE_HXX__

#include "HDF5Objects.h"
#include "H5Object.hxx"

namespace org_modules_hdf5
{

class H5Dataspace : public H5Object
{
    hid_t space;

public:

    H5Dataspace(H5Object & _parent, hid_t _space);
    ~H5Dataspace();

    virtual hid_t getH5Id();
    virtual std::vector<unsigned int> getDims(const bool b) const;
    virtual std::string getTypeName() const;

    virtual std::string dump(std::map<haddr_t, std::string> & alreadyVisited, const unsigned int indentLevel) const;
    virtual std::string toString(const unsigned int indentLevel) const;
    virtual std::string getStringDims() const;

    virtual void getAccessibleAttribute(const std::string & name, const int pos, void * pvApiCtx) const;

    void select() const;

    virtual bool isDataspace() const
    {
        return true;
    }

    template <typename T>
    hsize_t * select(const unsigned int size, const T * start, const T * stride, const T * count, const T * block) const
    {
        return select(space, size, start, stride, count, block);
    }

    template <typename T>
    static hsize_t * select(const hid_t space, const unsigned int size, const T * start, const T * stride, const T * count, const T * block)
    {
        if (!start)
        {
            H5Sselect_all(space);
            return 0;
        }

        if (H5Sget_simple_extent_ndims(space) != size)
        {
            throw H5Exception(__LINE__, __FILE__, _("Invalid selection rank."));
        }

        herr_t err;
        hsize_t * hstart = new hsize_t[size];
        hsize_t * hstride = new hsize_t[size];
        hsize_t * hcount = new hsize_t[size];
        hsize_t * hblock = new hsize_t[size];
        hsize_t * dims = new hsize_t[size];

        for (unsigned int i = 0; i < size; i++)
        {
            hstart[i] = (hsize_t)start[i] - 1;
            hstride[i] = stride ? (hsize_t)stride[i] : 1;
            hblock[i] = block ? (hsize_t)block[i] : 1;
            hcount[i] = (hsize_t)count[i];
            dims[i] = hblock[i] * hcount[i];
        }

        err = H5Sselect_hyperslab(space, H5S_SELECT_SET, hstart, hstride, hcount, hblock);
        delete[] hstart;
        delete[] hstride;
        delete[] hcount;
        delete[] hblock;
        if (err < 0)
        {
            delete[] dims;
            throw H5Exception(__LINE__, __FILE__, _("Invalid selection."));
        }

        if (H5Sselect_valid(space) <= 0)
        {
            H5Sselect_all(space);
            delete[] dims;
            throw H5Exception(__LINE__, __FILE__, _("Invalid selection."));
        }

        return dims;
    }

    template <typename T>
    void select(const unsigned int size, const T * coords) const
    {
        herr_t err;
        hsize_t * hcoords = new hsize_t[size];
        H5S_seloper_t selop = H5S_SELECT_SET;

        err = H5Sselect_elements(space, selop, (size_t)size, hcoords);
        delete[] hcoords;
        if (err)
        {
            throw H5Exception(__LINE__, __FILE__, _("Invalid selection."));
        }

        if (H5Sselect_valid(space) <= 0)
        {
            H5Sselect_all(space);
            throw H5Exception(__LINE__, __FILE__, _("Invalid selection."));
        }
    }

    static H5Dataspace & createDataspace(H5Object & parent, const std::string & type);
    static H5Dataspace & createDataspace(H5Object & parent, const int rank, const hsize_t * dims, const hsize_t * maxdims);
};
}

#endif // __H5DATASPACE_HXX__