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 PLUGINARGLIST_H 00011 #define PLUGINARGLIST_H 00012 00013 #include "PlugInArg.h" 00014 #include "PlugInManagerServices.h" 00015 #include "TypeConverter.h" 00016 00017 #include <string> 00018 #include <vector> 00019 00020 /** 00021 * All input/output arguments for a plug-in 00022 * 00023 * This class is responsible for managing all plug-in arguments that are required 00024 * for a plug-in argument list. 00025 * 00026 * @see PlugInArg 00027 */ 00028 class PlugInArgList 00029 { 00030 public: 00031 /** 00032 * Empty the Plug-In Argument List. 00033 * 00034 * The emptyList() method clears the vector of Plug-In Arguments, 00035 * but does NOT destruct the individual Plug-In Arguments. It 00036 * simply removes the references to them from the vector. 00037 */ 00038 virtual void emptyList() = 0; 00039 00040 /** 00041 * Add an argument to a PlugInArgList with a default. 00042 * 00043 * @param name 00044 * The name for the argument. 00045 * @param defaultValue 00046 * The default value for the argument. 00047 * @param description 00048 * The user-centric description for the argument. 00049 * 00050 * @return True if the operation succeeded, false otherwise. 00051 */ 00052 template<typename T> 00053 bool addArg(const std::string& name, const T& defaultValue, const std::string &description = std::string()) 00054 { 00055 Service<PlugInManagerServices> pPims; 00056 00057 PlugInArg* pArg = pPims->getPlugInArg(); 00058 if (pArg != NULL) 00059 { 00060 pArg->setName(name); 00061 pArg->setType(TypeConverter::toString<T>()); 00062 pArg->setDefaultValue(&defaultValue); 00063 pArg->setDescription(description); 00064 return addArg(*pArg); 00065 } 00066 00067 return false; 00068 } 00069 00070 /** 00071 * Add an argument to a PlugInArgList with a default. 00072 * 00073 * This version allows NULL defaults. 00074 * 00075 * @param name 00076 * The name for the argument. 00077 * @param pDefaultValue 00078 * The default value for the argument as a pointer. 00079 * @param description 00080 * The user-centric description for the argument. 00081 * 00082 * @return True if the operation succeeded, false otherwise. 00083 */ 00084 template<typename T> 00085 bool addArg(const std::string& name, const T* pDefaultValue, const std::string &description = std::string()) 00086 { 00087 Service<PlugInManagerServices> pPims; 00088 00089 PlugInArg* pArg = pPims->getPlugInArg(); 00090 if (pArg != NULL) 00091 { 00092 pArg->setName(name); 00093 pArg->setType(TypeConverter::toString<T>()); 00094 pArg->setDefaultValue(pDefaultValue); 00095 pArg->setDescription(description); 00096 return addArg(*pArg); 00097 } 00098 00099 return false; 00100 } 00101 00102 /** 00103 * Add an argument to a PlugInArgList without a default. 00104 * 00105 * @param name 00106 * The name for the argument. 00107 * @param description 00108 * The user-centric description for the argument. 00109 * 00110 * @return True if the operation succeeded, false otherwise. 00111 */ 00112 template<typename T> 00113 bool addArg(const std::string &name, const std::string &description = std::string()) 00114 { 00115 Service<PlugInManagerServices> pPims; 00116 00117 PlugInArg* pArg = pPims->getPlugInArg(); 00118 if (pArg != NULL) 00119 { 00120 pArg->setName(name); 00121 pArg->setType(TypeConverter::toString<T>()); 00122 pArg->setDescription(description); 00123 return addArg(*pArg); 00124 } 00125 00126 return false; 00127 } 00128 00129 /** 00130 * Add a Plug-In Argument to the list. 00131 * 00132 * The addArg() method adds the given Plug-In Argument to the and of 00133 * the existing Argument list, and returns a flag indicating whether 00134 * it was successful. 00135 * 00136 * @param arg 00137 * Reference to the Plug-In Argument to be added to the list. 00138 * 00139 * @return The method returns true if the Plug-In Argument was 00140 * successfully added to the list, otherwise returns false. 00141 */ 00142 virtual bool addArg(const PlugInArg& arg) = 0; 00143 00144 /** 00145 * Returns the actual or default value for this plug-in argument. 00146 * 00147 * This version returns a pointer to the type requested. This is useful for model types 00148 * which must always be used as pointers. 00149 * 00150 * @param name 00151 * The name of the plug-in argument to retrive. 00152 * 00153 * @return A pointer to the actual value for this plug-in argument 00154 * if the actual value has been set. Otherwise the default 00155 * value is returned if set. If neither is set or the argument 00156 * is not present in the arg list or the argument type does 00157 * not match that provided exactly, NULL is returned 00158 */ 00159 template<typename T> 00160 T* getPlugInArgValue(const std::string &name) const 00161 { 00162 PlugInArg *pArg = NULL; 00163 if(!getArg(name, pArg) || (pArg == NULL)) 00164 { 00165 return NULL; 00166 } 00167 return pArg->getPlugInArgValue<T>(); 00168 } 00169 00170 /** 00171 * Returns the actual or default value for this plug-in argument. 00172 * 00173 * This version returns a pointer to the type requested. This is useful for model types 00174 * which must always be used as pointers. 00175 * 00176 * @param name 00177 * The name of the plug-in argument to retrive. 00178 * 00179 * @warning This function does not do any type checking. 00180 * If the type of the argument does not match the 00181 * template argument, the return value is undefined. 00182 * 00183 * @return A pointer to the actual value for this plug-in argument 00184 * if the actual value has been set. Otherwise the default 00185 * value is returned if set. If neither is set or the argument 00186 * is not present in the arg list, NULL is returned 00187 */ 00188 template<typename T> 00189 T* getPlugInArgValueUnsafe(const std::string &name) const 00190 { 00191 PlugInArg *pArg = NULL; 00192 if(!getArg(name, pArg) || (pArg == NULL)) 00193 { 00194 return NULL; 00195 } 00196 return pArg->getPlugInArgValueUnsafe<T>(); 00197 } 00198 00199 /** 00200 * Returns the actual or default value for this plug-in argument. 00201 * 00202 * This version sets the output argument to the value of the requested plug-in argument. This 00203 * version is useful for basic types and other types that have an equals operator. 00204 * 00205 * @param name 00206 * The name of the plug-in argument to retrive. 00207 * 00208 * @param value 00209 * Output argument which will contain the value of the argument. This will 00210 * remain unchanged if the argument is not present in the arg list or 00211 * there the actual and default values are not set. 00212 * 00213 * @return False if the argument is not present in the arg list or the actual 00214 * and default argument values are not set, or the type of argument 00215 * does not exactly match that provided. 00216 * True if the actual or default value for the argument is set. If the 00217 * actual or default argument value is a NULL pointer, the output argument 00218 * will remain unchanged and this function will return true. 00219 */ 00220 template<typename T> 00221 bool getPlugInArgValue(const std::string &name, T &value) const 00222 { 00223 PlugInArg *pArg = NULL; 00224 if(!getArg(name, pArg) || (pArg == NULL)) 00225 { 00226 return false; 00227 } 00228 if (TypeConverter::toString<T>() != pArg->getType()) 00229 { 00230 return false; 00231 } 00232 00233 T *pValue = pArg->getPlugInArgValueUnsafe<T>(); 00234 if(pValue == NULL) 00235 { 00236 return pArg->isActualSet() || pArg->isDefaultSet(); 00237 } 00238 value = *pValue; 00239 return true; 00240 } 00241 00242 /** 00243 * Sets the actual value for a plug-in argument. 00244 * 00245 * @param name 00246 * The name of the plug-in argument for which to set its value. 00247 * @param pValue 00248 * The actual value of the argument. 00249 * 00250 * @return Returns \c true if the value is successfully set into the arg. 00251 * Returns \c false if the argument is not present in the arg list 00252 * or if the type of the given value is not the same as the arg 00253 * type. 00254 * 00255 * @see PlugInArg::setPlugInArgValue() 00256 */ 00257 template<typename T> 00258 bool setPlugInArgValue(const std::string &name, T *pValue) 00259 { 00260 PlugInArg *pArg = NULL; 00261 if(!getArg(name, pArg) || (pArg == NULL)) 00262 { 00263 return false; 00264 } 00265 00266 return pArg->setPlugInArgValue(pValue); 00267 } 00268 00269 /** 00270 * Set the actual value for a plug-in argument. 00271 * 00272 * This method performs loose type checking, which requires that \c T be a 00273 * subclass of TypeAwareObject. If \c T is not the same or a subclass of 00274 * the argument type, this method will fail. 00275 * 00276 * @param name 00277 * The name of the plug-in argument for which to set its value. 00278 * @param pValue 00279 * The actual value of the argument. 00280 * 00281 * @return Returns \c true if the value is successfully set into the arg. 00282 * Returns \c false if the argument is not present in the arg list 00283 * or if the type of the given value is not a kind of the arg type. 00284 * 00285 * @see PlugInArg::setPlugInArgValueLoose() 00286 */ 00287 template<typename T> 00288 bool setPlugInArgValueLoose(const std::string &name, T *pValue) 00289 { 00290 PlugInArg *pArg = NULL; 00291 if(!getArg(name, pArg) || (pArg == NULL)) 00292 { 00293 return false; 00294 } 00295 00296 return pArg->setPlugInArgValueLoose(pValue); 00297 } 00298 00299 /** 00300 * Get a Plug-In Argument from the list. 00301 * 00302 * The getArg() method sets an output parameter to point to the 00303 * desired Plug-In Argument, if it exists, and returns a flag 00304 * indicating success status. 00305 * 00306 * @param argName 00307 * Reference to the Plug-In Argument to be added to the list. 00308 * @param arg 00309 * Pointer that will be set to the desired Plug-In Argument 00310 * if found, or NULL if not found. 00311 * 00312 * @return The method returns true if the Plug-In Argument was 00313 * successfully found in the list, otherwise returns false. 00314 */ 00315 virtual bool getArg(const std::string& argName, PlugInArg*& arg) const = 0; 00316 00317 /** 00318 * Get a Plug-In Argument from the list. 00319 * 00320 * This method sets an output parameter to point to the 00321 * desired Plug-In Argument, if it exists, and returns a flag 00322 * indicating success status. 00323 * 00324 * @param argNumber 00325 * Index into the vector containing the Plug-In Argument List. 00326 * Since arguments are always added to the end of the vector, 00327 * it is possible to pull them back out using an offset 00328 * number, rather than specifying the name of the Argument. 00329 * An index of 0 corresponds to the first argument in the list, 00330 * and an index of getCount()-1 corresponds to the last 00331 * argument in the list. 00332 * @param arg 00333 * Pointer that will be set to the desired Plug-In Argument 00334 * if found, or NULL if not found. 00335 * 00336 * @return The method returns true if the index was a valid index in 00337 * the list, otherwise returns false. 00338 */ 00339 virtual bool getArg(int argNumber, PlugInArg*& arg) const = 0; 00340 00341 /** 00342 * Get the number of Arguments in the Plug-In Argument List. 00343 * 00344 * The getCount() method returns the number of arguments that have 00345 * been added to the list using addArg() or catenateLists(). 00346 * 00347 * @return The method returns the number of elements in the Plug-In 00348 * Argument List, or 0 if none have been added. 00349 */ 00350 virtual unsigned short getCount() const = 0; 00351 00352 /** 00353 * Concatenate a Plug-In Argument List to the end of this list. 00354 * 00355 * The catenateLists() method adds each Argument in the given Plug-In 00356 * Argument List to the current list using addArg(). 00357 * 00358 * @param plugInArg 00359 * Plug-In Argument List whose elements are to be added to 00360 * the end of this List. 00361 * 00362 * @return The method returns true if all items in argList were 00363 * successfully added to this list, otherwise returns false. 00364 */ 00365 virtual bool catenateLists(const PlugInArgList& plugInArg) = 0; 00366 00367 protected: 00368 /** 00369 * This should be destroyed by calling PlugInManagerServices::destroyPlugInArgList. 00370 */ 00371 virtual ~PlugInArgList() {} 00372 }; 00373 00374 #endif // PLUGINARGLIST_H