From 5fcec401bd019aa0928e11c3d4cff62ea064c1ef Mon Sep 17 00:00:00 2001 From: Binh-Minh Ribler Date: Thu, 27 Sep 2012 18:26:16 -0500 Subject: [svn-r22836] Purpose: Fix bug HDFFV-533 and add other missing functions Description: In this bug, H5File doesn't have the ability to create attribute. The following changes will provide that functionality and several others that were also missing: - Added an abstract class H5Location in between IdComponent and H5Object. - New class structure of IdComponent, H5Location, H5Object, H5File IdComponent | H5Location / \ H5Object H5File - Wrappers in H5Object were moved to H5Location because the related C functions take either file, group, dataset, or named datatype ID. - Added wrapper for H5Rget_obj_type2 - Added tests for file attributes and H5Rget_obj_type2 wrapper Platforms tested: Linux/32 2.6 (jam) Linux/64 2.6 (koala) SunOS 5.10 (linew) --- c++/src/H5AbstractDs.cpp | 5 - c++/src/H5Attribute.cpp | 25 ++ c++/src/H5Attribute.h | 4 + c++/src/H5Classes.h | 1 + c++/src/H5Cpp.h | 1 + c++/src/H5DataSet.cpp | 27 -- c++/src/H5DataSet.h | 5 - c++/src/H5DataType.cpp | 25 -- c++/src/H5DataType.h | 5 - c++/src/H5File.cpp | 157 +---------- c++/src/H5File.h | 20 +- c++/src/H5Group.cpp | 27 -- c++/src/H5Group.h | 5 - c++/src/H5Location.cpp | 669 +++++++++++++++++++++++++++++++++++++++++++++++ c++/src/H5Location.h | 146 +++++++++++ c++/src/H5Object.cpp | 501 +---------------------------------- c++/src/H5Object.h | 81 ++---- c++/src/Makefile.am | 17 +- c++/src/Makefile.in | 34 +-- c++/test/h5cpputil.cpp | 1 + c++/test/tattr.cpp | 135 +++++++--- c++/test/tfile.cpp | 137 +++++++++- c++/test/trefer.cpp | 44 ++-- 23 files changed, 1148 insertions(+), 924 deletions(-) create mode 100644 c++/src/H5Location.cpp create mode 100644 c++/src/H5Location.h diff --git a/c++/src/H5AbstractDs.cpp b/c++/src/H5AbstractDs.cpp index 9cf1ee8..e6cacf9 100644 --- a/c++/src/H5AbstractDs.cpp +++ b/c++/src/H5AbstractDs.cpp @@ -25,11 +25,6 @@ #include "H5CommonFG.h" #include "H5Alltypes.h" -#include // remove when done - - using std::cerr; - using std::endl; - #ifndef H5_NO_NAMESPACE namespace H5 { #endif diff --git a/c++/src/H5Attribute.cpp b/c++/src/H5Attribute.cpp index 226ae5c..120ed39 100644 --- a/c++/src/H5Attribute.cpp +++ b/c++/src/H5Attribute.cpp @@ -385,6 +385,31 @@ hsize_t Attribute::getStorageSize() const } //-------------------------------------------------------------------------- +// Function: Attribute::flush +///\brief Flushes all buffers associated with a file specified by +/// this attribute, to disk. +///\param scope - IN: Specifies the scope of the flushing action, +/// which can be either of these values: +/// \li \c H5F_SCOPE_GLOBAL - Flushes the entire virtual file +/// \li \c H5F_SCOPE_LOCAL - Flushes only the specified file +///\exception H5::AttributeIException +///\par Description +/// This attribute is used to identify the file to be flushed. +// Programmer Binh-Minh Ribler - 2012 +// Modification +// Sep 2012 - BMR +// Duplicated from H5Location +//-------------------------------------------------------------------------- +void Attribute::flush(H5F_scope_t scope) const +{ + herr_t ret_value = H5Fflush(getId(), scope); + if( ret_value < 0 ) + { + throw AttributeIException("Attribute::flush", "H5Fflush failed"); + } +} + +//-------------------------------------------------------------------------- // Function: Attribute::getId // Purpose: Get the id of this attribute // Description: diff --git a/c++/src/H5Attribute.h b/c++/src/H5Attribute.h index 284e5bc..ff4dac6 100644 --- a/c++/src/H5Attribute.h +++ b/c++/src/H5Attribute.h @@ -51,6 +51,10 @@ class H5_DLLCPP Attribute : public AbstractDs, public IdComponent { void write(const DataType& mem_type, const void *buf ) const; void write(const DataType& mem_type, const H5std_string& strg ) const; + // Flushes all buffers associated with the file specified by this + // attribute to disk + void flush( H5F_scope_t scope ) const; + // Returns this class name virtual H5std_string fromClass () const { return("Attribute"); } diff --git a/c++/src/H5Classes.h b/c++/src/H5Classes.h index f691548..c3b61ca 100644 --- a/c++/src/H5Classes.h +++ b/c++/src/H5Classes.h @@ -22,6 +22,7 @@ namespace H5 { #endif class Exception; class IdComponent; + class H5Location; class H5Object; class PropList; class FileCreatPropList; diff --git a/c++/src/H5Cpp.h b/c++/src/H5Cpp.h index 75d82ba..ddf4f19 100644 --- a/c++/src/H5Cpp.h +++ b/c++/src/H5Cpp.h @@ -22,6 +22,7 @@ #include "H5IdComponent.h" #include "H5DataSpace.h" #include "H5PropList.h" +#include "H5Location.h" #include "H5Object.h" #include "H5AbstractDs.h" #include "H5Attribute.h" diff --git a/c++/src/H5DataSet.cpp b/c++/src/H5DataSet.cpp index f7aaa72..624aa6f 100644 --- a/c++/src/H5DataSet.cpp +++ b/c++/src/H5DataSet.cpp @@ -644,33 +644,6 @@ void DataSet::fillMemBuf(void *buf, DataType& buf_type, DataSpace& space) } } -#ifndef H5_NO_DEPRECATED_SYMBOLS -//-------------------------------------------------------------------------- -// Function: DataSet::getObjType -///\brief Retrieves the type of object that an object reference points to. -///\param ref_type - IN: Type of reference to query, valid values are: -/// \li \c H5R_OBJECT - Reference is an object reference. -/// \li \c H5R_DATASET_REGION - Reference is a dataset region reference. -///\param ref - IN: Reference to query -///\return An object type, which can be one of the following: -/// \li \c H5G_LINK (0) - Object is a symbolic link. -/// \li \c H5G_GROUP (1) - Object is a group. -/// \li \c H5G_DATASET (2) - Object is a dataset. -/// \li \c H5G_TYPE (3) - Object is a named datatype -///\exception H5::DataSetIException -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -H5G_obj_t DataSet::getObjType(void *ref, H5R_type_t ref_type) const -{ - try { - return(p_get_obj_type(ref, ref_type)); - } - catch (IdComponentException E) { - throw DataSetIException("DataSet::getObjType", E.getDetailMsg()); - } -} -#endif /* H5_NO_DEPRECATED_SYMBOLS */ - //-------------------------------------------------------------------------- // Function: DataSet::getRegion ///\brief Retrieves a dataspace with the region pointed to selected. diff --git a/c++/src/H5DataSet.h b/c++/src/H5DataSet.h index 3d9183d..406f7e2 100644 --- a/c++/src/H5DataSet.h +++ b/c++/src/H5DataSet.h @@ -76,11 +76,6 @@ class H5_DLLCPP DataSet : public H5Object, public AbstractDs { // Iterates the selected elements in the specified dataspace - not implemented in C++ style yet int iterateElems( void* buf, const DataType& type, const DataSpace& space, H5D_operator_t op, void* op_data = NULL ); -#ifndef H5_NO_DEPRECATED_SYMBOLS - // Retrieves the type of object that an object reference points to. - H5G_obj_t getObjType(void *ref, H5R_type_t ref_type = H5R_OBJECT) const; -#endif /* H5_NO_DEPRECATED_SYMBOLS */ - // Retrieves a dataspace with the region pointed to selected. DataSpace getRegion(void *ref, H5R_type_t ref_type = H5R_DATASET_REGION) const; diff --git a/c++/src/H5DataType.cpp b/c++/src/H5DataType.cpp index 3edb163..7533da2 100644 --- a/c++/src/H5DataType.cpp +++ b/c++/src/H5DataType.cpp @@ -679,31 +679,6 @@ bool DataType::isVariableStr() const } } -#ifndef H5_NO_DEPRECATED_SYMBOLS -//-------------------------------------------------------------------------- -// Function: DataType::getObjType -///\brief Retrieves the type of object that an object reference points to. -///\param ref - IN: Reference to query -///\param ref_type - IN: Type of reference to query -///\return Object type, which can be one of the following: -/// \li \c H5G_LINK Object is a symbolic link. -/// \li \c H5G_GROUP Object is a group. -/// \li \c H5G_DATASET Object is a dataset. -/// \li \c H5G_TYPE Object is a named datatype -///\exception H5::DataTypeIException -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -H5G_obj_t DataType::getObjType(void *ref, H5R_type_t ref_type) const -{ - try { - return(p_get_obj_type(ref, ref_type)); - } - catch (IdComponentException E) { - throw DataTypeIException(inMemFunc("getObjType"), E.getDetailMsg()); - } -} -#endif /* H5_NO_DEPRECATED_SYMBOLS */ - //-------------------------------------------------------------------------- // Function: DataType::getRegion ///\brief Retrieves a dataspace with the region pointed to selected. diff --git a/c++/src/H5DataType.h b/c++/src/H5DataType.h index 48aeaf8..258bdc7 100644 --- a/c++/src/H5DataType.h +++ b/c++/src/H5DataType.h @@ -101,11 +101,6 @@ class H5_DLLCPP DataType : public H5Object { // Checks whether this datatype is a variable-length string. bool isVariableStr() const; -#ifndef H5_NO_DEPRECATED_SYMBOLS - // Retrieves the type of object that an object reference points to. - H5G_obj_t getObjType(void *ref, H5R_type_t ref_type = H5R_OBJECT) const; -#endif /* H5_NO_DEPRECATED_SYMBOLS */ - // Retrieves a dataspace with the region pointed to selected. DataSpace getRegion(void *ref, H5R_type_t ref_type = H5R_DATASET_REGION) const; diff --git a/c++/src/H5File.cpp b/c++/src/H5File.cpp index da0241f..d023001 100644 --- a/c++/src/H5File.cpp +++ b/c++/src/H5File.cpp @@ -50,7 +50,7 @@ namespace H5 { ///\brief Default constructor: creates a stub H5File object. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -H5File::H5File() : IdComponent(), id(0) {} +H5File::H5File() : H5Location(0) {} //-------------------------------------------------------------------------- // Function: H5File overloaded constructor @@ -79,7 +79,7 @@ H5File::H5File() : IdComponent(), id(0) {} /// http://www.hdfgroup.org/HDF5/doc/RM/RM_H5F.html#File-Create // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -H5File::H5File( const char* name, unsigned int flags, const FileCreatPropList& create_plist, const FileAccPropList& access_plist ) : IdComponent(0) +H5File::H5File( const char* name, unsigned int flags, const FileCreatPropList& create_plist, const FileAccPropList& access_plist ) : H5Location(0) { p_get_file(name, flags, create_plist, access_plist); } @@ -97,7 +97,7 @@ H5File::H5File( const char* name, unsigned int flags, const FileCreatPropList& c /// FileCreatPropList::DEFAULT // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -H5File::H5File( const H5std_string& name, unsigned int flags, const FileCreatPropList& create_plist, const FileAccPropList& access_plist ) : IdComponent(0) +H5File::H5File( const H5std_string& name, unsigned int flags, const FileCreatPropList& create_plist, const FileAccPropList& access_plist ) : H5Location(0) { p_get_file(name.c_str(), flags, create_plist, access_plist); } @@ -140,32 +140,13 @@ void H5File::p_get_file(const char* name, unsigned int flags, const FileCreatPro ///\param original - IN: H5File instance to copy // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -H5File::H5File(const H5File& original) : IdComponent(original) +H5File::H5File(const H5File& original) : H5Location(original) { id = original.getId(); incRefCount(); // increment number of references to this id } //-------------------------------------------------------------------------- -// Function: H5File::flush -///\brief Flushes all buffers associated with a file to disk. -///\param scope - IN: Specifies the scope of the flushing action, -/// which can be either of these values: -/// \li \c H5F_SCOPE_GLOBAL - Flushes the entire virtual file -/// \li \c H5F_SCOPE_LOCAL - Flushes only the specified file -///\exception H5::FileIException -// Programmer Binh-Minh Ribler - Dec. 2005 -//-------------------------------------------------------------------------- -void H5File::flush(H5F_scope_t scope) const -{ - herr_t ret_value = H5Fflush( id, scope ); - if( ret_value < 0 ) - { - throw FileIException("H5File::flush", "H5Fflush failed"); - } -} - -//-------------------------------------------------------------------------- // Function: H5File::isHdf5 ///\brief Determines whether a file in HDF5 format. ///\param name - IN: Name of the file @@ -489,49 +470,6 @@ void H5File::getVFDHandle(void **file_handle) const } } -//-------------------------------------------------------------------------- -// Function: H5File::getFileName -///\brief Gets the name of this file. -///\return File name -///\exception H5::FileIException -// Programmer Binh-Minh Ribler - Jul, 2004 -//-------------------------------------------------------------------------- -H5std_string H5File::getFileName() const -{ - try { - return(p_get_file_name()); - } - catch (IdComponentException E) { - throw FileIException("H5File::getFileName", E.getDetailMsg()); - } -} - -#ifndef H5_NO_DEPRECATED_SYMBOLS -//-------------------------------------------------------------------------- -// Function: H5File::getObjType -///\brief Retrieves the type of object that an object reference points to. -///\param ref - IN: Reference to query -///\param ref_type - IN: Type of reference, valid values are: -/// \li \c H5R_OBJECT - Reference is an object reference -/// \li \c H5R_DATASET_REGION - Reference is a dataset region reference -///\return Object type, which can be one of the following: -/// \li \c H5G_LINK - Object is a symbolic link. -/// \li \c H5G_GROUP - Object is a group. -/// \li \c H5G_DATASET - Object is a dataset. -/// \li \c H5G_TYPE - Object is a named datatype -///\exception H5::FileIException -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -H5G_obj_t H5File::getObjType(void *ref, H5R_type_t ref_type) const -{ - try { - return(p_get_obj_type(ref, ref_type)); - } - catch (IdComponentException E) { - throw FileIException("H5File::getObjType", E.getDetailMsg()); - } -} -#endif /* H5_NO_DEPRECATED_SYMBOLS */ //-------------------------------------------------------------------------- // Function: H5File::getRegion @@ -593,93 +531,6 @@ void H5File::p_reference(void* ref, const char* name, hid_t space_id, H5R_type_t } } -//-------------------------------------------------------------------------- -// Function: H5File::reference -///\brief Creates a reference to an HDF5 object or a dataset region. -///\param ref - IN: Reference pointer -///\param name - IN: Name of the object to be referenced -///\param dataspace - IN: Dataspace with selection -///\param ref_type - IN: Type of reference to query, valid values are: -/// \li \c H5R_OBJECT - Reference is an object reference -/// \li \c H5R_DATASET_REGION - Reference is a dataset region -/// reference - this is the default -///\exception H5::IdComponentException -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void H5File::reference(void* ref, const char* name, const DataSpace& dataspace, H5R_type_t ref_type) const -{ - try { - p_reference(ref, name, dataspace.getId(), ref_type); - } - catch (IdComponentException E) { - throw IdComponentException("H5File::reference", E.getDetailMsg()); - } -} - -//-------------------------------------------------------------------------- -// Function: H5File::reference -///\brief This is an overloaded function, provided for your convenience. -/// It differs from the above function in that it only creates -/// a reference to an HDF5 object, not to a dataset region. -///\param ref - IN: Reference pointer -///\param name - IN: Name of the object to be referenced - \c char pointer -///\exception H5::IdComponentException -///\par Description -// This function passes H5R_OBJECT and -1 to the protected -// function for it to pass to the C API H5Rcreate -// to create a reference to the named object. -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void H5File::reference(void* ref, const char* name) const -{ - try { - p_reference(ref, name, -1, H5R_OBJECT); - } - catch (IdComponentException E) { - throw IdComponentException("H5File::reference", E.getDetailMsg()); - } -} -//-------------------------------------------------------------------------- -// Function: H5File::reference -///\brief This is an overloaded function, provided for your convenience. -/// It differs from the above function in that it takes an -/// \c H5std_string for the object's name. -///\param ref - IN: Reference pointer -///\param name - IN: Name of the object to be referenced - \c H5std_string -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void H5File::reference(void* ref, const H5std_string& name) const -{ - reference(ref, name.c_str()); -} - -#ifndef H5_NO_DEPRECATED_SYMBOLS -//-------------------------------------------------------------------------- -// Function: H5File::p_get_obj_type (protected) -// Purpose Retrieves the type of object that an object reference points to. -// Parameters -// ref - IN: Reference to query -// ref_type - IN: Type of reference to query -// Return An object type, which can be one of the following: -// H5G_LINK Object is a symbolic link. -// H5G_GROUP Object is a group. -// H5G_DATASET Object is a dataset. -// H5G_TYPE Object is a named datatype -// Exception H5::IdComponentException -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -H5G_obj_t H5File::p_get_obj_type(void *ref, H5R_type_t ref_type) const -{ - H5G_obj_t obj_type = H5Rget_obj_type1(getId(), ref_type, ref); - - if (obj_type == H5G_UNKNOWN) - { - throw IdComponentException("", "H5Rget_obj_type failed"); - } - return(obj_type); -} -#endif /* H5_NO_DEPRECATED_SYMBOLS */ - //-------------------------------------------------------------------------- // Function: H5File::p_get_region (protected) diff --git a/c++/src/H5File.h b/c++/src/H5File.h index b69c963..ad1dee7 100644 --- a/c++/src/H5File.h +++ b/c++/src/H5File.h @@ -21,7 +21,7 @@ namespace H5 { #endif -class H5_DLLCPP H5File : public IdComponent, public CommonFG { +class H5_DLLCPP H5File : public H5Location, public CommonFG { public: // Creates or opens an HDF5 file. H5File( const char* name, unsigned int flags, @@ -40,18 +40,12 @@ class H5_DLLCPP H5File : public IdComponent, public CommonFG { // Close this file. virtual void close(); - // Flushes all buffers associated with this file to disk - void flush(H5F_scope_t scope) const; - // Gets the access property list of this file. FileAccPropList getAccessPlist() const; // Gets the creation property list of this file. FileCreatPropList getCreatePlist() const; - // Gets the name of this file. - H5std_string getFileName() const; - // Retrieves the file size of an opened file. hsize_t getFileSize() const; @@ -67,11 +61,6 @@ class H5_DLLCPP H5File : public IdComponent, public CommonFG { // and datatypes) in the same file. void getObjIDs(unsigned types, size_t max_objs, hid_t *oid_list) const; -#ifndef H5_NO_DEPRECATED_SYMBOLS - // Retrieves the type of object that an object reference points to. - H5G_obj_t getObjType(void *ref, H5R_type_t ref_type = H5R_OBJECT) const; -#endif /* H5_NO_DEPRECATED_SYMBOLS */ - // Retrieves a dataspace with the region pointed to selected. DataSpace getRegion(void *ref, H5R_type_t ref_type = H5R_DATASET_REGION) const; @@ -87,13 +76,6 @@ class H5_DLLCPP H5File : public IdComponent, public CommonFG { void reOpen(); // added for better name void reopen(); - // Creates a reference to a named HDF5 object or to a dataset region - // in this object. - void reference(void* ref, const char* name, const DataSpace& dataspace, - H5R_type_t ref_type = H5R_DATASET_REGION) const; - void reference(void* ref, const char* name) const; - void reference(void* ref, const H5std_string& name) const; - // Returns this class name virtual H5std_string fromClass () const { return("H5File"); } diff --git a/c++/src/H5Group.cpp b/c++/src/H5Group.cpp index 6ec2dbb..f620310 100644 --- a/c++/src/H5Group.cpp +++ b/c++/src/H5Group.cpp @@ -147,33 +147,6 @@ Group::Group(Attribute& attr, const void* ref, H5R_type_t ref_type) : H5Object() } } -#ifndef H5_NO_DEPRECATED_SYMBOLS -//-------------------------------------------------------------------------- -// Function: Group::getObjType -///\brief Retrieves the type of object that an object reference points to. -///\param ref - IN: Reference to query -///\param ref_type - IN: Type of reference to query, valid values are: -/// \li \c H5R_OBJECT - Reference is an object reference. -/// \li \c H5R_DATASET_REGION - Reference is a dataset region reference. -///\return An object type, which can be one of the following: -/// \li \c H5G_LINK (0) - Object is a symbolic link. -/// \li \c H5G_GROUP (1) - Object is a group. -/// \li \c H5G_DATASET (2) - Object is a dataset. -/// \li \c H5G_TYPE (3) - Object is a named datatype -///\exception H5::GroupIException -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -H5G_obj_t Group::getObjType(void *ref, H5R_type_t ref_type) const -{ - try { - return(p_get_obj_type(ref, ref_type)); - } - catch (IdComponentException E) { - throw GroupIException("Group::getObjType", E.getDetailMsg()); - } -} -#endif /* H5_NO_DEPRECATED_SYMBOLS */ - //-------------------------------------------------------------------------- // Function: Group::getRegion ///\brief Retrieves a dataspace with the region pointed to selected. diff --git a/c++/src/H5Group.h b/c++/src/H5Group.h index 9978cf0..c4213a8 100644 --- a/c++/src/H5Group.h +++ b/c++/src/H5Group.h @@ -26,11 +26,6 @@ class H5_DLLCPP Group : public H5Object, public CommonFG { // Close this group. virtual void close(); -#ifndef H5_NO_DEPRECATED_SYMBOLS - // Retrieves the type of object that an object reference points to. - H5G_obj_t getObjType(void *ref, H5R_type_t ref_type = H5R_OBJECT) const; -#endif /* H5_NO_DEPRECATED_SYMBOLS */ - // Retrieves a dataspace with the region pointed to selected. DataSpace getRegion(void *ref, H5R_type_t ref_type = H5R_DATASET_REGION) const; diff --git a/c++/src/H5Location.cpp b/c++/src/H5Location.cpp new file mode 100644 index 0000000..b93cd86 --- /dev/null +++ b/c++/src/H5Location.cpp @@ -0,0 +1,669 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * Copyright by The HDF Group. * + * Copyright by the Board of Trustees of the University of Illinois. * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include + +#include "H5Include.h" +#include "H5Exception.h" +#include "H5IdComponent.h" +#include "H5PropList.h" +#include "H5Location.h" +#include "H5Object.h" +#include "H5DcreatProp.h" +#include "H5DxferProp.h" +#include "H5FaccProp.h" +#include "H5FcreatProp.h" +#include "H5CommonFG.h" +#include "H5DataType.h" +#include "H5DataSpace.h" +#include "H5AbstractDs.h" +#include "H5File.h" +#include "H5DataSet.h" +#include "H5Attribute.h" + +#ifndef H5_NO_NAMESPACE +namespace H5 { +#endif + +#ifndef DOXYGEN_SHOULD_SKIP_THIS +// userAttrOpWrpr simply interfaces between the user's function and the +// C library function H5Aiterate2; used to resolve the different prototype +// problem. May be moved to Iterator later. +extern "C" herr_t userAttrOpWrpr(hid_t loc_id, const char *attr_name, + const H5A_info_t *ainfo, void *op_data) +{ + H5std_string s_attr_name = H5std_string( attr_name ); +#ifdef NO_STATIC_CAST + UserData4Aiterate* myData = (UserData4Aiterate *) op_data; +#else + UserData4Aiterate* myData = static_cast (op_data); +#endif + myData->op( *myData->location, s_attr_name, myData->opData ); + return 0; +} + +//-------------------------------------------------------------------------- +// Function: H5Location default constructor (protected) +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +H5Location::H5Location() : IdComponent(0) {} + +//-------------------------------------------------------------------------- +// Function: H5Location overloaded constructor (protected) +// Purpose Creates an H5Location object using the id of an existing HDF5 +// object. +// Parameters object_id - IN: Id of an existing HDF5 object +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +H5Location::H5Location(const hid_t object_id) : IdComponent(object_id) {} + +#endif // DOXYGEN_SHOULD_SKIP_THIS + +//-------------------------------------------------------------------------- +// Function: H5Location copy constructor +///\brief Copy constructor: makes a copy of the original H5Location +/// instance. +///\param original - IN: H5Location instance to copy +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +H5Location::H5Location( const H5Location& original ) : IdComponent( original ) {} + +//-------------------------------------------------------------------------- +// Function: H5Location::createAttribute +///\brief Creates an attribute for a group, dataset, or named datatype. +///\param name - IN: Name of the attribute +///\param data_type - IN: Datatype for the attribute +///\param data_space - IN: Dataspace for the attribute - only simple +/// dataspaces are allowed at this time +///\param create_plist - IN: Creation property list - default to +/// PropList::DEFAULT +///\return Attribute instance +///\exception H5::AttributeIException +///\par Description +/// The attribute name specified in \a name must be unique. +/// Attempting to create an attribute with the same name as an +/// existing attribute will raise an exception, leaving the +/// pre-existing attribute intact. To overwrite an existing +/// attribute with a new attribute of the same name, first +/// delete the existing one with \c H5Location::removeAttr, then +/// recreate it with this function. +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +Attribute H5Location::createAttribute( const char* name, const DataType& data_type, const DataSpace& data_space, const PropList& create_plist ) const +{ + hid_t type_id = data_type.getId(); + hid_t space_id = data_space.getId(); + hid_t plist_id = create_plist.getId(); + hid_t attr_id = H5Acreate2(getId(), name, type_id, space_id, plist_id, H5P_DEFAULT ); + + // If the attribute id is valid, create and return the Attribute object + if( attr_id > 0 ) + { + Attribute attr( attr_id ); + return( attr ); + } + else + throw AttributeIException(inMemFunc("createAttribute"), "H5Acreate2 failed"); +} + +//-------------------------------------------------------------------------- +// Function: H5Location::createAttribute +///\brief This is an overloaded member function, provided for convenience. +/// It differs from the above function in that it takes +/// a reference to an \c H5std_string for \a name. +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +Attribute H5Location::createAttribute( const H5std_string& name, const DataType& data_type, const DataSpace& data_space, const PropList& create_plist ) const +{ + return( createAttribute( name.c_str(), data_type, data_space, create_plist )); +} + +//-------------------------------------------------------------------------- +// Function: H5Location::openAttribute +///\brief Opens an attribute given its name. +///\param name - IN: Name of the attribute +///\return Attribute instance +///\exception H5::AttributeIException +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +Attribute H5Location::openAttribute( const char* name ) const +{ + hid_t attr_id = H5Aopen(getId(), name, H5P_DEFAULT); + if( attr_id > 0 ) + { + Attribute attr( attr_id ); + return( attr ); + } + else + { + throw AttributeIException(inMemFunc("openAttribute"), "H5Aopen failed"); + } +} + +//-------------------------------------------------------------------------- +// Function: H5Location::openAttribute +///\brief This is an overloaded member function, provided for convenience. +/// It differs from the above function in that it takes +/// a reference to an \c H5std_string for \a name. +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +Attribute H5Location::openAttribute( const H5std_string& name ) const +{ + return( openAttribute( name.c_str()) ); +} + +//-------------------------------------------------------------------------- +// Function: H5Location::openAttribute +///\brief Opens an attribute given its index. +///\param idx - IN: Index of the attribute, a 0-based, non-negative integer +///\return Attribute instance +///\exception H5::AttributeIException +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +Attribute H5Location::openAttribute( const unsigned int idx ) const +{ + hid_t attr_id = H5Aopen_by_idx(getId(), ".", H5_INDEX_CRT_ORDER, + H5_ITER_INC, (hsize_t)idx, H5P_DEFAULT, H5P_DEFAULT); + if( attr_id > 0 ) + { + Attribute attr( attr_id ); + return( attr ); + } + else + { + throw AttributeIException(inMemFunc("openAttribute"), "H5Aopen_by_idx failed"); + } +} + +//-------------------------------------------------------------------------- +// Function: H5Location::iterateAttrs +///\brief Iterates a user's function over all the attributes of an H5 +/// object, which may be a group, dataset or named datatype. +///\param user_op - IN: User's function to operate on each attribute +///\param _idx - IN/OUT: Starting (IN) and ending (OUT) attribute indices +///\param op_data - IN: User's data to pass to user's operator function +///\return Returned value of the last operator if it was non-zero, or +/// zero if all attributes were processed +///\exception H5::AttributeIException +///\par Description +/// The signature of user_op is +/// void (*)(H5::H5Location&, H5std_string, void*). +/// For information, please refer to the C layer Reference Manual +/// at: +/// http://www.hdfgroup.org/HDF5/doc/RM/RM_H5A.html#Annot-Iterate +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +int H5Location::iterateAttrs( attr_operator_t user_op, unsigned *_idx, void *op_data ) +{ + // store the user's function and data + UserData4Aiterate* userData = new UserData4Aiterate; + userData->opData = op_data; + userData->op = user_op; + userData->location = this; + + // call the C library routine H5Aiterate2 to iterate the attributes + hsize_t idx = _idx ? (hsize_t)*_idx : 0; + int ret_value = H5Aiterate2(getId(), H5_INDEX_NAME, H5_ITER_INC, &idx, + userAttrOpWrpr, (void *) userData); + + // release memory + delete userData; + + if( ret_value >= 0 ) { + /* Pass back update index value to calling code */ + if (_idx) + *_idx = (unsigned)idx; + + return( ret_value ); + } + else // raise exception when H5Aiterate returns a negative value + throw AttributeIException(inMemFunc("iterateAttrs"), "H5Aiterate2 failed"); +} + +//-------------------------------------------------------------------------- +// Function: H5Location::getNumAttrs +///\brief Returns the number of attributes attached to this HDF5 object. +///\return Number of attributes +///\exception H5::AttributeIException +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +int H5Location::getNumAttrs() const +{ + H5O_info_t oinfo; /* Object info */ + + if(H5Oget_info(getId(), &oinfo) < 0) + throw AttributeIException(inMemFunc("getNumAttrs"), "H5Oget_info failed"); + else + return( (int)oinfo.num_attrs ); +} + +//-------------------------------------------------------------------------- +// Function: H5Location::removeAttr +///\brief Removes the named attribute from this object. +///\param name - IN: Name of the attribute to be removed +///\exception H5::AttributeIException +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +void H5Location::removeAttr( const char* name ) const +{ + herr_t ret_value = H5Adelete(getId(), name); + if( ret_value < 0 ) + throw AttributeIException(inMemFunc("removeAttr"), "H5Adelete failed"); +} + +//-------------------------------------------------------------------------- +// Function: H5Location::removeAttr +///\brief This is an overloaded member function, provided for convenience. +/// It differs from the above function in that it takes +/// a reference to an \c H5std_string for \a name. +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +void H5Location::removeAttr( const H5std_string& name ) const +{ + removeAttr( name.c_str() ); +} + +//-------------------------------------------------------------------------- +// Function: H5Location::renameAttr +///\brief Renames the named attribute from this object. +///\param oldname - IN: Name of the attribute to be renamed +///\param newname - IN: New name ame of the attribute +///\exception H5::AttributeIException +// Programmer Binh-Minh Ribler - Mar, 2005 +//-------------------------------------------------------------------------- +void H5Location::renameAttr(const char* oldname, const char* newname) const +{ + herr_t ret_value = H5Arename(getId(), oldname, newname); + if (ret_value < 0) + throw AttributeIException(inMemFunc("renameAttr"), "H5Arename failed"); +} + +//-------------------------------------------------------------------------- +// Function: H5Location::renameAttr +///\brief This is an overloaded member function, provided for convenience. +/// It differs from the above function in that it takes +/// a reference to an \c H5std_string for the names. +// Programmer Binh-Minh Ribler - Mar, 2005 +//-------------------------------------------------------------------------- +void H5Location::renameAttr(const H5std_string& oldname, const H5std_string& newname) const +{ + renameAttr (oldname.c_str(), newname.c_str()); +} + +//-------------------------------------------------------------------------- +// Function: H5Location::flush +///\brief Flushes all buffers associated with a location to disk. +///\param scope - IN: Specifies the scope of the flushing action, +/// which can be either of these values: +/// \li \c H5F_SCOPE_GLOBAL - Flushes the entire virtual file +/// \li \c H5F_SCOPE_LOCAL - Flushes only the specified file +///\exception H5::FileIException +///\par Description +/// This location is used to identify the file to be flushed. +// Programmer Binh-Minh Ribler - 2012 +// Modification +// Sep 2012 - BMR +// Moved from H5File/H5Object +//-------------------------------------------------------------------------- +void H5Location::flush(H5F_scope_t scope) const +{ + herr_t ret_value = H5Fflush(getId(), scope); + if( ret_value < 0 ) + { + throw FileIException(inMemFunc("flush"), "H5Fflush failed"); + } +} + +//-------------------------------------------------------------------------- +// Function: H5Location::getFileName +///\brief Gets the name of the file, in which this HDF5 object belongs. +///\return File name +///\exception H5::IdComponentException +// Programmer Binh-Minh Ribler - Jul, 2004 +//-------------------------------------------------------------------------- +H5std_string H5Location::getFileName() const +{ + try { + return(p_get_file_name()); + } + catch (IdComponentException E) { + throw FileIException(inMemFunc("getFileName"), E.getDetailMsg()); + } +} + +//-------------------------------------------------------------------------- +// Function: H5Location::p_reference (protected) +// Purpose Creates a reference to an HDF5 object or a dataset region. +// Parameters +// name - IN: Name of the object to be referenced +// dataspace - IN: Dataspace with selection +// ref_type - IN: Type of reference; default to \c H5R_DATASET_REGION +// Exception H5::IdComponentException +// Programmer Binh-Minh Ribler - May, 2004 +//-------------------------------------------------------------------------- +void H5Location::p_reference(void* ref, const char* name, hid_t space_id, H5R_type_t ref_type) const +{ + herr_t ret_value = H5Rcreate(ref, getId(), name, ref_type, space_id); + if (ret_value < 0) + { + throw ReferenceException("", "H5Rcreate failed"); + } +} + +//-------------------------------------------------------------------------- +// Function: H5Location::reference +///\brief Creates a reference to an HDF5 object or a dataset region. +///\param ref - IN: Reference pointer +///\param name - IN: Name of the object to be referenced +///\param dataspace - IN: Dataspace with selection +///\param ref_type - IN: Type of reference to query, valid values are: +/// \li \c H5R_OBJECT - Reference is an object reference. +/// \li \c H5R_DATASET_REGION - Reference is a dataset region +/// reference. - this is the default +///\exception H5::ReferenceException +// Programmer Binh-Minh Ribler - May, 2004 +//-------------------------------------------------------------------------- +void H5Location::reference(void* ref, const char* name, const DataSpace& dataspace, H5R_type_t ref_type) const +{ + try { + p_reference(ref, name, dataspace.getId(), ref_type); + } + catch (ReferenceException E) { + throw ReferenceException("H5Location::reference", E.getDetailMsg()); + } +} + +//-------------------------------------------------------------------------- +// Function: H5Location::reference +///\brief This is an overloaded function, provided for your convenience. +/// It differs from the above function in that it only creates +/// a reference to an HDF5 object, not to a dataset region. +///\param ref - IN: Reference pointer +///\param name - IN: Name of the object to be referenced - \c char pointer +///\exception H5::ReferenceException +///\par Description +// This function passes H5R_OBJECT and -1 to the protected +// function for it to pass to the C API H5Rcreate +// to create a reference to the named object. +// Programmer Binh-Minh Ribler - May, 2004 +//-------------------------------------------------------------------------- +void H5Location::reference(void* ref, const char* name) const +{ + try { + p_reference(ref, name, -1, H5R_OBJECT); + } + catch (ReferenceException E) { + throw ReferenceException("H5Location::reference", E.getDetailMsg()); + } +} + +//-------------------------------------------------------------------------- +// Function: H5Location::reference +///\brief This is an overloaded function, provided for your convenience. +/// It differs from the above function in that it takes an +/// \c H5std_string for the object's name. +///\param ref - IN: Reference pointer +///\param name - IN: Name of the object to be referenced - \c H5std_string +// Programmer Binh-Minh Ribler - May, 2004 +//-------------------------------------------------------------------------- +void H5Location::reference(void* ref, const H5std_string& name) const +{ + reference(ref, name.c_str()); +} + +//-------------------------------------------------------------------------- +// Function: H5Location::p_dereference (protected) +// Purpose Dereference a ref into an hdf5 object. +// Parameters +// loc_id - IN: An hdf5 identifier specifying the location of the +// referenced object +// ref - IN: Reference pointer +// ref_type - IN: Reference type +// Exception H5::ReferenceException +// Programmer Binh-Minh Ribler - Oct, 2006 +// Modification +// May 2008 - BMR +// Moved from IdComponent. +//-------------------------------------------------------------------------- +hid_t H5Location::p_dereference(hid_t loc_id, const void* ref, H5R_type_t ref_type) +{ + hid_t temp_id; + temp_id = H5Rdereference2(loc_id, H5P_DEFAULT, ref_type, ref); + if (temp_id < 0) + { + throw ReferenceException("", "H5Rdereference failed"); + } + + // No failure, set id to the object + return(temp_id); +} + +//-------------------------------------------------------------------------- +// Function: H5Location::dereference +///\brief Dereferences a reference into an HDF5 object, given an HDF5 object. +///\param obj - IN: Object specifying the location of the referenced object +///\param ref - IN: Reference pointer +///\param ref_type - IN: Reference type +///\exception H5::ReferenceException +// Programmer Binh-Minh Ribler - Oct, 2006 +// Modification +// May, 2008 +// Corrected missing parameters. - BMR +//-------------------------------------------------------------------------- +void H5Location::dereference(H5Object& obj, const void* ref, H5R_type_t ref_type) +{ + hid_t temp_id; + try { + temp_id = p_dereference(obj.getId(), ref, ref_type); + } + catch (ReferenceException E) { + throw ReferenceException("H5Location::dereference - located by object", E.getDetailMsg()); + } + p_setId(temp_id); +} + +//-------------------------------------------------------------------------- +// Function: H5Location::dereference +///\brief Dereferences a reference into an HDF5 object, given an HDF5 file. +///\param h5file - IN: HDF5 file specifying the location of the referenced object +///\param ref - IN: Reference pointer +///\param ref_type - IN: Reference type +///\exception H5::ReferenceException +// Programmer Binh-Minh Ribler - Oct, 2006 +// Modification +// May, 2008 +// Corrected missing parameters. - BMR +//-------------------------------------------------------------------------- +void H5Location::dereference(H5File& h5file, const void* ref, H5R_type_t ref_type) +{ + hid_t temp_id; + try { + temp_id = p_dereference(h5file.getId(), ref, ref_type); + } + catch (ReferenceException E) { + throw ReferenceException("H5Location::dereference - located by file", E.getDetailMsg()); + } + p_setId(temp_id); +} + +//-------------------------------------------------------------------------- +// Function: H5Location::dereference +///\brief Dereferences a reference into an HDF5 object, given an attribute. +///\param attr - IN: Attribute specifying the location of the referenced object +///\param ref - IN: Reference pointer +///\param ref_type - IN: Reference type +///\exception H5::ReferenceException +// Programmer Binh-Minh Ribler - Oct, 2006 +// Modification +// May, 2008 +// Corrected missing parameters. - BMR +//-------------------------------------------------------------------------- +void H5Location::dereference(Attribute& attr, const void* ref, H5R_type_t ref_type) +{ + hid_t temp_id; + try { + temp_id = p_dereference(attr.getId(), ref, ref_type); + } + catch (ReferenceException E) { + throw ReferenceException("H5Location::dereference - located by attribute", E.getDetailMsg()); + } + p_setId(temp_id); +} + +#ifndef H5_NO_DEPRECATED_SYMBOLS +//-------------------------------------------------------------------------- +// Function: H5Location::getObjType +///\brief Retrieves the type of object that an object reference points to. +///\param ref_type - IN: Type of reference to query, valid values are: +/// \li \c H5R_OBJECT \tReference is an object reference. +/// \li \c H5R_DATASET_REGION \tReference is a dataset region reference. +///\param ref - IN: Reference to query +///\return An object type, which can be one of the following: +/// \li \c H5G_UNKNOWN \tA failure occurs. (-1) +/// \li \c H5G_GROUP \tObject is a group. +/// \li \c H5G_DATASET \tObject is a dataset. +/// \li \c H5G_TYPE Object \tis a named datatype +/// \li \c H5G_LINK \tObject is a symbolic link. +/// \li \c H5G_UDLINK \tObject is a user-defined link. +///\exception H5::ReferenceException +// Programmer Binh-Minh Ribler - May, 2004 +// Modification +// Sep 2012: Moved up from H5File, Group, DataSet, and DataType +//-------------------------------------------------------------------------- +H5G_obj_t H5Location::getObjType(void *ref, H5R_type_t ref_type) const +{ + try { + return(p_get_obj_type(ref, ref_type)); + } + catch (ReferenceException E) { + throw ReferenceException("H5Location::getObjType", E.getDetailMsg()); + } +} + +//-------------------------------------------------------------------------- +// Function: H5Location::p_get_obj_type (protected) +// Purpose Retrieves the type of object that an object reference points to. +// Parameters +// ref - IN: Reference to query +// ref_type - IN: Type of reference to query +// Return An object type, which can be one of the following: +// H5G_UNKNOWN \tFailure occurs (-1) +// H5G_GROUP \tObject is a group. +// H5G_DATASET \tObject is a dataset. +// H5G_TYPE Object \tis a named datatype. +// H5G_LINK \tObject is a symbolic link. +// H5G_UDLINK \tObject is a user-defined link. +// Exception H5::ReferenceException +// Programmer Binh-Minh Ribler - May, 2004 +//-------------------------------------------------------------------------- +H5G_obj_t H5Location::p_get_obj_type(void *ref, H5R_type_t ref_type) const +{ + H5G_obj_t obj_type = H5Rget_obj_type1(getId(), ref_type, ref); + + if (obj_type == H5G_UNKNOWN) + { + throw ReferenceException("", "H5Rget_obj_type1 failed"); + } + return(obj_type); +} +#endif /* H5_NO_DEPRECATED_SYMBOLS */ + + +//-------------------------------------------------------------------------- +// Function: H5Location::getRefObjType +///\brief Retrieves the type of object that an object reference points to. +///\param ref - IN: Reference to query +///\param ref_type - IN: Type of reference to query, valid values are: +/// \li \c H5R_OBJECT - Reference is an object reference. +/// \li \c H5R_DATASET_REGION - Reference is a dataset region reference. +///\return An object type, which can be one of the following: +/// \li \c H5O_TYPE_UNKNOWN - Unknown object type (-1) +/// \li \c H5O_TYPE_GROUP - Object is a group +/// \li \c H5O_TYPE_DATASET - Object is a dataset +/// \li \c H5O_TYPE_NAMED_DATATYPE - Object is a named datatype +/// \li \c H5O_TYPE_NTYPES - Number of different object types +///\exception H5::ReferenceException +// Programmer Binh-Minh Ribler - May, 2004 +//-------------------------------------------------------------------------- +H5O_type_t H5Location::getRefObjType(void *ref, H5R_type_t ref_type) const +{ + try { + return(p_get_ref_obj_type(ref, ref_type)); + } + catch (ReferenceException E) { + throw ReferenceException("H5Location::getRefObjType", E.getDetailMsg()); + } +} + +//-------------------------------------------------------------------------- +// Function: H5Location::p_get_ref_obj_type (protected) +// Purpose Retrieves the type of object that an object reference points to. +// Parameters +// ref - IN: Reference to query +// ref_type - IN: Type of reference to query +// Return An object type, which can be one of the following: +// H5O_TYPE_UNKNOWN - Unknown object type (-1) +// H5O_TYPE_GROUP - Object is a group +// H5O_TYPE_DATASET - Object is a dataset +// H5O_TYPE_NAMED_DATATYPE - Object is a named datatype +// H5O_TYPE_NTYPES - Number of object types +// Exception H5::ReferenceException +// Programmer Binh-Minh Ribler - May, 2004 +//-------------------------------------------------------------------------- +H5O_type_t H5Location::p_get_ref_obj_type(void *ref, H5R_type_t ref_type) const +{ + H5O_type_t obj_type = H5O_TYPE_UNKNOWN; + herr_t ret_value = H5Rget_obj_type2(getId(), ref_type, ref, &obj_type); + + if (obj_type == H5O_TYPE_UNKNOWN || obj_type >= H5O_TYPE_NTYPES) + { + throw ReferenceException("", "H5Rget_obj_type2 failed"); + } + return(obj_type); +} + + +//-------------------------------------------------------------------------- +// Function: H5Location::p_get_region (protected) +// Purpose Retrieves a dataspace with the region pointed to selected. +// Parameters +// ref_type - IN: Type of reference to get region of - default +// to H5R_DATASET_REGION +// ref - IN: Reference to get region of +// Return Dataspace id +// Exception H5::ReferenceException +// Programmer Binh-Minh Ribler - May, 2004 +//-------------------------------------------------------------------------- +hid_t H5Location::p_get_region(void *ref, H5R_type_t ref_type) const +{ + hid_t space_id = H5Rget_region(getId(), ref_type, ref); + if (space_id < 0) + { + throw ReferenceException("", "H5Rget_region failed"); + } + return(space_id); +} + + +//-------------------------------------------------------------------------- +// Function: H5Location destructor +///\brief Noop destructor. +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +H5Location::~H5Location() {} + +#ifndef H5_NO_NAMESPACE +} // end namespace +#endif diff --git a/c++/src/H5Location.h b/c++/src/H5Location.h new file mode 100644 index 0000000..d1dd892 --- /dev/null +++ b/c++/src/H5Location.h @@ -0,0 +1,146 @@ +// C++ informative line for the emacs editor: -*- C++ -*- +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * Copyright by The HDF Group. * + * Copyright by the Board of Trustees of the University of Illinois. * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef _H5Location_H +#define _H5Location_H + +#include "H5Classes.h" // constains forward class declarations + +// H5Location is an abstract class. It provides a collection of wrappers +// of C functions which take location IDs. Most of these were in H5Object +// but are now moved here for H5File's access. + +#ifndef H5_NO_NAMESPACE +namespace H5 { +#endif + +class H5_DLLCPP H5Location; // forward declaration for UserData4Aiterate + +// Define the operator function pointer for H5Aiterate(). +typedef void (*attr_operator_t)( H5Location& loc/*in*/, + const H5std_string attr_name/*in*/, + void *operator_data/*in,out*/); + +class UserData4Aiterate { // user data for attribute iteration + public: + attr_operator_t op; + void* opData; + H5Location* location; +}; + +// An H5Location can be a file, group, dataset, named datatype, or attribute. + +class H5_DLLCPP H5Location : public IdComponent { + public: + // Creates an attribute for the specified object at this location + // PropList is currently not used, so always be default. + Attribute createAttribute( const char* name, const DataType& type, const DataSpace& space, const PropList& create_plist = PropList::DEFAULT ) const; + Attribute createAttribute( const H5std_string& name, const DataType& type, const DataSpace& space, const PropList& create_plist = PropList::DEFAULT ) const; + + // Given its name, opens the attribute that belongs to an object at + // this location. + Attribute openAttribute( const char* name ) const; + Attribute openAttribute( const H5std_string& name ) const; + + // Given its index, opens the attribute that belongs to an object at + // this location. + Attribute openAttribute( const unsigned int idx ) const; + + // Flushes all buffers associated with this location to disk. + void flush( H5F_scope_t scope ) const; + + // Gets the name of the file, specified by this location. + H5std_string getFileName() const; + + // Determines the number of attributes at this location. + int getNumAttrs() const; + +#ifndef H5_NO_DEPRECATED_SYMBOLS + // Retrieves the type of object that an object reference points to. + H5G_obj_t getObjType(void *ref, H5R_type_t ref_type = H5R_OBJECT) const; +#endif /* H5_NO_DEPRECATED_SYMBOLS */ + + // Retrieves the type of object that an object reference points to. + H5O_type_t getRefObjType(void *ref, H5R_type_t ref_type = H5R_OBJECT) const; + // Note: getRefObjType deprecates getObjType, but getObjType's name is + // misleading, so getRefObjType is used in the new function instead. + + // Iterate user's function over the attributes at this location. + int iterateAttrs( attr_operator_t user_op, unsigned* idx = NULL, void* op_data = NULL ); + + // Removes the named attribute from this location. + void removeAttr( const char* name ) const; + void removeAttr( const H5std_string& name ) const; + + // Renames the named attribute to a new name. + void renameAttr(const char* oldname, const char* newname) const; + void renameAttr(const H5std_string& oldname, const H5std_string& newname) const; + + // Creates a reference to a named object or to a dataset region + // in this object. + void reference(void* ref, const char* name, const DataSpace& dataspace, + H5R_type_t ref_type = H5R_DATASET_REGION) const; + void reference(void* ref, const char* name) const; + void reference(void* ref, const H5std_string& name) const; + + // Open a referenced object whose location is specified by either + // a file, an HDF5 object, or an attribute. + void dereference(H5File& h5file, const void* ref, H5R_type_t ref_type = H5R_OBJECT); + void dereference(H5Object& obj, const void* ref, H5R_type_t ref_type = H5R_OBJECT); + void dereference(Attribute& attr, const void* ref, H5R_type_t ref_type = H5R_OBJECT); + + // For subclasses. + virtual hid_t getId() const = 0; + + protected: +#ifndef DOXYGEN_SHOULD_SKIP_THIS + // Default constructor, + H5Location(); + + // Creates a copy of an existing object giving the location id. + H5Location(const hid_t loc_id); + + // Copy constructor. + H5Location(const H5Location& original); + + // Creates a reference to an HDF5 object or a dataset region. + void p_reference(void* ref, const char* name, hid_t space_id, H5R_type_t ref_type) const; + + // Dereferences a ref into an HDF5 id. + hid_t p_dereference(hid_t loc_id, const void* ref, H5R_type_t ref_type); + +#ifndef H5_NO_DEPRECATED_SYMBOLS + // Retrieves the type of object that an object reference points to. + H5G_obj_t p_get_obj_type(void *ref, H5R_type_t ref_type) const; +#endif /* H5_NO_DEPRECATED_SYMBOLS */ + + // Retrieves the type of object that an object reference points to. + H5O_type_t p_get_ref_obj_type(void *ref, H5R_type_t ref_type) const; + + // Retrieves a dataspace with the region pointed to selected. + hid_t p_get_region(void *ref, H5R_type_t ref_type) const; + + // Noop destructor. + virtual ~H5Location(); + +#endif // DOXYGEN_SHOULD_SKIP_THIS + +}; /* end class H5Location */ + +#ifndef H5_NO_NAMESPACE +} +#endif +#endif diff --git a/c++/src/H5Object.cpp b/c++/src/H5Object.cpp index 3c85502..1fdaf3f 100644 --- a/c++/src/H5Object.cpp +++ b/c++/src/H5Object.cpp @@ -40,7 +40,7 @@ namespace H5 { // userAttrOpWrpr simply interfaces between the user's function and the // C library function H5Aiterate2; used to resolve the different prototype // problem. May be moved to Iterator later. -extern "C" herr_t userAttrOpWrpr(hid_t loc_id, const char *attr_name, +extern "C" herr_t userAttrOpWrpr1(hid_t loc_id, const char *attr_name, const H5A_info_t *ainfo, void *op_data) { H5std_string s_attr_name = H5std_string( attr_name ); @@ -49,18 +49,18 @@ extern "C" herr_t userAttrOpWrpr(hid_t loc_id, const char *attr_name, #else UserData4Aiterate* myData = static_cast (op_data); #endif - myData->op( *myData->object, s_attr_name, myData->opData ); + myData->op( *myData->location, s_attr_name, myData->opData ); return 0; } //-------------------------------------------------------------------------- // Function: H5Object default constructor (protected) // Description -// The id is set by IdComponent() but subclass constructor will +// The id is set by H5Location() but subclass constructor will // set it to a valid HDF5 id. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -H5Object::H5Object() : IdComponent(0) {} +H5Object::H5Object() : H5Location(0) {} //-------------------------------------------------------------------------- // Function: H5Object overloaded constructor (protected) @@ -69,7 +69,7 @@ H5Object::H5Object() : IdComponent(0) {} // Parameters object_id - IN: Id of an existing HDF5 object // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -H5Object::H5Object( const hid_t object_id ) : IdComponent( object_id ) {} +H5Object::H5Object( const hid_t object_id ) : H5Location( object_id ) {} #endif // DOXYGEN_SHOULD_SKIP_THIS @@ -80,496 +80,7 @@ H5Object::H5Object( const hid_t object_id ) : IdComponent( object_id ) {} ///\param original - IN: H5Object instance to copy // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -H5Object::H5Object( const H5Object& original ) : IdComponent( original ) {} - -//-------------------------------------------------------------------------- -// Function: H5Object::createAttribute -///\brief Creates an attribute for a group, dataset, or named datatype. -///\param name - IN: Name of the attribute -///\param data_type - IN: Datatype for the attribute -///\param data_space - IN: Dataspace for the attribute - only simple -/// dataspaces are allowed at this time -///\param create_plist - IN: Creation property list - default to -/// PropList::DEFAULT -///\return Attribute instance -///\exception H5::AttributeIException -///\par Description -/// The attribute name specified in \a name must be unique. -/// Attempting to create an attribute with the same name as an -/// existing attribute will raise an exception, leaving the -/// pre-existing attribute intact. To overwrite an existing -/// attribute with a new attribute of the same name, first -/// delete the existing one with \c H5Object::removeAttr, then -/// recreate it with this function. -// Programmer Binh-Minh Ribler - 2000 -//-------------------------------------------------------------------------- -Attribute H5Object::createAttribute( const char* name, const DataType& data_type, const DataSpace& data_space, const PropList& create_plist ) const -{ - hid_t type_id = data_type.getId(); - hid_t space_id = data_space.getId(); - hid_t plist_id = create_plist.getId(); - hid_t attr_id = H5Acreate2(getId(), name, type_id, space_id, plist_id, H5P_DEFAULT ); - - // If the attribute id is valid, create and return the Attribute object - if( attr_id > 0 ) - { - Attribute attr( attr_id ); - return( attr ); - } - else - throw AttributeIException(inMemFunc("createAttribute"), "H5Acreate2 failed"); -} - -//-------------------------------------------------------------------------- -// Function: H5Object::createAttribute -///\brief This is an overloaded member function, provided for convenience. -/// It differs from the above function in that it takes -/// a reference to an \c H5std_string for \a name. -// Programmer Binh-Minh Ribler - 2000 -//-------------------------------------------------------------------------- -Attribute H5Object::createAttribute( const H5std_string& name, const DataType& data_type, const DataSpace& data_space, const PropList& create_plist ) const -{ - return( createAttribute( name.c_str(), data_type, data_space, create_plist )); -} - -//-------------------------------------------------------------------------- -// Function: H5Object::openAttribute -///\brief Opens an attribute given its name. -///\param name - IN: Name of the attribute -///\return Attribute instance -///\exception H5::AttributeIException -// Programmer Binh-Minh Ribler - 2000 -//-------------------------------------------------------------------------- -Attribute H5Object::openAttribute( const char* name ) const -{ - hid_t attr_id = H5Aopen(getId(), name, H5P_DEFAULT); - if( attr_id > 0 ) - { - Attribute attr( attr_id ); - return( attr ); - } - else - { - throw AttributeIException(inMemFunc("openAttribute"), "H5Aopen failed"); - } -} - -//-------------------------------------------------------------------------- -// Function: H5Object::openAttribute -///\brief This is an overloaded member function, provided for convenience. -/// It differs from the above function in that it takes -/// a reference to an \c H5std_string for \a name. -// Programmer Binh-Minh Ribler - 2000 -//-------------------------------------------------------------------------- -Attribute H5Object::openAttribute( const H5std_string& name ) const -{ - return( openAttribute( name.c_str()) ); -} - -//-------------------------------------------------------------------------- -// Function: H5Object::openAttribute -///\brief Opens an attribute given its index. -///\param idx - IN: Index of the attribute, a 0-based, non-negative integer -///\return Attribute instance -///\exception H5::AttributeIException -// Programmer Binh-Minh Ribler - 2000 -//-------------------------------------------------------------------------- -Attribute H5Object::openAttribute( const unsigned int idx ) const -{ - hid_t attr_id = H5Aopen_by_idx(getId(), ".", H5_INDEX_CRT_ORDER, - H5_ITER_INC, (hsize_t)idx, H5P_DEFAULT, H5P_DEFAULT); - if( attr_id > 0 ) - { - Attribute attr( attr_id ); - return( attr ); - } - else - { - throw AttributeIException(inMemFunc("openAttribute"), "H5Aopen_by_idx failed"); - } -} - -//-------------------------------------------------------------------------- -// Function: H5Object::iterateAttrs -///\brief Iterates a user's function over all the attributes of an H5 -/// object, which may be a group, dataset or named datatype. -///\param user_op - IN: User's function to operate on each attribute -///\param _idx - IN/OUT: Starting (IN) and ending (OUT) attribute indices -///\param op_data - IN: User's data to pass to user's operator function -///\return Returned value of the last operator if it was non-zero, or -/// zero if all attributes were processed -///\exception H5::AttributeIException -///\par Description -/// The signature of user_op is -/// void (*)(H5::H5Object&, H5std_string, void*). -/// For information, please refer to the C layer Reference Manual -/// at: -/// http://www.hdfgroup.org/HDF5/doc/RM/RM_H5A.html#Annot-Iterate -// Programmer Binh-Minh Ribler - 2000 -//-------------------------------------------------------------------------- -int H5Object::iterateAttrs( attr_operator_t user_op, unsigned *_idx, void *op_data ) -{ - // store the user's function and data - UserData4Aiterate* userData = new UserData4Aiterate; - userData->opData = op_data; - userData->op = user_op; - userData->object = this; - - // call the C library routine H5Aiterate2 to iterate the attributes - hsize_t idx = _idx ? (hsize_t)*_idx : 0; - int ret_value = H5Aiterate2(getId(), H5_INDEX_NAME, H5_ITER_INC, &idx, - userAttrOpWrpr, (void *) userData); - - // release memory - delete userData; - - if( ret_value >= 0 ) { - /* Pass back update index value to calling code */ - if (_idx) - *_idx = (unsigned)idx; - - return( ret_value ); - } - else // raise exception when H5Aiterate returns a negative value - throw AttributeIException(inMemFunc("iterateAttrs"), "H5Aiterate2 failed"); -} - -//-------------------------------------------------------------------------- -// Function: H5Object::getNumAttrs -///\brief Returns the number of attributes attached to this HDF5 object. -///\return Number of attributes -///\exception H5::AttributeIException -// Programmer Binh-Minh Ribler - 2000 -//-------------------------------------------------------------------------- -int H5Object::getNumAttrs() const -{ - H5O_info_t oinfo; /* Object info */ - - if(H5Oget_info(getId(), &oinfo) < 0) - throw AttributeIException(inMemFunc("getNumAttrs"), "H5Oget_info failed"); - else - return( (int)oinfo.num_attrs ); -} - -//-------------------------------------------------------------------------- -// Function: H5Object::removeAttr -///\brief Removes the named attribute from this object. -///\param name - IN: Name of the attribute to be removed -///\exception H5::AttributeIException -// Programmer Binh-Minh Ribler - 2000 -//-------------------------------------------------------------------------- -void H5Object::removeAttr( const char* name ) const -{ - herr_t ret_value = H5Adelete(getId(), name); - if( ret_value < 0 ) - throw AttributeIException(inMemFunc("removeAttr"), "H5Adelete failed"); -} - -//-------------------------------------------------------------------------- -// Function: H5Object::removeAttr -///\brief This is an overloaded member function, provided for convenience. -/// It differs from the above function in that it takes -/// a reference to an \c H5std_string for \a name. -// Programmer Binh-Minh Ribler - 2000 -//-------------------------------------------------------------------------- -void H5Object::removeAttr( const H5std_string& name ) const -{ - removeAttr( name.c_str() ); -} - -//-------------------------------------------------------------------------- -// Function: H5Object::renameAttr -///\brief Renames the named attribute from this object. -///\param oldname - IN: Name of the attribute to be renamed -///\param newname - IN: New name ame of the attribute -///\exception H5::AttributeIException -// Programmer Binh-Minh Ribler - Mar, 2005 -//-------------------------------------------------------------------------- -void H5Object::renameAttr(const char* oldname, const char* newname) const -{ - herr_t ret_value = H5Arename(getId(), oldname, newname); - if (ret_value < 0) - throw AttributeIException(inMemFunc("renameAttr"), "H5Arename failed"); -} - -//-------------------------------------------------------------------------- -// Function: H5Object::renameAttr -///\brief This is an overloaded member function, provided for convenience. -/// It differs from the above function in that it takes -/// a reference to an \c H5std_string for the names. -// Programmer Binh-Minh Ribler - Mar, 2005 -//-------------------------------------------------------------------------- -void H5Object::renameAttr(const H5std_string& oldname, const H5std_string& newname) const -{ - renameAttr (oldname.c_str(), newname.c_str()); -} - -//-------------------------------------------------------------------------- -// Function: H5Object::flush -///\brief Flushes all buffers associated with a file to disk. -///\param scope - IN: Specifies the scope of the flushing action, -/// which can be either of these values: -/// \li \c H5F_SCOPE_GLOBAL - Flushes the entire virtual file -/// \li \c H5F_SCOPE_LOCAL - Flushes only the specified file -///\exception H5::AttributeIException -///\par Description -/// This object is used to identify the file to be flushed. -// Programmer Binh-Minh Ribler - 2000 -//-------------------------------------------------------------------------- -void H5Object::flush(H5F_scope_t scope) const -{ - herr_t ret_value = H5Fflush(getId(), scope); - if( ret_value < 0 ) - { - throw FileIException(inMemFunc("flush"), "H5Fflush failed"); - } -} - -//-------------------------------------------------------------------------- -// Function: H5Object::getFileName -///\brief Gets the name of the file, in which this HDF5 object belongs. -///\return File name -///\exception H5::IdComponentException -// Programmer Binh-Minh Ribler - Jul, 2004 -//-------------------------------------------------------------------------- -H5std_string H5Object::getFileName() const -{ - try { - return(p_get_file_name()); - } - catch (IdComponentException E) { - throw FileIException(inMemFunc("getFileName"), E.getDetailMsg()); - } -} - -//-------------------------------------------------------------------------- -// Function: H5Object::p_reference (protected) -// Purpose Creates a reference to an HDF5 object or a dataset region. -// Parameters -// name - IN: Name of the object to be referenced -// dataspace - IN: Dataspace with selection -// ref_type - IN: Type of reference; default to \c H5R_DATASET_REGION -// Exception H5::IdComponentException -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void H5Object::p_reference(void* ref, const char* name, hid_t space_id, H5R_type_t ref_type) const -{ - herr_t ret_value = H5Rcreate(ref, getId(), name, ref_type, space_id); - if (ret_value < 0) - { - throw IdComponentException("", "H5Rcreate failed"); - } -} - -//-------------------------------------------------------------------------- -// Function: H5Object::reference -///\brief Creates a reference to an HDF5 object or a dataset region. -///\param ref - IN: Reference pointer -///\param name - IN: Name of the object to be referenced -///\param dataspace - IN: Dataspace with selection -///\param ref_type - IN: Type of reference to query, valid values are: -/// \li \c H5R_OBJECT - Reference is an object reference. -/// \li \c H5R_DATASET_REGION - Reference is a dataset region -/// reference. - this is the default -///\exception H5::IdComponentException -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void H5Object::reference(void* ref, const char* name, const DataSpace& dataspace, H5R_type_t ref_type) const -{ - try { - p_reference(ref, name, dataspace.getId(), ref_type); - } - catch (IdComponentException E) { - throw IdComponentException("H5Object::reference", E.getDetailMsg()); - } -} - -//-------------------------------------------------------------------------- -// Function: H5Object::reference -///\brief This is an overloaded function, provided for your convenience. -/// It differs from the above function in that it only creates -/// a reference to an HDF5 object, not to a dataset region. -///\param ref - IN: Reference pointer -///\param name - IN: Name of the object to be referenced - \c char pointer -///\exception H5::IdComponentException -///\par Description -// This function passes H5R_OBJECT and -1 to the protected -// function for it to pass to the C API H5Rcreate -// to create a reference to the named object. -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void H5Object::reference(void* ref, const char* name) const -{ - try { - p_reference(ref, name, -1, H5R_OBJECT); - } - catch (IdComponentException E) { - throw IdComponentException("H5Object::reference", E.getDetailMsg()); - } -} - -//-------------------------------------------------------------------------- -// Function: H5Object::reference -///\brief This is an overloaded function, provided for your convenience. -/// It differs from the above function in that it takes an -/// \c H5std_string for the object's name. -///\param ref - IN: Reference pointer -///\param name - IN: Name of the object to be referenced - \c H5std_string -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void H5Object::reference(void* ref, const H5std_string& name) const -{ - reference(ref, name.c_str()); -} - -//-------------------------------------------------------------------------- -// Function: H5Object::p_dereference (protected) -// Purpose Dereference a ref into an hdf5 object. -// Parameters -// loc_id - IN: An hdf5 identifier specifying the location of the -// referenced object -// ref - IN: Reference pointer -// ref_type - IN: Reference type -// Exception H5::ReferenceException -// Programmer Binh-Minh Ribler - Oct, 2006 -// Modification -// May 2008 - BMR -// Moved from IdComponent. -//-------------------------------------------------------------------------- -hid_t H5Object::p_dereference(hid_t loc_id, const void* ref, H5R_type_t ref_type) -{ - hid_t temp_id; - temp_id = H5Rdereference2(loc_id, H5P_DEFAULT, ref_type, ref); - if (temp_id < 0) - { - throw ReferenceException("", "H5Rdereference failed"); - } - - // No failure, set id to the object - return(temp_id); -} - -//-------------------------------------------------------------------------- -// Function: H5Object::dereference -///\brief Dereferences a reference into an HDF5 object, given an HDF5 object. -///\param obj - IN: Object specifying the location of the referenced object -///\param ref - IN: Reference pointer -///\param ref_type - IN: Reference type -///\exception H5::ReferenceException -// Programmer Binh-Minh Ribler - Oct, 2006 -// Modification -// May, 2008 -// Corrected missing parameters. - BMR -//-------------------------------------------------------------------------- -void H5Object::dereference(H5Object& obj, const void* ref, H5R_type_t ref_type) -{ - hid_t temp_id; - try { - temp_id = p_dereference(obj.getId(), ref, ref_type); - } - catch (ReferenceException E) { - throw ReferenceException("H5Object::dereference - located by object", E.getDetailMsg()); - } - p_setId(temp_id); -} - -//-------------------------------------------------------------------------- -// Function: H5Object::dereference -///\brief Dereferences a reference into an HDF5 object, given an HDF5 file. -///\param h5file - IN: HDF5 file specifying the location of the referenced object -///\param ref - IN: Reference pointer -///\param ref_type - IN: Reference type -///\exception H5::ReferenceException -// Programmer Binh-Minh Ribler - Oct, 2006 -// Modification -// May, 2008 -// Corrected missing parameters. - BMR -//-------------------------------------------------------------------------- -void H5Object::dereference(H5File& h5file, const void* ref, H5R_type_t ref_type) -{ - hid_t temp_id; - try { - temp_id = p_dereference(h5file.getId(), ref, ref_type); - } - catch (ReferenceException E) { - throw ReferenceException("H5Object::dereference - located by file", E.getDetailMsg()); - } - p_setId(temp_id); -} - -//-------------------------------------------------------------------------- -// Function: H5Object::dereference -///\brief Dereferences a reference into an HDF5 object, given an attribute. -///\param attr - IN: Attribute specifying the location of the referenced object -///\param ref - IN: Reference pointer -///\param ref_type - IN: Reference type -///\exception H5::ReferenceException -// Programmer Binh-Minh Ribler - Oct, 2006 -// Modification -// May, 2008 -// Corrected missing parameters. - BMR -//-------------------------------------------------------------------------- -void H5Object::dereference(Attribute& attr, const void* ref, H5R_type_t ref_type) -{ - hid_t temp_id; - try { - temp_id = p_dereference(attr.getId(), ref, ref_type); - } - catch (ReferenceException E) { - throw ReferenceException("H5Object::dereference - located by attribute", E.getDetailMsg()); - } - p_setId(temp_id); -} - -#ifndef H5_NO_DEPRECATED_SYMBOLS -//-------------------------------------------------------------------------- -// Function: H5Object::p_get_obj_type (protected) -// Purpose Retrieves the type of object that an object reference points to. -// Parameters -// ref - IN: Reference to query -// ref_type - IN: Type of reference to query -// Return An object type, which can be one of the following: -// H5G_LINK Object is a symbolic link. -// H5G_GROUP Object is a group. -// H5G_DATASET Object is a dataset. -// H5G_TYPE Object is a named datatype -// Exception H5::IdComponentException -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -H5G_obj_t H5Object::p_get_obj_type(void *ref, H5R_type_t ref_type) const -{ - H5G_obj_t obj_type = H5Rget_obj_type1(getId(), ref_type, ref); - - if (obj_type == H5G_UNKNOWN) - { - throw IdComponentException("", "H5Rget_obj_type failed"); - } - return(obj_type); -} -#endif /* H5_NO_DEPRECATED_SYMBOLS */ - - -//-------------------------------------------------------------------------- -// Function: H5Object::p_get_region (protected) -// Purpose Retrieves a dataspace with the region pointed to selected. -// Parameters -// ref_type - IN: Type of reference to get region of - default -// to H5R_DATASET_REGION -// ref - IN: Reference to get region of -// Return Dataspace id -// Exception H5::IdComponentException -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -hid_t H5Object::p_get_region(void *ref, H5R_type_t ref_type) const -{ - hid_t space_id = H5Rget_region(getId(), ref_type, ref); - if (space_id < 0) - { - throw IdComponentException("", "H5Rget_region failed"); - } - return(space_id); -} - +H5Object::H5Object( const H5Object& original ) : H5Location( original ) {} //-------------------------------------------------------------------------- // Function: H5Object destructor diff --git a/c++/src/H5Object.h b/c++/src/H5Object.h index 4ac417b..5c264ec 100644 --- a/c++/src/H5Object.h +++ b/c++/src/H5Object.h @@ -17,11 +17,22 @@ #ifndef _H5Object_H #define _H5Object_H +#include "H5Location.h" #include "H5Classes.h" // constains forward class declarations // H5Object is a baseclass. It has these subclasses: // Group, DataSet, and DataType. // DataType, in turn, has several specific datatypes as subclasses. +// Modification: +// Sept 18, 2012: Added class H5Location in between IdComponent and +// H5Object. An H5File now inherits from H5Location. All HDF5 +// wrappers in H5Object are moved up to H5Location. H5Object +// is left mostly empty for future wrappers that are only for +// group, dataset, and named datatype. Note that the reason for +// adding H5Location instead of simply moving H5File to be under +// H5Object is H5File is not an HDF5 object, and renaming H5Object +// to H5Location will risk breaking user applications. +// -BMR #ifndef H5_NO_NAMESPACE namespace H5 { @@ -31,13 +42,13 @@ namespace H5 { class H5_DLLCPP H5Object; // forward declaration for UserData4Aiterate // Define the operator function pointer for H5Aiterate(). -typedef void (*attr_operator_t)( H5Object& loc/*in*/, +typedef void (*attr1_operator_t)( H5Object& loc/*in*/, const H5std_string attr_name/*in*/, void *operator_data/*in,out*/); -class UserData4Aiterate { // user data for attribute iteration +class UserData4Aiterate1 { // user data for attribute iteration public: - attr_operator_t op; + attr1_operator_t op; void* opData; H5Object* object; }; @@ -45,53 +56,8 @@ class UserData4Aiterate { // user data for attribute iteration // The above part is being moved into Iterator, but not completed -class H5_DLLCPP H5Object : public IdComponent { +class H5_DLLCPP H5Object : public H5Location { public: - // Creates an attribute for a group, dataset, or named datatype. - // PropList is currently not used, so always be default. - Attribute createAttribute( const char* name, const DataType& type, const DataSpace& space, const PropList& create_plist = PropList::DEFAULT ) const; - Attribute createAttribute( const H5std_string& name, const DataType& type, const DataSpace& space, const PropList& create_plist = PropList::DEFAULT ) const; - - // Opens an attribute given its name. - Attribute openAttribute( const char* name ) const; - Attribute openAttribute( const H5std_string& name ) const; - - // Opens an attribute given its index. - Attribute openAttribute( const unsigned int idx ) const; - - // Flushes all buffers associated with this object to disk - void flush( H5F_scope_t scope ) const; - - // Gets the name of the file, in which this HDF5 object belongs. - H5std_string getFileName() const; - - // Determines the number of attributes attached to this object. - int getNumAttrs() const; - - // Iterate user's function over the attributes of this object - int iterateAttrs( attr_operator_t user_op, unsigned* idx = NULL, void* op_data = NULL ); - - // Removes the named attribute from this object. - void removeAttr( const char* name ) const; - void removeAttr( const H5std_string& name ) const; - - // Renames the attribute to a new name. - void renameAttr(const char* oldname, const char* newname) const; - void renameAttr(const H5std_string& oldname, const H5std_string& newname) const; - - // Creates a reference to a named Hdf5 object or to a dataset region - // in this object. - void reference(void* ref, const char* name, const DataSpace& dataspace, - H5R_type_t ref_type = H5R_DATASET_REGION) const; - void reference(void* ref, const char* name) const; - void reference(void* ref, const H5std_string& name) const; - - // Open a referenced HDF5 object whose location is specified by either - // a file, an HDF5 object, or an attribute. - void dereference(H5File& h5file, const void* ref, H5R_type_t ref_type = H5R_OBJECT); - void dereference(H5Object& obj, const void* ref, H5R_type_t ref_type = H5R_OBJECT); - void dereference(Attribute& attr, const void* ref, H5R_type_t ref_type = H5R_OBJECT); - // Copy constructor: makes copy of an H5Object object. H5Object(const H5Object& original); @@ -106,23 +72,6 @@ class H5_DLLCPP H5Object : public IdComponent { // Creates a copy of an existing object giving the object id H5Object( const hid_t object_id ); - // Gets the id of the H5 file in which the given object is located. - hid_t p_get_file_id(); - - // Creates a reference to an HDF5 object or a dataset region. - void p_reference(void* ref, const char* name, hid_t space_id, H5R_type_t ref_type) const; - - // Dereferences a ref into an hdf5 id. - hid_t p_dereference(hid_t loc_id, const void* ref, H5R_type_t ref_type); - -#ifndef H5_NO_DEPRECATED_SYMBOLS - // Retrieves the type of object that an object reference points to. - H5G_obj_t p_get_obj_type(void *ref, H5R_type_t ref_type) const; -#endif /* H5_NO_DEPRECATED_SYMBOLS */ - - // Retrieves a dataspace with the region pointed to selected. - hid_t p_get_region(void *ref, H5R_type_t ref_type) const; - #endif // DOXYGEN_SHOULD_SKIP_THIS }; /* end class H5Object */ diff --git a/c++/src/Makefile.am b/c++/src/Makefile.am index 6278fa0..ec942fd 100644 --- a/c++/src/Makefile.am +++ b/c++/src/Makefile.am @@ -40,12 +40,12 @@ bin_SCRIPTS=h5c++ # Source files for the library libhdf5_cpp_la_SOURCES=H5Exception.cpp H5IdComponent.cpp H5Library.cpp \ - H5Attribute.cpp H5Object.cpp H5PropList.cpp H5FaccProp.cpp \ - H5FcreatProp.cpp H5DcreatProp.cpp H5DxferProp.cpp H5DataType.cpp \ - H5DataSpace.cpp H5AbstractDs.cpp H5AtomType.cpp H5PredType.cpp \ - H5EnumType.cpp H5IntType.cpp H5FloatType.cpp H5StrType.cpp \ - H5ArrayType.cpp H5VarLenType.cpp H5CompType.cpp H5DataSet.cpp \ - H5CommonFG.cpp H5Group.cpp H5File.cpp + H5Attribute.cpp H5Location.cpp H5Object.cpp H5PropList.cpp \ + H5FaccProp.cpp H5FcreatProp.cpp H5DcreatProp.cpp H5DxferProp.cpp \ + H5DataType.cpp H5DataSpace.cpp H5AbstractDs.cpp H5AtomType.cpp \ + H5PredType.cpp H5EnumType.cpp H5IntType.cpp H5FloatType.cpp \ + H5StrType.cpp H5ArrayType.cpp H5VarLenType.cpp H5CompType.cpp \ + H5DataSet.cpp H5CommonFG.cpp H5Group.cpp H5File.cpp # HDF5 C++ library depends on HDF5 Library. libhdf5_cpp_la_LIBADD=$(LIBHDF5) @@ -55,8 +55,9 @@ include_HEADERS=H5Cpp.h H5AbstractDs.h H5AtomType.h H5Attribute.h H5Classes.h \ H5CommonFG.h H5CompType.h H5DataSet.h H5DataSpace.h H5DataType.h \ H5DcreatProp.h H5DxferProp.h H5EnumType.h H5Exception.h H5FaccProp.h \ H5FcreatProp.h H5File.h H5FloatType.h H5Group.h H5IdComponent.h \ - H5Include.h H5IntType.h H5Library.h H5Object.h H5PredType.h \ - H5PropList.h H5StrType.h H5CppDoc.h H5ArrayType.h H5VarLenType.h + H5Include.h H5IntType.h H5Library.h H5Location.h H5Object.h \ + H5PredType.h H5PropList.h H5StrType.h H5CppDoc.h H5ArrayType.h \ + H5VarLenType.h # h5c++ and libhdf5.settings are generated during configure. Remove only when # distclean. diff --git a/c++/src/Makefile.in b/c++/src/Makefile.in index 3dd5c58..9a085e7 100644 --- a/c++/src/Makefile.in +++ b/c++/src/Makefile.in @@ -120,12 +120,12 @@ am__installdirs = "$(DESTDIR)$(libdir)" "$(DESTDIR)$(bindir)" \ LTLIBRARIES = $(lib_LTLIBRARIES) libhdf5_cpp_la_DEPENDENCIES = $(LIBHDF5) am_libhdf5_cpp_la_OBJECTS = H5Exception.lo H5IdComponent.lo \ - H5Library.lo H5Attribute.lo H5Object.lo H5PropList.lo \ - H5FaccProp.lo H5FcreatProp.lo H5DcreatProp.lo H5DxferProp.lo \ - H5DataType.lo H5DataSpace.lo H5AbstractDs.lo H5AtomType.lo \ - H5PredType.lo H5EnumType.lo H5IntType.lo H5FloatType.lo \ - H5StrType.lo H5ArrayType.lo H5VarLenType.lo H5CompType.lo \ - H5DataSet.lo H5CommonFG.lo H5Group.lo H5File.lo + H5Library.lo H5Attribute.lo H5Location.lo H5Object.lo \ + H5PropList.lo H5FaccProp.lo H5FcreatProp.lo H5DcreatProp.lo \ + H5DxferProp.lo H5DataType.lo H5DataSpace.lo H5AbstractDs.lo \ + H5AtomType.lo H5PredType.lo H5EnumType.lo H5IntType.lo \ + H5FloatType.lo H5StrType.lo H5ArrayType.lo H5VarLenType.lo \ + H5CompType.lo H5DataSet.lo H5CommonFG.lo H5Group.lo H5File.lo libhdf5_cpp_la_OBJECTS = $(am_libhdf5_cpp_la_OBJECTS) AM_V_lt = $(am__v_lt_@AM_V@) am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) @@ -257,7 +257,6 @@ FILTERS = @FILTERS@ FSEARCH_DIRS = @FSEARCH_DIRS@ GPFS = @GPFS@ GREP = @GREP@ -H5BLD_STATIC = @H5BLD_STATIC@ H5_CFLAGS = @H5_CFLAGS@ H5_CPPFLAGS = @H5_CPPFLAGS@ H5_CXXFLAGS = @H5_CXXFLAGS@ @@ -272,7 +271,6 @@ HAVE_DMALLOC = @HAVE_DMALLOC@ HAVE_FORTRAN_2003 = @HAVE_FORTRAN_2003@ HDF5_HL = @HDF5_HL@ HDF5_INTERFACES = @HDF5_INTERFACES@ -HDF5_USE_SHLIB = @HDF5_USE_SHLIB@ HDF_CXX = @HDF_CXX@ HDF_FORTRAN = @HDF_FORTRAN@ HDF_FORTRAN2003 = @HDF_FORTRAN2003@ @@ -469,7 +467,7 @@ CHECK_CLEANFILES = *.chkexe *.chklog *.clog # Add libtool shared library version numbers to the HDF5 library # See libtool versioning documentation online. LT_VERS_INTERFACE = 6 -LT_VERS_REVISION = 121 +LT_VERS_REVISION = 118 LT_VERS_AGE = 0 # Include src directory @@ -484,12 +482,12 @@ bin_SCRIPTS = h5c++ # Source files for the library libhdf5_cpp_la_SOURCES = H5Exception.cpp H5IdComponent.cpp H5Library.cpp \ - H5Attribute.cpp H5Object.cpp H5PropList.cpp H5FaccProp.cpp \ - H5FcreatProp.cpp H5DcreatProp.cpp H5DxferProp.cpp H5DataType.cpp \ - H5DataSpace.cpp H5AbstractDs.cpp H5AtomType.cpp H5PredType.cpp \ - H5EnumType.cpp H5IntType.cpp H5FloatType.cpp H5StrType.cpp \ - H5ArrayType.cpp H5VarLenType.cpp H5CompType.cpp H5DataSet.cpp \ - H5CommonFG.cpp H5Group.cpp H5File.cpp + H5Attribute.cpp H5Location.cpp H5Object.cpp H5PropList.cpp \ + H5FaccProp.cpp H5FcreatProp.cpp H5DcreatProp.cpp H5DxferProp.cpp \ + H5DataType.cpp H5DataSpace.cpp H5AbstractDs.cpp H5AtomType.cpp \ + H5PredType.cpp H5EnumType.cpp H5IntType.cpp H5FloatType.cpp \ + H5StrType.cpp H5ArrayType.cpp H5VarLenType.cpp H5CompType.cpp \ + H5DataSet.cpp H5CommonFG.cpp H5Group.cpp H5File.cpp # HDF5 C++ library depends on HDF5 Library. @@ -500,8 +498,9 @@ include_HEADERS = H5Cpp.h H5AbstractDs.h H5AtomType.h H5Attribute.h H5Classes.h H5CommonFG.h H5CompType.h H5DataSet.h H5DataSpace.h H5DataType.h \ H5DcreatProp.h H5DxferProp.h H5EnumType.h H5Exception.h H5FaccProp.h \ H5FcreatProp.h H5File.h H5FloatType.h H5Group.h H5IdComponent.h \ - H5Include.h H5IntType.h H5Library.h H5Object.h H5PredType.h \ - H5PropList.h H5StrType.h H5CppDoc.h H5ArrayType.h H5VarLenType.h + H5Include.h H5IntType.h H5Library.h H5Location.h H5Object.h \ + H5PredType.h H5PropList.h H5StrType.h H5CppDoc.h H5ArrayType.h \ + H5VarLenType.h # h5c++ and libhdf5.settings are generated during configure. Remove only when @@ -666,6 +665,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/H5IdComponent.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/H5IntType.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/H5Library.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/H5Location.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/H5Object.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/H5PredType.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/H5PropList.Plo@am__quote@ diff --git a/c++/test/h5cpputil.cpp b/c++/test/h5cpputil.cpp index 4485808..c6a7a2a 100644 --- a/c++/test/h5cpputil.cpp +++ b/c++/test/h5cpputil.cpp @@ -100,6 +100,7 @@ void issue_fail_msg(const char* where, int line, const char* file_name, { //if (GetTestVerbosity()>=VERBO_HI) { + cerr << endl; cerr << ">>> FAILED in " << where << " at line " << line << " in " << file_name << " - " << message << endl << endl; } diff --git a/c++/test/tattr.cpp b/c++/test/tattr.cpp index 6bb9ca0..6f7674d 100644 --- a/c++/test/tattr.cpp +++ b/c++/test/tattr.cpp @@ -42,8 +42,13 @@ #include "h5cpputil.h" // C++ utilility header file -const H5std_string FILENAME("tattr.h5"); -const H5std_string ATTR_TMP_NAME("temp_name"); +const H5std_string FILE_BASIC("tattr_basic.h5"); +const H5std_string FILE_COMPOUND("tattr_compound.h5"); +const H5std_string FILE_SCALAR("tattr_scalar.h5"); +const H5std_string FILE_MULTI("tattr_multi.h5"); +const H5std_string FILE_DTYPE("tattr_dtype.h5"); +const H5std_string ATTR_TMP_NAME("temp_attr_name"); +const H5std_string FATTR_TMP_NAME("temp_fattr_name"); const size_t ATTR_MAX_DIMS = 7; /* 3-D dataset with fixed dimensions */ @@ -63,6 +68,10 @@ const int ATTR1_RANK = 1; const int ATTR1_DIM1 = 3; int attr_data1[ATTR1_DIM1]={512,-234,98123}; /* Test data for 1st attribute */ +// File attribute, using the same rank and dimensions as ATTR1_NAME's +const H5std_string FATTR1_NAME("File Attr1"); +const H5std_string FATTR2_NAME("File Attr2"); + const H5std_string ATTR2_NAME("Attr2"); const int ATTR2_RANK = 2; const int ATTR2_DIM1 = 2; @@ -121,7 +130,7 @@ static void test_attr_basic_write() try { // Create file - H5File fid1 (FILENAME, H5F_ACC_TRUNC); + H5File fid1 (FILE_BASIC, H5F_ACC_TRUNC); // Create dataspace for dataset DataSpace ds_space (SPACE1_RANK, dims1); @@ -136,6 +145,12 @@ static void test_attr_basic_write() // Create dataspace for attribute DataSpace att_space (ATTR1_RANK, dims2); + // Create a file attribute + Attribute file_attr2 = fid1.createAttribute (FATTR1_NAME, PredType::NATIVE_INT, att_space); + + // Create a file attribute + Attribute file_attr1 = fid1.createAttribute (FATTR2_NAME, PredType::NATIVE_INT, att_space); + // Create an attribute for the dataset Attribute ds_attr1 = dataset.createAttribute (ATTR1_NAME, PredType::NATIVE_INT, att_space); @@ -163,8 +178,9 @@ static void test_attr_basic_write() if(attr_data1[i]!=read_data1[i]) TestErrPrintf("%d: attribute data different: attr_data1[%d]=%d,read_data1[%d]=%d\n",__LINE__,i,attr_data1[i],i,read_data1[i]); - // Create another attribute for this dataset - Attribute ds_attr2 = dataset.createAttribute (ATTR1A_NAME, PredType::NATIVE_INT, att_space); + // Create two more attributes for this dataset, but only write to one. + Attribute ds_attr2 = dataset.createAttribute (ATTR2_NAME, PredType::NATIVE_INT, att_space); + Attribute ds_attr3 = dataset.createAttribute (ATTR3_NAME, PredType::NATIVE_INT, att_space); // Write attribute information ds_attr2.write (PredType::NATIVE_INT, attr_data1a); @@ -180,6 +196,7 @@ static void test_attr_basic_write() // Close both attributes ds_attr1.close(); ds_attr2.close(); + ds_attr3.close(); /* * Test attribute with group @@ -240,14 +257,32 @@ static void test_attr_rename() try { // Open file - H5File fid1(FILENAME, H5F_ACC_RDWR); + H5File fid1(FILE_BASIC, H5F_ACC_RDWR); + + // Check rename of attribute belonging to a file + + // Change attribute name + fid1.renameAttr(FATTR1_NAME, FATTR_TMP_NAME); + + // Open attribute again + Attribute fattr1(fid1.openAttribute(FATTR_TMP_NAME)); + + // Verify new attribute name + H5std_string fattr_name = fattr1.getName(); + verify_val(fattr_name, FATTR_TMP_NAME, "Attribute::getName", __LINE__, __FILE__); + + int num_attrs = fid1.getNumAttrs(); + verify_val(num_attrs, 2, "Attribute::getNumAttrs", __LINE__, __FILE__); + + // Change first file attribute back to the original name + fid1.renameAttr(FATTR_TMP_NAME, FATTR1_NAME); // Open the dataset DataSet dataset = fid1.openDataSet(DSET1_NAME); - // Check rename + // Check rename of attribute belonging to a dataset - // change attribute name + // Change attribute name dataset.renameAttr(ATTR1_NAME, ATTR_TMP_NAME); // Open attribute again @@ -269,11 +304,11 @@ static void test_attr_rename() attr1.close(); // Open the second attribute - Attribute attr2(dataset.openAttribute(ATTR1A_NAME)); + Attribute attr2(dataset.openAttribute(ATTR2_NAME)); // Verify second attribute name H5std_string attr2_name = attr2.getName(); - verify_val(attr2_name, ATTR1A_NAME, "Attribute::getName", __LINE__, __FILE__); + verify_val(attr2_name, ATTR2_NAME, "Attribute::getName", __LINE__, __FILE__); // Read attribute information immediately, without closing attribute attr2.read (PredType::NATIVE_INT, read_data1); @@ -311,14 +346,14 @@ static void test_attr_basic_read() try { // Open file - H5File fid1(FILENAME, H5F_ACC_RDWR); + H5File fid1(FILE_BASIC, H5F_ACC_RDWR); // Open the dataset DataSet dataset = fid1.openDataSet(DSET1_NAME); // Verify the correct number of attributes int num_attrs = dataset.getNumAttrs(); - verify_val(num_attrs, 2, "H5Object::getNumAttrs", __LINE__, __FILE__); + verify_val(num_attrs, 3, "DataSet::getNumAttrs", __LINE__, __FILE__); // Open an attribute for the dataset Attribute ds_attr=dataset.openAttribute(ATTR1_NAME); @@ -378,7 +413,7 @@ static void test_attr_compound_write() try { // Create file - H5File fid1(FILENAME.c_str(), H5F_ACC_TRUNC); + H5File fid1(FILE_COMPOUND.c_str(), H5F_ACC_TRUNC); // Create dataspace for dataset hsize_t dims1[] = {SPACE1_DIM1, SPACE1_DIM2, SPACE1_DIM3}; @@ -442,14 +477,14 @@ static void test_attr_compound_read() try { // Open file - H5File fid1(FILENAME, H5F_ACC_RDWR); + H5File fid1(FILE_COMPOUND, H5F_ACC_RDWR); // Open the dataset DataSet dataset = fid1.openDataSet(DSET1_NAME); // Verify the correct number of attributes int num_attrs = dataset.getNumAttrs(); - verify_val(num_attrs, 1, "H5Object::getNumAttrs", __LINE__, __FILE__); + verify_val(num_attrs, 1, "DataSet::getNumAttrs", __LINE__, __FILE__); // Open 1st attribute for the dataset Attribute attr = dataset.openAttribute((unsigned)0); @@ -571,7 +606,7 @@ static void test_attr_scalar_write() try { // Create file - H5File fid1(FILENAME, H5F_ACC_TRUNC); + H5File fid1(FILE_SCALAR, H5F_ACC_TRUNC); // Create dataspace for dataset hsize_t dims1[] = {SPACE1_DIM1, SPACE1_DIM2, SPACE1_DIM3}; @@ -625,14 +660,14 @@ static void test_attr_scalar_read() try { // Open file - H5File fid1(FILENAME, H5F_ACC_RDWR); + H5File fid1(FILE_SCALAR, H5F_ACC_RDWR); // Open the dataset DataSet dataset = fid1.openDataSet(DSET1_NAME); // Verify the correct number of attributes int num_attrs = dataset.getNumAttrs(); - verify_val(num_attrs, 1, "H5Object::getNumAttrs", __LINE__, __FILE__); + verify_val(num_attrs, 1, "DataSet::getNumAttrs", __LINE__, __FILE__); // Open an attribute for the dataset Attribute ds_attr=dataset.openAttribute(ATTR5_NAME); @@ -669,7 +704,7 @@ static void test_attr_mult_write() try { // Create file - H5File fid1 (FILENAME, H5F_ACC_TRUNC); + H5File fid1 (FILE_MULTI, H5F_ACC_TRUNC); // Create dataspace for dataset hsize_t dims1[] = {SPACE1_DIM1, SPACE1_DIM2, SPACE1_DIM3}; @@ -746,14 +781,14 @@ static void test_attr_mult_read() try { // Open file - H5File fid1(FILENAME, H5F_ACC_RDWR); + H5File fid1(FILE_MULTI, H5F_ACC_RDWR); // Open the dataset DataSet dataset = fid1.openDataSet(DSET1_NAME); // Verify the correct number of attributes int num_attrs = dataset.getNumAttrs(); - verify_val(num_attrs, 3, "H5Object::getNumAttrs", __LINE__, __FILE__); + verify_val(num_attrs, 3, "DataSet::getNumAttrs", __LINE__, __FILE__); // Open 1st attribute for the dataset Attribute attr = dataset.openAttribute((unsigned)0); @@ -929,22 +964,42 @@ static void test_attr_mult_read() static void test_attr_delete() { H5std_string attr_name; // Buffer for attribute names + int ii; - // Output message about test being performed + // Output message about test being performed SUBTEST("Removing Attribute Function"); try { - // Open file - H5File fid1(FILENAME, H5F_ACC_RDWR); + // Open file. + H5File fid1(FILE_BASIC, H5F_ACC_RDWR); + + // Get the number of file attributes + int num_attrs = fid1.getNumAttrs(); + verify_val(num_attrs, 2, "H5File::getNumAttrs", __LINE__, __FILE__); + + // Delete the second file attribute + fid1.removeAttr(FATTR2_NAME); + + // Get the number of file attributes + num_attrs = fid1.getNumAttrs(); + verify_val(num_attrs, 1, "H5File::getNumAttrs", __LINE__, __FILE__); + + // Verify the name of the only file attribute left + Attribute fattr = fid1.openAttribute((uint)0); + H5std_string attr_name = fattr.getName(); + verify_val(attr_name, FATTR1_NAME, "Attribute::getName", __LINE__, __FILE__); + fattr.close(); + + // Test deleting non-existing attribute // Open the dataset DataSet dataset = fid1.openDataSet(DSET1_NAME); // Verify the correct number of attributes - int num_attrs = dataset.getNumAttrs(); - verify_val(num_attrs, 3, "H5Object::getNumAttrs", __LINE__, __FILE__); + num_attrs = dataset.getNumAttrs(); + verify_val(num_attrs, 3, "DataSet::getNumAttrs", __LINE__, __FILE__); - // Try to delete bogus attribute, should fail. + // Try to delete bogus attribute, should fail try { dataset.removeAttr("Bogus"); @@ -954,16 +1009,18 @@ static void test_attr_delete() catch (AttributeIException E) // catching invalid removing attribute {} // do nothing, exception expected + // Test deleting dataset's attributes + // Verify the correct number of attributes num_attrs = dataset.getNumAttrs(); - verify_val(num_attrs, 3, "H5Object::getNumAttrs", __LINE__, __FILE__); + verify_val(num_attrs, 3, "DataSet::getNumAttrs", __LINE__, __FILE__); // Delete middle (2nd) attribute dataset.removeAttr(ATTR2_NAME); // Verify the correct number of attributes num_attrs = dataset.getNumAttrs(); - verify_val(num_attrs, 2, "H5Object::getNumAttrs", __LINE__, __FILE__); + verify_val(num_attrs, 2, "DataSet::getNumAttrs", __LINE__, __FILE__); // Open 1st attribute for the dataset Attribute attr = dataset.openAttribute((unsigned)0); @@ -989,9 +1046,9 @@ static void test_attr_delete() // Verify the correct number of attributes num_attrs = dataset.getNumAttrs(); - verify_val(num_attrs, 1, "H5Object::getNumAttrs", __LINE__, __FILE__); + verify_val(num_attrs, 1, "DataSet::getNumAttrs", __LINE__, __FILE__); - // Open last (formally 3rd) attribute for the dataset + // Open the only attribute for the dataset (formally 3rd) attr = dataset.openAttribute((unsigned)0); // Verify Name @@ -1005,7 +1062,7 @@ static void test_attr_delete() // Verify the correct number of attributes num_attrs = dataset.getNumAttrs(); - verify_val(num_attrs, 0, "H5Object::getNumAttrs", __LINE__, __FILE__); + verify_val(num_attrs, 0, "DataSet::getNumAttrs", __LINE__, __FILE__); PASSED(); } // end try block @@ -1035,19 +1092,19 @@ static void test_attr_dtype_shared() try { // Create a file - H5File fid1(FILENAME, H5F_ACC_TRUNC); + H5File fid1(FILE_DTYPE, H5F_ACC_TRUNC); // Close file fid1.close(); // Get size of file h5_stat_size_t empty_filesize; // Size of empty file - empty_filesize = h5_get_file_size(FILENAME.c_str(), H5P_DEFAULT); + empty_filesize = h5_get_file_size(FILE_DTYPE.c_str(), H5P_DEFAULT); if (empty_filesize < 0) TestErrPrintf("Line %d: file size wrong!\n", __LINE__); // Open the file again - fid1.openFile(FILENAME, H5F_ACC_RDWR); + fid1.openFile(FILE_DTYPE, H5F_ACC_RDWR); // Enclosing to work around the issue of unused variables and/or // objects created by copy constructors stay around until end of @@ -1120,7 +1177,7 @@ static void test_attr_dtype_shared() fid1.close(); // Open the file again - fid1.openFile(FILENAME, H5F_ACC_RDWR); + fid1.openFile(FILE_DTYPE, H5F_ACC_RDWR); { // Second enclosed block... @@ -1161,7 +1218,7 @@ static void test_attr_dtype_shared() fid1.close(); // Check size of file - filesize = h5_get_file_size(FILENAME.c_str(), H5P_DEFAULT); + filesize = h5_get_file_size(FILE_DTYPE.c_str(), H5P_DEFAULT); verify_val((long)filesize, (long)empty_filesize, "Checking file size", __LINE__, __FILE__); PASSED(); @@ -1192,7 +1249,7 @@ static void test_string_attr() try { // Create file - H5File fid1(FILENAME, H5F_ACC_RDWR); + H5File fid1(FILE_BASIC, H5F_ACC_RDWR); // // Fixed-lenth string attributes @@ -1349,6 +1406,6 @@ extern "C" #endif void cleanup_attr() { - HDremove(FILENAME.c_str()); + //HDremove(FILENAME.c_str()); } diff --git a/c++/test/tfile.cpp b/c++/test/tfile.cpp index df01752..ba38d7a 100644 --- a/c++/test/tfile.cpp +++ b/c++/test/tfile.cpp @@ -333,6 +333,7 @@ static void test_file_open() tmpl1.getSymk( iparm1, iparm2); verify_val(iparm1, F2_SYM_INTERN_K, "FileCreatPropList::getSymk", __LINE__, __FILE__); verify_val(iparm2, F2_SYM_LEAF_K, "FileCreatPropList::getSymk", __LINE__, __FILE__); + PASSED(); } // end of try block @@ -381,8 +382,16 @@ static void test_file_size() hsize_t file_size = file4.getFileSize(); // Check if file size is reasonable. It's supposed to be 2KB now. - if(file_size<1*KB || file_size>4*KB) - issue_fail_msg("test_file_size()", __LINE__, __FILE__); + if (file_size < 1*KB || file_size > 4*KB) + issue_fail_msg("test_file_size()", __LINE__, __FILE__, "getFileSize() returned unreasonable value"); + + // Get the amount of free space in the file + hssize_t free_space = file4.getFreeSpace(); + + // Check if it's reasonable. It's 0 now. + if (free_space < 0 || free_space > 4*KB) + issue_fail_msg("test_file_size()", __LINE__, __FILE__, "getFreeSpace returned unreasonable value"); + PASSED(); } // end of try block @@ -415,7 +424,8 @@ const int NX = 4; const int NY = 5; const H5std_string GROUPNAME ("group"); const H5std_string DSETNAME ("dataset"); -const H5std_string ATTRNAME ("attribute"); +const H5std_string DATTRNAME ("dataset attribute"); +const H5std_string FATTRNAME ("file attribute"); const H5std_string DTYPENAME ("compound"); // Compound datatype @@ -431,17 +441,17 @@ static void test_file_name() H5std_string file_name; try { - // Create a file using default properties. + // Create a file using default properties H5File file4(FILE4, H5F_ACC_TRUNC); - // Get file name from the file instance. + // Get file name from the file instance file_name = file4.getFileName(); verify_val(file_name, FILE4, "H5File::getFileName", __LINE__, __FILE__); // Create a group in the root group Group group(file4.createGroup(GROUPNAME, 0)); - // Get and verify file name + // Get and verify file name via a group file_name = group.getFileName(); verify_val(file_name, FILE4, "Group::getFileName", __LINE__, __FILE__); @@ -452,12 +462,12 @@ static void test_file_name() // Create a new dataset DataSet dataset(file4.createDataSet (DSETNAME, PredType::NATIVE_INT, space)); - // Get and verify file name + // Get and verify file name via a dataset file_name = dataset.getFileName(); verify_val(file_name, FILE4, "DataSet::getFileName", __LINE__, __FILE__); // Create an attribute for the dataset - Attribute attr(dataset.createAttribute(ATTRNAME, PredType::NATIVE_INT, space)); + Attribute attr(dataset.createAttribute(DATTRNAME, PredType::NATIVE_INT, space)); // Get and verify file name file_name = attr.getFileName(); @@ -486,6 +496,116 @@ static void test_file_name() } // test_file_name() +#define NUM_OBJS 4 +#define NUM_ATTRS 3 +const int RANK1 = 1; +const int ATTR1_DIM1 = 3; +const H5std_string FILE5("tfattrs.h5"); +const H5std_string FATTR1_NAME ("file attribute 1"); +const H5std_string FATTR2_NAME ("file attribute 2"); +int fattr_data[ATTR1_DIM1]={512,-234,98123}; /* Test data for file attribute */ +int dattr_data[ATTR1_DIM1]={256,-123,1000}; /* Test data for dataset attribute */ +static void test_file_attribute() +{ + int rdata[ATTR1_DIM1]; + int i; + + // Output message about test being performed + SUBTEST("File Attribute"); + + H5std_string file_name; + try { + // Create a file using default properties. + H5File file5(FILE5, H5F_ACC_TRUNC); + + // Create the data space + hsize_t dims[RANK1] = {ATTR1_DIM1}; + DataSpace space(RANK1, dims); + + // Create two attributes for the file + Attribute fattr1(file5.createAttribute(FATTR1_NAME, PredType::NATIVE_FLOAT, space)); + Attribute fattr2(file5.createAttribute(FATTR2_NAME, PredType::NATIVE_INT, space)); + + fattr2.write(PredType::NATIVE_INT, fattr_data); + + try { + // Try to create the same attribute again (should fail) + Attribute fattr_dup(file5.createAttribute(FATTR2_NAME, PredType::NATIVE_INT, space)); + // Should FAIL but didn't, so throw an invalid action exception + throw InvalidActionException("H5File createAttribute", "Attempted to create an existing attribute."); + } + catch( AttributeIException E ) // catch creating existing attribute + {} // do nothing, FAIL expected + + // Create a new dataset + DataSet dataset(file5.createDataSet (DSETNAME, PredType::NATIVE_INT, space)); + + // Create an attribute for the dataset + Attribute dattr(dataset.createAttribute(DATTRNAME, PredType::NATIVE_INT, space)); + + // Write data to the second file attribute + dattr.write(PredType::NATIVE_INT, dattr_data); + + // Test flushing out the data from the attribute object + dattr.flush(H5F_SCOPE_GLOBAL); + + // Get and verify the number of all objects in the file + // Current: 1 file, 2 file attr, 1 ds, and 1 ds attr. + ssize_t num_objs = file5.getObjCount(H5F_OBJ_ALL); + verify_val(num_objs, 5, "H5File::getObjCount", __LINE__, __FILE__); + + num_objs = file5.getObjCount(H5F_OBJ_GROUP); + verify_val(num_objs, 0, "H5File::getObjCount(H5F_OBJ_GROUP)", __LINE__, __FILE__); + num_objs = file5.getObjCount(H5F_OBJ_DATASET); + verify_val(num_objs, 1, "H5File::getObjCount(H5F_OBJ_DATASET)", __LINE__, __FILE__); + num_objs = file5.getObjCount(H5F_OBJ_ATTR); + verify_val(num_objs, 3, "H5File::getObjCount(H5F_OBJ_ATTR)", __LINE__, __FILE__); + num_objs = file5.getObjCount(H5F_OBJ_DATATYPE); + verify_val(num_objs, 0, "H5File::getObjCount(H5F_OBJ_DATATYPE)", __LINE__, __FILE__); + num_objs = file5.getObjCount(H5F_OBJ_FILE); + verify_val(num_objs, 1, "H5File::getObjCount(H5F_OBJ_FILE)", __LINE__, __FILE__); + + // Get the file name using the attributes + H5std_string fname = fattr1.getFileName(); + verify_val(fname, FILE5, "H5File::getFileName()", __LINE__, __FILE__); + + fname.clear(); + fname = dattr.getFileName(); + verify_val(fname, FILE5, "H5File::getFileName()", __LINE__, __FILE__); + + // Get the class of a file attribute's datatype + H5T_class_t atclass = fattr1.getTypeClass(); + verify_val(atclass, H5T_FLOAT, "Attribute::getTypeClass()", __LINE__, __FILE__); + + // Get and verify the number of attributes attached to a file + int n_attrs = file5.getNumAttrs(); + verify_val(n_attrs, 2, "H5File::getNumAttrs()", __LINE__, __FILE__); + + // Get and verify the number of attributes attached to a dataset + n_attrs = 0; + n_attrs = dataset.getNumAttrs(); + verify_val(n_attrs, 1, "DataSet::getNumAttrs()", __LINE__, __FILE__); + + // Read back attribute's data + HDmemset(rdata, 0, sizeof(rdata)); + dattr.read(PredType::NATIVE_INT, rdata); + /* Check results */ + for (i = 0; i < ATTR1_DIM1; i++) { + if (rdata[i] != dattr_data[i]) { + H5_FAILED(); + cerr << endl; + cerr << "element [" << i << "] is " << rdata[i] << + "but should have been " << dattr_data[i] << endl; + } + } + PASSED(); + } // end of try block + + catch (Exception E) { + issue_fail_msg("test_file_name()", __LINE__, __FILE__, E.getCDetailMsg()); + } +} // test_file_attribute() + /*------------------------------------------------------------------------- * Function: test_file * @@ -513,6 +633,7 @@ void test_file() test_file_open(); // Test file opening test_file_size(); // Test file size test_file_name(); // Test getting file's name + test_file_attribute(); // Test file attribute feature } // test_file() diff --git a/c++/test/trefer.cpp b/c++/test/trefer.cpp index 7f63d33..4eb5b21 100644 --- a/c++/test/trefer.cpp +++ b/c++/test/trefer.cpp @@ -149,34 +149,25 @@ static void test_reference_obj(void) // Create a dataset dataset = file1->createDataSet("Dataset3", PredType::STD_REF_OBJ, sid1); - // Create reference to dataset + // Create reference to dataset and test getRefObjType file1->reference(&wbuf[0], "/Group1/Dataset1"); + H5O_type_t refobj_type = dataset.getRefObjType(&wbuf[0], H5R_OBJECT); + verify_val(refobj_type, H5O_TYPE_DATASET, "DataSet::getRefObjType", __LINE__, __FILE__); -#ifndef H5_NO_DEPRECATED_SYMBOLS - H5G_obj_t obj_type = dataset.getObjType(&wbuf[0], H5R_OBJECT); - verify_val(obj_type, H5G_DATASET, "DataSet::getObjType", __LINE__, __FILE__); -#endif /* H5_NO_DEPRECATED_SYMBOLS */ - - // Create reference to dataset + // Create reference to dataset and test getRefObjType file1->reference(&wbuf[1], "/Group1/Dataset2"); -#ifndef H5_NO_DEPRECATED_SYMBOLS - obj_type = dataset.getObjType(&wbuf[1], H5R_OBJECT); - verify_val(obj_type, H5G_DATASET, "DataSet::getObjType", __LINE__, __FILE__); -#endif /* H5_NO_DEPRECATED_SYMBOLS */ + refobj_type = dataset.getRefObjType(&wbuf[1], H5R_OBJECT); + verify_val(refobj_type, H5O_TYPE_DATASET, "DataSet::getRefObjType", __LINE__, __FILE__); // Create reference to group file1->reference(&wbuf[2], "/Group1"); -#ifndef H5_NO_DEPRECATED_SYMBOLS - obj_type = dataset.getObjType(&wbuf[2], H5R_OBJECT); - verify_val(obj_type, H5G_GROUP, "DataSet::getObjType", __LINE__, __FILE__); -#endif /* H5_NO_DEPRECATED_SYMBOLS */ + refobj_type = dataset.getRefObjType(&wbuf[2], H5R_OBJECT); + verify_val(refobj_type, H5O_TYPE_GROUP, "DataSet::getRefObjType", __LINE__, __FILE__); // Create reference to named datatype file1->reference(&wbuf[3], "/Group1/Datatype1"); -#ifndef H5_NO_DEPRECATED_SYMBOLS - obj_type = dataset.getObjType(&wbuf[3], H5R_OBJECT); - verify_val(obj_type, H5G_TYPE, "DataSet::getObjType", __LINE__, __FILE__); -#endif /* H5_NO_DEPRECATED_SYMBOLS */ + refobj_type = dataset.getRefObjType(&wbuf[3], H5R_OBJECT); + verify_val(refobj_type, H5O_TYPE_NAMED_DATATYPE, "DataSet::getRefObjType", __LINE__, __FILE__); // Write selection to disk dataset.write(wbuf, PredType::STD_REF_OBJ); @@ -253,7 +244,7 @@ static void test_reference_obj(void) // Test getting the type of objects // Test getObjTypeByIdx(hsize_t idx) - obj_type = group.getObjTypeByIdx(0); + H5G_obj_t obj_type = group.getObjTypeByIdx(0); verify_val(obj_type, H5G_DATASET, "Group::getObjTypeByIdx(index)", __LINE__, __FILE__); // Test getObjTypeByIdx(hsize_t idx, char* type_name) @@ -333,6 +324,18 @@ static void test_reference_obj(void) /**************************************************************** ** +** test_reference_compat(): Test basic object reference functionality. +** Tests references to various kinds of objects using deprecated API. +** +****************************************************************/ +static void test_reference_compat(void) +{ + // Not yet +} // test_reference_compat() + + +/**************************************************************** +** ** test_reference(): Main reference testing routine. ** ****************************************************************/ @@ -346,6 +349,7 @@ void test_reference(void) MESSAGE(5, ("Testing References\n")); test_reference_obj(); // Test basic object reference functionality + test_reference_compat(); // Tests deprecated reference routines (not yet) } // test_reference() -- cgit v0.12