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);
701 #if defined(HAVE_TEUCHOS_MODIFY_DEFAULTS_DURING_VALIDATION)
702 validEntry.
validator()->validateAndModify(this->
name(itr), validEntryName, &newEntry);
708 this->
setEntry(validEntryName,newEntry);
717 sublist_list_t::iterator sl_itr = sublist_list.begin();
718 sl_itr != sublist_list.end();
722 if (!sl_itr->validList->disableRecursiveValidation_) {
723 sl_itr->list->validateParametersAndSetDefaults(*sl_itr->validList,depth-1);
726 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
727 *out <<
"\n*** Existing ParameterList::validateParametersAndSetDefaults(...) "
728 "for this->name()=\""<<this->
name()<<
"\"...\n";
736 void ParameterList::updateSubListNames(
int depth)
738 const std::string this_name = this->
name();
740 for( itr = this->nonconstBegin(); itr != this->nonconstEnd(); ++itr ) {
741 const std::string &entryName = this->
name(itr);
744 ParameterList &sublistEntry = getValue<ParameterList>(theEntry);
745 sublistEntry.
setName(this_name+std::string(
"->")+entryName);
747 sublistEntry.updateSubListNames(depth-1);
753 void ParameterList::validateEntryExists(
754 const std::string & ,
const std::string &name_in,
755 const ParameterEntry *entry_in
759 entry_in==NULL, Exceptions::InvalidParameterName
760 ,
"Error! The parameter \""<<name_in<<
"\" does not exist"\
761 "\nin the parameter (sub)list \""<<this->
name()<<
"\"."
762 "\n\nThe current parameters set in (sub)list \""<<this->
name()<<
"\" are:\n\n"
768 void ParameterList::validateEntryIsList(
769 const std::string &name_in,
const ParameterEntry &entry_in
773 !entry_in.isList(), Exceptions::InvalidParameterType
774 ,
"Error, the parameter \"" << name_in <<
"\" is not a list, it is of type \""
775 <<entry_in.getAny(
false).typeName()<<
"\"!" );
779 void ParameterList::validateMissingSublistMustExist(
const std::string &baselist_name,
780 const std::string &sublist_name,
const bool mustAlreadyExist)
const
783 mustAlreadyExist, Exceptions::InvalidParameterName
784 ,
"The sublist "<<baselist_name<<
"->\""<<sublist_name<<
"\" does not exist!"
792 bool Teuchos::operator==(
const ParameterList& list1,
const ParameterList& list2 )
800 if (!Teuchos::haveSameModifiers(list1, list2)){
803 ParameterList::ConstIterator itr1, itr2;
805 itr1 = list1.begin(), itr2 = list2.begin();
806 itr1 != list1.end() && itr2 != list2.end();
810 const std::string &entryName1 = list1.name(itr1);
811 const std::string &entryName2 = list2.name(itr2);
812 const ParameterEntry &entry1 = list1.entry(itr1);
813 const ParameterEntry &entry2 = list2.entry(itr2);
814 if( entryName1 != entryName2 ) {
817 else if( entry1 != entry2 ) {
826 if ((itr1 != list1.end()) || (itr2 != list2.end())) {
833 bool Teuchos::haveSameModifiers(
const ParameterList &list1,
const ParameterList &list2) {
835 const RCP<const ParameterListModifier> &modifier1 = list1.getModifier();
836 const RCP<const ParameterListModifier> &modifier2 = list2.getModifier();
838 const bool modifier1_is_null =
is_null(modifier1);
839 const bool modifier2_is_null =
is_null(modifier2);
840 if( modifier1_is_null || modifier2_is_null ){
841 if ( modifier1_is_null != modifier2_is_null ){
844 }
else if ( *modifier1 != *modifier2 ){
848 ParameterList::ConstIterator itr1, itr2;
850 itr1 = list1.begin(), itr2 = list2.begin();
851 itr1 != list1.end() && itr2 != list2.end();
856 const ParameterEntry &entry1 = itr1->second;
857 const ParameterEntry &entry2 = itr2->second;
858 if (entry1.isList() && entry2.isList()){
860 Teuchos::getValue<ParameterList>(entry2) ) ){
869 bool Teuchos::haveSameValues(
const ParameterList& list1,
const ParameterList& list2,
bool verbose )
877 ParameterList::ConstIterator itr1, itr2;
879 itr1 = list1.begin(), itr2 = list2.begin();
880 itr1 != list1.end() && itr2 != list2.end();
884 const std::string &entryName1 = list1.name(itr1);
885 const std::string &entryName2 = list2.name(itr2);
886 const ParameterEntry &entry1 = list1.entry(itr1);
887 const ParameterEntry &entry2 = list2.entry(itr2);
888 if( entryName1 != entryName2 ) {
889 if (verbose) std::cerr <<
"entryName1 \"" << entryName1 <<
"\" != entryName2 \"" << entryName2 <<
"\"\n";
892 if( entry1.isList() && entry2.isList() ) {
895 getValue<ParameterList>(entry1),
896 getValue<ParameterList>(entry2),
904 if (verbose) std::cerr <<
"sublists \"" << entryName1 <<
"\" differ\n";
909 if( entry1.getAny() != entry2.getAny() ) {
910 if (verbose) std::cerr <<
"for key \"" << entryName1 <<
"\", value \"" << entry1.getAny() <<
"\" != \"" << entry2.getAny() <<
"\"\n";
916 if ((itr1 != list1.end()) || (itr2 != list2.end())) {
917 if (verbose) std::cerr <<
"lists are not the same size\n";
924 bool Teuchos::haveSameValuesSorted(
const ParameterList& list1,
const ParameterList& list2,
bool verbose )
932 ParameterList::ConstIterator itr1, itr2;
933 Array<std::string> arr1, arr2;
934 for(itr1 = list1.begin(); itr1 != list1.end(); ++itr1){
935 arr1.push_back(list1.name(itr1));
937 for(itr2 = list2.begin(); itr2 != list2.end(); ++itr2){
938 arr2.push_back(list2.name(itr2));
941 if (arr1.size() != arr2.size()) {
942 if (verbose) std::cerr <<
"lists are not the same size\n";
945 std::sort(arr1.begin(), arr1.end());
946 std::sort(arr2.begin(), arr2.end());
949 iarr1 = arr1.begin(), iarr2 = arr2.begin();
950 iarr1 != arr1.end() && iarr2 != arr2.end();
954 const std::string &entryName1 = *iarr1;
955 const std::string &entryName2 = *iarr2;
956 const ParameterEntry &entry1 = list1.getEntry(entryName1);
957 const ParameterEntry &entry2 = list2.getEntry(entryName2);
958 if( entryName1 != entryName2 ) {
959 if (verbose) std::cerr <<
"entryName1 \"" << entryName1 <<
"\" != entryName2 \"" << entryName2 <<
"\"\n";
962 if( entry1.isList() && entry2.isList() ) {
965 getValue<ParameterList>(entry1),
966 getValue<ParameterList>(entry2),
974 if (verbose) std::cerr <<
"sublists \"" << entryName1 <<
"\" differ\n";
979 if( entry1.getAny() != entry2.getAny() ) {
980 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.