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);
524 for (itr = valid_pl.
begin(); itr != valid_pl.
end(); ++itr){
525 const std::string &entry_name = itr->first;
527 if (cur_entry.
isList() && depth > 0){
529 if(!valid_pl_sublist.disableRecursiveModification_){
533 ,
"Error, the parameter {name=\""<<entry_name<<
"\","
535 ",value=\""<<filterValueToString(cur_entry)<<
"\"}"
536 "\nin the parameter (sub)list \""<<this->
name()<<
"\""
537 "\nwas not found in the list of parameters during modification."
538 "\n\nThe parameters and types are:\n"
542 pl_sublist.modifyParameterList(valid_pl_sublist, depth-1);
550 const bool left_to_right)
554 std::deque<std::reference_wrapper<ParameterList>> refs, valid_refs, tmp, valid_tmp;
555 tmp.push_back(*
this);
556 valid_tmp.push_back(valid_pl);
557 while (!valid_tmp.empty()){
561 valid_tmp.pop_front();
562 refs.push_back(cur_node);
563 valid_refs.push_back(valid_cur_node);
565 for (
auto itr = valid_cur_node.
begin(); itr != valid_cur_node.
end(); ++itr){
566 const std::string &entry_name = itr->first;
567 if (valid_cur_node.
isSublist(entry_name)){
570 if (!valid_cur_node_sublist.disableRecursiveReconciliation_){
573 ,
"Error, the parameter {name=\"" << entry_name <<
"\","
575 ",value=\"" << filterValueToString(cur_entry) <<
"\"}"
576 "\nin the parameter (sub)list \"" <<cur_node.
name() <<
"\""
577 "\nwas not found in the list of parameters during reconciliation."
578 "\n\nThe parameters and types are:\n"
582 valid_tmp.push_back(valid_cur_node_sublist);
583 tmp.push_back(cur_node.
sublist(entry_name));
585 valid_tmp.push_front(valid_cur_node_sublist);
586 tmp.push_front(cur_node.
sublist(entry_name));
595 std::deque<std::reference_wrapper<ParameterList>>::reverse_iterator ref, valid_ref;
596 for(ref = refs.rbegin(), valid_ref = valid_refs.rbegin();
597 ref != refs.rend() && valid_ref != valid_refs.rend();
599 if (
nonnull(modifier = valid_ref->
get().getModifier())) {
600 modifier->reconcile(ref->get());
611 typedef std::deque<ListPlusValidList> sublist_list_t;
612 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
615 *out <<
"\n*** Entering ParameterList::validateParametersAndSetDefaults(...) "
616 "for this->name()=\""<<this->
name()<<
"\"...\n";
623 sublist_list_t sublist_list;
626 for (itr = this->nonconstBegin(); itr != this->nonconstEnd(); ++itr) {
627 const std::string &entryName = this->
name(itr);
629 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
631 *out <<
"\nentryName=\""<<entryName<<
"\"\n";
636 ,
"Error, the parameter {name=\""<<entryName<<
"\","
638 ",value=\""<<filterValueToString(theEntry)<<
"\"}"
639 "\nin the parameter (sub)list \""<<this->
name()<<
"\""
640 "\nwas not found in the list of valid parameters!"
641 "\n\nThe valid parameters and types are:\n"
646 validator->validateAndModify(entryName, this->
name(), &theEntry);
650 const bool validType =
657 ,
"Error, the parameter {name=\""<<entryName<<
"\","
659 ",value=\""<<filterValueToString(theEntry)<<
"\"}"
660 "\nin the parameter (sub)list \""<<this->
name()<<
"\""
661 "\nexists in the list of valid parameters but has the wrong type."
662 "\n\nThe correct type is \""
668 if( theEntry.
isList() && depth > 0 ) {
669 sublist_list.push_back(
671 &getValue<ParameterList>(theEntry),
672 &getValue<ParameterList>(*validEntry)
684 for (itr = validParamList.
begin(); itr != validParamList.
end(); ++itr) {
685 const std::string &validEntryName = validParamList.
name(itr);
700 this->
setEntry(validEntryName,newEntry);
709 sublist_list_t::iterator sl_itr = sublist_list.begin();
710 sl_itr != sublist_list.end();
714 if (!sl_itr->validList->disableRecursiveValidation_) {
715 sl_itr->list->validateParametersAndSetDefaults(*sl_itr->validList,depth-1);
718 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
719 *out <<
"\n*** Existing ParameterList::validateParametersAndSetDefaults(...) "
720 "for this->name()=\""<<this->
name()<<
"\"...\n";
728 void ParameterList::updateSubListNames(
int depth)
730 const std::string this_name = this->
name();
732 for( itr = this->nonconstBegin(); itr != this->nonconstEnd(); ++itr ) {
733 const std::string &entryName = this->
name(itr);
736 ParameterList &sublistEntry = getValue<ParameterList>(theEntry);
737 sublistEntry.
setName(this_name+std::string(
"->")+entryName);
739 sublistEntry.updateSubListNames(depth-1);
745 void ParameterList::validateEntryExists(
746 const std::string & ,
const std::string &name_in,
747 const ParameterEntry *entry_in
751 entry_in==NULL, Exceptions::InvalidParameterName
752 ,
"Error! The parameter \""<<name_in<<
"\" does not exist"\
753 "\nin the parameter (sub)list \""<<this->
name()<<
"\"."
754 "\n\nThe current parameters set in (sub)list \""<<this->
name()<<
"\" are:\n\n"
760 void ParameterList::validateEntryIsList(
761 const std::string &name_in,
const ParameterEntry &entry_in
765 !entry_in.isList(), Exceptions::InvalidParameterType
766 ,
"Error, the parameter \"" << name_in <<
"\" is not a list, it is of type \""
767 <<entry_in.getAny(
false).typeName()<<
"\"!" );
771 void ParameterList::validateMissingSublistMustExist(
const std::string &baselist_name,
772 const std::string &sublist_name,
const bool mustAlreadyExist)
const
775 mustAlreadyExist, Exceptions::InvalidParameterName
776 ,
"The sublist "<<baselist_name<<
"->\""<<sublist_name<<
"\" does not exist!"
784 bool Teuchos::operator==(
const ParameterList& list1,
const ParameterList& list2 )
792 if (!Teuchos::haveSameModifiers(list1, list2)){
795 ParameterList::ConstIterator itr1, itr2;
797 itr1 = list1.begin(), itr2 = list2.begin();
798 itr1 != list1.end() && itr2 != list2.end();
802 const std::string &entryName1 = list1.name(itr1);
803 const std::string &entryName2 = list2.name(itr2);
804 const ParameterEntry &entry1 = list1.entry(itr1);
805 const ParameterEntry &entry2 = list2.entry(itr2);
806 if( entryName1 != entryName2 ) {
809 else if( entry1 != entry2 ) {
818 if ((itr1 != list1.end()) || (itr2 != list2.end())) {
825 bool Teuchos::haveSameModifiers(
const ParameterList &list1,
const ParameterList &list2) {
827 const RCP<const ParameterListModifier> &modifier1 = list1.getModifier();
828 const RCP<const ParameterListModifier> &modifier2 = list2.getModifier();
830 const bool modifier1_is_null =
is_null(modifier1);
831 const bool modifier2_is_null =
is_null(modifier2);
832 if( modifier1_is_null || modifier2_is_null ){
833 if ( modifier1_is_null != modifier2_is_null ){
836 }
else if ( *modifier1 != *modifier2 ){
840 ParameterList::ConstIterator itr1, itr2;
842 itr1 = list1.begin(), itr2 = list2.begin();
843 itr1 != list1.end() && itr2 != list2.end();
848 const ParameterEntry &entry1 = itr1->second;
849 const ParameterEntry &entry2 = itr2->second;
850 if (entry1.isList() && entry2.isList()){
852 Teuchos::getValue<ParameterList>(entry2) ) ){
861 bool Teuchos::haveSameValues(
const ParameterList& list1,
const ParameterList& list2,
bool verbose )
869 ParameterList::ConstIterator itr1, itr2;
871 itr1 = list1.begin(), itr2 = list2.begin();
872 itr1 != list1.end() && itr2 != list2.end();
876 const std::string &entryName1 = list1.name(itr1);
877 const std::string &entryName2 = list2.name(itr2);
878 const ParameterEntry &entry1 = list1.entry(itr1);
879 const ParameterEntry &entry2 = list2.entry(itr2);
880 if( entryName1 != entryName2 ) {
881 if (verbose) std::cerr <<
"entryName1 \"" << entryName1 <<
"\" != entryName2 \"" << entryName2 <<
"\"\n";
884 if( entry1.isList() && entry2.isList() ) {
887 getValue<ParameterList>(entry1),
888 getValue<ParameterList>(entry2),
896 if (verbose) std::cerr <<
"sublists \"" << entryName1 <<
"\" differ\n";
901 if( entry1.getAny() != entry2.getAny() ) {
902 if (verbose) std::cerr <<
"for key \"" << entryName1 <<
"\", value \"" << entry1.getAny() <<
"\" != \"" << entry2.getAny() <<
"\"\n";
908 if ((itr1 != list1.end()) || (itr2 != list2.end())) {
909 if (verbose) std::cerr <<
"lists are not the same size\n";
916 bool Teuchos::haveSameValuesSorted(
const ParameterList& list1,
const ParameterList& list2,
bool verbose )
924 ParameterList::ConstIterator itr1, itr2;
925 Array<std::string> arr1, arr2;
926 for(itr1 = list1.begin(); itr1 != list1.end(); ++itr1){
927 arr1.push_back(list1.name(itr1));
929 for(itr2 = list2.begin(); itr2 != list2.end(); ++itr2){
930 arr2.push_back(list2.name(itr2));
933 if (arr1.size() != arr2.size()) {
934 if (verbose) std::cerr <<
"lists are not the same size\n";
937 std::sort(arr1.begin(), arr1.end());
938 std::sort(arr2.begin(), arr2.end());
941 iarr1 = arr1.begin(), iarr2 = arr2.begin();
942 iarr1 != arr1.end() && iarr2 != arr2.end();
946 const std::string &entryName1 = *iarr1;
947 const std::string &entryName2 = *iarr2;
948 const ParameterEntry &entry1 = list1.getEntry(entryName1);
949 const ParameterEntry &entry2 = list2.getEntry(entryName2);
950 if( entryName1 != entryName2 ) {
951 if (verbose) std::cerr <<
"entryName1 \"" << entryName1 <<
"\" != entryName2 \"" << entryName2 <<
"\"\n";
954 if( entry1.isList() && entry2.isList() ) {
957 getValue<ParameterList>(entry1),
958 getValue<ParameterList>(entry2),
966 if (verbose) std::cerr <<
"sublists \"" << entryName1 <<
"\" differ\n";
971 if( entry1.getAny() != entry2.getAny() ) {
972 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.