From 988e907b113c992cd2878705a5b98b499a4ccf41 Mon Sep 17 00:00:00 2001 From: Binh-Minh Ribler Date: Tue, 20 May 2008 15:24:44 -0500 Subject: [svn-r15046] Purpose: Fixed bugs Description: The class hierarchy was revised to address the problem reported in bugzilla #1068. Classes AbstractDS and Attribute are moved out of H5Object. Class Attribute now multiply inherits from IdComponent and AbstractDs and class DataSet from H5Object and AbstractDs. In addition, data member IdComponent::id was moved into subclasses: Attribute, DataSet, DataSpace, DataType, H5File, Group, and PropList. Also fixed bugzilla 1045: revised Attribute::write and Attribute::read wrappers to handle memory allocation/deallocation properly. (bugzilla 1045) Platforms tested: SunOS 5.10 (linew) Linux 2.6 (kagiso) FreeBSD (duty) --- c++/src/H5AbstractDs.cpp | 12 ++- c++/src/H5AbstractDs.h | 5 +- c++/src/H5Attribute.cpp | 143 ++++++++++++++++++++++--- c++/src/H5Attribute.h | 19 +++- c++/src/H5Classes.h | 1 - c++/src/H5CommonFG.cpp | 33 ++++++ c++/src/H5DataSet.cpp | 115 +++++++++++--------- c++/src/H5DataSet.h | 19 ++-- c++/src/H5DataSpace.cpp | 74 +++++++++++-- c++/src/H5DataSpace.h | 7 ++ c++/src/H5DataType.cpp | 133 +++++++++++++---------- c++/src/H5DataType.h | 18 ++-- c++/src/H5DcreatProp.cpp | 2 +- c++/src/H5DxferProp.h | 1 - c++/src/H5File.cpp | 266 +++++++++++++++++++++++++++++++++++++--------- c++/src/H5File.h | 33 +++++- c++/src/H5Group.cpp | 108 ++++++++++--------- c++/src/H5Group.h | 17 ++- c++/src/H5IdComponent.cpp | 200 +++++++--------------------------- c++/src/H5IdComponent.h | 33 ++---- c++/src/H5Object.cpp | 196 ++++++++++++++++++++++++++++++++-- c++/src/H5Object.h | 29 ++++- c++/src/H5PropList.cpp | 87 +++++++++++---- c++/src/H5PropList.h | 7 ++ 24 files changed, 1063 insertions(+), 495 deletions(-) diff --git a/c++/src/H5AbstractDs.cpp b/c++/src/H5AbstractDs.cpp index 7cb170d..a61cc88 100644 --- a/c++/src/H5AbstractDs.cpp +++ b/c++/src/H5AbstractDs.cpp @@ -34,21 +34,21 @@ namespace H5 { ///\brief Default constructor // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -AbstractDs::AbstractDs() : H5Object() {} +AbstractDs::AbstractDs(){} //-------------------------------------------------------------------------- // Function: AbstractDs default constructor ///\brief Creates an AbstractDs instance using an existing id. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -AbstractDs::AbstractDs( const hid_t ds_id ) : H5Object( ds_id ) {} +AbstractDs::AbstractDs(const hid_t ds_id){} //-------------------------------------------------------------------------- // Function: AbstractDs copy constructor ///\brief Copy constructor: makes a copy of the original AbstractDs object. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -AbstractDs::AbstractDs( const AbstractDs& original ) : H5Object( original ) {} +AbstractDs::AbstractDs(const AbstractDs& original){} //-------------------------------------------------------------------------- // Function: AbstractDs::getTypeClass @@ -80,8 +80,10 @@ H5T_class_t AbstractDs::getTypeClass() const return( type_class ); else { - throw DataTypeIException(inMemFunc("getTypeClass"), - "H5Tget_class returns H5T_NO_CLASS"); + if (fromClass() == "DataSet") + throw DataTypeIException("DataSet::getTypeClass", "H5Tget_class returns H5T_NO_CLASS"); + else if (fromClass() == "Attribute") + throw DataTypeIException("Attribute::getTypeClass", "H5Tget_class returns H5T_NO_CLASS"); } } diff --git a/c++/src/H5AbstractDs.h b/c++/src/H5AbstractDs.h index 2f4e520..c98e5e1 100644 --- a/c++/src/H5AbstractDs.h +++ b/c++/src/H5AbstractDs.h @@ -33,7 +33,7 @@ class FloatType; class IntType; class StrType; class VarLenType; -class H5_DLLCPP AbstractDs : public H5Object { +class H5_DLLCPP AbstractDs { public: // Gets a copy the datatype of that this abstract dataset uses. // Note that this datatype is a generic one and can only be accessed @@ -62,6 +62,9 @@ class H5_DLLCPP AbstractDs : public H5Object { // dataset - pure virtual. virtual hsize_t getStorageSize() const = 0; + // Returns this class name + virtual H5std_string fromClass() const = 0; + // Copy constructor AbstractDs( const AbstractDs& original ); diff --git a/c++/src/H5Attribute.cpp b/c++/src/H5Attribute.cpp index 1bca691..0182816 100644 --- a/c++/src/H5Attribute.cpp +++ b/c++/src/H5Attribute.cpp @@ -27,11 +27,13 @@ #include "H5Object.h" #include "H5AbstractDs.h" #include "H5Attribute.h" +#include "H5FaccProp.h" +#include "H5FcreatProp.h" #include "H5DcreatProp.h" #include "H5CommonFG.h" #include "H5DataType.h" #include "H5DataSpace.h" -#include "H5private.h" +#include "H5File.h" #ifndef H5_NO_NAMESPACE namespace H5 { @@ -46,7 +48,7 @@ namespace H5 { ///\brief Default constructor: Creates a stub attribute // Programmer Binh-Minh Ribler - May, 2004 //-------------------------------------------------------------------------- -Attribute::Attribute() : AbstractDs() {} +Attribute::Attribute() : AbstractDs(), IdComponent(), id(0) {} //-------------------------------------------------------------------------- // Function: Attribute copy constructor @@ -54,7 +56,11 @@ Attribute::Attribute() : AbstractDs() {} ///\param original - IN: Original Attribute object to copy // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -Attribute::Attribute( const Attribute& original ) : AbstractDs( original ) {} +Attribute::Attribute(const Attribute& original) : AbstractDs(), IdComponent() +{ + id = original.getId(); + incRefCount(); // increment number of references to this id +} //-------------------------------------------------------------------------- // Function: Attribute overloaded constructor @@ -64,7 +70,10 @@ Attribute::Attribute( const Attribute& original ) : AbstractDs( original ) {} ///\exception H5::AttributeIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -Attribute::Attribute(const hid_t existing_id) : AbstractDs(existing_id) {} +Attribute::Attribute(const hid_t existing_id) : AbstractDs(), IdComponent() +{ + id = existing_id; +} //-------------------------------------------------------------------------- // Function: Attribute::write @@ -131,22 +140,36 @@ void Attribute::read( const DataType& mem_type, void *buf ) const ///\exception H5::AttributeIException // Programmer Binh-Minh Ribler - Apr, 2003 // Modification -// 2006/12/9 - H5Aread allocates memory for character string -// buffer with malloc, therefore, no allocation here, -// but HDfree is needed. - BMR +// Mar 2008 +// Corrected a misunderstanding that H5Aread would allocate +// space for the buffer. Obtained the attribute size and +// allocated memory properly. - BMR //-------------------------------------------------------------------------- void Attribute::read( const DataType& mem_type, H5std_string& strg ) const { - char* strg_C; // temporary C-string for C API + // Get the attribute size and allocate temporary C-string for C API + hsize_t attr_size = H5Aget_storage_size(id); + if (attr_size <= 0) + { + throw AttributeIException("Attribute::read", "Unable to get attribute size before reading"); + } + char* strg_C = new char [attr_size+1]; + if (strg_C == NULL) + { + throw AttributeIException("Attribute::read", "Unable to allocate buffer to read the attribute"); + } - // call C API to get the attribute string of chars - herr_t ret_value = H5Aread( id, mem_type.getId(), &strg_C); + // Call C API to get the attribute data, a string of chars + herr_t ret_value = H5Aread(id, mem_type.getId(), &strg_C); if( ret_value < 0 ) { throw AttributeIException("Attribute::read", "H5Aread failed"); } - strg = strg_C; // get 'string' from the C char* - HDfree(strg_C); + + // Get 'string' from the C char* and release resource + strg_C[attr_size] = '\0'; + strg = strg_C; + delete []strg_C; } //-------------------------------------------------------------------------- @@ -194,6 +217,23 @@ hid_t Attribute::p_get_type() const } //-------------------------------------------------------------------------- +// Function: Attribute::getFileName +///\brief Gets the name of the file, in which this attribute belongs. +///\return File name +///\exception H5::IdComponentException +// Programmer Binh-Minh Ribler - Jul, 2004 +//-------------------------------------------------------------------------- +H5std_string Attribute::getFileName() const +{ + try { + return(p_get_file_name()); + } + catch (IdComponentException E) { + throw FileIException("Attribute::getFileName", E.getDetailMsg()); + } +} + +//-------------------------------------------------------------------------- // Function: Attribute::getName ///\brief Gets the name of this attribute, returning its length. ///\param buf_size - IN: Desired length of the name @@ -283,6 +323,70 @@ hsize_t Attribute::getStorageSize() const } //-------------------------------------------------------------------------- +// Function: Attribute::dereference +// Purpose Dereference a ref into a DataSet object. +// Parameters +// ref - IN: Reference pointer +// Exception H5::IdComponentException +// Programmer Binh-Minh Ribler - Oct, 2006 +// Modification +// May 2008 - BMR +// Moved from IdComponent into H5File, H5Object, and Attribute +//-------------------------------------------------------------------------- +Attribute::Attribute(H5Object& obj, void* ref) : AbstractDs(), IdComponent() +{ + id = obj.p_dereference(ref); +} + +Attribute::Attribute(H5File& h5file, void* ref) : AbstractDs(), IdComponent() +{ + id = h5file.p_dereference(ref); +} + +//-------------------------------------------------------------------------- +// Function: Attribute::getId +// Purpose: Get the id of this attribute +// Description: +// Class hierarchy is revised to address bugzilla 1068. Class +// AbstractDS and Attribute are moved out of H5Object. In +// addition, member IdComponent::id is moved into subclasses, and +// IdComponent::getId now becomes pure virtual function. +// Programmer Binh-Minh Ribler - May, 2008 +//-------------------------------------------------------------------------- +hid_t Attribute::getId() const +{ + return(id); +} + +//-------------------------------------------------------------------------- +// Function: Attribute::setId +///\brief Sets the identifier of this object to a new value. +/// +///\exception H5::IdComponentException when the attempt to close the HDF5 +/// object fails +// Description: +// The underlaying reference counting in the C library ensures +// that the current valid id of this object is properly closed. +// Then the object's id is reset to the new id. +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +void Attribute::setId(const hid_t new_id) +{ + // handling references to this old id + try { + close(); + } + catch (Exception close_error) { + throw AttributeIException("Attribute::setId", close_error.getDetailMsg()); + } + // reset object's id to the given id + id = new_id; + + // increment the reference counter of the new id + incRefCount(); +} + +//-------------------------------------------------------------------------- // Function: Attribute::close ///\brief Closes this attribute. /// @@ -315,11 +419,18 @@ void Attribute::close() //-------------------------------------------------------------------------- Attribute::~Attribute() { - try { - close(); + int counter = getCounter(id); + if (counter > 1) + { + decRefCount(id); } - catch (Exception close_error) { - cerr << "Attribute::~Attribute - " << close_error.getDetailMsg() << endl; + else if (counter == 1) + { + try { + close(); + } catch (Exception close_error) { + cerr << "Attribute::~Attribute - " << close_error.getDetailMsg() << endl; + } } } diff --git a/c++/src/H5Attribute.h b/c++/src/H5Attribute.h index 513b6ab..49faeea 100644 --- a/c++/src/H5Attribute.h +++ b/c++/src/H5Attribute.h @@ -21,11 +21,14 @@ namespace H5 { #endif -class H5_DLLCPP Attribute : public AbstractDs { +class H5_DLLCPP Attribute : public AbstractDs, public IdComponent { public: // Closes this attribute. virtual void close(); + // Gets the name of the file, in which this attribute belongs. + H5std_string getFileName() const; + // Gets the name of this attribute. ssize_t getName( size_t buf_size, H5std_string& attr_name ) const; H5std_string getName( size_t buf_size ) const; // returns name, not its length @@ -45,11 +48,15 @@ class H5_DLLCPP Attribute : public AbstractDs { void write(const DataType& mem_type, const void *buf ) const; void write(const DataType& mem_type, const H5std_string& strg ) const; + // Creates an attribute by way of dereference. + Attribute(H5Object& obj, void* ref); + Attribute(H5File& file, void* ref); + // Returns this class name virtual H5std_string fromClass () const { return("Attribute"); } - // Creates a copy of an existing attribute using the attribute id - Attribute( const hid_t attr_id ); + // Creates a copy of an existing attribute using the attribute id + Attribute( const hid_t attr_id ); // Copy constructor: makes a copy of an existing Attribute object. Attribute( const Attribute& original ); @@ -57,10 +64,16 @@ class H5_DLLCPP Attribute : public AbstractDs { // Default constructor Attribute(); + // Gets/Sets the attribute id. + virtual hid_t getId() const; + virtual void setId(const hid_t new_id); + // Destructor: properly terminates access to this attribute. virtual ~Attribute(); private: + hid_t id; // HDF5 attribute id + // This function contains the common code that is used by // getTypeClass and various API functions getXxxType // defined in AbstractDs for generic datatype and specific diff --git a/c++/src/H5Classes.h b/c++/src/H5Classes.h index 03f1257..f691548 100644 --- a/c++/src/H5Classes.h +++ b/c++/src/H5Classes.h @@ -21,7 +21,6 @@ namespace H5 { #endif class Exception; - class ReferenceCounter; class IdComponent; class H5Object; class PropList; diff --git a/c++/src/H5CommonFG.cpp b/c++/src/H5CommonFG.cpp index 8386e03..364f639 100644 --- a/c++/src/H5CommonFG.cpp +++ b/c++/src/H5CommonFG.cpp @@ -15,6 +15,11 @@ #include +// remove when done +#include + using std::cerr; + using std::endl; + #include "H5Include.h" #include "H5Exception.h" #include "H5IdComponent.h" @@ -239,7 +244,13 @@ DataSet CommonFG::openDataSet( const H5std_string& name ) const ///\par Description /// Note that both names are interpreted relative to the /// specified location. +/// For information on creating hard link and soft link, please +/// refer to the C layer Reference Manual at: +/// http://hdfgroup.org/HDF5/doc/RM/RM_H5L.html#Link-CreateHard and +/// http://hdfgroup.org/HDF5/doc/RM/RM_H5L.html#Link-CreateSoft // Programmer Binh-Minh Ribler - 2000 +// Modification +// 2007: QAK modified to use H5L APIs - BMR //-------------------------------------------------------------------------- void CommonFG::link( H5L_type_t link_type, const char* curr_name, const char* new_name ) const { @@ -283,6 +294,8 @@ void CommonFG::link( H5L_type_t link_type, const H5std_string& curr_name, const ///\param name - IN: Name of the object to be removed ///\exception H5::FileIException or H5::GroupIException // Programmer Binh-Minh Ribler - 2000 +// Modification +// 2007: QAK modified to use H5L APIs - BMR //-------------------------------------------------------------------------- void CommonFG::unlink( const char* name ) const { @@ -317,6 +330,8 @@ void CommonFG::unlink( const H5std_string& name ) const /// to the Group Interface in the HDF5 User's Guide at: /// http://hdf.ncsa.uiuc.edu/HDF5/doc/Groups.html // Programmer Binh-Minh Ribler - 2000 +// Modification +// 2007: QAK modified to use H5L APIs - BMR //-------------------------------------------------------------------------- void CommonFG::move( const char* src, const char* dst ) const { @@ -380,6 +395,7 @@ void CommonFG::getObjinfo( const H5std_string& name, hbool_t follow_link, H5G_st /// It differs from the above functions in that it doesn't have /// the paramemter \a follow_link. // Programmer Binh-Minh Ribler - Nov, 2005 +// Note: need to modify to use H5Oget_info and H5Lget_info - BMR //-------------------------------------------------------------------------- void CommonFG::getObjinfo( const char* name, H5G_stat_t& statbuf ) const { @@ -452,6 +468,11 @@ H5std_string CommonFG::getLinkval( const H5std_string& name, size_t size ) const /// object header, e.g., data sets, groups, named data types, /// and data spaces, but not symbolic links. // Programmer Binh-Minh Ribler - 2000 +// Modification +// 2007: QAK modified to use H5O APIs; however the first parameter is +// no longer just file or group, this function should be moved +// to another class to accommodate attribute, dataset, and named +// datatype. - BMR //-------------------------------------------------------------------------- void CommonFG::setComment( const char* name, const char* comment ) const { @@ -480,6 +501,10 @@ void CommonFG::setComment( const H5std_string& name, const H5std_string& comment ///\param name - IN: Name of the object ///\exception H5::FileIException or H5::GroupIException // Programmer Binh-Minh Ribler - May 2005 +// 2007: QAK modified to use H5O APIs; however the first parameter is +// no longer just file or group, this function should be moved +// to another class to accommodate attribute, dataset, and named +// datatype. - BMR //-------------------------------------------------------------------------- void CommonFG::removeComment(const char* name) const { @@ -509,6 +534,10 @@ void CommonFG::removeComment(const H5std_string& name) const ///\return Comment string ///\exception H5::FileIException or H5::GroupIException // Programmer Binh-Minh Ribler - May 2005 +// 2007: QAK modified to use H5O APIs; however the first parameter is +// no longer just file or group, this function should be moved +// to another class to accommodate attribute, dataset, and named +// datatype. - BMR //-------------------------------------------------------------------------- H5std_string CommonFG::getComment (const H5std_string& name) const { @@ -549,6 +578,10 @@ H5std_string CommonFG::getComment (const H5std_string& name) const ///\return Comment string ///\exception H5::FileIException or H5::GroupIException // Programmer Binh-Minh Ribler - 2000 +// 2007: QAK modified to use H5O APIs; however the first parameter is +// no longer just file or group, this function should be moved +// to another class to accommodate attribute, dataset, and named +// datatype. - BMR //-------------------------------------------------------------------------- H5std_string CommonFG::getComment( const char* name, size_t bufsize ) const { diff --git a/c++/src/H5DataSet.cpp b/c++/src/H5DataSet.cpp index 9858e7d..be80949 100644 --- a/c++/src/H5DataSet.cpp +++ b/c++/src/H5DataSet.cpp @@ -28,10 +28,13 @@ #include "H5PropList.h" #include "H5DxferProp.h" #include "H5DcreatProp.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" #ifndef H5_NO_NAMESPACE @@ -47,7 +50,7 @@ namespace H5 { ///\brief Default constructor: creates a stub DataSet. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -DataSet::DataSet() : AbstractDs() {} +DataSet::DataSet() : AbstractDs(), H5Object(), id(0) {} //-------------------------------------------------------------------------- // Function: DataSet overloaded constructor @@ -55,7 +58,10 @@ DataSet::DataSet() : AbstractDs() {} ///\param existing_id - IN: Id of an existing dataset // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -DataSet::DataSet(const hid_t existing_id) : AbstractDs(existing_id) {} +DataSet::DataSet(const hid_t existing_id) : AbstractDs(), H5Object() +{ + id = existing_id; +} //-------------------------------------------------------------------------- // Function: DataSet copy constructor @@ -63,7 +69,12 @@ DataSet::DataSet(const hid_t existing_id) : AbstractDs(existing_id) {} ///\param original - IN: DataSet instance to copy // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -DataSet::DataSet( const DataSet& original ) : AbstractDs( original ) {} +DataSet::DataSet(const DataSet& original) : AbstractDs(original), H5Object() +{ + id = original.getId(); + incRefCount(); // increment number of references to this id + +} //-------------------------------------------------------------------------- // Function: DataSet overload constructor - dereference @@ -77,9 +88,14 @@ DataSet::DataSet( const DataSet& original ) : AbstractDs( original ) {} /// is a datatype that has been named by DataType::commit. // Programmer Binh-Minh Ribler - Oct, 2006 //-------------------------------------------------------------------------- -DataSet::DataSet(IdComponent& obj, void* ref) : AbstractDs() +DataSet::DataSet(H5Object& obj, void* ref) : AbstractDs(), H5Object() { - IdComponent::dereference(obj, ref); + id = obj.p_dereference(ref); +} + +DataSet::DataSet(H5File& h5file, void* ref) : AbstractDs(), H5Object() +{ + id = h5file.p_dereference(ref); } //-------------------------------------------------------------------------- @@ -455,52 +471,6 @@ void DataSet::fillMemBuf(void *buf, DataType& buf_type, DataSpace& space) } } -//-------------------------------------------------------------------------- -// Function: DataSet::Reference -///\brief Important!!! - This functions may not work correctly, it -/// will be removed in the near future. Please use -/// DataSet::reference instead! -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void* DataSet::Reference(const char* name, DataSpace& dataspace, H5R_type_t ref_type) const -{ - try { - return(p_reference(name, dataspace.getId(), ref_type)); - } - catch (IdComponentException E) { - throw DataSetIException("DataSet::Reference", E.getDetailMsg()); - } -} - -//-------------------------------------------------------------------------- -// Function: DataSet::Reference -///\brief Important!!! - This functions may not work correctly, it -/// will be removed in the near future. Please use similar -/// DataSet::reference instead! -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void* DataSet::Reference(const char* name) const -{ - try { - return(p_reference(name, -1, H5R_OBJECT)); - } - catch (IdComponentException E) { - throw DataSetIException("DataSet::Reference", E.getDetailMsg()); - } -} - -//-------------------------------------------------------------------------- -// Function: DataSet::Reference -///\brief Important!!! - This functions may not work correctly, it -/// will be removed in the near future. Please use similar -/// DataSet::reference instead! -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void* DataSet::Reference(const H5std_string& name) const -{ - return(Reference(name.c_str())); -} - #ifndef H5_NO_DEPRECATED_SYMBOLS //-------------------------------------------------------------------------- // Function: DataSet::getObjType @@ -550,6 +520,49 @@ DataSpace DataSet::getRegion(void *ref, H5R_type_t ref_type) const } //-------------------------------------------------------------------------- +// Function: DataSet::getId +// Purpose: Get the id of this attribute +// Description: +// Class hierarchy is revised to address bugzilla 1068. Class +// AbstractDs and Attribute are moved out of H5Object. In +// addition, member IdComponent::id is moved into subclasses, and +// IdComponent::getId now becomes pure virtual function. +// Programmer Binh-Minh Ribler - May, 2008 +//-------------------------------------------------------------------------- +hid_t DataSet::getId() const +{ + return(id); +} + +//-------------------------------------------------------------------------- +// Function: DataSet::setId +///\brief Sets the identifier of this object to a new value. +/// +///\exception H5::IdComponentException when the attempt to close the HDF5 +/// object fails +// Description: +// The underlaying reference counting in the C library ensures +// that the current valid id of this object is properly closed. +// Then the object's id is reset to the new id. +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +void DataSet::setId(const hid_t new_id) +{ + // handling references to this old id + try { + close(); + } + catch (Exception close_error) { + throw DataSetIException(inMemFunc("setId"), close_error.getDetailMsg()); + } + // reset object's id to the given id + id = new_id; + + // increment the reference counter of the new id + incRefCount(); +} + +//-------------------------------------------------------------------------- // Function: DataSet::close ///\brief Closes this dataset. /// diff --git a/c++/src/H5DataSet.h b/c++/src/H5DataSet.h index 7323f52..f968932 100644 --- a/c++/src/H5DataSet.h +++ b/c++/src/H5DataSet.h @@ -23,7 +23,7 @@ namespace H5 { #endif -class H5_DLLCPP DataSet : public AbstractDs { +class H5_DLLCPP DataSet : public H5Object, public AbstractDs { public: // Close this dataset. virtual void close(); @@ -81,19 +81,12 @@ class H5_DLLCPP DataSet : public AbstractDs { // Retrieves a dataspace with the region pointed to selected. DataSpace getRegion(void *ref, H5R_type_t ref_type = H5R_DATASET_REGION) const; - // Creates a reference to a named Hdf5 object or to a dataset region - // in this object. - void* Reference(const char* name, DataSpace& dataspace, H5R_type_t ref_type = H5R_DATASET_REGION) const; - - // Creates a reference to a named Hdf5 object in this object. - void* Reference(const char* name) const; // will be obsolete - void* Reference(const H5std_string& name) const; // will be obsolete - // Returns this class name virtual H5std_string fromClass () const { return("DataSet"); } // Creates a dataset by way of dereference. - DataSet(IdComponent& obj, void* ref); + DataSet(H5Object& obj, void* ref); + DataSet(H5File& file, void* ref); // Default constructor. DataSet(); @@ -104,10 +97,16 @@ class H5_DLLCPP DataSet : public AbstractDs { // Creates a copy of an existing DataSet using its id. DataSet(const hid_t existing_id); + // Gets the dataset id. + virtual hid_t getId() const; + virtual void setId(const hid_t new_id); + // Destructor: properly terminates access to this dataset. virtual ~DataSet(); private: + hid_t id; // HDF5 dataset id + // This function contains the common code that is used by // getTypeClass and various API functions getXxxType // defined in AbstractDs for generic datatype and specific diff --git a/c++/src/H5DataSpace.cpp b/c++/src/H5DataSpace.cpp index 245e27d..adeb2db 100644 --- a/c++/src/H5DataSpace.cpp +++ b/c++/src/H5DataSpace.cpp @@ -47,7 +47,7 @@ const DataSpace DataSpace::ALL( H5S_ALL ); ///\exception H5::DataSpaceIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -DataSpace::DataSpace( H5S_class_t type ) : IdComponent(0) +DataSpace::DataSpace(H5S_class_t type) : IdComponent() { id = H5Screate( type ); if( id < 0 ) @@ -65,7 +65,7 @@ DataSpace::DataSpace( H5S_class_t type ) : IdComponent(0) ///\exception H5::DataSpaceIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -DataSpace::DataSpace( int rank, const hsize_t * dims, const hsize_t * maxdims) : IdComponent(0) +DataSpace::DataSpace( int rank, const hsize_t * dims, const hsize_t * maxdims) : IdComponent() { id = H5Screate_simple( rank, dims, maxdims ); if( id < 0 ) @@ -82,7 +82,10 @@ DataSpace::DataSpace( int rank, const hsize_t * dims, const hsize_t * maxdims) : ///\exception H5::DataSpaceIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -DataSpace::DataSpace(const hid_t existing_id) : IdComponent(existing_id) {} +DataSpace::DataSpace(const hid_t existing_id) : IdComponent() +{ + id = existing_id; +} //-------------------------------------------------------------------------- // Function: DataSpace copy constructor @@ -90,7 +93,11 @@ DataSpace::DataSpace(const hid_t existing_id) : IdComponent(existing_id) {} ///\param original - IN: DataSpace object to copy // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -DataSpace::DataSpace( const DataSpace& original ) : IdComponent( original ) {} +DataSpace::DataSpace(const DataSpace& original) : IdComponent(original) +{ + id = original.getId(); + incRefCount(); // increment number of references to this id +} //-------------------------------------------------------------------------- // Function: DataSpace::copy @@ -548,6 +555,50 @@ void DataSpace::selectHyperslab( H5S_seloper_t op, const hsize_t *count, const h } //-------------------------------------------------------------------------- +// Function: DataSpace::getId +// Purpose: Get the id of this attribute +// Modification: +// May 2008 - BMR +// Class hierarchy is revised to address bugzilla 1068. Class +// AbstractDS and Attribute are moved out of H5Object. In +// addition, member IdComponent::id is moved into subclasses, and +// IdComponent::getId now becomes pure virtual function. +// Programmer Binh-Minh Ribler - May, 2008 +//-------------------------------------------------------------------------- +hid_t DataSpace::getId() const +{ + return(id); +} + +//-------------------------------------------------------------------------- +// Function: DataSpace::setId +///\brief Sets the identifier of this object to a new value. +/// +///\exception H5::IdComponentException when the attempt to close the HDF5 +/// object fails +// Description: +// The underlaying reference counting in the C library ensures +// that the current valid id of this object is properly closed. +// Then the object's id is reset to the new id. +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +void DataSpace::setId(const hid_t new_id) +{ + // handling references to this old id + try { + close(); + } + catch (Exception close_error) { + throw DataSpaceIException(inMemFunc("setId"), close_error.getDetailMsg()); + } + // reset object's id to the given id + id = new_id; + + // increment the reference counter of the new id + incRefCount(); +} + +//-------------------------------------------------------------------------- // Function: DataSpace::close ///\brief Closes this dataspace. /// @@ -581,11 +632,16 @@ void DataSpace::close() //-------------------------------------------------------------------------- DataSpace::~DataSpace() { - try { - close(); - } - catch (Exception close_error) { - cerr << "DataSpace::~DataSpace - " << close_error.getDetailMsg() << endl; + int counter = getCounter(id); + if (counter > 1) + decRefCount(id); + else if (counter == 1) + { + try { + close(); + } catch (Exception close_error) { + cerr << "DataSpace::~DataSpace - " << close_error.getDetailMsg() << endl; + } } } diff --git a/c++/src/H5DataSpace.h b/c++/src/H5DataSpace.h index e1c5ba4..1173c26 100644 --- a/c++/src/H5DataSpace.h +++ b/c++/src/H5DataSpace.h @@ -112,8 +112,15 @@ class H5_DLLCPP DataSpace : public IdComponent { // Copy constructor: makes a copy of the original DataSpace object. DataSpace(const DataSpace& original); + // Gets the dataspace id. + virtual hid_t getId() const; + virtual void setId(const hid_t new_id); + // Destructor: properly terminates access to this dataspace. virtual ~DataSpace(); + + private: + hid_t id; // HDF5 dataspace id }; #ifndef H5_NO_NAMESPACE } diff --git a/c++/src/H5DataType.cpp b/c++/src/H5DataType.cpp index c31395e..592d800 100644 --- a/c++/src/H5DataType.cpp +++ b/c++/src/H5DataType.cpp @@ -62,7 +62,10 @@ namespace H5 { // - BMR 5/2004 // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -DataType::DataType(const hid_t existing_id) : H5Object(existing_id) {} +DataType::DataType(const hid_t existing_id) : H5Object() +{ + id = existing_id; +} //-------------------------------------------------------------------------- // Function: DataType overloaded constructor @@ -92,9 +95,19 @@ DataType::DataType( const H5T_class_t type_class, size_t size ) : H5Object() /// is a datatype that has been named by DataType::commit. // Programmer Binh-Minh Ribler - Oct, 2006 //-------------------------------------------------------------------------- -DataType::DataType(IdComponent& obj, void* ref) : H5Object() + /* DataType::DataType(IdComponent& obj, void* ref) : H5Object() +{ + H5Object::dereference(obj, ref); +} + */ +DataType::DataType(H5Object& obj, void* ref) : H5Object() { - IdComponent::dereference(obj, ref); + id = obj.p_dereference(ref); +} + +DataType::DataType(H5File& file, void* ref) : H5Object() +{ + id = file.p_dereference(ref); } //-------------------------------------------------------------------------- @@ -102,14 +115,18 @@ DataType::DataType(IdComponent& obj, void* ref) : H5Object() ///\brief Default constructor: Creates a stub datatype // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -DataType::DataType() : H5Object() {} +DataType::DataType() : H5Object(), id(0) {} //-------------------------------------------------------------------------- // Function: DataType copy constructor ///\brief Copy constructor: makes a copy of the original DataType object. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -DataType::DataType(const DataType& original) : H5Object(original) {} +DataType::DataType(const DataType& original) : H5Object(original) +{ + id = original.getId(); + incRefCount(); // increment number of references to this id +} //-------------------------------------------------------------------------- // Function: DataType::copy @@ -616,52 +633,6 @@ bool DataType::isVariableStr() const } } -//-------------------------------------------------------------------------- -// Function: DataType::Reference -///\brief Important!!! - This functions may not work correctly, it -/// will be removed in the near future. Please use similar -/// DataType::reference instead! -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void* DataType::Reference(const char* name, DataSpace& dataspace, H5R_type_t ref_type) const -{ - try { - return(p_reference(name, dataspace.getId(), ref_type)); - } - catch (IdComponentException E) { - throw DataTypeIException(inMemFunc("Reference"), E.getDetailMsg()); - } -} - -//-------------------------------------------------------------------------- -// Function: DataType::Reference -///\brief Important!!! - This functions may not work correctly, it -/// will be removed in the near future. Please use similar -/// DataType::reference instead! -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void* DataType::Reference(const char* name) const -{ - try { - return(p_reference(name, -1, H5R_OBJECT)); - } - catch (IdComponentException E) { - throw DataTypeIException(inMemFunc("Reference"), E.getDetailMsg()); - } -} - -//-------------------------------------------------------------------------- -// Function: DataType::Reference -///\brief Important!!! - This functions may not work correctly, it -/// will be removed in the near future. Please use similar -/// DataType::reference instead! -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void* DataType::Reference(const H5std_string& name) const -{ - return(Reference(name.c_str())); -} - #ifndef H5_NO_DEPRECATED_SYMBOLS //-------------------------------------------------------------------------- // Function: DataType::getObjType @@ -708,6 +679,50 @@ DataSpace DataType::getRegion(void *ref, H5R_type_t ref_type) const } //-------------------------------------------------------------------------- +// Function: DataType::getId +// Purpose: Get the id of this attribute +// Modification: +// May 2008 - BMR +// Class hierarchy is revised to address bugzilla 1068. Class +// AbstractDS and Attribute are moved out of H5Object. In +// addition, member IdComponent::id is moved into subclasses, and +// IdComponent::getId now becomes pure virtual function. +// Programmer Binh-Minh Ribler - May, 2008 +//-------------------------------------------------------------------------- +hid_t DataType::getId() const +{ + return(id); +} + +//-------------------------------------------------------------------------- +// Function: DataType::setId +///\brief Sets the identifier of this object to a new value. +/// +///\exception H5::IdComponentException when the attempt to close the HDF5 +/// object fails +// Description: +// The underlaying reference counting in the C library ensures +// that the current valid id of this object is properly closed. +// Then the object's id is reset to the new id. +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +void DataType::setId(const hid_t new_id) +{ + // handling references to this old id + try { + close(); + } + catch (Exception close_error) { + throw DataTypeIException(inMemFunc("setId"), close_error.getDetailMsg()); + } + // reset object's id to the given id + id = new_id; + + // increment the reference counter of the new id + incRefCount(); +} + +//-------------------------------------------------------------------------- // Function: DataType::close ///\brief Closes the datatype if it is not a predefined type. /// @@ -740,14 +755,20 @@ void DataType::close() //-------------------------------------------------------------------------- DataType::~DataType() { - try { - close(); + int counter = getCounter(id); + if (counter > 1) + { + decRefCount(id); } - catch (Exception close_error) { - cerr << inMemFunc("~DataType - ") << close_error.getDetailMsg() << endl; + else if (counter == 1) + { + try { + close(); + } catch (Exception close_error) { + cerr << inMemFunc("~DataType - ") << close_error.getDetailMsg() << endl; + } } } - #ifndef H5_NO_NAMESPACE } // end namespace #endif diff --git a/c++/src/H5DataType.h b/c++/src/H5DataType.h index 588a57a..509dd49 100644 --- a/c++/src/H5DataType.h +++ b/c++/src/H5DataType.h @@ -30,7 +30,8 @@ class H5_DLLCPP DataType : public H5Object { DataType( const DataType& original ); // Creates a datatype by way of dereference. - DataType(IdComponent& obj, void* ref); + DataType(H5Object& obj, void* ref); + DataType(H5File& file, void* ref); // Closes this datatype. virtual void close(); @@ -99,14 +100,6 @@ class H5_DLLCPP DataType : public H5Object { // Checks whether this datatype is a variable-length string. bool isVariableStr() const; - // Creates a reference to a named HDF5 object or to a dataset region - // in this object. - void* Reference(const char* name, DataSpace& dataspace, H5R_type_t ref_type = H5R_DATASET_REGION) const; // will be obsolete - - // Creates a reference to a named HDF5 object in this object. - void* Reference(const char* name) const; // will be obsolete - void* Reference(const H5std_string& name) const; // will be obsolete - #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; @@ -124,8 +117,15 @@ class H5_DLLCPP DataType : public H5Object { // Default constructor DataType(); + // Gets the datatype id. + virtual hid_t getId() const; + virtual void setId(const hid_t new_id); + // Destructor: properly terminates access to this datatype. virtual ~DataType(); + + protected: + hid_t id; // HDF5 datatype id private: void p_commit(hid_t loc_id, const char* name); }; diff --git a/c++/src/H5DcreatProp.cpp b/c++/src/H5DcreatProp.cpp index e524355..19a4e6e 100644 --- a/c++/src/H5DcreatProp.cpp +++ b/c++/src/H5DcreatProp.cpp @@ -54,7 +54,7 @@ DSetCreatPropList::DSetCreatPropList( const DSetCreatPropList& orig ) : PropList /// existing dataset creation property list. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -DSetCreatPropList::DSetCreatPropList(const hid_t plist_id) : PropList( plist_id ) {} +DSetCreatPropList::DSetCreatPropList(const hid_t plist_id) : PropList(plist_id) {} //-------------------------------------------------------------------------- // Function: DSetCreatPropList::setChunk diff --git a/c++/src/H5DxferProp.h b/c++/src/H5DxferProp.h index 8da29c4..11e15fc 100644 --- a/c++/src/H5DxferProp.h +++ b/c++/src/H5DxferProp.h @@ -104,7 +104,6 @@ class H5_DLLCPP DSetMemXferPropList : public PropList { // Noop destructor virtual ~DSetMemXferPropList(); - }; #ifndef H5_NO_NAMESPACE } diff --git a/c++/src/H5File.cpp b/c++/src/H5File.cpp index aa11e31..6448467 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(0) {} +H5File::H5File() : IdComponent(), id(0) {} //-------------------------------------------------------------------------- // Function: H5File overloaded constructor @@ -140,7 +140,11 @@ 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) : IdComponent(original) +{ + id = original.getId(); + incRefCount(); // increment number of references to this id +} //-------------------------------------------------------------------------- // Function: H5File::flush @@ -502,52 +506,6 @@ H5std_string H5File::getFileName() const } } -//-------------------------------------------------------------------------- -// Function: H5File::Reference -///\brief Important!!! - This functions does not work correctly, it -/// will be removed in the near future. Please use -/// H5File::reference instead! -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void* H5File::Reference(const char* name, DataSpace& dataspace, H5R_type_t ref_type) const -{ - try { - return(p_reference(name, dataspace.getId(), ref_type)); - } - catch (IdComponentException E) { - throw FileIException("H5File::Reference", E.getDetailMsg()); - } -} - -//-------------------------------------------------------------------------- -// Function: H5File::Reference -///\brief Important!!! - This functions does not work correctly, it -/// will be removed in the near future. Please use similar -/// H5File::reference instead! -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void* H5File::Reference(const char* name) const -{ - try { - return(p_reference(name, -1, H5R_OBJECT)); - } - catch (IdComponentException E) { - throw FileIException("H5File::Reference", E.getDetailMsg()); - } -} - -//-------------------------------------------------------------------------- -// Function: H5File::Reference -///\brief Important!!! - This functions does not work correctly, it -/// will be removed in the near future. Please use similar -/// H5File::reference instead! -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void* H5File::Reference(const H5std_string& name) const -{ - return(Reference(name.c_str())); -} - #ifndef H5_NO_DEPRECATED_SYMBOLS //-------------------------------------------------------------------------- // Function: H5File::getObjType @@ -617,6 +575,159 @@ hsize_t H5File::getFileSize() const } //-------------------------------------------------------------------------- +// Function: H5File::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 H5File::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: 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 \tReference is an object reference. +/// \li \c H5R_DATASET_REGION \tReference 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 std::string for the object's name. +///\param ref - IN: Reference pointer +///\param name - IN: Name of the object to be referenced - \c std::string +// Programmer Binh-Minh Ribler - May, 2004 +//-------------------------------------------------------------------------- +void H5File::reference(void* ref, const H5std_string& name) const +{ + reference(ref, name.c_str()); +} + +//-------------------------------------------------------------------------- +// Function: H5File::dereference +// Purpose Dereference a ref into a DataSet object. +// Parameters +// ref - IN: Reference pointer +// Exception H5::IdComponentException +// Programmer Binh-Minh Ribler - Oct, 2006 +// Modification +// May 2008 - BMR +// Moved from IdComponent into H5File and H5Object +//-------------------------------------------------------------------------- +void H5File::dereference(H5Object& obj, void* ref) +{ + hid_t temp_id; + try { + temp_id = p_dereference(ref); + } + catch (ReferenceException ref_err) { + throw (inMemFunc("dereference"), ref_err.getDetailMsg()); + } + + // No failure, set id to the object passed in + obj.setId(temp_id); +} + +#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) +// 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 H5File::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); +} + +//-------------------------------------------------------------------------- // Function: H5File::getLocId // Purpose: Get the id of this file // Description @@ -630,6 +741,50 @@ hid_t H5File::getLocId() const } //-------------------------------------------------------------------------- +// Function: H5File::getId +// Purpose: Get the id of this attribute +// Modification: +// May 2008 - BMR +// Class hierarchy is revised to address bugzilla 1068. Class +// AbstractDS and Attribute are moved out of H5Object. In +// addition, member IdComponent::id is moved into subclasses, and +// IdComponent::getId now becomes pure virtual function. +// Programmer Binh-Minh Ribler - May, 2008 +//-------------------------------------------------------------------------- +hid_t H5File::getId() const +{ + return(id); +} + +//-------------------------------------------------------------------------- +// Function: H5File::setId +///\brief Sets the identifier of this object to a new value. +/// +///\exception H5::IdComponentException when the attempt to close the HDF5 +/// object fails +// Description: +// The underlaying reference counting in the C library ensures +// that the current valid id of this object is properly closed. +// Then the object's id is reset to the new id. +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +void H5File::setId(const hid_t new_id) +{ + // handling references to this old id + try { + close(); + } + catch (Exception E) { + throw FileIException("H5File::setId", E.getDetailMsg()); + } + // reset object's id to the given id + id = new_id; + + // increment the reference counter of the new id + incRefCount(); +} + +//-------------------------------------------------------------------------- // Function: H5File::close ///\brief Closes this HDF5 file. /// @@ -683,11 +838,18 @@ void H5File::throwException(const H5std_string& func_name, const H5std_string& m //-------------------------------------------------------------------------- H5File::~H5File() { - try { - close(); + int counter = getCounter(id); + if (counter > 1) + { + decRefCount(id); } - catch (Exception close_error) { - cerr << "H5File::~H5File - " << close_error.getDetailMsg() << endl; + else if (counter == 1) + { + try { + close(); + } catch (Exception close_error) { + cerr << "H5File::~H5File - " << close_error.getDetailMsg() << endl; + } } } diff --git a/c++/src/H5File.h b/c++/src/H5File.h index b239efe..2badcc7 100644 --- a/c++/src/H5File.h +++ b/c++/src/H5File.h @@ -89,11 +89,15 @@ class H5_DLLCPP H5File : public IdComponent, public CommonFG { // Creates a reference to a named HDF5 object or to a dataset region // in this object. - void* Reference(const char* name, DataSpace& dataspace, H5R_type_t ref_type = H5R_DATASET_REGION) const; // will be obsolete + 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; - // Creates a reference to a named Hdf5 object in this object. - void* Reference(const char* name) const; // will be obsolete - void* Reference(const H5std_string& name) const; // will be obsolete + //change prototype: void dereference(IdComponent& obj, void* ref); + // may need individual - remove when done + // Open a referenced HDF5 object. + void dereference(H5Object& obj, void* ref); // Returns this class name virtual H5std_string fromClass () const { return("H5File"); } @@ -110,14 +114,35 @@ class H5_DLLCPP H5File : public IdComponent, public CommonFG { // Copy constructor: makes a copy of the original H5File object. H5File(const H5File& original); + // Gets/Sets the HDF5 file id. + virtual hid_t getId() const; + virtual void setId(const hid_t new_id); + // H5File destructor. virtual ~H5File(); private: + hid_t id; // HDF5 file id + +#ifndef DOXYGEN_SHOULD_SKIP_THIS + // This function is private and contains common code between the // constructors taking a string or a char* void p_get_file( const char* name, unsigned int flags, const FileCreatPropList& create_plist, const FileAccPropList& access_plist ); + // 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; + +#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 + }; #ifndef H5_NO_NAMESPACE } diff --git a/c++/src/H5Group.cpp b/c++/src/H5Group.cpp index f3c058a..7ab3b61 100644 --- a/c++/src/H5Group.cpp +++ b/c++/src/H5Group.cpp @@ -50,7 +50,7 @@ namespace H5 { ///\brief Default constructor: creates a stub Group. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -Group::Group() : H5Object() {} +Group::Group() : H5Object(), id(0) {} //-------------------------------------------------------------------------- // Function: Group copy constructor @@ -58,7 +58,11 @@ Group::Group() : H5Object() {} ///\param original - IN: Original group to copy // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -Group::Group( const Group& original ) : H5Object( original ) {} +Group::Group(const Group& original) : H5Object(original) +{ + id = original.getId(); + incRefCount(); // increment number of references to this id +} //-------------------------------------------------------------------------- // Function: Group::getLocId @@ -77,7 +81,10 @@ hid_t Group::getLocId() const ///\param group_id - IN: Id of an existing group // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -Group::Group( const hid_t group_id ) : H5Object( group_id ) {} +Group::Group(const hid_t existing_id) : H5Object() +{ + id = existing_id; +} //-------------------------------------------------------------------------- // Function: Group overload constructor - dereference @@ -89,55 +96,14 @@ Group::Group( const hid_t group_id ) : H5Object( group_id ) {} /// is a datatype that has been named by DataType::commit. // Programmer Binh-Minh Ribler - Oct, 2006 //-------------------------------------------------------------------------- -Group::Group(IdComponent& obj, void* ref) : H5Object() +Group::Group(H5Object& obj, void* ref) : H5Object() { - IdComponent::dereference(obj, ref); -} - -//-------------------------------------------------------------------------- -// Function: Group::Reference -///\brief Important!!! - This functions may not work correctly, it -/// will be removed in the near future. Please use similar -/// Group::reference instead! -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void* Group::Reference(const char* name, DataSpace& dataspace, H5R_type_t ref_type) const -{ - try { - return(p_reference(name, dataspace.getId(), ref_type)); - } - catch (IdComponentException E) { - throw GroupIException("Group::Reference", E.getDetailMsg()); - } + id = obj.p_dereference(ref); } -//-------------------------------------------------------------------------- -// Function: Group::Reference -///\brief Important!!! - This functions may not work correctly, it -/// will be removed in the near future. Please use similar -/// Group::reference instead! -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void* Group::Reference(const char* name) const +Group::Group(H5File& h5file, void* ref) : H5Object() { - try { - return(p_reference(name, -1, H5R_OBJECT)); - } - catch (IdComponentException E) { - throw GroupIException("Group::Reference", E.getDetailMsg()); - } -} - -//-------------------------------------------------------------------------- -// Function: Group::Reference -///\brief Important!!! - This functions may not work correctly, it -/// will be removed in the near future. Please use similar -/// Group::reference instead! -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void* Group::Reference(const H5std_string& name) const -{ - return(Reference(name.c_str())); + id = h5file.p_dereference(ref); } #ifndef H5_NO_DEPRECATED_SYMBOLS @@ -188,6 +154,50 @@ DataSpace Group::getRegion(void *ref, H5R_type_t ref_type) const } //-------------------------------------------------------------------------- +// Function: Group::getId +// Purpose: Get the id of this attribute +// Modification: +// May 2008 - BMR +// Class hierarchy is revised to address bugzilla 1068. Class +// AbstractDS and Attribute are moved out of H5Object. In +// addition, member IdComponent::id is moved into subclasses, and +// IdComponent::getId now becomes pure virtual function. +// Programmer Binh-Minh Ribler - May, 2008 +//-------------------------------------------------------------------------- +hid_t Group::getId() const +{ + return(id); +} + +//-------------------------------------------------------------------------- +// Function: Group::setId +///\brief Sets the identifier of this object to a new value. +/// +///\exception H5::IdComponentException when the attempt to close the HDF5 +/// object fails +// Description: +// The underlaying reference counting in the C library ensures +// that the current valid id of this object is properly closed. +// Then the object's id is reset to the new id. +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +void Group::setId(const hid_t new_id) +{ + // handling references to this old id + try { + close(); + } + catch (Exception close_error) { + throw GroupIException("Group::setId", close_error.getDetailMsg()); + } + // reset object's id to the given id + id = new_id; + + // increment the reference counter of the new id + incRefCount(); +} + +//-------------------------------------------------------------------------- // Function: Group::close ///\brief Closes this group. /// @@ -196,6 +206,8 @@ DataSpace Group::getRegion(void *ref, H5R_type_t ref_type) const //-------------------------------------------------------------------------- void Group::close() { + /* cerr << "Group::close/p_valid_id" << endl; + */ if (p_valid_id(id)) { herr_t ret_value = H5Gclose( id ); diff --git a/c++/src/H5Group.h b/c++/src/H5Group.h index 5df85bb..091ea72 100644 --- a/c++/src/H5Group.h +++ b/c++/src/H5Group.h @@ -34,14 +34,6 @@ class H5_DLLCPP Group : public H5Object, public CommonFG { // Retrieves a dataspace with the region pointed to selected. DataSpace getRegion(void *ref, H5R_type_t ref_type = H5R_DATASET_REGION) const; - // Creates a reference to a named Hdf5 object or to a dataset region - // in this object. - void* Reference(const char* name, DataSpace& dataspace, H5R_type_t ref_type = H5R_DATASET_REGION) const; // will be obsolete - - // Creates a reference to a named Hdf5 object in this object. - void* Reference(const char* name) const; // will be obsolete - void* Reference(const H5std_string& name) const; // will be obsolete - // Returns this class name virtual H5std_string fromClass () const { return("Group"); } @@ -52,7 +44,8 @@ class H5_DLLCPP Group : public H5Object, public CommonFG { virtual hid_t getLocId() const; // Creates a group by way of dereference. - Group(IdComponent& obj, void* ref); + Group(H5Object& obj, void* ref); + Group(H5File& obj, void* ref); // default constructor Group(); @@ -60,12 +53,18 @@ class H5_DLLCPP Group : public H5Object, public CommonFG { // Copy constructor: makes a copy of the original object Group(const Group& original); + // Gets the group id. + virtual hid_t getId() const; + virtual void setId(const hid_t new_id); + // Destructor virtual ~Group(); // Creates a copy of an existing group using its id. Group( const hid_t group_id ); + private: + hid_t id; // HDF5 group id }; #ifndef H5_NO_NAMESPACE } diff --git a/c++/src/H5IdComponent.cpp b/c++/src/H5IdComponent.cpp index bb015fb..fa2f3db 100644 --- a/c++/src/H5IdComponent.cpp +++ b/c++/src/H5IdComponent.cpp @@ -18,7 +18,6 @@ #endif /*H5_VMS*/ #include - #include "H5Include.h" #include "H5Exception.h" #include "H5Library.h" @@ -36,7 +35,7 @@ namespace H5 { ///\exception H5::DataTypeIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -IdComponent::IdComponent(const hid_t h5_id) : id(h5_id) {} +IdComponent::IdComponent(const hid_t h5_id) {} //-------------------------------------------------------------------------- // Function: IdComponent copy constructor @@ -44,11 +43,7 @@ IdComponent::IdComponent(const hid_t h5_id) : id(h5_id) {} ///\param original - IN: IdComponent instance to copy // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -IdComponent::IdComponent( const IdComponent& original ) -{ - id = original.id; - incRefCount(); // increment number of references to this id -} +IdComponent::IdComponent( const IdComponent& original ) {} //-------------------------------------------------------------------------- // Function: IdComponent::incRefCount @@ -69,7 +64,7 @@ void IdComponent::incRefCount(const hid_t obj_id) const //-------------------------------------------------------------------------- void IdComponent::incRefCount() const { - incRefCount(id); + incRefCount(getId()); } //-------------------------------------------------------------------------- @@ -99,7 +94,7 @@ void IdComponent::decRefCount(const hid_t obj_id) const //-------------------------------------------------------------------------- void IdComponent::decRefCount() const { - decRefCount(id); + decRefCount(getId()); } //-------------------------------------------------------------------------- @@ -128,7 +123,7 @@ int IdComponent::getCounter(const hid_t obj_id) const //-------------------------------------------------------------------------- int IdComponent::getCounter() const { - return (getCounter(id)); + return (getCounter(getId())); } //-------------------------------------------------------------------------- @@ -173,7 +168,7 @@ IdComponent& IdComponent::operator=( const IdComponent& rhs ) if (this != &rhs) { // handling references to this id - try { + try { close(); } catch (Exception close_error) { @@ -181,10 +176,9 @@ IdComponent& IdComponent::operator=( const IdComponent& rhs ) } // copy the data members from the rhs object - id = rhs.id; - - // increment the reference counter - incRefCount(); + setId(rhs.getId()); + /* id = rhs.id; + */ } return *this; } @@ -201,6 +195,7 @@ IdComponent& IdComponent::operator=( const IdComponent& rhs ) // Then the object's id is reset to the new id. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- +#if 0 void IdComponent::setId(const hid_t new_id) { // handling references to this old id @@ -212,11 +207,14 @@ void IdComponent::setId(const hid_t new_id) } // reset object's id to the given id - id = new_id; + /* id = new_id; + */ + setId(new_id); // increment the reference counter of the new id incRefCount(); } +#endif //-------------------------------------------------------------------------- // Function: IdComponent::getId @@ -224,10 +222,12 @@ void IdComponent::setId(const hid_t new_id) ///\return HDF5 id // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- +/* hid_t IdComponent::getId () const { return(id); } +*/ //-------------------------------------------------------------------------- // Function: IdComponent destructor @@ -273,7 +273,10 @@ H5std_string IdComponent::inMemFunc(const char* func_name) const ///\brief Default constructor. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -IdComponent::IdComponent() : id(-1) {} +IdComponent::IdComponent() { + /* setId(-1); + */ +} //-------------------------------------------------------------------------- // Function: IdComponent::p_get_file_name (protected) @@ -287,8 +290,10 @@ IdComponent::IdComponent() : id(-1) {} //-------------------------------------------------------------------------- H5std_string IdComponent::p_get_file_name() const { + hid_t temp_id = getId(); + // Preliminary call to H5Fget_name to get the length of the file name - ssize_t name_size = H5Fget_name(id, NULL, 0); + ssize_t name_size = H5Fget_name(temp_id, NULL, 0); // If H5Aget_name returns a negative value, raise an exception, if( name_size < 0 ) @@ -298,7 +303,7 @@ H5std_string IdComponent::p_get_file_name() const // Call H5Fget_name again to get the actual file name char* name_C = new char[name_size+1]; // temporary C-string for C API - name_size = H5Fget_name(id, name_C, name_size+1); + name_size = H5Fget_name(temp_id, name_C, name_size+1); // Check for failure again if( name_size < 0 ) @@ -313,83 +318,21 @@ H5std_string IdComponent::p_get_file_name() const } //-------------------------------------------------------------------------- -// Function: IdComponent::p_reference (protected) -// Purpose Creates a reference to an HDF5 object or a dataset region. +// Function: H5Object::p_dereference (protected) +// Purpose Opens the HDF5 object referenced. // 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 +// ref - IN: Reference pointer +// Exception H5::IdComponentException +// Programmer Binh-Minh Ribler - Oct, 2006 //-------------------------------------------------------------------------- -void IdComponent::p_reference(void* ref, const char* name, hid_t space_id, H5R_type_t ref_type) const +hid_t IdComponent::p_dereference(void* ref) { - herr_t ret_value = H5Rcreate(ref, id, name, ref_type, space_id); - if (ret_value < 0) + hid_t temp_id = H5Rdereference(getId(), H5R_OBJECT, ref); + if (temp_id < 0) { - throw IdComponentException("", "H5Rcreate failed"); + throw ReferenceException("", "H5Rdereference failed"); } -} - -//-------------------------------------------------------------------------- -// Function: IdComponent::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 \tReference is an object reference. -/// \li \c H5R_DATASET_REGION \tReference is a dataset region -/// reference. - this is the default -///\exception H5::IdComponentException -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void IdComponent::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("IdComponent::reference", E.getDetailMsg()); - } -} - -//-------------------------------------------------------------------------- -// Function: IdComponent::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 IdComponent::reference(void* ref, const char* name) const -{ - try { - p_reference(ref, name, -1, H5R_OBJECT); - } - catch (IdComponentException E) { - throw IdComponentException("IdComponent::reference", E.getDetailMsg()); - } -} - -//-------------------------------------------------------------------------- -// Function: IdComponent::reference -///\brief This is an overloaded function, provided for your convenience. -/// It differs from the above function in that it takes an -/// \c std::string for the object's name. -///\param ref - IN: Reference pointer -///\param name - IN: Name of the object to be referenced - \c std::string -// Programmer Binh-Minh Ribler - May, 2004 -//-------------------------------------------------------------------------- -void IdComponent::reference(void* ref, const H5std_string& name) const -{ - reference(ref, name.c_str()); + return(temp_id); } //-------------------------------------------------------------------------- @@ -406,84 +349,17 @@ void IdComponent::reference(void* ref, const H5std_string& name) const // BMR - Oct 8, 2006 // Programmer Binh-Minh Ribler - May, 2004 //-------------------------------------------------------------------------- -void* IdComponent::p_reference(const char* name, hid_t space_id, H5R_type_t ref_type) const + /* void* IdComponent::p_reference(const char* name, hid_t space_id, H5R_type_t ref_type) const { hobj_ref_t ref; - herr_t ret_value = H5Rcreate(&ref, id, name, ref_type, space_id); + herr_t ret_value = H5Rcreate(&ref, getId(), name, ref_type, space_id); if (ret_value < 0) { throw IdComponentException("", "H5Rcreate failed"); } return (reinterpret_cast(ref)); } - -//-------------------------------------------------------------------------- -// Function: IdComponent::dereference -// Purpose Opens the HDF5 object referenced. -// Parameters -// obj - IN: Dataset reference object is in or location of -// object that the dataset is located within. -// ref - IN: Reference pointer -// Exception H5::IdComponentException -// Programmer Binh-Minh Ribler - Oct, 2006 -//-------------------------------------------------------------------------- -void IdComponent::dereference(IdComponent& obj, void* ref) -{ - id = H5Rdereference(obj.getId(), H5R_OBJECT, ref); - if (id < 0) - { - throw IdComponentException("", "H5Rdereference failed"); - } -} - -#ifndef H5_NO_DEPRECATED_SYMBOLS -//-------------------------------------------------------------------------- -// Function: IdComponent::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 IdComponent::p_get_obj_type(void *ref, H5R_type_t ref_type) const -{ - H5G_obj_t obj_type = H5Rget_obj_type1(id, ref_type, ref); - - if (obj_type == H5G_UNKNOWN) - { - throw IdComponentException("", "H5Rget_obj_type failed"); - } - return(obj_type); -} -#endif /* H5_NO_DEPRECATED_SYMBOLS */ - -//-------------------------------------------------------------------------- -// Function: IdComponent::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 IdComponent::p_get_region(void *ref, H5R_type_t ref_type) const -{ - hid_t space_id = H5Rget_region(id, ref_type, ref); - if (space_id < 0) - { - throw IdComponentException("", "H5Rget_region failed"); - } - return(space_id); -} - + */ // // Local functions used in this class // @@ -495,7 +371,7 @@ hid_t IdComponent::p_get_region(void *ref, H5R_type_t ref_type) const // Return true if id is valid, false, otherwise // Programmer Binh-Minh Ribler - May, 2005 //-------------------------------------------------------------------------- -bool IdComponent::p_valid_id(const hid_t obj_id) const +bool IdComponent::p_valid_id(const hid_t obj_id) { H5I_type_t id_type = H5Iget_type(obj_id); if (id_type <= H5I_BADID || id_type >= H5I_NTYPES) diff --git a/c++/src/H5IdComponent.h b/c++/src/H5IdComponent.h index facdefd..c34b2da 100644 --- a/c++/src/H5IdComponent.h +++ b/c++/src/H5IdComponent.h @@ -44,16 +44,14 @@ class H5_DLLCPP IdComponent { // Assignment operator. IdComponent& operator=( const IdComponent& rhs ); - 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; + // Opens the HDF5 object referenced. + hid_t p_dereference(void* ref); - // Open a referenced HDF5 object. - void dereference(IdComponent& obj, void* ref); + // Gets the identifier of this object. + virtual hid_t getId () const = 0; // Sets the identifier of this object to a new value. - void setId(const hid_t new_id); + virtual void setId(const hid_t new_id) = 0; // Creates an object to hold an HDF5 identifier. IdComponent( const hid_t h5_id ); @@ -61,9 +59,6 @@ class H5_DLLCPP IdComponent { // Copy constructor: makes copy of the original IdComponent object. IdComponent( const IdComponent& original ); - // Gets the value of IdComponent's data member. - virtual hid_t getId () const; - #ifndef DOXYGEN_SHOULD_SKIP_THIS // Pure virtual function for there are various H5*close for the // subclasses. @@ -83,7 +78,6 @@ class H5_DLLCPP IdComponent { protected: #ifndef DOXYGEN_SHOULD_SKIP_THIS - hid_t id; // HDF5 object id // Default constructor. IdComponent(); @@ -91,23 +85,8 @@ class H5_DLLCPP IdComponent { // Gets the name of the file, in which an HDF5 object belongs. H5std_string p_get_file_name() const; - // 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; - void* p_reference(const char* name, hid_t space_id, H5R_type_t ref_type) const; // will be removed - -#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; - // Verifies that the given id is valid. - bool p_valid_id(const hid_t obj_id) const; + static bool p_valid_id(const hid_t obj_id); #endif // DOXYGEN_SHOULD_SKIP_THIS diff --git a/c++/src/H5Object.cpp b/c++/src/H5Object.cpp index c027f51..de0f2d5 100644 --- a/c++/src/H5Object.cpp +++ b/c++/src/H5Object.cpp @@ -21,10 +21,15 @@ #include "H5PropList.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 @@ -103,7 +108,7 @@ Attribute H5Object::createAttribute( const char* name, const DataType& data_type 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(id, name, type_id, space_id, plist_id, H5P_DEFAULT ); + 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 ) @@ -137,7 +142,7 @@ Attribute H5Object::createAttribute( const H5std_string& name, const DataType& d //-------------------------------------------------------------------------- Attribute H5Object::openAttribute( const char* name ) const { - hid_t attr_id = H5Aopen( id, name, H5P_DEFAULT ); + hid_t attr_id = H5Aopen(getId(), name, H5P_DEFAULT); if( attr_id > 0 ) { Attribute attr( attr_id ); @@ -171,7 +176,8 @@ Attribute H5Object::openAttribute( const H5std_string& name ) const //-------------------------------------------------------------------------- Attribute H5Object::openAttribute( const unsigned int idx ) const { - hid_t attr_id = H5Aopen_by_idx(id, ".", H5_INDEX_CRT_ORDER, H5_ITER_INC, (hsize_t)idx, H5P_DEFAULT, H5P_DEFAULT); + 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 ); @@ -209,7 +215,8 @@ int H5Object::iterateAttrs( attr_operator_t user_op, unsigned *_idx, void *op_da // call the C library routine H5Aiterate2 to iterate the attributes hsize_t idx = (hsize_t)*_idx; - int ret_value = H5Aiterate2(id, H5_INDEX_NAME, H5_ITER_INC, &idx, userAttrOpWrpr, (void *) userData); + int ret_value = H5Aiterate2(getId(), H5_INDEX_NAME, H5_ITER_INC, &idx, + userAttrOpWrpr, (void *) userData); // release memory delete userData; @@ -235,7 +242,7 @@ int H5Object::getNumAttrs() const { H5O_info_t oinfo; /* Object info */ - if(H5Oget_info(id, &oinfo) < 0) + if(H5Oget_info(getId(), &oinfo) < 0) throw AttributeIException(inMemFunc("getNumAttrs"), "H5Oget_info failed"); else return( (int)oinfo.num_attrs ); @@ -250,7 +257,7 @@ int H5Object::getNumAttrs() const //-------------------------------------------------------------------------- void H5Object::removeAttr( const char* name ) const { - herr_t ret_value = H5Adelete(id, name); + herr_t ret_value = H5Adelete(getId(), name); if( ret_value < 0 ) throw AttributeIException(inMemFunc("removeAttr"), "H5Adelete failed"); } @@ -277,7 +284,7 @@ void H5Object::removeAttr( const H5std_string& name ) const //-------------------------------------------------------------------------- void H5Object::renameAttr(const char* oldname, const char* newname) const { - herr_t ret_value = H5Arename(id, oldname, newname); + herr_t ret_value = H5Arename(getId(), oldname, newname); if (ret_value < 0) throw AttributeIException(inMemFunc("renameAttr"), "H5Arename failed"); } @@ -306,9 +313,9 @@ void H5Object::renameAttr(const H5std_string& oldname, const H5std_string& newna /// This object is used to identify the file to be flushed. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -void H5Object::flush(H5F_scope_t scope ) const +void H5Object::flush(H5F_scope_t scope) const { - herr_t ret_value = H5Fflush( id, scope ); + herr_t ret_value = H5Fflush(getId(), scope); if( ret_value < 0 ) { throw FileIException(inMemFunc("flush"), "H5Fflush failed"); @@ -333,6 +340,177 @@ H5std_string H5Object::getFileName() const } //-------------------------------------------------------------------------- +// 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 \tReference is an object reference. +/// \li \c H5R_DATASET_REGION \tReference 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 std::string for the object's name. +///\param ref - IN: Reference pointer +///\param name - IN: Name of the object to be referenced - \c std::string +// Programmer Binh-Minh Ribler - May, 2004 +//-------------------------------------------------------------------------- +void H5Object::reference(void* ref, const H5std_string& name) const +{ + reference(ref, name.c_str()); +} + +//-------------------------------------------------------------------------- +// Function: H5Object::dereference +// Purpose Dereference a ref into a DataSet object. +// Parameters +// ref - IN: Reference pointer +// Exception H5::IdComponentException +// Programmer Binh-Minh Ribler - Oct, 2006 +// Modification +// May 2008 - BMR +// Moved from IdComponent into H5File and H5Object +//-------------------------------------------------------------------------- +void H5Object::dereference(H5File& h5file, void* ref) +{ + hid_t temp_id; + try { + temp_id = h5file.p_dereference(ref); + } + catch (ReferenceException ref_err) { + throw (inMemFunc("dereference"), ref_err.getDetailMsg()); + } + + // No failure, set id to the object + /* obj.setId(temp_id); + */ + setId(temp_id); +} + +void H5Object::dereference(H5Object& obj, void* ref) +{ + hid_t temp_id; + try { + temp_id = obj.p_dereference(ref); + } + catch (ReferenceException ref_err) { + throw (inMemFunc("dereference"), ref_err.getDetailMsg()); + } + + // No failure, set id to the object + 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); +} + + +//-------------------------------------------------------------------------- // Function: H5Object destructor ///\brief Noop destructor. // Programmer Binh-Minh Ribler - 2000 diff --git a/c++/src/H5Object.h b/c++/src/H5Object.h index 77de529..49655a1 100644 --- a/c++/src/H5Object.h +++ b/c++/src/H5Object.h @@ -20,8 +20,7 @@ #include "H5Classes.h" // constains forward class declarations // H5Object is a baseclass. It has these subclasses: -// Group, AbstractDs, and DataType. -// AbstractDs, in turn, has subclasses DataSet and Attribute. +// Group, DataSet, and DataType. // DataType, in turn, has several specific datatypes as subclasses. #ifndef H5_NO_NAMESPACE @@ -80,6 +79,17 @@ class H5_DLLCPP H5Object : public IdComponent { 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. + void dereference(H5File& h5file, void* ref); + void dereference(H5Object& obj, void* ref); + // Copy constructor: makes copy of an H5Object object. H5Object(const H5Object& original); @@ -93,6 +103,21 @@ 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; + +#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/H5PropList.cpp b/c++/src/H5PropList.cpp index 136fb3b..64c1743 100644 --- a/c++/src/H5PropList.cpp +++ b/c++/src/H5PropList.cpp @@ -25,6 +25,7 @@ #include "H5Exception.h" #include "H5IdComponent.h" #include "H5PropList.h" +#include "H5private.h" // for HDfree #ifndef H5_NO_NAMESPACE namespace H5 { @@ -44,7 +45,7 @@ const PropList PropList::DEFAULT( H5P_DEFAULT ); ///\brief Default constructor: creates a stub property list object. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -PropList::PropList() : IdComponent( 0 ) {} +PropList::PropList() : IdComponent(), id(0) {} //-------------------------------------------------------------------------- // Function: PropList copy constructor @@ -52,7 +53,11 @@ PropList::PropList() : IdComponent( 0 ) {} ///\param original - IN: The original property list to copy // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -PropList::PropList( const PropList& original ) : IdComponent( original ) {} +PropList::PropList(const PropList& original) : IdComponent(original) +{ + id = original.getId(); + incRefCount(); // increment number of references to this id +} //-------------------------------------------------------------------------- // Function: PropList overloaded constructor @@ -68,7 +73,7 @@ PropList::PropList( const PropList& original ) : IdComponent( original ) {} // description was what I came up with from reading the code. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -PropList::PropList( const hid_t plist_id ) : IdComponent(0) +PropList::PropList( const hid_t plist_id ) : IdComponent() { if (H5I_GENPROP_CLS == H5Iget_type(plist_id)) { // call C routine to create the new property @@ -157,7 +162,7 @@ void PropList::copyProp(PropList& dest, const char *name) const /// It differs from the above function only in what arguments it /// accepts. ///\param dest - IN: Destination property list or class -///\param name - IN: Name of the property to copy - \c std::string +///\param name - IN: Name of the property to copy - \c H5std_string // Programmer Binh-Minh Ribler - Jul, 2005 //-------------------------------------------------------------------------- void PropList::copyProp( PropList& dest, const H5std_string& name ) const @@ -194,7 +199,7 @@ void PropList::copyProp( PropList& dest, PropList& src, const char *name ) const /// accepts. - Obsolete ///\param dest - IN: Destination property list or class ///\param src - IN: Source property list or class -///\param name - IN: Name of the property to copy - \c std::string +///\param name - IN: Name of the property to copy - \c H5std_string // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- void PropList::copyProp( PropList& dest, PropList& src, const H5std_string& name ) const @@ -203,6 +208,49 @@ void PropList::copyProp( PropList& dest, PropList& src, const H5std_string& name } //-------------------------------------------------------------------------- +// Function: PropList::getId +// Purpose: Get the id of this attribute +// Description: +// Class hierarchy is revised to address bugzilla 1068. Class +// AbstractDS and Attribute are moved out of H5Object. In +// addition, member IdComponent::id is moved into subclasses, and +// IdComponent::getId now becomes pure virtual function. +// Programmer Binh-Minh Ribler - May, 2008 +//-------------------------------------------------------------------------- +hid_t PropList::getId() const +{ + return(id); +} + +//-------------------------------------------------------------------------- +// Function: PropList::setId +///\brief Sets the identifier of this object to a new value. +/// +///\exception H5::IdComponentException when the attempt to close the HDF5 +/// object fails +// Description: +// The underlaying reference counting in the C library ensures +// that the current valid id of this object is properly closed. +// Then the object's id is reset to the new id. +// Programmer Binh-Minh Ribler - 2000 +//-------------------------------------------------------------------------- +void PropList::setId(const hid_t new_id) +{ + // handling references to this old id + try { + close(); + } + catch (Exception close_error) { + throw PropListIException(inMemFunc("setId"), close_error.getDetailMsg()); + } + // reset object's id to the given id + id = new_id; + + // increment the reference counter of the new id + incRefCount(); +} + +//-------------------------------------------------------------------------- // Function: PropList::close ///\brief Closes the property list if it is not a default one. /// @@ -273,7 +321,7 @@ bool PropList::propExist(const char* name ) const ///\brief This is an overloaded member function, provided for convenience. /// It differs from the above function only in what arguments it /// accepts. -///\param name - IN: Name of property to check for - \c std::string +///\param name - IN: Name of property to check for - \c H5std_string // Programmer: Binh-Minh Ribler - April, 2004 //-------------------------------------------------------------------------- bool PropList::propExist(const H5std_string& name ) const @@ -326,7 +374,7 @@ void PropList::getProperty(const char* name, void* value) const /// It differs from the above function only in what arguments it /// accepts. ///\param name - IN: Name of property to query - \c char pointer -///\return The property that is a \c std::string. +///\return The property that is a \c H5std_string. ///\exception H5::PropListIException // Programmer: Binh-Minh Ribler - April, 2004 //-------------------------------------------------------------------------- @@ -343,7 +391,7 @@ H5std_string PropList::getProperty(const char* name) const } // Return propety value as a string after deleting temp C-string - H5std_string prop_strg = H5std_string(prop_strg_C); + H5std_string prop_strg(prop_strg_C); delete []prop_strg_C; return (prop_strg); } @@ -352,7 +400,7 @@ H5std_string PropList::getProperty(const char* name) const ///\brief This is an overloaded member function, provided for convenience. /// It differs from the above function only in what arguments it /// accepts. -///\param name - IN: Name of property to query - \c std::string +///\param name - IN: Name of property to query - \c H5std_string ///\param value - OUT: Pointer to the buffer for the property value // Programmer: Binh-Minh Ribler - April, 2004 //-------------------------------------------------------------------------- @@ -365,8 +413,8 @@ void PropList::getProperty(const H5std_string& name, void* value) const ///\brief This is an overloaded member function, provided for convenience. /// It differs from the above function only in what arguments it /// accepts. -///\param name - IN: Name of property to query - \c std::string -///\return The property that is a \c std::string. +///\param name - IN: Name of property to query - \c H5std_string +///\return The property that is a \c H5std_string. // Programmer: Binh-Minh Ribler - April, 2004 //-------------------------------------------------------------------------- H5std_string PropList::getProperty(const H5std_string& name) const @@ -402,7 +450,7 @@ size_t PropList::getPropSize(const char *name) const ///\brief This is an overloaded member function, provided for convenience. /// It differs from the above function only in what arguments it /// accepts. -///\param name - IN: Name of property to query - \c std::string +///\param name - IN: Name of property to query - \c H5std_string /// // Programmer: Binh-Minh Ribler - April, 2004 //-------------------------------------------------------------------------- @@ -421,12 +469,13 @@ size_t PropList::getPropSize(const H5std_string& name) const H5std_string PropList::getClassName() const { char* temp_str; - temp_str = H5Pget_class_name(id); + temp_str = H5Pget_class_name(id); // this API specified that temp_str must + // be freed. if (temp_str != NULL) { - H5std_string class_name = H5std_string(temp_str); - free(temp_str); + H5std_string class_name(temp_str); + HDfree(temp_str); return(class_name); } else @@ -489,7 +538,7 @@ void PropList::setProperty(const char* name, const char* charptr) const /// It differs from the above function only in what arguments it /// accepts. ///\param name - IN: Name of property to set - \c char pointer -///\param strg - IN: Value for the property is a \c std::string +///\param strg - IN: Value for the property is a \c H5std_string // Programmer: Binh-Minh Ribler - April, 2004 //-------------------------------------------------------------------------- void PropList::setProperty(const char* name, H5std_string& strg) const @@ -502,7 +551,7 @@ void PropList::setProperty(const char* name, H5std_string& strg) const ///\brief This is an overloaded member function, provided for convenience. /// It differs from the above function only in what arguments it /// accepts. -///\param name - IN: Name of property to set - \c std::string +///\param name - IN: Name of property to set - \c H5std_string ///\param value - IN: Void pointer to the value for the property // Programmer: Binh-Minh Ribler - April, 2004 //-------------------------------------------------------------------------- @@ -516,7 +565,7 @@ void PropList::setProperty(const H5std_string& name, void* value) const ///\brief This is an overloaded member function, provided for convenience. /// It differs from the above function only in what arguments it /// accepts. -///\param name - IN: Name of property to set - \c std::string +///\param name - IN: Name of property to set - \c H5std_string ///\param strg - IN: Value for the property is a \c std::string // Programmer: Binh-Minh Ribler - April, 2004 //-------------------------------------------------------------------------- @@ -569,7 +618,7 @@ void PropList::removeProp(const char *name) const ///\brief This is an overloaded member function, provided for convenience. /// It differs from the above function only in what arguments it /// accepts. -///\param name - IN: Name of property to remove - \c std::string +///\param name - IN: Name of property to remove - \c H5std_string // Programmer: Binh-Minh Ribler - April, 2004 //-------------------------------------------------------------------------- void PropList::removeProp(const H5std_string& name) const diff --git a/c++/src/H5PropList.h b/c++/src/H5PropList.h index 09ea6f1..75a3d21 100644 --- a/c++/src/H5PropList.h +++ b/c++/src/H5PropList.h @@ -103,8 +103,15 @@ class H5_DLLCPP PropList : public IdComponent { // Copy constructor: creates a copy of a PropList object. PropList(const PropList& original); + // Gets the property list id. + virtual hid_t getId() const; + virtual void setId(const hid_t new_id); + // Destructor: properly terminates access to this property list. virtual ~PropList(); + + protected: + hid_t id; // HDF5 property list id }; #ifndef H5_NO_NAMESPACE -- cgit v0.12