46 #include "Teuchos_FancyOStream.hpp"
48 #include "Teuchos_VerboseObject.hpp"
56 return ( entry.
isList() ? std::string(
"...") :
toString(entry.getAny()) );
60 struct ListPlusValidList {
67 :list(_list),validList(_validList)
83 :name_(name_in), modifier_(modifier_in)
90 params_ = source.params_;
91 disableRecursiveValidation_ = source.disableRecursiveValidation_;
92 disableRecursiveModification_= source.disableRecursiveModification_;
93 disableRecursiveReconciliation_ = source.disableRecursiveReconciliation_;
94 modifier_ = source.modifier_;
112 name_ = source.name_;
113 params_ = source.params_;
114 disableRecursiveValidation_ = source.disableRecursiveValidation_;
115 disableRecursiveModification_= source.disableRecursiveModification_;
116 disableRecursiveReconciliation_ = source.disableRecursiveReconciliation_;
117 modifier_ = source.modifier_;
122 void ParameterList::setModifier(
125 modifier_ = modifier_in;
132 const std::string &name_i = this->
name(i);
143 this->updateSubListNames();
153 const std::string &name_i = this->
name(i);
171 this->updateSubListNames();
178 disableRecursiveValidation_ =
true;
185 disableRecursiveModification_ =
true;
192 disableRecursiveReconciliation_ =
true;
209 if (!(
entry(i).isUsed())) {
210 os <<
"WARNING: Parameter \"" <<
name(i) <<
"\" " <<
entry(i)
211 <<
" is unused" << std::endl;
219 std::ostringstream oss;
223 for( itr = this->
begin(), i = 0; itr != this->
end(); ++itr, ++i ) {
224 const std::string &entryName = this->
name(itr);
228 <<
" = "<<filterValueToString(theEntry) <<
"\n";
239 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
240 return params_.
getObjPtr(param_idx)->isList();
250 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
258 std::string
const& name_in,
bool throwIfNotExists
263 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
269 if (throwIfNotExists) {
270 validateEntryExists(
"get", name_in, 0);
277 const std::string& name_in,
bool mustAlreadyExist,
278 const std::string& docString
287 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
290 validateEntryIsList(name_in, *sublist_entry_ptr);
294 validateMissingSublistMustExist(this->
name(), name_in, mustAlreadyExist);
295 const Ordinal new_param_idx =
308 return any_cast<
ParameterList>(sublist_entry_ptr->getAny(
false));
314 const std::string& docString
320 ,
"The parameter "<<this->
name()<<
"->\""<<name_in<<
"\" already exists."
323 subpl.setModifier(modifier_in);
333 if (param_idx == SIOVOCB::getInvalidOrdinal()) {
334 validateMissingSublistMustExist(this->
name(), name_in,
true);
338 validateEntryIsList(name_in, *sublist_entry_ptr);
340 return any_cast<
ParameterList>(sublist_entry_ptr->getAny(
false));
352 const int indent = printOptions.indent();
353 const bool showTypes = printOptions.showTypes();
354 const bool showFlags = printOptions.showFlags();
355 const bool showDoc = printOptions.showDoc();
356 const bool showDefault = printOptions.showDefault();
357 const std::string linePrefix(indent,
' ');
359 out = getFancyOStream(
rcp(&os,
false));
360 OSTab tab(out,indent);
362 *out <<
"[empty list]" << std::endl;
368 const std::string &name_i = this->
name(i);
377 const std::string &docString = entry_i.
docString();
380 *out <<
" = "; entry_i.
leftshift(os,showFlags); *out << std::endl;
383 validator->printDoc(docString,
OSTab(os).o());
385 else if (docString.length()) {
396 const std::string &docString = entry_i.
docString();
397 const std::string &name_i = this->
name(i);
398 *out << name_i <<
" -> " << std::endl;
399 if( docString.length() && showDoc ) {
402 getValue<ParameterList>(entry_i).
print(
OSTab(out).o(), printOptions.copy().indent(0));
409 std::ostream&
ParameterList::print(std::ostream& os,
int indent,
bool showTypes,
bool showFlags,
bool showDefault)
const
411 return this->
print(os,
PrintOptions().indent(indent).showTypes(showTypes).showFlags(showFlags).showDefault(showDefault));
422 typedef std::deque<ListPlusValidList> sublist_list_t;
423 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
426 *out <<
"\n*** Entering ParameterList::validateParameters(...) for "
427 "this->name()=\""<<this->
name()<<
"\"...\n";
434 sublist_list_t sublist_list;
436 for (itr = this->
begin(); itr != this->
end(); ++itr) {
437 const std::string &entryName = this->
name(itr);
439 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
441 *out <<
"\nentryName=\""<<entryName<<
"\"\n";
454 ,
"Error, the parameter {name=\""<<entryName<<
"\","
456 ",value=\""<<filterValueToString(theEntry)<<
"\"}"
457 "\nin the parameter (sub)list \""<<this->
name()<<
"\""
458 "\nwas not found in the list of valid parameters!"
459 "\n\nThe valid parameters and types are:\n"
464 validator->validate(theEntry, entryName, this->
name());
467 const bool validType =
474 ,
"Error, the parameter {name=\""<<entryName<<
"\","
476 ",value=\""<<filterValueToString(theEntry)<<
"\"}"
477 "\nin the parameter (sub)list \""<<this->
name()<<
"\""
478 "\nexists in the list of valid parameters but has the wrong type."
479 "\n\nThe correct type is \""
483 if( theEntry.
isList() && depth > 0 ) {
484 sublist_list.push_back(
486 &getValue<ParameterList>(theEntry),&getValue<ParameterList>(*validEntry)
495 sublist_list_t::const_iterator sl_itr = sublist_list.begin();
496 sl_itr != sublist_list.end();
500 if (!sl_itr->validList->disableRecursiveValidation_) {
501 sl_itr->list->validateParameters(
509 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
510 *out <<
"\n*** Existing ParameterList::validateParameters(...) for "
511 "this->name()=\""<<this->
name()<<
"\"...\n";
521 modifier->modify(*
this, valid_pl);
522 this->setModifier(modifier);
525 for (itr = valid_pl.
begin(); itr != valid_pl.
end(); ++itr){
526 const std::string &entry_name = itr->first;
528 if (cur_entry.
isList() && depth > 0){
530 if(!valid_pl_sublist.disableRecursiveModification_){
534 ,
"Error, the parameter {name=\""<<entry_name<<
"\","
536 ",value=\""<<filterValueToString(cur_entry)<<
"\"}"
537 "\nin the parameter (sub)list \""<<this->
name()<<
"\""
538 "\nwas not found in the list of parameters during modification."
539 "\n\nThe parameters and types are:\n"
543 pl_sublist.modifyParameterList(valid_pl_sublist, depth-1);
551 const bool left_to_right)
555 std::deque<std::reference_wrapper<ParameterList>> refs, valid_refs, tmp, valid_tmp;
556 tmp.push_back(*
this);
557 valid_tmp.push_back(valid_pl);
558 while (!valid_tmp.empty()){
562 valid_tmp.pop_front();
563 refs.push_back(cur_node);
564 valid_refs.push_back(valid_cur_node);
566 for (
auto itr = valid_cur_node.
begin(); itr != valid_cur_node.
end(); ++itr){
567 const std::string &entry_name = itr->first;
568 if (valid_cur_node.
isSublist(entry_name)){
571 if (!valid_cur_node_sublist.disableRecursiveReconciliation_){
574 ,
"Error, the parameter {name=\"" << entry_name <<
"\","
576 ",value=\"" << filterValueToString(cur_entry) <<
"\"}"
577 "\nin the parameter (sub)list \"" <<cur_node.
name() <<
"\""
578 "\nwas not found in the list of parameters during reconciliation."
579 "\n\nThe parameters and types are:\n"
583 valid_tmp.push_back(valid_cur_node_sublist);
584 tmp.push_back(cur_node.
sublist(entry_name));
586 valid_tmp.push_front(valid_cur_node_sublist);
587 tmp.push_front(cur_node.
sublist(entry_name));
596 std::deque<std::reference_wrapper<ParameterList>>::reverse_iterator ref, valid_ref;
597 for(ref = refs.rbegin(), valid_ref = valid_refs.rbegin();
598 ref != refs.rend() && valid_ref != valid_refs.rend();
600 if (
nonnull(modifier = valid_ref->
get().getModifier())) {
601 modifier->reconcile(ref->get());
612 typedef std::deque<ListPlusValidList> sublist_list_t;
613 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
616 *out <<
"\n*** Entering ParameterList::validateParametersAndSetDefaults(...) "
617 "for this->name()=\""<<this->
name()<<
"\"...\n";
624 sublist_list_t sublist_list;
627 for (itr = this->nonconstBegin(); itr != this->nonconstEnd(); ++itr) {
628 const std::string &entryName = this->
name(itr);
630 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
632 *out <<
"\nentryName=\""<<entryName<<
"\"\n";
637 ,
"Error, the parameter {name=\""<<entryName<<
"\","
639 ",value=\""<<filterValueToString(theEntry)<<
"\"}"
640 "\nin the parameter (sub)list \""<<this->
name()<<
"\""
641 "\nwas not found in the list of valid parameters!"
642 "\n\nThe valid parameters and types are:\n"
647 validator->validateAndModify(entryName, this->
name(), &theEntry);
651 const bool validType =
658 ,
"Error, the parameter {name=\""<<entryName<<
"\","
660 ",value=\""<<filterValueToString(theEntry)<<
"\"}"
661 "\nin the parameter (sub)list \""<<this->
name()<<
"\""
662 "\nexists in the list of valid parameters but has the wrong type."
663 "\n\nThe correct type is \""
669 if( theEntry.
isList() && depth > 0 ) {
670 sublist_list.push_back(
672 &getValue<ParameterList>(theEntry),
673 &getValue<ParameterList>(*validEntry)
685 for (itr = validParamList.
begin(); itr != validParamList.
end(); ++itr) {
686 const std::string &validEntryName = validParamList.
name(itr);
702 #if defined(HAVE_TEUCHOS_MODIFY_DEFAULTS_DURING_VALIDATION)
703 validEntry.
validator()->validateAndModify(this->
name(itr), validEntryName, &newEntry);
709 this->
setEntry(validEntryName,newEntry);
718 sublist_list_t::iterator sl_itr = sublist_list.begin();
719 sl_itr != sublist_list.end();
723 if (!sl_itr->validList->disableRecursiveValidation_) {
724 sl_itr->list->validateParametersAndSetDefaults(*sl_itr->validList,depth-1);
727 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
728 *out <<
"\n*** Existing ParameterList::validateParametersAndSetDefaults(...) "
729 "for this->name()=\""<<this->
name()<<
"\"...\n";
737 void ParameterList::updateSubListNames(
int depth)
739 const std::string this_name = this->
name();
741 for( itr = this->nonconstBegin(); itr != this->nonconstEnd(); ++itr ) {
742 const std::string &entryName = this->
name(itr);
745 ParameterList &sublistEntry = getValue<ParameterList>(theEntry);
746 sublistEntry.
setName(this_name+std::string(
"->")+entryName);
748 sublistEntry.updateSubListNames(depth-1);
754 void ParameterList::validateEntryExists(
755 const std::string & ,
const std::string &name_in,
756 const ParameterEntry *entry_in
760 entry_in==NULL, Exceptions::InvalidParameterName
761 ,
"Error! The parameter \""<<name_in<<
"\" does not exist"\
762 "\nin the parameter (sub)list \""<<this->
name()<<
"\"."
763 "\n\nThe current parameters set in (sub)list \""<<this->
name()<<
"\" are:\n\n"
769 void ParameterList::validateEntryIsList(
770 const std::string &name_in,
const ParameterEntry &entry_in
774 !entry_in.isList(), Exceptions::InvalidParameterType
775 ,
"Error, the parameter \"" << name_in <<
"\" is not a list, it is of type \""
776 <<entry_in.getAny(
false).typeName()<<
"\"!" );
780 void ParameterList::validateMissingSublistMustExist(
const std::string &baselist_name,
781 const std::string &sublist_name,
const bool mustAlreadyExist)
const
784 mustAlreadyExist, Exceptions::InvalidParameterName
785 ,
"The sublist "<<baselist_name<<
"->\""<<sublist_name<<
"\" does not exist!"
793 bool Teuchos::operator==(
const ParameterList& list1,
const ParameterList& list2 )
801 if (!Teuchos::haveSameModifiers(list1, list2)){
804 ParameterList::ConstIterator itr1, itr2;
806 itr1 = list1.begin(), itr2 = list2.begin();
807 itr1 != list1.end() && itr2 != list2.end();
811 const std::string &entryName1 = list1.name(itr1);
812 const std::string &entryName2 = list2.name(itr2);
813 const ParameterEntry &entry1 = list1.entry(itr1);
814 const ParameterEntry &entry2 = list2.entry(itr2);
815 if( entryName1 != entryName2 ) {
818 else if( entry1 != entry2 ) {
827 if ((itr1 != list1.end()) || (itr2 != list2.end())) {
834 bool Teuchos::haveSameModifiers(
const ParameterList &list1,
const ParameterList &list2) {
836 const RCP<const ParameterListModifier> &modifier1 = list1.getModifier();
837 const RCP<const ParameterListModifier> &modifier2 = list2.getModifier();
839 const bool modifier1_is_null =
is_null(modifier1);
840 const bool modifier2_is_null =
is_null(modifier2);
841 if( modifier1_is_null || modifier2_is_null ){
842 if ( modifier1_is_null != modifier2_is_null ){
845 }
else if ( *modifier1 != *modifier2 ){
849 ParameterList::ConstIterator itr1, itr2;
851 itr1 = list1.begin(), itr2 = list2.begin();
852 itr1 != list1.end() && itr2 != list2.end();
857 const ParameterEntry &entry1 = itr1->second;
858 const ParameterEntry &entry2 = itr2->second;
859 if (entry1.isList() && entry2.isList()){
861 Teuchos::getValue<ParameterList>(entry2) ) ){
870 bool Teuchos::haveSameValues(
const ParameterList& list1,
const ParameterList& list2,
bool verbose )
878 ParameterList::ConstIterator itr1, itr2;
880 itr1 = list1.begin(), itr2 = list2.begin();
881 itr1 != list1.end() && itr2 != list2.end();
885 const std::string &entryName1 = list1.name(itr1);
886 const std::string &entryName2 = list2.name(itr2);
887 const ParameterEntry &entry1 = list1.entry(itr1);
888 const ParameterEntry &entry2 = list2.entry(itr2);
889 if( entryName1 != entryName2 ) {
890 if (verbose) std::cerr <<
"entryName1 \"" << entryName1 <<
"\" != entryName2 \"" << entryName2 <<
"\"\n";
893 if( entry1.isList() && entry2.isList() ) {
896 getValue<ParameterList>(entry1),
897 getValue<ParameterList>(entry2),
905 if (verbose) std::cerr <<
"sublists \"" << entryName1 <<
"\" differ\n";
910 if( entry1.getAny() != entry2.getAny() ) {
911 if (verbose) std::cerr <<
"for key \"" << entryName1 <<
"\", value \"" << entry1.getAny() <<
"\" != \"" << entry2.getAny() <<
"\"\n";
917 if ((itr1 != list1.end()) || (itr2 != list2.end())) {
918 if (verbose) std::cerr <<
"lists are not the same size\n";
925 bool Teuchos::haveSameValuesSorted(
const ParameterList& list1,
const ParameterList& list2,
bool verbose )
933 ParameterList::ConstIterator itr1, itr2;
934 Array<std::string> arr1, arr2;
935 for(itr1 = list1.begin(); itr1 != list1.end(); ++itr1){
936 arr1.push_back(list1.name(itr1));
938 for(itr2 = list2.begin(); itr2 != list2.end(); ++itr2){
939 arr2.push_back(list2.name(itr2));
942 if (arr1.size() != arr2.size()) {
943 if (verbose) std::cerr <<
"lists are not the same size\n";
946 std::sort(arr1.begin(), arr1.end());
947 std::sort(arr2.begin(), arr2.end());
950 iarr1 = arr1.begin(), iarr2 = arr2.begin();
951 iarr1 != arr1.end() && iarr2 != arr2.end();
955 const std::string &entryName1 = *iarr1;
956 const std::string &entryName2 = *iarr2;
957 const ParameterEntry &entry1 = list1.getEntry(entryName1);
958 const ParameterEntry &entry2 = list2.getEntry(entryName2);
959 if( entryName1 != entryName2 ) {
960 if (verbose) std::cerr <<
"entryName1 \"" << entryName1 <<
"\" != entryName2 \"" << entryName2 <<
"\"\n";
963 if( entry1.isList() && entry2.isList() ) {
966 getValue<ParameterList>(entry1),
967 getValue<ParameterList>(entry2),
975 if (verbose) std::cerr <<
"sublists \"" << entryName1 <<
"\" differ\n";
980 if( entry1.getAny() != entry2.getAny() ) {
981 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.