14#include "Teuchos_FancyOStream.hpp"
16#include "Teuchos_VerboseObject.hpp"
24 return ( entry.
isList() ? std::string(
"...") : toString(entry.
getAny()) );
28struct ListPlusValidList {
29 Teuchos::ParameterList *list;
30 Teuchos::ParameterList *validList;
32 Teuchos::ParameterList *_list
33 ,Teuchos::ParameterList *_validList
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_;
72 return params_.numObjects();
81 params_ = source.params_;
82 disableRecursiveValidation_ = source.disableRecursiveValidation_;
83 disableRecursiveModification_= source.disableRecursiveModification_;
84 disableRecursiveReconciliation_ = source.disableRecursiveReconciliation_;
85 modifier_ = source.modifier_;
90void 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";
206 const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
207 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
208 return params_.getObjPtr(param_idx)->isList();
217 const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
218 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
226 std::string
const& name_in,
bool throwIfNotExists
230 const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
231 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
233 params_.removeObj(param_idx);
237 if (throwIfNotExists) {
238 validateEntryExists(
"get", name_in, 0);
245 const std::string& name_in,
bool mustAlreadyExist,
246 const std::string& docString
251 const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
255 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
257 sublist_entry_ptr = params_.getNonconstObjPtr(param_idx);
258 validateEntryIsList(name_in, *sublist_entry_ptr);
262 validateMissingSublistMustExist(this->
name(), name_in, mustAlreadyExist);
263 const Ordinal new_param_idx =
273 sublist_entry_ptr = params_.getNonconstObjPtr(new_param_idx);
282 const std::string& docString
288 ,
"The parameter "<<this->
name()<<
"->\""<<name_in<<
"\" already exists."
291 subpl.setModifier(modifier_in);
300 const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
301 if (param_idx == SIOVOCB::getInvalidOrdinal()) {
302 validateMissingSublistMustExist(this->
name(), name_in,
true);
306 validateEntryIsList(name_in, *sublist_entry_ptr);
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,
' ');
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 ) {
377std::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(
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"
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(
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";
705void 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);
714 sublistEntry.
setName(this_name+std::string(
"->")+entryName);
716 sublistEntry.updateSubListNames(depth-1);
722void ParameterList::validateEntryExists(
723 const std::string & ,
const std::string &name_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"
737void ParameterList::validateEntryIsList(
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()<<
"\"!" );
748void 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!"
761bool Teuchos::operator==(
const ParameterList& list1,
const ParameterList& 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())) {
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()){
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";
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());
916 Array<std::string>::iterator iarr1, iarr2;
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";
Templated Parameter List class.
A std::string utilities class for Teuchos.
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.
T & getValue(const ParameterEntry &entry)
A templated helper function for returning the value of type T held in the ParameterEntry 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 & setEntry(const std::string &name, U &&entry)
Set a parameter directly as a ParameterEntry.
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.
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.
static RCP< FancyOStream > getDefaultOStream()
Get the default output stream object.
ValueType & any_cast(any &operand)
Used to extract the templated value held in Teuchos::any to a given value type.
std::string typeName() const
Return the name of the type.
const std::type_info & type() const
Return the type of value being stored.
RCP< basic_FancyOStream< CharT, Traits > > tab(const RCP< basic_FancyOStream< CharT, Traits > > &out, const int tabs=1, const std::basic_string< CharT, Traits > linePrefix="")
Create a tab for an RCP-wrapped basic_FancyOStream object to cause the indentation of all output auto...
RCP< basic_FancyOStream< char > > getFancyOStream(const RCP< std::basic_ostream< char > > &out)
Get a FancyOStream from an std::ostream 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 is_null(const boost::shared_ptr< T > &p)
Returns true if p.get()==NULL.
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.