45 #include "Teuchos_FancyOStream.hpp" 47 #include "Teuchos_VerboseObject.hpp" 55 return ( entry.
isList() ? std::string(
"...") :
toString(entry.getAny()) );
59 struct ListPlusValidList {
66 :list(_list),validList(_validList)
81 :name_(
"ANONYMOUS"), disableRecursiveValidation_(false)
86 :name_(name_in), disableRecursiveValidation_(false)
93 params_ = source.params_;
94 disableRecursiveValidation_ = source.disableRecursiveValidation_;
112 name_ = source.name_;
113 params_ = source.params_;
114 disableRecursiveValidation_ = source.disableRecursiveValidation_;
122 const std::string &name_i = this->
name(i);
126 getValue<ParameterList>(entry_i) );
132 this->updateSubListNames();
142 const std::string &name_i = this->
name(i);
146 getValue<ParameterList>(entry_i) );
157 this->updateSubListNames();
164 disableRecursiveValidation_ =
true;
172 if (!(
entry(i).isUsed())) {
173 os <<
"WARNING: Parameter \"" <<
name(i) <<
"\" " <<
entry(i)
174 <<
" is unused" << std::endl;
182 std::ostringstream oss;
186 for( itr = this->
begin(), i = 0; itr != this->
end(); ++itr, ++i ) {
187 const std::string &entryName = this->
name(itr);
191 <<
" = "<<filterValueToString(theEntry) <<
"\n";
202 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
203 return params_.
getObjPtr(param_idx)->isList();
213 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
221 std::string
const& name_in,
bool throwIfNotExists
226 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
232 if (throwIfNotExists) {
233 validateEntryExists(
"get", name_in, 0);
240 const std::string& name_in,
bool mustAlreadyExist,
241 const std::string& docString
250 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
253 validateEntryIsList(name_in, *sublist_entry_ptr);
257 validateMissingSublistMustExist(this->
name(), name_in, mustAlreadyExist);
258 const Ordinal new_param_idx =
271 return any_cast<
ParameterList>(sublist_entry_ptr->getAny(
false));
280 if (param_idx == SIOVOCB::getInvalidOrdinal()) {
281 validateMissingSublistMustExist(this->
name(), name_in,
true);
285 validateEntryIsList(name_in, *sublist_entry_ptr);
287 return any_cast<
ParameterList>(sublist_entry_ptr->getAny(
false));
299 const int indent = printOptions.indent();
300 const bool showTypes = printOptions.showTypes();
301 const bool showFlags = printOptions.showFlags();
302 const bool showDoc = printOptions.showDoc();
303 const std::string linePrefix(indent,
' ');
305 out = getFancyOStream(
rcp(&os,
false));
306 OSTab tab(out,indent);
308 *out <<
"[empty list]" << std::endl;
314 const std::string &name_i = this->
name(i);
321 const std::string &docString = entry_i.
docString();
324 *out <<
" = "; entry_i.
leftshift(os,showFlags); *out << std::endl;
327 validator->printDoc(docString,
OSTab(os).o());
329 else if (docString.length()) {
340 const std::string &docString = entry_i.
docString();
341 const std::string &name_i = this->
name(i);
342 *out << name_i <<
" -> " << std::endl;
343 if( docString.length() && showDoc ) {
346 getValue<ParameterList>(entry_i).
print(
OSTab(out).o(), printOptions.copy().indent(0));
355 return this->
print(os,
PrintOptions().indent(indent).showTypes(showTypes).showFlags(showFlags));
366 typedef std::deque<ListPlusValidList> sublist_list_t;
367 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE 370 *out <<
"\n*** Entering ParameterList::validateParameters(...) for " 371 "this->name()=\""<<this->
name()<<
"\"...\n";
378 sublist_list_t sublist_list;
380 for (itr = this->
begin(); itr != this->
end(); ++itr) {
381 const std::string &entryName = this->
name(itr);
383 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE 385 *out <<
"\nentryName=\""<<entryName<<
"\"\n";
398 ,
"Error, the parameter {name=\""<<entryName<<
"\"," 400 ",value=\""<<filterValueToString(theEntry)<<
"\"}" 401 "\nin the parameter (sub)list \""<<this->
name()<<
"\"" 402 "\nwas not found in the list of valid parameters!" 403 "\n\nThe valid parameters and types are:\n" 408 validator->validate(theEntry, entryName, this->
name());
411 const bool validType =
418 ,
"Error, the parameter {name=\""<<entryName<<
"\"," 420 ",value=\""<<filterValueToString(theEntry)<<
"\"}" 421 "\nin the parameter (sub)list \""<<this->
name()<<
"\"" 422 "\nexists in the list of valid parameters but has the wrong type." 423 "\n\nThe correct type is \"" 427 if( theEntry.
isList() && depth > 0 ) {
428 sublist_list.push_back(
430 &getValue<ParameterList>(theEntry),&getValue<ParameterList>(*validEntry)
439 sublist_list_t::const_iterator sl_itr = sublist_list.begin();
440 sl_itr != sublist_list.end();
444 if (!sl_itr->validList->disableRecursiveValidation_) {
445 sl_itr->list->validateParameters(
453 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE 454 *out <<
"\n*** Existing ParameterList::validateParameters(...) for " 455 "this->name()=\""<<this->
name()<<
"\"...\n";
465 typedef std::deque<ListPlusValidList> sublist_list_t;
466 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE 469 *out <<
"\n*** Entering ParameterList::validateParametersAndSetDefaults(...) " 470 "for this->name()=\""<<this->
name()<<
"\"...\n";
477 sublist_list_t sublist_list;
480 for (itr = this->nonconstBegin(); itr != this->nonconstEnd(); ++itr) {
481 const std::string &entryName = this->
name(itr);
483 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE 485 *out <<
"\nentryName=\""<<entryName<<
"\"\n";
490 ,
"Error, the parameter {name=\""<<entryName<<
"\"," 492 ",value=\""<<filterValueToString(theEntry)<<
"\"}" 493 "\nin the parameter (sub)list \""<<this->
name()<<
"\"" 494 "\nwas not found in the list of valid parameters!" 495 "\n\nThe valid parameters and types are:\n" 500 validator->validateAndModify(entryName, this->
name(), &theEntry);
504 const bool validType =
511 ,
"Error, the parameter {name=\""<<entryName<<
"\"," 513 ",value=\""<<filterValueToString(theEntry)<<
"\"}" 514 "\nin the parameter (sub)list \""<<this->
name()<<
"\"" 515 "\nexists in the list of valid parameters but has the wrong type." 516 "\n\nThe correct type is \"" 522 if( theEntry.
isList() && depth > 0 ) {
523 sublist_list.push_back(
525 &getValue<ParameterList>(theEntry),
526 &getValue<ParameterList>(*validEntry)
538 for (itr = validParamList.
begin(); itr != validParamList.
end(); ++itr) {
539 const std::string &validEntryName = validParamList.
name(itr);
554 this->
setEntry(validEntryName,newEntry);
563 sublist_list_t::iterator sl_itr = sublist_list.begin();
564 sl_itr != sublist_list.end();
568 if (!sl_itr->validList->disableRecursiveValidation_) {
569 sl_itr->list->validateParametersAndSetDefaults(*sl_itr->validList,depth-1);
572 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE 573 *out <<
"\n*** Existing ParameterList::validateParametersAndSetDefaults(...) " 574 "for this->name()=\""<<this->
name()<<
"\"...\n";
582 void ParameterList::updateSubListNames(
int depth)
584 const std::string this_name = this->
name();
586 for( itr = this->nonconstBegin(); itr != this->nonconstEnd(); ++itr ) {
587 const std::string &entryName = this->
name(itr);
590 ParameterList &sublistEntry = getValue<ParameterList>(theEntry);
591 sublistEntry.
setName(this_name+std::string(
"->")+entryName);
593 sublistEntry.updateSubListNames(depth-1);
599 void ParameterList::validateEntryExists(
600 const std::string & ,
const std::string &name_in,
601 const ParameterEntry *entry_in
605 entry_in==NULL, Exceptions::InvalidParameterName
606 ,
"Error! The parameter \""<<name_in<<
"\" does not exist"\
607 "\nin the parameter (sub)list \""<<this->
name()<<
"\"." 608 "\n\nThe current parameters set in (sub)list \""<<this->
name()<<
"\" are:\n\n" 614 void ParameterList::validateEntryIsList(
615 const std::string &name_in,
const ParameterEntry &entry_in
619 !entry_in.isList(), Exceptions::InvalidParameterType
620 ,
"Error, the parameter \"" << name_in <<
"\" is not a list, it is of type \"" 621 <<entry_in.getAny(
false).typeName()<<
"\"!" );
625 void ParameterList::validateMissingSublistMustExist(
const std::string &baselist_name,
626 const std::string &sublist_name,
const bool mustAlreadyExist)
const 629 mustAlreadyExist, Exceptions::InvalidParameterName
630 ,
"The sublist "<<baselist_name<<
"->\""<<sublist_name<<
"\" does not exist!" 639 bool Teuchos::operator==(
const ParameterList& list1,
const ParameterList& list2 )
647 ParameterList::ConstIterator itr1, itr2;
649 itr1 = list1.begin(), itr2 = list2.begin();
650 itr1 != list1.end() && itr2 != list2.end();
654 const std::string &entryName1 = list1.name(itr1);
655 const std::string &entryName2 = list2.name(itr2);
656 const ParameterEntry &entry1 = list1.entry(itr1);
657 const ParameterEntry &entry2 = list2.entry(itr2);
658 if( entryName1 != entryName2 ) {
661 else if( entry1 != entry2 ) {
670 if ((itr1 != list1.end()) || (itr2 != list2.end())) {
677 bool Teuchos::haveSameValues(
const ParameterList& list1,
const ParameterList& list2 )
685 ParameterList::ConstIterator itr1, itr2;
687 itr1 = list1.begin(), itr2 = list2.begin();
688 itr1 != list1.end() && itr2 != list2.end();
692 const std::string &entryName1 = list1.name(itr1);
693 const std::string &entryName2 = list2.name(itr2);
694 const ParameterEntry &entry1 = list1.entry(itr1);
695 const ParameterEntry &entry2 = list2.entry(itr2);
696 if( entryName1 != entryName2 ) {
699 if( entry1.isList() && entry2.isList() ) {
702 getValue<ParameterList>(entry1),
703 getValue<ParameterList>(entry2))
714 if( entry1.getAny() != entry2.getAny() ) {
720 if ((itr1 != list1.end()) || (itr2 != list2.end())) {
C++ Standard Library compatable filtered iterator.
std::string currentParametersString() const
Create a single formated std::string of all of the zero-level parameters in this list.
Ordinal getObjOrdinalIndex(const std::string &key) const
Get the ordinal index given the string key.
Ptr< const ObjType > getObjPtr(const Ordinal &idx) const
Get a const semi-persisting association with the stored object indexed by ordinal.
basic_OSTab< char > OSTab
ParameterList & disableRecursiveValidation()
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT bool haveSameValues(const ParameterList &list1, const ParameterList &list2)
Returns true if two parameter lists have the same values.
void setValidator(RCP< const ParameterEntryValidator > const &validator)
Set the validator.
ConstIterator begin() const
An iterator pointing to the first entry.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
This object is held as the "value" in the Teuchos::ParameterList std::map.
const ParameterEntry & entry(ConstIterator i) const
Access to ParameterEntry (i.e., returns i->second)
bool isSublist(const std::string &name) const
Whether the given sublist exists in this list.
Ordinal numParams() const
Get the number of stored parameters.
void setAnyValue(const any &value, bool isDefault=false)
Set the value as an any object.
static std::ostream & printLines(std::ostream &os, const std::string &linePrefix, const std::string &lines)
Print lines with prefix first.
Tabbing class for helping to create formated, indented output for a basic_FancyOStream object...
void unused(std::ostream &os) const
Print out unused parameters in the ParameterList.
ParameterEntry * getEntryPtr(const std::string &name)
Retrieves the pointer for an entry with the name name if it exists.
std::string toString(const any &rhs)
Converts the value in any to a std::string.
A std::string utilities class for Teuchos.
ParameterList & setEntry(const std::string &name, const ParameterEntry &entry)
Set a parameter directly as a ParameterEntry.
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
Validate the parameters in this list given valid selections in the input list.
ConstIterator end() const
An iterator pointing beyond the last entry.
void print() const
Print function to use in debugging in a debugger.
bool remove(std::string const &name, bool throwIfNotExists=true)
Remove a parameter (does not depend on the type of the parameter).
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Templated Parameter List class.
ParameterList & operator=(const ParameterList &source)
Replace the current parameter list with source.
static RCP< FancyOStream > getDefaultOStream()
Get the default output stream object.
std::ostream & leftshift(std::ostream &os, bool printFlags=true) const
Output a non-list parameter to the given output stream.
bool isDefault() const
Indicate whether this entry takes on the default value.
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
Validate the parameters in this list given valid selections in the input list and set defaults for th...
Utility class for setting and passing in print options.
bool isList() const
Return whether or not the value itself is a list.
Ordinal numObjects() const
RCP< const ParameterEntryValidator > validator() const
Return the (optional) validator object.
A list of parameters of arbitrary type.
ParameterList & setParameters(const ParameterList &source)
const std::type_info & type() const
Return the type of value being stored.
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...
Ordinal setObj(const std::string &key, const ObjType &obj)
Set (or reset) object by value and return its ordinal index.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
ParameterList & setParametersNotAlreadySet(const ParameterList &source)
std::string typeName() const
Return the name of the type.
ParameterList()
Constructor.
bool isUsed() const
Return whether or not the value has been used; i.e., whether or not the value has been retrieved via ...
EValidateDefaults
Validation defaults enum.
bool isParameter(const std::string &name) const
Whether the given parameter exists in this list.
std::string docString() const
Return the (optional) documentation std::string.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Creates an empty sublist and returns a reference to the sublist name. If the list already exists...
ParameterList & setName(const std::string &name)
Set the name of *this list.
Smart reference counting pointer class for automatic garbage collection.
void removeObj(const Ordinal &idx)
Remove an object given its ordinal index.
virtual ~ParameterList()
Destructor.
Ptr< ObjType > getNonconstObjPtr(const Ordinal &idx)
Get a nonconst semi-persisting association with the stored object indexed by ordinal.
EValidateUsed
Validation used enum.
Base types for StringIndexedOrderedValueObjectContainer.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...
#define TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.