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 DATAVARIANTANYDATA_H 00011 #define DATAVARIANTANYDATA_H 00012 00013 #include "AnyData.h" 00014 #include "DataVariant.h" 00015 #include "ObjectResource.h" 00016 #include "SessionItemDeserializer.h" 00017 #include "SessionItemSerializer.h" 00018 #include "xmlreader.h" 00019 #include "xmlwriter.h" 00020 00021 /** 00022 * A custom data container for an Any element that contains a DataVariant. 00023 * 00024 * This subclass of AnyData is provided as a convenience for data that can be 00025 * stored in a DataVariant. 00026 */ 00027 class DataVariantAnyData : public AnyData 00028 { 00029 public: 00030 /** 00031 * Creates the custom data object. 00032 * 00033 * This constructor creates an empty data object with an empty DataVariant. 00034 * It can be called directly by plug-ins to create the custom data object 00035 * to set into an Any element provided that the plug-in module will remain 00036 * loaded for the lifetime of the element. Plug-ins should create this 00037 * object from the ObjectFactory if the plug-in module will be unloaded 00038 * before the Any element is destroyed. 00039 */ 00040 DataVariantAnyData() 00041 { 00042 } 00043 00044 /** 00045 * Creates and initializes the custom data object. 00046 * 00047 * This constructor creates a data object and initializes the internal 00048 * DataVariant with the given value. It can be called directly by plug-ins 00049 * to create the custom data object to set into an Any element provided 00050 * that the plug-in module will remain loaded for the lifetime of the 00051 * element. Plug-ins must create an empty object from the ObjectFactory if 00052 * the plug-in module will be unloaded before the Any element is destroyed. 00053 * 00054 * @param value 00055 * The data value with which the internal DataVariant is 00056 * initialized. 00057 * 00058 * @see setAttribute() 00059 */ 00060 template<typename T> 00061 DataVariantAnyData(const T& value) : 00062 mData(value) 00063 { 00064 } 00065 00066 /** 00067 * Destroys the custom data object. 00068 * 00069 * The destructor is called by the Any element when the element is 00070 * destroyed. The internal DataVariant is deleted, thereby deleting the 00071 * data it contains. 00072 * 00073 * @see DataVariant::~DataVariant() 00074 */ 00075 ~DataVariantAnyData() 00076 { 00077 } 00078 00079 /** 00080 * Creates a copy of the custom data. 00081 * 00082 * This method is called when the Any::copy() method is called to copy the 00083 * data element. The default implementation of this method creates a new 00084 * DataVariantAnyData and sets the new internal DataVariant equal to this 00085 * DataVariant. 00086 * 00087 * @return A pointer to a new DataVariantAnyData object containing a copy 00088 * of this data. 00089 * 00090 * @see DataVariant::operator=(const DataVariant&) 00091 */ 00092 AnyData* copy() const 00093 { 00094 FactoryResource<DataVariantAnyData> pData; 00095 pData->mData = mData; 00096 00097 return pData.release(); 00098 } 00099 00100 /** 00101 * Saves the DataVariant as part of a full session save. 00102 * 00103 * @param serializer 00104 * The object to use to save the DataVariant as part of the current 00105 * session. 00106 * 00107 * @return Returns \c true if the DataVariant was successfully saved and 00108 * \c false otherwise. 00109 */ 00110 bool serialize(SessionItemSerializer& serializer) const 00111 { 00112 XMLWriter xml("DataVariant"); 00113 if (mData.toXml(&xml) == false) 00114 { 00115 return false; 00116 } 00117 00118 return serializer.serialize(xml); 00119 } 00120 00121 /** 00122 * Restores the DataVariant from a saved session. 00123 * 00124 * @param deserializer 00125 * The object to use to restore the DataVariant from a saved 00126 * session. 00127 * 00128 * @return Returns \c true if the DataVariant was successfully restored and 00129 * \c false otherwise. 00130 */ 00131 bool deserialize(SessionItemDeserializer& deserializer) 00132 { 00133 XmlReader reader(NULL, false); 00134 00135 XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* pRoot = deserializer.deserialize(reader, "DataVariant"); 00136 if (pRoot == NULL) 00137 { 00138 return false; 00139 } 00140 00141 return mData.fromXml(pRoot, XmlBase::VERSION); 00142 } 00143 00144 /** 00145 * Sets the custom data to set into an Any element. 00146 * 00147 * @param data 00148 * The custom data. 00149 */ 00150 void setAttribute(const DataVariant& data) 00151 { 00152 mData = data; 00153 } 00154 00155 /** 00156 * Returns the custom data to set into an Any element. 00157 * 00158 * @return A reference to the data variant storing the custom data. The 00159 * data can be modified directly. 00160 */ 00161 DataVariant& getAttribute() 00162 { 00163 return mData; 00164 } 00165 00166 /** 00167 * Returns read-only access to the custom data to set into an Any element. 00168 * 00169 * @return A const reference to the data variant storing the custom data. 00170 * The data represented by the returned reference should not be 00171 * modified. To modify the values, call the non-const version of 00172 * getAttribute(). 00173 */ 00174 const DataVariant& getAttribute() const 00175 { 00176 return mData; 00177 } 00178 00179 private: 00180 DataVariant mData; 00181 }; 00182 00183 #endif