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 //
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 #ifndef TEUCHOS_STANDARD_PARAMETER_ENTRY_VALIDATORS_H
43 #define TEUCHOS_STANDARD_PARAMETER_ENTRY_VALIDATORS_H
44 
49 #include "Teuchos_TwoDArray.hpp"
50 #include "Teuchos_Assert.hpp"
51 #include "Teuchos_StrUtils.hpp"
54 
55 #ifdef HAVE_TEUCHOSCORE_QUADMATH
56 # include <quadmath.h> // __float128 constants and functions
57 #endif // HAVE_TEUCHOSCORE_QUADMATH
58 
59 #include <locale>
60 
61 
62 namespace Teuchos {
63 
91 template<class IntegralType>
93 public:
96 
115  const std::string& defaultParameterName,
116  const bool caseSensitive = true);
117 
142  const ArrayView<const IntegralType>& integralValues,
143  std::string const& defaultParameterName,
144  const bool caseSensitive = true);
145 
176  const ArrayView<const std::string>& stringsDocs,
177  const ArrayView<const IntegralType>& integralValues,
178  const std::string& defaultParameterName,
179  const bool caseSensitive = true);
181 
183 
194  IntegralType getIntegralValue(
195  const std::string &str, const std::string &paramName = "",
196  const std::string &sublistName = ""
197  ) const;
198 
214  IntegralType
215  getIntegralValue (const ParameterEntry &entry,
216  const std::string &paramName = "",
217  const std::string &sublistName = "",
218  const bool activeQuery = true) const;
219 
235  std::string
236  getStringValue (const ParameterEntry &entry,
237  const std::string &paramName = "",
238  const std::string &sublistName = "",
239  const bool activeQuery = true) const;
240 
247  IntegralType
248  getIntegralValue (ParameterList& paramList,
249  const std::string& paramName,
250  const std::string& defaultValue) const;
251 
255  std::string getStringValue(
256  ParameterList &paramList, const std::string &paramName,
257  const std::string &defaultValue
258  ) const;
259 
266 
271  const std::string& getDefaultParameterName() const;
272 
284  std::string validateString(
285  const std::string &str, const std::string &paramName = "",
286  const std::string &sublistName = ""
287  ) const;
288 
293  bool isCaseSensitive () const {
294  return caseSensitive_;
295  }
296 
298 
300 
302  const std::string getXMLTypeName() const;
303 
305  void printDoc(
306  std::string const& docString,
307  std::ostream & out
308  ) const;
309 
312  validStringValues() const;
313 
315  void validate(
316  ParameterEntry const& entry,
317  std::string const& paramName,
318  std::string const& sublistName
319  ) const;
320 
321 #if defined(HAVE_TEUCHOS_MODIFY_DEFAULTS_DURING_VALIDATION)
322  void validateAndModify(
323  std::string const& paramName,
324  std::string const& sublistName,
325  ParameterEntry * entry
326  ) const;
327 #endif
328 
330 
331 private:
333  std::string validValues_;
336 
337  typedef std::map<std::string,IntegralType> map_t;
339  typedef std::map<IntegralType,std::string> inv_map_t;
341 
342  const bool caseSensitive_;
343 
355  void init(const ArrayView<const std::string>& strings,
356  const ArrayView<const IntegralType>& integralValues);
357 
358  void setValidValues(
359  ArrayView<const std::string> const& strings,
360  ArrayView<const std::string> const* stringsDocs = NULL
361  );
362 
363  // Not defined and not to be called.
365 
367  static std::string upperCase (const std::string s) {
368  std::string s_upper = s;
369  std::transform (s_upper.begin (), s_upper.end (), s_upper.begin (), ::toupper);
370  return s_upper;
371  }
372 };
373 
374 
379 template<class IntegralType>
380 RCP<StringToIntegralParameterEntryValidator<IntegralType> >
381 stringToIntegralParameterEntryValidator(
382  ArrayView<const std::string> const& strings,
383  std::string const& defaultParameterName
384  );
385 
386 
391 template<class IntegralType>
392 RCP<StringToIntegralParameterEntryValidator<IntegralType> >
393 stringToIntegralParameterEntryValidator(
394  ArrayView<const std::string> const& strings,
395  std::string const& defaultParameterName,
396  const bool caseSensitive
397  );
398 
399 
404 template<class IntegralType>
405 RCP<StringToIntegralParameterEntryValidator<IntegralType> >
406 stringToIntegralParameterEntryValidator(
407  ArrayView<const std::string> const& strings,
408  ArrayView<const IntegralType> const& integralValues,
409  std::string const& defaultParameterName
410  );
411 
412 
417 template<class IntegralType>
418 RCP<StringToIntegralParameterEntryValidator<IntegralType> >
419 stringToIntegralParameterEntryValidator(
420  ArrayView<const std::string> const& strings,
421  ArrayView<const IntegralType> const& integralValues,
422  std::string const& defaultParameterName,
423  const bool caseSensitive
424  );
425 
426 
431 template<class IntegralType>
432 RCP<StringToIntegralParameterEntryValidator<IntegralType> >
433 stringToIntegralParameterEntryValidator(
434  ArrayView<const std::string> const& strings,
435  ArrayView<const std::string> const& stringsDocs,
436  ArrayView<const IntegralType> const& integralValues,
437  std::string const& defaultParameterName
438  );
439 
440 
445 template<class IntegralType>
446 RCP<StringToIntegralParameterEntryValidator<IntegralType> >
447 stringToIntegralParameterEntryValidator(
448  ArrayView<const std::string> const& strings,
449  ArrayView<const std::string> const& stringsDocs,
450  ArrayView<const IntegralType> const& integralValues,
451  std::string const& defaultParameterName,
452  const bool caseSensitive
453  );
454 
455 
466 template<class IntegralType>
467 void setStringToIntegralParameter(
468  std::string const& paramName,
469  std::string const& defaultValue,
470  std::string const& docString,
471  ArrayView<const std::string> const& strings,
472  ParameterList * paramList
473  );
474 
475 
487 template<class IntegralType>
488 void setStringToIntegralParameter(
489  std::string const& paramName,
490  std::string const& defaultValue,
491  std::string const& docString,
492  ArrayView<const std::string> const& strings,
493  ArrayView<const IntegralType> const& integralValues,
494  ParameterList * paramList
495  );
496 
497 
509 template<class IntegralType>
510 void setStringToIntegralParameter(
511  std::string const& paramName,
512  std::string const& defaultValue,
513  std::string const& docString,
514  ArrayView<const std::string> const& strings,
515  ArrayView<const std::string> const& stringsDocs,
516  ArrayView<const IntegralType> const& integralValues,
517  ParameterList * paramList
518  );
519 
520 
531 template<class IntegralType>
532 IntegralType getIntegralValue(
533  ParameterList const& paramList, std::string const& paramName
534  );
535 
536 
548 template<class IntegralType>
549 std::string getStringValue(
550  ParameterList const& paramList, std::string const& paramName
551  );
552 
553 
559 template<class IntegralType>
560 RCP<const StringToIntegralParameterEntryValidator<IntegralType> >
562  ParameterEntry const& entry, ParameterList const& paramList,
563  std::string const& paramName
564  );
565 
566 
572 std::string getVerbosityLevelParameterValueName(
573  const EVerbosityLevel verbLevel
574  );
575 
576 
581 RCP<StringToIntegralParameterEntryValidator<EVerbosityLevel> >
582 verbosityLevelParameterEntryValidator(std::string const& defaultParameterName);
583 
589 template<class IntegralType>
591 
592 public:
593 
596 
601  getDummyObject();
602 
604 };
605 
606 template<class IntegralType>
609 {
610  return stringToIntegralParameterEntryValidator<IntegralType>(
611  tuple<std::string>(""), tuple<std::string>(""),
612  tuple<IntegralType>((IntegralType)1), "");
613 }
614 
628  : public ParameterEntryValidator
629 {
630 public:
631 
634 
636 
638 
641 
643  bool getBool(
644  const ParameterEntry &entry, const std::string &paramName = "",
645  const std::string &sublistName = "", const bool activeQuery = true
646  ) const;
647 
651  bool getBool(
652  ParameterList &paramList, const std::string &paramName,
653  const int defaultValue
654  ) const;
655 
657 
660 
662  const std::string getXMLTypeName() const;
663 
665  void printDoc(
666  std::string const& docString,
667  std::ostream & out
668  ) const;
669 
672  validStringValues() const;
673 
675  void validate(
676  ParameterEntry const& entry,
677  std::string const& paramName,
678  std::string const& sublistName
679  ) const;
680 
682  void validateAndModify(
683  std::string const& paramName,
684  std::string const& sublistName,
685  ParameterEntry * entry
686  ) const;
687 
689 
690 private:
691 
692  // ////////////////////////////
693  // Private data members
694 
695  std::string acceptedTypesString_;
696 
697  // ////////////////////////////
698  // Private member functions
699 
700  void finishInitialization();
701 
702  void throwTypeError(
703  ParameterEntry const& entry,
704  std::string const& paramName,
705  std::string const& sublistName
706  ) const;
707 };
708 
709 // Nonmember helper functions
710 
711 
717 boolParameterEntryValidator();
718 
719 
734  : public ParameterEntryValidator
735 {
736 public:
737 
740 
742  enum EPreferredType { PREFER_INT, PREFER_LONG_LONG, PREFER_DOUBLE, PREFER_STRING };
743 
744 
747  public:
749  AcceptedTypes( bool allowAllTypesByDefault = true )
750  :allowInt_(allowAllTypesByDefault)
751  ,allowLongLong_(allowAllTypesByDefault)
752  ,allowDouble_(allowAllTypesByDefault)
753  ,allowString_(allowAllTypesByDefault)
754  {}
756  AcceptedTypes& allowInt( bool _allowInt )
757  { allowInt_ = _allowInt; return *this; }
759  AcceptedTypes& allowLongLong( bool _allowLongLong )
760  { allowLongLong_ = _allowLongLong; return *this; }
762  AcceptedTypes& allowDouble( bool _allowDouble )
763  { allowDouble_ = _allowDouble; return *this; }
765  AcceptedTypes& allowString( bool _allowString )
766  { allowString_ = _allowString; return *this; }
768  bool allowInt() const { return allowInt_; }
770  bool allowLongLong() const { return allowLongLong_; }
772  bool allowDouble() const { return allowDouble_; }
774  bool allowString() const { return allowString_; }
775  private:
776  bool allowInt_;
780  };
781 
783 
786 
791 
803  EPreferredType const preferredType,
804  AcceptedTypes const& acceptedTypes
805  );
806 
808 
811 
817  int getInt(
818  const ParameterEntry &entry, const std::string &paramName = "",
819  const std::string &sublistName = "", const bool activeQuery = true
820  ) const;
821 
827  long long getLongLong(
828  const ParameterEntry &entry, const std::string &paramName = "",
829  const std::string &sublistName = "", const bool activeQuery = true
830  ) const;
831 
835  double getDouble(
836  const ParameterEntry &entry, const std::string &paramName = "",
837  const std::string &sublistName = "", const bool activeQuery = true
838  ) const;
839 
841  std::string getString(
842  const ParameterEntry &entry, const std::string &paramName = "",
843  const std::string &sublistName = "", const bool activeQuery = true
844  ) const;
845 
849  int getInt(
850  ParameterList &paramList, const std::string &paramName,
851  const int defaultValue
852  ) const;
853 
857  long long getLongLong(
858  ParameterList &paramList, const std::string &paramName,
859  const long long defaultValue
860  ) const;
861 
865  double getDouble(
866  ParameterList &paramList, const std::string &paramName,
867  const double defaultValue
868  ) const;
869 
873  std::string getString(
874  ParameterList &paramList, const std::string &paramName,
875  const std::string &defaultValue
876  ) const;
877 
880  bool isIntAllowed() const;
881 
884  bool isLongLongAllowed() const;
885 
888  bool isDoubleAllowed() const;
889 
892  bool isStringAllowed() const;
893 
896  EPreferredType getPreferredType() const;
897 
899  static const std::string& getPrefferedTypeString (EPreferredType enumValue)
900  {
901  switch (enumValue) {
902  case PREFER_INT:
903  return getIntEnumString ();
904  case PREFER_LONG_LONG:
905  return getLongLongEnumString ();
906  case PREFER_DOUBLE:
907  return getDoubleEnumString ();
908  case PREFER_STRING:
909  return getStringEnumString ();
910  default:
911  const std::string typeString (toString (enumValue));
912  throw std::runtime_error("Cannot convert enumValue: " + typeString + " to a string");
913  }
914  }
915 
917  static EPreferredType getPrefferedTypeStringEnum (const std::string& enumString)
918  {
919  if (enumString == getIntEnumString ()) {
920  return PREFER_INT;
921  }
922  else if (enumString == getLongLongEnumString ()) {
923  return PREFER_LONG_LONG;
924  }
925  else if (enumString == getDoubleEnumString ()) {
926  return PREFER_DOUBLE;
927  }
928  else if (enumString == getStringEnumString ()) {
929  return PREFER_STRING;
930  }
931  else {
932  throw std::runtime_error ("Cannot convert enumString: " + enumString + " to an enum");
933  }
934  }
935 
937 
940 
942  const std::string getXMLTypeName() const;
943 
945  void printDoc(
946  std::string const& docString,
947  std::ostream & out
948  ) const;
949 
951  ValidStringsList
952  validStringValues() const;
953 
955  void validate(
956  ParameterEntry const& entry,
957  std::string const& paramName,
958  std::string const& sublistName
959  ) const;
960 
962  void validateAndModify(
963  std::string const& paramName,
964  std::string const& sublistName,
965  ParameterEntry * entry
966  ) const;
967 
968 
970 
971 private:
972 
973  // ////////////////////////////
974  // Private data members
975 
977  std::string acceptedTypesString_;
978 
979 //use pragmas to disable some false-positive warnings for windows sharedlibs export
980 #ifdef _MSC_VER
981 #pragma warning(push)
982 #pragma warning(disable:4251)
983 #endif
985 #ifdef _MSC_VER
986 #pragma warning(pop)
987 #endif
988 
989  // ////////////////////////////
990  // Private member functions
991 
992  /* \brief Gets the string representing the "int" preferred type enum */
993  static const std::string& getIntEnumString(){
994  static const std::string intEnumString_ = TypeNameTraits<int>::name();
995  return intEnumString_;
996  }
997 
998  /* \brief Gets the string representing the "int" preferred type enum */
999  static const std::string& getLongLongEnumString(){
1000  static const std::string longLongEnumString_ = TypeNameTraits<long long>::name();
1001  return longLongEnumString_;
1002  }
1003 
1004  /* \brief Gets the string representing the "double" preferred type enum */
1005  static const std::string& getDoubleEnumString(){
1006  static const std::string doubleEnumString_ = TypeNameTraits<double>::name();
1007  return doubleEnumString_;
1008  }
1009 
1010  /* \brief Gets the string representing the "string" preferred type enum */
1011  static const std::string& getStringEnumString(){
1012  static const std::string stringEnumString_ = TypeNameTraits<std::string>::name();
1013  return stringEnumString_;
1014  }
1015 
1016 
1017  void finishInitialization();
1018 
1019  void throwTypeError(
1020  ParameterEntry const& entry,
1021  std::string const& paramName,
1022  std::string const& sublistName
1023  ) const;
1024 
1025 };
1026 
1027 
1028 // Nonmember helper functions
1029 
1030 
1035 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT RCP<AnyNumberParameterEntryValidator>
1036 anyNumberParameterEntryValidator();
1037 
1038 
1043 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT RCP<AnyNumberParameterEntryValidator>
1044 anyNumberParameterEntryValidator(
1046  AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
1047  );
1048 
1054 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setIntParameter(
1055  std::string const& paramName,
1056  int const value, std::string const& docString,
1057  ParameterList *paramList,
1058  AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
1059  = AnyNumberParameterEntryValidator::AcceptedTypes()
1060  );
1061 
1062 
1068 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setLongLongParameter(
1069  std::string const& paramName,
1070  long long const value, std::string const& docString,
1071  ParameterList *paramList,
1072  AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
1073  = AnyNumberParameterEntryValidator::AcceptedTypes()
1074  );
1075 
1081 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setDoubleParameter(
1082  std::string const& paramName,
1083  double const& value, std::string const& docString,
1084  ParameterList *paramList,
1085  AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
1086  = AnyNumberParameterEntryValidator::AcceptedTypes()
1087  );
1088 
1089 
1095 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setNumericStringParameter(
1096  std::string const& paramName,
1097  std::string const& value, std::string const& docString,
1098  ParameterList *paramList,
1099  AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
1100  = AnyNumberParameterEntryValidator::AcceptedTypes()
1101  );
1102 
1103 
1118 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT int getIntParameter(
1119  ParameterList const& paramList, std::string const& paramName
1120  );
1121 
1122 
1137 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT long long getLongLongParameter(
1138  ParameterList const& paramList, std::string const& paramName
1139  );
1140 
1155 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT double getDoubleParameter(
1156  ParameterList const& paramList,
1157  std::string const& paramName
1158  );
1159 
1160 
1176 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT std::string getNumericStringParameter(
1177  ParameterList const& paramList,
1178  std::string const& paramName
1179  );
1180 
1186 template<>
1188 
1189 public:
1190 
1193 
1197  static RCP<AnyNumberParameterEntryValidator > getDummyObject();
1198 
1200 
1201 };
1202 
1203 
1208 template <class T>
1211  static inline T notDefined() {
1212  return T::this_type_is_missing_a_specialization();
1213  }
1214 };
1215 
1216 
1237 template <class T>
1239 public:
1240 
1242  static inline T min()
1244 
1246  static inline T max()
1248 
1251  static inline T defaultStep()
1253 
1256  static inline unsigned short defaultPrecision()
1258 
1259 };
1260 
1261 
1262 template<>
1263 class EnhancedNumberTraits<short int>{
1264 public:
1265  static inline short int min() { return std::numeric_limits<short int>::min(); }
1266  static inline short int max() { return std::numeric_limits<short int>::max(); }
1267  static inline short int defaultStep() { return 1; }
1268  static inline unsigned short defaultPrecision() { return 0; }
1269 };
1270 
1271 
1272 template<>
1273 class EnhancedNumberTraits<short unsigned int>{
1274 public:
1275  static inline short unsigned int min() { return std::numeric_limits<short unsigned int>::min(); }
1276  static inline short unsigned int max() { return std::numeric_limits<short unsigned int>::max(); }
1277  static inline short unsigned int defaultStep() { return 1; }
1278  static inline unsigned short defaultPrecision() { return 0; }
1279 };
1280 
1281 
1282 template<>
1284 public:
1285  static inline int min() { return std::numeric_limits<int>::min(); }
1286  static inline int max() { return std::numeric_limits<int>::max(); }
1287  static inline int defaultStep() { return 1; }
1288  static inline unsigned short defaultPrecision() { return 0; }
1289 };
1290 
1291 
1292 template<>
1293 class EnhancedNumberTraits<unsigned int>{
1294 public:
1295  static inline unsigned int min() { return std::numeric_limits<unsigned int>::min(); }
1296  static inline unsigned int max() { return std::numeric_limits<unsigned int>::max(); }
1297  static inline unsigned int defaultStep() { return 1; }
1298  static inline unsigned short defaultPrecision() { return 0; }
1299 };
1300 
1301 
1302 template<>
1303 class EnhancedNumberTraits<long int>{
1304 public:
1305  static inline long int min() { return std::numeric_limits<long int>::min(); }
1306  static inline long int max() { return std::numeric_limits<long int>::max(); }
1307  static inline long int defaultStep() { return 1; }
1308  static inline unsigned short defaultPrecision() { return 0; }
1309 };
1310 
1311 
1312 template<>
1313 class EnhancedNumberTraits<long unsigned int>{
1314 public:
1315  static inline long unsigned int min() { return std::numeric_limits<long unsigned int>::min(); }
1316  static inline long unsigned int max() { return std::numeric_limits<long unsigned int>::max(); }
1317  static inline long unsigned int defaultStep() { return 1; }
1318  static inline unsigned short defaultPrecision() { return 0; }
1319 };
1320 
1321 
1322 template<>
1323 class EnhancedNumberTraits<long long int>{
1324 public:
1325  static inline long long int min() { return std::numeric_limits<long long int>::min(); }
1326  static inline long long int max() { return std::numeric_limits<long long int>::max(); }
1327  static inline long long int defaultStep() { return 1; }
1328  static inline unsigned short defaultPrecision() { return 0; }
1329 };
1330 
1331 
1332 template<>
1333 class EnhancedNumberTraits<long long unsigned int>{
1334 public:
1335  static inline long long unsigned int min() { return std::numeric_limits<long long unsigned int>::min(); }
1336  static inline long long unsigned int max() { return std::numeric_limits<long long unsigned int>::max(); }
1337  static inline long long unsigned int defaultStep() { return 1; }
1338  static inline unsigned short defaultPrecision() { return 0; }
1339 };
1340 
1341 
1342 #ifdef HAVE_TEUCHOSCORE_QUADMATH
1343 template<>
1344 class EnhancedNumberTraits<__float128>{
1345 public:
1346  static inline __float128 min() { return -std::numeric_limits<__float128>::max(); }
1347  static inline __float128 max() { return std::numeric_limits<__float128>::max(); }
1348  static inline __float128 defaultStep() { return 1; }
1349  static inline unsigned short defaultPrecision() { return 100; }
1350 };
1351 #endif // HAVE_TEUCHOSCORE_QUADMATH
1352 
1353 template<>
1354 class EnhancedNumberTraits<double>{
1355 public:
1356  static inline double min() { return -std::numeric_limits<double>::max(); }
1357  static inline double max() { return std::numeric_limits<double>::max(); }
1358  static inline double defaultStep() { return 1; }
1359  static inline unsigned short defaultPrecision() { return 100; }
1360 };
1361 
1362 #ifdef HAVE_TEUCHOS_LONG_DOUBLE
1363 template<>
1364 class EnhancedNumberTraits<long double>{
1365 public:
1366  static inline long double min() { return -std::numeric_limits<long double>::max(); }
1367  static inline long double max() { return std::numeric_limits<long double>::max(); }
1368  static inline long double defaultStep() { return 1; }
1369  static inline unsigned short defaultPrecision() { return 100; }
1370 };
1371 #endif
1372 
1373 template<>
1375 public:
1376  static inline float min() { return -std::numeric_limits<float>::max(); }
1377  static inline float max() { return std::numeric_limits<float>::max(); }
1378  static inline float defaultStep() { return 1; }
1379  static inline unsigned short defaultPrecision() { return 100; }
1380 };
1381 
1387 template <class T>
1389 
1390 public:
1391 
1394 
1410  T min,
1411  T max,
1413  unsigned short precision=EnhancedNumberTraits<T>::defaultPrecision()):
1415  minVal(min), maxVal(max), step_(step), precision_(precision),
1416  containsMin(true), containsMax(true){}
1417 
1423  minVal(EnhancedNumberTraits<T>::min()),
1424  maxVal(EnhancedNumberTraits<T>::max()),
1425  step_(EnhancedNumberTraits<T>::defaultStep()),
1426  precision_(EnhancedNumberTraits<T>::defaultPrecision()),
1427  containsMin(false),
1428  containsMax(false){}
1429 
1431 
1433 
1434 
1439  void setMin(T min){
1440  minVal = min;
1441  containsMin = true;
1442  }
1443 
1448  void setMax(T max){
1449  maxVal = max;
1450  containsMax = true;
1451  }
1452 
1457  void setStep(T step){
1458  step_ = step;
1459  }
1460 
1465  void setPrecision(unsigned short precision){
1466  precision_ = precision;
1467  }
1468 
1470 
1473 
1478  T getMin() const{
1479  return minVal;
1480  }
1481 
1486  T getMax() const{
1487  return maxVal;
1488  }
1489 
1494  T getStep() const{
1495  return step_;
1496  }
1497 
1502  unsigned short getPrecision() const{
1503  return precision_;
1504  }
1505 
1507 
1509 
1510 
1515  bool hasMin() const{
1516  return containsMin;
1517  }
1518 
1523  bool hasMax() const{
1524  return containsMax;
1525  }
1526 
1528 
1531 
1534  return null;
1535  }
1536 
1538  void validate(ParameterEntry const &entry, std::string const &paramName,
1539  std::string const &sublistName) const;
1540 
1542  void validateAndModify( std::string const& paramName,
1543  std::string const& sublistName, ParameterEntry * entry) const;
1544 
1547  const bool activeQuery) const;
1548 
1550  const std::string getXMLTypeName() const{
1551  return "EnhancedNumberValidator(" + TypeNameTraits<T>::name()+ ")";
1552  }
1553 
1555  void printDoc(std::string const &docString, std::ostream &out) const{
1556  StrUtils::printLines(out,"# ",docString);
1557  out << "#\tValidator Used: " << std::endl;
1558  out << "#\t\tNumber Validator" << std::endl;
1559  out << "#\t\tType: " << Teuchos::TypeNameTraits<T>::name() <<
1560  std::endl;
1561  out << "#\t\tMin (inclusive): " << minVal << std::endl;
1562  out << "#\t\tMax (inclusive): " << maxVal << std::endl;
1563  }
1564 
1566 
1567 private:
1570 
1571  // note this was discussed in issue #612
1572  // currently we are keeping a string validator with EnhancedNumberValidator
1573  // an alternative is to make a combined class for AnyNumberParameterEntryValidator
1574  // and EnhancedNumberValidator
1575  bool useIntConversions() const;
1576 
1578 
1581 
1585 
1589 
1593 
1597  unsigned short precision_;
1598 
1602 
1606 
1608 
1609 };
1610 
1611 template<class T>
1613  std::string const& paramName,
1614  std::string const& sublistName,
1615  ParameterEntry * entry
1616  ) const
1617 {
1618  TEUCHOS_TEST_FOR_EXCEPT(0==entry);
1619 
1620  any anyValue = entry->getAny(true);
1621  // preferred type is not string
1622  if( anyValue.type() == typeid(std::string) ) {
1623  anyValue = getNumberFromString(*entry,false);
1624  entry->setValue(
1625  any_cast<T>(anyValue),
1626  false // isDefault
1627  );
1628  }
1629  else {
1630  // default behavior
1632  paramName, sublistName, entry);
1633  }
1634 }
1635 
1636 template<class T>
1638 {
1639  // this will need some rethinking and exists only for supporting
1640  // conversion of strings to the templated type T
1641  // but we may want to unify this into the base class anyways
1642  // and share string conversion concepts with other parameters
1643  // like AnyNumberParameterEntryValidator
1644  if(typeid(T) == typeid(char)) return true;
1645  if(typeid(T) == typeid(unsigned char)) return true;
1646  if(typeid(T) == typeid(int)) return true;
1647  if(typeid(T) == typeid(unsigned int)) return true;
1648  if(typeid(T) == typeid(short)) return true;
1649  if(typeid(T) == typeid(unsigned short)) return true;
1650  if(typeid(T) == typeid(long)) return true;
1651  if(typeid(T) == typeid(unsigned long)) return true;
1652  if(typeid(T) == typeid(long long)) return true;
1653  if(typeid(T) == typeid(unsigned long long)) return true;
1654 
1655  // default to double stod to older atof conversion
1656  // depending on HAVE_TEUCHOSCORE_CXX11
1657  // those conversions would probably handle all above discrete types anyways
1658  return false;
1659 }
1660 
1661 template<class T>
1663  const ParameterEntry &entry, const bool activeQuery
1664  ) const
1665 {
1666  // perhaps we want to just eliminate the int checks
1667  // and always use double conversion which I think would work
1668  // well for all types - but this will give us a behavior which mirrors
1669  // AnyNumberParameterEntryValidator more closely
1670  const any &anyValue = entry.getAny(activeQuery);
1671  if(useIntConversions()) {
1672  return any((T)convertStringToInt(any_cast<std::string>(anyValue)));
1673  }
1674  else { // if not discrete, read as a double and cast to our type T
1675  return any((T)convertStringToDouble(any_cast<std::string>(anyValue)));
1676  }
1677 }
1678 
1679 template<class T>
1680 void EnhancedNumberValidator<T>::validate(ParameterEntry const &entry, std::string const &paramName,
1681  std::string const &sublistName) const
1682 {
1683  any anyValue = entry.getAny(true);
1684 
1685  // This was new code added to allow EnhancedNumberValidator to accept a string
1686  // This was added for consistency with AnyNumberParameterEntryValidator
1687  // and the new BoolParameterEntryValidator which all take string
1688  // We may wish to change this to be optional like AnyNumberParameterEntryValidator
1689  if( anyValue.type() == typeid(std::string) ) {
1690  // try to upgrade from a string to a number
1691  anyValue = getNumberFromString(entry, false);
1692  }
1693 
1694  const std::string &entryName = entry.getAny(false).typeName();
1695  TEUCHOS_TEST_FOR_EXCEPTION(anyValue.type() != typeid(T),
1697  "The \"" << paramName << "\"" <<
1698  " parameter in the \"" << sublistName <<
1699  "\" sublist is has an error." << std::endl << std::endl <<
1700  "Error: The value that you entered was the wrong type." << std::endl <<
1701  "Parameter: " << paramName << std::endl <<
1702  "Type specified: " << entryName << std::endl <<
1703  "Type accepted: " << Teuchos::TypeNameTraits<T>::name() << std::endl);
1704 
1705  bool isValueInRange;
1706  any_cast<T>(anyValue) >= minVal && any_cast<T>(anyValue) <= maxVal
1707  ? isValueInRange = true : isValueInRange=false;
1708  TEUCHOS_TEST_FOR_EXCEPTION(!(isValueInRange),
1710  "The \"" << paramName << "\"" <<
1711  " parameter in the \"" << sublistName <<
1712  "\" sublist is has an error." << std::endl << std::endl <<
1713  "Error: The value that was entered doesn't fall with in " <<
1714  "the range set by the validator" << std::endl <<
1715  "Parameter: " << paramName << std::endl <<
1716  "Min: " << minVal << std::endl <<
1717  "Max: " << maxVal << std::endl <<
1718  "Value entered: " <<
1719  (any_cast<T>(anyValue)) << std::endl << std::endl);
1720 }
1721 
1727 template<class T>
1729 
1730 public:
1731 
1734 
1739 
1741 };
1742 
1743 template<class T>
1746 {
1747  return rcp(new EnhancedNumberValidator<T>);
1748 }
1749 
1759 
1760 public:
1761 
1764 
1767  static bool mustAlreadyExistDefault() { return false; }
1768 
1770 
1773 
1779  FileNameValidator(bool mustAlreadyExist = mustAlreadyExistDefault());
1780 
1782 
1784 
1785 
1791  bool fileMustExist() const;
1792 
1794 
1795 
1806  bool fileEmptyNameOK() const;
1807 
1809 
1811 
1812 
1821  bool setFileMustExist(bool shouldFileExist);
1822 
1831  bool setFileEmptyNameOK(bool isEmptyNameOK);
1832 
1834 
1837 
1839  ValidStringsList validStringValues() const;
1840 
1842  void validate(
1843  ParameterEntry const &entry,
1844  std::string const &paramName,
1845  std::string const &sublistName) const;
1846 
1848  const std::string getXMLTypeName() const;
1849 
1851  void printDoc(std::string const &docString, std::ostream &out) const;
1852 
1854 
1855 private:
1856 
1859 
1865 
1867 
1868 };
1869 
1875 template<>
1877 
1878 public:
1879 
1882 
1886  static RCP<FileNameValidator> getDummyObject();
1887 
1889 
1890 };
1891 
1900 
1901 public:
1902 
1905 
1908  StringValidator();
1909 
1914  StringValidator(const Teuchos::Array<std::string> &validStrings);
1915 
1917 
1919 
1920 
1928  ValidStringsList setValidStrings(
1929  const Teuchos::Array<std::string> &validStrings);
1930 
1932 
1935 
1937  ValidStringsList validStringValues() const;
1938 
1940  void validate(ParameterEntry const &entry, std::string const &paramName,
1941  std::string const &sublistName) const;
1942 
1944  const std::string getXMLTypeName() const;
1945 
1947  void printDoc(std::string const &docString, std::ostream &out) const;
1948 
1950 
1951 private:
1952 
1955 
1958  ValidStringsList validStrings_;
1959 
1961 
1962 };
1963 
1969 template<>
1971 
1972 public:
1973 
1976 
1980  static RCP<StringValidator> getDummyObject();
1981 
1983 
1984 };
1985 
1986 
1990 template<class ValidatorType, class EntryType>
1992 
1993 public:
1994 
1997 
2006  prototypeValidator_(prototypeValidator){}
2007 
2009 
2012 
2015  return prototypeValidator_;
2016  }
2017 
2019 
2022 
2025  return prototypeValidator_->validStringValues();
2026  }
2027 
2029 
2030 private:
2031 
2034 
2038 
2041 
2043 
2044 };
2045 
2058 template<class ValidatorType, class EntryType>
2059 class TwoDArrayValidator : public AbstractArrayValidator<ValidatorType, EntryType>{
2060 public:
2063 
2071  AbstractArrayValidator<ValidatorType, EntryType>(prototypeValidator){}
2072 
2074 
2077 
2079  virtual void validate(ParameterEntry const &entry, std::string const &paramName,
2080  std::string const &sublistName) const;
2081 
2083  const std::string getXMLTypeName() const{
2084  return "TwoDArrayValidator(" +
2085  this->getPrototype()->getXMLTypeName() + ", " +
2087  }
2088 
2090  virtual void printDoc(std::string const &docString, std::ostream &out) const
2091  {
2092  StrUtils::printLines(out,"# ",docString);
2093  std::string toPrint;
2094  toPrint += "TwoDArrayValidator:\n";
2095  toPrint += "Prototype Validator:\n";
2096  this->getPrototype()->printDoc(toPrint, out);
2097  }
2098 
2100 
2101 };
2102 
2103 template<class ValidatorType, class EntryType>
2104 void TwoDArrayValidator<ValidatorType, EntryType>::validate(ParameterEntry const &entry, std::string const &paramName,
2105  std::string const &sublistName) const
2106 {
2107  any anyValue = entry.getAny(true);
2108  const std::string &entryName = entry.getAny(false).typeName();
2111  "The \"" << paramName << "\"" <<
2112  " parameter in the \"" << sublistName <<
2113  "\" sublist is has an error." << std::endl << std::endl <<
2114  "Error: The value you entered was the wrong type." << std::endl <<
2115  "Parameter: " << paramName << std::endl <<
2116  "Type specified: " << entryName << std::endl <<
2117  "Type accepted: " << TypeNameTraits<TwoDArray<EntryType> >::name() <<
2118  std::endl << std::endl);
2119 
2120  TwoDArray<EntryType> extracted =
2121  getValue<Teuchos::TwoDArray<EntryType> >(entry);
2122  RCP<const ParameterEntryValidator> prototype = this->getPrototype();
2123  for(int i = 0; i<extracted.getNumRows(); ++i){
2124  for(int j = 0; j<extracted.getNumCols(); ++j){
2125  ParameterEntry dummyParameter;
2126  dummyParameter.setValue(extracted(i,j));
2127  try{
2128  prototype->validate(
2129  dummyParameter, paramName, sublistName);
2130  }
2132  std::stringstream oss;
2133  oss << "TwoDArray Validator Exception:" << std::endl <<
2134  "Bad Index: (" << i << "," << j << ")" << std::endl << e.what();
2135  throw Exceptions::InvalidParameterValue(oss.str());
2136  }
2137  }
2138  }
2139 }
2140 
2141 
2147 template<class ValidatorType, class EntryType>
2148 class DummyObjectGetter<TwoDArrayValidator<ValidatorType, EntryType> >{
2149 
2150 public:
2151 
2154 
2159 
2161 
2162 };
2163 
2164 template<class ValidatorType, class EntryType>
2167 {
2170 }
2171 
2176  public TwoDArrayValidator<StringValidator, std::string>{
2177 
2178 public:
2179 
2182 
2185  TwoDArrayValidator<StringValidator, std::string>(prototypeValidator){}
2186 
2188 
2189 };
2190 
2191 
2197  public TwoDArrayValidator<FileNameValidator, std::string>{
2198 
2199 public:
2200 
2203 
2206  TwoDArrayValidator<FileNameValidator, std::string>(prototypeValidator){}
2207 
2209 
2210 };
2211 
2212 
2216 template<class T>
2217 class TwoDArrayNumberValidator : public TwoDArrayValidator<EnhancedNumberValidator<T>, T>{
2218 public:
2221 
2224  RCP<const EnhancedNumberValidator<T> > prototypeValidator):
2225  TwoDArrayValidator<EnhancedNumberValidator<T>, T>(prototypeValidator){}
2226 
2228 
2229 };
2230 
2231 
2243 template<class ValidatorType, class EntryType>
2244 class ArrayValidator : public AbstractArrayValidator<ValidatorType, EntryType>{
2245 
2246 public:
2247 
2250 
2257  AbstractArrayValidator<ValidatorType, EntryType>(prototypeValidator){}
2258 
2260 
2263 
2265  virtual void validate(ParameterEntry const &entry, std::string const &paramName,
2266  std::string const &sublistName) const;
2267 
2269  const std::string getXMLTypeName() const{
2270  return "ArrayValidator(" +
2271  this->getPrototype()->getXMLTypeName() + ", " +
2273  }
2274 
2276  virtual void printDoc(std::string const &docString, std::ostream &out) const
2277  {
2278  StrUtils::printLines(out,"# ",docString);
2279  std::string toPrint;
2280  toPrint += "ArrayValidator:\n";
2281  toPrint += "Prototype Validator:\n";
2282  this->getPrototype()->printDoc(toPrint, out);
2283  }
2284 
2286 
2287 };
2288 
2289 template<class ValidatorType, class EntryType>
2290 void ArrayValidator<ValidatorType, EntryType>::validate(ParameterEntry const &entry, std::string const &paramName,
2291  std::string const &sublistName) const
2292 {
2293  any anyValue = entry.getAny(true);
2294  const std::string &entryName = entry.getAny(false).typeName();
2295  TEUCHOS_TEST_FOR_EXCEPTION(anyValue.type() != typeid(Array<EntryType>),
2297  "The \"" << paramName << "\"" <<
2298  " parameter in the \"" << sublistName <<
2299  "\" sublist is has an error." << std::endl << std::endl <<
2300  "Error: The value you entered was the wrong type." << std::endl <<
2301  "Parameter: " << paramName << std::endl <<
2302  "Type specified: " << entryName << std::endl <<
2303  "Type accepted: " << TypeNameTraits<Array<EntryType> >::name() <<
2304  std::endl << std::endl);
2305 
2306  Array<EntryType> extracted =
2307  getValue<Teuchos::Array<EntryType> >(entry);
2308  RCP<const ParameterEntryValidator> prototype = this->getPrototype();
2309  for(int i = 0; i<extracted.size(); ++i){
2310  ParameterEntry dummyParameter;
2311  dummyParameter.setValue(extracted[i]);
2312  try{
2313  prototype->validate(
2314  dummyParameter, paramName, sublistName);
2315  }
2317  std::stringstream oss;
2318  oss << "Array Validator Exception:" << std::endl <<
2319  "Bad Index: " << i << std::endl << e.what();
2320  throw Exceptions::InvalidParameterValue(oss.str());
2321  }
2322  }
2323 }
2324 
2330 template<class ValidatorType, class EntryType>
2331 class DummyObjectGetter<ArrayValidator<ValidatorType, EntryType> >{
2332 
2333 public:
2334 
2337 
2342 
2344 
2345 };
2346 
2347 template<class ValidatorType, class EntryType>
2350 {
2353 }
2354 
2355 
2365  public ArrayValidator<StringValidator, std::string>{
2366 
2367 public:
2368 
2371 
2374  ArrayValidator<StringValidator, std::string>(prototypeValidator){}
2375 
2377 
2378 };
2379 
2380 
2389 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT ArrayFileNameValidator : public ArrayValidator<FileNameValidator, std::string>{
2390 
2391 public:
2392 
2395 
2398  ArrayValidator<FileNameValidator, std::string>(prototypeValidator){}
2399 
2401 
2402 };
2403 
2404 
2412 template<class T>
2413 class ArrayNumberValidator : public ArrayValidator<EnhancedNumberValidator<T>, T>{
2414 public:
2417 
2420  RCP<const EnhancedNumberValidator<T> > prototypeValidator):
2421  ArrayValidator<EnhancedNumberValidator<T>, T>(prototypeValidator){}
2422 
2424 
2425 };
2426 
2427 
2428 
2429 // ///////////////////////////
2430 // Implementations
2431 
2432 
2433 //
2434 // StringToIntegralParameterEntryValidator
2435 //
2436 
2437 
2438 // Constructors
2439 
2440 
2441 template<class IntegralType>
2444  std::string const& defaultParameterName,
2445  const bool caseSensitive) :
2447  defaultParameterName_ (defaultParameterName),
2448  caseSensitive_ (caseSensitive)
2449 {
2450  const int length = static_cast<int>(strings.size());
2451  Array<IntegralType> integralValues(length);
2452  for (int i = 0; i < length; ++i) integralValues[i] = static_cast<IntegralType>(i);
2453  init(strings, integralValues);
2454  setValidValues (strings);
2455 }
2456 
2457 
2458 template<class IntegralType>
2461  ArrayView<const IntegralType> const& integralValues,
2462  std::string const& defaultParameterName,
2463  const bool caseSensitive) :
2465  defaultParameterName_ (defaultParameterName),
2466  caseSensitive_ (caseSensitive)
2467 {
2468 #ifdef TEUCHOS_DEBUG
2469  TEUCHOS_ASSERT_EQUALITY( strings.size(), integralValues.size() );
2470 #endif
2471  init(strings, integralValues);
2472  setValidValues (strings);
2473 }
2474 
2475 template<class IntegralType>
2478  ArrayView<const std::string> const& stringsDocs,
2479  ArrayView<const IntegralType> const& integralValues,
2480  std::string const& defaultParameterName,
2481  const bool caseSensitive) :
2483  defaultParameterName_ (defaultParameterName),
2484  caseSensitive_ (caseSensitive)
2485 {
2486 #ifdef TEUCHOS_DEBUG
2487  TEUCHOS_ASSERT_EQUALITY( strings.size(), stringsDocs.size() );
2488 #endif
2489 
2491  strings.size() != stringsDocs.size(),
2492  std::logic_error,
2493  "The input arrays strings and stringsDocs must have the same length.");
2494 
2495  init(strings, integralValues);
2496  setValidValues(strings,&stringsDocs);
2497 }
2498 
2499 template <class IntegralType>
2501  ArrayView<const std::string> const &strings,
2502  ArrayView<const IntegralType> const &integralValues) {
2503 
2504 #ifdef TEUCHOS_DEBUG
2505  TEUCHOS_ASSERT_EQUALITY(strings.size(), integralValues.size());
2506 #endif
2507 
2509  strings.size() != integralValues.size(), std::logic_error,
2510  "The input arrays strings and integralValues must have the same length.");
2511 
2512  typedef typename map_t::value_type val_t;
2513  typedef typename inv_map_t::value_type inv_val_t;
2514  for (int i = 0; i < static_cast<int>(strings.size()); ++i) {
2515  const std::string name =
2516  caseSensitive_ ? strings[i] : upperCase(strings[i]);
2517  const bool unique = map_.insert(val_t(name, integralValues[i])).second;
2518  TEUCHOS_TEST_FOR_EXCEPTION(!unique, std::logic_error,
2519  "For parameter \"" << defaultParameterName_
2520  << "\": "
2521  "strings["
2522  << i << "] = \"" << strings[i]
2523  << "\" is a duplicate.");
2524  inv_map_.insert(inv_val_t(integralValues[i], name));
2525  }
2526 }
2527 
2528 // Lookup functions
2529 
2530 
2531 template<class IntegralType>
2532 IntegralType
2534  const std::string &str, const std::string &paramName
2535  ,const std::string &sublistName
2536  ) const
2537 {
2538  typename map_t::const_iterator itr = map_.find (caseSensitive_ ? str : upperCase (str));
2540  itr == map_.end(), Exceptions::InvalidParameterValue
2541  ,"Error, the value \"" << str << "\" is not recognized for the parameter \""
2542  << ( paramName.length() ? paramName : defaultParameterName_ ) << "\""
2543  << "\nin the sublist \"" << sublistName << "\"."
2544  << "\n\nValid values include:"
2545  << "\n {\n"
2546  << validValues_
2547  << " }"
2548  );
2549  return (*itr).second;
2550 }
2551 
2552 
2553 template<class IntegralType>
2554 IntegralType
2556  const ParameterEntry &entry, const std::string &paramName
2557  ,const std::string &sublistName, const bool activeQuery
2558  ) const
2559 {
2560  if (entry.isType<IntegralType>()){
2561  return any_cast<IntegralType>(entry.getAny(activeQuery));
2562  } else{
2563  const bool validType = ( entry.getAny(activeQuery).type() == typeid(std::string) );
2566  ,"Error, the parameter {paramName=\""<<(paramName.length()?paramName:defaultParameterName_)
2567  << "\",type=\""<<entry.getAny(activeQuery).typeName()<<"\"}"
2568  << "\nin the sublist \"" << sublistName << "\""
2569  << "\nhas the wrong type."
2570  << "\n\nThe correct type is \"string\"!"
2571  );
2572  const std::string
2573  &strValue = any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail!
2574  return getIntegralValue(strValue,paramName,sublistName); // This will validate the value and throw!
2575  }
2576 }
2577 
2578 
2579 template<class IntegralType>
2580 std::string
2582  const ParameterEntry &entry, const std::string &paramName
2583  ,const std::string &sublistName, const bool activeQuery
2584  ) const
2585 {
2586  if (entry.isType<IntegralType>()){
2587  const IntegralType intVal = any_cast<IntegralType>(entry.getAny(activeQuery));
2588  typename inv_map_t::const_iterator itr = inv_map_.find(intVal);
2589  // typename inv_map_t::const_iterator itr = inv_map_.find(intVal);
2590  // TODO: Maybe do a test on intVal but it should be valid by construction
2591  return (*itr).second;
2592  } else{
2593  // Validate the parameter's type and value
2594  this->getIntegralValue(entry,paramName,sublistName,activeQuery);
2595  // Return the std::string value which is now validated!
2596  return any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail!
2597  }
2598 }
2599 
2600 
2601 template<class IntegralType>
2602 IntegralType
2604  ParameterList &paramList, const std::string &paramName
2605  ,const std::string &defaultValue
2606  ) const
2607 {
2608  const std::string& strValue =
2609  paramList.get (paramName,
2610  caseSensitive_ ? defaultValue : upperCase (defaultValue));
2611  return getIntegralValue (strValue, paramName, paramList.name ());
2612 }
2613 
2614 
2615 template<class IntegralType>
2616 std::string
2618  ParameterList &paramList, const std::string &paramName
2619  ,const std::string &defaultValue
2620  ) const
2621 {
2622  const std::string& strValue =
2623  paramList.get (paramName,
2624  caseSensitive_ ? defaultValue : upperCase (defaultValue));
2625  getIntegralValue(strValue,paramName,paramList.name()); // Validate!
2626  return strValue;
2627 }
2628 
2629 template<class IntegralType>
2632 {
2633  return validStringValuesDocs_;
2634 }
2635 
2636 template<class IntegralType>
2637 const std::string&
2639 {
2640  return defaultParameterName_;
2641 }
2642 
2643 template<class IntegralType>
2644 std::string
2646  const std::string &str, const std::string &paramName
2647  ,const std::string &sublistName
2648  ) const
2649 {
2650  getIntegralValue (caseSensitive_ ? str : upperCase (str),
2651  paramName,
2652  sublistName); // Validate!
2653  return str;
2654 }
2655 
2656 
2657 // Overridden from ParameterEntryValidator
2658 
2659 template<class IntegralType>
2660 const std::string
2662  return "StringIntegralValidator(" + TypeNameTraits<IntegralType>::name () + ")";
2663 }
2664 
2665 template<class IntegralType>
2667  std::string const& docString
2668  ,std::ostream & out
2669  ) const
2670 {
2671  StrUtils::printLines(out,"# ",docString);
2672  out << "# Valid std::string values:\n";
2673  out << "# {\n";
2674  if(validStringValuesDocs_.get()) {
2675  for( int i = 0; i < static_cast<int>(validStringValues_->size()); ++i ) {
2676  out << "# \"" << (*validStringValues_)[i] << "\"\n";
2677  StrUtils::printLines(out,"# ",(*validStringValuesDocs_)[i] );
2678  }
2679  }
2680  else {
2681  StrUtils::printLines(out,"# ",validValues_);
2682  // Note: Above validValues_ has for initial spaces already so indent should
2683  // be correct!
2684  }
2685  out << "# }\n";
2686 }
2687 
2688 
2689 template<class IntegralType>
2692 {
2693  return validStringValues_;
2694 }
2695 
2696 
2697 template<class IntegralType>
2699  ParameterEntry const& entry
2700  ,std::string const& paramName
2701  ,std::string const& sublistName
2702  ) const
2703 {
2704  this->getIntegralValue (entry, paramName, sublistName, false);
2705 }
2706 
2707 
2708 #if defined(HAVE_TEUCHOS_MODIFY_DEFAULTS_DURING_VALIDATION)
2709 template<class IntegralType>
2711  std::string const& paramName,
2712  std::string const& sublistName,
2713  ParameterEntry * entry
2714  ) const
2715 {
2716  entry->setValue(this->getIntegralValue(*entry, paramName, sublistName, false));
2717 }
2718 #endif
2719 
2720 
2721 // private
2722 
2723 template<class IntegralType>
2725  ArrayView<const std::string> const& strings
2726  ,ArrayView<const std::string> const* stringsDocs
2727  )
2728 {
2729  if (caseSensitive_) {
2730  validStringValues_ = rcp (new Array<std::string> (strings));
2731  }
2732  else {
2733  RCP<Array<std::string> > vals (new Array<std::string> (strings.size ()));
2734  for (Array<std::string>::size_type i = 0; i < strings.size (); ++i) {
2735  (*vals)[i] = upperCase (strings[i]);
2736  }
2737  validStringValues_ = rcp_const_cast<const Array<std::string> > (vals);
2738  }
2739 
2740  if (stringsDocs) {
2741  validStringValuesDocs_ = rcp (new Array<std::string> (*stringsDocs));
2742  }
2743  // Build the list of valid values in the same order as passed in by the client.
2744  std::ostringstream oss;
2745  for (int i = 0; i < static_cast<int> (strings.size()); ++i) {
2746  oss << " \"" << strings[i] << "\"\n";
2747  }
2748  // Note: Above four spaces is designed for the error output above.
2749  validValues_ = oss.str();
2750 }
2751 
2752 
2753 } // namespace Teuchos
2754 
2755 
2756 //
2757 // Nonmember function implementations for StringToIntegralParameterEntryValidator
2758 //
2759 
2760 
2761 template<class IntegralType>
2762 inline
2764 Teuchos::stringToIntegralParameterEntryValidator(
2765  ArrayView<const std::string> const& strings,
2766  std::string const& defaultParameterName
2767  )
2768 {
2769  return rcp(
2770  new StringToIntegralParameterEntryValidator<IntegralType>(
2771  strings, defaultParameterName
2772  )
2773  );
2774 }
2775 
2776 
2777 template<class IntegralType>
2778 inline
2780 Teuchos::stringToIntegralParameterEntryValidator(
2781  ArrayView<const std::string> const& strings,
2782  std::string const& defaultParameterName,
2783  const bool caseSensitive
2784  )
2785 {
2786  typedef StringToIntegralParameterEntryValidator<IntegralType> ret_type;
2787  return rcp (new ret_type (strings, defaultParameterName, caseSensitive));
2788 }
2789 
2790 
2791 
2792 template<class IntegralType>
2793 inline
2795 Teuchos::stringToIntegralParameterEntryValidator(
2796  ArrayView<const std::string> const& strings,
2797  ArrayView<const IntegralType> const& integralValues,
2798  std::string const& defaultParameterName
2799  )
2800 {
2801  return rcp(
2802  new StringToIntegralParameterEntryValidator<IntegralType>(
2803  strings, integralValues, defaultParameterName
2804  )
2805  );
2806 }
2807 
2808 
2809 template<class IntegralType>
2810 inline
2812 Teuchos::stringToIntegralParameterEntryValidator(
2813  ArrayView<const std::string> const& strings,
2814  ArrayView<const IntegralType> const& integralValues,
2815  std::string const& defaultParameterName,
2816  const bool caseSensitive)
2817 {
2818  typedef StringToIntegralParameterEntryValidator<IntegralType> ret_type;
2819  return rcp (new ret_type (strings, integralValues,
2820  defaultParameterName, caseSensitive));
2821 }
2822 
2823 
2824 template<class IntegralType>
2825 inline
2827 Teuchos::stringToIntegralParameterEntryValidator(
2828  ArrayView<const std::string> const& strings,
2829  ArrayView<const std::string> const& stringsDocs,
2830  ArrayView<const IntegralType> const& integralValues,
2831  std::string const& defaultParameterName
2832  )
2833 {
2834  return rcp(
2835  new StringToIntegralParameterEntryValidator<IntegralType>(
2836  strings, stringsDocs, integralValues, defaultParameterName
2837  )
2838  );
2839 }
2840 
2841 
2842 template<class IntegralType>
2843 inline
2845 Teuchos::stringToIntegralParameterEntryValidator(
2846  ArrayView<const std::string> const& strings,
2847  ArrayView<const std::string> const& stringsDocs,
2848  ArrayView<const IntegralType> const& integralValues,
2849  std::string const& defaultParameterName,
2850  const bool caseSensitive)
2851 {
2852  typedef StringToIntegralParameterEntryValidator<IntegralType> ret_type;
2853  return rcp (new ret_type (strings, stringsDocs, integralValues,
2854  defaultParameterName, caseSensitive));
2855 }
2856 
2857 
2858 template<class IntegralType>
2859 void Teuchos::setStringToIntegralParameter(
2860  std::string const& paramName,
2861  std::string const& defaultValue,
2862  std::string const& docString,
2863  ArrayView<const std::string> const& strings,
2864  ParameterList * paramList
2865  )
2866 {
2867  typedef ParameterEntryValidator PEV;
2868  TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
2869  paramList->set(
2870  paramName, defaultValue, docString,
2871  rcp_implicit_cast<const PEV>(
2872  stringToIntegralParameterEntryValidator<IntegralType>(
2873  strings, paramName
2874  )
2875  )
2876  );
2877 }
2878 
2879 
2880 template<class IntegralType>
2881 void Teuchos::setStringToIntegralParameter(
2882  std::string const& paramName,
2883  std::string const& defaultValue,
2884  std::string const& docString,
2885  ArrayView<const std::string> const& strings,
2886  ArrayView<const IntegralType> const& integralValues,
2887  ParameterList * paramList
2888  )
2889 {
2890  typedef ParameterEntryValidator PEV;
2891  TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
2892  paramList->set(
2893  paramName, defaultValue, docString,
2894  rcp_implicit_cast<const PEV>(
2895  stringToIntegralParameterEntryValidator<IntegralType>(
2896  strings, integralValues, paramName
2897  )
2898  )
2899  );
2900 }
2901 
2902 
2903 template<class IntegralType>
2904 void Teuchos::setStringToIntegralParameter(
2905  std::string const& paramName,
2906  std::string const& defaultValue,
2907  std::string const& docString,
2908  ArrayView<const std::string> const& strings,
2909  ArrayView<const std::string> const& stringsDocs,
2910  ArrayView<const IntegralType> const& integralValues,
2911  ParameterList * paramList
2912  )
2913 
2914 {
2915  typedef ParameterEntryValidator PEV;
2916  TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
2917  paramList->set(
2918  paramName, defaultValue, docString,
2919  rcp_implicit_cast<const PEV>(
2920  stringToIntegralParameterEntryValidator<IntegralType>(
2921  strings, stringsDocs, integralValues, paramName
2922  )
2923  )
2924  );
2925 }
2926 
2927 
2928 template<class IntegralType>
2929 IntegralType Teuchos::getIntegralValue(
2930  ParameterList const& paramList, std::string const& paramName
2931  )
2932 {
2933  const ParameterEntry &entry = paramList.getEntry(paramName);
2934  if (entry.isType<IntegralType>()){
2935  return getValue<IntegralType>(entry);
2936  } else{
2937  RCP<const StringToIntegralParameterEntryValidator<IntegralType> >
2938  integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>(
2939  entry, paramList, paramName
2940  );
2941  return integralValidator->getIntegralValue(
2942  entry, paramName, paramList.name(), true );
2943  }
2944 }
2945 
2946 
2947 template<class IntegralType>
2948 std::string Teuchos::getStringValue(
2949  ParameterList const& paramList, std::string const& paramName
2950  )
2951 {
2952  const ParameterEntry &entry = paramList.getEntry(paramName);
2953  RCP<const StringToIntegralParameterEntryValidator<IntegralType> >
2954  integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>(
2955  entry, paramList, paramName
2956  );
2957  return integralValidator->getStringValue(
2958  entry, paramName, paramList.name(), true
2959  );
2960 }
2961 
2962 
2963 template<class IntegralType>
2966  ParameterEntry const& entry, ParameterList const& paramList,
2967  std::string const& paramName
2968  )
2969 {
2970  const RCP<const ParameterEntryValidator> validator = entry.validator();
2972  is_null(validator), Exceptions::InvalidParameterType,
2973  "Error! The parameter \""<<paramName<<"\" exists\n"
2974  "in the parameter (sub)list \""<<paramList.name()<<"\"\n"
2975  "but it does not contain any validator needed to extract\n"
2976  "an integral value of type \""<<TypeNameTraits<IntegralType>::name()<<"\"!"
2977  );
2978  const RCP<const StringToIntegralParameterEntryValidator<IntegralType> > integralValidator =
2979  rcp_dynamic_cast<const StringToIntegralParameterEntryValidator<IntegralType> >(
2980  validator
2981  );
2983  is_null(integralValidator), Exceptions::InvalidParameterType,
2984  "Error! The parameter \""<<paramName<<"\" exists\n"
2985  "in the parameter (sub)list \""<<paramList.name()<<"\"\n"
2986  "but it contains the wrong type of validator. The expected validator type\n"
2987  "is \""<<TypeNameTraits<StringToIntegralParameterEntryValidator<IntegralType> >::name()<<"\"\n"
2988  "but the contained validator type is \""<<typeName(*validator)<<"\"!"
2989  );
2990  return integralValidator;
2991 }
2992 
2993 
2994 #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.
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)
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)