46 #include "Teuchos_FancyOStream.hpp"
48 #include "Teuchos_VerboseObject.hpp"
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);
136 getValue<ParameterList>(entry_i) );
142 this->updateSubListNames();
152 const std::string &name_i = this->
name(i);
156 getValue<ParameterList>(entry_i) );
167 this->updateSubListNames();
174 disableRecursiveValidation_ =
true;
181 disableRecursiveModification_ =
true;
188 disableRecursiveReconciliation_ =
true;
205 if (!(
entry(i).isUsed())) {
206 os <<
"WARNING: Parameter \"" <<
name(i) <<
"\" " <<
entry(i)
207 <<
" is unused" << std::endl;
215 std::ostringstream oss;
219 for( itr = this->
begin(), i = 0; itr != this->
end(); ++itr, ++i ) {
220 const std::string &entryName = this->
name(itr);
224 <<
" = "<<filterValueToString(theEntry) <<
"\n";
235 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
236 return params_.
getObjPtr(param_idx)->isList();
246 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
254 std::string
const& name_in,
bool throwIfNotExists
259 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
265 if (throwIfNotExists) {
266 validateEntryExists(
"get", name_in, 0);
273 const std::string& name_in,
bool mustAlreadyExist,
274 const std::string& docString
283 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
286 validateEntryIsList(name_in, *sublist_entry_ptr);
290 validateMissingSublistMustExist(this->
name(), name_in, mustAlreadyExist);
291 const Ordinal new_param_idx =
304 return any_cast<ParameterList>(sublist_entry_ptr->getAny(
false));
310 const std::string& docString
316 ,
"The parameter "<<this->
name()<<
"->\""<<name_in<<
"\" already exists."
319 subpl.setModifier(modifier_in);
329 if (param_idx == SIOVOCB::getInvalidOrdinal()) {
330 validateMissingSublistMustExist(this->
name(), name_in,
true);
334 validateEntryIsList(name_in, *sublist_entry_ptr);
336 return any_cast<ParameterList>(sublist_entry_ptr->getAny(
false));
348 const int indent = printOptions.indent();
349 const bool showTypes = printOptions.showTypes();
350 const bool showFlags = printOptions.showFlags();
351 const bool showDoc = printOptions.showDoc();
352 const std::string linePrefix(indent,
' ');
354 out = getFancyOStream(
rcp(&os,
false));
355 OSTab tab(out,indent);
357 *out <<
"[empty list]" << std::endl;
363 const std::string &name_i = this->
name(i);
370 const std::string &docString = entry_i.
docString();
373 *out <<
" = "; entry_i.
leftshift(os,showFlags); *out << std::endl;
376 validator->printDoc(docString,
OSTab(os).o());
378 else if (docString.length()) {
389 const std::string &docString = entry_i.
docString();
390 const std::string &name_i = this->
name(i);
391 *out << name_i <<
" -> " << std::endl;
392 if( docString.length() && showDoc ) {
395 getValue<ParameterList>(entry_i).print(
OSTab(out).o(), printOptions.copy().indent(0));
404 return this->
print(os,
PrintOptions().indent(indent).showTypes(showTypes).showFlags(showFlags));
415 typedef std::deque<ListPlusValidList> sublist_list_t;
416 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
419 *out <<
"\n*** Entering ParameterList::validateParameters(...) for "
420 "this->name()=\""<<this->
name()<<
"\"...\n";
427 sublist_list_t sublist_list;
429 for (itr = this->
begin(); itr != this->
end(); ++itr) {
430 const std::string &entryName = this->
name(itr);
432 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
434 *out <<
"\nentryName=\""<<entryName<<
"\"\n";
447 ,
"Error, the parameter {name=\""<<entryName<<
"\","
449 ",value=\""<<filterValueToString(theEntry)<<
"\"}"
450 "\nin the parameter (sub)list \""<<this->
name()<<
"\""
451 "\nwas not found in the list of valid parameters!"
452 "\n\nThe valid parameters and types are:\n"
457 validator->validate(theEntry, entryName, this->
name());
460 const bool validType =
467 ,
"Error, the parameter {name=\""<<entryName<<
"\","
469 ",value=\""<<filterValueToString(theEntry)<<
"\"}"
470 "\nin the parameter (sub)list \""<<this->
name()<<
"\""
471 "\nexists in the list of valid parameters but has the wrong type."
472 "\n\nThe correct type is \""
476 if( theEntry.
isList() && depth > 0 ) {
477 sublist_list.push_back(
479 &getValue<ParameterList>(theEntry),&getValue<ParameterList>(*validEntry)
488 sublist_list_t::const_iterator sl_itr = sublist_list.begin();
489 sl_itr != sublist_list.end();
493 if (!sl_itr->validList->disableRecursiveValidation_) {
494 sl_itr->list->validateParameters(
502 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
503 *out <<
"\n*** Existing ParameterList::validateParameters(...) for "
504 "this->name()=\""<<this->
name()<<
"\"...\n";
514 modifier->modify(*
this, valid_pl);
517 for (itr = valid_pl.
begin(); itr != valid_pl.
end(); ++itr){
518 const std::string &entry_name = itr->first;
520 if (cur_entry.
isList() && depth > 0){
522 if(!valid_pl_sublist.disableRecursiveModification_){
526 ,
"Error, the parameter {name=\""<<entry_name<<
"\","
528 ",value=\""<<filterValueToString(cur_entry)<<
"\"}"
529 "\nin the parameter (sub)list \""<<this->
name()<<
"\""
530 "\nwas not found in the list of parameters during modification."
531 "\n\nThe parameters and types are:\n"
543 const bool left_to_right)
547 std::deque<std::reference_wrapper<ParameterList>> refs, valid_refs, tmp, valid_tmp;
548 tmp.push_back(*
this);
549 valid_tmp.push_back(valid_pl);
550 while (!valid_tmp.empty()){
554 valid_tmp.pop_front();
555 refs.push_back(cur_node);
556 valid_refs.push_back(valid_cur_node);
558 for (
auto itr = valid_cur_node.
begin(); itr != valid_cur_node.
end(); ++itr){
559 const std::string &entry_name = itr->first;
560 if (valid_cur_node.
isSublist(entry_name)){
563 if (!valid_cur_node_sublist.disableRecursiveReconciliation_){
566 ,
"Error, the parameter {name=\"" << entry_name <<
"\","
568 ",value=\"" << filterValueToString(cur_entry) <<
"\"}"
569 "\nin the parameter (sub)list \"" <<cur_node.
name() <<
"\""
570 "\nwas not found in the list of parameters during reconciliation."
571 "\n\nThe parameters and types are:\n"
575 valid_tmp.push_back(valid_cur_node_sublist);
576 tmp.push_back(cur_node.
sublist(entry_name));
578 valid_tmp.push_front(valid_cur_node_sublist);
579 tmp.push_front(cur_node.
sublist(entry_name));
588 std::deque<std::reference_wrapper<ParameterList>>::reverse_iterator ref, valid_ref;
589 for(ref = refs.rbegin(), valid_ref = valid_refs.rbegin();
590 ref != refs.rend() && valid_ref != valid_refs.rend();
592 if (
nonnull(modifier = valid_ref->
get().getModifier())) {
593 modifier->reconcile(ref->get());
604 typedef std::deque<ListPlusValidList> sublist_list_t;
605 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
608 *out <<
"\n*** Entering ParameterList::validateParametersAndSetDefaults(...) "
609 "for this->name()=\""<<this->
name()<<
"\"...\n";
616 sublist_list_t sublist_list;
619 for (itr = this->nonconstBegin(); itr != this->nonconstEnd(); ++itr) {
620 const std::string &entryName = this->
name(itr);
622 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
624 *out <<
"\nentryName=\""<<entryName<<
"\"\n";
629 ,
"Error, the parameter {name=\""<<entryName<<
"\","
631 ",value=\""<<filterValueToString(theEntry)<<
"\"}"
632 "\nin the parameter (sub)list \""<<this->
name()<<
"\""
633 "\nwas not found in the list of valid parameters!"
634 "\n\nThe valid parameters and types are:\n"
639 validator->validateAndModify(entryName, this->
name(), &theEntry);
643 const bool validType =
650 ,
"Error, the parameter {name=\""<<entryName<<
"\","
652 ",value=\""<<filterValueToString(theEntry)<<
"\"}"
653 "\nin the parameter (sub)list \""<<this->
name()<<
"\""
654 "\nexists in the list of valid parameters but has the wrong type."
655 "\n\nThe correct type is \""
661 if( theEntry.
isList() && depth > 0 ) {
662 sublist_list.push_back(
664 &getValue<ParameterList>(theEntry),
665 &getValue<ParameterList>(*validEntry)
677 for (itr = validParamList.
begin(); itr != validParamList.
end(); ++itr) {
678 const std::string &validEntryName = validParamList.
name(itr);
693 this->
setEntry(validEntryName,newEntry);
702 sublist_list_t::iterator sl_itr = sublist_list.begin();
703 sl_itr != sublist_list.end();
707 if (!sl_itr->validList->disableRecursiveValidation_) {
708 sl_itr->list->validateParametersAndSetDefaults(*sl_itr->validList,depth-1);
711 #ifdef TEUCHOS_PARAMETER_LIST_SHOW_TRACE
712 *out <<
"\n*** Existing ParameterList::validateParametersAndSetDefaults(...) "
713 "for this->name()=\""<<this->
name()<<
"\"...\n";
721 void ParameterList::updateSubListNames(
int depth)
723 const std::string this_name = this->
name();
725 for( itr = this->nonconstBegin(); itr != this->nonconstEnd(); ++itr ) {
726 const std::string &entryName = this->
name(itr);
729 ParameterList &sublistEntry = getValue<ParameterList>(theEntry);
730 sublistEntry.
setName(this_name+std::string(
"->")+entryName);
732 sublistEntry.updateSubListNames(depth-1);
738 void ParameterList::validateEntryExists(
739 const std::string & ,
const std::string &name_in,
740 const ParameterEntry *entry_in
744 entry_in==NULL, Exceptions::InvalidParameterName
745 ,
"Error! The parameter \""<<name_in<<
"\" does not exist"\
746 "\nin the parameter (sub)list \""<<this->
name()<<
"\"."
747 "\n\nThe current parameters set in (sub)list \""<<this->
name()<<
"\" are:\n\n"
753 void ParameterList::validateEntryIsList(
754 const std::string &name_in,
const ParameterEntry &entry_in
758 !entry_in.isList(), Exceptions::InvalidParameterType
759 ,
"Error, the parameter \"" << name_in <<
"\" is not a list, it is of type \""
760 <<entry_in.getAny(
false).typeName()<<
"\"!" );
764 void ParameterList::validateMissingSublistMustExist(
const std::string &baselist_name,
765 const std::string &sublist_name,
const bool mustAlreadyExist)
const
768 mustAlreadyExist, Exceptions::InvalidParameterName
769 ,
"The sublist "<<baselist_name<<
"->\""<<sublist_name<<
"\" does not exist!"
777 bool Teuchos::operator==(
const ParameterList& list1,
const ParameterList& list2 )
785 if (!Teuchos::haveSameModifiers(list1, list2)){
788 ParameterList::ConstIterator itr1, itr2;
790 itr1 = list1.begin(), itr2 = list2.begin();
791 itr1 != list1.end() && itr2 != list2.end();
795 const std::string &entryName1 = list1.name(itr1);
796 const std::string &entryName2 = list2.name(itr2);
797 const ParameterEntry &entry1 = list1.entry(itr1);
798 const ParameterEntry &entry2 = list2.entry(itr2);
799 if( entryName1 != entryName2 ) {
802 else if( entry1 != entry2 ) {
811 if ((itr1 != list1.end()) || (itr2 != list2.end())) {
818 bool Teuchos::haveSameModifiers(
const ParameterList &list1,
const ParameterList &list2) {
820 ParameterList::ConstIterator itr1, itr2;
822 itr1 = list1.begin(), itr2 = list2.begin();
823 itr1 != list1.end() && itr2 != list2.end();
829 if( modifier1 != modifier2 ) {
836 Teuchos::getValue<ParameterList>(entry2) ) ){
845 bool Teuchos::haveSameValues(
const ParameterList& list1,
const ParameterList& list2,
bool verbose )
853 ParameterList::ConstIterator itr1, itr2;
855 itr1 = list1.begin(), itr2 = list2.begin();
856 itr1 != list1.end() && itr2 != list2.end();
860 const std::string &entryName1 = list1.name(itr1);
861 const std::string &entryName2 = list2.name(itr2);
862 const ParameterEntry &entry1 = list1.entry(itr1);
863 const ParameterEntry &entry2 = list2.entry(itr2);
864 if( entryName1 != entryName2 ) {
865 if (verbose) std::cerr <<
"entryName1 \"" << entryName1 <<
"\" != entryName2 \"" << entryName2 <<
"\"\n";
868 if( entry1.isList() && entry2.isList() ) {
871 getValue<ParameterList>(entry1),
872 getValue<ParameterList>(entry2),
880 if (verbose) std::cerr <<
"sublists \"" << entryName1 <<
"\" differ\n";
885 if( entry1.getAny() != entry2.getAny() ) {
886 if (verbose) std::cerr <<
"for key \"" << entryName1 <<
"\", value \"" << entry1.
getAny() <<
"\" != \"" << entry2.getAny() <<
"\"\n";
892 if ((itr1 != list1.end()) || (itr2 != list2.end())) {
893 if (verbose) std::cerr <<
"lists are not the same size\n";
900 bool Teuchos::haveSameValuesSorted(
const ParameterList& list1,
const ParameterList& list2,
bool verbose )
908 ParameterList::ConstIterator itr1, itr2;
909 Array<std::string> arr1, arr2;
910 for(itr1 = list1.begin(); itr1 != list1.end(); ++itr1){
911 arr1.push_back(list1.name(itr1));
913 for(itr2 = list2.begin(); itr2 != list2.end(); ++itr2){
914 arr2.push_back(list2.name(itr2));
917 if (arr1.size() != arr2.size()) {
918 if (verbose) std::cerr <<
"lists are not the same size\n";
921 std::sort(arr1.begin(), arr1.end());
922 std::sort(arr2.begin(), arr2.end());
925 iarr1 = arr1.begin(), iarr2 = arr2.begin();
926 iarr1 != arr1.end() && iarr2 != arr2.end();
930 const std::string &entryName1 = *iarr1;
931 const std::string &entryName2 = *iarr2;
932 const ParameterEntry &entry1 = list1.getEntry(entryName1);
933 const ParameterEntry &entry2 = list2.getEntry(entryName2);
934 if( entryName1 != entryName2 ) {
935 if (verbose) std::cerr <<
"entryName1 \"" << entryName1 <<
"\" != entryName2 \"" << entryName2 <<
"\"\n";
938 if( entry1.isList() && entry2.isList() ) {
941 getValue<ParameterList>(entry1),
942 getValue<ParameterList>(entry2),
950 if (verbose) std::cerr <<
"sublists \"" << entryName1 <<
"\" differ\n";
955 if( entry1.getAny() != entry2.getAny() ) {
956 if (verbose) std::cerr <<
"for key \"" << entryName1 <<
"\", value \"" << entry1.getAny() <<
"\" != \"" << entry2.getAny() <<
"\"\n";
Templated Parameter List class.
A std::string utilities class for Teuchos.
std::vector< std::string >::iterator iterator
The type of a forward iterator.
This object is held as the "value" in the Teuchos::ParameterList std::map.
void setValidator(RCP< const ParameterEntryValidator > const &validator)
Set the validator.
bool isUsed() const
Return whether or not the value has been used; i.e., whether or not the value has been retrieved via ...
any & getAny(bool activeQry=true)
Direct access to the Teuchos::any data value underlying this object. The bool argument activeQry (def...
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 docString() const
Return the (optional) documentation std::string.
bool isList() const
Return whether or not the value itself is a list.
void setAnyValue(const any &value, bool isDefault=false)
Set the value as an any object.
bool isDefault() const
Indicate whether this entry takes on the default value.
Utility class for setting and passing in print options.
A list of parameters of arbitrary type.
void reconcileParameterList(ParameterList &validParamList, const bool left_to_right=true)
Reconcile a parameter list after validation.
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.
ParameterList & setParameters(const ParameterList &source)
const ParameterEntry & entry(ConstIterator i) const
Access to ParameterEntry (i.e., returns i->second)
ConstIterator end() const
An iterator pointing beyond the last entry.
virtual ~ParameterList()
Destructor.
Ordinal numParams() const
Get the number of stored parameters.
ParameterList & setParametersNotAlreadySet(const ParameterList &source)
RCP< const ParameterListModifier > getModifier() const
Return the optional modifier object.
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.
void unused(std::ostream &os) const
Print out unused parameters in the ParameterList.
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...
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,...
void print() const
Print function to use in debugging in a debugger.
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.
const std::string & name() const
The name of this ParameterList.
ParameterList()=default
Constructor.
void modifyParameterList(ParameterList &validParamList, int const depth=1000)
Modify the valid parameter list prior to validation.
bool isSublist(const std::string &name) const
Whether the given sublist exists in this list.
ParameterList & disableRecursiveValidation()
ParameterList & disableRecursiveReconciliation()
ParameterList & operator=(const ParameterList &source)
Replace the current parameter list with source.
ParameterEntry * getEntryPtr(const std::string &name)
Retrieves the pointer for an entry with the name name if it exists.
params_t::ConstIterator ConstIterator
Parameter container const iterator typedef.
ConstIterator begin() const
An iterator pointing to the first entry.
ParameterList & disableRecursiveModification()
std::string currentParametersString() const
Create a single formated std::string of all of the zero-level parameters in this list.
ParameterList & setEntry(const std::string &name, const ParameterEntry &entry)
Set a parameter directly as a ParameterEntry.
bool isParameter(const std::string &name) const
Whether the given parameter exists in this list.
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT bool haveSameModifiers(const ParameterList &list1, const ParameterList &list2)
Returns true if two parameter lists have the same modifiers.
ParameterList & setName(const std::string &name)
Set the name of *this list.
ParameterList & disableRecursiveAll()
bool remove(std::string const &name, bool throwIfNotExists=true)
Remove a parameter (does not depend on the type of the parameter).
EValidateDefaults
Validation defaults enum.
EValidateUsed
Validation used enum.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists.
Smart reference counting pointer class for automatic garbage collection.
T * get() const
Get the raw C++ pointer to the underlying object.
static std::ostream & printLines(std::ostream &os, const std::string &linePrefix, const std::string &lines)
Print lines with prefix first.
Base types for StringIndexedOrderedValueObjectContainer.
Ordinal getObjOrdinalIndex(const std::string &key) const
Get the ordinal index given the string key.
Ordinal numObjects() const
void removeObj(const Ordinal &idx)
Remove an object given its ordinal index.
Ptr< ObjType > getNonconstObjPtr(const Ordinal &idx)
Get a nonconst semi-persisting association with the stored object indexed by ordinal.
Ordinal setObj(const std::string &key, const ObjType &obj)
Set (or reset) object by value and return its ordinal index.
Ptr< const ObjType > getObjPtr(const Ordinal &idx) const
Get a const semi-persisting association with the stored object indexed by ordinal.
static RCP< FancyOStream > getDefaultOStream()
Get the default output stream object.
std::string toString(const any &rhs)
Converts the value in any to a std::string.
const std::type_info & type() const
Return the type of value being stored.
std::string typeName() const
Return the name of the type.
Tabbing class for helping to create formated, indented output for a basic_FancyOStream object.
#define TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
basic_OSTab< char > OSTab
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
@ VALIDATE_DEFAULTS_ENABLED
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.