Hdf5Attribute.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 HDF5ATTRIBUTE_H
00011 #define HDF5ATTRIBUTE_H
00012 
00013 #include <string>
00014 
00015 #include "DataVariant.h"
00016 #include "Hdf5Data.h"
00017 #include "Hdf5Node.h"
00018 #include "TypeConverter.h"
00019 
00020 /**
00021  * The HDF Attribute class is designed to provide an abstraction for the HDF Attribute data type.
00022  * Attributes are small metadata elements that are associated with an Hdf5Dataset.
00023  *
00024  * The Hdf5Attribute class CANNOT be created by itself. Only an Hdf5Element can create or destroy
00025  * them.
00026   * Currently supported are all basic types and vectors of basic types. Any other types must have their
00027  * memory allocated manually. Examples:
00028  *
00029  * @code
00030  * // assume pDataset exists and is a member of some Hdf5Group
00031  * // create a complex attribute
00032  * Hdf5Attribute* pAttr = pDataset->addAttribute("complex", 2);
00033  * IntegerComplex* pCompl = new IntegerComplex[2];
00034  * // set pCompl
00035  * pAttr->setValue(pCompl); // pAttr now owns this memory
00036  * @endcode
00037  *
00038  * @code
00039  * // assume pDataset exists and is a member of some Hdf5Group
00040  * // create a vector<int> attribute
00041  * // since this is a built-in type, the attribute allocates the memory for us
00042  * Hdf5Attribute* pAttr = pDataset->addAttribute("vector<int>", 2);
00043  * int* pVector = pAttr->getFirstValue(); // get a pointer to the first value in the vector and start writing
00044  * fread(pVector, sizeof(int), 2, pInputFile); // read directly from file into attribute
00045  * @endcode
00046  *
00047  * @see Hdf5Dataset::addAttribute(), Hdf5File::addAttribute()
00048  */
00049 class Hdf5Attribute : public Hdf5Data, public Hdf5Node
00050 {
00051 public:
00052    /**
00053     * Sets the value of the attribute.
00054     *
00055     * @param  var
00056     *         A variant holding the attribute value.
00057     */
00058    virtual void setVariant(const DataVariant &var);
00059 
00060    /**
00061     * Returns a reference to the object.
00062     *
00063     * @return Returns a reference to the member value representing the attribute.
00064     */
00065    virtual const DataVariant &getVariant() const;
00066 
00067    /**
00068     * Returns a reference to the object.
00069     *
00070     * @return Returns a reference to the member value representing the attribute.
00071     */
00072    virtual DataVariant &getVariant();
00073 
00074    /**
00075     * Retrieves the value.
00076     *
00077     * @return true if the value was successfully retrieved or valse otherwise.
00078     */
00079    template<class T>
00080    bool getValueAs(T& value) const
00081    {
00082       return getVariant().getValue(value);
00083    }
00084 
00085 
00086    /**
00087     * Returns a pointer to the value or NULL if the type does not match.
00088     *
00089     * @return Returns the pointer value representing the attribute.
00090     */
00091    template<class T>
00092    const T* getValueAsPointer() const
00093    {
00094       return getVariant().getPointerToValue<T>();
00095    }
00096 
00097 protected:
00098    /**
00099     * Creates an attribute with a given type and number of elements.
00100     *
00101     * This method cannot be called directly. Use Hdf5Element::addAttribute()
00102     * to add an attribute to an Hdf5Element.
00103     *
00104     * @param  pParent
00105     *         The Hdf5Node that this attribute belongs to.
00106     * @param  name
00107     *         The name of the attribute.
00108     * @param  value
00109     *         The current value of the attribute.
00110     */
00111    Hdf5Attribute(Hdf5Node* pParent, const std::string& name, const DataVariant& value);
00112 
00113    /**
00114     * Destroys an Hdf5Attribute object.
00115     *
00116     * Use Hdf5Element::removeAttribute() to delete an Hdf5Attribute from an Hdf5Element.
00117     */
00118    ~Hdf5Attribute();
00119 
00120    Hdf5Data::DataReader* createReader() const;
00121 
00122    /* Only Hdf5Element can create or destroy Hdf5Attribute types.
00123     */
00124    friend class Hdf5Element;
00125 
00126 private:
00127    DataVariant mValue;
00128 };
00129 
00130 #endif

Software Development Kit - Opticks 4.9.0 Build 16218