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 
322 
323 private:
325  std::string validValues_;
328 
329  typedef std::map<std::string,IntegralType> map_t;
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 
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 
693 boolParameterEntryValidator();
694 
695 
710  : public ParameterEntryValidator
711 {
712 public:
713 
716 
718  enum EPreferredType { PREFER_INT, PREFER_LONG_LONG, PREFER_DOUBLE, PREFER_STRING };
719 
720 
723  public:
725  AcceptedTypes( bool allowAllTypesByDefault = true )
726  :allowInt_(allowAllTypesByDefault)
727  ,allowLongLong_(allowAllTypesByDefault)
728  ,allowDouble_(allowAllTypesByDefault)
729  ,allowString_(allowAllTypesByDefault)
730  {}
732  AcceptedTypes& allowInt( bool _allowInt )
733  { allowInt_ = _allowInt; return *this; }
735  AcceptedTypes& allowLongLong( bool _allowLongLong )
736  { allowLongLong_ = _allowLongLong; return *this; }
738  AcceptedTypes& allowDouble( bool _allowDouble )
739  { allowDouble_ = _allowDouble; return *this; }
741  AcceptedTypes& allowString( bool _allowString )
742  { allowString_ = _allowString; return *this; }
744  bool allowInt() const { return allowInt_; }
746  bool allowLongLong() const { return allowLongLong_; }
748  bool allowDouble() const { return allowDouble_; }
750  bool allowString() const { return allowString_; }
751  private:
752  bool allowInt_;
756  };
757 
759 
762 
767 
779  EPreferredType const preferredType,
780  AcceptedTypes const& acceptedTypes
781  );
782 
784 
787 
793  int getInt(
794  const ParameterEntry &entry, const std::string &paramName = "",
795  const std::string &sublistName = "", const bool activeQuery = true
796  ) const;
797 
803  long long getLongLong(
804  const ParameterEntry &entry, const std::string &paramName = "",
805  const std::string &sublistName = "", const bool activeQuery = true
806  ) const;
807 
811  double getDouble(
812  const ParameterEntry &entry, const std::string &paramName = "",
813  const std::string &sublistName = "", const bool activeQuery = true
814  ) const;
815 
817  std::string getString(
818  const ParameterEntry &entry, const std::string &paramName = "",
819  const std::string &sublistName = "", const bool activeQuery = true
820  ) const;
821 
825  int getInt(
826  ParameterList &paramList, const std::string &paramName,
827  const int defaultValue
828  ) const;
829 
833  long long getLongLong(
834  ParameterList &paramList, const std::string &paramName,
835  const long long defaultValue
836  ) const;
837 
841  double getDouble(
842  ParameterList &paramList, const std::string &paramName,
843  const double defaultValue
844  ) const;
845 
849  std::string getString(
850  ParameterList &paramList, const std::string &paramName,
851  const std::string &defaultValue
852  ) const;
853 
856  bool isIntAllowed() const;
857 
860  bool isLongLongAllowed() const;
861 
864  bool isDoubleAllowed() const;
865 
868  bool isStringAllowed() const;
869 
872  EPreferredType getPreferredType() const;
873 
875  static const std::string& getPrefferedTypeString (EPreferredType enumValue)
876  {
877  switch (enumValue) {
878  case PREFER_INT:
879  return getIntEnumString ();
880  case PREFER_LONG_LONG:
881  return getLongLongEnumString ();
882  case PREFER_DOUBLE:
883  return getDoubleEnumString ();
884  case PREFER_STRING:
885  return getStringEnumString ();
886  default:
887  const std::string typeString (toString (enumValue));
888  throw std::runtime_error("Cannot convert enumValue: " + typeString + " to a string");
889  }
890  }
891 
893  static EPreferredType getPrefferedTypeStringEnum (const std::string& enumString)
894  {
895  if (enumString == getIntEnumString ()) {
896  return PREFER_INT;
897  }
898  else if (enumString == getLongLongEnumString ()) {
899  return PREFER_LONG_LONG;
900  }
901  else if (enumString == getDoubleEnumString ()) {
902  return PREFER_DOUBLE;
903  }
904  else if (enumString == getStringEnumString ()) {
905  return PREFER_STRING;
906  }
907  else {
908  throw std::runtime_error ("Cannot convert enumString: " + enumString + " to an enum");
909  }
910  }
911 
913 
916 
918  const std::string getXMLTypeName() const;
919 
921  void printDoc(
922  std::string const& docString,
923  std::ostream & out
924  ) const;
925 
927  ValidStringsList
928  validStringValues() const;
929 
931  void validate(
932  ParameterEntry const& entry,
933  std::string const& paramName,
934  std::string const& sublistName
935  ) const;
936 
938  void validateAndModify(
939  std::string const& paramName,
940  std::string const& sublistName,
941  ParameterEntry * entry
942  ) const;
943 
944 
946 
947 private:
948 
949  // ////////////////////////////
950  // Private data members
951 
953  std::string acceptedTypesString_;
954 
955 //use pragmas to disable some false-positive warnings for windows sharedlibs export
956 #ifdef _MSC_VER
957 #pragma warning(push)
958 #pragma warning(disable:4251)
959 #endif
961 #ifdef _MSC_VER
962 #pragma warning(pop)
963 #endif
964 
965  // ////////////////////////////
966  // Private member functions
967 
968  /* \brief Gets the string representing the "int" preferred type enum */
969  static const std::string& getIntEnumString(){
970  static const std::string intEnumString_ = TypeNameTraits<int>::name();
971  return intEnumString_;
972  }
973 
974  /* \brief Gets the string representing the "int" preferred type enum */
975  static const std::string& getLongLongEnumString(){
976  static const std::string longLongEnumString_ = TypeNameTraits<long long>::name();
977  return longLongEnumString_;
978  }
979 
980  /* \brief Gets the string representing the "double" preferred type enum */
981  static const std::string& getDoubleEnumString(){
982  static const std::string doubleEnumString_ = TypeNameTraits<double>::name();
983  return doubleEnumString_;
984  }
985 
986  /* \brief Gets the string representing the "string" preferred type enum */
987  static const std::string& getStringEnumString(){
988  static const std::string stringEnumString_ = TypeNameTraits<std::string>::name();
989  return stringEnumString_;
990  }
991 
992 
993  void finishInitialization();
994 
995  void throwTypeError(
996  ParameterEntry const& entry,
997  std::string const& paramName,
998  std::string const& sublistName
999  ) const;
1000 
1001 };
1002 
1003 
1004 // Nonmember helper functions
1005 
1006 
1011 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT RCP<AnyNumberParameterEntryValidator>
1012 anyNumberParameterEntryValidator();
1013 
1014 
1019 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT RCP<AnyNumberParameterEntryValidator>
1020 anyNumberParameterEntryValidator(
1022  AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
1023  );
1024 
1030 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setIntParameter(
1031  std::string const& paramName,
1032  int const value, std::string const& docString,
1033  ParameterList *paramList,
1034  AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
1035  = AnyNumberParameterEntryValidator::AcceptedTypes()
1036  );
1037 
1038 
1044 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setLongLongParameter(
1045  std::string const& paramName,
1046  long long const value, std::string const& docString,
1047  ParameterList *paramList,
1048  AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
1049  = AnyNumberParameterEntryValidator::AcceptedTypes()
1050  );
1051 
1057 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setDoubleParameter(
1058  std::string const& paramName,
1059  double const& value, std::string const& docString,
1060  ParameterList *paramList,
1061  AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
1062  = AnyNumberParameterEntryValidator::AcceptedTypes()
1063  );
1064 
1065 
1071 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setNumericStringParameter(
1072  std::string const& paramName,
1073  std::string const& value, std::string const& docString,
1074  ParameterList *paramList,
1075  AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
1076  = AnyNumberParameterEntryValidator::AcceptedTypes()
1077  );
1078 
1079 
1094 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT int getIntParameter(
1095  ParameterList const& paramList, std::string const& paramName
1096  );
1097 
1098 
1113 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT long long getLongLongParameter(
1114  ParameterList const& paramList, std::string const& paramName
1115  );
1116 
1131 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT double getDoubleParameter(
1132  ParameterList const& paramList,
1133  std::string const& paramName
1134  );
1135 
1136 
1152 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT std::string getNumericStringParameter(
1153  ParameterList const& paramList,
1154  std::string const& paramName
1155  );
1156 
1162 template<>
1164 
1165 public:
1166 
1169 
1173  static RCP<AnyNumberParameterEntryValidator > getDummyObject();
1174 
1176 
1177 };
1178 
1179 
1184 template <class T>
1187  static inline T notDefined() {
1188  return T::this_type_is_missing_a_specialization();
1189  }
1190 };
1191 
1192 
1213 template <class T>
1215 public:
1216 
1218  static inline T min()
1220 
1222  static inline T max()
1224 
1227  static inline T defaultStep()
1229 
1232  static inline unsigned short defaultPrecision()
1234 
1235 };
1236 
1237 
1238 template<>
1239 class EnhancedNumberTraits<short int>{
1240 public:
1241  static inline short int min() { return std::numeric_limits<short int>::min(); }
1242  static inline short int max() { return std::numeric_limits<short int>::max(); }
1243  static inline short int defaultStep() { return 1; }
1244  static inline unsigned short defaultPrecision() { return 0; }
1245 };
1246 
1247 
1248 template<>
1249 class EnhancedNumberTraits<short unsigned int>{
1250 public:
1251  static inline short unsigned int min() { return std::numeric_limits<short unsigned int>::min(); }
1252  static inline short unsigned int max() { return std::numeric_limits<short unsigned int>::max(); }
1253  static inline short unsigned int defaultStep() { return 1; }
1254  static inline unsigned short defaultPrecision() { return 0; }
1255 };
1256 
1257 
1258 template<>
1260 public:
1261  static inline int min() { return std::numeric_limits<int>::min(); }
1262  static inline int max() { return std::numeric_limits<int>::max(); }
1263  static inline int defaultStep() { return 1; }
1264  static inline unsigned short defaultPrecision() { return 0; }
1265 };
1266 
1267 
1268 template<>
1269 class EnhancedNumberTraits<unsigned int>{
1270 public:
1271  static inline unsigned int min() { return std::numeric_limits<unsigned int>::min(); }
1272  static inline unsigned int max() { return std::numeric_limits<unsigned int>::max(); }
1273  static inline unsigned int defaultStep() { return 1; }
1274  static inline unsigned short defaultPrecision() { return 0; }
1275 };
1276 
1277 
1278 template<>
1279 class EnhancedNumberTraits<long int>{
1280 public:
1281  static inline long int min() { return std::numeric_limits<long int>::min(); }
1282  static inline long int max() { return std::numeric_limits<long int>::max(); }
1283  static inline long int defaultStep() { return 1; }
1284  static inline unsigned short defaultPrecision() { return 0; }
1285 };
1286 
1287 
1288 template<>
1289 class EnhancedNumberTraits<long unsigned int>{
1290 public:
1291  static inline long unsigned int min() { return std::numeric_limits<long unsigned int>::min(); }
1292  static inline long unsigned int max() { return std::numeric_limits<long unsigned int>::max(); }
1293  static inline long unsigned int defaultStep() { return 1; }
1294  static inline unsigned short defaultPrecision() { return 0; }
1295 };
1296 
1297 
1298 template<>
1299 class EnhancedNumberTraits<long long int>{
1300 public:
1301  static inline long long int min() { return std::numeric_limits<long long int>::min(); }
1302  static inline long long int max() { return std::numeric_limits<long long int>::max(); }
1303  static inline long long int defaultStep() { return 1; }
1304  static inline unsigned short defaultPrecision() { return 0; }
1305 };
1306 
1307 
1308 template<>
1309 class EnhancedNumberTraits<long long unsigned int>{
1310 public:
1311  static inline long long unsigned int min() { return std::numeric_limits<long long unsigned int>::min(); }
1312  static inline long long unsigned int max() { return std::numeric_limits<long long unsigned int>::max(); }
1313  static inline long long unsigned int defaultStep() { return 1; }
1314  static inline unsigned short defaultPrecision() { return 0; }
1315 };
1316 
1317 
1318 #ifdef HAVE_TEUCHOSCORE_QUADMATH
1319 template<>
1320 class EnhancedNumberTraits<__float128>{
1321 public:
1322  static inline __float128 min() { return -std::numeric_limits<__float128>::max(); }
1323  static inline __float128 max() { return std::numeric_limits<__float128>::max(); }
1324  static inline __float128 defaultStep() { return 1; }
1325  static inline unsigned short defaultPrecision() { return 100; }
1326 };
1327 #endif // HAVE_TEUCHOSCORE_QUADMATH
1328 
1329 template<>
1330 class EnhancedNumberTraits<double>{
1331 public:
1332  static inline double min() { return -std::numeric_limits<double>::max(); }
1333  static inline double max() { return std::numeric_limits<double>::max(); }
1334  static inline double defaultStep() { return 1; }
1335  static inline unsigned short defaultPrecision() { return 100; }
1336 };
1337 
1338 template<>
1340 public:
1341  static inline float min() { return -std::numeric_limits<float>::max(); }
1342  static inline float max() { return std::numeric_limits<float>::max(); }
1343  static inline float defaultStep() { return 1; }
1344  static inline unsigned short defaultPrecision() { return 100; }
1345 };
1346 
1352 template <class T>
1354 
1355 public:
1356 
1359 
1375  T min,
1376  T max,
1378  unsigned short precision=EnhancedNumberTraits<T>::defaultPrecision()):
1380  minVal(min), maxVal(max), step_(step), precision_(precision),
1381  containsMin(true), containsMax(true){}
1382 
1388  minVal(EnhancedNumberTraits<T>::min()),
1389  maxVal(EnhancedNumberTraits<T>::max()),
1390  step_(EnhancedNumberTraits<T>::defaultStep()),
1391  precision_(EnhancedNumberTraits<T>::defaultPrecision()),
1392  containsMin(false),
1393  containsMax(false){}
1394 
1396 
1398 
1399 
1404  void setMin(T min){
1405  minVal = min;
1406  containsMin = true;
1407  }
1408 
1413  void setMax(T max){
1414  maxVal = max;
1415  containsMax = true;
1416  }
1417 
1422  void setStep(T step){
1423  step_ = step;
1424  }
1425 
1430  void setPrecision(unsigned short precision){
1431  precision_ = precision;
1432  }
1433 
1435 
1438 
1443  T getMin() const{
1444  return minVal;
1445  }
1446 
1451  T getMax() const{
1452  return maxVal;
1453  }
1454 
1459  T getStep() const{
1460  return step_;
1461  }
1462 
1467  unsigned short getPrecision() const{
1468  return precision_;
1469  }
1470 
1472 
1474 
1475 
1480  bool hasMin() const{
1481  return containsMin;
1482  }
1483 
1488  bool hasMax() const{
1489  return containsMax;
1490  }
1491 
1493 
1496 
1499  return null;
1500  }
1501 
1503  void validate(ParameterEntry const &entry, std::string const &paramName,
1504  std::string const &sublistName) const;
1505 
1507  void validateAndModify( std::string const& paramName,
1508  std::string const& sublistName, ParameterEntry * entry) const;
1509 
1512  const bool activeQuery) const;
1513 
1515  const std::string getXMLTypeName() const{
1516  return "EnhancedNumberValidator(" + TypeNameTraits<T>::name()+ ")";
1517  }
1518 
1520  void printDoc(std::string const &docString, std::ostream &out) const{
1521  StrUtils::printLines(out,"# ",docString);
1522  out << "#\tValidator Used: " << std::endl;
1523  out << "#\t\tNumber Validator" << std::endl;
1524  out << "#\t\tType: " << Teuchos::TypeNameTraits<T>::name() <<
1525  std::endl;
1526  out << "#\t\tMin (inclusive): " << minVal << std::endl;
1527  out << "#\t\tMax (inclusive): " << maxVal << std::endl;
1528  }
1529 
1531 
1532 private:
1535 
1536  // note this was discussed in issue #612
1537  // currently we are keeping a string validator with EnhancedNumberValidator
1538  // an alternative is to make a combined class for AnyNumberParameterEntryValidator
1539  // and EnhancedNumberValidator
1540  bool useIntConversions() const;
1541 
1543 
1546 
1550 
1554 
1558 
1562  unsigned short precision_;
1563 
1567 
1571 
1573 
1574 };
1575 
1576 template<class T>
1578  std::string const& paramName,
1579  std::string const& sublistName,
1580  ParameterEntry * entry
1581  ) const
1582 {
1583  TEUCHOS_TEST_FOR_EXCEPT(0==entry);
1584 
1585  any anyValue = entry->getAny(true);
1586  // preferred type is not string
1587  if( anyValue.type() == typeid(std::string) ) {
1588  anyValue = getNumberFromString(*entry,false);
1589  entry->setValue(
1590  any_cast<T>(anyValue),
1591  false // isDefault
1592  );
1593  }
1594  else {
1595  // default behavior
1597  paramName, sublistName, entry);
1598  }
1599 }
1600 
1601 template<class T>
1603 {
1604  // this will need some rethinking and exists only for supporting
1605  // conversion of strings to the templated type T
1606  // but we may want to unify this into the base class anyways
1607  // and share string conversion concepts with other parameters
1608  // like AnyNumberParameterEntryValidator
1609  if(typeid(T) == typeid(char)) return true;
1610  if(typeid(T) == typeid(unsigned char)) return true;
1611  if(typeid(T) == typeid(int)) return true;
1612  if(typeid(T) == typeid(unsigned int)) return true;
1613  if(typeid(T) == typeid(short)) return true;
1614  if(typeid(T) == typeid(unsigned short)) return true;
1615  if(typeid(T) == typeid(long)) return true;
1616  if(typeid(T) == typeid(unsigned long)) return true;
1617  if(typeid(T) == typeid(long long)) return true;
1618  if(typeid(T) == typeid(unsigned long long)) return true;
1619 
1620  // default to double stod to older atof conversion
1621  // depending on HAVE_TEUCHOSCORE_CXX11
1622  // those conversions would probably handle all above discrete types anyways
1623  return false;
1624 }
1625 
1626 template<class T>
1628  const ParameterEntry &entry, const bool activeQuery
1629  ) const
1630 {
1631  // perhaps we want to just eliminate the int checks
1632  // and always use double conversion which I think would work
1633  // well for all types - but this will give us a behavior which mirrors
1634  // AnyNumberParameterEntryValidator more closely
1635  const any &anyValue = entry.getAny(activeQuery);
1636  if(useIntConversions()) {
1637  return any((T)convertStringToInt(any_cast<std::string>(anyValue)));
1638  }
1639  else { // if not discrete, read as a double and cast to our type T
1640  return any((T)convertStringToDouble(any_cast<std::string>(anyValue)));
1641  }
1642 }
1643 
1644 template<class T>
1645 void EnhancedNumberValidator<T>::validate(ParameterEntry const &entry, std::string const &paramName,
1646  std::string const &sublistName) const
1647 {
1648  any anyValue = entry.getAny(true);
1649 
1650  // This was new code added to allow EnhancedNumberValidator to accept a string
1651  // This was added for consistency with AnyNumberParameterEntryValidator
1652  // and the new BoolParameterEntryValidator which all take string
1653  // We may wish to change this to be optional like AnyNumberParameterEntryValidator
1654  if( anyValue.type() == typeid(std::string) ) {
1655  // try to upgrade from a string to a number
1656  anyValue = getNumberFromString(entry, false);
1657  }
1658 
1659  const std::string &entryName = entry.getAny(false).typeName();
1660  TEUCHOS_TEST_FOR_EXCEPTION(anyValue.type() != typeid(T),
1662  "The \"" << paramName << "\"" <<
1663  " parameter in the \"" << sublistName <<
1664  "\" sublist is has an error." << std::endl << std::endl <<
1665  "Error: The value that you entered was the wrong type." << std::endl <<
1666  "Parameter: " << paramName << std::endl <<
1667  "Type specified: " << entryName << std::endl <<
1668  "Type accepted: " << Teuchos::TypeNameTraits<T>::name() << std::endl);
1669 
1670  bool isValueInRange;
1671  any_cast<T>(anyValue) >= minVal && any_cast<T>(anyValue) <= maxVal
1672  ? isValueInRange = true : isValueInRange=false;
1673  TEUCHOS_TEST_FOR_EXCEPTION(!(isValueInRange),
1675  "The \"" << paramName << "\"" <<
1676  " parameter in the \"" << sublistName <<
1677  "\" sublist is has an error." << std::endl << std::endl <<
1678  "Error: The value that was entered doesn't fall with in " <<
1679  "the range set by the validator" << std::endl <<
1680  "Parameter: " << paramName << std::endl <<
1681  "Min: " << minVal << std::endl <<
1682  "Max: " << maxVal << std::endl <<
1683  "Value entered: " <<
1684  (any_cast<T>(anyValue)) << std::endl << std::endl);
1685 }
1686 
1692 template<class T>
1694 
1695 public:
1696 
1699 
1704 
1706 };
1707 
1708 template<class T>
1711 {
1712  return rcp(new EnhancedNumberValidator<T>);
1713 }
1714 
1724 
1725 public:
1726 
1729 
1732  static bool mustAlreadyExistDefault() { return false; }
1733 
1735 
1738 
1744  FileNameValidator(bool mustAlreadyExist = mustAlreadyExistDefault());
1745 
1747 
1749 
1750 
1756  bool fileMustExist() const;
1757 
1759 
1760 
1771  bool fileEmptyNameOK() const;
1772 
1774 
1776 
1777 
1786  bool setFileMustExist(bool shouldFileExist);
1787 
1796  bool setFileEmptyNameOK(bool isEmptyNameOK);
1797 
1799 
1802 
1804  ValidStringsList validStringValues() const;
1805 
1807  void validate(
1808  ParameterEntry const &entry,
1809  std::string const &paramName,
1810  std::string const &sublistName) const;
1811 
1813  const std::string getXMLTypeName() const;
1814 
1816  void printDoc(std::string const &docString, std::ostream &out) const;
1817 
1819 
1820 private:
1821 
1824 
1830 
1832 
1833 };
1834 
1840 template<>
1842 
1843 public:
1844 
1847 
1851  static RCP<FileNameValidator> getDummyObject();
1852 
1854 
1855 };
1856 
1865 
1866 public:
1867 
1870 
1873  StringValidator();
1874 
1879  StringValidator(const Teuchos::Array<std::string> &validStrings);
1880 
1882 
1884 
1885 
1893  ValidStringsList setValidStrings(
1894  const Teuchos::Array<std::string> &validStrings);
1895 
1897 
1900 
1902  ValidStringsList validStringValues() const;
1903 
1905  void validate(ParameterEntry const &entry, std::string const &paramName,
1906  std::string const &sublistName) const;
1907 
1909  const std::string getXMLTypeName() const;
1910 
1912  void printDoc(std::string const &docString, std::ostream &out) const;
1913 
1915 
1916 private:
1917 
1920 
1923  ValidStringsList validStrings_;
1924 
1926 
1927 };
1928 
1934 template<>
1936 
1937 public:
1938 
1941 
1945  static RCP<StringValidator> getDummyObject();
1946 
1948 
1949 };
1950 
1951 
1955 template<class ValidatorType, class EntryType>
1957 
1958 public:
1959 
1962 
1971  prototypeValidator_(prototypeValidator){}
1972 
1974 
1977 
1980  return prototypeValidator_;
1981  }
1982 
1984 
1987 
1990  return prototypeValidator_->validStringValues();
1991  }
1992 
1994 
1995 private:
1996 
1999 
2003 
2006 
2008 
2009 };
2010 
2023 template<class ValidatorType, class EntryType>
2024 class TwoDArrayValidator : public AbstractArrayValidator<ValidatorType, EntryType>{
2025 public:
2028 
2036  AbstractArrayValidator<ValidatorType, EntryType>(prototypeValidator){}
2037 
2039 
2042 
2044  virtual void validate(ParameterEntry const &entry, std::string const &paramName,
2045  std::string const &sublistName) const;
2046 
2048  const std::string getXMLTypeName() const{
2049  return "TwoDArrayValidator(" +
2050  this->getPrototype()->getXMLTypeName() + ", " +
2052  }
2053 
2055  virtual void printDoc(std::string const &docString, std::ostream &out) const
2056  {
2057  StrUtils::printLines(out,"# ",docString);
2058  std::string toPrint;
2059  toPrint += "TwoDArrayValidator:\n";
2060  toPrint += "Prototype Validator:\n";
2061  this->getPrototype()->printDoc(toPrint, out);
2062  }
2063 
2065 
2066 };
2067 
2068 template<class ValidatorType, class EntryType>
2069 void TwoDArrayValidator<ValidatorType, EntryType>::validate(ParameterEntry const &entry, std::string const &paramName,
2070  std::string const &sublistName) const
2071 {
2072  any anyValue = entry.getAny(true);
2073  const std::string &entryName = entry.getAny(false).typeName();
2076  "The \"" << paramName << "\"" <<
2077  " parameter in the \"" << sublistName <<
2078  "\" sublist is has an error." << std::endl << std::endl <<
2079  "Error: The value you entered was the wrong type." << std::endl <<
2080  "Parameter: " << paramName << std::endl <<
2081  "Type specified: " << entryName << std::endl <<
2082  "Type accepted: " << TypeNameTraits<TwoDArray<EntryType> >::name() <<
2083  std::endl << std::endl);
2084 
2085  TwoDArray<EntryType> extracted =
2086  getValue<Teuchos::TwoDArray<EntryType> >(entry);
2087  RCP<const ParameterEntryValidator> prototype = this->getPrototype();
2088  for(int i = 0; i<extracted.getNumRows(); ++i){
2089  for(int j = 0; j<extracted.getNumCols(); ++j){
2090  ParameterEntry dummyParameter;
2091  dummyParameter.setValue(extracted(i,j));
2092  try{
2093  prototype->validate(
2094  dummyParameter, paramName, sublistName);
2095  }
2097  std::stringstream oss;
2098  oss << "TwoDArray Validator Exception:" << std::endl <<
2099  "Bad Index: (" << i << "," << j << ")" << std::endl << e.what();
2100  throw Exceptions::InvalidParameterValue(oss.str());
2101  }
2102  }
2103  }
2104 }
2105 
2106 
2112 template<class ValidatorType, class EntryType>
2113 class DummyObjectGetter<TwoDArrayValidator<ValidatorType, EntryType> >{
2114 
2115 public:
2116 
2119 
2124 
2126 
2127 };
2128 
2129 template<class ValidatorType, class EntryType>
2132 {
2135 }
2136 
2141  public TwoDArrayValidator<StringValidator, std::string>{
2142 
2143 public:
2144 
2147 
2150  TwoDArrayValidator<StringValidator, std::string>(prototypeValidator){}
2151 
2153 
2154 };
2155 
2156 
2162  public TwoDArrayValidator<FileNameValidator, std::string>{
2163 
2164 public:
2165 
2168 
2171  TwoDArrayValidator<FileNameValidator, std::string>(prototypeValidator){}
2172 
2174 
2175 };
2176 
2177 
2181 template<class T>
2182 class TwoDArrayNumberValidator : public TwoDArrayValidator<EnhancedNumberValidator<T>, T>{
2183 public:
2186 
2189  RCP<const EnhancedNumberValidator<T> > prototypeValidator):
2190  TwoDArrayValidator<EnhancedNumberValidator<T>, T>(prototypeValidator){}
2191 
2193 
2194 };
2195 
2196 
2208 template<class ValidatorType, class EntryType>
2209 class ArrayValidator : public AbstractArrayValidator<ValidatorType, EntryType>{
2210 
2211 public:
2212 
2215 
2222  AbstractArrayValidator<ValidatorType, EntryType>(prototypeValidator){}
2223 
2225 
2228 
2230  virtual void validate(ParameterEntry const &entry, std::string const &paramName,
2231  std::string const &sublistName) const;
2232 
2234  const std::string getXMLTypeName() const{
2235  return "ArrayValidator(" +
2236  this->getPrototype()->getXMLTypeName() + ", " +
2238  }
2239 
2241  virtual void printDoc(std::string const &docString, std::ostream &out) const
2242  {
2243  StrUtils::printLines(out,"# ",docString);
2244  std::string toPrint;
2245  toPrint += "ArrayValidator:\n";
2246  toPrint += "Prototype Validator:\n";
2247  this->getPrototype()->printDoc(toPrint, out);
2248  }
2249 
2251 
2252 };
2253 
2254 template<class ValidatorType, class EntryType>
2255 void ArrayValidator<ValidatorType, EntryType>::validate(ParameterEntry const &entry, std::string const &paramName,
2256  std::string const &sublistName) const
2257 {
2258  any anyValue = entry.getAny(true);
2259  const std::string &entryName = entry.getAny(false).typeName();
2260  TEUCHOS_TEST_FOR_EXCEPTION(anyValue.type() != typeid(Array<EntryType>),
2262  "The \"" << paramName << "\"" <<
2263  " parameter in the \"" << sublistName <<
2264  "\" sublist is has an error." << std::endl << std::endl <<
2265  "Error: The value you entered was the wrong type." << std::endl <<
2266  "Parameter: " << paramName << std::endl <<
2267  "Type specified: " << entryName << std::endl <<
2268  "Type accepted: " << TypeNameTraits<Array<EntryType> >::name() <<
2269  std::endl << std::endl);
2270 
2271  Array<EntryType> extracted =
2272  getValue<Teuchos::Array<EntryType> >(entry);
2273  RCP<const ParameterEntryValidator> prototype = this->getPrototype();
2274  for(int i = 0; i<extracted.size(); ++i){
2275  ParameterEntry dummyParameter;
2276  dummyParameter.setValue(extracted[i]);
2277  try{
2278  prototype->validate(
2279  dummyParameter, paramName, sublistName);
2280  }
2282  std::stringstream oss;
2283  oss << "Array Validator Exception:" << std::endl <<
2284  "Bad Index: " << i << std::endl << e.what();
2285  throw Exceptions::InvalidParameterValue(oss.str());
2286  }
2287  }
2288 }
2289 
2295 template<class ValidatorType, class EntryType>
2296 class DummyObjectGetter<ArrayValidator<ValidatorType, EntryType> >{
2297 
2298 public:
2299 
2302 
2307 
2309 
2310 };
2311 
2312 template<class ValidatorType, class EntryType>
2315 {
2318 }
2319 
2320 
2330  public ArrayValidator<StringValidator, std::string>{
2331 
2332 public:
2333 
2336 
2339  ArrayValidator<StringValidator, std::string>(prototypeValidator){}
2340 
2342 
2343 };
2344 
2345 
2354 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT ArrayFileNameValidator : public ArrayValidator<FileNameValidator, std::string>{
2355 
2356 public:
2357 
2360 
2363  ArrayValidator<FileNameValidator, std::string>(prototypeValidator){}
2364 
2366 
2367 };
2368 
2369 
2377 template<class T>
2378 class ArrayNumberValidator : public ArrayValidator<EnhancedNumberValidator<T>, T>{
2379 public:
2382 
2385  RCP<const EnhancedNumberValidator<T> > prototypeValidator):
2386  ArrayValidator<EnhancedNumberValidator<T>, T>(prototypeValidator){}
2387 
2389 
2390 };
2391 
2392 
2393 
2394 // ///////////////////////////
2395 // Implementations
2396 
2397 
2398 //
2399 // StringToIntegralParameterEntryValidator
2400 //
2401 
2402 
2403 // Constructors
2404 
2405 
2406 template<class IntegralType>
2409  std::string const& defaultParameterName,
2410  const bool caseSensitive) :
2412  defaultParameterName_ (defaultParameterName),
2413  caseSensitive_ (caseSensitive)
2414 {
2415  typedef typename map_t::value_type val_t;
2416  for (int i = 0; i < static_cast<int> (strings.size ()); ++i) {
2417  const bool unique = caseSensitive_ ?
2418  map_.insert (val_t (strings[i], static_cast<IntegralType> (i))).second :
2419  map_.insert (val_t (upperCase (strings[i]), static_cast<IntegralType> (i))).second;
2421  ! unique, std::logic_error,
2422  "For parameter \"" << defaultParameterName_ << "\": "
2423  "strings[" << i << "] = \"" << strings[i] << "\" is a duplicate.");
2424  }
2425  setValidValues (strings);
2426 }
2427 
2428 
2429 template<class IntegralType>
2432  ArrayView<const IntegralType> const& integralValues,
2433  std::string const& defaultParameterName,
2434  const bool caseSensitive) :
2436  defaultParameterName_ (defaultParameterName),
2437  caseSensitive_ (caseSensitive)
2438 {
2439 #ifdef TEUCHOS_DEBUG
2440  TEUCHOS_ASSERT_EQUALITY( strings.size(), integralValues.size() );
2441 #endif
2443  strings.size() != integralValues.size(),
2444  std::logic_error,
2445  "The input arrays strings and integralValues must have the same length.");
2446 
2447  typedef typename map_t::value_type val_t;
2448  for (int i = 0; i < static_cast<int> (strings.size ()); ++i) {
2449  const bool unique = caseSensitive_ ?
2450  map_.insert (val_t (strings[i], integralValues[i])).second :
2451  map_.insert (val_t (upperCase (strings[i]), integralValues[i])).second;
2452 
2454  ! unique, std::logic_error,
2455  "For parameter \"" << defaultParameterName_ << "\": "
2456  "strings[" << i << "] = \"" << strings[i] << "\" is a duplicate.");
2457  }
2458  setValidValues (strings);
2459 }
2460 
2461 template<class IntegralType>
2464  ArrayView<const std::string> const& stringsDocs,
2465  ArrayView<const IntegralType> const& integralValues,
2466  std::string const& defaultParameterName,
2467  const bool caseSensitive) :
2469  defaultParameterName_ (defaultParameterName),
2470  caseSensitive_ (caseSensitive)
2471 {
2472 #ifdef TEUCHOS_DEBUG
2473  TEUCHOS_ASSERT_EQUALITY( strings.size(), stringsDocs.size() );
2474  TEUCHOS_ASSERT_EQUALITY( strings.size(), integralValues.size() );
2475 #endif
2476 
2478  strings.size() != integralValues.size(),
2479  std::logic_error,
2480  "The input arrays strings and integralValues must have the same length.");
2481 
2483  strings.size() != stringsDocs.size(),
2484  std::logic_error,
2485  "The input arrays strings and stringsDocs must have the same length.");
2486 
2487  typedef typename map_t::value_type val_t;
2488  for (int i = 0; i < static_cast<int> (strings.size ()); ++i) {
2489  const bool unique = caseSensitive_ ?
2490  map_.insert (val_t (strings[i], integralValues[i])).second :
2491  map_.insert (val_t (upperCase (strings[i]), integralValues[i])).second;
2493  ! unique, std::logic_error,
2494  "For parameter \"" << defaultParameterName_ << "\": "
2495  "strings[" << i << "] = \"" << strings[i] << "\" is a duplicate.");
2496  }
2497  setValidValues(strings,&stringsDocs);
2498 }
2499 
2500 // Lookup functions
2501 
2502 
2503 template<class IntegralType>
2504 IntegralType
2506  const std::string &str, const std::string &paramName
2507  ,const std::string &sublistName
2508  ) const
2509 {
2510  typename map_t::const_iterator itr = map_.find (caseSensitive_ ? str : upperCase (str));
2512  itr == map_.end(), Exceptions::InvalidParameterValue
2513  ,"Error, the value \"" << str << "\" is not recognized for the parameter \""
2514  << ( paramName.length() ? paramName : defaultParameterName_ ) << "\""
2515  << "\nin the sublist \"" << sublistName << "\"."
2516  << "\n\nValid values include:"
2517  << "\n {\n"
2518  << validValues_
2519  << " }"
2520  );
2521  return (*itr).second;
2522 }
2523 
2524 
2525 template<class IntegralType>
2526 IntegralType
2528  const ParameterEntry &entry, const std::string &paramName
2529  ,const std::string &sublistName, const bool activeQuery
2530  ) const
2531 {
2532  const bool validType = ( entry.getAny(activeQuery).type() == typeid(std::string) );
2535  ,"Error, the parameter {paramName=\""<<(paramName.length()?paramName:defaultParameterName_)
2536  << "\",type=\""<<entry.getAny(activeQuery).typeName()<<"\"}"
2537  << "\nin the sublist \"" << sublistName << "\""
2538  << "\nhas the wrong type."
2539  << "\n\nThe correct type is \"string\"!"
2540  );
2541  const std::string
2542  &strValue = any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail!
2543  return getIntegralValue(strValue,paramName,sublistName); // This will validate the value and throw!
2544 }
2545 
2546 
2547 template<class IntegralType>
2548 std::string
2550  const ParameterEntry &entry, const std::string &paramName
2551  ,const std::string &sublistName, const bool activeQuery
2552  ) const
2553 {
2554  // Validate the parameter's type and value
2555  this->getIntegralValue(entry,paramName,sublistName,activeQuery);
2556  // Return the std::string value which is now validated!
2557  return any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail!
2558 }
2559 
2560 
2561 template<class IntegralType>
2562 IntegralType
2564  ParameterList &paramList, const std::string &paramName
2565  ,const std::string &defaultValue
2566  ) const
2567 {
2568  const std::string& strValue =
2569  paramList.get (paramName,
2570  caseSensitive_ ? defaultValue : upperCase (defaultValue));
2571  return getIntegralValue (strValue, paramName, paramList.name ());
2572 }
2573 
2574 
2575 template<class IntegralType>
2576 std::string
2578  ParameterList &paramList, const std::string &paramName
2579  ,const std::string &defaultValue
2580  ) const
2581 {
2582  const std::string& strValue =
2583  paramList.get (paramName,
2584  caseSensitive_ ? defaultValue : upperCase (defaultValue));
2585  getIntegralValue(strValue,paramName,paramList.name()); // Validate!
2586  return strValue;
2587 }
2588 
2589 template<class IntegralType>
2592 {
2593  return validStringValuesDocs_;
2594 }
2595 
2596 template<class IntegralType>
2597 const std::string&
2599 {
2600  return defaultParameterName_;
2601 }
2602 
2603 template<class IntegralType>
2604 std::string
2606  const std::string &str, const std::string &paramName
2607  ,const std::string &sublistName
2608  ) const
2609 {
2610  getIntegralValue (caseSensitive_ ? str : upperCase (str),
2611  paramName,
2612  sublistName); // Validate!
2613  return str;
2614 }
2615 
2616 
2617 // Overridden from ParameterEntryValidator
2618 
2619 template<class IntegralType>
2620 const std::string
2622  return "StringIntegralValidator(" + TypeNameTraits<IntegralType>::name () + ")";
2623 }
2624 
2625 template<class IntegralType>
2627  std::string const& docString
2628  ,std::ostream & out
2629  ) const
2630 {
2631  StrUtils::printLines(out,"# ",docString);
2632  out << "# Valid std::string values:\n";
2633  out << "# {\n";
2634  if(validStringValuesDocs_.get()) {
2635  for( int i = 0; i < static_cast<int>(validStringValues_->size()); ++i ) {
2636  out << "# \"" << (*validStringValues_)[i] << "\"\n";
2637  StrUtils::printLines(out,"# ",(*validStringValuesDocs_)[i] );
2638  }
2639  }
2640  else {
2641  StrUtils::printLines(out,"# ",validValues_);
2642  // Note: Above validValues_ has for initial spaces already so indent should
2643  // be correct!
2644  }
2645  out << "# }\n";
2646 }
2647 
2648 
2649 template<class IntegralType>
2652 {
2653  return validStringValues_;
2654 }
2655 
2656 
2657 template<class IntegralType>
2659  ParameterEntry const& entry
2660  ,std::string const& paramName
2661  ,std::string const& sublistName
2662  ) const
2663 {
2664  this->getIntegralValue (entry, paramName, sublistName, false);
2665 }
2666 
2667 
2668 // private
2669 
2670 template<class IntegralType>
2672  ArrayView<const std::string> const& strings
2673  ,ArrayView<const std::string> const* stringsDocs
2674  )
2675 {
2676  if (caseSensitive_) {
2677  validStringValues_ = rcp (new Array<std::string> (strings));
2678  }
2679  else {
2680  RCP<Array<std::string> > vals (new Array<std::string> (strings.size ()));
2681  for (Array<std::string>::size_type i = 0; i < strings.size (); ++i) {
2682  (*vals)[i] = upperCase (strings[i]);
2683  }
2684  validStringValues_ = rcp_const_cast<const Array<std::string> > (vals);
2685  }
2686 
2687  if (stringsDocs) {
2688  validStringValuesDocs_ = rcp (new Array<std::string> (*stringsDocs));
2689  }
2690  // Build the list of valid values in the same order as passed in by the client.
2691  std::ostringstream oss;
2692  for (int i = 0; i < static_cast<int> (strings.size()); ++i) {
2693  oss << " \"" << strings[i] << "\"\n";
2694  }
2695  // Note: Above four spaces is designed for the error output above.
2696  validValues_ = oss.str();
2697 }
2698 
2699 
2700 } // namespace Teuchos
2701 
2702 
2703 //
2704 // Nonmember function implementations for StringToIntegralParameterEntryValidator
2705 //
2706 
2707 
2708 template<class IntegralType>
2709 inline
2711 Teuchos::stringToIntegralParameterEntryValidator(
2712  ArrayView<const std::string> const& strings,
2713  std::string const& defaultParameterName
2714  )
2715 {
2716  return rcp(
2717  new StringToIntegralParameterEntryValidator<IntegralType>(
2718  strings, defaultParameterName
2719  )
2720  );
2721 }
2722 
2723 
2724 template<class IntegralType>
2725 inline
2727 Teuchos::stringToIntegralParameterEntryValidator(
2728  ArrayView<const std::string> const& strings,
2729  std::string const& defaultParameterName,
2730  const bool caseSensitive
2731  )
2732 {
2733  typedef StringToIntegralParameterEntryValidator<IntegralType> ret_type;
2734  return rcp (new ret_type (strings, defaultParameterName, caseSensitive));
2735 }
2736 
2737 
2738 
2739 template<class IntegralType>
2740 inline
2742 Teuchos::stringToIntegralParameterEntryValidator(
2743  ArrayView<const std::string> const& strings,
2744  ArrayView<const IntegralType> const& integralValues,
2745  std::string const& defaultParameterName
2746  )
2747 {
2748  return rcp(
2749  new StringToIntegralParameterEntryValidator<IntegralType>(
2750  strings, integralValues, defaultParameterName
2751  )
2752  );
2753 }
2754 
2755 
2756 template<class IntegralType>
2757 inline
2759 Teuchos::stringToIntegralParameterEntryValidator(
2760  ArrayView<const std::string> const& strings,
2761  ArrayView<const IntegralType> const& integralValues,
2762  std::string const& defaultParameterName,
2763  const bool caseSensitive)
2764 {
2765  typedef StringToIntegralParameterEntryValidator<IntegralType> ret_type;
2766  return rcp (new ret_type (strings, integralValues,
2767  defaultParameterName, caseSensitive));
2768 }
2769 
2770 
2771 template<class IntegralType>
2772 inline
2774 Teuchos::stringToIntegralParameterEntryValidator(
2775  ArrayView<const std::string> const& strings,
2776  ArrayView<const std::string> const& stringsDocs,
2777  ArrayView<const IntegralType> const& integralValues,
2778  std::string const& defaultParameterName
2779  )
2780 {
2781  return rcp(
2782  new StringToIntegralParameterEntryValidator<IntegralType>(
2783  strings, stringsDocs, integralValues, defaultParameterName
2784  )
2785  );
2786 }
2787 
2788 
2789 template<class IntegralType>
2790 inline
2792 Teuchos::stringToIntegralParameterEntryValidator(
2793  ArrayView<const std::string> const& strings,
2794  ArrayView<const std::string> const& stringsDocs,
2795  ArrayView<const IntegralType> const& integralValues,
2796  std::string const& defaultParameterName,
2797  const bool caseSensitive)
2798 {
2799  typedef StringToIntegralParameterEntryValidator<IntegralType> ret_type;
2800  return rcp (new ret_type (strings, stringsDocs, integralValues,
2801  defaultParameterName, caseSensitive));
2802 }
2803 
2804 
2805 template<class IntegralType>
2806 void Teuchos::setStringToIntegralParameter(
2807  std::string const& paramName,
2808  std::string const& defaultValue,
2809  std::string const& docString,
2810  ArrayView<const std::string> const& strings,
2811  ParameterList * paramList
2812  )
2813 {
2814  typedef ParameterEntryValidator PEV;
2815  TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
2816  paramList->set(
2817  paramName, defaultValue, docString,
2818  rcp_implicit_cast<const PEV>(
2819  stringToIntegralParameterEntryValidator<IntegralType>(
2820  strings, paramName
2821  )
2822  )
2823  );
2824 }
2825 
2826 
2827 template<class IntegralType>
2828 void Teuchos::setStringToIntegralParameter(
2829  std::string const& paramName,
2830  std::string const& defaultValue,
2831  std::string const& docString,
2832  ArrayView<const std::string> const& strings,
2833  ArrayView<const IntegralType> const& integralValues,
2834  ParameterList * paramList
2835  )
2836 {
2837  typedef ParameterEntryValidator PEV;
2838  TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
2839  paramList->set(
2840  paramName, defaultValue, docString,
2841  rcp_implicit_cast<const PEV>(
2842  stringToIntegralParameterEntryValidator<IntegralType>(
2843  strings, integralValues, paramName
2844  )
2845  )
2846  );
2847 }
2848 
2849 
2850 template<class IntegralType>
2851 void Teuchos::setStringToIntegralParameter(
2852  std::string const& paramName,
2853  std::string const& defaultValue,
2854  std::string const& docString,
2855  ArrayView<const std::string> const& strings,
2856  ArrayView<const std::string> const& stringsDocs,
2857  ArrayView<const IntegralType> const& integralValues,
2858  ParameterList * paramList
2859  )
2860 
2861 {
2862  typedef ParameterEntryValidator PEV;
2863  TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
2864  paramList->set(
2865  paramName, defaultValue, docString,
2866  rcp_implicit_cast<const PEV>(
2867  stringToIntegralParameterEntryValidator<IntegralType>(
2868  strings, stringsDocs, integralValues, paramName
2869  )
2870  )
2871  );
2872 }
2873 
2874 
2875 template<class IntegralType>
2876 IntegralType Teuchos::getIntegralValue(
2877  ParameterList const& paramList, std::string const& paramName
2878  )
2879 {
2880  const ParameterEntry &entry = paramList.getEntry(paramName);
2881  RCP<const StringToIntegralParameterEntryValidator<IntegralType> >
2882  integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>(
2883  entry, paramList, paramName
2884  );
2885  return integralValidator->getIntegralValue(
2886  entry, paramName, paramList.name(), true );
2887 }
2888 
2889 
2890 template<class IntegralType>
2891 std::string Teuchos::getStringValue(
2892  ParameterList const& paramList, std::string const& paramName
2893  )
2894 {
2895  const ParameterEntry &entry = paramList.getEntry(paramName);
2896  RCP<const StringToIntegralParameterEntryValidator<IntegralType> >
2897  integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>(
2898  entry, paramList, paramName
2899  );
2900  return integralValidator->getStringValue(
2901  entry, paramName, paramList.name(), true
2902  );
2903 }
2904 
2905 
2906 template<class IntegralType>
2909  ParameterEntry const& entry, ParameterList const& paramList,
2910  std::string const& paramName
2911  )
2912 {
2913  const RCP<const ParameterEntryValidator> validator = entry.validator();
2915  is_null(validator), Exceptions::InvalidParameterType,
2916  "Error! The parameter \""<<paramName<<"\" exists\n"
2917  "in the parameter (sub)list \""<<paramList.name()<<"\"\n"
2918  "but it does not contain any validator needed to extract\n"
2919  "an integral value of type \""<<TypeNameTraits<IntegralType>::name()<<"\"!"
2920  );
2921  const RCP<const StringToIntegralParameterEntryValidator<IntegralType> > integralValidator =
2922  rcp_dynamic_cast<const StringToIntegralParameterEntryValidator<IntegralType> >(
2923  validator
2924  );
2926  is_null(integralValidator), Exceptions::InvalidParameterType,
2927  "Error! The parameter \""<<paramName<<"\" exists\n"
2928  "in the parameter (sub)list \""<<paramList.name()<<"\"\n"
2929  "but it contains the wrong type of validator. The expected validator type\n"
2930  "is \""<<TypeNameTraits<StringToIntegralParameterEntryValidator<IntegralType> >::name()<<"\"\n"
2931  "but the contained validator type is \""<<typeName(*validator)<<"\"!"
2932  );
2933  return integralValidator;
2934 }
2935 
2936 
2937 #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 hasMax() const
Determines whether or not the validator has a maximum value.
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.
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)