Hdf5CustomWriter.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 HDF5CUSTOMWRITER_H
00011 #define HDF5CUSTOMWRITER_H
00012 
00013 #include "Hdf5Resource.h"
00014 
00015 /**
00016  * This interface should be implemented to allow a custom
00017  * type to be written by the HdfPlugInLib.  In addition
00018  * to implementing this custom interface, a template
00019  * specialization of createHdf5CustomWriter() needs
00020  * to be created for the given type.  This class
00021  * has a highly defined calling order for the functions
00022  * which is defined below:
00023  *    - An Hdf5CustomWriter instance is created by
00024  *      invoking createHdf5CustomWriter().
00025  *    - Hdf5CustomWriter::setDataToWrite() is called
00026  *      and if false is returned, the Hdf5CustomWriter
00027  *      instance is deleted.
00028  *    - Hdf5CustomWriter::getWriteFileType(),
00029  *      Hdf5CustomWriter::getWriteMemoryType() and
00030  *      Hdf5CustomWriter::createDataSpace() are called in
00031  *      any order.
00032  *    - Hdf5CustomWriter::getWriteBuffer() is called.
00033  *    - The Hdf5CustomWriter instance is deleted.
00034  */
00035 class Hdf5CustomWriter
00036 {
00037 public:
00038    /**
00039     * Destroys the writer object.
00040     */
00041    virtual ~Hdf5CustomWriter() {}
00042 
00043    /**
00044     * Sets the data that should be written by
00045     * this writer.  If the given data is not
00046     * supported by this writer, false should
00047     * be returned.
00048     *
00049     * @param pObject
00050     *        The data to write.  This data
00051     *        can safely be cast from void* to
00052     *        T* where T is the type used in 
00053     *        template specialization of 
00054     *        createHdf5CustomWriter() that
00055     *        returned this writer instance.
00056     *
00057     * @return Returns true if the data can be written
00058     * by this writer, false otherwise.
00059     */
00060    virtual bool setDataToWrite(void* pObject) = 0;
00061 
00062    /**
00063     * Return the HDF5 datatype, ie. hid_t that
00064     * represents the file type. 
00065     * This type will be provided as the file datatype
00066     * to either a H5Acreate1() or H5Dcreate1() call in 
00067     * the HDF5 library depending on whether data
00068     * is being written to an HDF5 attribute or an
00069     * HDF5 dataset.  The hid_t returned from
00070     * this function will be properly closed
00071     * by the Hdf5TypeResource object when
00072     * the write operation is complete.
00073     *
00074     * This method and getWriteMemoryType() are provided
00075     * so that the types returned can vary.  For example
00076     * the file type could be packed and the memory type
00077     * unpacked.  Another example would be the file type
00078     * could be big endian and the memory type little
00079     * endian.
00080     * 
00081     *
00082     * @return Returns the HDF5 datatype that
00083     *         represents the file type of the
00084     *         data being written.
00085     */
00086    virtual Hdf5TypeResource getWriteFileType() const = 0;
00087 
00088    /**
00089     * Return the HDF5 datatype, ie. hid_t that
00090     * represents the in-memory type.  The type
00091     * returned should be compatible with the
00092     * void* buffer returned from getWriteBuffer().
00093     * This type will be provided as the memory datatype
00094     * to either a H5Awrite() or H5Dwrite() call in 
00095     * the HDF5 library depending on whether data
00096     * is being written to an HDF5 attribute or an
00097     * HDF5 dataset.  The hid_t returned from
00098     * this function will be properly closed
00099     * by the Hdf5TypeResource object when
00100     * the write operation is complete.
00101     *
00102     * This method and getWriteFileType() are provided
00103     * so that the types returned can vary.  For example
00104     * the file type could be packed and the memory type
00105     * unpacked.  Another example would be the file type
00106     * could be big endian and the memory type little
00107     * endian.
00108     *
00109     * @return Returns the HDF5 datatype that
00110     *         represents the in-memory type of
00111     *         the data being written.
00112     */
00113    virtual Hdf5TypeResource getWriteMemoryType() const = 0;
00114 
00115    /**
00116     * Returns the HDF5 dataspace that should be used
00117     * in the H5Acreate1() or H5Dcreate1().  The dataspace
00118     * returned should be compatible with the void* buffer
00119     * that will be returned from getWriteBuffer().  The
00120     * dataspace returned should also be compatible
00121     * with the dimensions of the data that was provided
00122     * to setDataToWrite().
00123     * The hid_t returned from this function will be 
00124     * properly closed by the Hdf5DataSpaceResource object
00125     * when the write operation is complete.
00126     *
00127     * @return Returns the HDF5 dataspace that represents
00128     *         the data being written.
00129     */
00130    virtual Hdf5DataSpaceResource createDataSpace() const = 0;
00131 
00132    /**
00133     * Returns a buffer suitable to pass to the H5Awrite()
00134     * or H5Dwrite() calls in the HDF5 library.  The size
00135     * and layout of the buffer are dependent on the HDF5
00136     * datatype returned from getWriteMemoryType() and
00137     * the dataspace returned from createDataSpace().
00138     * This pointer should be owned by the Hdf5CustomWriter
00139     * and deleted in the destructor.
00140     *
00141     * @return Returns a buffer suitable to pass to
00142     *         the HDF5 library calls of H5Awrite()
00143     *         or H5Dwrite().
00144     */
00145    virtual const void* getWriteBuffer() const = 0;
00146 };
00147 
00148 /**
00149  * This function should be specialized for any custom types
00150  * that need to be written using the HdfUtilities::writeAttribute()
00151  * or HdfUtilities::writeDataset() functions.
00152  * An example is shown below:
00153  * \code
00154  * struct mySampleType
00155  * {
00156  *   int foo;
00157  *   int bar;
00158  * };
00159  *
00160  * class mySampleTypeWriter; //assume this is declared elsewhere as class mySampleTypeWriter : public Hdf5CustomWriter
00161  * //Want to write mySampleType using Hdf5Utilities::writeAttribute() function.
00162  * template<>
00163  * Hdf5CustomWriter* createHdf5CustomWriter<mySampleType>()
00164  * {
00165  *    return new mySampleTypeWriter();
00166  * }
00167  *
00168  * //now to write the data as an attribute.
00169  * //assume groupId is a hid_t of the HDF5 group we want to write the attribute to.
00170  * mySampleType sampleData;
00171  * sampleData.foo = 3;
00172  * sampleData.bar = 100;
00173  * HdfUtilities::writeAttribute(groupId, "myAttribute", sampleData);
00174  * //the writeAttribute call above will use your createHdf5CustomWriter specialization and your subclass
00175  * //of Hdf5CustomWriter to perform the write of mySampleType to the Hdf5 file as an HDF5 attribute.
00176  *
00177  * //now to write the data to a dataset
00178  * //assume fd is hid_t which is an open HDF5 file handle
00179  * HdfUtilities::writeDataset(fd, "/Level1/Level2/MySampleDataset", sampleData);
00180  * //the writeDataset call above will use your createHdf5CustomWriter specialization and your subclass
00181  * //of Hdf5CustomWriter to perform the write of mySampleType to the Hdf5 file as an HDF5 dataset.
00182  * \endcode
00183  * 
00184  * @return Returns an Hdf5CustomWriter implementation that is suitable 
00185  *         for writing the given type to an HDF5 file.  If the given
00186  *         value is unsupported by the writer, NULL should be returned.
00187  */
00188 template<typename T>
00189 Hdf5CustomWriter* createHdf5CustomWriter();
00190 
00191 #endif

Software Development Kit - Opticks 4.9.0 Build 16218