/c++/src/H5CompType.cpp

https://gitlab.com/jorjpimm/hdf5 · C++ · 489 lines · 209 code · 31 blank · 249 comment · 11 complexity · 5d68d89d27b9706439b6acb7ccd41991 MD5 · raw file

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2. * Copyright by The HDF Group. *
  3. * Copyright by the Board of Trustees of the University of Illinois. *
  4. * All rights reserved. *
  5. * *
  6. * This file is part of HDF5. The full HDF5 copyright notice, including *
  7. * terms governing use, modification, and redistribution, is contained in *
  8. * the files COPYING and Copyright.html. COPYING can be found at the root *
  9. * of the source code distribution tree; Copyright.html can be found at the *
  10. * root level of an installed copy of the electronic HDF5 document set and *
  11. * is linked from the top-level documents page. It can also be found at *
  12. * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *
  13. * access to either file, you may request a copy from help@hdfgroup.org. *
  14. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  15. #include <string>
  16. #include "H5Include.h"
  17. #include "H5Exception.h"
  18. #include "H5IdComponent.h"
  19. #include "H5PropList.h"
  20. #include "H5Object.h"
  21. #include "H5DcreatProp.h"
  22. #include "H5CommonFG.h"
  23. #include "H5Alltypes.h"
  24. #include "H5AbstractDs.h"
  25. #include "H5DxferProp.h"
  26. #include "H5DataSpace.h"
  27. #include "H5DataSet.h"
  28. #include "H5private.h"
  29. #ifndef H5_NO_NAMESPACE
  30. namespace H5 {
  31. #endif
  32. //--------------------------------------------------------------------------
  33. // Function: CompType default constructor
  34. ///\brief Default constructor: Creates a stub compound datatype
  35. // Programmer Binh-Minh Ribler - 2000
  36. //--------------------------------------------------------------------------
  37. CompType::CompType() : DataType() {}
  38. //--------------------------------------------------------------------------
  39. // Function: CompType copy constructor
  40. ///\brief Copy constructor: makes copy of the original CompType object
  41. ///\param original - IN: Original CompType instance
  42. // Programmer Binh-Minh Ribler - 2000
  43. //--------------------------------------------------------------------------
  44. CompType::CompType( const CompType& original ) : DataType( original ) {}
  45. //--------------------------------------------------------------------------
  46. // Function: CompType overloaded constructor
  47. ///\brief Creates a CompType object using the id of an existing datatype.
  48. ///\param existing_id - IN: Id of an existing compound datatype
  49. // Programmer Binh-Minh Ribler - 2000
  50. //--------------------------------------------------------------------------
  51. CompType::CompType( const hid_t existing_id ) : DataType( existing_id ) {}
  52. //--------------------------------------------------------------------------
  53. // Function: CompType overloaded constructor
  54. ///\brief Creates an empty compound datatype given a size, in bytes.
  55. ///\param size - IN: Number of bytes in the datatype to create
  56. ///\exception H5::DataTypeIException
  57. // Description
  58. // The DataType constructor calls the C API H5Tcreate to create
  59. // the compound datatype.
  60. // Programmer Binh-Minh Ribler - 2000
  61. //--------------------------------------------------------------------------
  62. CompType::CompType( size_t size ) : DataType( H5T_COMPOUND, size ) {}
  63. //--------------------------------------------------------------------------
  64. // Function: CompType overloaded constructor
  65. ///\brief Gets the compound datatype of the specified dataset.
  66. ///\param dataset - IN: Dataset that this enum datatype associates with
  67. ///\return CompType instance
  68. ///\exception H5::DataTypeIException
  69. // Programmer Binh-Minh Ribler - 2000
  70. //--------------------------------------------------------------------------
  71. CompType::CompType( const DataSet& dataset ) : DataType()
  72. {
  73. // Calls C function H5Dget_type to get the id of the datatype
  74. id = H5Dget_type( dataset.getId() );
  75. // If the datatype id is invalid, throw exception
  76. if( id < 0 )
  77. {
  78. throw DataSetIException("CompType constructor", "H5Dget_type failed");
  79. }
  80. }
  81. //--------------------------------------------------------------------------
  82. // Function: CompType::getNmembers
  83. ///\brief Returns the number of members in this compound datatype.
  84. ///\return Number of members
  85. ///\exception H5::DataTypeIException
  86. // Programmer Binh-Minh Ribler - 2000
  87. //--------------------------------------------------------------------------
  88. int CompType::getNmembers() const
  89. {
  90. int num_members = H5Tget_nmembers( id );
  91. if( num_members < 0 )
  92. {
  93. throw DataTypeIException("CompType::getNmembers",
  94. "H5Tget_nmembers returns negative number of members");
  95. }
  96. return( num_members );
  97. }
  98. //--------------------------------------------------------------------------
  99. // Function: CompType::getMemberName
  100. ///\brief Returns the name of a member in this compound datatype.
  101. ///\param member_num - IN: Zero-based index of the member
  102. ///\return Name of member
  103. ///\exception H5::DataTypeIException
  104. // Programmer Binh-Minh Ribler - 2000
  105. //--------------------------------------------------------------------------
  106. H5std_string CompType::getMemberName( unsigned member_num ) const
  107. {
  108. char* member_name_C = H5Tget_member_name( id, member_num );
  109. if( member_name_C == NULL ) // NULL means failure
  110. {
  111. throw DataTypeIException("CompType::getMemberName",
  112. "H5Tget_member_name returns NULL for member name");
  113. }
  114. H5std_string member_name = H5std_string(member_name_C); // convert C string to string
  115. H5free_memory(member_name_C); // free the C string
  116. return( member_name ); // return the member name string
  117. }
  118. //--------------------------------------------------------------------------
  119. // Function: CompType::getMemberIndex
  120. ///\brief Returns the index of a member in this compound datatype.
  121. ///\param name - IN: Name of the member
  122. ///\return Index of member
  123. ///\exception H5::DataTypeIException
  124. ///\par Description
  125. /// Members are stored in no particular order with numbers 0
  126. /// through N-1, where N is the value returned by the member
  127. /// function \c CompType::getNmembers.
  128. // Programmer Binh-Minh Ribler - May 16, 2002
  129. //--------------------------------------------------------------------------
  130. int CompType::getMemberIndex(const char* name) const
  131. {
  132. int member_index = H5Tget_member_index(id, name);
  133. if( member_index < 0 )
  134. {
  135. throw DataTypeIException("CompType::getMemberIndex",
  136. "H5Tget_member_index returns negative value");
  137. }
  138. return( member_index );
  139. }
  140. int CompType::getMemberIndex(const H5std_string& name) const
  141. {
  142. return(getMemberIndex(name.c_str()));
  143. }
  144. //--------------------------------------------------------------------------
  145. // Function: CompType::getMemberOffset
  146. ///\brief Returns the byte offset of the beginning of a member with
  147. /// respect to the beginning of the compound data type datum.
  148. ///\param member_num - IN: Zero-based index of the member
  149. ///\return Byte offset
  150. ///\exception H5::DataTypeIException
  151. // Programmer Binh-Minh Ribler - 2000
  152. // Description
  153. /// Members are stored in no particular order with numbers 0
  154. /// through N-1, where N is the value returned by the member
  155. /// function \c CompType::getNmembers.
  156. //
  157. // Note that byte offset being returned as 0 doesn't indicate
  158. // a failure. (According to Quincey)
  159. //--------------------------------------------------------------------------
  160. size_t CompType::getMemberOffset( unsigned member_num ) const
  161. {
  162. size_t offset = H5Tget_member_offset( id, member_num );
  163. return( offset );
  164. }
  165. //--------------------------------------------------------------------------
  166. // Function: CompType::getMemberClass
  167. ///\brief Gets the type class of the specified member.
  168. ///\param member_num - IN: Zero-based index of the member
  169. ///\return Type class of the member
  170. ///\exception H5::DataTypeIException
  171. // Programmer Binh-Minh Ribler - 2000
  172. // Modification
  173. // Modified to use H5Tget_member_class instead. - Jul, 2005
  174. //--------------------------------------------------------------------------
  175. H5T_class_t CompType::getMemberClass( unsigned member_num ) const
  176. {
  177. H5T_class_t member_class = H5Tget_member_class(id, member_num);
  178. if( member_class == H5T_NO_CLASS )
  179. {
  180. throw DataTypeIException("CompType::getMemberClass",
  181. "H5Tget_member_class returns H5T_NO_CLASS");
  182. }
  183. return(member_class);
  184. }
  185. // This private member function calls the C API to get the identifier
  186. // of the specified member. It provides the id to construct appropriate
  187. // sub-types in the functions getMemberXxxType below, where Xxx indicates
  188. // the sub-types.
  189. hid_t CompType::p_get_member_type(unsigned member_num) const
  190. {
  191. // get the id of the specified member first
  192. hid_t member_type_id = H5Tget_member_type( id, member_num );
  193. if( member_type_id > 0 )
  194. return( member_type_id );
  195. else
  196. {
  197. // p_get_member_type is private, caller will catch this exception
  198. // then throw another with appropriate API name
  199. throw DataTypeIException("", "H5Tget_member_type failed");
  200. }
  201. }
  202. //--------------------------------------------------------------------------
  203. // Function: CompType::getMemberDataType
  204. ///\brief Returns the generic datatype of the specified member in this
  205. /// compound datatype.
  206. ///\param member_num - IN: Zero-based index of the member
  207. ///\return DataType instance
  208. ///\exception H5::DataTypeIException
  209. // Programmer Binh-Minh Ribler - 2000
  210. //--------------------------------------------------------------------------
  211. DataType CompType::getMemberDataType( unsigned member_num ) const
  212. {
  213. try {
  214. DataType datatype;
  215. f_DataType_setId(&datatype, p_get_member_type(member_num));
  216. return(datatype);
  217. }
  218. catch (DataTypeIException E) {
  219. throw DataTypeIException("CompType::getMemberDataType", E.getDetailMsg());
  220. }
  221. }
  222. //--------------------------------------------------------------------------
  223. // Function: CompType::getMemberArrayType
  224. ///\brief Returns the array datatype of the specified member in this
  225. /// compound datatype.
  226. ///\param member_num - IN: Zero-based index of the member
  227. ///\return ArrayType instance
  228. ///\exception H5::DataTypeIException
  229. // Programmer Binh-Minh Ribler - Jul, 2005
  230. //--------------------------------------------------------------------------
  231. ArrayType CompType::getMemberArrayType( unsigned member_num ) const
  232. {
  233. try {
  234. ArrayType arraytype(p_get_member_type(member_num));
  235. f_DataType_setId(&arraytype, p_get_member_type(member_num));
  236. return(arraytype);
  237. }
  238. catch (DataTypeIException E) {
  239. throw DataTypeIException("CompType::getMemberArrayType", E.getDetailMsg());
  240. }
  241. }
  242. //--------------------------------------------------------------------------
  243. // Function: CompType::getMemberCompType
  244. ///\brief Returns the compound datatype of the specified member in this
  245. /// compound datatype.
  246. ///\param member_num - IN: Zero-based index of the member
  247. ///\return CompType instance
  248. ///\exception H5::DataTypeIException
  249. // Programmer Binh-Minh Ribler - 2000
  250. //--------------------------------------------------------------------------
  251. CompType CompType::getMemberCompType( unsigned member_num ) const
  252. {
  253. try {
  254. CompType comptype(p_get_member_type(member_num));
  255. f_DataType_setId(&comptype, p_get_member_type(member_num));
  256. return(comptype);
  257. }
  258. catch (DataTypeIException E) {
  259. throw DataTypeIException("CompType::getMemberCompType", E.getDetailMsg());
  260. }
  261. }
  262. //--------------------------------------------------------------------------
  263. // Function: CompType::getMemberEnumType
  264. ///\brief Returns the enumeration datatype of the specified member in
  265. /// this compound datatype.
  266. ///\param member_num - IN: Zero-based index of the member
  267. ///\return EnumType instance
  268. ///\exception H5::DataTypeIException
  269. // Programmer Binh-Minh Ribler - 2000
  270. //--------------------------------------------------------------------------
  271. EnumType CompType::getMemberEnumType( unsigned member_num ) const
  272. {
  273. try {
  274. EnumType enumtype(p_get_member_type(member_num));
  275. f_DataType_setId(&enumtype, p_get_member_type(member_num));
  276. return(enumtype);
  277. }
  278. catch (DataTypeIException E) {
  279. throw DataTypeIException("CompType::getMemberEnumType", E.getDetailMsg());
  280. }
  281. }
  282. //--------------------------------------------------------------------------
  283. // Function: CompType::getMemberIntType
  284. ///\brief Returns the integer datatype of the specified member in this
  285. /// compound datatype.
  286. ///\param member_num - IN: Zero-based index of the member
  287. ///\return IntType instance
  288. ///\exception H5::DataTypeIException
  289. // Programmer Binh-Minh Ribler - 2000
  290. //--------------------------------------------------------------------------
  291. IntType CompType::getMemberIntType( unsigned member_num ) const
  292. {
  293. try {
  294. IntType inttype(p_get_member_type(member_num));
  295. f_DataType_setId(&inttype, p_get_member_type(member_num));
  296. return(inttype);
  297. }
  298. catch (DataTypeIException E) {
  299. throw DataTypeIException("CompType::getMemberIntType", E.getDetailMsg());
  300. }
  301. }
  302. //--------------------------------------------------------------------------
  303. // Function: CompType::getMemberFloatType
  304. ///\brief Returns the floating-point datatype of the specified member
  305. /// in this compound datatype.
  306. ///\param member_num - IN: Zero-based index of the member
  307. ///\return FloatType instance
  308. ///\exception H5::DataTypeIException
  309. // Programmer Binh-Minh Ribler - 2000
  310. //--------------------------------------------------------------------------
  311. FloatType CompType::getMemberFloatType( unsigned member_num ) const
  312. {
  313. try {
  314. FloatType floatype(p_get_member_type(member_num));
  315. f_DataType_setId(&floatype, p_get_member_type(member_num));
  316. return(floatype);
  317. }
  318. catch (DataTypeIException E) {
  319. throw DataTypeIException("CompType::getMemberFloatType", E.getDetailMsg());
  320. }
  321. }
  322. //--------------------------------------------------------------------------
  323. // Function: CompType::getMemberStrType
  324. ///\brief Returns the string datatype of the specified member in this
  325. /// compound datatype.
  326. ///\param member_num - IN: Zero-based index of the member
  327. ///\return StrType instance
  328. ///\exception H5::DataTypeIException
  329. // Programmer Binh-Minh Ribler - 2000
  330. //--------------------------------------------------------------------------
  331. StrType CompType::getMemberStrType( unsigned member_num ) const
  332. {
  333. try {
  334. StrType strtype(p_get_member_type(member_num));
  335. f_DataType_setId(&strtype, p_get_member_type(member_num));
  336. return(strtype);
  337. }
  338. catch (DataTypeIException E) {
  339. throw DataTypeIException("CompType::getMemberStrType", E.getDetailMsg());
  340. }
  341. }
  342. //--------------------------------------------------------------------------
  343. // Function: CompType::getMemberVarLenType
  344. ///\brief Returns the variable length datatype of the specified member
  345. /// in this compound datatype.
  346. ///\param member_num - IN: Zero-based index of the member
  347. ///\return VarLenType instance
  348. ///\exception H5::DataTypeIException
  349. // Programmer Binh-Minh Ribler - Jul, 2005
  350. //--------------------------------------------------------------------------
  351. VarLenType CompType::getMemberVarLenType( unsigned member_num ) const
  352. {
  353. try {
  354. VarLenType varlentype(p_get_member_type(member_num));
  355. f_DataType_setId(&varlentype, p_get_member_type(member_num));
  356. return(varlentype);
  357. }
  358. catch (DataTypeIException E) {
  359. throw DataTypeIException("CompType::getMemberVarLenType", E.getDetailMsg());
  360. }
  361. }
  362. /* old style of getMemberType - using overloads; new style above
  363. returns the appropriate datatypes but has different named functions.
  364. In the old style, a datatype must be passed into the function.
  365. May, 2004: These should be reconsidered to provide more convenience.
  366. // Returns the datatype of the specified member in this compound datatype.
  367. // Several overloading of getMemberType are for different datatypes
  368. void CompType::getMemberType( unsigned member_num, EnumType& enumtype ) const
  369. {
  370. p_get_member_type(member_num, enumtype);
  371. }
  372. void CompType::getMemberType( unsigned member_num, CompType& comptype ) const
  373. {
  374. p_get_member_type(member_num, comptype);
  375. }
  376. void CompType::getMemberType( unsigned member_num, IntType& inttype ) const
  377. {
  378. p_get_member_type(member_num, inttype);
  379. }
  380. void CompType::getMemberType( unsigned member_num, FloatType& floatype ) const
  381. {
  382. p_get_member_type(member_num, floatype);
  383. }
  384. void CompType::getMemberType( unsigned member_num, StrType& strtype ) const
  385. {
  386. p_get_member_type(member_num, strtype);
  387. }
  388. // end of overloading of getMemberType
  389. */
  390. //--------------------------------------------------------------------------
  391. // Function: CompType::insertMember
  392. ///\brief Inserts a new member to this compound datatype.
  393. ///\param name - IN: Name of the new member
  394. ///\param offset - IN: Offset in memory structure of the field to insert
  395. ///\param new_member - IN: New member to be inserted
  396. ///\exception H5::DataTypeIException
  397. // Programmer Binh-Minh Ribler - 2000
  398. //--------------------------------------------------------------------------
  399. void CompType::insertMember( const H5std_string& name, size_t offset, const DataType& new_member ) const
  400. {
  401. // Convert string to C-string
  402. const char* name_C;
  403. name_C = name.c_str(); // name_C refers to the contents of name as a C-str
  404. hid_t new_member_id = new_member.getId(); // get new_member id for C API
  405. // Call C routine H5Tinsert to add the new member
  406. herr_t ret_value = H5Tinsert( id, name_C, offset, new_member_id );
  407. if( ret_value < 0 )
  408. {
  409. throw DataTypeIException("CompType::insertMember", "H5Tinsert failed");
  410. }
  411. }
  412. //--------------------------------------------------------------------------
  413. // Function: CompType::pack
  414. ///\brief Recursively removes padding from within a compound datatype.
  415. ///
  416. ///\exception H5::DataTypeIException
  417. // Programmer Binh-Minh Ribler - 2000
  418. //--------------------------------------------------------------------------
  419. void CompType::pack() const
  420. {
  421. // Calls C routine H5Tpack to remove padding
  422. herr_t ret_value = H5Tpack( id );
  423. if( ret_value < 0 )
  424. {
  425. throw DataTypeIException("CompType::pack", "H5Tpack failed");
  426. }
  427. }
  428. //--------------------------------------------------------------------------
  429. // Function: CompType::setSize
  430. ///\brief Sets the total size for this compound datatype.
  431. ///\param size - IN: Size to set
  432. ///\exception H5::DataTypeIException
  433. // Note
  434. // H5Tset_size works on atom datatypes and compound datatypes only
  435. // Programmer Binh-Minh Ribler - 2014
  436. //--------------------------------------------------------------------------
  437. void CompType::setSize(size_t size) const
  438. {
  439. // Call C routine H5Tset_size to set the total size
  440. herr_t ret_value = H5Tset_size(id, size);
  441. if (ret_value < 0)
  442. {
  443. throw DataTypeIException("CompType::setSize", "H5Tset_size failed");
  444. }
  445. }
  446. //--------------------------------------------------------------------------
  447. // Function: CompType destructor
  448. ///\brief Properly terminates access to this compound datatype.
  449. // Programmer Binh-Minh Ribler - 2000
  450. //--------------------------------------------------------------------------
  451. CompType::~CompType() {}
  452. #ifndef H5_NO_NAMESPACE
  453. } // end namespace
  454. #endif