/libs/hdf5/hdf5-1.8.18/c++/src/H5EnumType.cpp

https://gitlab.com/jeff.lovelace/hypermac · C++ · 278 lines · 111 code · 24 blank · 143 comment · 8 complexity · cbcd3feaabb487fd7aba206222cb3481 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 "H5AbstractDs.h"
  22. #include "H5DxferProp.h"
  23. #include "H5DataSpace.h"
  24. #include "H5OcreatProp.h"
  25. #include "H5DcreatProp.h"
  26. #include "H5CommonFG.h"
  27. #include "H5DataType.h"
  28. #include "H5DataSet.h"
  29. #include "H5AtomType.h"
  30. #include "H5IntType.h"
  31. #include "H5EnumType.h"
  32. #include "H5private.h" // for HDmemset
  33. namespace H5 {
  34. //--------------------------------------------------------------------------
  35. // Function: EnumType default constructor
  36. ///\brief Default constructor: Creates a stub datatype
  37. // Programmer Binh-Minh Ribler - 2000
  38. //--------------------------------------------------------------------------
  39. EnumType::EnumType() : DataType() {}
  40. //--------------------------------------------------------------------------
  41. // Function: EnumType overloaded constructor
  42. ///\brief Creates an EnumType object using the id of an existing datatype.
  43. ///\param existing_id - IN: Id of an existing datatype
  44. ///\exception H5::DataTypeIException
  45. // Programmer Binh-Minh Ribler - 2000
  46. //--------------------------------------------------------------------------
  47. EnumType::EnumType( const hid_t existing_id ) : DataType( existing_id ) {}
  48. //--------------------------------------------------------------------------
  49. // Function: EnumType copy constructor
  50. ///\brief Copy constructor: makes a copy of the original EnumType object.
  51. // Programmer Binh-Minh Ribler - 2000
  52. //--------------------------------------------------------------------------
  53. EnumType::EnumType( const EnumType& original ) : DataType( original ) {}
  54. //--------------------------------------------------------------------------
  55. // Function: EnumType overloaded constructor
  56. ///\brief Creates an empty enumeration datatype given a size, in bytes.
  57. ///\param size - IN: Number of bytes in the datatype to create
  58. ///\exception H5::DataTypeIException
  59. // Description
  60. // The DataType constructor calls the C API H5Tcreate to create
  61. // the enum datatype.
  62. // Programmer Binh-Minh Ribler - 2000
  63. //--------------------------------------------------------------------------
  64. EnumType::EnumType( size_t size ) : DataType( H5T_ENUM, size ) {}
  65. //--------------------------------------------------------------------------
  66. // Function: EnumType overloaded constructor
  67. ///\brief Gets the enum datatype of the specified dataset.
  68. ///\param dataset - IN: Dataset that this enum datatype associates with
  69. ///\exception H5::DataTypeIException
  70. // Programmer Binh-Minh Ribler - 2000
  71. //--------------------------------------------------------------------------
  72. EnumType::EnumType( const DataSet& dataset ) : DataType()
  73. {
  74. // Calls C function H5Dget_type to get the id of the datatype
  75. id = H5Dget_type( dataset.getId() );
  76. // If the datatype id is not valid, throw an exception
  77. if( id < 0 )
  78. {
  79. throw DataSetIException("EnumType constructor", "H5Dget_type failed");
  80. }
  81. }
  82. //--------------------------------------------------------------------------
  83. // Function: EnumType overloaded constructor
  84. ///\brief Creates a new enum datatype based on an integer datatype.
  85. ///\param data_type - IN: Base datatype
  86. ///\exception H5::DataTypeIException
  87. // Programmer Binh-Minh Ribler - 2000
  88. //--------------------------------------------------------------------------
  89. EnumType::EnumType( const IntType& data_type ) : DataType()
  90. {
  91. // Calls C function H5Tenum_create to get the id of the datatype
  92. id = H5Tenum_create( data_type.getId() );
  93. // If the datatype id is not valid, throw an exception
  94. if( id < 0 )
  95. {
  96. throw DataSetIException("EnumType constructor", "H5Tenum_create failed");
  97. }
  98. }
  99. //--------------------------------------------------------------------------
  100. // Function: EnumType::insert
  101. ///\brief Inserts a new member to this enumeration datatype.
  102. ///\param name - IN: Name of the new member
  103. ///\param value - IN: Pointer to the value of the new member
  104. ///\exception H5::DataTypeIException
  105. // Programmer Binh-Minh Ribler - 2000
  106. //--------------------------------------------------------------------------
  107. void EnumType::insert( const char* name, void *value ) const
  108. {
  109. // Calls C routine H5Tenum_insert to insert the new enum datatype member.
  110. herr_t ret_value = H5Tenum_insert( id, name, value );
  111. if( ret_value < 0 )
  112. {
  113. throw DataTypeIException("EnumType::insert", "H5Tenum_insert failed");
  114. }
  115. }
  116. //--------------------------------------------------------------------------
  117. // Function: EnumType::insert
  118. ///\brief This is an overloaded member function, provided for convenience.
  119. /// It differs from the above function only in the type of
  120. /// argument \a name.
  121. // Programmer Binh-Minh Ribler - 2000
  122. //--------------------------------------------------------------------------
  123. void EnumType::insert( const H5std_string& name, void *value ) const
  124. {
  125. insert( name.c_str(), value );
  126. }
  127. //--------------------------------------------------------------------------
  128. // Function: EnumType::nameOf
  129. ///\brief Returns the symbol name corresponding to a specified member
  130. /// of this enumeration datatype.
  131. ///\param value - IN: Pointer to the value of the enum datatype
  132. ///\param size - IN: Size for the name
  133. ///\exception H5::DataTypeIException
  134. // Programmer Binh-Minh Ribler - 2000
  135. //--------------------------------------------------------------------------
  136. H5std_string EnumType::nameOf( void *value, size_t size ) const
  137. {
  138. char* name_C = new char[size+1]; // temporary C-string for C API
  139. HDmemset(name_C, 0, size+1); // clear buffer
  140. // Calls C routine H5Tenum_nameof to get the name of the specified enum type
  141. herr_t ret_value = H5Tenum_nameof( id, value, name_C, size );
  142. // If H5Tenum_nameof returns a negative value, raise an exception,
  143. if( ret_value < 0 )
  144. {
  145. delete []name_C;
  146. throw DataTypeIException("EnumType::nameOf", "H5Tenum_nameof failed");
  147. }
  148. // otherwise, create the string to hold the datatype name and return it
  149. H5std_string name(name_C);
  150. delete []name_C;
  151. return( name );
  152. }
  153. //--------------------------------------------------------------------------
  154. // Function: EnumType::valueOf
  155. ///\brief Retrieves the value corresponding to a member of this
  156. /// enumeration datatype, given the member's name.
  157. ///\param name - IN: Name of the queried member
  158. ///\param value - OUT: Pointer to the retrieved value
  159. ///\exception H5::DataTypeIException
  160. // Programmer Binh-Minh Ribler - 2000
  161. //--------------------------------------------------------------------------
  162. void EnumType::valueOf( const char* name, void *value ) const
  163. {
  164. // Calls C routine H5Tenum_valueof to get the enum datatype value
  165. herr_t ret_value = H5Tenum_valueof( id, name, value );
  166. if( ret_value < 0 )
  167. {
  168. throw DataTypeIException("EnumType::valueOf", "H5Tenum_valueof failed");
  169. }
  170. }
  171. //--------------------------------------------------------------------------
  172. // Function: EnumType::valueOf
  173. ///\brief This is an overloaded member function, provided for convenience.
  174. /// It differs from the above function only in the type of
  175. /// argument \a name.
  176. // Programmer Binh-Minh Ribler - 2000
  177. //--------------------------------------------------------------------------
  178. void EnumType::valueOf( const H5std_string& name, void *value ) const
  179. {
  180. valueOf( name.c_str(), value );
  181. }
  182. //--------------------------------------------------------------------------
  183. // Function: EnumType::getMemberIndex
  184. ///\brief Returns the index of a member in this enumeration datatype.
  185. ///\param name - IN: Name of the queried member
  186. ///\return Index of the member if it exists. Index will have the value
  187. /// between 0 and \c N-1, where \c N is the value returned by the
  188. /// member function \c EnumType::getNmembers.
  189. ///\exception H5::DataTypeIException
  190. // Programmer Binh-Minh Ribler - May 16, 2002
  191. //--------------------------------------------------------------------------
  192. int EnumType::getMemberIndex(const char *name) const
  193. {
  194. int member_index = H5Tget_member_index(id, name);
  195. if( member_index < 0 )
  196. {
  197. throw DataTypeIException("EnumType::getMemberIndex",
  198. "H5Tget_member_index returns negative value");
  199. }
  200. return( member_index );
  201. }
  202. //--------------------------------------------------------------------------
  203. // Function: EnumType::getMemberIndex
  204. ///\brief This is an overloaded member function, provided for convenience.
  205. /// It differs from the above function only in the type of
  206. /// argument \a name.
  207. // Programmer Binh-Minh Ribler - May 16, 2002
  208. //--------------------------------------------------------------------------
  209. int EnumType::getMemberIndex(const H5std_string& name) const
  210. {
  211. return(EnumType::getMemberIndex(name.c_str()));
  212. }
  213. //--------------------------------------------------------------------------
  214. // Function: EnumType::getNmembers
  215. ///\brief Returns the number of members in this enumeration datatype.
  216. ///\return Number of members
  217. ///\exception H5::DataTypeIException
  218. // Programmer Binh-Minh Ribler - May, 2004
  219. //--------------------------------------------------------------------------
  220. int EnumType::getNmembers() const
  221. {
  222. int num_members = H5Tget_nmembers( id );
  223. if( num_members < 0 )
  224. {
  225. throw DataTypeIException("EnumType::getNmembers",
  226. "H5Tget_nmembers returns negative number of members");
  227. }
  228. return( num_members );
  229. }
  230. //--------------------------------------------------------------------------
  231. // Function: EnumType::getMemberValue
  232. ///\brief Retrieves the value of a member in this enumeration datatype,
  233. /// given the member's index.
  234. ///\param memb_no - IN: Index of the queried member
  235. ///\param value - OUT: Pointer to the retrieved value
  236. ///\exception H5::DataTypeIException
  237. // Programmer Binh-Minh Ribler - 2000
  238. //--------------------------------------------------------------------------
  239. void EnumType::getMemberValue( unsigned memb_no, void *value ) const
  240. {
  241. // Call C routine H5Tget_member_value to get the datatype member's value
  242. hid_t ret_value = H5Tget_member_value( id, memb_no, value );
  243. if( ret_value < 0 )
  244. {
  245. throw DataTypeIException("EnumType::getMemberValue", "H5Tget_member_value failed");
  246. }
  247. }
  248. //--------------------------------------------------------------------------
  249. // Function: EnumType destructor
  250. ///\brief Properly terminates access to this enum datatype.
  251. // Programmer Binh-Minh Ribler - 2000
  252. //--------------------------------------------------------------------------
  253. EnumType::~EnumType() {}
  254. } // end namespace