Hdf5Data.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 HDF5DATA_H
00011 #define HDF5DATA_H
00012 
00013 #include "Hdf5CustomReader.h"
00014 
00015 #include <memory>
00016 #include <string>
00017 #include <vector>
00018 
00019 #include <hdf5.h>
00020 
00021 /**
00022  * This class is an abstraction of the data contained within a HDF5 attribute
00023  * or HDF5 dataset.  This class provides a way to determine the type of
00024  * data stored within the attribute or dataset along with the dimensions.
00025  * This class also provides a templated method to extract the contents of
00026  * the attribute or dataset.
00027  */
00028 class Hdf5Data
00029 {
00030 public:
00031    virtual ~Hdf5Data() {}
00032 
00033    /**
00034     * Gets the type name of this HDF data.
00035     *
00036     * @return the type name of this HDF data.
00037     *
00038     * @see HdfUtilities::hdf5TypeToTypeString() for more details.
00039     */
00040    virtual std::string getTypeName() const;
00041 
00042    /**
00043     * Sets the type name of this HDF data.
00044     *
00045     * @param typeName
00046     *        The type name of this HDF data.  See HdfUtilities::hdf5TypeToTypeString()
00047     *        for more details.
00048     */
00049    virtual void setTypeName(const std::string& typeName);
00050 
00051    /**
00052     * Returns the sizes of the dimensions of the HDF data.  If
00053     * data contains a scalar value, the returned vector will be empty.
00054     * For example, if the data has 3 dimensions with sizes of 10, 50, 5,
00055     * the getDimensionSizes().size() method should return 3. The
00056     * getDimensionSizes()[0] should return 10 and getDimensionSizes()[1]
00057     * should return 50.
00058     *
00059     * @return The sizes of the dimensions of the dataset.
00060     */
00061    virtual const std::vector<hsize_t>& getDimensionSizes() const;
00062 
00063    /**
00064     * Sets the dimension sizes of this HDF data.  If the data
00065     * contains a scalar value, the provided vector should be empty.
00066     *
00067     * @param dimensionSizes
00068     *        The dimension sizes of the HDF data.
00069     */
00070    virtual void setDimensionSizes(const std::vector<hsize_t>& dimensionSizes);
00071 
00072    /**
00073     * Reads data from the HDF object.  The caller of this function
00074     * is responsible for deleting the returned memory.
00075     *
00076     * @return Returns the parsed value.  If the data was not of the correct
00077     *         type or dimension, NULL will be returned.
00078     */
00079    template<typename T>
00080    T* readData() const
00081    {
00082       std::auto_ptr<DataReader> pDataReader(createReader());
00083       DO_IF(pDataReader.get() == NULL, return NULL);
00084       hid_t type = pDataReader->getType();
00085       DO_IF(type < 0, return NULL);
00086       hid_t dataSpace = pDataReader->getDataSpace();
00087       DO_IF(dataSpace < 0, return NULL);
00088       std::auto_ptr<Hdf5CustomReader> pCustomReader(createHdf5CustomReader<T>(type));
00089       DO_IF(pCustomReader.get() == NULL, return NULL);
00090       return reinterpret_cast<T*>(pDataReader->readData(pCustomReader.get()));
00091    }
00092 
00093    /**
00094     * \cond INTERNAL
00095     * This class is not being documented because in order to construct an
00096     * instance that will be used, you have to subclass this interface and
00097     * only two classes are currently permitted to do so.  This class
00098     * is not protected because then the two subclasses would have to
00099     * show that in their header files as well.
00100     */
00101    class DataReader
00102    {
00103    public:
00104       virtual ~DataReader() {}
00105       virtual hid_t getType() = 0;
00106       virtual hid_t getDataSpace() = 0;
00107       virtual void* readData(Hdf5CustomReader* pReader) = 0;
00108    };
00109    /// \endcond
00110 
00111 protected:
00112    Hdf5Data();
00113 
00114 
00115    virtual DataReader* createReader() const = 0;
00116 
00117    std::string mTypeName;
00118    std::vector<hsize_t> mDimensionSizes;
00119 };
00120 
00121 #endif

Software Development Kit - Opticks 4.9.0 Build 16218