10 #ifndef TEUCHOS_PARAMETER_LIST_H
11 #define TEUCHOS_PARAMETER_LIST_H
17 #include "TeuchosParameterList_config.h"
18 #include "Teuchos_ParameterListExceptions.hpp"
21 #include "Teuchos_StringIndexedOrderedValueObjectContainer.hpp"
22 #include "Teuchos_Assert.hpp"
120 PrintOptions() : indent_(0), showTypes_(
false), showFlags_(
false), showDoc_(
false), showDefault_(
true) {}
121 PrintOptions& indent(
int _indent) { indent_ = _indent;
return *
this; }
122 PrintOptions& showTypes(
bool _showTypes) { showTypes_ = _showTypes;
return *
this; }
123 PrintOptions& showFlags(
bool _showFlags) { showFlags_ = _showFlags;
return *
this; }
124 PrintOptions& showDoc(
bool _showDoc) { showDoc_ = _showDoc;
return *
this; }
125 PrintOptions& showDefault(
bool _showDefault) { showDefault_ = _showDefault;
return *
this; }
126 PrintOptions& incrIndent(
int indents) { indent_ += indents;
return *
this; }
127 int indent()
const {
return indent_; }
128 bool showTypes()
const {
return showTypes_; }
129 bool showFlags()
const {
return showFlags_; }
130 bool showDoc()
const {
return showDoc_; }
131 bool showDefault()
const {
return showDefault_; }
255 std::string
const& docString =
"",
263 std::string
const& name,
char value[], std::string
const& docString =
"",
272 std::string
const& name,
const char value[], std::string
const& docString =
"",
280 std::string
const& name,
ParameterList const& value, std::string
const& docString =
""
287 template <
typename U,
typename = std::enable_if_t<std::is_same_v<std::decay_t<U>, ParameterEntry>>>
297 int const depth = 1000);
320 T&
get(
const std::string& name, T def_value);
325 std::string&
get(
const std::string& name,
char def_value[]);
330 std::string&
get(
const std::string& name,
const char def_value[]);
363 T&
get (
const std::string& name);
393 const T&
get (
const std::string& name)
const;
402 T* getPtr(
const std::string& name);
411 const T* getPtr(
const std::string& name)
const;
473 std::string
const& name,
bool throwIfNotExists = true
487 const std::string& name,
bool mustAlreadyExist =
false,
488 const std::string& docString =
""
497 const std::string& docString =
""
512 const std::string& name()
const;
518 bool isParameter (
const std::string& name)
const;
524 bool isSublist (
const std::string& name)
const;
532 bool isType (
const std::string& name)
const;
534 #ifndef DOXYGEN_SHOULD_SKIP_THIS
546 bool isType(
const std::string& name, T* ptr)
const;
550 Ordinal numParams ()
const;
566 std::ostream& print(std::ostream& os,
const PrintOptions &printOptions)
const;
570 std::ostream& print(std::ostream& os,
int indent = 0,
bool showTypes =
false,
bool showFlags =
true,
bool showDefault =
true )
const;
573 void unused(std::ostream& os)
const;
576 std::string currentParametersString()
const;
644 void validateParameters(
646 int const depth = 1000,
688 void validateParametersAndSetDefaults(
690 int const depth = 1000
707 void modifyParameterList(
ParameterList &validParamList,
int const depth = 1000);
720 const bool left_to_right =
true);
733 void validateEntryExists(
const std::string &funcName,
const std::string &name,
738 void validateEntryType(
const std::string &funcName,
const std::string &name,
741 void validateEntryIsList(
const std::string &name,
const ParameterEntry &entry)
const;
743 void validateMissingSublistMustExist(
const std::string &baselist_name,
744 const std::string &sublist_name,
const bool mustAlreadyExist)
const;
746 void updateSubListNames(
int depth = 0);
751 std::string name_ =
"ANONYMOUS";
765 bool disableRecursiveValidation_ =
false;
768 bool disableRecursiveModification_ =
false;
771 bool disableRecursiveReconciliation_ =
false;
839 static std::string name() {
return "ParameterList"; }
859 return !( list1 == list2 );
870 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT
bool haveSameModifiers (
const ParameterList& list1,
888 bool verbose =
false);
902 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT
bool haveSameValuesSorted(
const ParameterList& list1,
924 std::string
const& name_in, T
const& value_in, std::string
const& docString_in,
930 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
932 const std::string docString =
933 (docString_in.length() ? docString_in : param->docString());
935 (
nonnull(validator_in) ? validator_in : param->validator());
937 ParameterEntry param_new(value_in,
false,
false, docString, validator );
939 validator->validate(param_new, name_in, this->
name());
945 ParameterEntry param_new(value_in,
false,
false, docString_in, validator_in);
947 param_new.
validator()->validate(param_new, name_in, this->
name());
949 params_.
setObj(name_in, param_new);
957 std::string
const& name_in,
char value[], std::string
const& docString
960 {
return set(name_in, std::string(value), docString, validator); }
965 const std::string& name_in,
const char value[],
const std::string &docString
968 {
return set( name_in, std::string(value), docString, validator ); }
973 std::string
const& name_in,
ParameterList const& value, std::string
const&
981 template <
typename U,
typename>
985 params_.
setObj(name_in, std::forward<U>(entry_in));
994 for (itr = this->
begin(); itr != this->
end(); ++itr){
995 const std::string &entry_name = itr->first;
996 if (this->
isSublist(entry_name) && depth > 0){
1000 if (theEntry->
isType<T>()){
1011 template<
typename T>
1016 if (param_idx == SIOVOCB::getInvalidOrdinal()) {
1021 this->
template validateEntryType<T>(
"get", name_in, *param);
1022 return getValue<T>(*param);
1028 {
return get(name_in, std::string(def_value)); }
1033 {
return get(name_in, std::string(def_value)); }
1036 template<
typename T>
1040 validateEntryExists(
"get",name_in,foundEntry);
1041 this->
template validateEntryType<T>(
"get",name_in,*foundEntry);
1042 return getValue<T>(*foundEntry);
1046 template<
typename T>
1050 validateEntryExists(
"get",name_in,foundEntry);
1051 this->
template validateEntryType<T>(
"get",name_in,*foundEntry);
1052 return getValue<T>(*foundEntry);
1056 template<
typename T>
1062 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
1064 if (param_ptr->isType<T>()) {
1065 return ¶m_ptr->getValue<T>(0);
1077 template<
typename T>
1083 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
1085 if (param_ptr->isType<T>()) {
1086 return ¶m_ptr->getValue<T>(0);
1099 validateEntryExists(
"get", name_in, foundEntry);
1108 validateEntryExists(
"get", name_in, foundEntry);
1119 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
1132 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
1144 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
1156 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
1157 return rcpFromPtr(params_.
getObjPtr(param_idx));
1165 {
return modifier_; }
1178 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1179 template<
typename T>
1184 if (param_idx != SIOVOCB::getInvalidOrdinal()) {
1185 return params_.
getObjPtr(param_idx)->isType<T>();
1192 template<
typename T>
1195 return this->
isType(name_in, static_cast<T*>(0));
1204 return params_.
begin();
1210 return params_.
end();
1235 inline ParameterList::Iterator ParameterList::nonconstEnd()
1241 inline ParameterEntry& ParameterList::nonconstEntry(Iterator i)
1247 template<
typename T>
1249 void ParameterList::validateEntryType(
1250 const std::string &,
const std::string &name_in,
1251 const ParameterEntry &entry_in
1255 entry_in.getAny().type() !=
typeid(T), Exceptions::InvalidParameterType
1256 ,
"Error! An attempt was made to access parameter \""<<name_in<<
"\""
1257 " of type \""<<entry_in.getAny().typeName()<<
"\""
1258 "\nin the parameter (sub)list \""<<this->
name()<<
"\""
1274 template<
typename T>
1277 return l.template get<T>(name);
1286 template<
typename T>
1290 return getParameter<T>(l,name);
1300 template<
typename T>
1303 return l.template get<T>(name);
1314 template<
typename T>
1318 return l.template getPtr<T>(name);
1329 template<
typename T>
1333 return l.template getPtr<T>(name);
1343 template<
typename T>
1347 return l.
isType( name, (T*)NULL );
1357 template<
typename T>
1361 return l.
isType( name, (T*)NULL );
1376 template<
typename T>
1378 const std::string ¶mName
1384 paramList->
set(paramName,toString(array));
1452 template<
typename T>
1455 ,
const std::string ¶mName
1456 ,
const int arrayDim = -1
1457 ,
const bool mustExist =
true
1460 std::string arrayStr;
1462 arrayStr = getParameter<std::string>(paramList,paramName);
1466 *arrayStrPtr = getParameterPtr<std::string>(paramList,paramName);
1468 arrayStr = *arrayStrPtr;
1476 a = fromStringToArray<T>(arrayStr);
1481 ,
"Error! The parameter \""<<paramName<<
"\"\n"
1482 "in the sublist \""<<paramList.
name()<<
"\"\n"
1483 "exists, but the std::string value:\n"
1487 "is not a valid array represntation!"
1491 ( ( a.
size()>0 && arrayDim>=0 ) && static_cast<int>(a.
size())!=arrayDim )
1493 ,
"Error! The parameter \""<<paramName<<
"\"\n"
1494 "in the sublist \""<<paramList.
name()<<
"\"\n"
1495 "exists and is a valid array, but the dimension of\n"
1496 "the read in array a.size() = " << a.
size() <<
"\n"
1497 "was not equal to the expected size arrayDim = " << arrayDim <<
"!"
1515 template<
typename T>
1519 bool param_exists =
false;
1520 bool overwrite =
false;
1521 if (paramName == newName){
1525 param_exists =
true;
1527 "The parameter " << paramName <<
" is not of type " <<
typeid(T).name());
1529 std::logic_error,
"The parameter " << newName <<
" already exists in this "
1533 pl.
set(newName, params);
1535 return param_exists;
1545 bool mustAlreadyExist =
false,
const std::string& docString =
""
1548 return rcpWithEmbeddedObjPostDestroy(
1549 ¶mList->
sublist(name, mustAlreadyExist, docString), paramList, false );
1561 return rcpWithEmbeddedObjPostDestroy(
1562 ¶mList->
sublist(name), paramList, false );
void print() const
Print function to use in debugging in a debugger.
bool replaceParameterWithArray(const std::string ¶mName, const std::string &newName, ParameterList &pl)
Replace a parameter with an array containing the parameter.
Object held as the "value" in the Teuchos::ParameterList std::map.
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.
Array< T > getArrayFromStringParameter(const ParameterList ¶mList, const std::string ¶mName, const int arrayDim=-1, const bool mustExist=true)
Get an Array object (with entries of type T) from a parameter holding a std::string representation of...
RCP< ParameterEntry > getEntryRCP(const std::string &name)
Retrieves the RCP for an entry with the name name if it exists.
void setValidator(RCP< const ParameterEntryValidator > const &validator)
Set the validator.
RCP< const ParameterEntryValidator > validator() const
Return the (optional) validator object.
RCP< ParameterList > createParameterList()
Nonmember constructor.
RCP< ParameterList > createParameterList(const std::string &name)
Nonmember constructor.
T & get(const std::string &name, T def_value)
Return the parameter's value, or the default value if it is not there.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Set a parameter whose value has type T.
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.
bool isParameterType(ParameterList &l, const std::string &name)
A templated helper function for determining the type of a parameter entry for a non-const list...
Ordinal setObj(const std::string &key, U &&obj)
Set (or reset) object by value and return its ordinal index.
bool isType() const
Test the type of the data being contained.
RCP< ParameterList > sublist(const RCP< ParameterList > ¶mList, const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Return a RCP to a sublist in another RCP-ed parameter list.
void recursivelySetValidator(RCP< const ParameterEntryValidator > const &validator, int const depth=1000)
Recursively attach a validator to parameters of type T.
const T * getParameterPtr(const ParameterList &l, const std::string &name)
A templated helper function for getting a pointer to a parameter from a non-const list...
ParameterEntry * getEntryPtr(const std::string &name)
Retrieves the pointer for an entry with the name name if it exists.
RCP< ParameterList > parameterList()
Nonmember constructor.
ConstIterator end() const
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).
std::ostream & operator<<(std::ostream &os, const ParameterList &l)
Output stream operator for handling the printing of the parameter list.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
bool isSublist(const std::string &name) const
Whether the given sublist exists in this list.
bool isParameterType(const ParameterList &l, const std::string &name)
A templated helper function for determining the type of a parameter entry for a const list...
bool operator!=(const ParameterList &list1, const ParameterList &list2)
Returns true if two parameter lists are not the same.
RCP< ParameterList > parameterList(const ParameterList &source)
Nonmember constructor.
params_t::ConstIterator ConstIterator
Parameter container const iterator typedef.
const T & getParameter(const ParameterList &l, const std::string &name)
A templated helper function for getting a parameter from a const list. This helper function prevents ...
Utility class for setting and passing in print options.
T & getParameter(ParameterList &l, const std::string &name)
A templated helper function for getting a parameter from a non-const list. This helper function preve...
ConstIterator begin() const
An iterator pointing to the first entry.
RCP< const ParameterList > sublist(const RCP< const ParameterList > ¶mList, const std::string &name)
Return a RCP to a sublist in another RCP-ed parameter list.
T * getParameterPtr(ParameterList &l, const std::string &name)
A templated helper function for getting a pointer to a parameter from a non-const list...
Ptr< const ObjType > getObjPtr(const Ordinal &idx) const
Get a const semi-persisting association with the stored object indexed by ordinal.
T * getPtr(const std::string &name)
Retrieves the pointer for parameter name of type T from a list. A null pointer is returned if this pa...
A list of parameters of arbitrary type.
Provides std::map class for deficient platforms.
Parameter List Modifier class.
const ParameterEntry & entry(ConstIterator i) const
Access to ParameterEntry (i.e., returns i->second)
Templated array class derived from the STL std::vector.
RCP< const ParameterListModifier > getModifier() const
Return the optional modifier object.
Default traits class that just returns typeid(T).name().
ConstIterator begin() const
void setStringParameterFromArray(const std::string ¶mName, const Array< T > &array, ParameterList *paramList)
Set a std::string parameter representation of an array.
bool isType(const std::string &name) const
Whether the given parameter exists in this list and has type T.
EValidateDefaults
Validation defaults enum.
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.
ParameterEntry & getEntry(const std::string &name)
Retrieves an entry with the name name.
Ptr< ObjType > getNonconstObjPtr(const Ordinal &idx)
Get a nonconst semi-persisting association with the stored object indexed by ordinal.
EValidateUsed
Validation used enum.
RCP< ParameterList > parameterList(const std::string &name)
Nonmember constructor.
Reference-counted pointer class and non-member templated function implementations.
Base types for StringIndexedOrderedValueObjectContainer.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call...
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...
static std::string name()
#define TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...