summaryrefslogtreecommitdiff
path: root/modules/hdf5/src/cpp/H5BasicData.hxx
blob: 2d8c21902335025092ad854ffcbb22fc8d0567f5 (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
/*
 * 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 __H5BASICDATA_HXX__
#define __H5BASICDATA_HXX__

#include "H5Data.hxx"
#include "H5Object.hxx"
#include "H5DataConverter.hxx"

#define __SCILAB_STACK_CREATOR__(U,NAME) static void create(void * pvApiCtx, const int position, const int rows, const int cols, U * ptr, int * list, const int listPosition) \
    {                                                                   \
        SciErr err;                                                     \
        if (list)                                                       \
        {                                                               \
            if (rows == 0 || cols == 0)                                 \
            {                                                           \
                createMatrixOfDoubleInList(pvApiCtx, position, list, listPosition, 0, 0, 0); \
                return;                                                 \
            }                                                           \
            err = createMatrixOf##NAME##InList(pvApiCtx, position, list, listPosition, rows, cols, ptr); \
        }                                                               \
        else                                                            \
        {                                                               \
            if (rows == 0 || cols == 0)                                 \
            {                                                           \
                createEmptyMatrix(pvApiCtx, position);                  \
                return;                                                 \
            }                                                           \
            err = createMatrixOf##NAME(pvApiCtx, position, rows, cols, ptr); \
        }                                                               \
        if (err.iErr)                                                   \
        {                                                               \
            throw H5Exception(__LINE__, __FILE__, "Cannot allocate memory"); \
        }                                                               \
    }

#define __SCILAB_STACK_ALLOCATOR__(U,NAME) static void alloc(void * pvApiCtx, const int position, const int rows, const int cols, int * list, const int listPosition, U ** ptr) \
    {                                                                   \
        SciErr err;                                                     \
        if (list)                                                       \
        {                                                               \
            err = allocMatrixOf##NAME##InList(pvApiCtx, position, list, listPosition, rows, cols, ptr); \
        }                                                               \
        else                                                            \
        {                                                               \
            err = allocMatrixOf##NAME(pvApiCtx, position, rows, cols, ptr); \
        }                                                               \
        if (err.iErr)                                                   \
        {                                                               \
            throw H5Exception(__LINE__, __FILE__, "Cannot allocate memory"); \
        }                                                               \
    }

#define __SCILAB_ALLOCATORS_CREATORS__(U,NAME) __SCILAB_STACK_CREATOR__(U,NAME) \
    __SCILAB_STACK_ALLOCATOR__(U,NAME)


namespace org_modules_hdf5
{
template<typename T>
class H5BasicData : public H5Data
{

protected:

    T * transformedData;

public:

    H5BasicData(H5Object & _parent, const hsize_t _totalSize, const hsize_t _dataSize, const hsize_t _ndims, const hsize_t * _dims, T * _data, const hsize_t _stride, const size_t _offset, const bool _dataOwner) : H5Data(_parent, _totalSize, _dataSize, _ndims, _dims, static_cast<void *>(_data), _stride, _offset, _dataOwner), transformedData(0)
    {
        //std::cout << totalSize << ", " << stride << ", " << offset << std::endl;
    }

    virtual ~H5BasicData()
    {
        if (transformedData)
        {
            delete[] transformedData;
        }
    }

    virtual void printData(std::ostream & os, const unsigned int pos, const unsigned int indentLevel) const
    {
        os << static_cast<T *>(getData())[pos];
    }

    virtual void copyData(T * dest) const
    {
        if (dest)
        {
            if (stride == 0)
            {
                memcpy(static_cast<void *>(dest), data, totalSize * dataSize);
            }
            else if (transformedData)
            {
                memcpy(static_cast<void *>(dest), static_cast<void *>(transformedData), totalSize * dataSize);
            }
            else
            {
                char * cdata = static_cast<char *>(data) + offset;
                if (sizeof(T) == dataSize)
                {
                    for (int i = 0; i < totalSize; i++)
                    {
                        dest[i] = *((T *)cdata);
                        cdata += stride;
                    }
                }
                else
                {
                    char * _dest = reinterpret_cast<char *>(dest);
                    for (int i = 0; i < totalSize; i++)
                    {
                        memcpy(_dest, cdata, dataSize);
                        cdata += stride;
                        _dest += dataSize;
                    }
                }
            }
        }
        else
        {
            throw H5Exception(__LINE__, __FILE__, _("Cannot copy data to an empty pointer"));
        }
    }

    inline virtual void * getData() const
    {
        if (stride == 0)
        {
            return data;
        }
        else
        {
            if (!transformedData)
            {
                char * dest = new char[totalSize * dataSize];
                copyData(reinterpret_cast<T *>(dest));
                const_cast<H5BasicData *>(this)->transformedData = reinterpret_cast<T *>(dest);
            }

            return static_cast<void *>(transformedData);
        }
    }

    virtual void toScilab(void * pvApiCtx, const int lhsPosition, int * parentList = 0, const int listPosition = 0, const bool flip = true) const
    {
        T * newData = 0;
        hsize_t _ndims = ndims;
        hsize_t _totalSize = totalSize;
        hsize_t * _dims = const_cast<hsize_t *>(dims);

        if (_ndims == 0)
        {
            create(pvApiCtx, lhsPosition, 1, 1, static_cast<T *>(getData()), parentList, listPosition);
        }
        else if (_ndims == 1)
        {
            alloc(pvApiCtx, lhsPosition, 1, (int)*_dims, parentList, listPosition, &newData);
            copyData(newData);
        }
        else
        {
            if (_ndims == 2)
            {
                if (flip)
                {
                    alloc(pvApiCtx, lhsPosition, (int)_dims[1], (int)_dims[0], parentList, listPosition, &newData);
                }
                else
                {
                    alloc(pvApiCtx, lhsPosition, (int)_dims[0], (int)_dims[1], parentList, listPosition, &newData);
                }

                H5DataConverter::C2FHypermatrix(2, _dims, 0, static_cast<T *>(getData()), newData, flip);
            }
            else
            {
                int * list = getHypermatrix(pvApiCtx, lhsPosition, parentList, listPosition, flip);
                alloc(pvApiCtx, lhsPosition, (int)_totalSize, 1, list, 3, &newData);
                H5DataConverter::C2FHypermatrix((int)_ndims, _dims, _totalSize, static_cast<T *>(getData()), newData, flip);
            }
        }
    }

    virtual std::string dump(std::map<haddr_t, std::string> & alreadyVisited, const unsigned int indentLevel) const
    {
        return H5DataConverter::dump(alreadyVisited, indentLevel, (int)ndims, dims, *this);
    }

    static void putStringVectorOnStack(std::vector<std::string> & strs, const int rows, const int cols, const int pos, void * pvApiCtx)
    {
        if (rows * cols != strs.size())
        {
            throw H5Exception(__LINE__, __FILE__, _("Wrong dimensions."));
        }

        if (strs.size() == 0)
        {
            create(pvApiCtx, pos, 0, 0, "", 0, 0);
        }
        else
        {
            std::vector<const char *> _strs;
            _strs.reserve(strs.size());
            for (unsigned int i = 0; i < strs.size(); i++)
            {
                _strs.push_back(strs[i].c_str());
            }
            create(pvApiCtx, pos, rows, cols, const_cast<char **>(&(_strs[0])), 0, 0);
        }
    }

    __SCILAB_ALLOCATORS_CREATORS__(double, Double)
    __SCILAB_ALLOCATORS_CREATORS__(char, Integer8)
    __SCILAB_ALLOCATORS_CREATORS__(unsigned char, UnsignedInteger8)
    __SCILAB_ALLOCATORS_CREATORS__(short, Integer16)
    __SCILAB_ALLOCATORS_CREATORS__(unsigned short, UnsignedInteger16)
    __SCILAB_ALLOCATORS_CREATORS__(int, Integer32)
    __SCILAB_ALLOCATORS_CREATORS__(unsigned int, UnsignedInteger32)

#ifdef  _SCILAB_INT64__
    __SCILAB_ALLOCATORS_CREATORS__(long long, Integer64)
    __SCILAB_ALLOCATORS_CREATORS__(unsigned long long, UnsignedInteger64)
#endif

    __SCILAB_STACK_CREATOR__(char *, String)
    static void alloc(void * pvApiCtx, const int position, const int rows, const int cols, int * list, const int listPosition, char*** ptr) {}
};
}


#undef __SCILAB_STACK_CREATOR__
#undef __SCILAB_STACK_ALLOCATOR__
#undef __SCILAB_LIST_CREATOR__
#undef __SCILAB_LIST_ALLOCATOR__
#undef __SCILAB_ALLOCATORS_CREATORS__

#endif // __H5BASICDATA_HXX__