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>
358  ArrayView<const std::string> const& strings,
359  std::string const& defaultParameterName
360  );
361 
362 
367 template<class IntegralType>
370  ArrayView<const std::string> const& strings,
371  std::string const& defaultParameterName,
372  const bool caseSensitive
373  );
374 
375 
380 template<class IntegralType>
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>
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>
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>
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>
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 
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 
591 
592 
606 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT AnyNumberParameterEntryValidator
607  : public ParameterEntryValidator
608 {
609 public:
610 
613 
615  enum EPreferredType { PREFER_INT, PREFER_DOUBLE, PREFER_STRING };
616 
617 
620  public:
622  AcceptedTypes( bool allowAllTypesByDefault = true )
623  :allowInt_(allowAllTypesByDefault),allowDouble_(allowAllTypesByDefault),
624  allowString_(allowAllTypesByDefault)
625  {}
627  AcceptedTypes& allowInt( bool _allowInt )
628  { allowInt_ = _allowInt; return *this; }
630  AcceptedTypes& allowDouble( bool _allowDouble )
631  { allowDouble_ = _allowDouble; return *this; }
633  AcceptedTypes& allowString( bool _allowString )
634  { allowString_ = _allowString; return *this; }
636  bool allowInt() const { return allowInt_; }
638  bool allowDouble() const { return allowDouble_; }
640  bool allowString() const { return allowString_; }
641  private:
642  bool allowInt_;
643  bool allowDouble_;
644  bool allowString_;
645  };
646 
648 
651 
656 
668  EPreferredType const preferredType,
669  AcceptedTypes const& acceptedTypes
670  );
671 
673 
676 
678  int getInt(
679  const ParameterEntry &entry, const std::string &paramName = "",
680  const std::string &sublistName = "", const bool activeQuery = true
681  ) const;
682 
684  double getDouble(
685  const ParameterEntry &entry, const std::string &paramName = "",
686  const std::string &sublistName = "", const bool activeQuery = true
687  ) const;
688 
690  std::string getString(
691  const ParameterEntry &entry, const std::string &paramName = "",
692  const std::string &sublistName = "", const bool activeQuery = true
693  ) const;
694 
698  int getInt(
699  ParameterList &paramList, const std::string &paramName,
700  const int defaultValue
701  ) const;
702 
706  double getDouble(
707  ParameterList &paramList, const std::string &paramName,
708  const double defaultValue
709  ) const;
710 
714  std::string getString(
715  ParameterList &paramList, const std::string &paramName,
716  const std::string &defaultValue
717  ) const;
718 
721  bool isDoubleAllowed() const;
722 
725  bool isIntAllowed() const;
726 
729  bool isStringAllowed() const;
730 
733  EPreferredType getPreferredType() const;
734 
736  static const std::string& getPrefferedTypeString (EPreferredType enumValue)
737  {
738  switch (enumValue) {
739  case PREFER_INT:
740  return getIntEnumString ();
741  case PREFER_DOUBLE:
742  return getDoubleEnumString ();
743  case PREFER_STRING:
744  return getStringEnumString ();
745  default:
746  const std::string typeString (toString (enumValue));
747  throw std::runtime_error("Cannot convert enumValue: " + typeString + " to a string");
748  }
749  }
750 
752  static EPreferredType getPrefferedTypeStringEnum (const std::string& enumString)
753  {
754  if (enumString == getIntEnumString ()) {
755  return PREFER_INT;
756  }
757  else if (enumString == getDoubleEnumString ()) {
758  return PREFER_DOUBLE;
759  }
760  else if (enumString == getStringEnumString ()) {
761  return PREFER_STRING;
762  }
763  else {
764  throw std::runtime_error ("Cannot convert enumString: " + enumString + " to an enum");
765  }
766  }
767 
769 
772 
774  const std::string getXMLTypeName() const;
775 
777  void printDoc(
778  std::string const& docString,
779  std::ostream & out
780  ) const;
781 
784  validStringValues() const;
785 
787  void validate(
788  ParameterEntry const& entry,
789  std::string const& paramName,
790  std::string const& sublistName
791  ) const;
792 
794  void validateAndModify(
795  std::string const& paramName,
796  std::string const& sublistName,
797  ParameterEntry * entry
798  ) const;
799 
800 
802 
803 private:
804 
805  // ////////////////////////////
806  // Private data members
807 
808  EPreferredType preferredType_;
809  std::string acceptedTypesString_;
810 
811 //use pragmas to disable some false-positive warnings for windows sharedlibs export
812 #ifdef _MSC_VER
813 #pragma warning(push)
814 #pragma warning(disable:4251)
815 #endif
816  const AcceptedTypes acceptedTypes_;
817 #ifdef _MSC_VER
818 #pragma warning(pop)
819 #endif
820 
821  // ////////////////////////////
822  // Private member functions
823 
824  /* \brief Gets the string representing the "int" preferred type enum */
825  static const std::string& getIntEnumString(){
826  static const std::string intEnumString_ = TypeNameTraits<int>::name();
827  return intEnumString_;
828  }
829 
830  /* \brief Gets the string representing the "double" preferred type enum */
831  static const std::string& getDoubleEnumString(){
832  static const std::string doubleEnumString_ = TypeNameTraits<double>::name();
833  return doubleEnumString_;
834  }
835 
836  /* \brief Gets the string representing the "string" preferred type enum */
837  static const std::string& getStringEnumString(){
838  static const std::string stringEnumString_ = TypeNameTraits<std::string>::name();
839  return stringEnumString_;
840  }
841 
842 
843  void finishInitialization();
844 
845  void throwTypeError(
846  ParameterEntry const& entry,
847  std::string const& paramName,
848  std::string const& sublistName
849  ) const;
850 
851 };
852 
853 
854 // Nonmember helper functions
855 
856 
861 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT RCP<AnyNumberParameterEntryValidator>
862 anyNumberParameterEntryValidator();
863 
864 
869 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT RCP<AnyNumberParameterEntryValidator>
870 anyNumberParameterEntryValidator(
873  );
874 
880 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setIntParameter(
881  std::string const& paramName,
882  int const value, std::string const& docString,
883  ParameterList *paramList,
886  );
887 
888 
894 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setDoubleParameter(
895  std::string const& paramName,
896  double const& value, std::string const& docString,
897  ParameterList *paramList,
900  );
901 
902 
908 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setNumericStringParameter(
909  std::string const& paramName,
910  std::string const& value, std::string const& docString,
911  ParameterList *paramList,
914  );
915 
916 
931 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT int getIntParameter(
932  ParameterList const& paramList, std::string const& paramName
933  );
934 
935 
950 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT double getDoubleParameter(
951  ParameterList const& paramList,
952  std::string const& paramName
953  );
954 
955 
971 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT std::string getNumericStringParameter(
972  ParameterList const& paramList,
973  std::string const& paramName
974  );
975 
981 template<>
982 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT DummyObjectGetter<AnyNumberParameterEntryValidator>{
983 
984 public:
985 
988 
992  static RCP<AnyNumberParameterEntryValidator > getDummyObject();
993 
995 
996 };
997 
998 
1003 template <class T>
1006  static inline T notDefined() {
1007  return T::this_type_is_missing_a_specialization();
1008  }
1009 };
1010 
1011 
1032 template <class T>
1034 public:
1035 
1037  static inline T min()
1039 
1041  static inline T max()
1043 
1046  static inline T defaultStep()
1048 
1051  static inline unsigned short defaultPrecision()
1053 
1054 };
1055 
1056 
1057 template<>
1058 class EnhancedNumberTraits<short int>{
1059 public:
1060  static inline short int min() { return std::numeric_limits<short int>::min(); }
1061  static inline short int max() { return std::numeric_limits<short int>::max(); }
1062  static inline short int defaultStep() { return 1; }
1063  static inline unsigned short defaultPrecision() { return 0; }
1064 };
1065 
1066 
1067 template<>
1068 class EnhancedNumberTraits<short unsigned int>{
1069 public:
1070  static inline short unsigned int min() { return std::numeric_limits<short unsigned int>::min(); }
1071  static inline short unsigned int max() { return std::numeric_limits<short unsigned int>::max(); }
1072  static inline short unsigned int defaultStep() { return 1; }
1073  static inline unsigned short defaultPrecision() { return 0; }
1074 };
1075 
1076 
1077 template<>
1078 class EnhancedNumberTraits<int>{
1079 public:
1080  static inline int min() { return std::numeric_limits<int>::min(); }
1081  static inline int max() { return std::numeric_limits<int>::max(); }
1082  static inline int defaultStep() { return 1; }
1083  static inline unsigned short defaultPrecision() { return 0; }
1084 };
1085 
1086 
1087 template<>
1088 class EnhancedNumberTraits<unsigned int>{
1089 public:
1090  static inline unsigned int min() { return std::numeric_limits<unsigned int>::min(); }
1091  static inline unsigned int max() { return std::numeric_limits<unsigned int>::max(); }
1092  static inline unsigned int defaultStep() { return 1; }
1093  static inline unsigned short defaultPrecision() { return 0; }
1094 };
1095 
1096 
1097 template<>
1098 class EnhancedNumberTraits<long int>{
1099 public:
1100  static inline long int min() { return std::numeric_limits<long int>::min(); }
1101  static inline long int max() { return std::numeric_limits<long int>::max(); }
1102  static inline long int defaultStep() { return 1; }
1103  static inline unsigned short defaultPrecision() { return 0; }
1104 };
1105 
1106 
1107 template<>
1108 class EnhancedNumberTraits<long unsigned int>{
1109 public:
1110  static inline long unsigned int min() { return std::numeric_limits<long unsigned int>::min(); }
1111  static inline long unsigned int max() { return std::numeric_limits<long unsigned int>::max(); }
1112  static inline long unsigned int defaultStep() { return 1; }
1113  static inline unsigned short defaultPrecision() { return 0; }
1114 };
1115 
1116 
1117 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
1118 
1119 
1120 template<>
1121 class EnhancedNumberTraits<long long int>{
1122 public:
1123  static inline long long int min() { return std::numeric_limits<long long int>::min(); }
1124  static inline long long int max() { return std::numeric_limits<long long int>::max(); }
1125  static inline long long int defaultStep() { return 1; }
1126  static inline unsigned short defaultPrecision() { return 0; }
1127 };
1128 
1129 
1130 template<>
1131 class EnhancedNumberTraits<long long unsigned int>{
1132 public:
1133  static inline long long unsigned int min() { return std::numeric_limits<long long unsigned int>::min(); }
1134  static inline long long unsigned int max() { return std::numeric_limits<long long unsigned int>::max(); }
1135  static inline long long unsigned int defaultStep() { return 1; }
1136  static inline unsigned short defaultPrecision() { return 0; }
1137 };
1138 
1139 
1140 #endif // HAVE_TEUCHOS_LONG_LONG_INT
1141 
1142 
1143 #ifdef HAVE_TEUCHOSCORE_QUADMATH
1144 template<>
1145 class EnhancedNumberTraits<__float128>{
1146 public:
1147  static inline __float128 min() { return -std::numeric_limits<__float128>::max(); }
1148  static inline __float128 max() { return std::numeric_limits<__float128>::max(); }
1149  static inline __float128 defaultStep() { return 1; }
1150  static inline unsigned short defaultPrecision() { return 100; }
1151 };
1152 #endif // HAVE_TEUCHOSCORE_QUADMATH
1153 
1154 template<>
1155 class EnhancedNumberTraits<double>{
1156 public:
1157  static inline double min() { return -std::numeric_limits<double>::max(); }
1158  static inline double max() { return std::numeric_limits<double>::max(); }
1159  static inline double defaultStep() { return 1; }
1160  static inline unsigned short defaultPrecision() { return 100; }
1161 };
1162 
1163 template<>
1164 class EnhancedNumberTraits<float>{
1165 public:
1166  static inline float min() { return -std::numeric_limits<float>::max(); }
1167  static inline float max() { return std::numeric_limits<float>::max(); }
1168  static inline float defaultStep() { return 1; }
1169  static inline unsigned short defaultPrecision() { return 100; }
1170 };
1171 
1177 template <class T>
1179 
1180 public:
1181 
1184 
1200  T min,
1201  T max,
1203  unsigned short precision=EnhancedNumberTraits<T>::defaultPrecision()):
1205  minVal(min), maxVal(max), step_(step), precision_(precision),
1206  containsMin(true), containsMax(true){}
1207 
1213  minVal(EnhancedNumberTraits<T>::min()),
1214  maxVal(EnhancedNumberTraits<T>::max()),
1215  step_(EnhancedNumberTraits<T>::defaultStep()),
1216  precision_(EnhancedNumberTraits<T>::defaultPrecision()),
1217  containsMin(false),
1218  containsMax(false){}
1219 
1221 
1223 
1224 
1229  void setMin(T min){
1230  minVal = min;
1231  containsMin = true;
1232  }
1233 
1238  void setMax(T max){
1239  maxVal = max;
1240  containsMax = true;
1241  }
1242 
1247  void setStep(T step){
1248  step_ = step;
1249  }
1250 
1255  void setPrecision(unsigned short precision){
1256  precision_ = precision;
1257  }
1258 
1260 
1263 
1268  T getMin() const{
1269  return minVal;
1270  }
1271 
1276  T getMax() const{
1277  return maxVal;
1278  }
1279 
1284  T getStep() const{
1285  return step_;
1286  }
1287 
1292  unsigned short getPrecision() const{
1293  return precision_;
1294  }
1295 
1297 
1299 
1300 
1305  bool hasMin() const{
1306  return containsMin;
1307  }
1308 
1313  bool hasMax() const{
1314  return containsMax;
1315  }
1316 
1318 
1321 
1324  return null;
1325  }
1326 
1328  void validate(ParameterEntry const &entry, std::string const &paramName,
1329  std::string const &sublistName) const;
1330 
1332  const std::string getXMLTypeName() const{
1333  return "EnhancedNumberValidator(" + TypeNameTraits<T>::name()+ ")";
1334  }
1335 
1337  void printDoc(std::string const &docString, std::ostream &out) const{
1338  StrUtils::printLines(out,"# ",docString);
1339  out << "#\tValidator Used: " << std::endl;
1340  out << "#\t\tNumber Validator" << std::endl;
1341  out << "#\t\tType: " << Teuchos::TypeNameTraits<T>::name() <<
1342  std::endl;
1343  out << "#\t\tMin (inclusive): " << minVal << std::endl;
1344  out << "#\t\tMax (inclusive): " << maxVal << std::endl;
1345  }
1346 
1348 
1349 private:
1350 
1353 
1356  T minVal;
1357 
1360  T maxVal;
1361 
1364  T step_;
1365 
1369  unsigned short precision_;
1370 
1373  bool containsMin;
1374 
1377  bool containsMax;
1378 
1380 
1381 };
1382 
1383 template<class T>
1384 void EnhancedNumberValidator<T>::validate(ParameterEntry const &entry, std::string const &paramName,
1385  std::string const &sublistName) const
1386 {
1387  any anyValue = entry.getAny(true);
1388  const std::string &entryName = entry.getAny(false).typeName();
1389 
1390  TEUCHOS_TEST_FOR_EXCEPTION(anyValue.type() != typeid(T),
1392  "The \"" << paramName << "\"" <<
1393  " parameter in the \"" << sublistName <<
1394  "\" sublist is has an error." << std::endl << std::endl <<
1395  "Error: The value that you entered was the wrong type." << std::endl <<
1396  "Parameter: " << paramName << std::endl <<
1397  "Type specified: " << entryName << std::endl <<
1398  "Type accepted: " << Teuchos::TypeNameTraits<T>::name() << std::endl);
1399 
1400  bool isValueInRange;
1401  any_cast<T>(anyValue) >= minVal && any_cast<T>(anyValue) <= maxVal
1402  ? isValueInRange = true : isValueInRange=false;
1403  TEUCHOS_TEST_FOR_EXCEPTION(!(isValueInRange),
1405  "The \"" << paramName << "\"" <<
1406  " parameter in the \"" << sublistName <<
1407  "\" sublist is has an error." << std::endl << std::endl <<
1408  "Error: The value that was entered doesn't fall with in " <<
1409  "the range set by the validator" << std::endl <<
1410  "Parameter: " << paramName << std::endl <<
1411  "Min: " << minVal << std::endl <<
1412  "Max: " << maxVal << std::endl <<
1413  "Value entered: " <<
1414  (any_cast<T>(anyValue)) << std::endl << std::endl);
1415 }
1416 
1422 template<class T>
1424 
1425 public:
1426 
1429 
1433  static RCP<EnhancedNumberValidator<T> > getDummyObject();
1434 
1436 };
1437 
1438 template<class T>
1441 {
1442  return rcp(new EnhancedNumberValidator<T>);
1443 }
1444 
1453 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT FileNameValidator : public ParameterEntryValidator {
1454 
1455 public:
1456 
1459 
1462  static bool mustAlreadyExistDefault() { return false; }
1463 
1465 
1468 
1474  FileNameValidator(bool mustAlreadyExist = mustAlreadyExistDefault());
1475 
1477 
1479 
1480 
1486  bool fileMustExist() const;
1487 
1489 
1490 
1501  bool fileEmptyNameOK() const;
1502 
1504 
1506 
1507 
1516  bool setFileMustExist(bool shouldFileExist);
1517 
1526  bool setFileEmptyNameOK(bool isEmptyNameOK);
1527 
1529 
1532 
1535 
1537  void validate(
1538  ParameterEntry const &entry,
1539  std::string const &paramName,
1540  std::string const &sublistName) const;
1541 
1543  const std::string getXMLTypeName() const;
1544 
1546  void printDoc(std::string const &docString, std::ostream &out) const;
1547 
1549 
1550 private:
1551 
1554 
1558  bool mustAlreadyExist_;
1559  bool EmptyNameOK_;
1560 
1562 
1563 };
1564 
1570 template<>
1571 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT DummyObjectGetter<FileNameValidator>{
1572 
1573 public:
1574 
1577 
1581  static RCP<FileNameValidator> getDummyObject();
1582 
1584 
1585 };
1586 
1594 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT StringValidator : public ParameterEntryValidator {
1595 
1596 public:
1597 
1600 
1603  StringValidator();
1604 
1609  StringValidator(const Teuchos::Array<std::string> &validStrings);
1610 
1612 
1614 
1615 
1623  ValidStringsList setValidStrings(
1624  const Teuchos::Array<std::string> &validStrings);
1625 
1627 
1630 
1633 
1635  void validate(ParameterEntry const &entry, std::string const &paramName,
1636  std::string const &sublistName) const;
1637 
1639  const std::string getXMLTypeName() const;
1640 
1642  void printDoc(std::string const &docString, std::ostream &out) const;
1643 
1645 
1646 private:
1647 
1650 
1653  ValidStringsList validStrings_;
1654 
1656 
1657 };
1658 
1664 template<>
1665 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT DummyObjectGetter<StringValidator>{
1666 
1667 public:
1668 
1671 
1675  static RCP<StringValidator> getDummyObject();
1676 
1678 
1679 };
1680 
1681 
1685 template<class ValidatorType, class EntryType>
1687 
1688 public:
1689 
1692 
1701  prototypeValidator_(prototypeValidator){}
1702 
1704 
1707 
1710  return prototypeValidator_;
1711  }
1712 
1714 
1717 
1720  return prototypeValidator_->validStringValues();
1721  }
1722 
1724 
1725 private:
1726 
1729 
1732  RCP<const ValidatorType> prototypeValidator_;
1733 
1736 
1738 
1739 };
1740 
1753 template<class ValidatorType, class EntryType>
1754 class TwoDArrayValidator : public AbstractArrayValidator<ValidatorType, EntryType>{
1755 public:
1758 
1766  AbstractArrayValidator<ValidatorType, EntryType>(prototypeValidator){}
1767 
1769 
1772 
1774  virtual void validate(ParameterEntry const &entry, std::string const &paramName,
1775  std::string const &sublistName) const;
1776 
1778  const std::string getXMLTypeName() const{
1779  return "TwoDArrayValidator(" +
1780  this->getPrototype()->getXMLTypeName() + ", " +
1782  }
1783 
1785  virtual void printDoc(std::string const &docString, std::ostream &out) const
1786  {
1787  StrUtils::printLines(out,"# ",docString);
1788  std::string toPrint;
1789  toPrint += "TwoDArrayValidator:\n";
1790  toPrint += "Prototype Validator:\n";
1791  this->getPrototype()->printDoc(toPrint, out);
1792  }
1793 
1795 
1796 };
1797 
1798 template<class ValidatorType, class EntryType>
1799 void TwoDArrayValidator<ValidatorType, EntryType>::validate(ParameterEntry const &entry, std::string const &paramName,
1800  std::string const &sublistName) const
1801 {
1802  any anyValue = entry.getAny(true);
1803  const std::string &entryName = entry.getAny(false).typeName();
1806  "The \"" << paramName << "\"" <<
1807  " parameter in the \"" << sublistName <<
1808  "\" sublist is has an error." << std::endl << std::endl <<
1809  "Error: The value you entered was the wrong type." << std::endl <<
1810  "Parameter: " << paramName << std::endl <<
1811  "Type specified: " << entryName << std::endl <<
1812  "Type accepted: " << TypeNameTraits<TwoDArray<EntryType> >::name() <<
1813  std::endl << std::endl);
1814 
1815  TwoDArray<EntryType> extracted =
1816  getValue<Teuchos::TwoDArray<EntryType> >(entry);
1817  RCP<const ParameterEntryValidator> prototype = this->getPrototype();
1818  for(int i = 0; i<extracted.getNumRows(); ++i){
1819  for(int j = 0; j<extracted.getNumCols(); ++j){
1820  ParameterEntry dummyParameter;
1821  dummyParameter.setValue(extracted(i,j));
1822  try{
1823  prototype->validate(
1824  dummyParameter, paramName, sublistName);
1825  }
1827  std::stringstream oss;
1828  oss << "TwoDArray Validator Exception:" << std::endl <<
1829  "Bad Index: (" << i << "," << j << ")" << std::endl << e.what();
1830  throw Exceptions::InvalidParameterValue(oss.str());
1831  }
1832  }
1833  }
1834 }
1835 
1836 
1842 template<class ValidatorType, class EntryType>
1843 class DummyObjectGetter<TwoDArrayValidator<ValidatorType, EntryType> >{
1844 
1845 public:
1846 
1849 
1853  static RCP<TwoDArrayValidator<ValidatorType, EntryType> > getDummyObject();
1854 
1856 
1857 };
1858 
1859 template<class ValidatorType, class EntryType>
1862 {
1865 }
1866 
1870 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT TwoDArrayStringValidator :
1871  public TwoDArrayValidator<StringValidator, std::string>{
1872 
1873 public:
1874 
1877 
1880  TwoDArrayValidator<StringValidator, std::string>(prototypeValidator){}
1881 
1883 
1884 };
1885 
1886 
1891 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT TwoDArrayFileNameValidator :
1892  public TwoDArrayValidator<FileNameValidator, std::string>{
1893 
1894 public:
1895 
1898 
1901  TwoDArrayValidator<FileNameValidator, std::string>(prototypeValidator){}
1902 
1904 
1905 };
1906 
1907 
1911 template<class T>
1912 class TwoDArrayNumberValidator : public TwoDArrayValidator<EnhancedNumberValidator<T>, T>{
1913 public:
1916 
1919  RCP<const EnhancedNumberValidator<T> > prototypeValidator):
1920  TwoDArrayValidator<EnhancedNumberValidator<T>, T>(prototypeValidator){}
1921 
1923 
1924 };
1925 
1926 
1938 template<class ValidatorType, class EntryType>
1939 class ArrayValidator : public AbstractArrayValidator<ValidatorType, EntryType>{
1940 
1941 public:
1942 
1945 
1952  AbstractArrayValidator<ValidatorType, EntryType>(prototypeValidator){}
1953 
1955 
1958 
1960  virtual void validate(ParameterEntry const &entry, std::string const &paramName,
1961  std::string const &sublistName) const;
1962 
1964  const std::string getXMLTypeName() const{
1965  return "ArrayValidator(" +
1966  this->getPrototype()->getXMLTypeName() + ", " +
1968  }
1969 
1971  virtual void printDoc(std::string const &docString, std::ostream &out) const
1972  {
1973  StrUtils::printLines(out,"# ",docString);
1974  std::string toPrint;
1975  toPrint += "ArrayValidator:\n";
1976  toPrint += "Prototype Validator:\n";
1977  this->getPrototype()->printDoc(toPrint, out);
1978  }
1979 
1981 
1982 };
1983 
1984 template<class ValidatorType, class EntryType>
1985 void ArrayValidator<ValidatorType, EntryType>::validate(ParameterEntry const &entry, std::string const &paramName,
1986  std::string const &sublistName) const
1987 {
1988  any anyValue = entry.getAny(true);
1989  const std::string &entryName = entry.getAny(false).typeName();
1990  TEUCHOS_TEST_FOR_EXCEPTION(anyValue.type() != typeid(Array<EntryType>),
1992  "The \"" << paramName << "\"" <<
1993  " parameter in the \"" << sublistName <<
1994  "\" sublist is has an error." << std::endl << std::endl <<
1995  "Error: The value you entered was the wrong type." << std::endl <<
1996  "Parameter: " << paramName << std::endl <<
1997  "Type specified: " << entryName << std::endl <<
1998  "Type accepted: " << TypeNameTraits<Array<EntryType> >::name() <<
1999  std::endl << std::endl);
2000 
2001  Array<EntryType> extracted =
2002  getValue<Teuchos::Array<EntryType> >(entry);
2003  RCP<const ParameterEntryValidator> prototype = this->getPrototype();
2004  for(int i = 0; i<extracted.size(); ++i){
2005  ParameterEntry dummyParameter;
2006  dummyParameter.setValue(extracted[i]);
2007  try{
2008  prototype->validate(
2009  dummyParameter, paramName, sublistName);
2010  }
2012  std::stringstream oss;
2013  oss << "Array Validator Exception:" << std::endl <<
2014  "Bad Index: " << i << std::endl << e.what();
2015  throw Exceptions::InvalidParameterValue(oss.str());
2016  }
2017  }
2018 }
2019 
2025 template<class ValidatorType, class EntryType>
2026 class DummyObjectGetter<ArrayValidator<ValidatorType, EntryType> >{
2027 
2028 public:
2029 
2032 
2036  static RCP<ArrayValidator<ValidatorType, EntryType> > getDummyObject();
2037 
2039 
2040 };
2041 
2042 template<class ValidatorType, class EntryType>
2045 {
2048 }
2049 
2050 
2059 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT ArrayStringValidator :
2060  public ArrayValidator<StringValidator, std::string>{
2061 
2062 public:
2063 
2066 
2069  ArrayValidator<StringValidator, std::string>(prototypeValidator){}
2070 
2072 
2073 };
2074 
2075 
2084 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT ArrayFileNameValidator : public ArrayValidator<FileNameValidator, std::string>{
2085 
2086 public:
2087 
2090 
2093  ArrayValidator<FileNameValidator, std::string>(prototypeValidator){}
2094 
2096 
2097 };
2098 
2099 
2107 template<class T>
2108 class ArrayNumberValidator : public ArrayValidator<EnhancedNumberValidator<T>, T>{
2109 public:
2112 
2115  RCP<const EnhancedNumberValidator<T> > prototypeValidator):
2116  ArrayValidator<EnhancedNumberValidator<T>, T>(prototypeValidator){}
2117 
2119 
2120 };
2121 
2122 
2123 
2124 // ///////////////////////////
2125 // Implementations
2126 
2127 
2128 //
2129 // StringToIntegralParameterEntryValidator
2130 //
2131 
2132 
2133 // Constructors
2134 
2135 
2136 template<class IntegralType>
2139  std::string const& defaultParameterName,
2140  const bool caseSensitive) :
2142  defaultParameterName_ (defaultParameterName),
2143  caseSensitive_ (caseSensitive)
2144 {
2145  typedef typename map_t::value_type val_t;
2146  for (int i = 0; i < static_cast<int> (strings.size ()); ++i) {
2147  const bool unique = caseSensitive_ ?
2148  map_.insert (val_t (strings[i], static_cast<IntegralType> (i))).second :
2149  map_.insert (val_t (upperCase (strings[i]), static_cast<IntegralType> (i))).second;
2151  ! unique, std::logic_error,
2152  "For parameter \"" << defaultParameterName_ << "\": "
2153  "strings[" << i << "] = \"" << strings[i] << "\" is a duplicate.");
2154  }
2155  setValidValues (strings);
2156 }
2157 
2158 
2159 template<class IntegralType>
2162  ArrayView<const IntegralType> const& integralValues,
2163  std::string const& defaultParameterName,
2164  const bool caseSensitive) :
2166  defaultParameterName_ (defaultParameterName),
2167  caseSensitive_ (caseSensitive)
2168 {
2169 #ifdef TEUCHOS_DEBUG
2170  TEUCHOS_ASSERT_EQUALITY( strings.size(), integralValues.size() );
2171 #endif
2173  strings.size() != integralValues.size(),
2174  std::logic_error,
2175  "The input arrays strings and integralValues must have the same length.");
2176 
2177  typedef typename map_t::value_type val_t;
2178  for (int i = 0; i < static_cast<int> (strings.size ()); ++i) {
2179  const bool unique = caseSensitive_ ?
2180  map_.insert (val_t (strings[i], integralValues[i])).second :
2181  map_.insert (val_t (upperCase (strings[i]), integralValues[i])).second;
2182 
2184  ! unique, std::logic_error,
2185  "For parameter \"" << defaultParameterName_ << "\": "
2186  "strings[" << i << "] = \"" << strings[i] << "\" is a duplicate.");
2187  }
2188  setValidValues (strings);
2189 }
2190 
2191 template<class IntegralType>
2194  ArrayView<const std::string> const& stringsDocs,
2195  ArrayView<const IntegralType> const& integralValues,
2196  std::string const& defaultParameterName,
2197  const bool caseSensitive) :
2199  defaultParameterName_ (defaultParameterName),
2200  caseSensitive_ (caseSensitive)
2201 {
2202 #ifdef TEUCHOS_DEBUG
2203  TEUCHOS_ASSERT_EQUALITY( strings.size(), stringsDocs.size() );
2204  TEUCHOS_ASSERT_EQUALITY( strings.size(), integralValues.size() );
2205 #endif
2206 
2208  strings.size() != integralValues.size(),
2209  std::logic_error,
2210  "The input arrays strings and integralValues must have the same length.");
2211 
2213  strings.size() != stringsDocs.size(),
2214  std::logic_error,
2215  "The input arrays strings and stringsDocs must have the same length.");
2216 
2217  typedef typename map_t::value_type val_t;
2218  for (int i = 0; i < static_cast<int> (strings.size ()); ++i) {
2219  const bool unique = caseSensitive_ ?
2220  map_.insert (val_t (strings[i], integralValues[i])).second :
2221  map_.insert (val_t (upperCase (strings[i]), integralValues[i])).second;
2223  ! unique, std::logic_error,
2224  "For parameter \"" << defaultParameterName_ << "\": "
2225  "strings[" << i << "] = \"" << strings[i] << "\" is a duplicate.");
2226  }
2227  setValidValues(strings,&stringsDocs);
2228 }
2229 
2230 // Lookup functions
2231 
2232 
2233 template<class IntegralType>
2234 IntegralType
2236  const std::string &str, const std::string &paramName
2237  ,const std::string &sublistName
2238  ) const
2239 {
2240  typename map_t::const_iterator itr = map_.find (caseSensitive_ ? str : upperCase (str));
2242  itr == map_.end(), Exceptions::InvalidParameterValue
2243  ,"Error, the value \"" << str << "\" is not recognized for the parameter \""
2244  << ( paramName.length() ? paramName : defaultParameterName_ ) << "\""
2245  << "\nin the sublist \"" << sublistName << "\"."
2246  << "\n\nValid values include:"
2247  << "\n {\n"
2248  << validValues_
2249  << " }"
2250  );
2251  return (*itr).second;
2252 }
2253 
2254 
2255 template<class IntegralType>
2256 IntegralType
2258  const ParameterEntry &entry, const std::string &paramName
2259  ,const std::string &sublistName, const bool activeQuery
2260  ) const
2261 {
2262  const bool validType = ( entry.getAny(activeQuery).type() == typeid(std::string) );
2265  ,"Error, the parameter {paramName=\""<<(paramName.length()?paramName:defaultParameterName_)
2266  << "\",type=\""<<entry.getAny(activeQuery).typeName()<<"\"}"
2267  << "\nin the sublist \"" << sublistName << "\""
2268  << "\nhas the wrong type."
2269  << "\n\nThe correct type is \"string\"!"
2270  );
2271  const std::string
2272  &strValue = any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail!
2273  return getIntegralValue(strValue,paramName,sublistName); // This will validate the value and throw!
2274 }
2275 
2276 
2277 template<class IntegralType>
2278 std::string
2280  const ParameterEntry &entry, const std::string &paramName
2281  ,const std::string &sublistName, const bool activeQuery
2282  ) const
2283 {
2284  // Validate the parameter's type and value
2285  this->getIntegralValue(entry,paramName,sublistName,activeQuery);
2286  // Return the std::string value which is now validated!
2287  return any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail!
2288 }
2289 
2290 
2291 template<class IntegralType>
2292 IntegralType
2294  ParameterList &paramList, const std::string &paramName
2295  ,const std::string &defaultValue
2296  ) const
2297 {
2298  const std::string& strValue =
2299  paramList.get (paramName,
2300  caseSensitive_ ? defaultValue : upperCase (defaultValue));
2301  return getIntegralValue (strValue, paramName, paramList.name ());
2302 }
2303 
2304 
2305 template<class IntegralType>
2306 std::string
2308  ParameterList &paramList, const std::string &paramName
2309  ,const std::string &defaultValue
2310  ) const
2311 {
2312  const std::string& strValue =
2313  paramList.get (paramName,
2314  caseSensitive_ ? defaultValue : upperCase (defaultValue));
2315  getIntegralValue(strValue,paramName,paramList.name()); // Validate!
2316  return strValue;
2317 }
2318 
2319 template<class IntegralType>
2322 {
2323  return validStringValuesDocs_;
2324 }
2325 
2326 template<class IntegralType>
2327 const std::string&
2329 {
2330  return defaultParameterName_;
2331 }
2332 
2333 template<class IntegralType>
2334 std::string
2336  const std::string &str, const std::string &paramName
2337  ,const std::string &sublistName
2338  ) const
2339 {
2340  getIntegralValue (caseSensitive_ ? str : upperCase (str),
2341  paramName,
2342  sublistName); // Validate!
2343  return str;
2344 }
2345 
2346 
2347 // Overridden from ParameterEntryValidator
2348 
2349 template<class IntegralType>
2350 const std::string
2352  return "StringIntegralValidator(" + TypeNameTraits<IntegralType>::name () + ")";
2353 }
2354 
2355 template<class IntegralType>
2357  std::string const& docString
2358  ,std::ostream & out
2359  ) const
2360 {
2361  StrUtils::printLines(out,"# ",docString);
2362  out << "# Valid std::string values:\n";
2363  out << "# {\n";
2364  if(validStringValuesDocs_.get()) {
2365  for( int i = 0; i < static_cast<int>(validStringValues_->size()); ++i ) {
2366  out << "# \"" << (*validStringValues_)[i] << "\"\n";
2367  StrUtils::printLines(out,"# ",(*validStringValuesDocs_)[i] );
2368  }
2369  }
2370  else {
2371  StrUtils::printLines(out,"# ",validValues_);
2372  // Note: Above validValues_ has for initial spaces already so indent should
2373  // be correct!
2374  }
2375  out << "# }\n";
2376 }
2377 
2378 
2379 template<class IntegralType>
2382 {
2383  return validStringValues_;
2384 }
2385 
2386 
2387 template<class IntegralType>
2389  ParameterEntry const& entry
2390  ,std::string const& paramName
2391  ,std::string const& sublistName
2392  ) const
2393 {
2394  this->getIntegralValue (entry, paramName, sublistName, false);
2395 }
2396 
2397 
2398 // private
2399 
2400 template<class IntegralType>
2402  ArrayView<const std::string> const& strings
2403  ,ArrayView<const std::string> const* stringsDocs
2404  )
2405 {
2406  if (caseSensitive_) {
2407  validStringValues_ = rcp (new Array<std::string> (strings));
2408  }
2409  else {
2410  RCP<Array<std::string> > vals (new Array<std::string> (strings.size ()));
2411  for (Array<std::string>::size_type i = 0; i < strings.size (); ++i) {
2412  (*vals)[i] = upperCase (strings[i]);
2413  }
2414  validStringValues_ = rcp_const_cast<const Array<std::string> > (vals);
2415  }
2416 
2417  if (stringsDocs) {
2418  validStringValuesDocs_ = rcp (new Array<std::string> (*stringsDocs));
2419  }
2420  // Build the list of valid values in the same order as passed in by the client.
2421  std::ostringstream oss;
2422  for (int i = 0; i < static_cast<int> (strings.size()); ++i) {
2423  oss << " \"" << strings[i] << "\"\n";
2424  }
2425  // Note: Above four spaces is designed for the error output above.
2426  validValues_ = oss.str();
2427 }
2428 
2429 
2430 } // namespace Teuchos
2431 
2432 
2433 //
2434 // Nonmember function implementations for StringToIntegralParameterEntryValidator
2435 //
2436 
2437 
2438 template<class IntegralType>
2439 inline
2441 Teuchos::stringToIntegralParameterEntryValidator(
2442  ArrayView<const std::string> const& strings,
2443  std::string const& defaultParameterName
2444  )
2445 {
2446  return rcp(
2448  strings, defaultParameterName
2449  )
2450  );
2451 }
2452 
2453 
2454 template<class IntegralType>
2455 inline
2457 Teuchos::stringToIntegralParameterEntryValidator(
2458  ArrayView<const std::string> const& strings,
2459  std::string const& defaultParameterName,
2460  const bool caseSensitive
2461  )
2462 {
2464  return rcp (new ret_type (strings, defaultParameterName, caseSensitive));
2465 }
2466 
2467 
2468 
2469 template<class IntegralType>
2470 inline
2472 Teuchos::stringToIntegralParameterEntryValidator(
2473  ArrayView<const std::string> const& strings,
2474  ArrayView<const IntegralType> const& integralValues,
2475  std::string const& defaultParameterName
2476  )
2477 {
2478  return rcp(
2480  strings, integralValues, defaultParameterName
2481  )
2482  );
2483 }
2484 
2485 
2486 template<class IntegralType>
2487 inline
2489 Teuchos::stringToIntegralParameterEntryValidator(
2490  ArrayView<const std::string> const& strings,
2491  ArrayView<const IntegralType> const& integralValues,
2492  std::string const& defaultParameterName,
2493  const bool caseSensitive)
2494 {
2496  return rcp (new ret_type (strings, integralValues,
2497  defaultParameterName, caseSensitive));
2498 }
2499 
2500 
2501 template<class IntegralType>
2502 inline
2504 Teuchos::stringToIntegralParameterEntryValidator(
2505  ArrayView<const std::string> const& strings,
2506  ArrayView<const std::string> const& stringsDocs,
2507  ArrayView<const IntegralType> const& integralValues,
2508  std::string const& defaultParameterName
2509  )
2510 {
2511  return rcp(
2513  strings, stringsDocs, integralValues, defaultParameterName
2514  )
2515  );
2516 }
2517 
2518 
2519 template<class IntegralType>
2520 inline
2522 Teuchos::stringToIntegralParameterEntryValidator(
2523  ArrayView<const std::string> const& strings,
2524  ArrayView<const std::string> const& stringsDocs,
2525  ArrayView<const IntegralType> const& integralValues,
2526  std::string const& defaultParameterName,
2527  const bool caseSensitive)
2528 {
2530  return rcp (new ret_type (strings, stringsDocs, integralValues,
2531  defaultParameterName, caseSensitive));
2532 }
2533 
2534 
2535 template<class IntegralType>
2536 void Teuchos::setStringToIntegralParameter(
2537  std::string const& paramName,
2538  std::string const& defaultValue,
2539  std::string const& docString,
2540  ArrayView<const std::string> const& strings,
2541  ParameterList * paramList
2542  )
2543 {
2544  typedef ParameterEntryValidator PEV;
2545  TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
2546  paramList->set(
2547  paramName, defaultValue, docString,
2548  rcp_implicit_cast<const PEV>(
2549  stringToIntegralParameterEntryValidator<IntegralType>(
2550  strings, paramName
2551  )
2552  )
2553  );
2554 }
2555 
2556 
2557 template<class IntegralType>
2558 void Teuchos::setStringToIntegralParameter(
2559  std::string const& paramName,
2560  std::string const& defaultValue,
2561  std::string const& docString,
2562  ArrayView<const std::string> const& strings,
2563  ArrayView<const IntegralType> const& integralValues,
2564  ParameterList * paramList
2565  )
2566 {
2567  typedef ParameterEntryValidator PEV;
2568  TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
2569  paramList->set(
2570  paramName, defaultValue, docString,
2571  rcp_implicit_cast<const PEV>(
2572  stringToIntegralParameterEntryValidator<IntegralType>(
2573  strings, integralValues, paramName
2574  )
2575  )
2576  );
2577 }
2578 
2579 
2580 template<class IntegralType>
2581 void Teuchos::setStringToIntegralParameter(
2582  std::string const& paramName,
2583  std::string const& defaultValue,
2584  std::string const& docString,
2585  ArrayView<const std::string> const& strings,
2586  ArrayView<const std::string> const& stringsDocs,
2587  ArrayView<const IntegralType> const& integralValues,
2588  ParameterList * paramList
2589  )
2590 
2591 {
2592  typedef ParameterEntryValidator PEV;
2593  TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
2594  paramList->set(
2595  paramName, defaultValue, docString,
2596  rcp_implicit_cast<const PEV>(
2597  stringToIntegralParameterEntryValidator<IntegralType>(
2598  strings, stringsDocs, integralValues, paramName
2599  )
2600  )
2601  );
2602 }
2603 
2604 
2605 template<class IntegralType>
2606 IntegralType Teuchos::getIntegralValue(
2607  ParameterList const& paramList, std::string const& paramName
2608  )
2609 {
2610  const ParameterEntry &entry = paramList.getEntry(paramName);
2612  integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>(
2613  entry, paramList, paramName
2614  );
2615  return integralValidator->getIntegralValue(
2616  entry, paramName, paramList.name(), true );
2617 }
2618 
2619 
2620 template<class IntegralType>
2621 std::string Teuchos::getStringValue(
2622  ParameterList const& paramList, std::string const& paramName
2623  )
2624 {
2625  const ParameterEntry &entry = paramList.getEntry(paramName);
2627  integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>(
2628  entry, paramList, paramName
2629  );
2630  return integralValidator->getStringValue(
2631  entry, paramName, paramList.name(), true
2632  );
2633 }
2634 
2635 
2636 template<class IntegralType>
2639  ParameterEntry const& entry, ParameterList const& paramList,
2640  std::string const& paramName
2641  )
2642 {
2643  const RCP<const ParameterEntryValidator> validator = entry.validator();
2646  "Error! The parameter \""<<paramName<<"\" exists\n"
2647  "in the parameter (sub)list \""<<paramList.name()<<"\"\n"
2648  "but it does not contain any validator needed to extract\n"
2649  "an integral value of type \""<<TypeNameTraits<IntegralType>::name()<<"\"!"
2650  );
2653  validator
2654  );
2656  is_null(integralValidator), Exceptions::InvalidParameterType,
2657  "Error! The parameter \""<<paramName<<"\" exists\n"
2658  "in the parameter (sub)list \""<<paramList.name()<<"\"\n"
2659  "but it contains the wrong type of validator. The expected validator type\n"
2661  "but the contained validator type is \""<<typeName(*validator)<<"\"!"
2662  );
2663  return integralValidator;
2664 }
2665 
2666 
2667 #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< StringToIntegralParameterEntryValidator< IntegralType > > stringToIntegralParameterEntryValidator(ArrayView< const std::string > const &strings, std::string const &defaultParameterName)
Nonmember constructor (see implementation).
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.
EnhancedNumberValidator()
Constructs a EnhancedNumberValidator without an explicit minimum or maximum.
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.
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...
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< const ParameterEntryValidator > validator() const
Return the (optional) validator object.
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.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Set a parameter whose value has type T.
This object is held as the "value" in the Teuchos::ParameterList std::map.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
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<T> 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.
T * get() const
Get the raw C++ pointer to the underlying object.
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.
EVerbosityLevel
Verbosity level.
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.
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 interpurted 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.
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.
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.
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.
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.
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.
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.
Standard implementation of a ParameterEntryValidator that accepts numbers from a number of different ...
ParameterEntry & getEntry(const std::string &name)
Retrieves an entry with the name name.
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. ...
#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.
#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)