summaryrefslogtreecommitdiffstats
path: root/c++
diff options
context:
space:
mode:
authorBinh-Minh Ribler <bmribler@hdfgroup.org>2014-04-07 20:08:59 (GMT)
committerBinh-Minh Ribler <bmribler@hdfgroup.org>2014-04-07 20:08:59 (GMT)
commit67a61ed22f31b9af0ace476b0cc58d7236bb9ac3 (patch)
tree3f38c808358221ffbe9232ba1c23ebe439bfdb30 /c++
parent849177c7b6810d2fc4c16092fc3e37a3fee12075 (diff)
downloadhdf5-67a61ed22f31b9af0ace476b0cc58d7236bb9ac3.zip
hdf5-67a61ed22f31b9af0ace476b0cc58d7236bb9ac3.tar.gz
hdf5-67a61ed22f31b9af0ace476b0cc58d7236bb9ac3.tar.bz2
[svn-r24983] Purpose: Fix HDFFV-8367
Description: - Added wrappers to CommomFG for H5Oget_info_by_name() to get a child object's type (requested by user) H5O_type_t childObjType(const H5std_string& objname) const; H5O_type_t childObjType(const char* objname) const; H5O_type_t childObjType(hsize_t index, H5_index_t index_type=H5_INDEX_NAME, H5_iter_order_t order=H5_ITER_INC, const char* objname=".") const; - Added tests to tobject.cpp Platforms tested: Linux/ppc64 (ostrich) Linux/32 2.6 (jam) SunOS 5.11 (emu)
Diffstat (limited to 'c++')
-rw-r--r--c++/src/H5CommonFG.cpp119
-rw-r--r--c++/src/H5CommonFG.h6
-rw-r--r--c++/test/tobject.cpp66
3 files changed, 191 insertions, 0 deletions
diff --git a/c++/src/H5CommonFG.cpp b/c++/src/H5CommonFG.cpp
index fc8b08d..88cf989 100644
--- a/c++/src/H5CommonFG.cpp
+++ b/c++/src/H5CommonFG.cpp
@@ -939,7 +939,125 @@ ssize_t CommonFG::getObjnameByIdx(hsize_t idx, H5std_string& name, size_t size)
return (name_len);
}
+//--------------------------------------------------------------------------
+// Function: CommonFG::childObjType
+///\brief Returns the type of an object in this file/group, given the
+/// object's name.
+///\param objname - IN: Name of the object
+///\return Object type, which can have the following values for group,
+/// dataset, and named datatype
+/// \li \c H5O_TYPE_GROUP
+/// \li \c H5O_TYPE_DATASET
+/// \li \c H5O_TYPE_NAMED_DATATYPE
+/// Refer to the C API documentation for more details:
+/// http://www.hdfgroup.org/HDF5/doc/RM/RM_H5O.html#Object-GetInfo
+///\exception H5::FileIException or H5::GroupIException
+/// Exception will be thrown when:
+/// - an error returned by the C API
+/// - object type is not one of the valid values above
+// Programmer Binh-Minh Ribler - April, 2014
+//--------------------------------------------------------------------------
+H5O_type_t CommonFG::childObjType(const char* objname) const
+{
+ H5O_info_t objinfo;
+ H5O_type_t objtype = H5O_TYPE_UNKNOWN;
+
+ // Use C API to get information of the object
+ herr_t ret_value = H5Oget_info_by_name(getLocId(), objname, &objinfo, H5P_DEFAULT);
+
+ // Throw exception if C API returns failure
+ if (ret_value < 0)
+ throwException("childObjType", "H5Oget_info_by_name failed");
+ // Return a valid type or throw an exception for unknown type
+ else
+ switch (objinfo.type)
+ {
+ case H5O_TYPE_GROUP:
+ case H5O_TYPE_DATASET:
+ case H5O_TYPE_NAMED_DATATYPE:
+ objtype = objinfo.type;
+ break;
+ default:
+ throwException("childObjType", "Unknown type of object");
+ }
+ return(objtype);
+}
+
+//--------------------------------------------------------------------------
+// Function: CommonFG::childObjType
+///\brief This is an overloaded member function, provided for convenience.
+/// It takes an \a H5std_string for the object's name.
+///\brief Returns the type of an object in this group, given the
+/// object's name.
+///\param objname - IN: Name of the object (H5std_string&)
+///\exception H5::FileIException or H5::GroupIException
+// Programmer Binh-Minh Ribler - April, 2014
+//--------------------------------------------------------------------------
+H5O_type_t CommonFG::childObjType(const H5std_string& objname) const
+{
+ // Use overloaded function
+ H5O_type_t objtype = childObjType(objname.c_str());
+ return(objtype);
+}
+
+//--------------------------------------------------------------------------
+// Function: CommonFG::childObjType
+///\brief Returns the type of an object in this file/group, given the
+/// object's index and its type and order.
+///\param index - IN: Position of the object
+///\param index_type - IN: Type of the index, default to H5_INDEX_NAME
+///\param order - IN: Traversing order, default to H5_ITER_INC
+///\param objname - IN: Name of the object, default to "."
+///\return Object type, which can have the following values for group,
+/// dataset, and named datatype
+/// \li \c H5O_TYPE_GROUP
+/// \li \c H5O_TYPE_DATASET
+/// \li \c H5O_TYPE_NAMED_DATATYPE
+/// Refer to the C API documentation for more details:
+/// http://www.hdfgroup.org/HDF5/doc/RM/RM_H5O.html#Object-GetInfo
+///\exception H5::FileIException or H5::GroupIException
+/// Exception will be thrown when:
+/// - an error returned by the C API
+/// - object type is not one of the valid values above
+// Developer's Notes:
+// - this overload uses H5Oget_info_by_idx instead of H5Oget_info_by_name
+// like the previous childObjType()
+// - index is the required argument so, first
+// - objname is last because it's more likely the location is already
+// fully specified
+// - Leave property list out for now because C API is not using it, it
+// can be added later when needed.
+// Programmer Binh-Minh Ribler - April, 2014
+//--------------------------------------------------------------------------
+H5O_type_t CommonFG::childObjType(hsize_t index, H5_index_t index_type, H5_iter_order_t order, const char* objname) const
+{
+ herr_t ret_value;
+ H5O_info_t objinfo;
+ H5O_type_t objtype = H5O_TYPE_UNKNOWN;
+
+ // Use C API to get information of the object
+ ret_value = H5Oget_info_by_idx(getLocId(), objname, index_type, order, index, &objinfo, H5P_DEFAULT);
+
+ // Throw exception if C API returns failure
+ if (ret_value < 0)
+ throwException("childObjType", "H5Oget_info_by_idx failed");
+ // Return a valid type or throw an exception for unknown type
+ else
+ switch (objinfo.type)
+ {
+ case H5O_TYPE_GROUP:
+ case H5O_TYPE_DATASET:
+ case H5O_TYPE_NAMED_DATATYPE:
+ objtype = objinfo.type;
+ break;
+ default:
+ throwException("childObjType", "Unknown type of object");
+ }
+ return(objtype);
+}
+
#ifndef H5_NO_DEPRECATED_SYMBOLS
+#ifndef DOXYGEN_SHOULD_SKIP_THIS
//--------------------------------------------------------------------------
// Function: CommonFG::getObjTypeByIdx
///\brief Returns the type of an object in this group, given the
@@ -1010,6 +1128,7 @@ H5G_obj_t CommonFG::getObjTypeByIdx(hsize_t idx, H5std_string& type_name) const
}
return (obj_type);
}
+#endif // DOXYGEN_SHOULD_SKIP_THIS
#endif /* H5_NO_DEPRECATED_SYMBOLS */
#ifndef DOXYGEN_SHOULD_SKIP_THIS
diff --git a/c++/src/H5CommonFG.h b/c++/src/H5CommonFG.h
index 2f10e8e..8fc0b15 100644
--- a/c++/src/H5CommonFG.h
+++ b/c++/src/H5CommonFG.h
@@ -64,6 +64,12 @@ class H5_DLLCPP CommonFG {
ssize_t getObjnameByIdx(hsize_t idx, char* name, size_t size) const;
ssize_t getObjnameByIdx(hsize_t idx, H5std_string& name, size_t size) const;
+ // Retrieves the type of an object in this file or group, given the
+ // object's name
+ H5O_type_t childObjType(const H5std_string& objname) const;
+ H5O_type_t childObjType(const char* objname) const;
+ H5O_type_t childObjType(hsize_t index, H5_index_t index_type=H5_INDEX_NAME, H5_iter_order_t order=H5_ITER_INC, const char* objname=".") const;
+
#ifndef H5_NO_DEPRECATED_SYMBOLS
// Returns the type of an object in this group, given the
// object's index.
diff --git a/c++/test/tobject.cpp b/c++/test/tobject.cpp
index e6ef69c..b86d7cb 100644
--- a/c++/test/tobject.cpp
+++ b/c++/test/tobject.cpp
@@ -220,6 +220,71 @@ static void test_get_objname_ontypes()
} // test_get_objname_ontypes
/*-------------------------------------------------------------------------
+ * Function: test_get_objtype
+ *
+ * Purpose: Tests getting object type
+ *
+ * Return: Success: 0
+ * Failure: -1
+ *
+ * Programmer: Binh-Minh Ribler
+ * Friday, March 4, 2014
+ *
+ * Modifications:
+ *
+ *-------------------------------------------------------------------------
+ */
+static void test_get_objtype()
+{
+ SUBTEST("H5File::childObjType and H5Group::childObjType");
+
+ try {
+ // Open file
+ H5File file(FILE_OBJECTS, H5F_ACC_RDWR);
+
+ // Open the top group
+ Group grp1 = file.openGroup(GROUP1);
+
+ // Create a datatype and save it
+ DataType dtype(PredType::STD_I32LE);
+ dtype.commit(grp1, "STD_I32LE");
+
+ // Get and verify object type with
+ // H5O_type_t childObjType(const H5std_string& objname)
+ H5O_type_t objtype = file.childObjType(DSET_IN_FILE);
+ verify_val(objtype, H5O_TYPE_DATASET, "DataSet::childObjType", __LINE__, __FILE__);
+
+ // Get and verify object type with
+ // H5O_type_t childObjType(const char* objname)
+ objtype = grp1.childObjType(GROUP1_1.c_str());
+ verify_val(objtype, H5O_TYPE_GROUP, "DataSet::childObjType", __LINE__, __FILE__);
+
+ // Get and verify object type with
+ // H5O_type_t childObjType(hsize_t index, H5_index_t index_type,
+ // H5_iter_order_t order, const char* objname=".")
+ objtype = grp1.childObjType((hsize_t)1, H5_INDEX_NAME, H5_ITER_INC);
+ verify_val(objtype, H5O_TYPE_NAMED_DATATYPE, "DataSet::childObjType", __LINE__, __FILE__);
+
+ // Get and verify object type with
+ // H5O_type_t childObjType(hsize_t index,
+ // H5_index_t index_type=H5_INDEX_NAME,
+ // H5_iter_order_t order=H5_ITER_INC, const char* objname=".")
+ objtype = grp1.childObjType((hsize_t)2);
+ verify_val(objtype, H5O_TYPE_GROUP, "DataSet::childObjType", __LINE__, __FILE__);
+
+ // Everything will be closed as they go out of scope
+
+ PASSED();
+ } // try block
+
+ // catch all other exceptions
+ catch (Exception E)
+ {
+ issue_fail_msg("test_get_objtype", __LINE__, __FILE__);
+ }
+} // test_get_objtype
+
+/*-------------------------------------------------------------------------
* Function: test_objects
*
* Purpose: Tests HDF5 object related functionality
@@ -244,6 +309,7 @@ void test_object()
test_get_objname(); // Test get object name from groups/datasets
test_get_objname_ontypes(); // Test get object name from types
+ test_get_objtype(); // Test get object type
} // test_objects