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 PLUGINARG_H 00011 #define PLUGINARG_H 00012 00013 #include "TypeAwareObject.h" 00014 #include "TypeConverter.h" 00015 00016 #include <string> 00017 00018 /** 00019 * Containers for individual input and output plug-in parameters. 00020 * 00021 * Defines the interface that may be used by plug-ins to manipulate 00022 * their input and output arguments. 00023 * 00024 * @see TypeAwareObject, PlugInArgList 00025 */ 00026 class PlugInArg : virtual public TypeAwareObject 00027 { 00028 public: 00029 /** 00030 * Retrieves the plug-in argument name. 00031 * 00032 * @return The argument name. 00033 */ 00034 virtual const std::string& getName() const = 0; 00035 00036 /** 00037 * Retrieves the plug-in argument data type. 00038 * 00039 * @return A string representing the argument data type. 00040 */ 00041 virtual const std::string& getType() const = 0; 00042 00043 /** 00044 * Returns the actual or default value for this plug-in argument. 00045 * 00046 * @warning This function does not do any type checking. 00047 * If the type of the argument does not match the 00048 * template argument, the return value is undefined. 00049 * 00050 * @return A pointer to the actual value for this plug-in argument 00051 * if the actual value has been set. Otherwise the default 00052 * value is returned if set. If neither is set, 00053 * NULL is returned. 00054 */ 00055 template<typename T> 00056 T* getPlugInArgValueUnsafe() 00057 { 00058 T *pVal(NULL); 00059 if(isActualSet()) 00060 { 00061 pVal = static_cast<T*>(getActualValue()); 00062 } 00063 else if(isDefaultSet()) 00064 { 00065 pVal = static_cast<T*>(getDefaultValue()); 00066 } 00067 return pVal; 00068 } 00069 00070 /** 00071 * Returns the actual or default value for this plug-in argument. 00072 * 00073 * @return A pointer to the actual value for this plug-in argument 00074 * if the actual value has been set. Otherwise the default 00075 * value is returned if set. If neither is set, or the type 00076 * of this argument does not exactly match that provided, 00077 * NULL is returned. 00078 */ 00079 template<typename T> 00080 T* getPlugInArgValue() 00081 { 00082 if (TypeConverter::toString<T>() != getType()) 00083 { 00084 return NULL; 00085 } 00086 00087 return getPlugInArgValueUnsafe<T>(); 00088 } 00089 00090 /** 00091 * Returns the default value for this plug-in argument. 00092 * 00093 * @return A pointer to the default value for this plug-in 00094 * argument if a default value has been set. Otherwise 00095 * NULL is returned. 00096 */ 00097 virtual void* getDefaultValue() const = 0; 00098 00099 /** 00100 * Queries whether the default value for the plug-in argument has 00101 * been set. 00102 * 00103 * @return True if a default value has been set for the argument, 00104 * otherwise returns false. 00105 */ 00106 virtual bool isDefaultSet() const = 0; 00107 00108 /** 00109 * Returns the default value for this plug-in argument. 00110 * 00111 * @return A pointer to the actual value for this plug-in argument 00112 * if the actual value has been set. Otherwise NULL is 00113 * returned. 00114 */ 00115 virtual void* getActualValue() const = 0; 00116 00117 /** 00118 * Queries whether the actual value for the plug-in argument has 00119 * been set. 00120 * 00121 * @return True if the actual value has been set for the argument, 00122 * otherwise returns false. 00123 */ 00124 virtual bool isActualSet() const = 0; 00125 00126 /** 00127 * Sets the actual value for this plug-in argument. 00128 * 00129 * The type of the given value must be the same as the arg type. 00130 * 00131 * @param pValue 00132 * The actual value of the argument. 00133 * 00134 * @return Returns \c true if the value is successfully set into the arg. 00135 * Returns \c false if type of the given value is not the same as 00136 * the arg type. 00137 * 00138 * @see TypeConverter::toString() 00139 */ 00140 template<typename T> 00141 bool setPlugInArgValue(T* pValue) 00142 { 00143 if (TypeConverter::toString<T>() != getType()) 00144 { 00145 return false; 00146 } 00147 00148 setActualValue(reinterpret_cast<void*>(pValue)); 00149 return true; 00150 } 00151 00152 /** 00153 * Sets the actual value for this plug-in argument. 00154 * 00155 * This method performs loose type checking, which requires that \c T be a 00156 * subclass of TypeAwareObject. If \c T is not the same or a subclass of 00157 * the argument type, this method will fail. 00158 * 00159 * @param pValue 00160 * The actual value of the argument. 00161 * 00162 * @return Returns \c true if the value is successfully set into the arg. 00163 * Returns \c false if the type of the given value is not a kind 00164 * of the arg type. 00165 * 00166 * @see TypeAwareObject::isKindOf() 00167 */ 00168 template<typename T> 00169 bool setPlugInArgValueLoose(T* pValue) 00170 { 00171 TypeAwareObject* pTypeAwareObject = dynamic_cast<TypeAwareObject*>(pValue); 00172 if ((pTypeAwareObject != NULL) && (pTypeAwareObject->isKindOf(getType()) == false)) 00173 { 00174 return false; 00175 } 00176 00177 setActualValue(reinterpret_cast<void*>(pValue)); 00178 return true; 00179 } 00180 00181 /** 00182 * Sets the argument's default value. 00183 * 00184 * This method sets the argument's default value to the given value 00185 * and sets a flag indicating that a default value has been set. 00186 * 00187 * @param pDefValue 00188 * A pointer to the default value. The value type must be 00189 * identical to getType(). 00190 * 00191 * @param tryDeepCopy 00192 * If true, the PlugInArg will attempt to make a deep copy 00193 * of the provided value for types that DataVariant can 00194 * hold. If false or DataVariant cannot hold the given 00195 * type, the PlugInArg will directly store 00196 * and hold the provided pDefValue and will not take 00197 * ownership. 00198 */ 00199 virtual void setDefaultValue(const void* pDefValue, bool tryDeepCopy = true) = 0; 00200 00201 /** 00202 * Sets the argument's actual value. 00203 * 00204 * This method sets the argument's actual value to the given value 00205 * and sets a flag indicating that the actual value has been set. 00206 * 00207 * @param pValue 00208 * A pointer to the actual value. The value type must be 00209 * identical to getType(). 00210 * 00211 * @param tryDeepCopy 00212 * If true, the PlugInArg will attempt to make a deep copy 00213 * of the provided value for types that DataVariant can 00214 * hold. If false or DataVariant cannot hold the given 00215 * type, the PlugInArg will directly store 00216 * and hold the provided pValue and will not take 00217 * ownership. 00218 */ 00219 virtual void setActualValue(const void* pValue, bool tryDeepCopy = true) = 0; 00220 00221 /** 00222 * Sets the argument name. 00223 * 00224 * @param name 00225 * A string containing the new argument name, which cannot 00226 * be empty. 00227 */ 00228 virtual void setName(const std::string& name) = 0; 00229 00230 /** 00231 * Sets the argument type. 00232 * 00233 * @param type 00234 * A string containing the new argument type. 00235 * 00236 * @return True if the type was successfully set, otherwise false. 00237 */ 00238 virtual bool setType(const std::string& type) = 0; 00239 00240 /** 00241 * Set the user-centric description of this argument. 00242 * 00243 * @param description 00244 * The description of this argument. 00245 */ 00246 virtual void setDescription(const std::string &description) = 0; 00247 00248 /** 00249 * Get the user-centric description of this argument. 00250 * 00251 * @return The description of this argument. 00252 */ 00253 virtual const std::string &getDescription() const = 0; 00254 00255 protected: 00256 /** 00257 * This cannot be destroyed directly. This should be destroyed 00258 * by destroying the PlugInArgList it belongs to. 00259 * 00260 * @see PlugInArgList::~PlugInArgList. 00261 */ 00262 virtual ~PlugInArg() {} 00263 }; 00264 00265 #endif