00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "Teuchos_StandardParameterEntryValidators.hpp"
00030
00031
00032 std::string Teuchos::getVerbosityLevelParameterValueName(
00033 const EVerbosityLevel verbLevel
00034 )
00035 {
00036 switch(verbLevel) {
00037 case VERB_DEFAULT:
00038 return "default";
00039 case VERB_NONE:
00040 return "none";
00041 case VERB_LOW:
00042 return "low";
00043 case VERB_MEDIUM:
00044 return "medium";
00045 case VERB_HIGH:
00046 return "high";
00047 case VERB_EXTREME:
00048 return "extreme";
00049 default:
00050 TEST_FOR_EXCEPT("Should never get here!");
00051 }
00052 return "";
00053 }
00054
00055
00056 Teuchos::RCP<
00057 Teuchos::StringToIntegralParameterEntryValidator<Teuchos::EVerbosityLevel>
00058 >
00059 Teuchos::verbosityLevelParameterEntryValidator(
00060 std::string const& defaultParameterName
00061 )
00062 {
00063 return rcp(
00064 new StringToIntegralParameterEntryValidator<EVerbosityLevel>(
00065 tuple<std::string>(
00066 getVerbosityLevelParameterValueName(VERB_DEFAULT),
00067 getVerbosityLevelParameterValueName(VERB_NONE),
00068 getVerbosityLevelParameterValueName(VERB_LOW),
00069 getVerbosityLevelParameterValueName(VERB_MEDIUM),
00070 getVerbosityLevelParameterValueName(VERB_HIGH),
00071 getVerbosityLevelParameterValueName(VERB_EXTREME)
00072 ),
00073 tuple<std::string>(
00074 "Use level set in code",
00075 "Produce no output",
00076 "Produce minimal output",
00077 "Produce a little more output",
00078 "Produce a higher level of output",
00079 "Produce the highest level of output"
00080 ),
00081 tuple<EVerbosityLevel>(
00082 VERB_DEFAULT,
00083 VERB_NONE,
00084 VERB_LOW,
00085 VERB_MEDIUM,
00086 VERB_HIGH,
00087 VERB_EXTREME
00088 ),
00089 defaultParameterName
00090 )
00091 );
00092 }
00093
00094
00095 namespace Teuchos {
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 AnyNumberParameterEntryValidator::AnyNumberParameterEntryValidator()
00107 :preferredType_(PREFER_DOUBLE),
00108 acceptedTypes_(AcceptedTypes())
00109 {
00110 finishInitialization();
00111 }
00112
00113
00114 AnyNumberParameterEntryValidator::AnyNumberParameterEntryValidator(
00115 EPreferredType const preferredType,
00116 AcceptedTypes const& acceptedTypes
00117 )
00118 :preferredType_(preferredType),
00119 acceptedTypes_(acceptedTypes)
00120 {
00121 finishInitialization();
00122 }
00123
00124
00125
00126
00127
00128 int AnyNumberParameterEntryValidator::getInt(
00129 const ParameterEntry &entry, const std::string ¶mName
00130 ,const std::string &sublistName, const bool activeQuery
00131 ) const
00132 {
00133 const any &anyValue = entry.getAny(activeQuery);
00134 if( acceptedTypes_.allowInt() && anyValue.type() == typeid(int) )
00135 return any_cast<int>(anyValue);
00136 if( acceptedTypes_.allowDouble() && anyValue.type() == typeid(double) )
00137 return static_cast<int>(any_cast<double>(anyValue));
00138 if( acceptedTypes_.allowString() && anyValue.type() == typeid(std::string) )
00139 return std::atoi(any_cast<std::string>(anyValue).c_str());
00140 throwTypeError(entry,paramName,sublistName);
00141 return 0;
00142 }
00143
00144
00145 double AnyNumberParameterEntryValidator::getDouble(
00146 const ParameterEntry &entry, const std::string ¶mName
00147 ,const std::string &sublistName, const bool activeQuery
00148 ) const
00149 {
00150 const any &anyValue = entry.getAny(activeQuery);
00151 if( acceptedTypes_.allowInt() && anyValue.type() == typeid(int) )
00152 return static_cast<double>(any_cast<int>(anyValue));
00153 if( acceptedTypes_.allowDouble() && anyValue.type() == typeid(double) )
00154 return any_cast<double>(anyValue);
00155 if( acceptedTypes_.allowString() && anyValue.type() == typeid(std::string) )
00156 return std::atof(any_cast<std::string>(anyValue).c_str());
00157 throwTypeError(entry,paramName,sublistName);
00158 return 0.0;
00159 }
00160
00161
00162 std::string AnyNumberParameterEntryValidator::getString(
00163 const ParameterEntry &entry, const std::string ¶mName
00164 ,const std::string &sublistName, const bool activeQuery
00165 ) const
00166 {
00167 const any &anyValue = entry.getAny(activeQuery);
00168 if( acceptedTypes_.allowInt() && anyValue.type() == typeid(int) )
00169 return Utils::toString(any_cast<int>(anyValue));
00170 if( acceptedTypes_.allowDouble() && anyValue.type() == typeid(double) )
00171 return Utils::toString(any_cast<double>(anyValue));
00172 if( acceptedTypes_.allowString() && anyValue.type() == typeid(std::string) )
00173 return any_cast<std::string>(anyValue);
00174 throwTypeError(entry,paramName,sublistName);
00175 return "";
00176 }
00177
00178
00179 int AnyNumberParameterEntryValidator::getInt(
00180 ParameterList ¶mList, const std::string ¶mName
00181 ,const int defaultValue
00182 ) const
00183 {
00184 const ParameterEntry *entry = paramList.getEntryPtr(paramName);
00185 if(entry) return getInt(*entry,paramName,paramList.name(),true);
00186 return paramList.get(paramName,defaultValue);
00187 }
00188
00189
00190 double AnyNumberParameterEntryValidator::getDouble(
00191 ParameterList ¶mList, const std::string ¶mName
00192 ,const double defaultValue
00193 ) const
00194 {
00195 const ParameterEntry *entry = paramList.getEntryPtr(paramName);
00196 if(entry) return getDouble(*entry,paramName,paramList.name(),true);
00197 return paramList.get(paramName,defaultValue);
00198 }
00199
00200
00201 std::string AnyNumberParameterEntryValidator::getString(
00202 ParameterList ¶mList, const std::string ¶mName
00203 ,const std::string &defaultValue
00204 ) const
00205 {
00206 const ParameterEntry *entry = paramList.getEntryPtr(paramName);
00207 if(entry) return getString(*entry,paramName,paramList.name(),true);
00208 return paramList.get(paramName,defaultValue);
00209 }
00210
00211
00212
00213
00214
00215 void AnyNumberParameterEntryValidator::printDoc(
00216 std::string const& docString
00217 ,std::ostream & out
00218 ) const
00219 {
00220 StrUtils::printLines(out,"# ",docString);
00221 out << "# Accepted types: " << acceptedTypesString_ << ".\n";
00222 }
00223
00224
00225 RCP<const Array<std::string> >
00226 AnyNumberParameterEntryValidator::validStringValues() const
00227 {
00228 return null;
00229 }
00230
00231
00232 void AnyNumberParameterEntryValidator::validate(
00233 ParameterEntry const& entry
00234 ,std::string const& paramName
00235 ,std::string const& sublistName
00236 ) const
00237 {
00238
00239 getInt(entry,paramName,sublistName,false);
00240 }
00241
00242
00243 void AnyNumberParameterEntryValidator::validateAndModify(
00244 std::string const& paramName,
00245 std::string const& sublistName,
00246 ParameterEntry * entry
00247 ) const
00248 {
00249 TEST_FOR_EXCEPT(0==entry);
00250 switch(preferredType_) {
00251 case PREFER_INT:
00252 entry->setValue(
00253 getInt(*entry,paramName,sublistName,false),
00254 false
00255 );
00256 break;
00257 case PREFER_DOUBLE:
00258 entry->setValue(
00259 getDouble(*entry,paramName,sublistName,false),
00260 false
00261 );
00262 break;
00263 case PREFER_STRING:
00264 entry->setValue(
00265 getString(*entry,paramName,sublistName,false),
00266 false
00267 );
00268 break;
00269 default:
00270 TEST_FOR_EXCEPT("Error, Invalid EPreferredType value!");
00271 }
00272 }
00273
00274
00275
00276
00277
00278 void AnyNumberParameterEntryValidator::finishInitialization()
00279 {
00280
00281 std::ostringstream oss;
00282 bool addedType = false;
00283 if(acceptedTypes_.allowInt()) {
00284 oss << "\"int\"";
00285 addedType = true;
00286 }
00287 if(acceptedTypes_.allowDouble()) {
00288 if(addedType) oss << ", ";
00289 oss << "\"double\"";
00290 addedType = true;
00291 }
00292 if(acceptedTypes_.allowString()) {
00293 if(addedType) oss << ", ";
00294 oss << "\"string\"";
00295 addedType = true;
00296 }
00297 acceptedTypesString_ = oss.str();
00298 }
00299
00300
00301 void AnyNumberParameterEntryValidator::throwTypeError(
00302 ParameterEntry const& entry
00303 ,std::string const& paramName
00304 ,std::string const& sublistName
00305 ) const
00306 {
00307 const std::string &entryName = entry.getAny(false).typeName();
00308 TEST_FOR_EXCEPTION_PURE_MSG(
00309 true, Exceptions::InvalidParameterType
00310 ,"Error, the parameter {paramName=\""<<paramName<<"\""
00311 ",type=\""<<entryName<<"\"}"
00312 << "\nin the sublist \"" << sublistName << "\""
00313 << "\nhas the wrong type."
00314 << "\n\nThe accepted types are: " << acceptedTypesString_ << "!";
00315 );
00316 }
00317
00318
00319 }
00320
00321
00322
00323
00324 Teuchos::RCP<Teuchos::AnyNumberParameterEntryValidator>
00325 Teuchos::anyNumberParameterEntryValidator(
00326 AnyNumberParameterEntryValidator::EPreferredType const preferredType,
00327 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
00328 )
00329 {
00330 return rcp(
00331 new AnyNumberParameterEntryValidator(
00332 preferredType, acceptedTypes
00333 )
00334 );
00335 }
00336
00337
00338 void Teuchos::setIntParameter(
00339 std::string const& paramName,
00340 int const value, std::string const& docString,
00341 ParameterList *paramList,
00342 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
00343 )
00344 {
00345 TEST_FOR_EXCEPT(0==paramList);
00346 paramList->set(
00347 paramName,value,docString,
00348 anyNumberParameterEntryValidator(
00349 AnyNumberParameterEntryValidator::PREFER_INT, acceptedTypes
00350 )
00351 );
00352 }
00353
00354
00355 void Teuchos::setDoubleParameter(
00356 std::string const& paramName,
00357 double const& value, std::string const& docString,
00358 ParameterList *paramList,
00359 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
00360 )
00361 {
00362 TEST_FOR_EXCEPT(0==paramList);
00363 paramList->set(
00364 paramName,value,docString,
00365 anyNumberParameterEntryValidator(
00366 AnyNumberParameterEntryValidator::PREFER_DOUBLE, acceptedTypes
00367 )
00368 );
00369 }
00370
00371
00372 void Teuchos::setNumericStringParameter(
00373 std::string const& paramName,
00374 std::string const& value, std::string const& docString,
00375 ParameterList *paramList,
00376 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
00377 )
00378 {
00379 TEST_FOR_EXCEPT(0==paramList);
00380 paramList->set(
00381 paramName,value,docString,
00382 anyNumberParameterEntryValidator(
00383 AnyNumberParameterEntryValidator::PREFER_STRING, acceptedTypes
00384 )
00385 );
00386 }
00387
00388
00389 int Teuchos::getIntParameter(
00390 ParameterList const& paramList,
00391 std::string const& paramName
00392 )
00393 {
00394 const ParameterEntry &entry = paramList.getEntry(paramName);
00395 RCP<const AnyNumberParameterEntryValidator>
00396 anyNumValidator = rcp_dynamic_cast<const AnyNumberParameterEntryValidator>(
00397 entry.validator()
00398 );
00399 if ( !is_null(anyNumValidator) )
00400 return anyNumValidator->getInt(entry,paramName,paramList.name());
00401 if ( typeid(int) == entry.getAny().type() )
00402 return any_cast<int>(entry.getAny());
00403
00404 const AnyNumberParameterEntryValidator myAnyNumValidator;
00405 return myAnyNumValidator.getInt(entry,paramName,paramList.name());
00406 }
00407
00408
00409 double Teuchos::getDoubleParameter(
00410 ParameterList const& paramList,
00411 std::string const& paramName
00412 )
00413 {
00414 const ParameterEntry &entry = paramList.getEntry(paramName);
00415 RCP<const AnyNumberParameterEntryValidator>
00416 anyNumValidator = rcp_dynamic_cast<const AnyNumberParameterEntryValidator>(
00417 entry.validator()
00418 );
00419 if ( !is_null(anyNumValidator) )
00420 return anyNumValidator->getDouble(entry,paramName,paramList.name());
00421 if ( typeid(double) == entry.getAny().type() )
00422 return any_cast<double>(entry.getAny());
00423
00424 const AnyNumberParameterEntryValidator myAnyNumValidator;
00425 return myAnyNumValidator.getDouble(entry,paramName,paramList.name());
00426 }
00427
00428
00429 std::string Teuchos::getNumericStringParameter(
00430 ParameterList const& paramList,
00431 std::string const& paramName
00432 )
00433 {
00434 const ParameterEntry &entry = paramList.getEntry(paramName);
00435 RCP<const AnyNumberParameterEntryValidator>
00436 anyNumValidator = rcp_dynamic_cast<const AnyNumberParameterEntryValidator>(
00437 entry.validator()
00438 );
00439 if ( !is_null(anyNumValidator) )
00440 return anyNumValidator->getString(entry,paramName,paramList.name());
00441 if ( typeid(std::string) == entry.getAny().type() )
00442 return any_cast<std::string>(entry.getAny());
00443
00444 const AnyNumberParameterEntryValidator myAnyNumValidator;
00445 return myAnyNumValidator.getString(entry,paramName,paramList.name());
00446 }
00447