Hdf5Resource.h

Go to the documentation of this file.
00001 /*
00002  * The information in this file is
00003  * Copyright(c) 2007 Ball Aerospace & Technologies Corporation
00004  * and is subject to the terms and conditions of the
00005  * GNU Lesser General Public License Version 2.1
00006  * The license text is available from   
00007  * http://www.gnu.org/licenses/lgpl.html
00008  */
00009 
00010 #ifndef HDF5RESOURCE_H
00011 #define HDF5RESOURCE_H
00012 
00013 #include "AppVerify.h"
00014 #include "Resource.h"
00015 #include <hdf5.h>
00016 
00017 #include <string>
00018 
00019 /**
00020  * The %Hdf5FileObject is a trait object for use with the %Resource template. 
00021  *
00022  * The %Hdf5FileObject is a trait object for use with the %Resource template. It provides capability for opening
00023  * and closing HDF5 files.
00024  * 
00025  * @see FileObject
00026  */
00027 class Hdf5FileObject
00028 {
00029 public:
00030    /**
00031     * This is an implementation detail of the %Hdf5FileObject class. 
00032     *
00033     * It is used for passing the parameters required by H5Fopen.
00034     */
00035    struct Args
00036    {
00037       /**
00038        * The file name of the HDF5 file to be opened.
00039        */
00040       std::string mFilename;
00041 
00042       /**
00043        * The access flags for opening the file.
00044        *
00045        * Default is to open read only. Other options include read only,
00046        * read-write, write, truncate. Options may be combined with the
00047        * | operator. For details, see the HDF5 documentation.
00048        */
00049       unsigned int mFlags;
00050 
00051       /**
00052        * The internal HDF5 access flags.
00053        *
00054        * These flags are different than the file-level access flags. They
00055        * specify what filters are needed to access the data. This should
00056        * almost always stay at the default, H5P_DEFAULT. For details regarding
00057        * this parameter, see the H5Fopen function in the HDF5 documentation.
00058        */
00059       hid_t mAccess;
00060 
00061       /**
00062        *  Creates a resource to an HDF5 file handle.
00063        *
00064        *  Auto-closes file when the object goes out of scope.
00065        *  
00066        *  @param  filename
00067        *          The name of the file to open.
00068        *  @param  flags
00069        *          The open flags for the file access (H5F_ACC_RDONLY, H5F_ACC_RDWR,
00070        *          H5F_ACC_TRUNC, H5F_ACC_EXCL, H5F_ACC_DEBUG, H5F_ACC_CREAT). These
00071        *          flags may be ORed together with the operator |. For details
00072        *          on these flags, see the HDF5 documentation.
00073        *  @param  access
00074        *          The internal HDF5 access flags.  These flags are different than
00075        *          the file-level access flags.  They specify what filters are
00076        *          needed to access the data.  This should almost always stay at
00077        *          the default, H5P_DEFAULT.
00078        */
00079       Args(std::string filename, unsigned flags = H5F_ACC_RDONLY, hid_t access = H5P_DEFAULT) :
00080          mFilename(filename), mFlags(flags), mAccess(access)
00081          {
00082          }
00083 
00084       /**
00085        * Default constructor
00086        */
00087       Args() : mFilename(), mFlags(0), mAccess(-1)
00088       {
00089       }
00090    };
00091 
00092    /**
00093     * Obtains an HDF5 file handle.
00094     *
00095     * See Resource.h for details.
00096     *
00097     * @param  args
00098     *         The arguments for obtaining the resource. Should be of type Hdf5FileObject::Args.
00099     * @return Returns a pointer to a handle of the dataset.
00100     */
00101    hid_t* obtainResource(const Args &args) const
00102    {
00103       int iValid = H5Fis_hdf5(args.mFilename.c_str());
00104       hid_t* pId = new hid_t;
00105       VERIFYRV(pId != NULL, NULL);
00106       *pId = -1; // invalidate the handle
00107       if (iValid > 0 && pId != NULL)
00108       {
00109          *pId = H5Fopen(args.mFilename.c_str(), args.mFlags, args.mAccess);
00110       }
00111       return pId;
00112    }
00113 
00114    /**
00115     * Releases an HDF5 file handle.
00116     *
00117     * See Resource.h for details.
00118     *
00119     * @param  args
00120     *         The arguments for releasing the resource. Should be of type Hdf5FileObject::Args.
00121     * @param  pHandle
00122     *         A pointer to the handle to be freed.
00123     */
00124    void releaseResource(const Args &args, hid_t* pHandle) const
00125    {
00126       if (pHandle != NULL && *pHandle != -1)
00127       {
00128          H5Fclose(*pHandle);
00129          delete pHandle;
00130       }
00131    }
00132 };
00133 
00134 /**
00135  *  This is a %Resource class that opens and closes HDF5 files. 
00136  *
00137  *  This is a %Resource class that opens and closes HDF5 files. It has a conversion
00138  *  operator to allow a %Hdf5FileResource object to be used where ever a hid_t
00139  *  that represents an open HDF5 File Handle may be used.
00140  *
00141  *  Example of Usage:
00142  *  @code
00143  *  Hdf5FileResource pFile("c:/data/a.h5"); // open for read-only access
00144  *  Hdf5FileResource pFile2("c:/data/b.h5", H5F_ACC_CREAT | H5F_ACC_RDWR); // create and open for read/write
00145  *  Hdf5FileResource pFile3("c:/data/c.h5", H5F_ACC_RDWR); // open for read/write access
00146  *  @endcode
00147  */
00148 class Hdf5FileResource : public Resource<hid_t, Hdf5FileObject>
00149 {
00150 public:
00151    /**
00152     *  Constructs a Resource object that wraps a hid_t HDF5 File Handle.
00153     *
00154     *  Opens the specified file using the specified access
00155     *  modes. The two arguments will ultimately be passed
00156     *  unmodified to H5Fopen.
00157     *
00158     *  @param   filename
00159     *           The name of the file to open.
00160     *  @param   flags
00161     *           The access mode to open the file with. These match
00162     *           the modes used with H5Fopen.
00163     *  @param   access
00164     *           How to open the file. Most users will never need to provide this option.
00165     *           See the HDF5 documentation for details.
00166     */
00167    Hdf5FileResource(const std::string& filename, unsigned flags = H5F_ACC_RDONLY, hid_t access = H5P_DEFAULT) :
00168       Resource<hid_t, Hdf5FileObject>(Args(filename, flags, access))
00169    {
00170    }
00171 
00172    /**
00173     * Constructs a Resource object that wraps a hid_t HDF5 File Handle.
00174     *
00175     * This will take ownership of an existing hid_t file handle and 
00176     * will ensure that it is closed.
00177     *
00178     * @param    file
00179     *           The HDF5 file handle.
00180     */
00181    Hdf5FileResource(hid_t file):
00182       Resource<hid_t, Hdf5FileObject>(new hid_t(file), Args())
00183    {
00184    }
00185 
00186    /**
00187     * Default constructor.
00188     */
00189    Hdf5FileResource() :
00190       Resource<hid_t, Hdf5FileObject>(NULL, Args())
00191    {
00192    }
00193 
00194    /**
00195     *  Returns a pointer to the underlying hid_t returned by H5Fopen.
00196     *
00197     *  Returns a pointer to the underlying hid_t. This operator,
00198     *  used in conjunction with the dereferencing operator,
00199     *  allows the %Hdf5FileResource object to be used where ever
00200     *  a hid_t would normally be used.
00201     *
00202     *  @return   A pointer to the underlying hid_t returned by H5Fopen.
00203     */
00204    operator hid_t*()
00205    {
00206       return get();
00207    }
00208 };
00209 
00210 /**
00211  *  The %Hdf5DatasetObject is a trait object for use with the %Resource template.
00212  *
00213  *  The %Hdf5DatasetObject is a trait object for use with the %Resource template. It provides capability for opening
00214  *  and closing HDF5 datasets.
00215  *
00216  *  @see Hdf5FileObject
00217  */
00218 class Hdf5DataSetObject
00219 {
00220 public:
00221    /**
00222     * This is an implementation detail of the %Hdf5DataSetObject class. 
00223     *
00224     * It is used for passing the parameters required by H5Dopen1.
00225     */
00226    struct Args
00227    {
00228       /**
00229        * The file handle that represents the file that contains
00230        * the dataset.
00231        */
00232       hid_t mFileHandle;
00233 
00234       /**
00235        * The full path and name of the dataset within the file.
00236        *
00237        * HDF5 provides access to datasets either by requiring
00238        * opening of each group within the file (similar to
00239        * performing "cd docs; cd stuff; cd mydir;") and performing
00240        * a dataset open at the end, OR by providing the full path
00241        * and name of the dataset (ie. "/docs/stuff/mydir/dataset").
00242        */
00243       std::string mFullPathAndName;
00244 
00245       /**
00246        * Constructs an Args object for an HDF5 Dataset Object.
00247        *
00248        * See Resource.h for details.
00249        *
00250        * @param  fileHandle
00251        *         A file handle repsesenting the file that contains the specified
00252        *         dataset.
00253        * @param  fullPathAndName
00254        *         The UNIX-style full path and name of the data element to open.
00255        */
00256       Args(hid_t fileHandle, const std::string& fullPathAndName) :
00257          mFileHandle(fileHandle), mFullPathAndName(fullPathAndName)
00258       {
00259       }
00260 
00261       /**
00262        * Default constructor
00263        */
00264       Args() :
00265          mFileHandle(-1),
00266          mFullPathAndName()
00267       {
00268       }
00269    };
00270 
00271    /**
00272     * Obtains an HDF5 dataset handle.
00273     *
00274     * See Resource.h for details.
00275     *
00276     * @param  args
00277     *         The arguments for obtaining the resource. Should be of type Hdf5DatasetObject::Args.
00278     * @return Returns a pointer to a handle of the dataset.
00279     */
00280    hid_t* obtainResource(const Args &args) const
00281    {
00282       hid_t* pHandle = new (std::nothrow) hid_t;
00283       if (pHandle != NULL)
00284       {
00285          *pHandle = H5Dopen1(args.mFileHandle, args.mFullPathAndName.c_str());
00286       }
00287       return pHandle;
00288    }
00289 
00290    /**
00291     * Releases an HDF5 dataset handle.
00292     *
00293     * See Resource.h for details.
00294     *
00295     * @param  args
00296     *         The arguments for releasing the resource. Should be of type Hdf5DatasetObject::Args.
00297     * @param  pHandle
00298     *         A pointer to the handle to be freed.
00299     */
00300    void releaseResource(const Args &args, hid_t* pHandle) const
00301    {
00302       if (pHandle != NULL && *pHandle != -1)
00303       {
00304          H5Dclose(*pHandle);
00305          delete pHandle;
00306       }
00307    }
00308 };
00309 
00310 /**
00311  *  This is a %Resource class that opens and closes HDF5 datasets. 
00312  *
00313  *  This is a %Resource class that opens and closes HDF5 datasets. It has a conversion
00314  *  operator to allow a %Hdf5DatasetResource object to be used where ever a hid_t
00315  *  that represents an open HDF5 dataset Handle may be used.
00316  *
00317  *  Example of Usage:
00318  *  @code
00319  *  Hdf5FileResource pFile("c:/data/a.h5"); // open for read-only access
00320  *  if (pFile.get() != NULL && *pFile > 0) // file exists
00321  *  {
00322  *     Hdf5DatasetResource pDataset(*pFile, "/home/dataset/1");
00323  *     if (pDataset != NULL && *pDataset > 0)
00324  *     {
00325  *        // do H5Dread here
00326  *     }
00327  *  }
00328  *  @endcode
00329  */
00330 class Hdf5DataSetResource : public Resource<hid_t, Hdf5DataSetObject>
00331 {
00332 public:
00333    /**
00334     *  Constructs a Resource object that wraps a hid_t HDF5 Dataset Handle.
00335     *
00336     *  Opens the specified dataset based on a file handle and the
00337     *  full path and name to the dataset within the file (ie. "/home/data/cube1").
00338     *  Calls H5Dopen1().
00339     *
00340     *  @param   fileHandle
00341     *           The HDF5 file handle. @see Hdf5FileResource::get().
00342     *  @param   datasetName
00343     *           The full path and name to the HDF5 dataset to open.
00344     *
00345     */
00346    Hdf5DataSetResource(hid_t fileHandle, const std::string& datasetName) :
00347       Resource<hid_t, Hdf5DataSetObject>(Args(fileHandle, datasetName))
00348    {
00349    }
00350 
00351    /**
00352     * Construct a Resource object that wraps a hid_t HDF5 Dataset Handle
00353     *
00354     * This will take ownership of an existing hid_t dataset handle and 
00355     * will ensure that it is closed.
00356     *
00357     * @param    dataset
00358     *           The HDF5 dataset handle.
00359     */
00360    Hdf5DataSetResource(hid_t dataset) :
00361       Resource<hid_t, Hdf5DataSetObject>(new hid_t(dataset), Args())
00362    {
00363    }
00364 
00365    /**
00366     * Default constructor.
00367     */
00368    Hdf5DataSetResource() :
00369       Resource<hid_t, Hdf5DataSetObject>(NULL, Args())
00370    {
00371    }
00372 
00373    /**
00374     *  Returns a pointer to the underlying hid_t returned by H5Dopen1.
00375     *
00376     *  Returns a pointer to the underlying hid_t. This operator,
00377     *  used in conjunction with the dereferencing operator,
00378     *  allows the %Hdf5DatasetResource object to be used where ever
00379     *  a hid_t would normally be used.
00380     *
00381     *  @return   A pointer to the underlying hid_t returned by H5Dopen1.
00382     */
00383    operator hid_t*()
00384    {
00385       return get();
00386    }
00387 };
00388 
00389 /**
00390  *  The Hdf5AttributeObject is a trait object for use with the %Resource template.
00391  *
00392  *  The Hdf5AttributeObject is a trait object for use with the %Resource template.
00393  *  It provides capability for closing HDF5 Attributes.
00394  *
00395  */
00396 class Hdf5AttributeObject
00397 {
00398 public:
00399    /**
00400     * This is an implementation detail of the Hdf5AttributeObject class. 
00401     *
00402     */
00403    struct Args
00404    {
00405       /**
00406        * Default constructor
00407        */
00408       Args()
00409       {
00410       }
00411    };
00412 
00413    /**
00414     * Obtains an HDF5 attribute.
00415     *
00416     * See Resource.h for details.
00417     *
00418     * @param  args
00419     *         The arguments for obtaining the resource. Should be of type Hdf5AttributeObject::Args.
00420     *
00421     * @return Returns NULL
00422     */
00423    hid_t* obtainResource(const Args &args) const
00424    {
00425       return NULL;
00426    }
00427 
00428    /**
00429     * Releases an HDF5 attribute.
00430     *
00431     * See Resource.h for details.
00432     *
00433     * @param  args
00434     *         The arguments for releasing the resource. Should be of type Hdf5AttributeObject::Args.
00435     * @param  pHandle
00436     *         A pointer to the handle to be freed.
00437     */
00438    void releaseResource(const Args &args, hid_t* pHandle) const
00439    {
00440       if ((pHandle != NULL) && (*pHandle != -1))
00441       {
00442          H5Aclose(*pHandle);
00443          delete pHandle;
00444       }
00445    }
00446 };
00447 
00448 /**
00449  *  This is a Resource class that closes HDF5 attributes. 
00450  *
00451  *  This is a Resource class that closes HDF5 attributes. It has a conversion
00452  *  operator to allow a Hdf5AttributeResource object to be used where ever a hid_t
00453  *  that represents an open HDF5 attribute Handle may be used.
00454  *
00455  */
00456 class Hdf5AttributeResource : public Resource<hid_t, Hdf5AttributeObject>
00457 {
00458 public:
00459    /**
00460     * Construct a Resource object that wraps a hid_t HDF5 Attribute Handle
00461     *
00462     * This will take ownership of an existing hid_t attribute handle and 
00463     * will ensure that it is closed.
00464     *
00465     * @param    attribute
00466     *           The HDF5 attribute handle.
00467     */
00468    Hdf5AttributeResource(hid_t attribute) :
00469       Resource<hid_t, Hdf5AttributeObject>(new hid_t(attribute), Args())
00470    {
00471    }
00472 
00473    /**
00474     * Default constructor.
00475     */
00476    Hdf5AttributeResource() :
00477       Resource<hid_t, Hdf5AttributeObject>(NULL, Args())
00478    {
00479    }
00480 
00481    /**
00482     *  Returns a pointer to the underlying hid_t held by this Resource.
00483     *
00484     *  Returns a pointer to the underlying hid_t. This operator,
00485     *  used in conjunction with the dereferencing operator,
00486     *  allows the Hdf5AttributeResource object to be used where ever
00487     *  a hid_t would normally be used.
00488     *
00489     *  @return   A pointer to the underlying hid_t held by this Resource.
00490     */
00491    operator hid_t*()
00492    {
00493       return get();
00494    }
00495 };
00496 
00497 /**
00498  *  The Hdf5TypeObject is a trait object for use with the Resource template.
00499  *
00500  *  The Hdf5TypeObject is a trait object for use with the Resource template.
00501  *  It provides capability for closing HDF5 types.
00502  *
00503  */
00504 class Hdf5TypeObject
00505 {
00506 public:
00507    /**
00508     * This is an implementation detail of the Hdf5TypeObject class. 
00509     *
00510     */
00511    struct Args
00512    {
00513       /**
00514        * Default constructor
00515        */
00516       Args()
00517       {
00518       }
00519    };
00520 
00521    /**
00522     * Obtains an HDF5 type.
00523     *
00524     * See Resource.h for details.
00525     *
00526     * @param  args
00527     *         The arguments for obtaining the resource. Should be of type Hdf5TypeObject::Args.
00528     *
00529     * @return Returns NULL
00530     */
00531    hid_t* obtainResource(const Args &args) const
00532    {
00533       return NULL;
00534    }
00535 
00536    /**
00537     * Closes an open HDF5 type.  If the
00538     * given type is a compound dataset,
00539     * the close will be recursive on all
00540     * of the member types.
00541     *
00542     * @param handle
00543     *        The open HDF5 type to close.
00544     */
00545    void closeResourceRecursive(hid_t handle) const
00546    {
00547       H5T_class_t type = H5Tget_class(handle);
00548       if (type == H5T_COMPOUND)
00549       {
00550          int memberCount = H5Tget_nmembers(handle);
00551          for (int member = 0; member < memberCount; ++member)
00552          {
00553             hid_t memberHandle = H5Tget_member_type(handle, member);
00554             closeResourceRecursive(memberHandle);
00555          }
00556       }
00557       H5Tclose(handle);
00558    }
00559 
00560    /**
00561     * Releases an HDF5 type.
00562     *
00563     * See Resource.h for details.
00564     *
00565     * @param  args
00566     *         The arguments for releasing the resource. Should be of type Hdf5TypeObject::Args.
00567     * @param  pHandle
00568     *         A pointer to the handle to be freed.
00569     */
00570    void releaseResource(const Args &args, hid_t* pHandle) const
00571    {
00572       if ((pHandle != NULL) && (*pHandle != -1))
00573       {
00574          closeResourceRecursive(*pHandle);
00575          delete pHandle;
00576       }
00577    }
00578 };
00579 
00580 /**
00581  *  This is a Resource class that closes HDF5 types. 
00582  *
00583  *  This is a Resource class that closes HDF5 types. It has a conversion
00584  *  operator to allow a Hdf5TypeResource object to be used where ever a hid_t
00585  *  that represents an open HDF5 type handle may be used.
00586  *
00587  */
00588 class Hdf5TypeResource : public Resource<hid_t, Hdf5TypeObject>
00589 {
00590 public:
00591    /**
00592     * Construct a Resource object that wraps a hid_t HDF5 type handle
00593     *
00594     * This will take ownership of an existing hid_t type handle and 
00595     * will ensure that it is closed.
00596     *
00597     * @param    type
00598     *           The HDF5 type handle.
00599     */
00600    Hdf5TypeResource(hid_t type) :
00601       Resource<hid_t, Hdf5TypeObject>(new hid_t(type), Args())
00602    {
00603    }
00604 
00605    /**
00606     * Default constructor.
00607     */
00608    Hdf5TypeResource() :
00609       Resource<hid_t, Hdf5TypeObject>(NULL, Args())
00610    {
00611    }
00612 
00613    /**
00614     *  Returns a pointer to the underlying hid_t held by this Resource.
00615     *
00616     *  Returns a pointer to the underlying hid_t. This operator,
00617     *  used in conjunction with the dereferencing operator,
00618     *  allows the Hdf5TypeResource object to be used where ever
00619     *  a hid_t would normally be used.
00620     *
00621     *  @return   A pointer to the underlying hid_t held by this Resource.
00622     */
00623    operator hid_t*()
00624    {
00625       return get();
00626    }
00627 };
00628 
00629 /**
00630  *  The Hdf5DataSpaceObject is a trait object for use with the Resource template.
00631  *
00632  *  The Hdf5DataSpaceObject is a trait object for use with the Resource template.
00633  *  It provides capability for closing HDF5 dataspaces.
00634  *
00635  */
00636 class Hdf5DataSpaceObject
00637 {
00638 public:
00639    /**
00640     * This is an implementation detail of the Hdf5DataSpaceObject class. 
00641     *
00642     */
00643    struct Args
00644    {
00645       /**
00646        * Default constructor
00647        */
00648       Args()
00649       {
00650       }
00651    };
00652 
00653    /**
00654     * Obtains an HDF5 dataspace.
00655     *
00656     * See Resource.h for details.
00657     *
00658     * @param  args
00659     *         The arguments for obtaining the resource. Should be of type Hdf5DataSpaceObject::Args.
00660     *
00661     * @return Returns NULL
00662     */
00663    hid_t* obtainResource(const Args &args) const
00664    {
00665       return NULL;
00666    }
00667 
00668    /**
00669     * Releases an HDF5 dataspace.
00670     *
00671     * See Resource.h for details.
00672     *
00673     * @param  args
00674     *         The arguments for releasing the resource. Should be of type Hdf5DataSpaceObject::Args.
00675     * @param  pHandle
00676     *         A pointer to the handle to be freed.
00677     */
00678    void releaseResource(const Args &args, hid_t* pHandle) const
00679    {
00680       if ((pHandle != NULL) && (*pHandle != -1))
00681       {
00682          H5Sclose(*pHandle);
00683          delete pHandle;
00684       }
00685    }
00686 };
00687 
00688 /**
00689  *  This is a Resource class that closes HDF5 dataspaces. 
00690  *
00691  *  This is a Resource class that closes HDF5 dataspaces. It has a conversion
00692  *  operator to allow a Hdf5DataSpaceResource object to be used where ever a hid_t
00693  *  that represents an open HDF5 dataspace handle may be used.
00694  *
00695  */
00696 class Hdf5DataSpaceResource : public Resource<hid_t, Hdf5DataSpaceObject>
00697 {
00698 public:
00699    /**
00700     * Construct a Resource object that wraps a hid_t HDF5 dataspace handle
00701     *
00702     * This will take ownership of an existing hid_t dataspace handle and 
00703     * will ensure that it is closed.
00704     *
00705     * @param    dataspace
00706     *           The HDF5 dataspace handle.
00707     */
00708    Hdf5DataSpaceResource(hid_t dataspace) :
00709       Resource<hid_t, Hdf5DataSpaceObject>(new hid_t(dataspace), Args())
00710    {
00711    }
00712 
00713    /**
00714     * Default constructor.
00715     */
00716    Hdf5DataSpaceResource() :
00717       Resource<hid_t, Hdf5DataSpaceObject>(NULL, Args())
00718    {
00719    }
00720 
00721    /**
00722     *  Returns a pointer to the underlying hid_t held by this Resource.
00723     *
00724     *  Returns a pointer to the underlying hid_t. This operator,
00725     *  used in conjunction with the dereferencing operator,
00726     *  allows the Hdf5DataSpaceResource object to be used where ever
00727     *  a hid_t would normally be used.
00728     *
00729     *  @return   A pointer to the underlying hid_t held by this Resource.
00730     */
00731    operator hid_t*()
00732    {
00733       return get();
00734    }
00735 };
00736 
00737 
00738 /**
00739  *  The Hdf5GroupObject is a trait object for use with the Resource template.
00740  *
00741  *  The Hdf5GroupObject is a trait object for use with the Resource template.
00742  *  It provides capability for closing HDF5 groups.
00743  *
00744  */
00745 class Hdf5GroupObject
00746 {
00747 public:
00748    /**
00749     * This is an implementation detail of the Hdf5GroupObject class. 
00750     *
00751     */
00752    struct Args
00753    {
00754       /**
00755        * Default constructor
00756        */
00757       Args()
00758       {
00759       }
00760    };
00761 
00762    /**
00763     * Obtains an HDF5 group.
00764     *
00765     * See Resource.h for details.
00766     *
00767     * @param  args
00768     *         The arguments for obtaining the resource. Should be of type Hdf5GroupObject::Args.
00769     *
00770     * @return Returns NULL
00771     */
00772    hid_t* obtainResource(const Args &args) const
00773    {
00774       return NULL;
00775    }
00776 
00777    /**
00778     * Releases an HDF5 group.
00779     *
00780     * See Resource.h for details.
00781     *
00782     * @param  args
00783     *         The arguments for releasing the resource. Should be of type Hdf5GroupObject::Args.
00784     * @param  pHandle
00785     *         A pointer to the handle to be freed.
00786     */
00787    void releaseResource(const Args &args, hid_t* pHandle) const
00788    {
00789       if ((pHandle != NULL) && (*pHandle != -1))
00790       {
00791          H5Gclose(*pHandle);
00792          delete pHandle;
00793       }
00794    }
00795 };
00796 
00797 /**
00798  *  This is a Resource class that closes HDF5 groups. 
00799  *
00800  *  This is a Resource class that closes HDF5 groups. It has a conversion
00801  *  operator to allow a Hdf5GroupResource object to be used where ever a hid_t
00802  *  that represents an open HDF5 group handle may be used.
00803  *
00804  */
00805 class Hdf5GroupResource : public Resource<hid_t, Hdf5GroupObject>
00806 {
00807 public:
00808    /**
00809     * Construct a Resource object that wraps a hid_t HDF5 group handle
00810     *
00811     * This will take ownership of an existing hid_t group handle and 
00812     * will ensure that it is closed.
00813     *
00814     * @param    group
00815     *           The HDF5 group handle.
00816     */
00817    Hdf5GroupResource(hid_t group) :
00818       Resource<hid_t, Hdf5GroupObject>(new hid_t(group), Args())
00819    {
00820    }
00821 
00822    /**
00823     * Default constructor.
00824     */
00825    Hdf5GroupResource() :
00826       Resource<hid_t, Hdf5GroupObject>(NULL, Args())
00827    {
00828    }
00829 
00830    /**
00831     *  Returns a pointer to the underlying hid_t held by this Resource.
00832     *
00833     *  Returns a pointer to the underlying hid_t. This operator,
00834     *  used in conjunction with the dereferencing operator,
00835     *  allows the Hdf5GroupResource object to be used where ever
00836     *  a hid_t would normally be used.
00837     *
00838     *  @return   A pointer to the underlying hid_t held by this Resource.
00839     */
00840    operator hid_t*()
00841    {
00842       return get();
00843    }
00844 };
00845 
00846 /**
00847  *  The Hdf5ErrorHandlerObject is a trait object for use with the Resource template.
00848  *
00849  *  The Hdf5ErrorHandlerObject is a trait object for use with the Resource template.
00850  *  It provides capability for setting and restoring a custom Hdf5 error handler.
00851  *
00852  */
00853 class Hdf5ErrorHandlerObject
00854 {
00855 public:
00856    /**
00857     * This is an implementation detail of the Hdf5ErrorHandlerObject class. 
00858     *
00859     */
00860    struct Args
00861    {
00862       /**
00863        *
00864        */
00865       Args(H5E_auto1_t errorFunc, void* pClientData) : mErrorFunc(errorFunc), mpClientData(pClientData)
00866       {
00867       }
00868 
00869       H5E_auto1_t mErrorFunc;
00870       void* mpClientData;
00871       H5E_auto1_t mOriginalErrorFunc;
00872       void* mpOriginalClientData;
00873    };
00874 
00875    /**
00876     * Obtains an HDF5 group.
00877     *
00878     * See Resource.h for details.
00879     *
00880     * @param  org_args
00881     *         The arguments for obtaining the resource. Should be of type Hdf5GroupObject::Args.
00882     *
00883     * @return Returns NULL
00884     */
00885    hid_t* obtainResource(const Args &org_args) const
00886    {
00887       Args& args = const_cast<Args&>(org_args);
00888       H5Eget_auto1(&args.mOriginalErrorFunc, &args.mpOriginalClientData);
00889       H5Eset_auto1(args.mErrorFunc, args.mpClientData);
00890       return NULL;
00891    }
00892 
00893    /**
00894     * Releases an HDF5 group.
00895     *
00896     * See Resource.h for details.
00897     *
00898     * @param  args
00899     *         The arguments for releasing the resource. Should be of type Hdf5GroupObject::Args.
00900     * @param  pHandle
00901     *         A pointer to the handle to be freed.
00902     */
00903    void releaseResource(const Args &args, hid_t* pHandle) const
00904    {
00905       H5Eset_auto1(args.mOriginalErrorFunc, args.mpOriginalClientData);
00906    }
00907 };
00908 
00909 /**
00910  *  This is a Resource class that sets and restores the Hdf5 error handling function.
00911  */
00912 class Hdf5ErrorHandlerResource : public Resource<hid_t, Hdf5ErrorHandlerObject>
00913 {
00914 public:
00915    /**
00916     * Construct a Resource object.
00917     */
00918    Hdf5ErrorHandlerResource(H5E_auto1_t errorFunc, void* pClientData) :
00919       Resource<hid_t, Hdf5ErrorHandlerObject>(Args(errorFunc, pClientData))
00920    {
00921    }
00922 };
00923 
00924 #endif

Software Development Kit - Opticks 4.9.0 Build 16218