summaryrefslogtreecommitdiffstats
path: root/c++/src/H5CompType.cpp
blob: c89fa5c8f280b52bd10019b57d5477dfd5123172 (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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <string>

#include "H5Include.h"
#include "H5Exception.h"
#include "H5IdComponent.h"
#include "H5PropList.h"
#include "H5OcreatProp.h"
#include "H5DcreatProp.h"
#include "H5DxferProp.h"
#include "H5LcreatProp.h"
#include "H5LaccProp.h"
#include "H5DaccProp.h"
#include "H5Location.h"
#include "H5Object.h"
#include "H5Alltypes.h"
#include "H5AbstractDs.h"
#include "H5DataSpace.h"
#include "H5DataSet.h"

namespace H5 {

//--------------------------------------------------------------------------
// Function:    CompType default constructor
///\brief       Default constructor: Creates a stub compound datatype
//--------------------------------------------------------------------------
CompType::CompType() : DataType()
{
}

//--------------------------------------------------------------------------
// Function:    CompType copy constructor
///\brief       Copy constructor: same HDF5 object as \a original
///\param       original - IN: Original CompType instance
//--------------------------------------------------------------------------
CompType::CompType(const CompType &original) : DataType(original)
{
}

//--------------------------------------------------------------------------
// Function:    CompType overloaded constructor
///\brief       Creates a CompType object using the id of an existing datatype.
///\param       existing_id - IN: Id of an existing compound datatype
//--------------------------------------------------------------------------
CompType::CompType(const hid_t existing_id) : DataType(existing_id)
{
}

//--------------------------------------------------------------------------
// Function:    CompType overloaded constructor
///\brief       Creates an empty compound datatype given a size, in bytes.
///\param       size - IN: Number of bytes in the datatype to create
///\exception   H5::DataTypeIException
// Description
//              The DataType constructor calls the C API H5Tcreate to create
//              the compound datatype.
//--------------------------------------------------------------------------
CompType::CompType(size_t size) : DataType(H5T_COMPOUND, size)
{
}

//--------------------------------------------------------------------------
// Function:    CompType overloaded constructor
///\brief       Gets the compound datatype of the specified dataset.
///\param       dataset - IN: Dataset that this enum datatype associates with
///\return      CompType instance
///\exception   H5::DataTypeIException
//--------------------------------------------------------------------------
CompType::CompType(const DataSet &dataset) : DataType()
{
    // Calls C function H5Dget_type to get the id of the datatype
    id = H5Dget_type(dataset.getId());

    // If the datatype id is invalid, throw exception
    if (id < 0) {
        throw DataSetIException("CompType constructor", "H5Dget_type failed");
    }
}

//--------------------------------------------------------------------------
// Function:    CompType overloaded constructor
///\brief       Creates an CompType instance by opening an HDF5 compound
///             given its name, provided as a C character string.
///\param       loc        - IN: Location of the type
///\param       dtype_name - IN: Compound type name
///\exception   H5::DataTypeIException
// Description
//              In 1.10.1, this constructor was introduced and may replace the
//              existing function CommonFG::openCompType(const char*) to
//              improve usability.
//              -BMR, Dec 2016
//--------------------------------------------------------------------------
CompType::CompType(const H5Location &loc, const char *dtype_name) : DataType()
{
    id = p_opentype(loc, dtype_name);
}

//--------------------------------------------------------------------------
// Function:    CompType overloaded constructor
///\brief       Creates an CompType instance by opening an HDF5 compound
///             datatype given its name, provided as an \c H5std_string.
///\param       loc        - IN: Location of the type
///\param       dtype_name - IN: Compound type name
///\exception   H5::DataTypeIException
// Description
//              In 1.10.1, this constructor was introduced and may replace the
//              existing function CommonFG::openCompType(const H5Location&)
//              to improve usability.
//              -BMR, Dec 2016
//--------------------------------------------------------------------------
CompType::CompType(const H5Location &loc, const H5std_string &dtype_name) : DataType()
{
    id = p_opentype(loc, dtype_name.c_str());
}

//--------------------------------------------------------------------------
// Function:    CompType::decode
///\brief       Returns a CompType object via DataType* by decoding the
///             binary object description of this datatype.
///
///\exception   H5::DataTypeIException
//--------------------------------------------------------------------------
DataType *
CompType::decode() const
{
    hid_t encoded_cmptype_id = H5I_INVALID_HID;
    try {
        encoded_cmptype_id = p_decode();
    }
    catch (DataTypeIException &err) {
        throw;
    }
    CompType *encoded_cmptype = new CompType;
    encoded_cmptype->p_setId(encoded_cmptype_id);
    return (encoded_cmptype);
}

//--------------------------------------------------------------------------
// Function:    CompType::getNmembers
///\brief       Returns the number of members in this compound datatype.
///\return      Number of members
///\exception   H5::DataTypeIException
//--------------------------------------------------------------------------
int
CompType::getNmembers() const
{
    int num_members = H5Tget_nmembers(id);
    if (num_members < 0) {
        throw DataTypeIException("CompType::getNmembers",
                                 "H5Tget_nmembers returns negative number of members");
    }
    return (num_members);
}

//--------------------------------------------------------------------------
// Function:    CompType::getMemberName
///\brief       Returns the name of a member in this compound datatype.
///\param       member_num - IN: Zero-based index of the member
///\return      Name of member
///\exception   H5::DataTypeIException
//--------------------------------------------------------------------------
H5std_string
CompType::getMemberName(unsigned member_num) const
{
    char *member_name_C = H5Tget_member_name(id, member_num);
    if (member_name_C == NULL) // NULL means failure
    {
        throw DataTypeIException("CompType::getMemberName",
                                 "H5Tget_member_name returns NULL for member name");
    }
    H5std_string member_name = H5std_string(member_name_C); // convert C string to string
    H5free_memory(member_name_C);                           // free the C string
    return (member_name);                                   // return the member name string
}

//--------------------------------------------------------------------------
// Function:    CompType::getMemberIndex
///\brief       Returns the index of a member in this compound datatype.
///\param       name - IN: Name of the member
///\return      Index of member
///\exception   H5::DataTypeIException
///\par Description
///             Members are stored in no particular order with numbers 0
///             through N-1, where N is the value returned by the member
///             function \c CompType::getNmembers.
//--------------------------------------------------------------------------
int
CompType::getMemberIndex(const char *name) const
{
    int member_index = H5Tget_member_index(id, name);
    if (member_index < 0) {
        throw DataTypeIException("CompType::getMemberIndex", "H5Tget_member_index returns negative value");
    }
    return (member_index);
}
int
CompType::getMemberIndex(const H5std_string &name) const
{
    return (getMemberIndex(name.c_str()));
}

//--------------------------------------------------------------------------
// Function:    CompType::getMemberOffset
///\brief       Returns the byte offset of the beginning of a member with
///             respect to the beginning of the compound data type datum.
///\param       member_num - IN: Zero-based index of the member
///\return      Byte offset
// Description
///             Members are stored in no particular order with numbers 0
///             through N-1, where N is the value returned by the member
///             function \c CompType::getNmembers.
//
//              Note that byte offset being returned as 0 doesn't indicate
//              a failure. (According to Quincey)
//--------------------------------------------------------------------------
size_t
CompType::getMemberOffset(unsigned member_num) const
{
    size_t offset = H5Tget_member_offset(id, member_num);
    return (offset);
}

//--------------------------------------------------------------------------
// Function:    CompType::getMemberClass
///\brief       Gets the type class of the specified member.
///\param       member_num - IN: Zero-based index of the member
///\return      Type class of the member
///\exception   H5::DataTypeIException
// Modification
//              Modified to use H5Tget_member_class instead. - Jul, 2005
//--------------------------------------------------------------------------
H5T_class_t
CompType::getMemberClass(unsigned member_num) const
{
    H5T_class_t member_class = H5Tget_member_class(id, member_num);
    if (member_class == H5T_NO_CLASS) {
        throw DataTypeIException("CompType::getMemberClass", "H5Tget_member_class returns H5T_NO_CLASS");
    }
    return (member_class);
}

// This private member function calls the C API to get the identifier
// of the specified member.  It provides the id to construct appropriate
// sub-types in the functions getMemberXxxType below, where Xxx indicates
// the sub-types.
hid_t
CompType::p_get_member_type(unsigned member_num) const
{
    // get the id of the specified member first
    hid_t member_type_id = H5Tget_member_type(id, member_num);
    if (member_type_id > 0)
        return (member_type_id);
    else {
        // p_get_member_type is private, caller will catch this exception
        // then throw another with appropriate API name
        throw DataTypeIException("", "H5Tget_member_type failed");
    }
}

//--------------------------------------------------------------------------
// Function:    CompType::getMemberDataType
///\brief       Returns the generic datatype of the specified member in this
///             compound datatype.
///\param       member_num - IN: Zero-based index of the member
///\return      DataType instance
///\exception   H5::DataTypeIException
//--------------------------------------------------------------------------
DataType
CompType::getMemberDataType(unsigned member_num) const
{
    try {
        DataType datatype;
        f_DataType_setId(&datatype, p_get_member_type(member_num));
        return (datatype);
    }
    catch (DataTypeIException &E) {
        throw DataTypeIException("CompType::getMemberDataType", E.getDetailMsg());
    }
}

//--------------------------------------------------------------------------
// Function:    CompType::getMemberArrayType
///\brief       Returns the array datatype of the specified member in this
///             compound datatype.
///\param       member_num - IN: Zero-based index of the member
///\return      ArrayType instance
///\exception   H5::DataTypeIException
//--------------------------------------------------------------------------
ArrayType
CompType::getMemberArrayType(unsigned member_num) const
{
    try {
        ArrayType arraytype;
        f_DataType_setId(&arraytype, p_get_member_type(member_num));
        return (arraytype);
    }
    catch (DataTypeIException &E) {
        throw DataTypeIException("CompType::getMemberArrayType", E.getDetailMsg());
    }
}

//--------------------------------------------------------------------------
// Function:    CompType::getMemberCompType
///\brief       Returns the compound datatype of the specified member in this
///             compound datatype.
///\param       member_num - IN: Zero-based index of the member
///\return      CompType instance
///\exception   H5::DataTypeIException
//--------------------------------------------------------------------------
CompType
CompType::getMemberCompType(unsigned member_num) const
{
    try {
        CompType comptype;
        f_DataType_setId(&comptype, p_get_member_type(member_num));
        return (comptype);
    }
    catch (DataTypeIException &E) {
        throw DataTypeIException("CompType::getMemberCompType", E.getDetailMsg());
    }
}

//--------------------------------------------------------------------------
// Function:    CompType::getMemberEnumType
///\brief       Returns the enumeration datatype of the specified member in
///             this compound datatype.
///\param       member_num - IN: Zero-based index of the member
///\return      EnumType instance
///\exception   H5::DataTypeIException
//--------------------------------------------------------------------------
EnumType
CompType::getMemberEnumType(unsigned member_num) const
{
    try {
        EnumType enumtype;
        f_DataType_setId(&enumtype, p_get_member_type(member_num));
        return (enumtype);
    }
    catch (DataTypeIException &E) {
        throw DataTypeIException("CompType::getMemberEnumType", E.getDetailMsg());
    }
}

//--------------------------------------------------------------------------
// Function:    CompType::getMemberIntType
///\brief       Returns the integer datatype of the specified member in this
///             compound datatype.
///\param       member_num - IN: Zero-based index of the member
///\return      IntType instance
///\exception   H5::DataTypeIException
//--------------------------------------------------------------------------
IntType
CompType::getMemberIntType(unsigned member_num) const
{
    try {
        IntType inttype;
        f_DataType_setId(&inttype, p_get_member_type(member_num));
        return (inttype);
    }
    catch (DataTypeIException &E) {
        throw DataTypeIException("CompType::getMemberIntType", E.getDetailMsg());
    }
}

//--------------------------------------------------------------------------
// Function:    CompType::getMemberFloatType
///\brief       Returns the floating-point datatype of the specified member
///             in this compound datatype.
///\param       member_num - IN: Zero-based index of the member
///\return      FloatType instance
///\exception   H5::DataTypeIException
//--------------------------------------------------------------------------
FloatType
CompType::getMemberFloatType(unsigned member_num) const
{
    try {
        FloatType floatype;
        f_DataType_setId(&floatype, p_get_member_type(member_num));
        return (floatype);
    }
    catch (DataTypeIException &E) {
        throw DataTypeIException("CompType::getMemberFloatType", E.getDetailMsg());
    }
}

//--------------------------------------------------------------------------
// Function:    CompType::getMemberStrType
///\brief       Returns the string datatype of the specified member in this
///             compound datatype.
///\param       member_num - IN: Zero-based index of the member
///\return      StrType instance
///\exception   H5::DataTypeIException
//--------------------------------------------------------------------------
StrType
CompType::getMemberStrType(unsigned member_num) const
{
    try {
        StrType strtype;
        f_DataType_setId(&strtype, p_get_member_type(member_num));
        return (strtype);
    }
    catch (DataTypeIException &E) {
        throw DataTypeIException("CompType::getMemberStrType", E.getDetailMsg());
    }
}

//--------------------------------------------------------------------------
// Function:    CompType::getMemberVarLenType
///\brief       Returns the variable length datatype of the specified member
///             in this compound datatype.
///\param       member_num - IN: Zero-based index of the member
///\return      VarLenType instance
///\exception   H5::DataTypeIException
//--------------------------------------------------------------------------
VarLenType
CompType::getMemberVarLenType(unsigned member_num) const
{
    try {
        VarLenType varlentype;
        f_DataType_setId(&varlentype, p_get_member_type(member_num));
        return (varlentype);
    }
    catch (DataTypeIException &E) {
        throw DataTypeIException("CompType::getMemberVarLenType", E.getDetailMsg());
    }
}

/* old style of getMemberType - using overloads; new style above
   returns the appropriate datatypes but has different named functions.
   In the old style, a datatype must be passed into the function.
   May, 2004: These should be reconsidered to provide more convenience.
// Returns the datatype of the specified member in this compound datatype.
// Several overloading of getMemberType are for different datatypes
void CompType::getMemberType(unsigned member_num, EnumType& enumtype) const
{
   p_get_member_type(member_num, enumtype);
}

void CompType::getMemberType(unsigned member_num, CompType& comptype) const
{
   p_get_member_type(member_num, comptype);
}

void CompType::getMemberType(unsigned member_num, IntType& inttype) const
{
   p_get_member_type(member_num, inttype);
}

void CompType::getMemberType(unsigned member_num, FloatType& floatype) const
{
   p_get_member_type(member_num, floatype);
}

void CompType::getMemberType(unsigned member_num, StrType& strtype) const
{
   p_get_member_type(member_num, strtype);
}
// end of overloading of getMemberType
*/

//--------------------------------------------------------------------------
// Function:    CompType::insertMember
///\brief       Inserts a new member to this compound datatype.
///\param       name - IN: Name of the new member
///\param       offset - IN: Offset in memory structure of the field to insert
///\param       new_member - IN: New member to be inserted
///\exception   H5::DataTypeIException
//--------------------------------------------------------------------------
void
CompType::insertMember(const H5std_string &name, size_t offset, const DataType &new_member) const
{
    // Convert string to C-string
    const char *name_C;
    name_C = name.c_str(); // name_C refers to the contents of name as a C-str

    hid_t new_member_id = new_member.getId(); // get new_member id for C API

    // Call C routine H5Tinsert to add the new member
    herr_t ret_value = H5Tinsert(id, name_C, offset, new_member_id);
    if (ret_value < 0) {
        throw DataTypeIException("CompType::insertMember", "H5Tinsert failed");
    }
}

//--------------------------------------------------------------------------
// Function:    CompType::pack
///\brief       Recursively removes padding from within a compound datatype.
///
///\exception   H5::DataTypeIException
//--------------------------------------------------------------------------
void
CompType::pack() const
{
    // Calls C routine H5Tpack to remove padding
    herr_t ret_value = H5Tpack(id);
    if (ret_value < 0) {
        throw DataTypeIException("CompType::pack", "H5Tpack failed");
    }
}

//--------------------------------------------------------------------------
// Function:    CompType::setSize
///\brief       Sets the total size for this compound datatype.
///\param       size - IN: Size to set
///\exception   H5::DataTypeIException
// Note
//      H5Tset_size works on atom datatypes and compound datatypes only
//--------------------------------------------------------------------------
void
CompType::setSize(size_t size) const
{
    // Call C routine H5Tset_size to set the total size
    herr_t ret_value = H5Tset_size(id, size);
    if (ret_value < 0) {
        throw DataTypeIException("CompType::setSize", "H5Tset_size failed");
    }
}

//--------------------------------------------------------------------------
// Function:    CompType destructor
///\brief       Properly terminates access to this compound datatype.
//--------------------------------------------------------------------------
CompType::~CompType()
{
}

} // namespace H5