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);
289 if( acceptedTypes_.
allowDouble() && anyValue.
type() ==
typeid(double) )
290 return as<int>(any_cast<
double>(anyValue));
291 if( acceptedTypes_.
allowString() && anyValue.
type() ==
typeid(std::string) )
292 return convertStringToInt(any_cast<std::string>(anyValue));
293 throwTypeError(entry,paramName,sublistName);
299 const std::string &sublistName,
const bool activeQuery
302 const any &anyValue = entry.
getAny(activeQuery);
303 if( acceptedTypes_.
allowInt() && anyValue.
type() ==
typeid(int) )
304 return as<double>(any_cast<
int>(anyValue));
305 if( acceptedTypes_.
allowDouble() && anyValue.
type() ==
typeid(double) )
306 return any_cast<
double>(anyValue);
307 if( acceptedTypes_.
allowString() && anyValue.
type() ==
typeid(std::string) )
308 return convertStringToDouble(any_cast<std::string>(anyValue));
309 throwTypeError(entry,paramName,sublistName);
316 const std::string &sublistName,
const bool activeQuery
319 const any &anyValue = entry.
getAny(activeQuery);
320 if( acceptedTypes_.
allowInt() && anyValue.
type() ==
typeid(int) )
322 if( acceptedTypes_.
allowDouble() && anyValue.
type() ==
typeid(double) )
324 if( acceptedTypes_.
allowString() && anyValue.
type() ==
typeid(std::string) )
325 return any_cast<std::string>(anyValue);
326 throwTypeError(entry,paramName,sublistName);
333 const int defaultValue
337 if(entry)
return getInt(*entry,paramName,paramList.
name(),
true);
338 return paramList.
get(paramName,defaultValue);
344 const double defaultValue
348 if(entry)
return getDouble(*entry,paramName,paramList.
name(),
true);
349 return paramList.
get(paramName,defaultValue);
355 const std::string &defaultValue
359 if(entry)
return getString(*entry,paramName,paramList.
name(),
true);
360 return paramList.
get(paramName,defaultValue);
385 return preferredType_;
394 return "anynumberValidator";
399 std::string
const & docString,
404 out <<
"# Accepted types: " << acceptedTypesString_ <<
".\n";
417 std::string
const& paramName,
418 std::string
const& sublistName
426 getDouble(entry, paramName, sublistName,
false);
431 std::string
const& paramName,
432 std::string
const& sublistName,
437 switch(preferredType_) {
440 getInt(*entry,paramName,sublistName,
false),
446 getDouble(*entry,paramName,sublistName,
false),
452 getString(*entry,paramName,sublistName,
false),
465 void AnyNumberParameterEntryValidator::finishInitialization()
468 std::ostringstream oss;
469 bool addedType =
false;
475 if(addedType) oss <<
", ";
480 if(addedType) oss <<
", ";
484 acceptedTypesString_ = oss.str();
488 void AnyNumberParameterEntryValidator::throwTypeError(
489 ParameterEntry
const& entry,
490 std::string
const& paramName,
491 std::string
const& sublistName
494 const std::string &entryName = entry.getAny(
false).typeName();
496 true, Exceptions::InvalidParameterType
497 ,
"Error, the parameter {paramName=\""<<paramName<<
"\"" 498 ",type=\""<<entryName<<
"\"}" 499 <<
"\nin the sublist \"" << sublistName <<
"\"" 500 <<
"\nhas the wrong type." 501 <<
"\n\nThe accepted types are: " << acceptedTypesString_ <<
"!";
506 RCP<AnyNumberParameterEntryValidator>
509 return anyNumberParameterEntryValidator(
510 AnyNumberParameterEntryValidator::PREFER_INT,
523 return mustAlreadyExist_;
533 this->mustAlreadyExist_ = shouldFileExist;
534 return mustAlreadyExist_;
539 this->EmptyNameOK_ = isEmptyNameOK;
551 std::string
const &sublistName)
const 557 "The \"" << paramName <<
"\"" <<
558 " parameter in the \"" << sublistName <<
559 "\" sublist is has an error." << std::endl << std::endl <<
560 "Error: The value that you entered was the wrong type." << std::endl <<
561 "Parameter: " << paramName << std::endl <<
562 "Type specified: " << entryName << std::endl <<
563 "Type accepted: " <<
typeid(std::string).name() <<
564 std::endl << std::endl);
565 if(mustAlreadyExist_ && !EmptyNameOK_){
566 std::string fileName = getValue<std::string>(entry);
569 "The \"" << paramName <<
"\"" <<
570 " parameter in the \"" << sublistName <<
571 "\" sublist is has an error." << std::endl << std::endl <<
572 "Error: The file must already exists. The value you entered does " <<
573 "not corresspond to an existing file name." << std::endl <<
574 "Parameter: " << paramName << std::endl <<
575 "File name specified: " << fileName << std::endl << std::endl);
582 return "FilenameValidator";
587 std::string
const &docString, std::ostream &out)
const 590 out <<
"# Validator Used: " << std::endl;
591 out <<
"# FileName Validator" << std::endl;
607 validStrings_(
rcp(new
Array<std::string>(validStrings)))
615 return validStrings_;
622 return validStrings_;
628 std::string
const &sublistName)
const 634 "The \"" << paramName <<
"\"" <<
635 " parameter in the \"" << sublistName <<
636 "\" sublist is has an error." << std::endl << std::endl <<
637 "Error: The value that you entered was the wrong type." <<
638 "Parameter: " << paramName << std::endl <<
639 "Type specified: " << entryName << std::endl <<
644 it = std::find(validStrings_->begin(),
645 validStrings_->end(), getValue<std::string>(entry));
648 "The \"" << paramName <<
"\"" <<
649 " parameter in the \"" << sublistName <<
650 "\" sublist is has an error." << std::endl << std::endl <<
651 "Error: The value that was entered doesn't fall with in " 652 "the range set by the validator." <<
653 "Parameter: " << paramName << std::endl <<
654 "Acceptable Values: " << *validStrings_ << std::endl <<
655 "Value entered: " << getValue<std::string>(entry) << std::endl <<
663 return "StringValidator";
668 std::ostream &out)
const 671 out <<
"# Validator Used: " << std::endl;
672 out <<
"# String Validator" << std::endl;
673 if (validStrings_.
get() && validStrings_->size()){
674 out <<
"# Acceptable Values: " << *validStrings_ << std::endl;
690 Teuchos::boolParameterEntryValidator()
692 return rcp(
new BoolParameterEntryValidator());
696 Teuchos::anyNumberParameterEntryValidator()
698 return rcp(
new AnyNumberParameterEntryValidator());
703 Teuchos::anyNumberParameterEntryValidator(
704 AnyNumberParameterEntryValidator::EPreferredType
const preferredType,
705 AnyNumberParameterEntryValidator::AcceptedTypes
const& acceptedTypes
709 new AnyNumberParameterEntryValidator(
710 preferredType, acceptedTypes
715 void Teuchos::setIntParameter(
716 std::string
const& paramName,
717 int const value, std::string
const& docString,
718 ParameterList *paramList,
719 AnyNumberParameterEntryValidator::AcceptedTypes
const& acceptedTypes
723 const RCP<const ParameterEntryValidator> paramEntryValidator =
725 AnyNumberParameterEntryValidator::PREFER_INT, acceptedTypes
727 paramList->set(paramName, value, docString, paramEntryValidator);
731 void Teuchos::setDoubleParameter(
732 std::string
const& paramName,
733 double const& value, std::string
const& docString,
734 ParameterList *paramList,
735 AnyNumberParameterEntryValidator::AcceptedTypes
const& acceptedTypes
739 const RCP<const ParameterEntryValidator> paramEntryValidator =
741 AnyNumberParameterEntryValidator::PREFER_DOUBLE, acceptedTypes
743 paramList->set(paramName, value, docString, paramEntryValidator);
747 void Teuchos::setNumericStringParameter(
748 std::string
const& paramName,
749 std::string
const& value, std::string
const& docString,
750 ParameterList *paramList,
751 AnyNumberParameterEntryValidator::AcceptedTypes
const& acceptedTypes
755 const RCP<const ParameterEntryValidator> paramEntryValidator =
757 AnyNumberParameterEntryValidator::PREFER_STRING, acceptedTypes
759 paramList->set(paramName, value, docString, paramEntryValidator);
763 int Teuchos::getIntParameter(
764 ParameterList
const& paramList,
765 std::string
const& paramName
768 const ParameterEntry &entry = paramList.getEntry(paramName);
769 RCP<const AnyNumberParameterEntryValidator>
773 if ( !
is_null(anyNumValidator) )
774 return anyNumValidator->getInt(entry,paramName,paramList.name());
775 if (
typeid(
int) == entry.getAny().type() )
776 return any_cast<int>(entry.getAny());
778 const AnyNumberParameterEntryValidator myAnyNumValidator;
779 return myAnyNumValidator.getInt(entry,paramName,paramList.name());
783 double Teuchos::getDoubleParameter(
784 ParameterList
const& paramList,
785 std::string
const& paramName
788 const ParameterEntry &entry = paramList.getEntry(paramName);
789 RCP<const AnyNumberParameterEntryValidator>
793 if ( !
is_null(anyNumValidator) )
794 return anyNumValidator->getDouble(entry,paramName,paramList.name());
795 if (
typeid(
double) == entry.getAny().type() )
796 return any_cast<double>(entry.getAny());
798 const AnyNumberParameterEntryValidator myAnyNumValidator;
799 return myAnyNumValidator.getDouble(entry,paramName,paramList.name());
803 std::string Teuchos::getNumericStringParameter(
804 ParameterList
const& paramList,
805 std::string
const& paramName
808 const ParameterEntry &entry = paramList.getEntry(paramName);
809 RCP<const AnyNumberParameterEntryValidator>
813 if ( !
is_null(anyNumValidator) )
814 return anyNumValidator->getString(entry,paramName,paramList.name());
815 if (
typeid(std::string) == entry.getAny().type() )
816 return any_cast<std::string>(entry.getAny());
818 const AnyNumberParameterEntryValidator myAnyNumValidator;
819 return myAnyNumValidator.getString(entry,paramName,paramList.name());
ValidStringsList setValidStrings(const Teuchos::Array< std::string > &validStrings)
Sets the Array of valid strings and returns what the current array of valid string now is...
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object...
void printDoc(std::string const &docString, std::ostream &out) const
Generate output as defined by the object.
static RCP< T > getDummyObject()
Retrieves a dummy object of type T.
bool setFileMustExist(bool shouldFileExist)
Sets whether or not the validator requires the file to already exist.
void setValue(T value, bool isDefault=false, const std::string &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Templated set method that uses the input value type to determine the type of parameter.
FileNameValidator(bool mustAlreadyExist=mustAlreadyExistDefault())
Constructs a FileNameValidator.
const std::string getXMLTypeName() const
Generate only a minimal amount of output.
RCP< T2 > rcp_dynamic_cast(const RCP< T1 > &p1, bool throw_on_fail=false)
Dynamic cast of underlying RCP type from T1* to T2*.
void printDoc(std::string const &docString, std::ostream &out) const
const std::string getXMLTypeName() const
EPreferredType
Determines what type is the preferred type.
T & get(const std::string &name, T def_value)
Return the parameter's value, or the default value if it is not there.
This object is held as the "value" in the Teuchos::ParameterList std::map.
void validate(ParameterEntry const &entry, std::string const ¶mName, std::string const &sublistName) const
bool isStringAllowed() const
Lookup whether or not strings are allowed.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Generate the most output possible.
EPreferredType getPreferredType() const
Lookup the preferred type.
Standard implementation of a ParameterEntryValidator that maps from a list of strings to an enum or i...
AcceptedTypes & allowString(bool _allowString)
Set allow an std::string value or not.
static std::ostream & printLines(std::ostream &os, const std::string &linePrefix, const std::string &lines)
Print lines with prefix first.
EVerbosityLevel
Verbosity level.
int getInt(const ParameterEntry &entry, const std::string ¶mName="", const std::string &sublistName="", const bool activeQuery=true) const
Get an integer value from a parameter entry. HAVE_TEUCHOSCORE_CXX11 will call std::stoi otherwise we ...
Modified boost::any class, which is a container for a templated value.
bool is_null(const ArrayRCP< T > &p)
Returns true if p.get()==NULL.
AcceptedTypes & allowInt(bool _allowInt)
Set allow an int value or not.
static std::string toString(const double &x)
Write a double as a std::string.
void validate(ParameterEntry const &entry, std::string const ¶mName, std::string const &sublistName) const
T * get() const
Get the raw C++ pointer to the underlying object.
bool is_null() const
Returns true if the underlying pointer is null.
double getDouble(const ParameterEntry &entry, const std::string ¶mName="", const std::string &sublistName="", const bool activeQuery=true) const
Get a double value from a parameter entry.
ParameterEntry * getEntryPtr(const std::string &name)
Retrieves the pointer for an entry with the name name if it exists.
void validate(ParameterEntry const &entry, std::string const ¶mName, std::string const &sublistName) const
Determines the types that are accepted.
ValidStringsList validStringValues() const
bool isIntAllowed() const
Lookup whether or not ints are allowed.
std::string getString(const ParameterEntry &entry, const std::string ¶mName="", const std::string &sublistName="", const bool activeQuery=true) const
Get a std::string value from a parameter entry.
bool fileMustExist() const
Gets the variable describing whether or not this validator wants the file that is specified to alread...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Generate a high level of output.
bool getBool(const ParameterEntry &entry, const std::string ¶mName="", const std::string &sublistName="", const bool activeQuery=true) const
Get bool value from a parameter entry.
bool fileEmptyNameOK() const
Gets the variable describing whether or not this validator is OK with file name being empty (even if ...
void printDoc(std::string const &docString, std::ostream &out) const
ValidStringsList validStringValues() const
bool setFileEmptyNameOK(bool isEmptyNameOK)
Sets whether or not the validator is OK with empty file name (even if fileMustExist() returns true) ...
Validate a file name entry.
ValidStringsList validStringValues() const
A list of parameters of arbitrary type.
StringValidator()
Constructs a StringValidator.
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT RCP< AnyNumberParameterEntryValidator > anyNumberParameterEntryValidator()
Nonmember constructor AnyNumberParameterEntryValidator.
AnyNumberParameterEntryValidator()
Construct with a preferrded type of double and accept all types.
A simple validator that only allows certain string values to be choosen or simply enforces that a par...
const std::type_info & type() const
Return the type of value being stored.
Abstract interface for an object that can validate a ParameterEntry's value.
const std::string & name() const
The name of this ParameterList.
any & getAny(bool activeQry=true)
Direct access to the Teuchos::any data value underlying this object. The bool argument activeQry (def...
void validateAndModify(std::string const ¶mName, std::string const &sublistName, ParameterEntry *entry) const
void printDoc(std::string const &docString, std::ostream &out) const
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
std::string typeName() const
Return the name of the type.
AcceptedTypes & allowDouble(bool _allowDouble)
Set allow a double value or not.
Smart reference counting pointer class for automatic garbage collection.
bool isDoubleAllowed() const
Lookup whether or not Doubles are allowed.
ValidStringsList validStringValues() const
const std::string getXMLTypeName() const
void validateAndModify(std::string const ¶mName, std::string const &sublistName, ParameterEntry *entry) const
Definition of Teuchos::as, for conversions between types.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call...
static std::string name()
#define TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
void validate(ParameterEntry const &entry, std::string const ¶mName, std::string const &sublistName) const
const std::string getXMLTypeName() const