14 #include "Teuchos_FancyOStream.hpp"
16 #include "Teuchos_VerboseObject.hpp"
24 return ( entry.
isList() ? std::string(
"...") :
toString(entry.getAny()) );
28 struct ListPlusValidList {
35 :list(_list),validList(_validList)
51 :name_(name_in), modifier_(modifier_in)
58 params_ = source.params_;
59 disableRecursiveValidation_ = source.disableRecursiveValidation_;
60 disableRecursiveModification_= source.disableRecursiveModification_;
61 disableRecursiveReconciliation_ = source.disableRecursiveReconciliation_;
62 modifier_ = source.modifier_;
81 params_ = source.params_;
82 disableRecursiveValidation_ = source.disableRecursiveValidation_;
83 disableRecursiveModification_= source.disableRecursiveModification_;
84 disableRecursiveReconciliation_ = source.disableRecursiveReconciliation_;
85 modifier_ = source.modifier_;
90 void ParameterList::setModifier(
93 modifier_ = modifier_in;
100 const std::string &name_i = this->
name(i);
111 this->updateSubListNames();
121 const std::string &name_i = this->
name(i);
139 this->updateSubListNames();
146 disableRecursiveValidation_ =
true;
153 disableRecursiveModification_ =
true;
160 disableRecursiveReconciliation_ =
true;
177 if (!(
entry(i).isUsed())) {
178 os <<
"WARNING: Parameter \"" <<
name(i) <<
"\" " <<
entry(i)
179 <<
" is unused" << std::endl;
187 std::ostringstream oss;
191 for( itr = this->
begin(), i = 0; itr != this->
end(); ++itr, ++i ) {
192 const std::string &entryName = this->
name(itr);
196 <<
" = "<<filterValueToString(theEntry) <<
"\n";
207 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
208 return params_.
getObjPtr(param_idx)->isList();
218 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
226 std::string
const& name_in,
bool throwIfNotExists
231 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
237 if (throwIfNotExists) {
238 validateEntryExists(
"get", name_in, 0);
245 const std::string& name_in,
bool mustAlreadyExist,
246 const std::string& docString
255 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
258 validateEntryIsList(name_in, *sublist_entry_ptr);
262 validateMissingSublistMustExist(this->
name(), name_in, mustAlreadyExist);
263 const Ordinal new_param_idx =
276 return any_cast<
ParameterList>(sublist_entry_ptr->getAny(
false));
282 const std::string& docString
288 ,
"The parameter "<<this->
name()<<
"->\""<<name_in<<
"\" already exists."
291 subpl.setModifier(modifier_in);
301 if (param_idx == SIOVOCB::getInvalidOrdinal()) {
302 validateMissingSublistMustExist(this->
name(), name_in,
true);
306 validateEntryIsList(name_in, *sublist_entry_ptr);
308 return any_cast<
ParameterList>(sublist_entry_ptr->getAny(
false));
320 const int indent = printOptions.indent();
321 const bool showTypes = printOptions.showTypes();
322 const bool showFlags = printOptions.showFlags();
323 const bool showDoc = printOptions.showDoc();
324 const bool showDefault = printOptions.showDefault();
325 const std::string linePrefix(indent,
' ');
327 out = getFancyOStream(
rcp(&os,
false));
328 OSTab tab(out,indent);
330 *out <<
"[empty list]" << std::endl;
336 const std::string &name_i = this->
name(i);
345 const std::string &docString = entry_i.
docString();
348 *out <<
" = "; entry_i.
leftshift(os,showFlags); *out << std::endl;
351 validator->printDoc(docString,
OSTab(os).o());
353 else if (docString.length()) {
364 const std::string &docString = entry_i.
docString();
365 const std::string &name_i = this->
name(i);
366 *out << name_i <<
" -> " << std::endl;
367 if( docString.length() && showDoc ) {
370 getValue<ParameterList>(entry_i).
print(
OSTab(out).o(), printOptions.copy().indent(0));
377 std::ostream&
ParameterList::print(std::ostream& os,
int indent,
bool showTypes,
bool showFlags,
bool showDefault)
const
379 return this->
print(os,
PrintOptions().indent(indent).showTypes(showTypes).showFlags(showFlags).showDefault(showDefault));
390 typedef std::deque<ListPlusValidList> sublist_list_t;
391 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
394 *out <<
"\n*** Entering ParameterList::validateParameters(...) for "
395 "this->name()=\""<<this->
name()<<
"\"...\n";
402 sublist_list_t sublist_list;
404 for (itr = this->
begin(); itr != this->
end(); ++itr) {
405 const std::string &entryName = this->
name(itr);
407 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
409 *out <<
"\nentryName=\""<<entryName<<
"\"\n";
422 ,
"Error, the parameter {name=\""<<entryName<<
"\","
424 ",value=\""<<filterValueToString(theEntry)<<
"\"}"
425 "\nin the parameter (sub)list \""<<this->
name()<<
"\""
426 "\nwas not found in the list of valid parameters!"
427 "\n\nThe valid parameters and types are:\n"
432 validator->validate(theEntry, entryName, this->
name());
435 const bool validType =
442 ,
"Error, the parameter {name=\""<<entryName<<
"\","
444 ",value=\""<<filterValueToString(theEntry)<<
"\"}"
445 "\nin the parameter (sub)list \""<<this->
name()<<
"\""
446 "\nexists in the list of valid parameters but has the wrong type."
447 "\n\nThe correct type is \""
451 if( theEntry.
isList() && depth > 0 ) {
452 sublist_list.push_back(
454 &getValue<ParameterList>(theEntry),&getValue<ParameterList>(*validEntry)
463 sublist_list_t::const_iterator sl_itr = sublist_list.begin();
464 sl_itr != sublist_list.end();
468 if (!sl_itr->validList->disableRecursiveValidation_) {
469 sl_itr->list->validateParameters(
477 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
478 *out <<
"\n*** Existing ParameterList::validateParameters(...) for "
479 "this->name()=\""<<this->
name()<<
"\"...\n";
489 modifier->modify(*
this, valid_pl);
490 this->setModifier(modifier);
493 for (itr = valid_pl.
begin(); itr != valid_pl.
end(); ++itr){
494 const std::string &entry_name = itr->first;
496 if (cur_entry.
isList() && depth > 0){
498 if(!valid_pl_sublist.disableRecursiveModification_){
502 ,
"Error, the parameter {name=\""<<entry_name<<
"\","
504 ",value=\""<<filterValueToString(cur_entry)<<
"\"}"
505 "\nin the parameter (sub)list \""<<this->
name()<<
"\""
506 "\nwas not found in the list of parameters during modification."
507 "\n\nThe parameters and types are:\n"
511 pl_sublist.modifyParameterList(valid_pl_sublist, depth-1);
519 const bool left_to_right)
523 std::deque<std::reference_wrapper<ParameterList>> refs, valid_refs, tmp, valid_tmp;
524 tmp.push_back(*
this);
525 valid_tmp.push_back(valid_pl);
526 while (!valid_tmp.empty()){
530 valid_tmp.pop_front();
531 refs.push_back(cur_node);
532 valid_refs.push_back(valid_cur_node);
534 for (
auto itr = valid_cur_node.
begin(); itr != valid_cur_node.
end(); ++itr){
535 const std::string &entry_name = itr->first;
536 if (valid_cur_node.
isSublist(entry_name)){
539 if (!valid_cur_node_sublist.disableRecursiveReconciliation_){
542 ,
"Error, the parameter {name=\"" << entry_name <<
"\","
544 ",value=\"" << filterValueToString(cur_entry) <<
"\"}"
545 "\nin the parameter (sub)list \"" <<cur_node.
name() <<
"\""
546 "\nwas not found in the list of parameters during reconciliation."
547 "\n\nThe parameters and types are:\n"
551 valid_tmp.push_back(valid_cur_node_sublist);
552 tmp.push_back(cur_node.
sublist(entry_name));
554 valid_tmp.push_front(valid_cur_node_sublist);
555 tmp.push_front(cur_node.
sublist(entry_name));
564 std::deque<std::reference_wrapper<ParameterList>>::reverse_iterator ref, valid_ref;
565 for(ref = refs.rbegin(), valid_ref = valid_refs.rbegin();
566 ref != refs.rend() && valid_ref != valid_refs.rend();
568 if (
nonnull(modifier = valid_ref->
get().getModifier())) {
569 modifier->reconcile(ref->get());
580 typedef std::deque<ListPlusValidList> sublist_list_t;
581 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
584 *out <<
"\n*** Entering ParameterList::validateParametersAndSetDefaults(...) "
585 "for this->name()=\""<<this->
name()<<
"\"...\n";
592 sublist_list_t sublist_list;
595 for (itr = this->nonconstBegin(); itr != this->nonconstEnd(); ++itr) {
596 const std::string &entryName = this->
name(itr);
598 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
600 *out <<
"\nentryName=\""<<entryName<<
"\"\n";
605 ,
"Error, the parameter {name=\""<<entryName<<
"\","
607 ",value=\""<<filterValueToString(theEntry)<<
"\"}"
608 "\nin the parameter (sub)list \""<<this->
name()<<
"\""
609 "\nwas not found in the list of valid parameters!"
610 "\n\nThe valid parameters and types are:\n"
615 validator->validateAndModify(entryName, this->
name(), &theEntry);
619 const bool validType =
626 ,
"Error, the parameter {name=\""<<entryName<<
"\","
628 ",value=\""<<filterValueToString(theEntry)<<
"\"}"
629 "\nin the parameter (sub)list \""<<this->
name()<<
"\""
630 "\nexists in the list of valid parameters but has the wrong type."
631 "\n\nThe correct type is \""
637 if( theEntry.
isList() && depth > 0 ) {
638 sublist_list.push_back(
640 &getValue<ParameterList>(theEntry),
641 &getValue<ParameterList>(*validEntry)
653 for (itr = validParamList.
begin(); itr != validParamList.
end(); ++itr) {
654 const std::string &validEntryName = validParamList.
name(itr);
670 #if defined(HAVE_TEUCHOS_MODIFY_DEFAULTS_DURING_VALIDATION)
671 validEntry.
validator()->validateAndModify(this->
name(itr), validEntryName, &newEntry);
677 this->
setEntry(validEntryName,newEntry);
686 sublist_list_t::iterator sl_itr = sublist_list.begin();
687 sl_itr != sublist_list.end();
691 if (!sl_itr->validList->disableRecursiveValidation_) {
692 sl_itr->list->validateParametersAndSetDefaults(*sl_itr->validList,depth-1);
695 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
696 *out <<
"\n*** Existing ParameterList::validateParametersAndSetDefaults(...) "
697 "for this->name()=\""<<this->
name()<<
"\"...\n";
705 void ParameterList::updateSubListNames(
int depth)
707 const std::string this_name = this->
name();
709 for( itr = this->nonconstBegin(); itr != this->nonconstEnd(); ++itr ) {
710 const std::string &entryName = this->
name(itr);
713 ParameterList &sublistEntry = getValue<ParameterList>(theEntry);
714 sublistEntry.
setName(this_name+std::string(
"->")+entryName);
716 sublistEntry.updateSubListNames(depth-1);
722 void ParameterList::validateEntryExists(
723 const std::string & ,
const std::string &name_in,
724 const ParameterEntry *entry_in
728 entry_in==NULL, Exceptions::InvalidParameterName
729 ,
"Error! The parameter \""<<name_in<<
"\" does not exist"\
730 "\nin the parameter (sub)list \""<<this->
name()<<
"\"."
731 "\n\nThe current parameters set in (sub)list \""<<this->
name()<<
"\" are:\n\n"
737 void ParameterList::validateEntryIsList(
738 const std::string &name_in,
const ParameterEntry &entry_in
742 !entry_in.isList(), Exceptions::InvalidParameterType
743 ,
"Error, the parameter \"" << name_in <<
"\" is not a list, it is of type \""
744 <<entry_in.getAny(
false).typeName()<<
"\"!" );
748 void ParameterList::validateMissingSublistMustExist(
const std::string &baselist_name,
749 const std::string &sublist_name,
const bool mustAlreadyExist)
const
752 mustAlreadyExist, Exceptions::InvalidParameterName
753 ,
"The sublist "<<baselist_name<<
"->\""<<sublist_name<<
"\" does not exist!"
761 bool Teuchos::operator==(
const ParameterList& list1,
const ParameterList& list2 )
769 if (!Teuchos::haveSameModifiers(list1, list2)){
772 ParameterList::ConstIterator itr1, itr2;
774 itr1 = list1.begin(), itr2 = list2.begin();
775 itr1 != list1.end() && itr2 != list2.end();
779 const std::string &entryName1 = list1.name(itr1);
780 const std::string &entryName2 = list2.name(itr2);
781 const ParameterEntry &entry1 = list1.entry(itr1);
782 const ParameterEntry &entry2 = list2.entry(itr2);
783 if( entryName1 != entryName2 ) {
786 else if( entry1 != entry2 ) {
795 if ((itr1 != list1.end()) || (itr2 != list2.end())) {
802 bool Teuchos::haveSameModifiers(
const ParameterList &list1,
const ParameterList &list2) {
804 const RCP<const ParameterListModifier> &modifier1 = list1.getModifier();
805 const RCP<const ParameterListModifier> &modifier2 = list2.getModifier();
807 const bool modifier1_is_null =
is_null(modifier1);
808 const bool modifier2_is_null =
is_null(modifier2);
809 if( modifier1_is_null || modifier2_is_null ){
810 if ( modifier1_is_null != modifier2_is_null ){
813 }
else if ( *modifier1 != *modifier2 ){
817 ParameterList::ConstIterator itr1, itr2;
819 itr1 = list1.begin(), itr2 = list2.begin();
820 itr1 != list1.end() && itr2 != list2.end();
825 const ParameterEntry &entry1 = itr1->second;
826 const ParameterEntry &entry2 = itr2->second;
827 if (entry1.isList() && entry2.isList()){
829 Teuchos::getValue<ParameterList>(entry2) ) ){
838 bool Teuchos::haveSameValues(
const ParameterList& list1,
const ParameterList& list2,
bool verbose )
846 ParameterList::ConstIterator itr1, itr2;
848 itr1 = list1.begin(), itr2 = list2.begin();
849 itr1 != list1.end() && itr2 != list2.end();
853 const std::string &entryName1 = list1.name(itr1);
854 const std::string &entryName2 = list2.name(itr2);
855 const ParameterEntry &entry1 = list1.entry(itr1);
856 const ParameterEntry &entry2 = list2.entry(itr2);
857 if( entryName1 != entryName2 ) {
858 if (verbose) std::cerr <<
"entryName1 \"" << entryName1 <<
"\" != entryName2 \"" << entryName2 <<
"\"\n";
861 if( entry1.isList() && entry2.isList() ) {
864 getValue<ParameterList>(entry1),
865 getValue<ParameterList>(entry2),
873 if (verbose) std::cerr <<
"sublists \"" << entryName1 <<
"\" differ\n";
878 if( entry1.getAny() != entry2.getAny() ) {
879 if (verbose) std::cerr <<
"for key \"" << entryName1 <<
"\", value \"" << entry1.getAny() <<
"\" != \"" << entry2.getAny() <<
"\"\n";
885 if ((itr1 != list1.end()) || (itr2 != list2.end())) {
886 if (verbose) std::cerr <<
"lists are not the same size\n";
893 bool Teuchos::haveSameValuesSorted(
const ParameterList& list1,
const ParameterList& list2,
bool verbose )
901 ParameterList::ConstIterator itr1, itr2;
902 Array<std::string> arr1, arr2;
903 for(itr1 = list1.begin(); itr1 != list1.end(); ++itr1){
904 arr1.push_back(list1.name(itr1));
906 for(itr2 = list2.begin(); itr2 != list2.end(); ++itr2){
907 arr2.push_back(list2.name(itr2));
910 if (arr1.size() != arr2.size()) {
911 if (verbose) std::cerr <<
"lists are not the same size\n";
914 std::sort(arr1.begin(), arr1.end());
915 std::sort(arr2.begin(), arr2.end());
918 iarr1 = arr1.begin(), iarr2 = arr2.begin();
919 iarr1 != arr1.end() && iarr2 != arr2.end();
923 const std::string &entryName1 = *iarr1;
924 const std::string &entryName2 = *iarr2;
925 const ParameterEntry &entry1 = list1.getEntry(entryName1);
926 const ParameterEntry &entry2 = list2.getEntry(entryName2);
927 if( entryName1 != entryName2 ) {
928 if (verbose) std::cerr <<
"entryName1 \"" << entryName1 <<
"\" != entryName2 \"" << entryName2 <<
"\"\n";
931 if( entry1.isList() && entry2.isList() ) {
934 getValue<ParameterList>(entry1),
935 getValue<ParameterList>(entry2),
943 if (verbose) std::cerr <<
"sublists \"" << entryName1 <<
"\" differ\n";
948 if( entry1.getAny() != entry2.getAny() ) {
949 if (verbose) std::cerr <<
"for key \"" << entryName1 <<
"\", value \"" << entry1.getAny() <<
"\" != \"" << entry2.getAny() <<
"\"\n";
void print() const
Print function to use in debugging in a debugger.
const std::string & name() const
The name of this ParameterList.
C++ Standard Library compatable filtered iterator.
Ordinal getObjOrdinalIndex(const std::string &key) const
Get the ordinal index given the string key.
ParameterList & setEntry(const std::string &name, U &&entry)
Set a parameter directly as a ParameterEntry.
ConstIterator end() const
An iterator pointing beyond the last entry.
basic_OSTab< char > OSTab
ParameterList & disableRecursiveValidation()
void setValidator(RCP< const ParameterEntryValidator > const &validator)
Set the validator.
RCP< const ParameterEntryValidator > validator() const
Return the (optional) validator object.
std::ostream & leftshift(std::ostream &os, bool printFlags=true) const
Output a non-list parameter to the given output stream.
std::string currentParametersString() const
Create a single formated std::string of all of the zero-level parameters in this list.
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT bool haveSameValues(const ParameterList &list1, const ParameterList &list2, bool verbose=false)
Returns true if two parameter lists have the same values.
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.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Ordinal setObj(const std::string &key, U &&obj)
Set (or reset) object by value and return its ordinal index.
void setAnyValue(const any &value, bool isDefault=false)
Set the value as an any object.
T * get() const
Get the raw C++ pointer to the underlying object.
Ordinal numParams() const
Get the number of stored parameters.
static std::ostream & printLines(std::ostream &os, const std::string &linePrefix, const std::string &lines)
Print lines with prefix first.
bool isDefault() const
Indicate whether this entry takes on the default value.
Tabbing class for helping to create formated, indented output for a basic_FancyOStream object...
bool is_null(const ArrayRCP< T > &p)
Returns true if p.get()==NULL.
ParameterList & disableRecursiveAll()
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.
bool isParameter(const std::string &name) const
Whether the given parameter exists in this list.
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.
bool isSublist(const std::string &name) const
Whether the given sublist exists in this list.
ParameterList & operator=(const ParameterList &source)
Replace the current parameter list with source.
static RCP< FancyOStream > getDefaultOStream()
Get the default output stream object.
ParameterList & disableRecursiveReconciliation()
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.
ConstIterator begin() const
An iterator pointing to the first entry.
bool isList() const
Return whether or not the value itself is a list.
Ptr< const ObjType > getObjPtr(const Ordinal &idx) const
Get a const semi-persisting association with the stored object indexed by ordinal.
A list of parameters of arbitrary type.
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.
ParameterList & setParameters(const ParameterList &source)
void modifyParameterList(ParameterList &validParamList, int const depth=1000)
Modify the valid parameter list prior to validation.
const ParameterEntry & entry(ConstIterator i) const
Access to ParameterEntry (i.e., returns i->second)
any & getAny(bool activeQry=true)
Direct access to the Teuchos::any data value underlying this object. The bool argument activeQry (def...
RCP< const ParameterListModifier > getModifier() const
Return the optional modifier object.
ParameterList()=default
Constructor.
ParameterList & setParametersNotAlreadySet(const ParameterList &source)
EValidateDefaults
Validation defaults enum.
void unused(std::ostream &os) const
Print out unused parameters in the ParameterList.
std::string typeName() const
Return the name of the type.
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.
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT bool haveSameModifiers(const ParameterList &list1, const ParameterList &list2)
Returns true if two parameter lists have the same modifiers.
const std::type_info & type() const
Return the type of value being stored.
virtual ~ParameterList()
Destructor.
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT bool haveSameValuesSorted(const ParameterList &list1, const ParameterList &list2, bool verbose=false)
Returns true if two parameter lists have the same values independent of ordering. ...
std::string docString() const
Return the (optional) documentation std::string.
bool isUsed() const
Return whether or not the value has been used; i.e., whether or not the value has been retrieved via ...
Ordinal numObjects() const
ParameterList & disableRecursiveModification()
std::vector< std::string >::iterator iterator
The type of a forward iterator.
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...
void reconcileParameterList(ParameterList &validParamList, const bool left_to_right=true)
Reconcile a parameter list after validation.
#define TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.