diff options
author | Quincey Koziol <koziol@hdfgroup.org> | 2008-10-21 19:10:01 (GMT) |
---|---|---|
committer | Quincey Koziol <koziol@hdfgroup.org> | 2008-10-21 19:10:01 (GMT) |
commit | 27ae4bccfd866a9bf334dc191631499d3cea1b19 (patch) | |
tree | 828abaa9099bc36b176c8440f0d905ae267d5b02 /c++/src | |
parent | 22378dbd24c08d7153f4f295b5bca057191edc38 (diff) | |
download | hdf5-27ae4bccfd866a9bf334dc191631499d3cea1b19.zip hdf5-27ae4bccfd866a9bf334dc191631499d3cea1b19.tar.gz hdf5-27ae4bccfd866a9bf334dc191631499d3cea1b19.tar.bz2 |
[svn-r15922] Description:
Bring revisions 15289:15457 from trunk into metadata journaling
branch.
Tested on:
FreeBSD/32 6.2 (duty) in debug mode
FreeBSD/64 6.2 (liberty) w/C++ & FORTRAN, in debug mode
Linux/32 2.6 (kagiso) w/PGI compilers, w/C++ & FORTRAN, w/threadsafe,
in debug mode
Linux/64-amd64 2.6 (smirom) w/default API=1.6.x, w/C++ & FORTRAN,
in production mode
Linux/64-ia64 2.6 (cobalt) w/Intel compilers, w/C++ & FORTRAN,
in production mode
Solaris/32 2.10 (linew) w/deprecated symbols disabled, w/C++ & FORTRAN,
w/szip filter, in production mode
Mac OS X/32 10.5.2 (amazon) in debug mode
Linux/64-ia64 2.4 (tg-login3) w/parallel, w/FORTRAN, in production mode
Diffstat (limited to 'c++/src')
-rw-r--r-- | c++/src/H5AbstractDs.cpp | 12 | ||||
-rw-r--r-- | c++/src/H5AbstractDs.h | 5 | ||||
-rw-r--r-- | c++/src/H5Attribute.cpp | 103 | ||||
-rw-r--r-- | c++/src/H5Attribute.h | 22 | ||||
-rw-r--r-- | c++/src/H5Classes.h | 1 | ||||
-rw-r--r-- | c++/src/H5CommonFG.cpp | 33 | ||||
-rw-r--r-- | c++/src/H5DataSet.cpp | 120 | ||||
-rw-r--r-- | c++/src/H5DataSet.h | 22 | ||||
-rw-r--r-- | c++/src/H5DataSpace.cpp | 65 | ||||
-rw-r--r-- | c++/src/H5DataSpace.h | 10 | ||||
-rw-r--r-- | c++/src/H5DataType.cpp | 132 | ||||
-rw-r--r-- | c++/src/H5DataType.h | 21 | ||||
-rw-r--r-- | c++/src/H5DcreatProp.cpp | 2 | ||||
-rw-r--r-- | c++/src/H5DxferProp.h | 1 | ||||
-rw-r--r-- | c++/src/H5FaccProp.cpp | 2 | ||||
-rw-r--r-- | c++/src/H5File.cpp | 232 | ||||
-rw-r--r-- | c++/src/H5File.h | 33 | ||||
-rw-r--r-- | c++/src/H5Group.cpp | 109 | ||||
-rw-r--r-- | c++/src/H5Group.h | 20 | ||||
-rw-r--r-- | c++/src/H5IdComponent.cpp | 247 | ||||
-rw-r--r-- | c++/src/H5IdComponent.h | 35 | ||||
-rw-r--r-- | c++/src/H5Object.cpp | 194 | ||||
-rw-r--r-- | c++/src/H5Object.h | 29 | ||||
-rw-r--r-- | c++/src/H5PropList.cpp | 56 | ||||
-rw-r--r-- | c++/src/H5PropList.h | 9 |
25 files changed, 990 insertions, 525 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 043dc4d..64993a2 100644 --- a/c++/src/H5Attribute.cpp +++ b/c++/src/H5Attribute.cpp @@ -26,12 +26,14 @@ #include "H5PropList.h" #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" +#include "H5Attribute.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 @@ -208,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 @@ -297,6 +323,67 @@ 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::p_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::p_setId(const hid_t new_id) +{ + // handling references to this old id + try { + close(); + } + catch (Exception close_error) { + throw AttributeIException("Attribute::p_setId", close_error.getDetailMsg()); + } + // reset object's id to the given id + id = new_id; +} + +//-------------------------------------------------------------------------- // Function: Attribute::close ///\brief Closes this attribute. /// @@ -312,8 +399,10 @@ void Attribute::close() { throw AttributeIException("Attribute::close", "H5Aclose failed"); } - // reset the id because the attribute that it represents is now closed - id = 0; + // reset the id when the attribute that it represents is no longer + // referenced + if (getCounter() == 0) + id = 0; } } diff --git a/c++/src/H5Attribute.h b/c++/src/H5Attribute.h index 513b6ab..7d7ca8d 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,19 @@ class H5_DLLCPP Attribute : public AbstractDs { // Default constructor Attribute(); + // Gets the attribute id. + virtual hid_t getId() const; + // Destructor: properly terminates access to this attribute. virtual ~Attribute(); + protected: + // Sets the attribute id. + virtual void p_setId(const hid_t new_id); + 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 <string> +// remove when done +#include <iostream> + 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..3051e08 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,11 @@ 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(original) +{ + id = original.getId(); + incRefCount(); // increment number of references to this id +} //-------------------------------------------------------------------------- // Function: DataSet overload constructor - dereference @@ -77,9 +87,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() +{ + id = obj.p_dereference(ref); +} + +DataSet::DataSet(H5File& h5file, void* ref) : AbstractDs(), H5Object() { - IdComponent::dereference(obj, ref); + id = h5file.p_dereference(ref); } //-------------------------------------------------------------------------- @@ -455,52 +470,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 +519,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::p_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::p_setId(const hid_t new_id) +{ + // handling references to this old id + try { + close(); + } + catch (Exception close_error) { + throw DataSetIException(inMemFunc("p_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. /// @@ -565,8 +577,10 @@ void DataSet::close() { throw DataSetIException("DataSet::close", "H5Dclose failed"); } - // reset the id because the dataset that it represents is now closed - id = 0; + // reset the id when the dataset that it represents is no longer + // referenced + if (getCounter() == 0) + id = 0; } } diff --git a/c++/src/H5DataSet.h b/c++/src/H5DataSet.h index 7323f52..f5d13d0 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,15 +97,24 @@ 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; + // 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 // sub-types virtual hid_t p_get_type() const; + + protected: + // Sets the dataset id. + virtual void p_setId(const hid_t new_id); }; #ifndef H5_NO_NAMESPACE } diff --git a/c++/src/H5DataSpace.cpp b/c++/src/H5DataSpace.cpp index 245e27d..3d74b51 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,47 @@ 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::p_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::p_setId(const hid_t new_id) +{ + // handling references to this old id + try { + close(); + } + catch (Exception close_error) { + throw DataSpaceIException(inMemFunc("p_setId"), close_error.getDetailMsg()); + } + // reset object's id to the given id + id = new_id; +} + +//-------------------------------------------------------------------------- // Function: DataSpace::close ///\brief Closes this dataspace. /// @@ -564,8 +612,10 @@ void DataSpace::close() { throw DataSpaceIException("DataSpace::close", "H5Sclose failed"); } - // reset the id because the dataspace that it represents is now closed - id = 0; + // reset the id when the dataspace that it represents is no longer + // referenced + if (getCounter() == 0) + id = 0; } } @@ -583,8 +633,7 @@ DataSpace::~DataSpace() { try { close(); - } - catch (Exception close_error) { + } 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..80842f7 100644 --- a/c++/src/H5DataSpace.h +++ b/c++/src/H5DataSpace.h @@ -112,8 +112,18 @@ 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; + // Destructor: properly terminates access to this dataspace. virtual ~DataSpace(); + + private: + hid_t id; // HDF5 dataspace id + + protected: + // Sets the dataspace id. + virtual void p_setId(const hid_t new_id); }; #ifndef H5_NO_NAMESPACE } diff --git a/c++/src/H5DataType.cpp b/c++/src/H5DataType.cpp index c31395e..e023bda 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,47 @@ 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::p_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::p_setId(const hid_t new_id) +{ + // handling references to this old id + try { + close(); + } + catch (Exception close_error) { + throw DataTypeIException(inMemFunc("p_setId"), close_error.getDetailMsg()); + } + // reset object's id to the given id + id = new_id; +} + +//-------------------------------------------------------------------------- // Function: DataType::close ///\brief Closes the datatype if it is not a predefined type. /// @@ -723,8 +735,10 @@ void DataType::close() { throw DataTypeIException(inMemFunc("close"), "H5Tclose failed"); } - // reset the id because the datatype that it represents is now closed - id = 0; + // reset the id when the datatype that it represents is no longer + // referenced + if (getCounter() == 0) + id = 0; } } @@ -740,14 +754,12 @@ void DataType::close() //-------------------------------------------------------------------------- DataType::~DataType() { - try { - close(); - } - catch (Exception close_error) { - cerr << inMemFunc("~DataType - ") << close_error.getDetailMsg() << endl; - } + 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..4e7ca03 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,18 @@ class H5_DLLCPP DataType : public H5Object { // Default constructor DataType(); + // Gets the datatype id. + virtual hid_t getId() const; + // Destructor: properly terminates access to this datatype. virtual ~DataType(); + + protected: + hid_t id; // HDF5 datatype id + + // Sets the datatype id. + virtual void p_setId(const hid_t new_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/H5FaccProp.cpp b/c++/src/H5FaccProp.cpp index 6a70b3b..f2ab0f3 100644 --- a/c++/src/H5FaccProp.cpp +++ b/c++/src/H5FaccProp.cpp @@ -225,7 +225,7 @@ void FileAccPropList::getFamily(hsize_t& memb_size, FileAccPropList& memb_plist) { throw PropListIException("FileAccPropList::getFamily", "H5Pget_fapl_family failed"); } - memb_plist.setId(memb_plist_id); + memb_plist.p_setId(memb_plist_id); } //-------------------------------------------------------------------------- diff --git a/c++/src/H5File.cpp b/c++/src/H5File.cpp index aa11e31..0e71543 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,134 @@ 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()); +} + +#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 +716,47 @@ 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::p_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::p_setId(const hid_t new_id) +{ + // handling references to this old id + try { + close(); + } + catch (Exception E) { + throw FileIException("H5File::p_setId", E.getDetailMsg()); + } + // reset object's id to the given id + id = new_id; +} + +//-------------------------------------------------------------------------- // Function: H5File::close ///\brief Closes this HDF5 file. /// @@ -645,8 +772,10 @@ void H5File::close() { throw FileIException("H5File::close", "H5Fclose failed"); } - // reset the id because the file that it represents is now closed - id = 0; + // reset the id when the file that it represents is no longer + // referenced + if (getCounter() == 0) + id = 0; } } @@ -685,8 +814,7 @@ H5File::~H5File() { try { close(); - } - catch (Exception close_error) { + } 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..b49118e 100644 --- a/c++/src/H5File.h +++ b/c++/src/H5File.h @@ -89,11 +89,10 @@ 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 - - // 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 + void reference(void* ref, const char* name, const DataSpace& dataspace, + H5R_type_t ref_type = H5R_DATASET_REGION) const; + void reference(void* ref, const char* name) const; + void reference(void* ref, const H5std_string& name) const; // Returns this class name virtual H5std_string fromClass () const { return("H5File"); } @@ -110,14 +109,38 @@ class H5_DLLCPP H5File : public IdComponent, public CommonFG { // Copy constructor: makes a copy of the original H5File object. H5File(const H5File& original); + // Gets the HDF5 file id. + virtual hid_t getId() const; + // 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; + + protected: + // Sets the HDF5 file id. + virtual void p_setId(const hid_t new_id); + +#endif // DOXYGEN_SHOULD_SKIP_THIS + }; #ifndef H5_NO_NAMESPACE } diff --git a/c++/src/H5Group.cpp b/c++/src/H5Group.cpp index f3c058a..f50691e 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()); - } -} - -//-------------------------------------------------------------------------- -// 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 -{ - try { - return(p_reference(name, -1, H5R_OBJECT)); - } - 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 H5std_string& name) const +Group::Group(H5File& h5file, void* ref) : H5Object() { - return(Reference(name.c_str())); + id = h5file.p_dereference(ref); } #ifndef H5_NO_DEPRECATED_SYMBOLS @@ -188,6 +154,47 @@ 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::p_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::p_setId(const hid_t new_id) +{ + // handling references to this old id + try { + close(); + } + catch (Exception close_error) { + throw GroupIException("Group::p_setId", close_error.getDetailMsg()); + } + // reset object's id to the given id + id = new_id; +} + +//-------------------------------------------------------------------------- // Function: Group::close ///\brief Closes this group. /// @@ -203,8 +210,10 @@ void Group::close() { throw GroupIException("Group::close", "H5Gclose failed"); } - // reset the id because the group that it represents is now closed - id = 0; + // reset the id when the group that it represents is no longer + // referenced + if (getCounter() == 0) + id = 0; } } diff --git a/c++/src/H5Group.h b/c++/src/H5Group.h index 5df85bb..d296b84 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,21 @@ 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; + // 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 + + protected: + // Sets the group id. + virtual void p_setId(const hid_t new_id); }; #ifndef H5_NO_NAMESPACE } diff --git a/c++/src/H5IdComponent.cpp b/c++/src/H5IdComponent.cpp index bb015fb..a068b20 100644 --- a/c++/src/H5IdComponent.cpp +++ b/c++/src/H5IdComponent.cpp @@ -18,7 +18,6 @@ #endif /*H5_VMS*/ #include <string> - #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(); + p_setId(rhs.getId()); + incRefCount(getId()); // a = b, so there are two objects with the same + // hdf5 id } return *this; } @@ -196,40 +190,31 @@ IdComponent& IdComponent::operator=( const IdComponent& rhs ) ///\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. +// 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 +// Modification +// 2008/7/23 - BMR +// Changed all subclasses' setId to p_setId and put back setId +// here. p_setId is used in the library where the id provided +// by a C API passed on to user's application in the form of a +// C++ API object, which will be destroyed properly, and so +// p_setId does not call incRefCount. On the other hand, the +// public version setId is used by other applications, in which +// the id passed to setId already has a reference count, so setId +// must call incRefCount. //-------------------------------------------------------------------------- void IdComponent::setId(const hid_t new_id) { - // handling references to this old id - try { - close(); - } - catch (Exception close_error) { - throw IdComponentException(inMemFunc("copy"), close_error.getDetailMsg()); - } - - // reset object's id to the given id - id = new_id; + // set to new_id + p_setId(new_id); // increment the reference counter of the new id incRefCount(); } //-------------------------------------------------------------------------- -// Function: IdComponent::getId -///\brief Returns the id of this object -///\return HDF5 id -// Programmer Binh-Minh Ribler - 2000 -//-------------------------------------------------------------------------- -hid_t IdComponent::getId () const -{ - return(id); -} - -//-------------------------------------------------------------------------- // Function: IdComponent destructor ///\brief Noop destructor. // Programmer Binh-Minh Ribler - 2000 @@ -273,7 +258,7 @@ H5std_string IdComponent::inMemFunc(const char* func_name) const ///\brief Default constructor. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -IdComponent::IdComponent() : id(-1) {} +IdComponent::IdComponent() {} //-------------------------------------------------------------------------- // Function: IdComponent::p_get_file_name (protected) @@ -287,8 +272,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 +285,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,175 +300,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 -{ - herr_t ret_value = H5Rcreate(ref, id, name, ref_type, space_id); - if (ret_value < 0) - { - throw IdComponentException("", "H5Rcreate 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()); -} - -//-------------------------------------------------------------------------- -// Function: IdComponent::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 -// Return A reference -// Exception H5::IdComponentException -// Notes This function is incorrect, and will be removed in the near -// future after notifying users of the new APIs ::reference's. -// 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 -{ - hobj_ref_t ref; - herr_t ret_value = H5Rcreate(&ref, id, name, ref_type, space_id); - if (ret_value < 0) - { - throw IdComponentException("", "H5Rcreate failed"); - } - return (reinterpret_cast<void*>(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 IdComponent::p_dereference(void* ref) { - hid_t space_id = H5Rget_region(id, ref_type, ref); - if (space_id < 0) + hid_t temp_id = H5Rdereference(getId(), H5R_OBJECT, ref); + if (temp_id < 0) { - throw IdComponentException("", "H5Rget_region failed"); + throw ReferenceException("", "H5Rdereference failed"); } - return(space_id); + return(temp_id); } // @@ -495,7 +328,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..7f573a1 100644 --- a/c++/src/H5IdComponent.h +++ b/c++/src/H5IdComponent.h @@ -44,13 +44,11 @@ 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); @@ -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,12 @@ 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); + + // Sets the identifier of this object to a new value. - this one + // doesn't increment reference count + virtual void p_setId(const hid_t new_id) = 0; #endif // DOXYGEN_SHOULD_SKIP_THIS diff --git a/c++/src/H5Object.cpp b/c++/src/H5Object.cpp index c027f51..9dafea8 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,175 @@ 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 + p_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 + p_setId(temp_id); +} + +#ifndef H5_NO_DEPRECATED_SYMBOLS +//-------------------------------------------------------------------------- +// Function: H5Object::p_get_obj_type (protected) +// Purpose Retrieves the type of object that an object reference points to. +// Parameters +// ref - IN: Reference to query +// ref_type - IN: Type of reference to query +// Return An object type, which can be one of the following: +// H5G_LINK Object is a symbolic link. +// H5G_GROUP Object is a group. +// H5G_DATASET Object is a dataset. +// H5G_TYPE Object is a named datatype +// Exception H5::IdComponentException +// Programmer Binh-Minh Ribler - May, 2004 +//-------------------------------------------------------------------------- +H5G_obj_t H5Object::p_get_obj_type(void *ref, H5R_type_t ref_type) const +{ + H5G_obj_t obj_type = H5Rget_obj_type1(getId(), ref_type, ref); + + if (obj_type == H5G_UNKNOWN) + { + throw IdComponentException("", "H5Rget_obj_type failed"); + } + return(obj_type); +} +#endif /* H5_NO_DEPRECATED_SYMBOLS */ + + +//-------------------------------------------------------------------------- +// Function: H5Object::p_get_region (protected) +// Purpose Retrieves a dataspace with the region pointed to selected. +// Parameters +// ref_type - IN: Type of reference to get region of - default +// to H5R_DATASET_REGION +// ref - IN: Reference to get region of +// Return Dataspace id +// Exception H5::IdComponentException +// Programmer Binh-Minh Ribler - May, 2004 +//-------------------------------------------------------------------------- +hid_t H5Object::p_get_region(void *ref, H5R_type_t ref_type) const +{ + hid_t space_id = H5Rget_region(getId(), ref_type, ref); + if (space_id < 0) + { + throw IdComponentException("", "H5Rget_region failed"); + } + return(space_id); +} + + +//-------------------------------------------------------------------------- // 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 f39ae78..579c8ac 100644 --- a/c++/src/H5PropList.cpp +++ b/c++/src/H5PropList.cpp @@ -46,7 +46,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 @@ -54,7 +54,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 @@ -70,7 +74,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 @@ -205,6 +209,46 @@ 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::p_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::p_setId(const hid_t new_id) +{ + // handling references to this old id + try { + close(); + } + catch (Exception close_error) { + throw PropListIException(inMemFunc("p_setId"), close_error.getDetailMsg()); + } + // reset object's id to the given id + id = new_id; +} + +//-------------------------------------------------------------------------- // Function: PropList::close ///\brief Closes the property list if it is not a default one. /// @@ -220,8 +264,10 @@ void PropList::close() { throw PropListIException(inMemFunc("close"), "H5Pclose failed"); } - // reset the id because the property list that it represents is now closed - id = 0; + // reset the id when the property list that it represents is no longer + // referenced + if (getCounter() == 0) + id = 0; } } diff --git a/c++/src/H5PropList.h b/c++/src/H5PropList.h index 09ea6f1..5dfa538 100644 --- a/c++/src/H5PropList.h +++ b/c++/src/H5PropList.h @@ -103,8 +103,17 @@ 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; + // Destructor: properly terminates access to this property list. virtual ~PropList(); + + protected: + hid_t id; // HDF5 property list id + + // Sets the property list id. + virtual void p_setId(const hid_t new_id); }; #ifndef H5_NO_NAMESPACE |