summaryrefslogtreecommitdiffstats
path: root/c++/src
diff options
context:
space:
mode:
authorBinh-Minh Ribler <bmribler@hdfgroup.org>2016-01-15 15:53:33 (GMT)
committerBinh-Minh Ribler <bmribler@hdfgroup.org>2016-01-15 15:53:33 (GMT)
commit0d68aa89cede07df85eca611510a6271fbbb1d8a (patch)
treebdef31cdbdb77168e26bd4e2ed0eb12665dada44 /c++/src
parentc09393ed0a2624cc04556249ef5549c2485efe8f (diff)
downloadhdf5-0d68aa89cede07df85eca611510a6271fbbb1d8a.zip
hdf5-0d68aa89cede07df85eca611510a6271fbbb1d8a.tar.gz
hdf5-0d68aa89cede07df85eca611510a6271fbbb1d8a.tar.bz2
[svn-r28905] Purpose: Fix user reported problem
Description: User Adam Rosenberger reported a failure when using the member function AbstractDs::getArrayType(). This problem was caused by missing initialization of the ArrayType's members in some cases. Solution: - Added ArrayType::setArrayInfo() to retrieve rank and dimensions of an array and store them in memory for easy access. - Re-factored a few functions to use the new function. - We'll give him 1.8.16 patch Platforms tested: Linux/32 2.6 (jam) Linux/64 (platypus) Darwin (osx1010test)
Diffstat (limited to 'c++/src')
-rw-r--r--c++/src/H5AbstractDs.cpp3
-rw-r--r--c++/src/H5ArrayType.cpp123
-rw-r--r--c++/src/H5ArrayType.h3
-rw-r--r--c++/src/H5CompType.cpp11
4 files changed, 88 insertions, 52 deletions
diff --git a/c++/src/H5AbstractDs.cpp b/c++/src/H5AbstractDs.cpp
index 06b3e22..4e9a4d5 100644
--- a/c++/src/H5AbstractDs.cpp
+++ b/c++/src/H5AbstractDs.cpp
@@ -141,8 +141,11 @@ ArrayType AbstractDs::getArrayType() const
// depending on which object invokes getArrayType. Then, create and
// return the ArrayType object
try {
+ // Create ArrayType and set values this way to work around the
+ // problem described in the JIRA issue HDFFV-7947
ArrayType arraytype;
f_DataType_setId(&arraytype, p_get_type());
+ arraytype.setArrayInfo();
return(arraytype);
}
catch (DataSetIException E) {
diff --git a/c++/src/H5ArrayType.cpp b/c++/src/H5ArrayType.cpp
index 85340f8..0f09631 100644
--- a/c++/src/H5ArrayType.cpp
+++ b/c++/src/H5ArrayType.cpp
@@ -35,12 +35,7 @@ namespace H5 {
///\brief Default constructor: Creates a stub ArrayType
// Programmer Binh-Minh Ribler - May 2004
//--------------------------------------------------------------------------
-ArrayType::ArrayType() : DataType()
-{
- // Initialize members
- rank = -1;
- dimensions = NULL;
-}
+ArrayType::ArrayType() : DataType(), rank(-1), dimensions(NULL) {}
//--------------------------------------------------------------------------
// Function: ArrayType overloaded constructor
@@ -51,20 +46,7 @@ ArrayType::ArrayType() : DataType()
//--------------------------------------------------------------------------
ArrayType::ArrayType( const hid_t existing_id ) : DataType( existing_id )
{
- // Get the rank of the existing array and store it in this array
- rank = H5Tget_array_ndims(existing_id);
- if (rank < 0)
- {
- throw DataTypeIException("ArrayType constructor (existing id)", "H5Tget_array_ndims failed");
- }
-
- // Allocate space for the dimensions
- dimensions = new hsize_t[rank];
-
- // Get the dimensions of the existing array and store it in this array
- int ret_value = H5Tget_array_dims2(id, dimensions);
- if (ret_value < 0)
- throw DataTypeIException("ArrayType constructor (existing id)", "H5Tget_array_dims2 failed");
+ setArrayInfo();
}
//--------------------------------------------------------------------------
@@ -111,25 +93,67 @@ ArrayType::ArrayType(const DataType& base_type, int ndims, const hsize_t* dims)
}
//--------------------------------------------------------------------------
+// Function: ArrayType::setArrayInfo
+///\brief Retrieves the rank and dimensions from the array datatype
+/// and store the info in this ArrayType object.
+///\exception H5::DataTypeIException
+// Programmer Binh-Minh Ribler - January 2016
+//--------------------------------------------------------------------------
+void ArrayType::setArrayInfo()
+{
+ // Get the rank of the array type specified by id from the C API
+ int ndims = H5Tget_array_ndims(id);
+ if (ndims < 0)
+ {
+ throw DataTypeIException("ArrayType::setArrayInfo", "H5Tget_array_ndims failed");
+ }
+
+ // Get the dimensions from the C API
+ hsize_t* dims;
+ dims = new hsize_t[ndims];
+ if (dims != NULL)
+ {
+ // Get the dimensions
+ ndims = H5Tget_array_dims2(id, dims);
+ if (ndims < 0)
+ throw DataTypeIException("ArrayType::setArrayInfo", "H5Tget_array_dims2 failed");
+
+ // Store the array's info in memory
+ rank = ndims;
+ dimensions = new hsize_t[rank];
+ for (int i = 0; i < rank; i++)
+ dimensions[i] = dims[i];
+ delete []dims;
+ }
+} // setArrayInfo
+
+//--------------------------------------------------------------------------
// Function: ArrayType::getArrayNDims
///\brief Returns the number of dimensions for an array datatype.
///\return Number of dimensions
///\exception H5::DataTypeIException
// Programmer Binh-Minh Ribler - May 2004
+// Modification
+// Modified to use setArrayInfo().
+// If rank is positive, return rank
+// If rank is invalid but object has a valid identifier, obtain the
+// rank and dimensions, store them in the object, and return rank
+// Otherwise, i.e., rank is invalid and object doesn't have a
+// valid identifier, throw an exception
//--------------------------------------------------------------------------
int ArrayType::getArrayNDims()
{
- // If the array's rank has not been stored, i.e. rank is init to -1,
- // retrieve it via the C API
- if (rank < 0)
- {
- rank = H5Tget_array_ndims(id);
- if (rank < 0)
- {
- throw DataTypeIException("ArrayType::getArrayNDims", "H5Tget_array_ndims failed");
- }
- }
- return(rank);
+ // Validate the id first, this object could be a default object
+ if (!p_valid_id(id))
+ throw DataTypeIException("ArrayType::getArrayNDims", "ArrayType object is not a valid array type.");
+
+ // If the array's info has not been stored, i.e. "rank" still has its
+ // initial value, -1, and "dimensions" is still NULL, retrieve rank and
+ // dimensions via the C API and store them in this ArrayType object.
+ if (rank < 0 && dimensions == NULL)
+ setArrayInfo();
+
+ return(rank);
}
//--------------------------------------------------------------------------
@@ -139,25 +163,30 @@ int ArrayType::getArrayNDims()
///\return Number of dimensions
///\exception H5::DataTypeIException
// Programmer Binh-Minh Ribler - May 2004
+// Modification
+// Jan, 2016
+// Modified to use setArrayInfo().
+// If the array information has not been stored, retrieve rank and
+// dimensions of the array type identified by "id" via the C API.
+// Copy "dimensions" to the user's buffer
//--------------------------------------------------------------------------
int ArrayType::getArrayDims(hsize_t* dims)
{
- // If the array's dimensions have not been stored, retrieve them via C API
- if (dimensions == NULL)
- {
- int ndims = H5Tget_array_dims2(id, dims);
- if (ndims < 0)
- throw DataTypeIException("ArrayType::getArrayDims", "H5Tget_array_dims2 failed");
- // Store the array's info in memory
- rank = ndims;
- dimensions = new hsize_t[rank];
- for (int i = 0; i < rank; i++)
- dimensions[i] = dims[i];
- }
- // Otherwise, simply copy what's in 'dimensions' to 'dims'
- for (int i = 0; i < rank; i++)
- dims[i] = dimensions[i];
- return(rank);
+ // Validate the id first, this object could be a default object
+ if (!p_valid_id(id))
+ throw DataTypeIException("ArrayType::getArrayDims", "ArrayType object is not a valid array type.");
+
+ // If the array's info has not been stored, i.e. "rank" still has its
+ // initial value, -1, and "dimensions" is still NULL, retrieve rank and
+ // dimensions via the C API and store them in this ArrayType object.
+ if (rank < 0 && dimensions == NULL)
+ setArrayInfo();
+
+ // Copy what's in "dimensions" to user's buffer "dims"
+ for (int i = 0; i < rank; i++)
+ dims[i] = dimensions[i];
+
+ return(rank);
}
//--------------------------------------------------------------------------
diff --git a/c++/src/H5ArrayType.h b/c++/src/H5ArrayType.h
index 6577a6e..c0f4b38 100644
--- a/c++/src/H5ArrayType.h
+++ b/c++/src/H5ArrayType.h
@@ -31,6 +31,9 @@ class H5_DLLCPP ArrayType : public DataType {
// specified base type.
ArrayType(const DataType& base_type, int ndims, const hsize_t* dims);
+ // Stores the rank and dimensions in memory.
+ void setArrayInfo();
+
// Returns the number of dimensions of this array datatype.
int getArrayNDims();
diff --git a/c++/src/H5CompType.cpp b/c++/src/H5CompType.cpp
index 6d31a68..82575d6 100644
--- a/c++/src/H5CompType.cpp
+++ b/c++/src/H5CompType.cpp
@@ -228,12 +228,12 @@ hid_t CompType::p_get_member_type(unsigned member_num) const
DataType CompType::getMemberDataType( unsigned member_num ) const
{
try {
- DataType datatype;
+ DataType datatype;
f_DataType_setId(&datatype, p_get_member_type(member_num));
- return(datatype);
+ return(datatype);
}
catch (DataTypeIException E) {
- throw DataTypeIException("CompType::getMemberDataType", E.getDetailMsg());
+ throw DataTypeIException("CompType::getMemberDataType", E.getDetailMsg());
}
}
@@ -249,9 +249,10 @@ DataType CompType::getMemberDataType( unsigned member_num ) const
ArrayType CompType::getMemberArrayType( unsigned member_num ) const
{
try {
- ArrayType arraytype(p_get_member_type(member_num));
+ ArrayType arraytype;
f_DataType_setId(&arraytype, p_get_member_type(member_num));
- return(arraytype);
+ arraytype.setArrayInfo();
+ return(arraytype);
}
catch (DataTypeIException E) {
throw DataTypeIException("CompType::getMemberArrayType", E.getDetailMsg());