Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_StandardParameterEntryValidators.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TEUCHOS_STANDARD_PARAMETER_ENTRY_VALIDATORS_H
11 #define TEUCHOS_STANDARD_PARAMETER_ENTRY_VALIDATORS_H
12 
17 #include "Teuchos_TwoDArray.hpp"
18 #include "Teuchos_Assert.hpp"
19 #include "Teuchos_StrUtils.hpp"
22 
23 #ifdef HAVE_TEUCHOSCORE_QUADMATH
24 # include <quadmath.h> // __float128 constants and functions
25 #endif // HAVE_TEUCHOSCORE_QUADMATH
26 
27 #include <locale>
28 
29 
30 namespace Teuchos {
31 
59 template<class IntegralType>
61 public:
64 
83  const std::string& defaultParameterName,
84  const bool caseSensitive = true);
85 
110  const ArrayView<const IntegralType>& integralValues,
111  std::string const& defaultParameterName,
112  const bool caseSensitive = true);
113 
144  const ArrayView<const std::string>& stringsDocs,
145  const ArrayView<const IntegralType>& integralValues,
146  const std::string& defaultParameterName,
147  const bool caseSensitive = true);
149 
151 
162  IntegralType getIntegralValue(
163  const std::string &str, const std::string &paramName = "",
164  const std::string &sublistName = ""
165  ) const;
166 
182  IntegralType
183  getIntegralValue (const ParameterEntry &entry,
184  const std::string &paramName = "",
185  const std::string &sublistName = "",
186  const bool activeQuery = true) const;
187 
203  std::string
204  getStringValue (const ParameterEntry &entry,
205  const std::string &paramName = "",
206  const std::string &sublistName = "",
207  const bool activeQuery = true) const;
208 
215  IntegralType
216  getIntegralValue (ParameterList& paramList,
217  const std::string& paramName,
218  const std::string& defaultValue) const;
219 
223  std::string getStringValue(
224  ParameterList &paramList, const std::string &paramName,
225  const std::string &defaultValue
226  ) const;
227 
234 
239  const std::string& getDefaultParameterName() const;
240 
252  std::string validateString(
253  const std::string &str, const std::string &paramName = "",
254  const std::string &sublistName = ""
255  ) const;
256 
261  bool isCaseSensitive () const {
262  return caseSensitive_;
263  }
264 
266 
268 
270  const std::string getXMLTypeName() const;
271 
273  void printDoc(
274  std::string const& docString,
275  std::ostream & out
276  ) const;
277 
280  validStringValues() const;
281 
283  void validate(
284  ParameterEntry const& entry,
285  std::string const& paramName,
286  std::string const& sublistName
287  ) const;
288 
289 #if defined(HAVE_TEUCHOS_MODIFY_DEFAULTS_DURING_VALIDATION)
290  void validateAndModify(
291  std::string const& paramName,
292  std::string const& sublistName,
293  ParameterEntry * entry
294  ) const;
295 #endif
296 
298 
299 private:
301  std::string validValues_;
304 
305  typedef std::map<std::string,IntegralType> map_t;
307  typedef std::map<IntegralType,std::string> inv_map_t;
309 
310  const bool caseSensitive_;
311 
323  void init(const ArrayView<const std::string>& strings,
324  const ArrayView<const IntegralType>& integralValues);
325 
326  void setValidValues(
327  ArrayView<const std::string> const& strings,
328  ArrayView<const std::string> const* stringsDocs = NULL
329  );
330 
331  // Not defined and not to be called.
333 
335  static std::string upperCase (const std::string s) {
336  std::string s_upper = s;
337  std::transform (s_upper.begin (), s_upper.end (), s_upper.begin (), ::toupper);
338  return s_upper;
339  }
340 };
341 
342 
347 template<class IntegralType>
348 RCP<StringToIntegralParameterEntryValidator<IntegralType> >
349 stringToIntegralParameterEntryValidator(
350  ArrayView<const std::string> const& strings,
351  std::string const& defaultParameterName
352  );
353 
354 
359 template<class IntegralType>
360 RCP<StringToIntegralParameterEntryValidator<IntegralType> >
361 stringToIntegralParameterEntryValidator(
362  ArrayView<const std::string> const& strings,
363  std::string const& defaultParameterName,
364  const bool caseSensitive
365  );
366 
367 
372 template<class IntegralType>
373 RCP<StringToIntegralParameterEntryValidator<IntegralType> >
374 stringToIntegralParameterEntryValidator(
375  ArrayView<const std::string> const& strings,
376  ArrayView<const IntegralType> const& integralValues,
377  std::string const& defaultParameterName
378  );
379 
380 
385 template<class IntegralType>
386 RCP<StringToIntegralParameterEntryValidator<IntegralType> >
387 stringToIntegralParameterEntryValidator(
388  ArrayView<const std::string> const& strings,
389  ArrayView<const IntegralType> const& integralValues,
390  std::string const& defaultParameterName,
391  const bool caseSensitive
392  );
393 
394 
399 template<class IntegralType>
400 RCP<StringToIntegralParameterEntryValidator<IntegralType> >
401 stringToIntegralParameterEntryValidator(
402  ArrayView<const std::string> const& strings,
403  ArrayView<const std::string> const& stringsDocs,
404  ArrayView<const IntegralType> const& integralValues,
405  std::string const& defaultParameterName
406  );
407 
408 
413 template<class IntegralType>
414 RCP<StringToIntegralParameterEntryValidator<IntegralType> >
415 stringToIntegralParameterEntryValidator(
416  ArrayView<const std::string> const& strings,
417  ArrayView<const std::string> const& stringsDocs,
418  ArrayView<const IntegralType> const& integralValues,
419  std::string const& defaultParameterName,
420  const bool caseSensitive
421  );
422 
423 
434 template<class IntegralType>
435 void setStringToIntegralParameter(
436  std::string const& paramName,
437  std::string const& defaultValue,
438  std::string const& docString,
439  ArrayView<const std::string> const& strings,
440  ParameterList * paramList
441  );
442 
443 
455 template<class IntegralType>
456 void setStringToIntegralParameter(
457  std::string const& paramName,
458  std::string const& defaultValue,
459  std::string const& docString,
460  ArrayView<const std::string> const& strings,
461  ArrayView<const IntegralType> const& integralValues,
462  ParameterList * paramList
463  );
464 
465 
477 template<class IntegralType>
478 void setStringToIntegralParameter(
479  std::string const& paramName,
480  std::string const& defaultValue,
481  std::string const& docString,
482  ArrayView<const std::string> const& strings,
483  ArrayView<const std::string> const& stringsDocs,
484  ArrayView<const IntegralType> const& integralValues,
485  ParameterList * paramList
486  );
487 
488 
499 template<class IntegralType>
500 IntegralType getIntegralValue(
501  ParameterList const& paramList, std::string const& paramName
502  );
503 
504 
516 template<class IntegralType>
517 std::string getStringValue(
518  ParameterList const& paramList, std::string const& paramName
519  );
520 
521 
527 template<class IntegralType>
528 RCP<const StringToIntegralParameterEntryValidator<IntegralType> >
530  ParameterEntry const& entry, ParameterList const& paramList,
531  std::string const& paramName
532  );
533 
534 
540 std::string getVerbosityLevelParameterValueName(
541  const EVerbosityLevel verbLevel
542  );
543 
544 
549 RCP<StringToIntegralParameterEntryValidator<EVerbosityLevel> >
550 verbosityLevelParameterEntryValidator(std::string const& defaultParameterName);
551 
557 template<class IntegralType>
559 
560 public:
561 
564 
569  getDummyObject();
570 
572 };
573 
574 template<class IntegralType>
577 {
578  return stringToIntegralParameterEntryValidator<IntegralType>(
579  tuple<std::string>(""), tuple<std::string>(""),
580  tuple<IntegralType>((IntegralType)1), "");
581 }
582 
596  : public ParameterEntryValidator
597 {
598 public:
599 
602 
604 
606 
609 
611  bool getBool(
612  const ParameterEntry &entry, const std::string &paramName = "",
613  const std::string &sublistName = "", const bool activeQuery = true
614  ) const;
615 
619  bool getBool(
620  ParameterList &paramList, const std::string &paramName,
621  const int defaultValue
622  ) const;
623 
625 
628 
630  const std::string getXMLTypeName() const;
631 
633  void printDoc(
634  std::string const& docString,
635  std::ostream & out
636  ) const;
637 
640  validStringValues() const;
641 
643  void validate(
644  ParameterEntry const& entry,
645  std::string const& paramName,
646  std::string const& sublistName
647  ) const;
648 
650  void validateAndModify(
651  std::string const& paramName,
652  std::string const& sublistName,
653  ParameterEntry * entry
654  ) const;
655 
657 
658 private:
659 
660  // ////////////////////////////
661  // Private data members
662 
663  std::string acceptedTypesString_;
664 
665  // ////////////////////////////
666  // Private member functions
667 
668  void finishInitialization();
669 
670  void throwTypeError(
671  ParameterEntry const& entry,
672  std::string const& paramName,
673  std::string const& sublistName
674  ) const;
675 };
676 
677 // Nonmember helper functions
678 
679 
685 boolParameterEntryValidator();
686 
687 
702  : public ParameterEntryValidator
703 {
704 public:
705 
708 
710  enum EPreferredType { PREFER_INT, PREFER_LONG_LONG, PREFER_DOUBLE, PREFER_STRING };
711 
712 
715  public:
717  AcceptedTypes( bool allowAllTypesByDefault = true )
718  :allowInt_(allowAllTypesByDefault)
719  ,allowLongLong_(allowAllTypesByDefault)
720  ,allowDouble_(allowAllTypesByDefault)
721  ,allowString_(allowAllTypesByDefault)
722  {}
724  AcceptedTypes& allowInt( bool _allowInt )
725  { allowInt_ = _allowInt; return *this; }
727  AcceptedTypes& allowLongLong( bool _allowLongLong )
728  { allowLongLong_ = _allowLongLong; return *this; }
730  AcceptedTypes& allowDouble( bool _allowDouble )
731  { allowDouble_ = _allowDouble; return *this; }
733  AcceptedTypes& allowString( bool _allowString )
734  { allowString_ = _allowString; return *this; }
736  bool allowInt() const { return allowInt_; }
738  bool allowLongLong() const { return allowLongLong_; }
740  bool allowDouble() const { return allowDouble_; }
742  bool allowString() const { return allowString_; }
743  private:
744  bool allowInt_;
748  };
749 
751 
754 
759 
771  EPreferredType const preferredType,
772  AcceptedTypes const& acceptedTypes
773  );
774 
776 
779 
785  int getInt(
786  const ParameterEntry &entry, const std::string &paramName = "",
787  const std::string &sublistName = "", const bool activeQuery = true
788  ) const;
789 
795  long long getLongLong(
796  const ParameterEntry &entry, const std::string &paramName = "",
797  const std::string &sublistName = "", const bool activeQuery = true
798  ) const;
799 
803  double getDouble(
804  const ParameterEntry &entry, const std::string &paramName = "",
805  const std::string &sublistName = "", const bool activeQuery = true
806  ) const;
807 
809  std::string getString(
810  const ParameterEntry &entry, const std::string &paramName = "",
811  const std::string &sublistName = "", const bool activeQuery = true
812  ) const;
813 
817  int getInt(
818  ParameterList &paramList, const std::string &paramName,
819  const int defaultValue
820  ) const;
821 
825  long long getLongLong(
826  ParameterList &paramList, const std::string &paramName,
827  const long long defaultValue
828  ) const;
829 
833  double getDouble(
834  ParameterList &paramList, const std::string &paramName,
835  const double defaultValue
836  ) const;
837 
841  std::string getString(
842  ParameterList &paramList, const std::string &paramName,
843  const std::string &defaultValue
844  ) const;
845 
848  bool isIntAllowed() const;
849 
852  bool isLongLongAllowed() const;
853 
856  bool isDoubleAllowed() const;
857 
860  bool isStringAllowed() const;
861 
864  EPreferredType getPreferredType() const;
865 
867  static const std::string& getPrefferedTypeString (EPreferredType enumValue)
868  {
869  switch (enumValue) {
870  case PREFER_INT:
871  return getIntEnumString ();
872  case PREFER_LONG_LONG:
873  return getLongLongEnumString ();
874  case PREFER_DOUBLE:
875  return getDoubleEnumString ();
876  case PREFER_STRING:
877  return getStringEnumString ();
878  default:
879  const std::string typeString (toString (enumValue));
880  throw std::runtime_error("Cannot convert enumValue: " + typeString + " to a string");
881  }
882  }
883 
885  static EPreferredType getPrefferedTypeStringEnum (const std::string& enumString)
886  {
887  if (enumString == getIntEnumString ()) {
888  return PREFER_INT;
889  }
890  else if (enumString == getLongLongEnumString ()) {
891  return PREFER_LONG_LONG;
892  }
893  else if (enumString == getDoubleEnumString ()) {
894  return PREFER_DOUBLE;
895  }
896  else if (enumString == getStringEnumString ()) {
897  return PREFER_STRING;
898  }
899  else {
900  throw std::runtime_error ("Cannot convert enumString: " + enumString + " to an enum");
901  }
902  }
903 
905 
908 
910  const std::string getXMLTypeName() const;
911 
913  void printDoc(
914  std::string const& docString,
915  std::ostream & out
916  ) const;
917 
919  ValidStringsList
920  validStringValues() const;
921 
923  void validate(
924  ParameterEntry const& entry,
925  std::string const& paramName,
926  std::string const& sublistName
927  ) const;
928 
930  void validateAndModify(
931  std::string const& paramName,
932  std::string const& sublistName,
933  ParameterEntry * entry
934  ) const;
935 
936 
938 
939 private:
940 
941  // ////////////////////////////
942  // Private data members
943 
945  std::string acceptedTypesString_;
946 
947 //use pragmas to disable some false-positive warnings for windows sharedlibs export
948 #ifdef _MSC_VER
949 #pragma warning(push)
950 #pragma warning(disable:4251)
951 #endif
953 #ifdef _MSC_VER
954 #pragma warning(pop)
955 #endif
956 
957  // ////////////////////////////
958  // Private member functions
959 
960  /* \brief Gets the string representing the "int" preferred type enum */
961  static const std::string& getIntEnumString(){
962  static const std::string intEnumString_ = TypeNameTraits<int>::name();
963  return intEnumString_;
964  }
965 
966  /* \brief Gets the string representing the "int" preferred type enum */
967  static const std::string& getLongLongEnumString(){
968  static const std::string longLongEnumString_ = TypeNameTraits<long long>::name();
969  return longLongEnumString_;
970  }
971 
972  /* \brief Gets the string representing the "double" preferred type enum */
973  static const std::string& getDoubleEnumString(){
974  static const std::string doubleEnumString_ = TypeNameTraits<double>::name();
975  return doubleEnumString_;
976  }
977 
978  /* \brief Gets the string representing the "string" preferred type enum */
979  static const std::string& getStringEnumString(){
980  static const std::string stringEnumString_ = TypeNameTraits<std::string>::name();
981  return stringEnumString_;
982  }
983 
984 
985  void finishInitialization();
986 
987  void throwTypeError(
988  ParameterEntry const& entry,
989  std::string const& paramName,
990  std::string const& sublistName
991  ) const;
992 
993 };
994 
995 
996 // Nonmember helper functions
997 
998 
1003 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT RCP<AnyNumberParameterEntryValidator>
1004 anyNumberParameterEntryValidator();
1005 
1006 
1011 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT RCP<AnyNumberParameterEntryValidator>
1012 anyNumberParameterEntryValidator(
1014  AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
1015  );
1016 
1022 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setIntParameter(
1023  std::string const& paramName,
1024  int const value, std::string const& docString,
1025  ParameterList *paramList,
1026  AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
1027  = AnyNumberParameterEntryValidator::AcceptedTypes()
1028  );
1029 
1030 
1036 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setLongLongParameter(
1037  std::string const& paramName,
1038  long long const value, std::string const& docString,
1039  ParameterList *paramList,
1040  AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
1041  = AnyNumberParameterEntryValidator::AcceptedTypes()
1042  );
1043 
1049 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setDoubleParameter(
1050  std::string const& paramName,
1051  double const& value, std::string const& docString,
1052  ParameterList *paramList,
1053  AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
1054  = AnyNumberParameterEntryValidator::AcceptedTypes()
1055  );
1056 
1057 
1063 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setNumericStringParameter(
1064  std::string const& paramName,
1065  std::string const& value, std::string const& docString,
1066  ParameterList *paramList,
1067  AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
1068  = AnyNumberParameterEntryValidator::AcceptedTypes()
1069  );
1070 
1071 
1086 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT int getIntParameter(
1087  ParameterList const& paramList, std::string const& paramName
1088  );
1089 
1090 
1105 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT long long getLongLongParameter(
1106  ParameterList const& paramList, std::string const& paramName
1107  );
1108 
1123 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT double getDoubleParameter(
1124  ParameterList const& paramList,
1125  std::string const& paramName
1126  );
1127 
1128 
1144 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT std::string getNumericStringParameter(
1145  ParameterList const& paramList,
1146  std::string const& paramName
1147  );
1148 
1154 template<>
1156 
1157 public:
1158 
1161 
1165  static RCP<AnyNumberParameterEntryValidator > getDummyObject();
1166 
1168 
1169 };
1170 
1171 
1176 template <class T>
1179  static inline T notDefined() {
1180  return T::this_type_is_missing_a_specialization();
1181  }
1182 };
1183 
1184 
1205 template <class T>
1207 public:
1208 
1210  static inline T min()
1212 
1214  static inline T max()
1216 
1219  static inline T defaultStep()
1221 
1224  static inline unsigned short defaultPrecision()
1226 
1227 };
1228 
1229 
1230 template<>
1231 class EnhancedNumberTraits<short int>{
1232 public:
1233  static inline short int min() { return std::numeric_limits<short int>::min(); }
1234  static inline short int max() { return std::numeric_limits<short int>::max(); }
1235  static inline short int defaultStep() { return 1; }
1236  static inline unsigned short defaultPrecision() { return 0; }
1237 };
1238 
1239 
1240 template<>
1241 class EnhancedNumberTraits<short unsigned int>{
1242 public:
1243  static inline short unsigned int min() { return std::numeric_limits<short unsigned int>::min(); }
1244  static inline short unsigned int max() { return std::numeric_limits<short unsigned int>::max(); }
1245  static inline short unsigned int defaultStep() { return 1; }
1246  static inline unsigned short defaultPrecision() { return 0; }
1247 };
1248 
1249 
1250 template<>
1252 public:
1253  static inline int min() { return std::numeric_limits<int>::min(); }
1254  static inline int max() { return std::numeric_limits<int>::max(); }
1255  static inline int defaultStep() { return 1; }
1256  static inline unsigned short defaultPrecision() { return 0; }
1257 };
1258 
1259 
1260 template<>
1261 class EnhancedNumberTraits<unsigned int>{
1262 public:
1263  static inline unsigned int min() { return std::numeric_limits<unsigned int>::min(); }
1264  static inline unsigned int max() { return std::numeric_limits<unsigned int>::max(); }
1265  static inline unsigned int defaultStep() { return 1; }
1266  static inline unsigned short defaultPrecision() { return 0; }
1267 };
1268 
1269 
1270 template<>
1271 class EnhancedNumberTraits<long int>{
1272 public:
1273  static inline long int min() { return std::numeric_limits<long int>::min(); }
1274  static inline long int max() { return std::numeric_limits<long int>::max(); }
1275  static inline long int defaultStep() { return 1; }
1276  static inline unsigned short defaultPrecision() { return 0; }
1277 };
1278 
1279 
1280 template<>
1281 class EnhancedNumberTraits<long unsigned int>{
1282 public:
1283  static inline long unsigned int min() { return std::numeric_limits<long unsigned int>::min(); }
1284  static inline long unsigned int max() { return std::numeric_limits<long unsigned int>::max(); }
1285  static inline long unsigned int defaultStep() { return 1; }
1286  static inline unsigned short defaultPrecision() { return 0; }
1287 };
1288 
1289 
1290 template<>
1291 class EnhancedNumberTraits<long long int>{
1292 public:
1293  static inline long long int min() { return std::numeric_limits<long long int>::min(); }
1294  static inline long long int max() { return std::numeric_limits<long long int>::max(); }
1295  static inline long long int defaultStep() { return 1; }
1296  static inline unsigned short defaultPrecision() { return 0; }
1297 };
1298 
1299 
1300 template<>
1301 class EnhancedNumberTraits<long long unsigned int>{
1302 public:
1303  static inline long long unsigned int min() { return std::numeric_limits<long long unsigned int>::min(); }
1304  static inline long long unsigned int max() { return std::numeric_limits<long long unsigned int>::max(); }
1305  static inline long long unsigned int defaultStep() { return 1; }
1306  static inline unsigned short defaultPrecision() { return 0; }
1307 };
1308 
1309 
1310 #ifdef HAVE_TEUCHOSCORE_QUADMATH
1311 template<>
1312 class EnhancedNumberTraits<__float128>{
1313 public:
1314  static inline __float128 min() { return -std::numeric_limits<__float128>::max(); }
1315  static inline __float128 max() { return std::numeric_limits<__float128>::max(); }
1316  static inline __float128 defaultStep() { return 1; }
1317  static inline unsigned short defaultPrecision() { return 100; }
1318 };
1319 #endif // HAVE_TEUCHOSCORE_QUADMATH
1320 
1321 template<>
1322 class EnhancedNumberTraits<double>{
1323 public:
1324  static inline double min() { return -std::numeric_limits<double>::max(); }
1325  static inline double max() { return std::numeric_limits<double>::max(); }
1326  static inline double defaultStep() { return 1; }
1327  static inline unsigned short defaultPrecision() { return 100; }
1328 };
1329 
1330 #ifdef HAVE_TEUCHOS_LONG_DOUBLE
1331 template<>
1332 class EnhancedNumberTraits<long double>{
1333 public:
1334  static inline long double min() { return -std::numeric_limits<long double>::max(); }
1335  static inline long double max() { return std::numeric_limits<long double>::max(); }
1336  static inline long double defaultStep() { return 1; }
1337  static inline unsigned short defaultPrecision() { return 100; }
1338 };
1339 #endif
1340 
1341 template<>
1343 public:
1344  static inline float min() { return -std::numeric_limits<float>::max(); }
1345  static inline float max() { return std::numeric_limits<float>::max(); }
1346  static inline float defaultStep() { return 1; }
1347  static inline unsigned short defaultPrecision() { return 100; }
1348 };
1349 
1355 template <class T>
1357 
1358 public:
1359 
1362 
1378  T min,
1379  T max,
1381  unsigned short precision=EnhancedNumberTraits<T>::defaultPrecision()):
1383  minVal(min), maxVal(max), step_(step), precision_(precision),
1384  containsMin(true), containsMax(true){}
1385 
1391  minVal(EnhancedNumberTraits<T>::min()),
1392  maxVal(EnhancedNumberTraits<T>::max()),
1393  step_(EnhancedNumberTraits<T>::defaultStep()),
1394  precision_(EnhancedNumberTraits<T>::defaultPrecision()),
1395  containsMin(false),
1396  containsMax(false){}
1397 
1399 
1401 
1402 
1407  void setMin(T min){
1408  minVal = min;
1409  containsMin = true;
1410  }
1411 
1416  void setMax(T max){
1417  maxVal = max;
1418  containsMax = true;
1419  }
1420 
1425  void setStep(T step){
1426  step_ = step;
1427  }
1428 
1433  void setPrecision(unsigned short precision){
1434  precision_ = precision;
1435  }
1436 
1438 
1441 
1446  T getMin() const{
1447  return minVal;
1448  }
1449 
1454  T getMax() const{
1455  return maxVal;
1456  }
1457 
1462  T getStep() const{
1463  return step_;
1464  }
1465 
1470  unsigned short getPrecision() const{
1471  return precision_;
1472  }
1473 
1475 
1477 
1478 
1483  bool hasMin() const{
1484  return containsMin;
1485  }
1486 
1491  bool hasMax() const{
1492  return containsMax;
1493  }
1494 
1496 
1499 
1502  return null;
1503  }
1504 
1506  void validate(ParameterEntry const &entry, std::string const &paramName,
1507  std::string const &sublistName) const;
1508 
1510  void validateAndModify( std::string const& paramName,
1511  std::string const& sublistName, ParameterEntry * entry) const;
1512 
1515  const bool activeQuery) const;
1516 
1518  const std::string getXMLTypeName() const{
1519  return "EnhancedNumberValidator(" + TypeNameTraits<T>::name()+ ")";
1520  }
1521 
1523  void printDoc(std::string const &docString, std::ostream &out) const{
1524  StrUtils::printLines(out,"# ",docString);
1525  out << "#\tValidator Used: " << std::endl;
1526  out << "#\t\tNumber Validator" << std::endl;
1527  out << "#\t\tType: " << Teuchos::TypeNameTraits<T>::name() <<
1528  std::endl;
1529  out << "#\t\tMin (inclusive): " << minVal << std::endl;
1530  out << "#\t\tMax (inclusive): " << maxVal << std::endl;
1531  }
1532 
1534 
1535 private:
1538 
1539  // note this was discussed in issue #612
1540  // currently we are keeping a string validator with EnhancedNumberValidator
1541  // an alternative is to make a combined class for AnyNumberParameterEntryValidator
1542  // and EnhancedNumberValidator
1543  bool useIntConversions() const;
1544 
1546 
1549 
1553 
1557 
1561 
1565  unsigned short precision_;
1566 
1570 
1574 
1576 
1577 };
1578 
1579 template<class T>
1581  std::string const& paramName,
1582  std::string const& sublistName,
1583  ParameterEntry * entry
1584  ) const
1585 {
1586  TEUCHOS_TEST_FOR_EXCEPT(0==entry);
1587 
1588  any anyValue = entry->getAny(true);
1589  // preferred type is not string
1590  if( anyValue.type() == typeid(std::string) ) {
1591  anyValue = getNumberFromString(*entry,false);
1592  entry->setValue(
1593  any_cast<T>(anyValue),
1594  false // isDefault
1595  );
1596  }
1597  else {
1598  // default behavior
1600  paramName, sublistName, entry);
1601  }
1602 }
1603 
1604 template<class T>
1606 {
1607  // this will need some rethinking and exists only for supporting
1608  // conversion of strings to the templated type T
1609  // but we may want to unify this into the base class anyways
1610  // and share string conversion concepts with other parameters
1611  // like AnyNumberParameterEntryValidator
1612  if(typeid(T) == typeid(char)) return true;
1613  if(typeid(T) == typeid(unsigned char)) return true;
1614  if(typeid(T) == typeid(int)) return true;
1615  if(typeid(T) == typeid(unsigned int)) return true;
1616  if(typeid(T) == typeid(short)) return true;
1617  if(typeid(T) == typeid(unsigned short)) return true;
1618  if(typeid(T) == typeid(long)) return true;
1619  if(typeid(T) == typeid(unsigned long)) return true;
1620  if(typeid(T) == typeid(long long)) return true;
1621  if(typeid(T) == typeid(unsigned long long)) return true;
1622 
1623  // default to double stod to older atof conversion
1624  // depending on HAVE_TEUCHOSCORE_CXX11
1625  // those conversions would probably handle all above discrete types anyways
1626  return false;
1627 }
1628 
1629 template<class T>
1631  const ParameterEntry &entry, const bool activeQuery
1632  ) const
1633 {
1634  // perhaps we want to just eliminate the int checks
1635  // and always use double conversion which I think would work
1636  // well for all types - but this will give us a behavior which mirrors
1637  // AnyNumberParameterEntryValidator more closely
1638  const any &anyValue = entry.getAny(activeQuery);
1639  if(useIntConversions()) {
1640  return any((T)convertStringToInt(any_cast<std::string>(anyValue)));
1641  }
1642  else { // if not discrete, read as a double and cast to our type T
1643  return any((T)convertStringToDouble(any_cast<std::string>(anyValue)));
1644  }
1645 }
1646 
1647 template<class T>
1648 void EnhancedNumberValidator<T>::validate(ParameterEntry const &entry, std::string const &paramName,
1649  std::string const &sublistName) const
1650 {
1651  any anyValue = entry.getAny(true);
1652 
1653  // This was new code added to allow EnhancedNumberValidator to accept a string
1654  // This was added for consistency with AnyNumberParameterEntryValidator
1655  // and the new BoolParameterEntryValidator which all take string
1656  // We may wish to change this to be optional like AnyNumberParameterEntryValidator
1657  if( anyValue.type() == typeid(std::string) ) {
1658  // try to upgrade from a string to a number
1659  anyValue = getNumberFromString(entry, false);
1660  }
1661 
1662  const std::string &entryName = entry.getAny(false).typeName();
1663  TEUCHOS_TEST_FOR_EXCEPTION(anyValue.type() != typeid(T),
1665  "The \"" << paramName << "\"" <<
1666  " parameter in the \"" << sublistName <<
1667  "\" sublist is has an error." << std::endl << std::endl <<
1668  "Error: The value that you entered was the wrong type." << std::endl <<
1669  "Parameter: " << paramName << std::endl <<
1670  "Type specified: " << entryName << std::endl <<
1671  "Type accepted: " << Teuchos::TypeNameTraits<T>::name() << std::endl);
1672 
1673  bool isValueInRange;
1674  any_cast<T>(anyValue) >= minVal && any_cast<T>(anyValue) <= maxVal
1675  ? isValueInRange = true : isValueInRange=false;
1676  TEUCHOS_TEST_FOR_EXCEPTION(!(isValueInRange),
1678  "The \"" << paramName << "\"" <<
1679  " parameter in the \"" << sublistName <<
1680  "\" sublist is has an error." << std::endl << std::endl <<
1681  "Error: The value that was entered doesn't fall with in " <<
1682  "the range set by the validator" << std::endl <<
1683  "Parameter: " << paramName << std::endl <<
1684  "Min: " << minVal << std::endl <<
1685  "Max: " << maxVal << std::endl <<
1686  "Value entered: " <<
1687  (any_cast<T>(anyValue)) << std::endl << std::endl);
1688 }
1689 
1695 template<class T>
1697 
1698 public:
1699 
1702 
1707 
1709 };
1710 
1711 template<class T>
1714 {
1715  return rcp(new EnhancedNumberValidator<T>);
1716 }
1717 
1727 
1728 public:
1729 
1732 
1735  static bool mustAlreadyExistDefault() { return false; }
1736 
1738 
1741 
1747  FileNameValidator(bool mustAlreadyExist = mustAlreadyExistDefault());
1748 
1750 
1752 
1753 
1759  bool fileMustExist() const;
1760 
1762 
1763 
1774  bool fileEmptyNameOK() const;
1775 
1777 
1779 
1780 
1789  bool setFileMustExist(bool shouldFileExist);
1790 
1799  bool setFileEmptyNameOK(bool isEmptyNameOK);
1800 
1802 
1805 
1807  ValidStringsList validStringValues() const;
1808 
1810  void validate(
1811  ParameterEntry const &entry,
1812  std::string const &paramName,
1813  std::string const &sublistName) const;
1814 
1816  const std::string getXMLTypeName() const;
1817 
1819  void printDoc(std::string const &docString, std::ostream &out) const;
1820 
1822 
1823 private:
1824 
1827 
1833 
1835 
1836 };
1837 
1843 template<>
1845 
1846 public:
1847 
1850 
1854  static RCP<FileNameValidator> getDummyObject();
1855 
1857 
1858 };
1859 
1868 
1869 public:
1870 
1873 
1876  StringValidator();
1877 
1882  StringValidator(const Teuchos::Array<std::string> &validStrings, bool caseSensitive=true);
1883 
1885 
1887 
1888 
1896  ValidStringsList setValidStrings(
1897  const Teuchos::Array<std::string> &validStrings);
1898 
1903  bool isCaseSensitive () const {
1904  return caseSensitive_;
1905  }
1906 
1908 
1911 
1913  ValidStringsList validStringValues() const;
1914 
1916  void validate(ParameterEntry const &entry, std::string const &paramName,
1917  std::string const &sublistName) const;
1918 
1920  const std::string getXMLTypeName() const;
1921 
1923  void printDoc(std::string const &docString, std::ostream &out) const;
1924 
1926 
1927 private:
1928 
1931 
1934  ValidStringsList validStrings_;
1935 
1937 
1939  static std::string upperCase (const std::string s) {
1940  std::string s_upper = s;
1941  std::transform (s_upper.begin (), s_upper.end (), s_upper.begin (), ::toupper);
1942  return s_upper;
1943  }
1944 
1946 
1947 };
1948 
1954 template<>
1956 
1957 public:
1958 
1961 
1965  static RCP<StringValidator> getDummyObject();
1966 
1968 
1969 };
1970 
1971 
1975 template<class ValidatorType, class EntryType>
1977 
1978 public:
1979 
1982 
1991  prototypeValidator_(prototypeValidator){}
1992 
1994 
1997 
2000  return prototypeValidator_;
2001  }
2002 
2004 
2007 
2010  return prototypeValidator_->validStringValues();
2011  }
2012 
2014 
2015 private:
2016 
2019 
2023 
2026 
2028 
2029 };
2030 
2043 template<class ValidatorType, class EntryType>
2044 class TwoDArrayValidator : public AbstractArrayValidator<ValidatorType, EntryType>{
2045 public:
2048 
2056  AbstractArrayValidator<ValidatorType, EntryType>(prototypeValidator){}
2057 
2059 
2062 
2064  virtual void validate(ParameterEntry const &entry, std::string const &paramName,
2065  std::string const &sublistName) const;
2066 
2068  const std::string getXMLTypeName() const{
2069  return "TwoDArrayValidator(" +
2070  this->getPrototype()->getXMLTypeName() + ", " +
2072  }
2073 
2075  virtual void printDoc(std::string const &docString, std::ostream &out) const
2076  {
2077  StrUtils::printLines(out,"# ",docString);
2078  std::string toPrint;
2079  toPrint += "TwoDArrayValidator:\n";
2080  toPrint += "Prototype Validator:\n";
2081  this->getPrototype()->printDoc(toPrint, out);
2082  }
2083 
2085 
2086 };
2087 
2088 template<class ValidatorType, class EntryType>
2089 void TwoDArrayValidator<ValidatorType, EntryType>::validate(ParameterEntry const &entry, std::string const &paramName,
2090  std::string const &sublistName) const
2091 {
2092  any anyValue = entry.getAny(true);
2093  const std::string &entryName = entry.getAny(false).typeName();
2096  "The \"" << paramName << "\"" <<
2097  " parameter in the \"" << sublistName <<
2098  "\" sublist is has an error." << std::endl << std::endl <<
2099  "Error: The value you entered was the wrong type." << std::endl <<
2100  "Parameter: " << paramName << std::endl <<
2101  "Type specified: " << entryName << std::endl <<
2102  "Type accepted: " << TypeNameTraits<TwoDArray<EntryType> >::name() <<
2103  std::endl << std::endl);
2104 
2105  TwoDArray<EntryType> extracted =
2106  getValue<Teuchos::TwoDArray<EntryType> >(entry);
2107  RCP<const ParameterEntryValidator> prototype = this->getPrototype();
2108  for(int i = 0; i<extracted.getNumRows(); ++i){
2109  for(int j = 0; j<extracted.getNumCols(); ++j){
2110  ParameterEntry dummyParameter;
2111  dummyParameter.setValue(extracted(i,j));
2112  try{
2113  prototype->validate(
2114  dummyParameter, paramName, sublistName);
2115  }
2117  std::stringstream oss;
2118  oss << "TwoDArray Validator Exception:" << std::endl <<
2119  "Bad Index: (" << i << "," << j << ")" << std::endl << e.what();
2120  throw Exceptions::InvalidParameterValue(oss.str());
2121  }
2122  }
2123  }
2124 }
2125 
2126 
2132 template<class ValidatorType, class EntryType>
2133 class DummyObjectGetter<TwoDArrayValidator<ValidatorType, EntryType> >{
2134 
2135 public:
2136 
2139 
2144 
2146 
2147 };
2148 
2149 template<class ValidatorType, class EntryType>
2152 {
2155 }
2156 
2161  public TwoDArrayValidator<StringValidator, std::string>{
2162 
2163 public:
2164 
2167 
2170  TwoDArrayValidator<StringValidator, std::string>(prototypeValidator){}
2171 
2173 
2174 };
2175 
2176 
2182  public TwoDArrayValidator<FileNameValidator, std::string>{
2183 
2184 public:
2185 
2188 
2191  TwoDArrayValidator<FileNameValidator, std::string>(prototypeValidator){}
2192 
2194 
2195 };
2196 
2197 
2201 template<class T>
2202 class TwoDArrayNumberValidator : public TwoDArrayValidator<EnhancedNumberValidator<T>, T>{
2203 public:
2206 
2209  RCP<const EnhancedNumberValidator<T> > prototypeValidator):
2210  TwoDArrayValidator<EnhancedNumberValidator<T>, T>(prototypeValidator){}
2211 
2213 
2214 };
2215 
2216 
2228 template<class ValidatorType, class EntryType>
2229 class ArrayValidator : public AbstractArrayValidator<ValidatorType, EntryType>{
2230 
2231 public:
2232 
2235 
2242  AbstractArrayValidator<ValidatorType, EntryType>(prototypeValidator){}
2243 
2245 
2248 
2250  virtual void validate(ParameterEntry const &entry, std::string const &paramName,
2251  std::string const &sublistName) const;
2252 
2254  const std::string getXMLTypeName() const{
2255  return "ArrayValidator(" +
2256  this->getPrototype()->getXMLTypeName() + ", " +
2258  }
2259 
2261  virtual void printDoc(std::string const &docString, std::ostream &out) const
2262  {
2263  StrUtils::printLines(out,"# ",docString);
2264  std::string toPrint;
2265  toPrint += "ArrayValidator:\n";
2266  toPrint += "Prototype Validator:\n";
2267  this->getPrototype()->printDoc(toPrint, out);
2268  }
2269 
2271 
2272 };
2273 
2274 template<class ValidatorType, class EntryType>
2275 void ArrayValidator<ValidatorType, EntryType>::validate(ParameterEntry const &entry, std::string const &paramName,
2276  std::string const &sublistName) const
2277 {
2278  any anyValue = entry.getAny(true);
2279  const std::string &entryName = entry.getAny(false).typeName();
2280  TEUCHOS_TEST_FOR_EXCEPTION(anyValue.type() != typeid(Array<EntryType>),
2282  "The \"" << paramName << "\"" <<
2283  " parameter in the \"" << sublistName <<
2284  "\" sublist is has an error." << std::endl << std::endl <<
2285  "Error: The value you entered was the wrong type." << std::endl <<
2286  "Parameter: " << paramName << std::endl <<
2287  "Type specified: " << entryName << std::endl <<
2288  "Type accepted: " << TypeNameTraits<Array<EntryType> >::name() <<
2289  std::endl << std::endl);
2290 
2291  Array<EntryType> extracted =
2292  getValue<Teuchos::Array<EntryType> >(entry);
2293  RCP<const ParameterEntryValidator> prototype = this->getPrototype();
2294  for(int i = 0; i<extracted.size(); ++i){
2295  ParameterEntry dummyParameter;
2296  dummyParameter.setValue(extracted[i]);
2297  try{
2298  prototype->validate(
2299  dummyParameter, paramName, sublistName);
2300  }
2302  std::stringstream oss;
2303  oss << "Array Validator Exception:" << std::endl <<
2304  "Bad Index: " << i << std::endl << e.what();
2305  throw Exceptions::InvalidParameterValue(oss.str());
2306  }
2307  }
2308 }
2309 
2315 template<class ValidatorType, class EntryType>
2316 class DummyObjectGetter<ArrayValidator<ValidatorType, EntryType> >{
2317 
2318 public:
2319 
2322 
2327 
2329 
2330 };
2331 
2332 template<class ValidatorType, class EntryType>
2335 {
2338 }
2339 
2340 
2350  public ArrayValidator<StringValidator, std::string>{
2351 
2352 public:
2353 
2356 
2359  ArrayValidator<StringValidator, std::string>(prototypeValidator){}
2360 
2362 
2363 };
2364 
2365 
2374 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT ArrayFileNameValidator : public ArrayValidator<FileNameValidator, std::string>{
2375 
2376 public:
2377 
2380 
2383  ArrayValidator<FileNameValidator, std::string>(prototypeValidator){}
2384 
2386 
2387 };
2388 
2389 
2397 template<class T>
2398 class ArrayNumberValidator : public ArrayValidator<EnhancedNumberValidator<T>, T>{
2399 public:
2402 
2405  RCP<const EnhancedNumberValidator<T> > prototypeValidator):
2406  ArrayValidator<EnhancedNumberValidator<T>, T>(prototypeValidator){}
2407 
2409 
2410 };
2411 
2412 
2413 
2414 // ///////////////////////////
2415 // Implementations
2416 
2417 
2418 //
2419 // StringToIntegralParameterEntryValidator
2420 //
2421 
2422 
2423 // Constructors
2424 
2425 
2426 template<class IntegralType>
2429  std::string const& defaultParameterName,
2430  const bool caseSensitive) :
2432  defaultParameterName_ (defaultParameterName),
2433  caseSensitive_ (caseSensitive)
2434 {
2435  const int length = static_cast<int>(strings.size());
2436  Array<IntegralType> integralValues(length);
2437  for (int i = 0; i < length; ++i) integralValues[i] = static_cast<IntegralType>(i);
2438  init(strings, integralValues);
2439  setValidValues (strings);
2440 }
2441 
2442 
2443 template<class IntegralType>
2446  ArrayView<const IntegralType> const& integralValues,
2447  std::string const& defaultParameterName,
2448  const bool caseSensitive) :
2450  defaultParameterName_ (defaultParameterName),
2451  caseSensitive_ (caseSensitive)
2452 {
2453 #ifdef TEUCHOS_DEBUG
2454  TEUCHOS_ASSERT_EQUALITY( strings.size(), integralValues.size() );
2455 #endif
2456  init(strings, integralValues);
2457  setValidValues (strings);
2458 }
2459 
2460 template<class IntegralType>
2463  ArrayView<const std::string> const& stringsDocs,
2464  ArrayView<const IntegralType> const& integralValues,
2465  std::string const& defaultParameterName,
2466  const bool caseSensitive) :
2468  defaultParameterName_ (defaultParameterName),
2469  caseSensitive_ (caseSensitive)
2470 {
2471 #ifdef TEUCHOS_DEBUG
2472  TEUCHOS_ASSERT_EQUALITY( strings.size(), stringsDocs.size() );
2473 #endif
2474 
2476  strings.size() != stringsDocs.size(),
2477  std::logic_error,
2478  "The input arrays strings and stringsDocs must have the same length.");
2479 
2480  init(strings, integralValues);
2481  setValidValues(strings,&stringsDocs);
2482 }
2483 
2484 template <class IntegralType>
2486  ArrayView<const std::string> const &strings,
2487  ArrayView<const IntegralType> const &integralValues) {
2488 
2489 #ifdef TEUCHOS_DEBUG
2490  TEUCHOS_ASSERT_EQUALITY(strings.size(), integralValues.size());
2491 #endif
2492 
2494  strings.size() != integralValues.size(), std::logic_error,
2495  "The input arrays strings and integralValues must have the same length.");
2496 
2497  typedef typename map_t::value_type val_t;
2498  typedef typename inv_map_t::value_type inv_val_t;
2499  for (int i = 0; i < static_cast<int>(strings.size()); ++i) {
2500  const std::string name =
2501  caseSensitive_ ? strings[i] : upperCase(strings[i]);
2502  const bool unique = map_.insert(val_t(name, integralValues[i])).second;
2503  TEUCHOS_TEST_FOR_EXCEPTION(!unique, std::logic_error,
2504  "For parameter \"" << defaultParameterName_
2505  << "\": "
2506  "strings["
2507  << i << "] = \"" << strings[i]
2508  << "\" is a duplicate.");
2509  inv_map_.insert(inv_val_t(integralValues[i], name));
2510  }
2511 }
2512 
2513 // Lookup functions
2514 
2515 
2516 template<class IntegralType>
2517 IntegralType
2519  const std::string &str, const std::string &paramName
2520  ,const std::string &sublistName
2521  ) const
2522 {
2523  typename map_t::const_iterator itr = map_.find (caseSensitive_ ? str : upperCase (str));
2525  itr == map_.end(), Exceptions::InvalidParameterValue
2526  ,"Error, the value \"" << str << "\" is not recognized for the parameter \""
2527  << ( paramName.length() ? paramName : defaultParameterName_ ) << "\""
2528  << "\nin the sublist \"" << sublistName << "\"."
2529  << "\n\nValid values include:"
2530  << "\n {\n"
2531  << validValues_
2532  << " }"
2533  );
2534  return (*itr).second;
2535 }
2536 
2537 
2538 template<class IntegralType>
2539 IntegralType
2541  const ParameterEntry &entry, const std::string &paramName
2542  ,const std::string &sublistName, const bool activeQuery
2543  ) const
2544 {
2545  if (entry.isType<IntegralType>()){
2546  return any_cast<IntegralType>(entry.getAny(activeQuery));
2547  } else{
2548  const bool validType = ( entry.getAny(activeQuery).type() == typeid(std::string) );
2551  ,"Error, the parameter {paramName=\""<<(paramName.length()?paramName:defaultParameterName_)
2552  << "\",type=\""<<entry.getAny(activeQuery).typeName()<<"\"}"
2553  << "\nin the sublist \"" << sublistName << "\""
2554  << "\nhas the wrong type."
2555  << "\n\nThe correct type is \"string\"!"
2556  );
2557  const std::string
2558  &strValue = any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail!
2559  return getIntegralValue(strValue,paramName,sublistName); // This will validate the value and throw!
2560  }
2561 }
2562 
2563 
2564 template<class IntegralType>
2565 std::string
2567  const ParameterEntry &entry, const std::string &paramName
2568  ,const std::string &sublistName, const bool activeQuery
2569  ) const
2570 {
2571  if (entry.isType<IntegralType>()){
2572  const IntegralType intVal = any_cast<IntegralType>(entry.getAny(activeQuery));
2573  typename inv_map_t::const_iterator itr = inv_map_.find(intVal);
2574  // typename inv_map_t::const_iterator itr = inv_map_.find(intVal);
2575  // TODO: Maybe do a test on intVal but it should be valid by construction
2576  return (*itr).second;
2577  } else{
2578  // Validate the parameter's type and value
2579  this->getIntegralValue(entry,paramName,sublistName,activeQuery);
2580  // Return the std::string value which is now validated!
2581  return any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail!
2582  }
2583 }
2584 
2585 
2586 template<class IntegralType>
2587 IntegralType
2589  ParameterList &paramList, const std::string &paramName
2590  ,const std::string &defaultValue
2591  ) const
2592 {
2593  const std::string& strValue =
2594  paramList.get (paramName,
2595  caseSensitive_ ? defaultValue : upperCase (defaultValue));
2596  return getIntegralValue (strValue, paramName, paramList.name ());
2597 }
2598 
2599 
2600 template<class IntegralType>
2601 std::string
2603  ParameterList &paramList, const std::string &paramName
2604  ,const std::string &defaultValue
2605  ) const
2606 {
2607  const std::string& strValue =
2608  paramList.get (paramName,
2609  caseSensitive_ ? defaultValue : upperCase (defaultValue));
2610  getIntegralValue(strValue,paramName,paramList.name()); // Validate!
2611  return strValue;
2612 }
2613 
2614 template<class IntegralType>
2617 {
2618  return validStringValuesDocs_;
2619 }
2620 
2621 template<class IntegralType>
2622 const std::string&
2624 {
2625  return defaultParameterName_;
2626 }
2627 
2628 template<class IntegralType>
2629 std::string
2631  const std::string &str, const std::string &paramName
2632  ,const std::string &sublistName
2633  ) const
2634 {
2635  getIntegralValue (caseSensitive_ ? str : upperCase (str),
2636  paramName,
2637  sublistName); // Validate!
2638  return str;
2639 }
2640 
2641 
2642 // Overridden from ParameterEntryValidator
2643 
2644 template<class IntegralType>
2645 const std::string
2647  return "StringIntegralValidator(" + TypeNameTraits<IntegralType>::name () + ")";
2648 }
2649 
2650 template<class IntegralType>
2652  std::string const& docString
2653  ,std::ostream & out
2654  ) const
2655 {
2656  StrUtils::printLines(out,"# ",docString);
2657  out << "# Valid std::string values:\n";
2658  out << "# {\n";
2659  if(validStringValuesDocs_.get()) {
2660  for( int i = 0; i < static_cast<int>(validStringValues_->size()); ++i ) {
2661  out << "# \"" << (*validStringValues_)[i] << "\"\n";
2662  StrUtils::printLines(out,"# ",(*validStringValuesDocs_)[i] );
2663  }
2664  }
2665  else {
2666  StrUtils::printLines(out,"# ",validValues_);
2667  // Note: Above validValues_ has for initial spaces already so indent should
2668  // be correct!
2669  }
2670  out << "# }\n";
2671 }
2672 
2673 
2674 template<class IntegralType>
2677 {
2678  return validStringValues_;
2679 }
2680 
2681 
2682 template<class IntegralType>
2684  ParameterEntry const& entry
2685  ,std::string const& paramName
2686  ,std::string const& sublistName
2687  ) const
2688 {
2689  this->getIntegralValue (entry, paramName, sublistName, false);
2690 }
2691 
2692 
2693 #if defined(HAVE_TEUCHOS_MODIFY_DEFAULTS_DURING_VALIDATION)
2694 template<class IntegralType>
2696  std::string const& paramName,
2697  std::string const& sublistName,
2698  ParameterEntry * entry
2699  ) const
2700 {
2701  entry->setValue(this->getIntegralValue(*entry, paramName, sublistName, false));
2702 }
2703 #endif
2704 
2705 
2706 // private
2707 
2708 template<class IntegralType>
2710  ArrayView<const std::string> const& strings
2711  ,ArrayView<const std::string> const* stringsDocs
2712  )
2713 {
2714  if (caseSensitive_) {
2715  validStringValues_ = rcp (new Array<std::string> (strings));
2716  }
2717  else {
2718  RCP<Array<std::string> > vals (new Array<std::string> (strings.size ()));
2719  for (Array<std::string>::size_type i = 0; i < strings.size (); ++i) {
2720  (*vals)[i] = upperCase (strings[i]);
2721  }
2722  validStringValues_ = rcp_const_cast<const Array<std::string> > (vals);
2723  }
2724 
2725  if (stringsDocs) {
2726  validStringValuesDocs_ = rcp (new Array<std::string> (*stringsDocs));
2727  }
2728  // Build the list of valid values in the same order as passed in by the client.
2729  std::ostringstream oss;
2730  for (int i = 0; i < static_cast<int> (strings.size()); ++i) {
2731  oss << " \"" << strings[i] << "\"\n";
2732  }
2733  // Note: Above four spaces is designed for the error output above.
2734  validValues_ = oss.str();
2735 }
2736 
2737 
2738 } // namespace Teuchos
2739 
2740 
2741 //
2742 // Nonmember function implementations for StringToIntegralParameterEntryValidator
2743 //
2744 
2745 
2746 template<class IntegralType>
2747 inline
2749 Teuchos::stringToIntegralParameterEntryValidator(
2750  ArrayView<const std::string> const& strings,
2751  std::string const& defaultParameterName
2752  )
2753 {
2754  return rcp(
2755  new StringToIntegralParameterEntryValidator<IntegralType>(
2756  strings, defaultParameterName
2757  )
2758  );
2759 }
2760 
2761 
2762 template<class IntegralType>
2763 inline
2765 Teuchos::stringToIntegralParameterEntryValidator(
2766  ArrayView<const std::string> const& strings,
2767  std::string const& defaultParameterName,
2768  const bool caseSensitive
2769  )
2770 {
2771  typedef StringToIntegralParameterEntryValidator<IntegralType> ret_type;
2772  return rcp (new ret_type (strings, defaultParameterName, caseSensitive));
2773 }
2774 
2775 
2776 
2777 template<class IntegralType>
2778 inline
2780 Teuchos::stringToIntegralParameterEntryValidator(
2781  ArrayView<const std::string> const& strings,
2782  ArrayView<const IntegralType> const& integralValues,
2783  std::string const& defaultParameterName
2784  )
2785 {
2786  return rcp(
2787  new StringToIntegralParameterEntryValidator<IntegralType>(
2788  strings, integralValues, defaultParameterName
2789  )
2790  );
2791 }
2792 
2793 
2794 template<class IntegralType>
2795 inline
2797 Teuchos::stringToIntegralParameterEntryValidator(
2798  ArrayView<const std::string> const& strings,
2799  ArrayView<const IntegralType> const& integralValues,
2800  std::string const& defaultParameterName,
2801  const bool caseSensitive)
2802 {
2803  typedef StringToIntegralParameterEntryValidator<IntegralType> ret_type;
2804  return rcp (new ret_type (strings, integralValues,
2805  defaultParameterName, caseSensitive));
2806 }
2807 
2808 
2809 template<class IntegralType>
2810 inline
2812 Teuchos::stringToIntegralParameterEntryValidator(
2813  ArrayView<const std::string> const& strings,
2814  ArrayView<const std::string> const& stringsDocs,
2815  ArrayView<const IntegralType> const& integralValues,
2816  std::string const& defaultParameterName
2817  )
2818 {
2819  return rcp(
2820  new StringToIntegralParameterEntryValidator<IntegralType>(
2821  strings, stringsDocs, integralValues, defaultParameterName
2822  )
2823  );
2824 }
2825 
2826 
2827 template<class IntegralType>
2828 inline
2830 Teuchos::stringToIntegralParameterEntryValidator(
2831  ArrayView<const std::string> const& strings,
2832  ArrayView<const std::string> const& stringsDocs,
2833  ArrayView<const IntegralType> const& integralValues,
2834  std::string const& defaultParameterName,
2835  const bool caseSensitive)
2836 {
2837  typedef StringToIntegralParameterEntryValidator<IntegralType> ret_type;
2838  return rcp (new ret_type (strings, stringsDocs, integralValues,
2839  defaultParameterName, caseSensitive));
2840 }
2841 
2842 
2843 template<class IntegralType>
2844 void Teuchos::setStringToIntegralParameter(
2845  std::string const& paramName,
2846  std::string const& defaultValue,
2847  std::string const& docString,
2848  ArrayView<const std::string> const& strings,
2849  ParameterList * paramList
2850  )
2851 {
2852  typedef ParameterEntryValidator PEV;
2853  TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
2854  paramList->set(
2855  paramName, defaultValue, docString,
2856  rcp_implicit_cast<const PEV>(
2857  stringToIntegralParameterEntryValidator<IntegralType>(
2858  strings, paramName
2859  )
2860  )
2861  );
2862 }
2863 
2864 
2865 template<class IntegralType>
2866 void Teuchos::setStringToIntegralParameter(
2867  std::string const& paramName,
2868  std::string const& defaultValue,
2869  std::string const& docString,
2870  ArrayView<const std::string> const& strings,
2871  ArrayView<const IntegralType> const& integralValues,
2872  ParameterList * paramList
2873  )
2874 {
2875  typedef ParameterEntryValidator PEV;
2876  TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
2877  paramList->set(
2878  paramName, defaultValue, docString,
2879  rcp_implicit_cast<const PEV>(
2880  stringToIntegralParameterEntryValidator<IntegralType>(
2881  strings, integralValues, paramName
2882  )
2883  )
2884  );
2885 }
2886 
2887 
2888 template<class IntegralType>
2889 void Teuchos::setStringToIntegralParameter(
2890  std::string const& paramName,
2891  std::string const& defaultValue,
2892  std::string const& docString,
2893  ArrayView<const std::string> const& strings,
2894  ArrayView<const std::string> const& stringsDocs,
2895  ArrayView<const IntegralType> const& integralValues,
2896  ParameterList * paramList
2897  )
2898 
2899 {
2900  typedef ParameterEntryValidator PEV;
2901  TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
2902  paramList->set(
2903  paramName, defaultValue, docString,
2904  rcp_implicit_cast<const PEV>(
2905  stringToIntegralParameterEntryValidator<IntegralType>(
2906  strings, stringsDocs, integralValues, paramName
2907  )
2908  )
2909  );
2910 }
2911 
2912 
2913 template<class IntegralType>
2914 IntegralType Teuchos::getIntegralValue(
2915  ParameterList const& paramList, std::string const& paramName
2916  )
2917 {
2918  const ParameterEntry &entry = paramList.getEntry(paramName);
2919  if (entry.isType<IntegralType>()){
2920  return getValue<IntegralType>(entry);
2921  } else{
2922  RCP<const StringToIntegralParameterEntryValidator<IntegralType> >
2923  integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>(
2924  entry, paramList, paramName
2925  );
2926  return integralValidator->getIntegralValue(
2927  entry, paramName, paramList.name(), true );
2928  }
2929 }
2930 
2931 
2932 template<class IntegralType>
2933 std::string Teuchos::getStringValue(
2934  ParameterList const& paramList, std::string const& paramName
2935  )
2936 {
2937  const ParameterEntry &entry = paramList.getEntry(paramName);
2938  RCP<const StringToIntegralParameterEntryValidator<IntegralType> >
2939  integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>(
2940  entry, paramList, paramName
2941  );
2942  return integralValidator->getStringValue(
2943  entry, paramName, paramList.name(), true
2944  );
2945 }
2946 
2947 
2948 template<class IntegralType>
2951  ParameterEntry const& entry, ParameterList const& paramList,
2952  std::string const& paramName
2953  )
2954 {
2955  const RCP<const ParameterEntryValidator> validator = entry.validator();
2957  is_null(validator), Exceptions::InvalidParameterType,
2958  "Error! The parameter \""<<paramName<<"\" exists\n"
2959  "in the parameter (sub)list \""<<paramList.name()<<"\"\n"
2960  "but it does not contain any validator needed to extract\n"
2961  "an integral value of type \""<<TypeNameTraits<IntegralType>::name()<<"\"!"
2962  );
2963  const RCP<const StringToIntegralParameterEntryValidator<IntegralType> > integralValidator =
2964  rcp_dynamic_cast<const StringToIntegralParameterEntryValidator<IntegralType> >(
2965  validator
2966  );
2968  is_null(integralValidator), Exceptions::InvalidParameterType,
2969  "Error! The parameter \""<<paramName<<"\" exists\n"
2970  "in the parameter (sub)list \""<<paramList.name()<<"\"\n"
2971  "but it contains the wrong type of validator. The expected validator type\n"
2972  "is \""<<TypeNameTraits<StringToIntegralParameterEntryValidator<IntegralType> >::name()<<"\"\n"
2973  "but the contained validator type is \""<<typeName(*validator)<<"\"!"
2974  );
2975  return integralValidator;
2976 }
2977 
2978 
2979 #endif // TEUCHOS_STANDARD_PARAMETER_ENTRY_VALIDATORS_H
static bool mustAlreadyExistDefault()
The default value of the mustAlreadyExist parameter in the constructor.
A thin wrapper around the Teuchos Array class that allows for 2 dimensional arrays.
std::string validateString(const std::string &str, const std::string &paramName="", const std::string &sublistName="") const
Validate the std::string and pass it on.
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object...
const std::string & name() const
The name of this ParameterList.
T getMax() const
Gets the maximum acceptable value for the validator.
bool isCaseSensitive() const
Whether this validator is case sensitive.
EVerbosityLevel
Verbosity level.
bool isCaseSensitive() const
Whether this validator is case sensitive.
EnhancedNumberValidator()
Constructs a EnhancedNumberValidator without an explicit minimum or maximum.
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
Convience class for StringValidators that are to be applied to TwoDArrays.
void printDoc(std::string const &docString, std::ostream &out) const
Print documentation to the given output string.
static RCP< T > getDummyObject()
Retrieves a dummy object of type T.
RCP< const StringToIntegralParameterEntryValidator< IntegralType > > getStringToIntegralParameterEntryValidator(ParameterEntry const &entry, ParameterList const &paramList, std::string const &paramName)
Get a StringToIntegralParameterEntryValidator&lt;IntegralType&gt; object out of a ParameterEntry object...
void setValue(T value, bool isDefault=false, const std::string &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Templated set method that uses the input value type to determine the type of parameter.
Convience class for EnhancedNumberValidators that are to be applied to arrays.
void validateAndModify(std::string const &paramName, std::string const &sublistName, ParameterEntry *entry) const
RCP< T2 > rcp_dynamic_cast(const RCP< T1 > &p1, bool throw_on_fail=false)
Dynamic cast of underlying RCP type from T1* to T2*.
EPreferredType
Determines what type is the preferred type.
T & get(const std::string &name, T def_value)
Return the parameter&#39;s value, or the default value if it is not there.
This object is held as the &quot;value&quot; in the Teuchos::ParameterList std::map.
T minVal
The minimum value accepted by the validator.
virtual void validate(ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
void validate(ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const
Default structure used by EnhancedNumberTraits&lt;T&gt; to produce a compile time error when the specializa...
void setMax(T max)
Sets the maximum acceptable value for the validator.
size_type size() const
The total number of items in the managed array.
Standard implementation of a ParameterEntryValidator that maps from a list of strings to an enum or i...
AcceptedTypes & allowString(bool _allowString)
Set allow an std::string value or not.
bool isType() const
Test the type of the data being contained.
bool hasMax() const
Determines whether or not the validator has a maximum value.
void init(const ArrayView< const std::string > &strings, const ArrayView< const IntegralType > &integralValues)
Auxiliary method to simplify constructors.
T getMin() const
Gets the minimum acceptable value for the validator.
static std::ostream & printLines(std::ostream &os, const std::string &linePrefix, const std::string &lines)
Print lines with prefix first.
ValidStringsList validStrings_
An array containing a list of all the valid string values.
AbstractArrayValidator()
Hidden default constructor.
Modified boost::any class, which is a container for a templated value.
static EPreferredType getPrefferedTypeStringEnum(const std::string &enumString)
Gets the preferred type enum associated with a give string.
bool is_null(const ArrayRCP< T > &p)
Returns true if p.get()==NULL.
AcceptedTypes & allowInt(bool _allowInt)
Set allow an int value or not.
AbstractArrayValidator(RCP< const ValidatorType > prototypeValidator)
Constructs an AbstractArrayValidator.
Takes a validator, wraps it, and applies it to a TwoDArray.
bool hasMin() const
Determines whether or not the validator has a minimum value.
unsigned short getPrecision() const
Gets the precision specified for the validator.
virtual void validateAndModify(std::string const &paramName, std::string const &sublistName, ParameterEntry *entry) const
Validate and perhaps modify a parameter entry&#39;s value.
A std::string utilities class for Teuchos.
A thin wrapper around the Array class which causes it to be interpreted as a 2D Array.
RCP< const ValidatorType > getPrototype() const
Returns the prototype validator for this Array Validator.
void printDoc(std::string const &docString, std::ostream &out) const
Convience class for StringValidators that are to be applied to arrays.
static T notDefined()
This function should not compile if there is an attempt to instantiate!
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Templated Parameter List class.
static unsigned short defaultPrecision()
Gets the default precision with which the number type should be displayed.
void validate(ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const
Validate the given ParameterEntry.
bool containsMax
Whetehr or not a maximum value has been specified for this validator.
Teuchos::any getNumberFromString(const ParameterEntry &entry, const bool activeQuery) const
std::string toString(const HashSet< Key > &h)
std::string getStringValue(const ParameterEntry &entry, const std::string &paramName="", const std::string &sublistName="", const bool activeQuery=true) const
Find the string value for the given ParameterEntry.
ArrayStringValidator(RCP< const StringValidator > prototypeValidator)
Convience class for EnhancedNumberValidators that are to be applied to TwoDArray. ...
An abstract base class for all ArrayValidators.
Standard implementation of a BoolParameterEntryValidator that accepts bool values (true/false) or str...
ArrayValidator(RCP< const ValidatorType > prototypeValidator)
Constructs a ArrayValidator.
Convience class for FileNameValidators that are to be applied to TwoDArrays.
AcceptedTypes(bool allowAllTypesByDefault=true)
Allow all types or not on construction.
void setStep(T step)
Sets the step being used for the validator.
Takes a validator, wraps it, and applies it to an array.
A list of parameters of arbitrary type.
bool mustAlreadyExist_
Whether or not the file specified in the parameter should already exist.
A simple validator that only allows certain string values to be choosen or simply enforces that a par...
static T min()
Gets the minimum possible value the number type can take on.
#define TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT
TwoDArrayValidator(RCP< const ValidatorType > prototypeValidator)
Constructs a ArrayValidator.
Abstract interface for an object that can validate a ParameterEntry&#39;s value.
any & getAny(bool activeQry=true)
Direct access to the Teuchos::any data value underlying this object. The bool argument activeQry (def...
void setPrecision(unsigned short precision)
Sets the precision specified for the validator.
IntegralType getIntegralValue(const std::string &str, const std::string &paramName="", const std::string &sublistName="") const
For a string value, find its corresponding enum or integer value.
void setMin(T min)
Sets the minimum acceptable value for the validator.
EnhancedNumberValidator(T min, T max, T step=EnhancedNumberTraits< T >::defaultStep(), unsigned short precision=EnhancedNumberTraits< T >::defaultPrecision())
Constructs a EnhancedNumberValidator.
virtual void printDoc(std::string const &docString, std::ostream &out) const
Nonowning array view.
RCP< const Array< std::string > > ValidStringsList
Default traits class that just returns typeid(T).name().
Class for retrieving a dummy object of type T.
unsigned short precision_
The number of decimal places with which the nubmer will be displayed in a UI. This value is meaningle...
static const std::string & getPrefferedTypeString(EPreferredType enumValue)
Gets the string representation of a given preferred type enum.
static std::string upperCase(const std::string s)
Return an upper-case copy of the string s.
ArrayFileNameValidator(RCP< const FileNameValidator > prototypeValidator)
static T defaultStep()
gets default amount a value of the number type should be incremented by when being utilizied in a UI...
static T max()
Gets the maximum possible value the number type can take on.
Class uesd to validate a particular type of number.
Convience class for FileNameValidators that are to be applied to arrays.
void setValidValues(ArrayView< const std::string > const &strings, ArrayView< const std::string > const *stringsDocs=NULL)
static std::string upperCase(const std::string s)
Return an upper-case copy of the string s.
Class defining the traits of the number type being used in an EnhancedNumberValidator.
virtual void printDoc(std::string const &docString, std::ostream &out) const
std::string typeName() const
Return the name of the type.
TwoDArrayNumberValidator(RCP< const EnhancedNumberValidator< T > > prototypeValidator)
AcceptedTypes & allowDouble(bool _allowDouble)
Set allow a double value or not.
ValidStringsList getStringDocs() const
Get a pointer to the array containing all the documentation strings.
Smart reference counting pointer class for automatic garbage collection.
TwoDArrayStringValidator(RCP< const StringValidator > prototypeValidator)
const std::type_info & type() const
Return the type of value being stored.
TwoDArrayFileNameValidator(RCP< const FileNameValidator > prototypeValidator)
virtual void validate(ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
This macro is checks that to numbers are equal and if not then throws an exception with a good error ...
const std::string & getDefaultParameterName() const
Get the name of the default parameter for the validator.
T step_
The increment to use when increaseing or decreaseing the value the validator is validating.
Standard implementation of a ParameterEntryValidator that accepts numbers from a number of different ...
T getStep() const
Gets the step being used for the validator.
Defines basic traits returning the name of a type in a portable and readable way. ...
AcceptedTypes & allowLongLong(bool _allowLongLong)
Set allow an long long value or not.
bool containsMin
Whether or not a minimum value has been specified for this validator.
#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...
T maxVal
The maximum value accepted by the validator.
RCP< const ValidatorType > prototypeValidator_
The prototype validator to be applied to each entry in the Array.
#define TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
ArrayNumberValidator(RCP< const EnhancedNumberValidator< T > > prototypeValidator)