Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_ParameterList.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 
43 #ifndef TEUCHOS_PARAMETER_LIST_H
44 #define TEUCHOS_PARAMETER_LIST_H
45 
50 #include "Teuchos_ParameterListExceptions.hpp"
53 #include "Teuchos_StringIndexedOrderedValueObjectContainer.hpp"
54 #include "Teuchos_Assert.hpp"
55 #include "Teuchos_RCP.hpp"
56 #include "Teuchos_Array.hpp"
57 #include "Teuchos_map.hpp"
58 
59 
64 namespace Teuchos {
65 
76 };
77 
88 };
89 
133 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT ParameterList {
134 
137 
140 
141 public:
142 
144 
145 
148 
150  class PrintOptions {
151  public:
152  PrintOptions() : indent_(0), showTypes_(false), showFlags_(false), showDoc_(false), showDefault_(true) {}
153  PrintOptions& indent(int _indent) { indent_ = _indent; return *this; }
154  PrintOptions& showTypes(bool _showTypes) { showTypes_ = _showTypes; return *this; }
155  PrintOptions& showFlags(bool _showFlags) { showFlags_ = _showFlags; return *this; }
156  PrintOptions& showDoc(bool _showDoc) { showDoc_ = _showDoc; return *this; }
157  PrintOptions& showDefault(bool _showDefault) { showDefault_ = _showDefault; return *this; }
158  PrintOptions& incrIndent(int indents) { indent_ += indents; return *this; }
159  int indent() const { return indent_; }
160  bool showTypes() const { return showTypes_; }
161  bool showFlags() const { return showFlags_; }
162  bool showDoc() const { return showDoc_; }
163  bool showDefault() const { return showDefault_; }
164  PrintOptions copy() const { return PrintOptions(*this); }
165  private:
166  int indent_;
167  bool showTypes_;
168  bool showFlags_;
169  bool showDoc_;
170  bool showDefault_;
171  };
172 
174 
176 
177 
179  ParameterList() = default;
180 
182  ParameterList(const std::string &name,
183  RCP<const ParameterListModifier> const& modifier = null);
184 
186  ParameterList(const ParameterList& source);
187 
189  virtual ~ParameterList();
190 
192 
194 
196  ParameterList& setName( const std::string &name );
197 
201  ParameterList& operator= (const ParameterList& source);
202 
203  void setModifier(
204  RCP<const ParameterListModifier> const& modifier
205  );
206 
215  ParameterList& setParameters(const ParameterList& source);
216 
226  ParameterList& setParametersNotAlreadySet(const ParameterList& source);
227 
235  ParameterList& disableRecursiveValidation();
236 
245  ParameterList& disableRecursiveModification();
246 
255  ParameterList& disableRecursiveReconciliation();
256 
264  ParameterList& disableRecursiveAll();
265 
284  template<typename T>
285  ParameterList& set (std::string const& name,
286  T const& value,
287  std::string const& docString = "",
288  RCP<const ParameterEntryValidator> const& validator = null);
289 
294  ParameterList& set(
295  std::string const& name, char value[], std::string const& docString = "",
296  RCP<const ParameterEntryValidator> const& validator = null
297  );
298 
303  ParameterList& set(
304  std::string const& name, const char value[], std::string const& docString = "",
305  RCP<const ParameterEntryValidator> const& validator = null
306  );
307 
311  ParameterList& set(
312  std::string const& name, ParameterList const& value, std::string const& docString = ""
313  );
314 
319  template <typename U, typename = std::enable_if_t<std::is_same_v<std::decay_t<U>, ParameterEntry>>>
320  ParameterList& setEntry(const std::string& name, U&& entry);
321 
327  template<typename T>
328  void recursivelySetValidator(RCP<const ParameterEntryValidator> const& validator,
329  int const depth = 1000);
330 
332 
334 
351  template<typename T>
352  T& get(const std::string& name, T def_value);
353 
357  std::string& get(const std::string& name, char def_value[]);
358 
362  std::string& get(const std::string& name, const char def_value[]);
363 
394  template<typename T>
395  T& get (const std::string& name);
396 
424  template<typename T>
425  const T& get (const std::string& name) const;
426 
432  template<typename T>
433  inline
434  T* getPtr(const std::string& name);
435 
441  template<typename T>
442  inline
443  const T* getPtr(const std::string& name) const;
444 
445  // ToDo: Add getSafePtr() functions to return Ptr<T> instead of raw T*
446 
452  ParameterEntry& getEntry(const std::string& name);
453 
459  inline
460  const ParameterEntry& getEntry(const std::string& name) const;
461 
464  inline
465  ParameterEntry* getEntryPtr(const std::string& name);
466 
467  // ToDo: Add function called getEntrySafePtr() to return Ptr<> as the main
468  // implementation and deprecate getEntryPtr()
469 
472  inline
473  const ParameterEntry* getEntryPtr(const std::string& name) const;
474 
477  inline RCP<ParameterEntry> getEntryRCP(const std::string& name);
478 
481  inline RCP<const ParameterEntry> getEntryRCP(const std::string& name) const;
482 
484  inline RCP<const ParameterListModifier> getModifier() const;
485 
487 
489 
490 
504  bool remove(
505  std::string const& name, bool throwIfNotExists = true
506  );
507 
509 
511 
512 
518  ParameterList& sublist(
519  const std::string& name, bool mustAlreadyExist = false,
520  const std::string& docString = ""
521  );
522 
527  ParameterList& sublist(
528  const std::string& name, RCP<const ParameterListModifier> const& modifier,
529  const std::string& docString = ""
530  );
531 
536  const ParameterList& sublist(const std::string& name) const;
537 
539 
541 
542 
544  const std::string& name() const;
545 
550  bool isParameter (const std::string& name) const;
551 
556  bool isSublist (const std::string& name) const;
557 
563  template<typename T>
564  bool isType (const std::string& name) const;
565 
566 #ifndef DOXYGEN_SHOULD_SKIP_THIS
567 
577  template<typename T>
578  bool isType(const std::string& name, T* ptr) const;
579 #endif
580 
582  Ordinal numParams () const;
583 
585 
587 
588 
594  void print() const;
595 
598  std::ostream& print(std::ostream& os, const PrintOptions &printOptions) const;
599 
602  std::ostream& print(std::ostream& os, int indent = 0, bool showTypes = false, bool showFlags = true, bool showDefault = true ) const;
603 
605  void unused(std::ostream& os) const;
606 
608  std::string currentParametersString() const;
609 
611 
613 
614 
616  inline ConstIterator begin() const;
617 
619  inline ConstIterator end() const;
620 
622  inline const std::string& name(ConstIterator i) const;
623 
625  inline const ParameterEntry& entry(ConstIterator i) const;
626 
628 
630 
631 
676  void validateParameters(
677  ParameterList const& validParamList,
678  int const depth = 1000,
679  EValidateUsed const validateUsed = VALIDATE_USED_ENABLED,
680  EValidateDefaults const validateDefaults = VALIDATE_DEFAULTS_ENABLED
681  ) const;
682 
720  void validateParametersAndSetDefaults(
721  ParameterList const& validParamList,
722  int const depth = 1000
723  );
724 
739  void modifyParameterList(ParameterList &validParamList, int const depth = 1000);
740 
751  void reconcileParameterList(ParameterList &validParamList,
752  const bool left_to_right = true);
753 
755 
756 private: // Functions
757 
759  inline Iterator nonconstBegin();
761  inline Iterator nonconstEnd();
763  inline ParameterEntry& nonconstEntry(Iterator i);
765  void validateEntryExists(const std::string &funcName, const std::string &name,
766  const ParameterEntry *entry) const;
767  // ToDo: Change above function to take Ptr<> instead of raw pointer.
769  template<typename T>
770  void validateEntryType(const std::string &funcName, const std::string &name,
771  const ParameterEntry &entry ) const;
773  void validateEntryIsList(const std::string &name, const ParameterEntry &entry) const;
775  void validateMissingSublistMustExist(const std::string &baselist_name,
776  const std::string &sublist_name, const bool mustAlreadyExist) const;
778  void updateSubListNames(int depth = 0);
779 
780 private: // Data members
781 
783  std::string name_ = "ANONYMOUS";
784 
786 //use pragmas to disable some false-positive warnings for windows sharedlibs export
787 //#ifdef _MSC_VER
788 //#pragma warning(push)
789 //#pragma warning(disable:4251)
790 //#endif
791  params_t params_;
792 //#ifdef _MSC_VER
793 //#pragma warning(pop)
794 //#endif
795 
797  bool disableRecursiveValidation_ = false;
798 
800  bool disableRecursiveModification_ = false;
801 
803  bool disableRecursiveReconciliation_ = false;
804 
805  RCP<const ParameterListModifier> modifier_ = null;
806 };
807 
808 
813 inline
815 {
816  return rcp(new ParameterList);
817 }
818 
819 
824 inline
825 RCP<ParameterList> parameterList(const std::string &name)
826 {
827  return rcp(new ParameterList(name));
828 }
829 
830 
835 inline
837 {
838  return rcp(new ParameterList(source));
839 }
840 
841 
846 inline
848 {
849  return rcp(new ParameterList);
850 }
851 
852 
857 inline
858 RCP<ParameterList> createParameterList(const std::string &name)
859 {
860  return rcp(new ParameterList(name));
861 }
862 
863 
868 template<>
869 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT TypeNameTraits<ParameterList> {
870 public:
871  static std::string name() { return "ParameterList"; }
872  static std::string concreteName( const ParameterList& /*t2*/ )
873  { return name(); }
874 };
875 
876 
881 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT bool operator==( const ParameterList& list1, const ParameterList& list2 );
882 
883 
888 inline
889 bool operator!=( const ParameterList& list1, const ParameterList& list2 )
890 {
891  return !( list1 == list2 );
892 }
893 
894 
902 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT bool haveSameModifiers (const ParameterList& list1,
905  const ParameterList& list2);
906 
907 
919 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT bool haveSameValues( const ParameterList& list1, const ParameterList& list2,
920  bool verbose = false);
921 
922 
934 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT bool haveSameValuesSorted( const ParameterList& list1,
935  const ParameterList& list2, bool verbose = false);
936 
937 
938 // /////////////////////////////////////////////////////
939 // Inline and Template Function Definitions
940 
941 
942 inline
943 ParameterList& ParameterList::setName( const std::string &name_in )
944 {
945  name_ = name_in;
946  return *this;
947 }
948 
949 
950 // Set functions
951 
952 
953 template<typename T>
954 inline
956  std::string const& name_in, T const& value_in, std::string const& docString_in,
957  RCP<const ParameterEntryValidator> const& validator_in
958  )
959 {
961  const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
962  if (param_idx != SIOVOCB::getInvalidOrdinal()) {
963  Ptr<ParameterEntry> param = params_.getNonconstObjPtr(param_idx);
964  const std::string docString =
965  (docString_in.length() ? docString_in : param->docString());
966  const RCP<const ParameterEntryValidator> validator =
967  (nonnull(validator_in) ? validator_in : param->validator());
968  // Create temp param to validate before setting
969  ParameterEntry param_new(value_in, false, false, docString, validator );
970  if (nonnull(validator)) {
971  validator->validate(param_new, name_in, this->name());
972  }
973  // Strong guarantee: (if exception is thrown, the value is not changed)
974  *param = param_new;
975  }
976  else {
977  ParameterEntry param_new(value_in, false, false, docString_in, validator_in);
978  if (nonnull(param_new.validator())) {
979  param_new.validator()->validate(param_new, name_in, this->name());
980  }
981  params_.setObj(name_in, param_new);
982  }
983  return *this;
984 }
985 
986 
987 inline
989  std::string const& name_in, char value[], std::string const& docString
990  ,RCP<const ParameterEntryValidator> const& validator
991  )
992 { return set(name_in, std::string(value), docString, validator); }
993 
994 
995 inline
997  const std::string& name_in, const char value[], const std::string &docString
998  ,RCP<const ParameterEntryValidator> const& validator
999  )
1000 { return set( name_in, std::string(value), docString, validator ); }
1001 
1002 
1003 inline
1005  std::string const& name_in, ParameterList const& value, std::string const& /*docString*/
1006  )
1007 {
1008  sublist(name_in) = value;
1009  return *this;
1010 }
1011 
1012 
1013 template <typename U, typename>
1014 inline
1015 ParameterList& ParameterList::setEntry(std::string const& name_in, U&& entry_in)
1016 {
1017  params_.setObj(name_in, std::forward<U>(entry_in));
1018  return *this;
1019 }
1020 
1021 template<typename T>
1023  RCP<const ParameterEntryValidator> const& validator, int const depth)
1024 {
1025  ConstIterator itr;
1026  for (itr = this->begin(); itr != this->end(); ++itr){
1027  const std::string &entry_name = itr->first;
1028  if (this->isSublist(entry_name) && depth > 0){
1029  this->sublist(entry_name).recursivelySetValidator<T>(validator, depth - 1);
1030  } else{
1031  ParameterEntry *theEntry = this->getEntryPtr(entry_name);
1032  if (theEntry->isType<T>()){
1033  theEntry->setValidator(validator);
1034  }
1035  }
1036  }
1037 }
1038 
1039 
1040 // Get functions
1041 
1042 
1043 template<typename T>
1044 T& ParameterList::get(const std::string& name_in, T def_value)
1045 {
1047  Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
1048  if (param_idx == SIOVOCB::getInvalidOrdinal()) {
1049  // Param does not exist
1050  param_idx = params_.setObj(name_in, ParameterEntry(def_value, true));
1051  }
1052  Ptr<ParameterEntry> param = params_.getNonconstObjPtr(param_idx);
1053  this->template validateEntryType<T>("get", name_in, *param);
1054  return getValue<T>(*param);
1055 }
1056 
1057 
1058 inline
1059 std::string& ParameterList::get(const std::string& name_in, char def_value[])
1060 { return get(name_in, std::string(def_value)); }
1061 
1062 
1063 inline
1064 std::string& ParameterList::get(const std::string& name_in, const char def_value[])
1065 { return get(name_in, std::string(def_value)); }
1066 
1067 
1068 template<typename T>
1069 T& ParameterList::get(const std::string& name_in)
1070 {
1071  ParameterEntry *foundEntry = this->getEntryPtr(name_in);
1072  validateEntryExists("get",name_in,foundEntry);
1073  this->template validateEntryType<T>("get",name_in,*foundEntry);
1074  return getValue<T>(*foundEntry);
1075 }
1076 
1077 
1078 template<typename T>
1079 const T& ParameterList::get(const std::string& name_in) const
1080 {
1081  const ParameterEntry *foundEntry = this->getEntryPtr(name_in);
1082  validateEntryExists("get",name_in,foundEntry);
1083  this->template validateEntryType<T>("get",name_in,*foundEntry);
1084  return getValue<T>(*foundEntry);
1085 }
1086 
1087 
1088 template<typename T>
1089 inline
1090 T* ParameterList::getPtr(const std::string& name_in)
1091 {
1093  const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
1094  if (param_idx != SIOVOCB::getInvalidOrdinal()) {
1095  const Ptr<ParameterEntry> param_ptr = params_.getNonconstObjPtr(param_idx);
1096  if (param_ptr->isType<T>()) {
1097  return &param_ptr->getValue<T>(0);
1098  }
1099  // Note: The above is inefficinet. You have to do the dynamic_cast twice
1100  // (once to see if it is the type and once to do the cast). This could be
1101  // made more efficinet by upgrading Teuchos::any to add a any_cast_ptr()
1102  // function but I don't think anyone actually uses this function.
1103  return 0;
1104  }
1105  return 0;
1106 }
1107 
1108 
1109 template<typename T>
1110 inline
1111 const T* ParameterList::getPtr(const std::string& name_in) const
1112 {
1114  const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
1115  if (param_idx != SIOVOCB::getInvalidOrdinal()) {
1116  const Ptr<const ParameterEntry> param_ptr = params_.getObjPtr(param_idx);
1117  if (param_ptr->isType<T>()) {
1118  return &param_ptr->getValue<T>(0);
1119  }
1120  // Note: The above is inefficinet, see above non-const getPtr() function.
1121  return 0;
1122  }
1123  return 0;
1124 }
1125 
1126 
1127 inline
1128 ParameterEntry& ParameterList::getEntry(const std::string& name_in)
1129 {
1130  ParameterEntry *foundEntry = this->getEntryPtr(name_in);
1131  validateEntryExists("get", name_in, foundEntry);
1132  return *foundEntry;
1133 }
1134 
1135 
1136 inline
1137 const ParameterEntry& ParameterList::getEntry(const std::string& name_in) const
1138 {
1139  const ParameterEntry *foundEntry = this->getEntryPtr(name_in);
1140  validateEntryExists("get", name_in, foundEntry);
1141  return *foundEntry;
1142 }
1143 
1144 
1145 inline
1147 ParameterList::getEntryPtr(const std::string& name_in)
1148 {
1150  const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
1151  if (param_idx != SIOVOCB::getInvalidOrdinal()) {
1152  return &*params_.getNonconstObjPtr(param_idx);
1153  }
1154  return 0;
1155 }
1156 
1157 
1158 inline
1159 const ParameterEntry*
1160 ParameterList::getEntryPtr(const std::string& name_in) const
1161 {
1163  const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
1164  if (param_idx != SIOVOCB::getInvalidOrdinal()) {
1165  return &*params_.getObjPtr(param_idx);
1166  }
1167  return 0;
1168 }
1169 
1170 
1171 inline RCP<ParameterEntry>
1172 ParameterList::getEntryRCP(const std::string& name_in)
1173 {
1175  const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
1176  if (param_idx != SIOVOCB::getInvalidOrdinal()) {
1177  return rcpFromPtr(params_.getNonconstObjPtr(param_idx));
1178  }
1179  return null;
1180 }
1181 
1182 
1184 ParameterList::getEntryRCP(const std::string& name_in) const
1185 {
1187  const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
1188  if (param_idx != SIOVOCB::getInvalidOrdinal()) {
1189  return rcpFromPtr(params_.getObjPtr(param_idx));
1190  }
1191  return null;
1192 }
1193 
1194 
1197 { return modifier_; }
1198 
1199 
1200 // Attribute Functions
1201 
1202 
1203 inline
1204 const std::string& ParameterList::name() const
1205 {
1206  return name_;
1207 }
1208 
1209 
1210 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1211 template<typename T>
1212 bool ParameterList::isType(const std::string& name_in, T* /*ptr*/) const
1213 {
1215  const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
1216  if (param_idx != SIOVOCB::getInvalidOrdinal()) {
1217  return params_.getObjPtr(param_idx)->isType<T>();
1218  }
1219  return false;
1220 }
1221 #endif
1222 
1223 
1224 template<typename T>
1225 bool ParameterList::isType(const std::string& name_in) const
1226 {
1227  return this->isType(name_in, static_cast<T*>(0));
1228 }
1229 
1230 
1231 // Read-only access to the iterator
1232 
1233 
1235 {
1236  return params_.begin();
1237 }
1238 
1239 
1241 {
1242  return params_.end();
1243 }
1244 
1245 
1246 inline const std::string& ParameterList::name(ConstIterator i) const
1247 {
1248  return (i->first);
1249 }
1250 
1251 
1253 {
1254  return (i->second);
1255 }
1256 
1257 
1258 // private
1259 
1260 
1261 inline ParameterList::Iterator ParameterList::nonconstBegin()
1262 {
1263  return params_.nonconstBegin();
1264 }
1265 
1266 
1267 inline ParameterList::Iterator ParameterList::nonconstEnd()
1268 {
1269  return params_.nonconstEnd();
1270 }
1271 
1272 
1273 inline ParameterEntry& ParameterList::nonconstEntry(Iterator i)
1274 {
1275  return (i->second);
1276 }
1277 
1278 
1279 template<typename T>
1280 inline
1281 void ParameterList::validateEntryType(
1282  const std::string &/*funcName*/, const std::string &name_in,
1283  const ParameterEntry &entry_in
1284  ) const
1285 {
1287  entry_in.getAny().type() != typeid(T), Exceptions::InvalidParameterType
1288  ,"Error! An attempt was made to access parameter \""<<name_in<<"\""
1289  " of type \""<<entry_in.getAny().typeName()<<"\""
1290  "\nin the parameter (sub)list \""<<this->name()<<"\""
1291  "\nusing the incorrect type \""<<TypeNameTraits<T>::name()<<"\"!"
1292  );
1293 }
1294 
1295 
1296 // //////////////////////////////////////
1297 // Helper functions
1298 
1299 
1306 template<typename T>
1307 T& getParameter( ParameterList& l, const std::string& name )
1308 {
1309  return l.template get<T>(name);
1310 }
1311 
1312 
1318 template<typename T>
1319 inline
1320 T& get( ParameterList& l, const std::string& name )
1321 {
1322  return getParameter<T>(l,name);
1323 }
1324 
1325 
1332 template<typename T>
1333 const T& getParameter( const ParameterList& l, const std::string& name )
1334 {
1335  return l.template get<T>(name);
1336 }
1337 
1338 
1346 template<typename T>
1347 inline
1348 T* getParameterPtr( ParameterList& l, const std::string& name )
1349 {
1350  return l.template getPtr<T>(name);
1351 }
1352 
1353 
1361 template<typename T>
1362 inline
1363 const T* getParameterPtr( const ParameterList& l, const std::string& name )
1364 {
1365  return l.template getPtr<T>(name);
1366 }
1367 
1368 
1375 template<typename T>
1376 inline
1377 bool isParameterType( ParameterList& l, const std::string& name )
1378 {
1379  return l.isType( name, (T*)NULL );
1380 }
1381 
1382 
1389 template<typename T>
1390 inline
1391 bool isParameterType( const ParameterList& l, const std::string& name )
1392 {
1393  return l.isType( name, (T*)NULL );
1394 }
1395 
1396 
1408 template<typename T>
1410  const std::string &paramName
1411  ,const Array<T> &array
1412  ,ParameterList *paramList
1413  )
1414 {
1415  TEUCHOS_TEST_FOR_EXCEPT(!paramList);
1416  paramList->set(paramName,toString(array));
1417 }
1418 
1419 
1484 template<typename T>
1486  const ParameterList &paramList
1487  ,const std::string &paramName
1488  ,const int arrayDim = -1
1489  ,const bool mustExist = true
1490  )
1491 {
1492  std::string arrayStr;
1493  if(mustExist) {
1494  arrayStr = getParameter<std::string>(paramList,paramName);
1495  }
1496  else {
1497  const std::string
1498  *arrayStrPtr = getParameterPtr<std::string>(paramList,paramName);
1499  if(arrayStrPtr) {
1500  arrayStr = *arrayStrPtr;
1501  }
1502  else {
1503  return Array<T>(); // Return an empty array
1504  }
1505  }
1506  Array<T> a;
1507  try {
1508  a = fromStringToArray<T>(arrayStr);
1509  }
1510  catch( const InvalidArrayStringRepresentation&) {
1513  ,"Error! The parameter \""<<paramName<<"\"\n"
1514  "in the sublist \""<<paramList.name()<<"\"\n"
1515  "exists, but the std::string value:\n"
1516  "----------\n"
1517  <<arrayStr<<
1518  "\n----------\n"
1519  "is not a valid array represntation!"
1520  );
1521  }
1523  ( ( a.size()>0 && arrayDim>=0 ) && static_cast<int>(a.size())!=arrayDim )
1525  ,"Error! The parameter \""<<paramName<<"\"\n"
1526  "in the sublist \""<<paramList.name()<<"\"\n"
1527  "exists and is a valid array, but the dimension of\n"
1528  "the read in array a.size() = " << a.size() << "\n"
1529  "was not equal to the expected size arrayDim = " << arrayDim << "!"
1530  );
1531  return a;
1532 }
1533 
1534 
1547 template<typename T>
1548 bool replaceParameterWithArray(const std::string &paramName, const std::string &newName,
1549  ParameterList &pl)
1550 {
1551  bool param_exists = false;
1552  bool overwrite = false;
1553  if (paramName == newName){
1554  overwrite = true;
1555  }
1556  if (pl.isParameter(paramName)){
1557  param_exists = true;
1558  TEUCHOS_TEST_FOR_EXCEPTION(!pl.isType<T>(paramName), std::logic_error,
1559  "The parameter " << paramName << " is not of type " << typeid(T).name());
1560  TEUCHOS_TEST_FOR_EXCEPTION(pl.isParameter(newName) && !overwrite,
1561  std::logic_error, "The parameter " << newName << " already exists in this "
1562  "parameter list.");
1563  Array<T> params = tuple<T>(pl.get<T>(paramName));
1564  pl.remove(paramName);
1565  pl.set(newName, params);
1566  }
1567  return param_exists;
1568 }
1569 
1570 
1574 inline
1576  const RCP<ParameterList> &paramList, const std::string& name,
1577  bool mustAlreadyExist = false, const std::string& docString = ""
1578  )
1579 {
1580  return rcpWithEmbeddedObjPostDestroy(
1581  &paramList->sublist(name, mustAlreadyExist, docString), paramList, false );
1582 }
1583 
1584 
1588 inline
1590  const RCP<const ParameterList> &paramList, const std::string& name
1591  )
1592 {
1593  return rcpWithEmbeddedObjPostDestroy(
1594  &paramList->sublist(name), paramList, false );
1595 }
1596 
1597 
1601 inline std::ostream& operator<<(std::ostream& os, const ParameterList& l)
1602 {
1603  return l.print(os);
1604 }
1605 
1606 
1607 } // end of Teuchos namespace
1608 
1609 
1610 #endif
void print() const
Print function to use in debugging in a debugger.
bool replaceParameterWithArray(const std::string &paramName, const std::string &newName, ParameterList &pl)
Replace a parameter with an array containing the parameter.
Object held as the &quot;value&quot; 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 &paramList, const std::string &paramName, 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&#39;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 &quot;value&quot; 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 > &paramList, 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.
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 > &paramList, 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-&gt;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().
void setStringParameterFromArray(const std::string &paramName, const Array< T > &array, ParameterList *paramList)
Set a std::string parameter representation of an array.
size_type size() const
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.
#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...
#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...