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