Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_StandardParameterEntryValidators.hpp
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 
45 #include "Teuchos_ParameterEntryValidator.hpp"
47 #include "Teuchos_ParameterListExceptions.hpp"
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 
322 
323 private:
324  std::string defaultParameterName_;
325  std::string validValues_;
326  ValidStringsList validStringValues_;
327  ValidStringsList validStringValuesDocs_;
328 
329  typedef std::map<std::string,IntegralType> map_t;
330  map_t map_;
331 
332  const bool caseSensitive_;
333 
334  void setValidValues(
335  ArrayView<const std::string> const& strings,
336  ArrayView<const std::string> const* stringsDocs = NULL
337  );
338 
339  // Not defined and not to be called.
341 
343  static std::string upperCase (const std::string s) {
344  std::string s_upper = s;
345  std::transform (s_upper.begin (), s_upper.end (), s_upper.begin (), ::toupper);
346  return s_upper;
347  }
348 };
349 
350 
355 template<class IntegralType>
356 RCP<StringToIntegralParameterEntryValidator<IntegralType> >
357 stringToIntegralParameterEntryValidator(
358  ArrayView<const std::string> const& strings,
359  std::string const& defaultParameterName
360  );
361 
362 
367 template<class IntegralType>
368 RCP<StringToIntegralParameterEntryValidator<IntegralType> >
369 stringToIntegralParameterEntryValidator(
370  ArrayView<const std::string> const& strings,
371  std::string const& defaultParameterName,
372  const bool caseSensitive
373  );
374 
375 
380 template<class IntegralType>
381 RCP<StringToIntegralParameterEntryValidator<IntegralType> >
382 stringToIntegralParameterEntryValidator(
383  ArrayView<const std::string> const& strings,
384  ArrayView<const IntegralType> const& integralValues,
385  std::string const& defaultParameterName
386  );
387 
388 
393 template<class IntegralType>
394 RCP<StringToIntegralParameterEntryValidator<IntegralType> >
395 stringToIntegralParameterEntryValidator(
396  ArrayView<const std::string> const& strings,
397  ArrayView<const IntegralType> const& integralValues,
398  std::string const& defaultParameterName,
399  const bool caseSensitive
400  );
401 
402 
407 template<class IntegralType>
408 RCP<StringToIntegralParameterEntryValidator<IntegralType> >
409 stringToIntegralParameterEntryValidator(
410  ArrayView<const std::string> const& strings,
411  ArrayView<const std::string> const& stringsDocs,
412  ArrayView<const IntegralType> const& integralValues,
413  std::string const& defaultParameterName
414  );
415 
416 
421 template<class IntegralType>
422 RCP<StringToIntegralParameterEntryValidator<IntegralType> >
423 stringToIntegralParameterEntryValidator(
424  ArrayView<const std::string> const& strings,
425  ArrayView<const std::string> const& stringsDocs,
426  ArrayView<const IntegralType> const& integralValues,
427  std::string const& defaultParameterName,
428  const bool caseSensitive
429  );
430 
431 
442 template<class IntegralType>
443 void setStringToIntegralParameter(
444  std::string const& paramName,
445  std::string const& defaultValue,
446  std::string const& docString,
447  ArrayView<const std::string> const& strings,
448  ParameterList * paramList
449  );
450 
451 
463 template<class IntegralType>
464 void setStringToIntegralParameter(
465  std::string const& paramName,
466  std::string const& defaultValue,
467  std::string const& docString,
468  ArrayView<const std::string> const& strings,
469  ArrayView<const IntegralType> const& integralValues,
470  ParameterList * paramList
471  );
472 
473 
485 template<class IntegralType>
486 void setStringToIntegralParameter(
487  std::string const& paramName,
488  std::string const& defaultValue,
489  std::string const& docString,
490  ArrayView<const std::string> const& strings,
491  ArrayView<const std::string> const& stringsDocs,
492  ArrayView<const IntegralType> const& integralValues,
493  ParameterList * paramList
494  );
495 
496 
507 template<class IntegralType>
508 IntegralType getIntegralValue(
509  ParameterList const& paramList, std::string const& paramName
510  );
511 
512 
524 template<class IntegralType>
525 std::string getStringValue(
526  ParameterList const& paramList, std::string const& paramName
527  );
528 
529 
535 template<class IntegralType>
536 RCP<const StringToIntegralParameterEntryValidator<IntegralType> >
538  ParameterEntry const& entry, ParameterList const& paramList,
539  std::string const& paramName
540  );
541 
542 
548 std::string getVerbosityLevelParameterValueName(
549  const EVerbosityLevel verbLevel
550  );
551 
552 
557 RCP<StringToIntegralParameterEntryValidator<EVerbosityLevel> >
558 verbosityLevelParameterEntryValidator(std::string const& defaultParameterName);
559 
565 template<class IntegralType>
567 
568 public:
569 
572 
577  getDummyObject();
578 
580 };
581 
582 template<class IntegralType>
585 {
586  return stringToIntegralParameterEntryValidator<IntegralType>(
587  tuple<std::string>(""), tuple<std::string>(""),
588  tuple<IntegralType>((IntegralType)1), "");
589 }
590 
603 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT BoolParameterEntryValidator
604  : public ParameterEntryValidator
605 {
606 public:
607 
610 
612 
614 
617 
619  bool getBool(
620  const ParameterEntry &entry, const std::string &paramName = "",
621  const std::string &sublistName = "", const bool activeQuery = true
622  ) const;
623 
627  bool getBool(
628  ParameterList &paramList, const std::string &paramName,
629  const int defaultValue
630  ) const;
631 
633 
636 
638  const std::string getXMLTypeName() const;
639 
641  void printDoc(
642  std::string const& docString,
643  std::ostream & out
644  ) const;
645 
648  validStringValues() const;
649 
651  void validate(
652  ParameterEntry const& entry,
653  std::string const& paramName,
654  std::string const& sublistName
655  ) const;
656 
658  void validateAndModify(
659  std::string const& paramName,
660  std::string const& sublistName,
661  ParameterEntry * entry
662  ) const;
663 
665 
666 private:
667 
668  // ////////////////////////////
669  // Private data members
670 
671  std::string acceptedTypesString_;
672 
673  // ////////////////////////////
674  // Private member functions
675 
676  void finishInitialization();
677 
678  void throwTypeError(
679  ParameterEntry const& entry,
680  std::string const& paramName,
681  std::string const& sublistName
682  ) const;
683 };
684 
685 // Nonmember helper functions
686 
687 
692 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT RCP<BoolParameterEntryValidator>
693 boolParameterEntryValidator();
694 
695 
709 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT AnyNumberParameterEntryValidator
710  : public ParameterEntryValidator
711 {
712 public:
713 
716 
718  enum EPreferredType { PREFER_INT, PREFER_DOUBLE, PREFER_STRING };
719 
720 
723  public:
725  AcceptedTypes( bool allowAllTypesByDefault = true )
726  :allowInt_(allowAllTypesByDefault),allowDouble_(allowAllTypesByDefault),
727  allowString_(allowAllTypesByDefault)
728  {}
730  AcceptedTypes& allowInt( bool _allowInt )
731  { allowInt_ = _allowInt; return *this; }
733  AcceptedTypes& allowDouble( bool _allowDouble )
734  { allowDouble_ = _allowDouble; return *this; }
736  AcceptedTypes& allowString( bool _allowString )
737  { allowString_ = _allowString; return *this; }
739  bool allowInt() const { return allowInt_; }
741  bool allowDouble() const { return allowDouble_; }
743  bool allowString() const { return allowString_; }
744  private:
745  bool allowInt_;
746  bool allowDouble_;
747  bool allowString_;
748  };
749 
751 
754 
759 
771  EPreferredType const preferredType,
772  AcceptedTypes const& acceptedTypes
773  );
774 
776 
779 
786  int getInt(
787  const ParameterEntry &entry, const std::string &paramName = "",
788  const std::string &sublistName = "", const bool activeQuery = true
789  ) const;
790 
796  double getDouble(
797  const ParameterEntry &entry, const std::string &paramName = "",
798  const std::string &sublistName = "", const bool activeQuery = true
799  ) const;
800 
802  std::string getString(
803  const ParameterEntry &entry, const std::string &paramName = "",
804  const std::string &sublistName = "", const bool activeQuery = true
805  ) const;
806 
810  int getInt(
811  ParameterList &paramList, const std::string &paramName,
812  const int defaultValue
813  ) const;
814 
818  double getDouble(
819  ParameterList &paramList, const std::string &paramName,
820  const double defaultValue
821  ) const;
822 
826  std::string getString(
827  ParameterList &paramList, const std::string &paramName,
828  const std::string &defaultValue
829  ) const;
830 
833  bool isDoubleAllowed() const;
834 
837  bool isIntAllowed() const;
838 
841  bool isStringAllowed() const;
842 
845  EPreferredType getPreferredType() const;
846 
848  static const std::string& getPrefferedTypeString (EPreferredType enumValue)
849  {
850  switch (enumValue) {
851  case PREFER_INT:
852  return getIntEnumString ();
853  case PREFER_DOUBLE:
854  return getDoubleEnumString ();
855  case PREFER_STRING:
856  return getStringEnumString ();
857  default:
858  const std::string typeString (toString (enumValue));
859  throw std::runtime_error("Cannot convert enumValue: " + typeString + " to a string");
860  }
861  }
862 
864  static EPreferredType getPrefferedTypeStringEnum (const std::string& enumString)
865  {
866  if (enumString == getIntEnumString ()) {
867  return PREFER_INT;
868  }
869  else if (enumString == getDoubleEnumString ()) {
870  return PREFER_DOUBLE;
871  }
872  else if (enumString == getStringEnumString ()) {
873  return PREFER_STRING;
874  }
875  else {
876  throw std::runtime_error ("Cannot convert enumString: " + enumString + " to an enum");
877  }
878  }
879 
881 
884 
886  const std::string getXMLTypeName() const;
887 
889  void printDoc(
890  std::string const& docString,
891  std::ostream & out
892  ) const;
893 
895  ValidStringsList
896  validStringValues() const;
897 
899  void validate(
900  ParameterEntry const& entry,
901  std::string const& paramName,
902  std::string const& sublistName
903  ) const;
904 
906  void validateAndModify(
907  std::string const& paramName,
908  std::string const& sublistName,
909  ParameterEntry * entry
910  ) const;
911 
912 
914 
915 private:
916 
917  // ////////////////////////////
918  // Private data members
919 
920  EPreferredType preferredType_;
921  std::string acceptedTypesString_;
922 
923 //use pragmas to disable some false-positive warnings for windows sharedlibs export
924 #ifdef _MSC_VER
925 #pragma warning(push)
926 #pragma warning(disable:4251)
927 #endif
928  const AcceptedTypes acceptedTypes_;
929 #ifdef _MSC_VER
930 #pragma warning(pop)
931 #endif
932 
933  // ////////////////////////////
934  // Private member functions
935 
936  /* \brief Gets the string representing the "int" preferred type enum */
937  static const std::string& getIntEnumString(){
938  static const std::string intEnumString_ = TypeNameTraits<int>::name();
939  return intEnumString_;
940  }
941 
942  /* \brief Gets the string representing the "double" preferred type enum */
943  static const std::string& getDoubleEnumString(){
944  static const std::string doubleEnumString_ = TypeNameTraits<double>::name();
945  return doubleEnumString_;
946  }
947 
948  /* \brief Gets the string representing the "string" preferred type enum */
949  static const std::string& getStringEnumString(){
950  static const std::string stringEnumString_ = TypeNameTraits<std::string>::name();
951  return stringEnumString_;
952  }
953 
954 
955  void finishInitialization();
956 
957  void throwTypeError(
958  ParameterEntry const& entry,
959  std::string const& paramName,
960  std::string const& sublistName
961  ) const;
962 
963 };
964 
965 
966 // Nonmember helper functions
967 
968 
973 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT RCP<AnyNumberParameterEntryValidator>
974 anyNumberParameterEntryValidator();
975 
976 
981 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT RCP<AnyNumberParameterEntryValidator>
982 anyNumberParameterEntryValidator(
984  AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
985  );
986 
992 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setIntParameter(
993  std::string const& paramName,
994  int const value, std::string const& docString,
995  ParameterList *paramList,
996  AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
997  = AnyNumberParameterEntryValidator::AcceptedTypes()
998  );
999 
1000 
1006 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setDoubleParameter(
1007  std::string const& paramName,
1008  double const& value, std::string const& docString,
1009  ParameterList *paramList,
1010  AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
1011  = AnyNumberParameterEntryValidator::AcceptedTypes()
1012  );
1013 
1014 
1020 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setNumericStringParameter(
1021  std::string const& paramName,
1022  std::string const& value, std::string const& docString,
1023  ParameterList *paramList,
1024  AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
1025  = AnyNumberParameterEntryValidator::AcceptedTypes()
1026  );
1027 
1028 
1043 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT int getIntParameter(
1044  ParameterList const& paramList, std::string const& paramName
1045  );
1046 
1047 
1062 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT double getDoubleParameter(
1063  ParameterList const& paramList,
1064  std::string const& paramName
1065  );
1066 
1067 
1083 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT std::string getNumericStringParameter(
1084  ParameterList const& paramList,
1085  std::string const& paramName
1086  );
1087 
1093 template<>
1094 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT DummyObjectGetter<AnyNumberParameterEntryValidator>{
1095 
1096 public:
1097 
1100 
1104  static RCP<AnyNumberParameterEntryValidator > getDummyObject();
1105 
1107 
1108 };
1109 
1110 
1115 template <class T>
1118  static inline T notDefined() {
1119  return T::this_type_is_missing_a_specialization();
1120  }
1121 };
1122 
1123 
1144 template <class T>
1146 public:
1147 
1149  static inline T min()
1151 
1153  static inline T max()
1155 
1158  static inline T defaultStep()
1160 
1163  static inline unsigned short defaultPrecision()
1165 
1166 };
1167 
1168 
1169 template<>
1170 class EnhancedNumberTraits<short int>{
1171 public:
1172  static inline short int min() { return std::numeric_limits<short int>::min(); }
1173  static inline short int max() { return std::numeric_limits<short int>::max(); }
1174  static inline short int defaultStep() { return 1; }
1175  static inline unsigned short defaultPrecision() { return 0; }
1176 };
1177 
1178 
1179 template<>
1180 class EnhancedNumberTraits<short unsigned int>{
1181 public:
1182  static inline short unsigned int min() { return std::numeric_limits<short unsigned int>::min(); }
1183  static inline short unsigned int max() { return std::numeric_limits<short unsigned int>::max(); }
1184  static inline short unsigned int defaultStep() { return 1; }
1185  static inline unsigned short defaultPrecision() { return 0; }
1186 };
1187 
1188 
1189 template<>
1190 class EnhancedNumberTraits<int>{
1191 public:
1192  static inline int min() { return std::numeric_limits<int>::min(); }
1193  static inline int max() { return std::numeric_limits<int>::max(); }
1194  static inline int defaultStep() { return 1; }
1195  static inline unsigned short defaultPrecision() { return 0; }
1196 };
1197 
1198 
1199 template<>
1200 class EnhancedNumberTraits<unsigned int>{
1201 public:
1202  static inline unsigned int min() { return std::numeric_limits<unsigned int>::min(); }
1203  static inline unsigned int max() { return std::numeric_limits<unsigned int>::max(); }
1204  static inline unsigned int defaultStep() { return 1; }
1205  static inline unsigned short defaultPrecision() { return 0; }
1206 };
1207 
1208 
1209 template<>
1210 class EnhancedNumberTraits<long int>{
1211 public:
1212  static inline long int min() { return std::numeric_limits<long int>::min(); }
1213  static inline long int max() { return std::numeric_limits<long int>::max(); }
1214  static inline long int defaultStep() { return 1; }
1215  static inline unsigned short defaultPrecision() { return 0; }
1216 };
1217 
1218 
1219 template<>
1220 class EnhancedNumberTraits<long unsigned int>{
1221 public:
1222  static inline long unsigned int min() { return std::numeric_limits<long unsigned int>::min(); }
1223  static inline long unsigned int max() { return std::numeric_limits<long unsigned int>::max(); }
1224  static inline long unsigned int defaultStep() { return 1; }
1225  static inline unsigned short defaultPrecision() { return 0; }
1226 };
1227 
1228 
1229 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
1230 
1231 
1232 template<>
1233 class EnhancedNumberTraits<long long int>{
1234 public:
1235  static inline long long int min() { return std::numeric_limits<long long int>::min(); }
1236  static inline long long int max() { return std::numeric_limits<long long int>::max(); }
1237  static inline long long int defaultStep() { return 1; }
1238  static inline unsigned short defaultPrecision() { return 0; }
1239 };
1240 
1241 
1242 template<>
1243 class EnhancedNumberTraits<long long unsigned int>{
1244 public:
1245  static inline long long unsigned int min() { return std::numeric_limits<long long unsigned int>::min(); }
1246  static inline long long unsigned int max() { return std::numeric_limits<long long unsigned int>::max(); }
1247  static inline long long unsigned int defaultStep() { return 1; }
1248  static inline unsigned short defaultPrecision() { return 0; }
1249 };
1250 
1251 
1252 #endif // HAVE_TEUCHOS_LONG_LONG_INT
1253 
1254 
1255 #ifdef HAVE_TEUCHOSCORE_QUADMATH
1256 template<>
1257 class EnhancedNumberTraits<__float128>{
1258 public:
1259  static inline __float128 min() { return -std::numeric_limits<__float128>::max(); }
1260  static inline __float128 max() { return std::numeric_limits<__float128>::max(); }
1261  static inline __float128 defaultStep() { return 1; }
1262  static inline unsigned short defaultPrecision() { return 100; }
1263 };
1264 #endif // HAVE_TEUCHOSCORE_QUADMATH
1265 
1266 template<>
1267 class EnhancedNumberTraits<double>{
1268 public:
1269  static inline double min() { return -std::numeric_limits<double>::max(); }
1270  static inline double max() { return std::numeric_limits<double>::max(); }
1271  static inline double defaultStep() { return 1; }
1272  static inline unsigned short defaultPrecision() { return 100; }
1273 };
1274 
1275 template<>
1276 class EnhancedNumberTraits<float>{
1277 public:
1278  static inline float min() { return -std::numeric_limits<float>::max(); }
1279  static inline float max() { return std::numeric_limits<float>::max(); }
1280  static inline float defaultStep() { return 1; }
1281  static inline unsigned short defaultPrecision() { return 100; }
1282 };
1283 
1289 template <class T>
1291 
1292 public:
1293 
1296 
1312  T min,
1313  T max,
1315  unsigned short precision=EnhancedNumberTraits<T>::defaultPrecision()):
1317  minVal(min), maxVal(max), step_(step), precision_(precision),
1318  containsMin(true), containsMax(true){}
1319 
1325  minVal(EnhancedNumberTraits<T>::min()),
1326  maxVal(EnhancedNumberTraits<T>::max()),
1327  step_(EnhancedNumberTraits<T>::defaultStep()),
1328  precision_(EnhancedNumberTraits<T>::defaultPrecision()),
1329  containsMin(false),
1330  containsMax(false){}
1331 
1333 
1335 
1336 
1341  void setMin(T min){
1342  minVal = min;
1343  containsMin = true;
1344  }
1345 
1350  void setMax(T max){
1351  maxVal = max;
1352  containsMax = true;
1353  }
1354 
1359  void setStep(T step){
1360  step_ = step;
1361  }
1362 
1367  void setPrecision(unsigned short precision){
1368  precision_ = precision;
1369  }
1370 
1372 
1375 
1380  T getMin() const{
1381  return minVal;
1382  }
1383 
1388  T getMax() const{
1389  return maxVal;
1390  }
1391 
1396  T getStep() const{
1397  return step_;
1398  }
1399 
1404  unsigned short getPrecision() const{
1405  return precision_;
1406  }
1407 
1409 
1411 
1412 
1417  bool hasMin() const{
1418  return containsMin;
1419  }
1420 
1425  bool hasMax() const{
1426  return containsMax;
1427  }
1428 
1430 
1433 
1436  return null;
1437  }
1438 
1440  void validate(ParameterEntry const &entry, std::string const &paramName,
1441  std::string const &sublistName) const;
1442 
1444  void validateAndModify( std::string const& paramName,
1445  std::string const& sublistName, ParameterEntry * entry) const;
1446 
1449  const bool activeQuery) const;
1450 
1452  const std::string getXMLTypeName() const{
1453  return "EnhancedNumberValidator(" + TypeNameTraits<T>::name()+ ")";
1454  }
1455 
1457  void printDoc(std::string const &docString, std::ostream &out) const{
1458  StrUtils::printLines(out,"# ",docString);
1459  out << "#\tValidator Used: " << std::endl;
1460  out << "#\t\tNumber Validator" << std::endl;
1461  out << "#\t\tType: " << Teuchos::TypeNameTraits<T>::name() <<
1462  std::endl;
1463  out << "#\t\tMin (inclusive): " << minVal << std::endl;
1464  out << "#\t\tMax (inclusive): " << maxVal << std::endl;
1465  }
1466 
1468 
1469 private:
1472 
1473  // note this was discussed in issue #612
1474  // currently we are keeping a string validator with EnhancedNumberValidator
1475  // an alternative is to make a combined class for AnyNumberParameterEntryValidator
1476  // and EnhancedNumberValidator
1477  bool useIntConversions() const;
1478 
1480 
1483 
1486  T minVal;
1487 
1490  T maxVal;
1491 
1494  T step_;
1495 
1499  unsigned short precision_;
1500 
1503  bool containsMin;
1504 
1507  bool containsMax;
1508 
1510 
1511 };
1512 
1513 template<class T>
1515  std::string const& paramName,
1516  std::string const& sublistName,
1517  ParameterEntry * entry
1518  ) const
1519 {
1520  TEUCHOS_TEST_FOR_EXCEPT(0==entry);
1521 
1522  any anyValue = entry->getAny(true);
1523  // preferred type is not string
1524  if( anyValue.type() == typeid(std::string) ) {
1525  anyValue = getNumberFromString(*entry,false);
1526  entry->setValue(
1527  any_cast<T>(anyValue),
1528  false // isDefault
1529  );
1530  }
1531  else {
1532  // default behavior
1534  paramName, sublistName, entry);
1535  }
1536 }
1537 
1538 template<class T>
1540 {
1541  // this will need some rethinking and exists only for supporting
1542  // conversion of strings to the templated type T
1543  // but we may want to unify this into the base class anyways
1544  // and share string conversion concepts with other parameters
1545  // like AnyNumberParameterEntryValidator
1546  if(typeid(T) == typeid(char)) return true;
1547  if(typeid(T) == typeid(unsigned char)) return true;
1548  if(typeid(T) == typeid(int)) return true;
1549  if(typeid(T) == typeid(unsigned int)) return true;
1550  if(typeid(T) == typeid(short)) return true;
1551  if(typeid(T) == typeid(unsigned short)) return true;
1552  if(typeid(T) == typeid(long)) return true;
1553  if(typeid(T) == typeid(unsigned long)) return true;
1554  if(typeid(T) == typeid(long long)) return true;
1555  if(typeid(T) == typeid(unsigned long long)) return true;
1556 
1557  // default to double stod to older atof conversion
1558  // depending on HAVE_TEUCHOSCORE_CXX11
1559  // those conversions would probably handle all above discrete types anyways
1560  return false;
1561 }
1562 
1563 template<class T>
1565  const ParameterEntry &entry, const bool activeQuery
1566  ) const
1567 {
1568  // perhaps we want to just eliminate the int checks
1569  // and always use double conversion which I think would work
1570  // well for all types - but this will give us a behavior which mirrors
1571  // AnyNumberParameterEntryValidator more closely
1572  const any &anyValue = entry.getAny(activeQuery);
1573  if(useIntConversions()) {
1574  return any((T)convertStringToInt(any_cast<std::string>(anyValue)));
1575  }
1576  else { // if not discrete, read as a double and cast to our type T
1577  return any((T)convertStringToDouble(any_cast<std::string>(anyValue)));
1578  }
1579 }
1580 
1581 template<class T>
1582 void EnhancedNumberValidator<T>::validate(ParameterEntry const &entry, std::string const &paramName,
1583  std::string const &sublistName) const
1584 {
1585  any anyValue = entry.getAny(true);
1586 
1587  // This was new code added to allow EnhancedNumberValidator to accept a string
1588  // This was added for consistency with AnyNumberParameterEntryValidator
1589  // and the new BoolParameterEntryValidator which all take string
1590  // We may wish to change this to be optional like AnyNumberParameterEntryValidator
1591  if( anyValue.type() == typeid(std::string) ) {
1592  // try to upgrade from a string to a number
1593  anyValue = getNumberFromString(entry, false);
1594  }
1595 
1596  const std::string &entryName = entry.getAny(false).typeName();
1597  TEUCHOS_TEST_FOR_EXCEPTION(anyValue.type() != typeid(T),
1599  "The \"" << paramName << "\"" <<
1600  " parameter in the \"" << sublistName <<
1601  "\" sublist is has an error." << std::endl << std::endl <<
1602  "Error: The value that you entered was the wrong type." << std::endl <<
1603  "Parameter: " << paramName << std::endl <<
1604  "Type specified: " << entryName << std::endl <<
1605  "Type accepted: " << Teuchos::TypeNameTraits<T>::name() << std::endl);
1606 
1607  bool isValueInRange;
1608  any_cast<T>(anyValue) >= minVal && any_cast<T>(anyValue) <= maxVal
1609  ? isValueInRange = true : isValueInRange=false;
1610  TEUCHOS_TEST_FOR_EXCEPTION(!(isValueInRange),
1612  "The \"" << paramName << "\"" <<
1613  " parameter in the \"" << sublistName <<
1614  "\" sublist is has an error." << std::endl << std::endl <<
1615  "Error: The value that was entered doesn't fall with in " <<
1616  "the range set by the validator" << std::endl <<
1617  "Parameter: " << paramName << std::endl <<
1618  "Min: " << minVal << std::endl <<
1619  "Max: " << maxVal << std::endl <<
1620  "Value entered: " <<
1621  (any_cast<T>(anyValue)) << std::endl << std::endl);
1622 }
1623 
1629 template<class T>
1631 
1632 public:
1633 
1636 
1641 
1643 };
1644 
1645 template<class T>
1648 {
1649  return rcp(new EnhancedNumberValidator<T>);
1650 }
1651 
1660 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT FileNameValidator : public ParameterEntryValidator {
1661 
1662 public:
1663 
1666 
1669  static bool mustAlreadyExistDefault() { return false; }
1670 
1672 
1675 
1681  FileNameValidator(bool mustAlreadyExist = mustAlreadyExistDefault());
1682 
1684 
1686 
1687 
1693  bool fileMustExist() const;
1694 
1696 
1697 
1708  bool fileEmptyNameOK() const;
1709 
1711 
1713 
1714 
1723  bool setFileMustExist(bool shouldFileExist);
1724 
1733  bool setFileEmptyNameOK(bool isEmptyNameOK);
1734 
1736 
1739 
1741  ValidStringsList validStringValues() const;
1742 
1744  void validate(
1745  ParameterEntry const &entry,
1746  std::string const &paramName,
1747  std::string const &sublistName) const;
1748 
1750  const std::string getXMLTypeName() const;
1751 
1753  void printDoc(std::string const &docString, std::ostream &out) const;
1754 
1756 
1757 private:
1758 
1761 
1765  bool mustAlreadyExist_;
1766  bool EmptyNameOK_;
1767 
1769 
1770 };
1771 
1777 template<>
1778 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT DummyObjectGetter<FileNameValidator>{
1779 
1780 public:
1781 
1784 
1788  static RCP<FileNameValidator> getDummyObject();
1789 
1791 
1792 };
1793 
1801 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT StringValidator : public ParameterEntryValidator {
1802 
1803 public:
1804 
1807 
1810  StringValidator();
1811 
1816  StringValidator(const Teuchos::Array<std::string> &validStrings);
1817 
1819 
1821 
1822 
1830  ValidStringsList setValidStrings(
1831  const Teuchos::Array<std::string> &validStrings);
1832 
1834 
1837 
1839  ValidStringsList validStringValues() const;
1840 
1842  void validate(ParameterEntry const &entry, std::string const &paramName,
1843  std::string const &sublistName) const;
1844 
1846  const std::string getXMLTypeName() const;
1847 
1849  void printDoc(std::string const &docString, std::ostream &out) const;
1850 
1852 
1853 private:
1854 
1857 
1860  ValidStringsList validStrings_;
1861 
1863 
1864 };
1865 
1871 template<>
1872 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT DummyObjectGetter<StringValidator>{
1873 
1874 public:
1875 
1878 
1882  static RCP<StringValidator> getDummyObject();
1883 
1885 
1886 };
1887 
1888 
1892 template<class ValidatorType, class EntryType>
1894 
1895 public:
1896 
1899 
1908  prototypeValidator_(prototypeValidator){}
1909 
1911 
1914 
1917  return prototypeValidator_;
1918  }
1919 
1921 
1924 
1927  return prototypeValidator_->validStringValues();
1928  }
1929 
1931 
1932 private:
1933 
1936 
1939  RCP<const ValidatorType> prototypeValidator_;
1940 
1943 
1945 
1946 };
1947 
1960 template<class ValidatorType, class EntryType>
1961 class TwoDArrayValidator : public AbstractArrayValidator<ValidatorType, EntryType>{
1962 public:
1965 
1973  AbstractArrayValidator<ValidatorType, EntryType>(prototypeValidator){}
1974 
1976 
1979 
1981  virtual void validate(ParameterEntry const &entry, std::string const &paramName,
1982  std::string const &sublistName) const;
1983 
1985  const std::string getXMLTypeName() const{
1986  return "TwoDArrayValidator(" +
1987  this->getPrototype()->getXMLTypeName() + ", " +
1989  }
1990 
1992  virtual void printDoc(std::string const &docString, std::ostream &out) const
1993  {
1994  StrUtils::printLines(out,"# ",docString);
1995  std::string toPrint;
1996  toPrint += "TwoDArrayValidator:\n";
1997  toPrint += "Prototype Validator:\n";
1998  this->getPrototype()->printDoc(toPrint, out);
1999  }
2000 
2002 
2003 };
2004 
2005 template<class ValidatorType, class EntryType>
2006 void TwoDArrayValidator<ValidatorType, EntryType>::validate(ParameterEntry const &entry, std::string const &paramName,
2007  std::string const &sublistName) const
2008 {
2009  any anyValue = entry.getAny(true);
2010  const std::string &entryName = entry.getAny(false).typeName();
2013  "The \"" << paramName << "\"" <<
2014  " parameter in the \"" << sublistName <<
2015  "\" sublist is has an error." << std::endl << std::endl <<
2016  "Error: The value you entered was the wrong type." << std::endl <<
2017  "Parameter: " << paramName << std::endl <<
2018  "Type specified: " << entryName << std::endl <<
2019  "Type accepted: " << TypeNameTraits<TwoDArray<EntryType> >::name() <<
2020  std::endl << std::endl);
2021 
2022  TwoDArray<EntryType> extracted =
2023  getValue<Teuchos::TwoDArray<EntryType> >(entry);
2024  RCP<const ParameterEntryValidator> prototype = this->getPrototype();
2025  for(int i = 0; i<extracted.getNumRows(); ++i){
2026  for(int j = 0; j<extracted.getNumCols(); ++j){
2027  ParameterEntry dummyParameter;
2028  dummyParameter.setValue(extracted(i,j));
2029  try{
2030  prototype->validate(
2031  dummyParameter, paramName, sublistName);
2032  }
2034  std::stringstream oss;
2035  oss << "TwoDArray Validator Exception:" << std::endl <<
2036  "Bad Index: (" << i << "," << j << ")" << std::endl << e.what();
2037  throw Exceptions::InvalidParameterValue(oss.str());
2038  }
2039  }
2040  }
2041 }
2042 
2043 
2049 template<class ValidatorType, class EntryType>
2050 class DummyObjectGetter<TwoDArrayValidator<ValidatorType, EntryType> >{
2051 
2052 public:
2053 
2056 
2061 
2063 
2064 };
2065 
2066 template<class ValidatorType, class EntryType>
2069 {
2072 }
2073 
2077 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT TwoDArrayStringValidator :
2078  public TwoDArrayValidator<StringValidator, std::string>{
2079 
2080 public:
2081 
2084 
2087  TwoDArrayValidator<StringValidator, std::string>(prototypeValidator){}
2088 
2090 
2091 };
2092 
2093 
2098 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT TwoDArrayFileNameValidator :
2099  public TwoDArrayValidator<FileNameValidator, std::string>{
2100 
2101 public:
2102 
2105 
2108  TwoDArrayValidator<FileNameValidator, std::string>(prototypeValidator){}
2109 
2111 
2112 };
2113 
2114 
2118 template<class T>
2119 class TwoDArrayNumberValidator : public TwoDArrayValidator<EnhancedNumberValidator<T>, T>{
2120 public:
2123 
2126  RCP<const EnhancedNumberValidator<T> > prototypeValidator):
2127  TwoDArrayValidator<EnhancedNumberValidator<T>, T>(prototypeValidator){}
2128 
2130 
2131 };
2132 
2133 
2145 template<class ValidatorType, class EntryType>
2146 class ArrayValidator : public AbstractArrayValidator<ValidatorType, EntryType>{
2147 
2148 public:
2149 
2152 
2159  AbstractArrayValidator<ValidatorType, EntryType>(prototypeValidator){}
2160 
2162 
2165 
2167  virtual void validate(ParameterEntry const &entry, std::string const &paramName,
2168  std::string const &sublistName) const;
2169 
2171  const std::string getXMLTypeName() const{
2172  return "ArrayValidator(" +
2173  this->getPrototype()->getXMLTypeName() + ", " +
2175  }
2176 
2178  virtual void printDoc(std::string const &docString, std::ostream &out) const
2179  {
2180  StrUtils::printLines(out,"# ",docString);
2181  std::string toPrint;
2182  toPrint += "ArrayValidator:\n";
2183  toPrint += "Prototype Validator:\n";
2184  this->getPrototype()->printDoc(toPrint, out);
2185  }
2186 
2188 
2189 };
2190 
2191 template<class ValidatorType, class EntryType>
2192 void ArrayValidator<ValidatorType, EntryType>::validate(ParameterEntry const &entry, std::string const &paramName,
2193  std::string const &sublistName) const
2194 {
2195  any anyValue = entry.getAny(true);
2196  const std::string &entryName = entry.getAny(false).typeName();
2197  TEUCHOS_TEST_FOR_EXCEPTION(anyValue.type() != typeid(Array<EntryType>),
2199  "The \"" << paramName << "\"" <<
2200  " parameter in the \"" << sublistName <<
2201  "\" sublist is has an error." << std::endl << std::endl <<
2202  "Error: The value you entered was the wrong type." << std::endl <<
2203  "Parameter: " << paramName << std::endl <<
2204  "Type specified: " << entryName << std::endl <<
2205  "Type accepted: " << TypeNameTraits<Array<EntryType> >::name() <<
2206  std::endl << std::endl);
2207 
2208  Array<EntryType> extracted =
2209  getValue<Teuchos::Array<EntryType> >(entry);
2210  RCP<const ParameterEntryValidator> prototype = this->getPrototype();
2211  for(int i = 0; i<extracted.size(); ++i){
2212  ParameterEntry dummyParameter;
2213  dummyParameter.setValue(extracted[i]);
2214  try{
2215  prototype->validate(
2216  dummyParameter, paramName, sublistName);
2217  }
2219  std::stringstream oss;
2220  oss << "Array Validator Exception:" << std::endl <<
2221  "Bad Index: " << i << std::endl << e.what();
2222  throw Exceptions::InvalidParameterValue(oss.str());
2223  }
2224  }
2225 }
2226 
2232 template<class ValidatorType, class EntryType>
2233 class DummyObjectGetter<ArrayValidator<ValidatorType, EntryType> >{
2234 
2235 public:
2236 
2239 
2244 
2246 
2247 };
2248 
2249 template<class ValidatorType, class EntryType>
2252 {
2255 }
2256 
2257 
2266 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT ArrayStringValidator :
2267  public ArrayValidator<StringValidator, std::string>{
2268 
2269 public:
2270 
2273 
2276  ArrayValidator<StringValidator, std::string>(prototypeValidator){}
2277 
2279 
2280 };
2281 
2282 
2291 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT ArrayFileNameValidator : public ArrayValidator<FileNameValidator, std::string>{
2292 
2293 public:
2294 
2297 
2300  ArrayValidator<FileNameValidator, std::string>(prototypeValidator){}
2301 
2303 
2304 };
2305 
2306 
2314 template<class T>
2315 class ArrayNumberValidator : public ArrayValidator<EnhancedNumberValidator<T>, T>{
2316 public:
2319 
2322  RCP<const EnhancedNumberValidator<T> > prototypeValidator):
2323  ArrayValidator<EnhancedNumberValidator<T>, T>(prototypeValidator){}
2324 
2326 
2327 };
2328 
2329 
2330 
2331 // ///////////////////////////
2332 // Implementations
2333 
2334 
2335 //
2336 // StringToIntegralParameterEntryValidator
2337 //
2338 
2339 
2340 // Constructors
2341 
2342 
2343 template<class IntegralType>
2344 StringToIntegralParameterEntryValidator<IntegralType>::
2345 StringToIntegralParameterEntryValidator (ArrayView<const std::string> const& strings,
2346  std::string const& defaultParameterName,
2347  const bool caseSensitive) :
2349  defaultParameterName_ (defaultParameterName),
2350  caseSensitive_ (caseSensitive)
2351 {
2352  typedef typename map_t::value_type val_t;
2353  for (int i = 0; i < static_cast<int> (strings.size ()); ++i) {
2354  const bool unique = caseSensitive_ ?
2355  map_.insert (val_t (strings[i], static_cast<IntegralType> (i))).second :
2356  map_.insert (val_t (upperCase (strings[i]), static_cast<IntegralType> (i))).second;
2358  ! unique, std::logic_error,
2359  "For parameter \"" << defaultParameterName_ << "\": "
2360  "strings[" << i << "] = \"" << strings[i] << "\" is a duplicate.");
2361  }
2362  setValidValues (strings);
2363 }
2364 
2365 
2366 template<class IntegralType>
2369  ArrayView<const IntegralType> const& integralValues,
2370  std::string const& defaultParameterName,
2371  const bool caseSensitive) :
2373  defaultParameterName_ (defaultParameterName),
2374  caseSensitive_ (caseSensitive)
2375 {
2376 #ifdef TEUCHOS_DEBUG
2377  TEUCHOS_ASSERT_EQUALITY( strings.size(), integralValues.size() );
2378 #endif
2380  strings.size() != integralValues.size(),
2381  std::logic_error,
2382  "The input arrays strings and integralValues must have the same length.");
2383 
2384  typedef typename map_t::value_type val_t;
2385  for (int i = 0; i < static_cast<int> (strings.size ()); ++i) {
2386  const bool unique = caseSensitive_ ?
2387  map_.insert (val_t (strings[i], integralValues[i])).second :
2388  map_.insert (val_t (upperCase (strings[i]), integralValues[i])).second;
2389 
2391  ! unique, std::logic_error,
2392  "For parameter \"" << defaultParameterName_ << "\": "
2393  "strings[" << i << "] = \"" << strings[i] << "\" is a duplicate.");
2394  }
2395  setValidValues (strings);
2396 }
2397 
2398 template<class IntegralType>
2401  ArrayView<const std::string> const& stringsDocs,
2402  ArrayView<const IntegralType> const& integralValues,
2403  std::string const& defaultParameterName,
2404  const bool caseSensitive) :
2406  defaultParameterName_ (defaultParameterName),
2407  caseSensitive_ (caseSensitive)
2408 {
2409 #ifdef TEUCHOS_DEBUG
2410  TEUCHOS_ASSERT_EQUALITY( strings.size(), stringsDocs.size() );
2411  TEUCHOS_ASSERT_EQUALITY( strings.size(), integralValues.size() );
2412 #endif
2413 
2415  strings.size() != integralValues.size(),
2416  std::logic_error,
2417  "The input arrays strings and integralValues must have the same length.");
2418 
2420  strings.size() != stringsDocs.size(),
2421  std::logic_error,
2422  "The input arrays strings and stringsDocs must have the same length.");
2423 
2424  typedef typename map_t::value_type val_t;
2425  for (int i = 0; i < static_cast<int> (strings.size ()); ++i) {
2426  const bool unique = caseSensitive_ ?
2427  map_.insert (val_t (strings[i], integralValues[i])).second :
2428  map_.insert (val_t (upperCase (strings[i]), integralValues[i])).second;
2430  ! unique, std::logic_error,
2431  "For parameter \"" << defaultParameterName_ << "\": "
2432  "strings[" << i << "] = \"" << strings[i] << "\" is a duplicate.");
2433  }
2434  setValidValues(strings,&stringsDocs);
2435 }
2436 
2437 // Lookup functions
2438 
2439 
2440 template<class IntegralType>
2441 IntegralType
2443  const std::string &str, const std::string &paramName
2444  ,const std::string &sublistName
2445  ) const
2446 {
2447  typename map_t::const_iterator itr = map_.find (caseSensitive_ ? str : upperCase (str));
2449  itr == map_.end(), Exceptions::InvalidParameterValue
2450  ,"Error, the value \"" << str << "\" is not recognized for the parameter \""
2451  << ( paramName.length() ? paramName : defaultParameterName_ ) << "\""
2452  << "\nin the sublist \"" << sublistName << "\"."
2453  << "\n\nValid values include:"
2454  << "\n {\n"
2455  << validValues_
2456  << " }"
2457  );
2458  return (*itr).second;
2459 }
2460 
2461 
2462 template<class IntegralType>
2463 IntegralType
2465  const ParameterEntry &entry, const std::string &paramName
2466  ,const std::string &sublistName, const bool activeQuery
2467  ) const
2468 {
2469  const bool validType = ( entry.getAny(activeQuery).type() == typeid(std::string) );
2472  ,"Error, the parameter {paramName=\""<<(paramName.length()?paramName:defaultParameterName_)
2473  << "\",type=\""<<entry.getAny(activeQuery).typeName()<<"\"}"
2474  << "\nin the sublist \"" << sublistName << "\""
2475  << "\nhas the wrong type."
2476  << "\n\nThe correct type is \"string\"!"
2477  );
2478  const std::string
2479  &strValue = any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail!
2480  return getIntegralValue(strValue,paramName,sublistName); // This will validate the value and throw!
2481 }
2482 
2483 
2484 template<class IntegralType>
2485 std::string
2487  const ParameterEntry &entry, const std::string &paramName
2488  ,const std::string &sublistName, const bool activeQuery
2489  ) const
2490 {
2491  // Validate the parameter's type and value
2492  this->getIntegralValue(entry,paramName,sublistName,activeQuery);
2493  // Return the std::string value which is now validated!
2494  return any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail!
2495 }
2496 
2497 
2498 template<class IntegralType>
2499 IntegralType
2501  ParameterList &paramList, const std::string &paramName
2502  ,const std::string &defaultValue
2503  ) const
2504 {
2505  const std::string& strValue =
2506  paramList.get (paramName,
2507  caseSensitive_ ? defaultValue : upperCase (defaultValue));
2508  return getIntegralValue (strValue, paramName, paramList.name ());
2509 }
2510 
2511 
2512 template<class IntegralType>
2513 std::string
2515  ParameterList &paramList, const std::string &paramName
2516  ,const std::string &defaultValue
2517  ) const
2518 {
2519  const std::string& strValue =
2520  paramList.get (paramName,
2521  caseSensitive_ ? defaultValue : upperCase (defaultValue));
2522  getIntegralValue(strValue,paramName,paramList.name()); // Validate!
2523  return strValue;
2524 }
2525 
2526 template<class IntegralType>
2529 {
2530  return validStringValuesDocs_;
2531 }
2532 
2533 template<class IntegralType>
2534 const std::string&
2536 {
2537  return defaultParameterName_;
2538 }
2539 
2540 template<class IntegralType>
2541 std::string
2543  const std::string &str, const std::string &paramName
2544  ,const std::string &sublistName
2545  ) const
2546 {
2547  getIntegralValue (caseSensitive_ ? str : upperCase (str),
2548  paramName,
2549  sublistName); // Validate!
2550  return str;
2551 }
2552 
2553 
2554 // Overridden from ParameterEntryValidator
2555 
2556 template<class IntegralType>
2557 const std::string
2559  return "StringIntegralValidator(" + TypeNameTraits<IntegralType>::name () + ")";
2560 }
2561 
2562 template<class IntegralType>
2564  std::string const& docString
2565  ,std::ostream & out
2566  ) const
2567 {
2568  StrUtils::printLines(out,"# ",docString);
2569  out << "# Valid std::string values:\n";
2570  out << "# {\n";
2571  if(validStringValuesDocs_.get()) {
2572  for( int i = 0; i < static_cast<int>(validStringValues_->size()); ++i ) {
2573  out << "# \"" << (*validStringValues_)[i] << "\"\n";
2574  StrUtils::printLines(out,"# ",(*validStringValuesDocs_)[i] );
2575  }
2576  }
2577  else {
2578  StrUtils::printLines(out,"# ",validValues_);
2579  // Note: Above validValues_ has for initial spaces already so indent should
2580  // be correct!
2581  }
2582  out << "# }\n";
2583 }
2584 
2585 
2586 template<class IntegralType>
2589 {
2590  return validStringValues_;
2591 }
2592 
2593 
2594 template<class IntegralType>
2596  ParameterEntry const& entry
2597  ,std::string const& paramName
2598  ,std::string const& sublistName
2599  ) const
2600 {
2601  this->getIntegralValue (entry, paramName, sublistName, false);
2602 }
2603 
2604 
2605 // private
2606 
2607 template<class IntegralType>
2609  ArrayView<const std::string> const& strings
2610  ,ArrayView<const std::string> const* stringsDocs
2611  )
2612 {
2613  if (caseSensitive_) {
2614  validStringValues_ = rcp (new Array<std::string> (strings));
2615  }
2616  else {
2617  RCP<Array<std::string> > vals (new Array<std::string> (strings.size ()));
2618  for (Array<std::string>::size_type i = 0; i < strings.size (); ++i) {
2619  (*vals)[i] = upperCase (strings[i]);
2620  }
2621  validStringValues_ = rcp_const_cast<const Array<std::string> > (vals);
2622  }
2623 
2624  if (stringsDocs) {
2625  validStringValuesDocs_ = rcp (new Array<std::string> (*stringsDocs));
2626  }
2627  // Build the list of valid values in the same order as passed in by the client.
2628  std::ostringstream oss;
2629  for (int i = 0; i < static_cast<int> (strings.size()); ++i) {
2630  oss << " \"" << strings[i] << "\"\n";
2631  }
2632  // Note: Above four spaces is designed for the error output above.
2633  validValues_ = oss.str();
2634 }
2635 
2636 
2637 } // namespace Teuchos
2638 
2639 
2640 //
2641 // Nonmember function implementations for StringToIntegralParameterEntryValidator
2642 //
2643 
2644 
2645 template<class IntegralType>
2646 inline
2648 Teuchos::stringToIntegralParameterEntryValidator(
2649  ArrayView<const std::string> const& strings,
2650  std::string const& defaultParameterName
2651  )
2652 {
2653  return rcp(
2654  new StringToIntegralParameterEntryValidator<IntegralType>(
2655  strings, defaultParameterName
2656  )
2657  );
2658 }
2659 
2660 
2661 template<class IntegralType>
2662 inline
2664 Teuchos::stringToIntegralParameterEntryValidator(
2665  ArrayView<const std::string> const& strings,
2666  std::string const& defaultParameterName,
2667  const bool caseSensitive
2668  )
2669 {
2670  typedef StringToIntegralParameterEntryValidator<IntegralType> ret_type;
2671  return rcp (new ret_type (strings, defaultParameterName, caseSensitive));
2672 }
2673 
2674 
2675 
2676 template<class IntegralType>
2677 inline
2679 Teuchos::stringToIntegralParameterEntryValidator(
2680  ArrayView<const std::string> const& strings,
2681  ArrayView<const IntegralType> const& integralValues,
2682  std::string const& defaultParameterName
2683  )
2684 {
2685  return rcp(
2686  new StringToIntegralParameterEntryValidator<IntegralType>(
2687  strings, integralValues, defaultParameterName
2688  )
2689  );
2690 }
2691 
2692 
2693 template<class IntegralType>
2694 inline
2696 Teuchos::stringToIntegralParameterEntryValidator(
2697  ArrayView<const std::string> const& strings,
2698  ArrayView<const IntegralType> const& integralValues,
2699  std::string const& defaultParameterName,
2700  const bool caseSensitive)
2701 {
2702  typedef StringToIntegralParameterEntryValidator<IntegralType> ret_type;
2703  return rcp (new ret_type (strings, integralValues,
2704  defaultParameterName, caseSensitive));
2705 }
2706 
2707 
2708 template<class IntegralType>
2709 inline
2711 Teuchos::stringToIntegralParameterEntryValidator(
2712  ArrayView<const std::string> const& strings,
2713  ArrayView<const std::string> const& stringsDocs,
2714  ArrayView<const IntegralType> const& integralValues,
2715  std::string const& defaultParameterName
2716  )
2717 {
2718  return rcp(
2719  new StringToIntegralParameterEntryValidator<IntegralType>(
2720  strings, stringsDocs, integralValues, defaultParameterName
2721  )
2722  );
2723 }
2724 
2725 
2726 template<class IntegralType>
2727 inline
2729 Teuchos::stringToIntegralParameterEntryValidator(
2730  ArrayView<const std::string> const& strings,
2731  ArrayView<const std::string> const& stringsDocs,
2732  ArrayView<const IntegralType> const& integralValues,
2733  std::string const& defaultParameterName,
2734  const bool caseSensitive)
2735 {
2736  typedef StringToIntegralParameterEntryValidator<IntegralType> ret_type;
2737  return rcp (new ret_type (strings, stringsDocs, integralValues,
2738  defaultParameterName, caseSensitive));
2739 }
2740 
2741 
2742 template<class IntegralType>
2743 void Teuchos::setStringToIntegralParameter(
2744  std::string const& paramName,
2745  std::string const& defaultValue,
2746  std::string const& docString,
2747  ArrayView<const std::string> const& strings,
2748  ParameterList * paramList
2749  )
2750 {
2751  typedef ParameterEntryValidator PEV;
2752  TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
2753  paramList->set(
2754  paramName, defaultValue, docString,
2755  rcp_implicit_cast<const PEV>(
2756  stringToIntegralParameterEntryValidator<IntegralType>(
2757  strings, paramName
2758  )
2759  )
2760  );
2761 }
2762 
2763 
2764 template<class IntegralType>
2765 void Teuchos::setStringToIntegralParameter(
2766  std::string const& paramName,
2767  std::string const& defaultValue,
2768  std::string const& docString,
2769  ArrayView<const std::string> const& strings,
2770  ArrayView<const IntegralType> const& integralValues,
2771  ParameterList * paramList
2772  )
2773 {
2774  typedef ParameterEntryValidator PEV;
2775  TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
2776  paramList->set(
2777  paramName, defaultValue, docString,
2778  rcp_implicit_cast<const PEV>(
2779  stringToIntegralParameterEntryValidator<IntegralType>(
2780  strings, integralValues, paramName
2781  )
2782  )
2783  );
2784 }
2785 
2786 
2787 template<class IntegralType>
2788 void Teuchos::setStringToIntegralParameter(
2789  std::string const& paramName,
2790  std::string const& defaultValue,
2791  std::string const& docString,
2792  ArrayView<const std::string> const& strings,
2793  ArrayView<const std::string> const& stringsDocs,
2794  ArrayView<const IntegralType> const& integralValues,
2795  ParameterList * paramList
2796  )
2797 
2798 {
2799  typedef ParameterEntryValidator PEV;
2800  TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
2801  paramList->set(
2802  paramName, defaultValue, docString,
2803  rcp_implicit_cast<const PEV>(
2804  stringToIntegralParameterEntryValidator<IntegralType>(
2805  strings, stringsDocs, integralValues, paramName
2806  )
2807  )
2808  );
2809 }
2810 
2811 
2812 template<class IntegralType>
2813 IntegralType Teuchos::getIntegralValue(
2814  ParameterList const& paramList, std::string const& paramName
2815  )
2816 {
2817  const ParameterEntry &entry = paramList.getEntry(paramName);
2818  RCP<const StringToIntegralParameterEntryValidator<IntegralType> >
2819  integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>(
2820  entry, paramList, paramName
2821  );
2822  return integralValidator->getIntegralValue(
2823  entry, paramName, paramList.name(), true );
2824 }
2825 
2826 
2827 template<class IntegralType>
2828 std::string Teuchos::getStringValue(
2829  ParameterList const& paramList, std::string const& paramName
2830  )
2831 {
2832  const ParameterEntry &entry = paramList.getEntry(paramName);
2833  RCP<const StringToIntegralParameterEntryValidator<IntegralType> >
2834  integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>(
2835  entry, paramList, paramName
2836  );
2837  return integralValidator->getStringValue(
2838  entry, paramName, paramList.name(), true
2839  );
2840 }
2841 
2842 
2843 template<class IntegralType>
2846  ParameterEntry const& entry, ParameterList const& paramList,
2847  std::string const& paramName
2848  )
2849 {
2850  const RCP<const ParameterEntryValidator> validator = entry.validator();
2852  is_null(validator), Exceptions::InvalidParameterType,
2853  "Error! The parameter \""<<paramName<<"\" exists\n"
2854  "in the parameter (sub)list \""<<paramList.name()<<"\"\n"
2855  "but it does not contain any validator needed to extract\n"
2856  "an integral value of type \""<<TypeNameTraits<IntegralType>::name()<<"\"!"
2857  );
2858  const RCP<const StringToIntegralParameterEntryValidator<IntegralType> > integralValidator =
2859  rcp_dynamic_cast<const StringToIntegralParameterEntryValidator<IntegralType> >(
2860  validator
2861  );
2863  is_null(integralValidator), Exceptions::InvalidParameterType,
2864  "Error! The parameter \""<<paramName<<"\" exists\n"
2865  "in the parameter (sub)list \""<<paramList.name()<<"\"\n"
2866  "but it contains the wrong type of validator. The expected validator type\n"
2867  "is \""<<TypeNameTraits<StringToIntegralParameterEntryValidator<IntegralType> >::name()<<"\"\n"
2868  "but the contained validator type is \""<<typeName(*validator)<<"\"!"
2869  );
2870  return integralValidator;
2871 }
2872 
2873 
2874 #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.
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...
EnhancedNumberValidator()
Constructs a EnhancedNumberValidator without an explicit minimum or maximum.
void printDoc(std::string const &docString, std::ostream &out) const
Convience class for StringValidators that are to be applied to TwoDArrays.
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<IntegralType> object out of a ParameterEntry object...
bool isCaseSensitive() const
Whether this validator is case sensitive.
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.
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 "value" in the Teuchos::ParameterList std::map.
virtual void validate(ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const
bool hasMax() const
Determines whether or not the validator has a maximum value.
ValueType & any_cast(any &operand)
Used to extract the templated value held in Teuchos::any to a given value type.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Default structure used by EnhancedNumberTraits<T> to produce a compile time error when the specializa...
void setMax(T max)
Sets the maximum acceptable value for the validator.
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.
size_type size() const
The total number of items in the managed array.
static std::ostream & printLines(std::ostream &os, const std::string &linePrefix, const std::string &lines)
Print lines with prefix first.
EVerbosityLevel
Verbosity level.
unsigned short getPrecision() const
Gets the precision specified for the validator.
ValidStringsList getStringDocs() const
Get a pointer to the array containing all the documentation strings.
Modified boost::any class, which is a container for a templated value.
Definition: Teuchos_any.hpp:86
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.
virtual void printDoc(std::string const &docString, std::ostream &out) const
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.
IntegralType getIntegralValue(ParameterList const &paramList, std::string const &paramName)
Get an integral value for a parameter that is assumed to already be set.
A std::string utilities class for Teuchos.
std::string validateString(const std::string &str, const std::string &paramName="", const std::string &sublistName="") const
Validate the std::string and pass it on.
A thin wrapper around the Array class which causes it to be interpurted as a 2D Array.
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!
Ordinal size_type
The type of Array sizes and capacities.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Templated Parameter List class.
void validate(ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const
Validate the given ParameterEntry.
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.
static unsigned short defaultPrecision()
Gets the default precision with which the number type should be displayed.
virtual void validateAndModify(std::string const &paramName, std::string const &sublistName, ParameterEntry *entry) const
Validate and perhaps modify a parameter entry&#39;s value.
T getMax() const
Gets the maximum acceptable value for the validator.
virtual void printDoc(std::string const &docString, std::ostream &out) const
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.
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.
TwoDArrayValidator(RCP< const ValidatorType > prototypeValidator)
Constructs a ArrayValidator.
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.
const std::type_info & type() const
Return the type of value being stored.
Abstract interface for an object that can validate a ParameterEntry&#39;s value.
const std::string & name() const
The name of this ParameterList.
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.
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.
Nonowning array view.
RCP< const Array< std::string > > ValidStringsList
Default traits class that just returns typeid(T).name().
virtual void validate(ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const
Class for retrieving a dummy object of type T.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
static const std::string & getPrefferedTypeString(EPreferredType enumValue)
Gets the string representation of a given preferred type enum.
std::string typeName() const
Return the name of the type.
const std::string & getDefaultParameterName() const
Get the name of the default parameter for the validator.
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 validateAndModify(std::string const &paramName, std::string const &sublistName, ParameterEntry *entry) const
RCP< const ValidatorType > getPrototype() const
Returns the prototype validator for this Array Validator.
void validate(ParameterEntry const &entry, std::string const &paramName, std::string const &sublistName) const
Class defining the traits of the number type being used in an EnhancedNumberValidator.
TwoDArrayNumberValidator(RCP< const EnhancedNumberValidator< T > > prototypeValidator)
AcceptedTypes & allowDouble(bool _allowDouble)
Set allow a double value or not.
Smart reference counting pointer class for automatic garbage collection.
TwoDArrayStringValidator(RCP< const StringValidator > prototypeValidator)
T getStep() const
Gets the step being used for the validator.
TwoDArrayFileNameValidator(RCP< const FileNameValidator > prototypeValidator)
#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 ...
RCP< T2 > rcp_const_cast(const RCP< T1 > &p1)
Constant cast of underlying RCP type from T1* to T2*.
Standard implementation of a ParameterEntryValidator that accepts numbers from a number of different ...
T getMin() const
Gets the minimum acceptable value for the validator.
Defines basic traits returning the name of a type in a portable and readable way. ...
Teuchos::any getNumberFromString(const ParameterEntry &entry, const bool activeQuery) const
void printDoc(std::string const &docString, std::ostream &out) const
Print documentation to the given output string.
#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...
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
bool hasMin() const
Determines whether or not the validator has a minimum value.
#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)