42 #include "Teuchos_StandardParameterEntryValidators.hpp"
46 std::string Teuchos::getVerbosityLevelParameterValueName(
65 true, std::invalid_argument,
"Teuchos::getVerbosityLevelParameterValue"
66 "Name(const Teuchos::EVerbosityLevel): Input argument " << verbLevel <<
67 " has an invalid value. Valid values are VERB_DEFAULT=" <<
VERB_DEFAULT
88 Teuchos::verbosityLevelParameterEntryValidator(
89 std::string
const& defaultParameterName
93 new StringToIntegralParameterEntryValidator<EVerbosityLevel>(
96 getVerbosityLevelParameterValueName(
VERB_NONE),
97 getVerbosityLevelParameterValueName(
VERB_LOW),
99 getVerbosityLevelParameterValueName(
VERB_HIGH),
103 "Use level set in code",
105 "Produce minimal output",
106 "Produce a little more output",
107 "Produce a higher level of output",
108 "Produce the highest level of output"
110 tuple<EVerbosityLevel>(
135 BoolParameterEntryValidator::BoolParameterEntryValidator()
137 finishInitialization();
146 const std::string &sublistName,
const bool activeQuery
149 const any &anyValue = entry.
getAny(activeQuery);
150 if( anyValue.
type() ==
typeid(bool) )
151 return any_cast<bool>(anyValue);
152 if( anyValue.
type() ==
typeid(std::string) ) {
153 std::string str = any_cast<std::string>(anyValue);
156 if( str ==
"false" ) {
159 else if( str ==
"true" ) {
164 throwTypeError(entry,paramName,sublistName);
170 const int defaultValue
174 if(entry)
return getBool(*entry,paramName,paramList.
name(),
true);
175 return paramList.
get(paramName,defaultValue);
182 return "boolValidator";
186 std::string
const & docString,
191 out <<
"# Accepted types: " << acceptedTypesString_ <<
".\n";
204 std::string
const& paramName,
205 std::string
const& sublistName
209 getBool(entry, paramName, sublistName,
false);
214 std::string
const& paramName,
215 std::string
const& sublistName,
227 void BoolParameterEntryValidator::finishInitialization()
229 std::ostringstream oss;
231 acceptedTypesString_ = oss.str();
233 acceptedTypesString_ = oss.str();
237 void BoolParameterEntryValidator::throwTypeError(
238 ParameterEntry
const& entry,
239 std::string
const& paramName,
240 std::string
const& sublistName
243 const std::string &entryName = entry.getAny(
false).typeName();
245 true, Exceptions::InvalidParameterType
246 ,
"Error, the parameter {paramName=\""<<paramName<<
"\""
247 ",type=\""<<entryName<<
"\"}"
248 <<
"\nin the sublist \"" << sublistName <<
"\""
249 <<
"\nhas the wrong type."
250 <<
"\n\nThe accepted types are: " << acceptedTypesString_ <<
"!";
263 : preferredType_(PREFER_DOUBLE), acceptedTypes_(
AcceptedTypes())
265 finishInitialization();
272 : preferredType_(preferredType), acceptedTypes_(acceptedTypes)
274 finishInitialization();
283 const std::string &sublistName,
const bool activeQuery
286 const any &anyValue = entry.
getAny(activeQuery);
287 if( acceptedTypes_.
allowInt() && anyValue.
type() ==
typeid(int) )
288 return any_cast<int>(anyValue);
290 return as<int>(any_cast<long long>(anyValue));
291 if( acceptedTypes_.
allowDouble() && anyValue.
type() ==
typeid(double) )
292 return as<int>(any_cast<double>(anyValue));
293 if( acceptedTypes_.
allowString() && anyValue.
type() ==
typeid(std::string) )
294 return convertStringToInt(any_cast<std::string>(anyValue));
295 throwTypeError(entry,paramName,sublistName);
301 const std::string &sublistName,
const bool activeQuery
304 const any &anyValue = entry.
getAny(activeQuery);
305 if( acceptedTypes_.
allowInt() && anyValue.
type() ==
typeid(int) )
306 return as<long long>(any_cast<int>(anyValue));
308 return any_cast<long long>(anyValue);
309 if( acceptedTypes_.
allowDouble() && anyValue.
type() ==
typeid(double) )
310 return as<int>(any_cast<double>(anyValue));
311 if( acceptedTypes_.
allowString() && anyValue.
type() ==
typeid(std::string) )
312 return convertStringToLongLong(any_cast<std::string>(anyValue));
313 throwTypeError(entry,paramName,sublistName);
319 const std::string &sublistName,
const bool activeQuery
322 const any &anyValue = entry.
getAny(activeQuery);
323 if( acceptedTypes_.
allowInt() && anyValue.
type() ==
typeid(int) )
324 return as<double>(any_cast<int>(anyValue));
326 return as<double>(any_cast<long long>(anyValue));
327 if( acceptedTypes_.
allowDouble() && anyValue.
type() ==
typeid(double) )
328 return any_cast<double>(anyValue);
329 if( acceptedTypes_.
allowString() && anyValue.
type() ==
typeid(std::string) )
330 return convertStringToDouble(any_cast<std::string>(anyValue));
331 throwTypeError(entry,paramName,sublistName);
338 const std::string &sublistName,
const bool activeQuery
341 const any &anyValue = entry.
getAny(activeQuery);
342 if( acceptedTypes_.
allowInt() && anyValue.
type() ==
typeid(int) )
346 if( acceptedTypes_.
allowDouble() && anyValue.
type() ==
typeid(double) )
348 if( acceptedTypes_.
allowString() && anyValue.
type() ==
typeid(std::string) )
349 return any_cast<std::string>(anyValue);
350 throwTypeError(entry,paramName,sublistName);
357 const int defaultValue
361 if(entry)
return getInt(*entry,paramName,paramList.
name(),
true);
362 return paramList.
get(paramName,defaultValue);
367 const long long defaultValue
372 return paramList.
get(paramName,defaultValue);
377 const double defaultValue
381 if(entry)
return getDouble(*entry,paramName,paramList.
name(),
true);
382 return paramList.
get(paramName,defaultValue);
388 const std::string &defaultValue
392 if(entry)
return getString(*entry,paramName,paramList.
name(),
true);
393 return paramList.
get(paramName,defaultValue);
420 return preferredType_;
429 return "anynumberValidator";
434 std::string
const & docString,
439 out <<
"# Accepted types: " << acceptedTypesString_ <<
".\n";
452 std::string
const& paramName,
453 std::string
const& sublistName
461 getDouble(entry, paramName, sublistName,
false);
466 std::string
const& paramName,
467 std::string
const& sublistName,
472 switch(preferredType_) {
475 getInt(*entry,paramName,sublistName,
false),
479 case PREFER_LONG_LONG:
487 getDouble(*entry,paramName,sublistName,
false),
493 getString(*entry,paramName,sublistName,
false),
506 void AnyNumberParameterEntryValidator::finishInitialization()
509 std::ostringstream oss;
510 bool addedType =
false;
516 oss <<
"\"long long\"";
520 if(addedType) oss <<
", ";
525 if(addedType) oss <<
", ";
529 acceptedTypesString_ = oss.str();
533 void AnyNumberParameterEntryValidator::throwTypeError(
534 ParameterEntry
const& entry,
535 std::string
const& paramName,
536 std::string
const& sublistName
539 const std::string &entryName = entry.getAny(
false).typeName();
541 true, Exceptions::InvalidParameterType
542 ,
"Error, the parameter {paramName=\""<<paramName<<
"\""
543 ",type=\""<<entryName<<
"\"}"
544 <<
"\nin the sublist \"" << sublistName <<
"\""
545 <<
"\nhas the wrong type."
546 <<
"\n\nThe accepted types are: " << acceptedTypesString_ <<
"!";
551 RCP<AnyNumberParameterEntryValidator>
554 return anyNumberParameterEntryValidator(
555 AnyNumberParameterEntryValidator::PREFER_INT,
568 return mustAlreadyExist_;
578 this->mustAlreadyExist_ = shouldFileExist;
579 return mustAlreadyExist_;
584 this->EmptyNameOK_ = isEmptyNameOK;
596 std::string
const &sublistName)
const
602 "The \"" << paramName <<
"\"" <<
603 " parameter in the \"" << sublistName <<
604 "\" sublist is has an error." << std::endl << std::endl <<
605 "Error: The value that you entered was the wrong type." << std::endl <<
606 "Parameter: " << paramName << std::endl <<
607 "Type specified: " << entryName << std::endl <<
608 "Type accepted: " <<
typeid(std::string).name() <<
609 std::endl << std::endl);
610 if(mustAlreadyExist_ && !EmptyNameOK_){
611 std::string fileName = getValue<std::string>(entry);
614 "The \"" << paramName <<
"\"" <<
615 " parameter in the \"" << sublistName <<
616 "\" sublist is has an error." << std::endl << std::endl <<
617 "Error: The file must already exists. The value you entered does " <<
618 "not corresspond to an existing file name." << std::endl <<
619 "Parameter: " << paramName << std::endl <<
620 "File name specified: " << fileName << std::endl << std::endl);
627 return "FilenameValidator";
632 std::string
const &docString, std::ostream &out)
const
635 out <<
"# Validator Used: " << std::endl;
636 out <<
"# FileName Validator" << std::endl;
652 validStrings_(
rcp(new
Array<std::string>(validStrings)))
660 return validStrings_;
667 return validStrings_;
673 std::string
const &sublistName)
const
679 "The \"" << paramName <<
"\"" <<
680 " parameter in the \"" << sublistName <<
681 "\" sublist is has an error." << std::endl << std::endl <<
682 "Error: The value that you entered was the wrong type." <<
683 "Parameter: " << paramName << std::endl <<
684 "Type specified: " << entryName << std::endl <<
689 it = std::find(validStrings_->begin(),
690 validStrings_->end(), getValue<std::string>(entry));
693 "The \"" << paramName <<
"\"" <<
694 " parameter in the \"" << sublistName <<
695 "\" sublist is has an error." << std::endl << std::endl <<
696 "Error: The value that was entered doesn't fall with in "
697 "the range set by the validator." <<
698 "Parameter: " << paramName << std::endl <<
699 "Acceptable Values: " << *validStrings_ << std::endl <<
700 "Value entered: " << getValue<std::string>(entry) << std::endl <<
708 return "StringValidator";
713 std::ostream &out)
const
716 out <<
"# Validator Used: " << std::endl;
717 out <<
"# String Validator" << std::endl;
718 if (validStrings_.
get() && validStrings_->size()){
719 out <<
"# Acceptable Values: " << *validStrings_ << std::endl;
735 Teuchos::boolParameterEntryValidator()
737 return rcp(
new BoolParameterEntryValidator());
741 Teuchos::anyNumberParameterEntryValidator()
743 return rcp(
new AnyNumberParameterEntryValidator());
748 Teuchos::anyNumberParameterEntryValidator(
749 AnyNumberParameterEntryValidator::EPreferredType
const preferredType,
750 AnyNumberParameterEntryValidator::AcceptedTypes
const& acceptedTypes
754 new AnyNumberParameterEntryValidator(
755 preferredType, acceptedTypes
760 void Teuchos::setIntParameter(
761 std::string
const& paramName,
762 int const value, std::string
const& docString,
763 ParameterList *paramList,
764 AnyNumberParameterEntryValidator::AcceptedTypes
const& acceptedTypes
768 const RCP<const ParameterEntryValidator> paramEntryValidator =
770 AnyNumberParameterEntryValidator::PREFER_INT, acceptedTypes
772 paramList->set(paramName, value, docString, paramEntryValidator);
776 void Teuchos::setDoubleParameter(
777 std::string
const& paramName,
778 double const& value, std::string
const& docString,
779 ParameterList *paramList,
780 AnyNumberParameterEntryValidator::AcceptedTypes
const& acceptedTypes
784 const RCP<const ParameterEntryValidator> paramEntryValidator =
786 AnyNumberParameterEntryValidator::PREFER_DOUBLE, acceptedTypes
788 paramList->set(paramName, value, docString, paramEntryValidator);
792 void Teuchos::setNumericStringParameter(
793 std::string
const& paramName,
794 std::string
const& value, std::string
const& docString,
795 ParameterList *paramList,
796 AnyNumberParameterEntryValidator::AcceptedTypes
const& acceptedTypes
800 const RCP<const ParameterEntryValidator> paramEntryValidator =
802 AnyNumberParameterEntryValidator::PREFER_STRING, acceptedTypes
804 paramList->set(paramName, value, docString, paramEntryValidator);
808 int Teuchos::getIntParameter(
809 ParameterList
const& paramList,
810 std::string
const& paramName
813 const ParameterEntry &entry = paramList.getEntry(paramName);
814 RCP<const AnyNumberParameterEntryValidator>
815 anyNumValidator = rcp_dynamic_cast<const AnyNumberParameterEntryValidator>(
818 if ( !
is_null(anyNumValidator) )
819 return anyNumValidator->getInt(entry,paramName,paramList.name());
820 if (
typeid(
int) == entry.getAny().type() )
821 return any_cast<int>(entry.getAny());
823 const AnyNumberParameterEntryValidator myAnyNumValidator;
824 return myAnyNumValidator.getInt(entry,paramName,paramList.name());
828 double Teuchos::getDoubleParameter(
829 ParameterList
const& paramList,
830 std::string
const& paramName
833 const ParameterEntry &entry = paramList.getEntry(paramName);
834 RCP<const AnyNumberParameterEntryValidator>
835 anyNumValidator = rcp_dynamic_cast<const AnyNumberParameterEntryValidator>(
838 if ( !
is_null(anyNumValidator) )
839 return anyNumValidator->getDouble(entry,paramName,paramList.name());
840 if (
typeid(
double) == entry.getAny().type() )
841 return any_cast<double>(entry.getAny());
843 const AnyNumberParameterEntryValidator myAnyNumValidator;
844 return myAnyNumValidator.getDouble(entry,paramName,paramList.name());
848 std::string Teuchos::getNumericStringParameter(
849 ParameterList
const& paramList,
850 std::string
const& paramName
853 const ParameterEntry &entry = paramList.getEntry(paramName);
854 RCP<const AnyNumberParameterEntryValidator>
855 anyNumValidator = rcp_dynamic_cast<const AnyNumberParameterEntryValidator>(
858 if ( !
is_null(anyNumValidator) )
859 return anyNumValidator->getString(entry,paramName,paramList.name());
860 if (
typeid(std::string) == entry.getAny().type() )
861 return any_cast<std::string>(entry.getAny());
863 const AnyNumberParameterEntryValidator myAnyNumValidator;
864 return myAnyNumValidator.getString(entry,paramName,paramList.name());