|
Teuchos - Trilinos Tools Package
Version of the Day
|
00001 // @HEADER 00002 // *********************************************************************** 00003 // 00004 // Teuchos: Common Tools Package 00005 // Copyright (2004) Sandia Corporation 00006 // 00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive 00008 // license for use of this work by or on behalf of the U.S. Government. 00009 // 00010 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are 00012 // met: 00013 // 00014 // 1. Redistributions of source code must retain the above copyright 00015 // notice, this list of conditions and the following disclaimer. 00016 // 00017 // 2. Redistributions in binary form must reproduce the above copyright 00018 // notice, this list of conditions and the following disclaimer in the 00019 // documentation and/or other materials provided with the distribution. 00020 // 00021 // 3. Neither the name of the Corporation nor the names of the 00022 // contributors may be used to endorse or promote products derived from 00023 // this software without specific prior written permission. 00024 // 00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY 00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE 00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00036 // 00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00038 // 00039 // *********************************************************************** 00040 // @HEADER 00041 00042 #ifndef TEUCHOS_STANDARD_PARAMETER_ENTRY_VALIDATORS_H 00043 #define TEUCHOS_STANDARD_PARAMETER_ENTRY_VALIDATORS_H 00044 00045 #include "Teuchos_ParameterEntryValidator.hpp" 00046 #include "Teuchos_ParameterList.hpp" 00047 #include "Teuchos_ParameterListExceptions.hpp" 00048 #include "Teuchos_VerbosityLevel.hpp" 00049 #include "Teuchos_TwoDArray.hpp" 00050 #include "Teuchos_Assert.hpp" 00051 #include "Teuchos_StrUtils.hpp" 00052 #include "Teuchos_TypeNameTraits.hpp" 00053 #include "Teuchos_DummyObjectGetter.hpp" 00054 00055 #include <locale> 00056 00057 00058 namespace Teuchos { 00059 00087 template<class IntegralType> 00088 class StringToIntegralParameterEntryValidator : public ParameterEntryValidator { 00089 public: 00092 00110 StringToIntegralParameterEntryValidator (const ArrayView<const std::string>& strings, 00111 const std::string& defaultParameterName, 00112 const bool caseSensitive = true); 00113 00137 StringToIntegralParameterEntryValidator (const ArrayView<const std::string>& strings, 00138 const ArrayView<const IntegralType>& integralValues, 00139 std::string const& defaultParameterName, 00140 const bool caseSensitive = true); 00141 00171 StringToIntegralParameterEntryValidator (const ArrayView<const std::string>& strings, 00172 const ArrayView<const std::string>& stringsDocs, 00173 const ArrayView<const IntegralType>& integralValues, 00174 const std::string& defaultParameterName, 00175 const bool caseSensitive = true); 00177 00179 00190 IntegralType getIntegralValue( 00191 const std::string &str, const std::string ¶mName = "", 00192 const std::string &sublistName = "" 00193 ) const; 00194 00210 IntegralType 00211 getIntegralValue (const ParameterEntry &entry, 00212 const std::string ¶mName = "", 00213 const std::string &sublistName = "", 00214 const bool activeQuery = true) const; 00215 00231 std::string 00232 getStringValue (const ParameterEntry &entry, 00233 const std::string ¶mName = "", 00234 const std::string &sublistName = "", 00235 const bool activeQuery = true) const; 00236 00243 IntegralType 00244 getIntegralValue (ParameterList& paramList, 00245 const std::string& paramName, 00246 const std::string& defaultValue) const; 00247 00251 std::string getStringValue( 00252 ParameterList ¶mList, const std::string ¶mName, 00253 const std::string &defaultValue 00254 ) const; 00255 00261 ValidStringsList getStringDocs() const; 00262 00267 const std::string& getDefaultParameterName() const; 00268 00280 std::string validateString( 00281 const std::string &str, const std::string ¶mName = "", 00282 const std::string &sublistName = "" 00283 ) const; 00284 00289 bool isCaseSensitive () const { 00290 return caseSensitive_; 00291 } 00292 00294 00296 00298 const std::string getXMLTypeName() const; 00299 00301 void printDoc( 00302 std::string const& docString, 00303 std::ostream & out 00304 ) const; 00305 00307 ValidStringsList 00308 validStringValues() const; 00309 00311 void validate( 00312 ParameterEntry const& entry, 00313 std::string const& paramName, 00314 std::string const& sublistName 00315 ) const; 00316 00318 00319 private: 00320 std::string defaultParameterName_; 00321 std::string validValues_; 00322 ValidStringsList validStringValues_; 00323 ValidStringsList validStringValuesDocs_; 00324 00325 typedef std::map<std::string,IntegralType> map_t; 00326 map_t map_; 00327 00328 const bool caseSensitive_; 00329 00330 void setValidValues( 00331 ArrayView<const std::string> const& strings, 00332 ArrayView<const std::string> const* stringsDocs = NULL 00333 ); 00334 00335 // Not defined and not to be called. 00336 StringToIntegralParameterEntryValidator(); 00337 00339 static std::string upperCase (const std::string s) { 00340 std::string s_upper = s; 00341 std::transform (s_upper.begin (), s_upper.end (), s_upper.begin (), ::toupper); 00342 return s_upper; 00343 } 00344 }; 00345 00346 00351 template<class IntegralType> 00352 RCP<StringToIntegralParameterEntryValidator<IntegralType> > 00353 stringToIntegralParameterEntryValidator( 00354 ArrayView<const std::string> const& strings, 00355 std::string const& defaultParameterName 00356 ); 00357 00358 00363 template<class IntegralType> 00364 RCP<StringToIntegralParameterEntryValidator<IntegralType> > 00365 stringToIntegralParameterEntryValidator( 00366 ArrayView<const std::string> const& strings, 00367 std::string const& defaultParameterName, 00368 const bool caseSensitive 00369 ); 00370 00371 00376 template<class IntegralType> 00377 RCP<StringToIntegralParameterEntryValidator<IntegralType> > 00378 stringToIntegralParameterEntryValidator( 00379 ArrayView<const std::string> const& strings, 00380 ArrayView<const IntegralType> const& integralValues, 00381 std::string const& defaultParameterName 00382 ); 00383 00384 00389 template<class IntegralType> 00390 RCP<StringToIntegralParameterEntryValidator<IntegralType> > 00391 stringToIntegralParameterEntryValidator( 00392 ArrayView<const std::string> const& strings, 00393 ArrayView<const IntegralType> const& integralValues, 00394 std::string const& defaultParameterName, 00395 const bool caseSensitive 00396 ); 00397 00398 00403 template<class IntegralType> 00404 RCP<StringToIntegralParameterEntryValidator<IntegralType> > 00405 stringToIntegralParameterEntryValidator( 00406 ArrayView<const std::string> const& strings, 00407 ArrayView<const std::string> const& stringsDocs, 00408 ArrayView<const IntegralType> const& integralValues, 00409 std::string const& defaultParameterName 00410 ); 00411 00412 00417 template<class IntegralType> 00418 RCP<StringToIntegralParameterEntryValidator<IntegralType> > 00419 stringToIntegralParameterEntryValidator( 00420 ArrayView<const std::string> const& strings, 00421 ArrayView<const std::string> const& stringsDocs, 00422 ArrayView<const IntegralType> const& integralValues, 00423 std::string const& defaultParameterName, 00424 const bool caseSensitive 00425 ); 00426 00427 00438 template<class IntegralType> 00439 void setStringToIntegralParameter( 00440 std::string const& paramName, 00441 std::string const& defaultValue, 00442 std::string const& docString, 00443 ArrayView<const std::string> const& strings, 00444 ParameterList * paramList 00445 ); 00446 00447 00459 template<class IntegralType> 00460 void setStringToIntegralParameter( 00461 std::string const& paramName, 00462 std::string const& defaultValue, 00463 std::string const& docString, 00464 ArrayView<const std::string> const& strings, 00465 ArrayView<const IntegralType> const& integralValues, 00466 ParameterList * paramList 00467 ); 00468 00469 00481 template<class IntegralType> 00482 void setStringToIntegralParameter( 00483 std::string const& paramName, 00484 std::string const& defaultValue, 00485 std::string const& docString, 00486 ArrayView<const std::string> const& strings, 00487 ArrayView<const std::string> const& stringsDocs, 00488 ArrayView<const IntegralType> const& integralValues, 00489 ParameterList * paramList 00490 ); 00491 00492 00503 template<class IntegralType> 00504 IntegralType getIntegralValue( 00505 ParameterList const& paramList, std::string const& paramName 00506 ); 00507 00508 00520 template<class IntegralType> 00521 std::string getStringValue( 00522 ParameterList const& paramList, std::string const& paramName 00523 ); 00524 00525 00531 template<class IntegralType> 00532 RCP<const StringToIntegralParameterEntryValidator<IntegralType> > 00533 getStringToIntegralParameterEntryValidator( 00534 ParameterEntry const& entry, ParameterList const& paramList, 00535 std::string const& paramName 00536 ); 00537 00538 00544 std::string getVerbosityLevelParameterValueName( 00545 const EVerbosityLevel verbLevel 00546 ); 00547 00548 00553 RCP<StringToIntegralParameterEntryValidator<EVerbosityLevel> > 00554 verbosityLevelParameterEntryValidator(std::string const& defaultParameterName); 00555 00561 template<class IntegralType> 00562 class DummyObjectGetter<StringToIntegralParameterEntryValidator<IntegralType> >{ 00563 00564 public: 00565 00568 00572 static RCP<StringToIntegralParameterEntryValidator<IntegralType> > 00573 getDummyObject(); 00574 00576 }; 00577 00578 template<class IntegralType> 00579 RCP<StringToIntegralParameterEntryValidator<IntegralType> > 00580 DummyObjectGetter<StringToIntegralParameterEntryValidator<IntegralType> >::getDummyObject() 00581 { 00582 return stringToIntegralParameterEntryValidator<IntegralType>( 00583 tuple<std::string>(""), tuple<std::string>(""), 00584 tuple<IntegralType>((IntegralType)1), ""); 00585 } 00586 00587 00588 00602 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT AnyNumberParameterEntryValidator 00603 : public ParameterEntryValidator 00604 { 00605 public: 00606 00609 00611 enum EPreferredType { PREFER_INT, PREFER_DOUBLE, PREFER_STRING }; 00612 00613 00615 class AcceptedTypes { 00616 public: 00618 AcceptedTypes( bool allowAllTypesByDefault = true ) 00619 :allowInt_(allowAllTypesByDefault),allowDouble_(allowAllTypesByDefault), 00620 allowString_(allowAllTypesByDefault) 00621 {} 00623 AcceptedTypes& allowInt( bool _allowInt ) 00624 { allowInt_ = _allowInt; return *this; } 00626 AcceptedTypes& allowDouble( bool _allowDouble ) 00627 { allowDouble_ = _allowDouble; return *this; } 00629 AcceptedTypes& allowString( bool _allowString ) 00630 { allowString_ = _allowString; return *this; } 00632 bool allowInt() const { return allowInt_; } 00634 bool allowDouble() const { return allowDouble_; } 00636 bool allowString() const { return allowString_; } 00637 private: 00638 bool allowInt_; 00639 bool allowDouble_; 00640 bool allowString_; 00641 }; 00642 00644 00647 00651 AnyNumberParameterEntryValidator(); 00652 00663 AnyNumberParameterEntryValidator( 00664 EPreferredType const preferredType, 00665 AcceptedTypes const& acceptedTypes 00666 ); 00667 00669 00672 00674 int getInt( 00675 const ParameterEntry &entry, const std::string ¶mName = "", 00676 const std::string &sublistName = "", const bool activeQuery = true 00677 ) const; 00678 00680 double getDouble( 00681 const ParameterEntry &entry, const std::string ¶mName = "", 00682 const std::string &sublistName = "", const bool activeQuery = true 00683 ) const; 00684 00686 std::string getString( 00687 const ParameterEntry &entry, const std::string ¶mName = "", 00688 const std::string &sublistName = "", const bool activeQuery = true 00689 ) const; 00690 00694 int getInt( 00695 ParameterList ¶mList, const std::string ¶mName, 00696 const int defaultValue 00697 ) const; 00698 00702 double getDouble( 00703 ParameterList ¶mList, const std::string ¶mName, 00704 const double defaultValue 00705 ) const; 00706 00710 std::string getString( 00711 ParameterList ¶mList, const std::string ¶mName, 00712 const std::string &defaultValue 00713 ) const; 00714 00717 bool isDoubleAllowed() const; 00718 00721 bool isIntAllowed() const; 00722 00725 bool isStringAllowed() const; 00726 00729 EPreferredType getPreferredType() const; 00730 00732 static const std::string& getPrefferedTypeString (EPreferredType enumValue) 00733 { 00734 switch (enumValue) { 00735 case PREFER_INT: 00736 return getIntEnumString (); 00737 case PREFER_DOUBLE: 00738 return getDoubleEnumString (); 00739 case PREFER_STRING: 00740 return getStringEnumString (); 00741 default: 00742 const std::string typeString (toString (enumValue)); 00743 throw std::runtime_error("Cannot convert enumValue: " + typeString + " to a string"); 00744 } 00745 } 00746 00748 static EPreferredType getPrefferedTypeStringEnum (const std::string& enumString) 00749 { 00750 if (enumString == getIntEnumString ()) { 00751 return PREFER_INT; 00752 } 00753 else if (enumString == getDoubleEnumString ()) { 00754 return PREFER_DOUBLE; 00755 } 00756 else if (enumString == getStringEnumString ()) { 00757 return PREFER_STRING; 00758 } 00759 else { 00760 throw std::runtime_error ("Cannot convert enumString: " + enumString + " to an enum"); 00761 } 00762 } 00763 00765 00768 00770 const std::string getXMLTypeName() const; 00771 00773 void printDoc( 00774 std::string const& docString, 00775 std::ostream & out 00776 ) const; 00777 00779 ValidStringsList 00780 validStringValues() const; 00781 00783 void validate( 00784 ParameterEntry const& entry, 00785 std::string const& paramName, 00786 std::string const& sublistName 00787 ) const; 00788 00790 void validateAndModify( 00791 std::string const& paramName, 00792 std::string const& sublistName, 00793 ParameterEntry * entry 00794 ) const; 00795 00796 00798 00799 private: 00800 00801 // //////////////////////////// 00802 // Private data members 00803 00804 EPreferredType preferredType_; 00805 std::string acceptedTypesString_; 00806 00807 //use pragmas to disable some false-positive warnings for windows sharedlibs export 00808 #ifdef _MSC_VER 00809 #pragma warning(push) 00810 #pragma warning(disable:4251) 00811 #endif 00812 const AcceptedTypes acceptedTypes_; 00813 #ifdef _MSC_VER 00814 #pragma warning(pop) 00815 #endif 00816 00817 // //////////////////////////// 00818 // Private member functions 00819 00820 /* \brief Gets the string representing the "int" preferred type enum */ 00821 static const std::string& getIntEnumString(){ 00822 static const std::string intEnumString_ = TypeNameTraits<int>::name(); 00823 return intEnumString_; 00824 } 00825 00826 /* \brief Gets the string representing the "double" preferred type enum */ 00827 static const std::string& getDoubleEnumString(){ 00828 static const std::string doubleEnumString_ = TypeNameTraits<double>::name(); 00829 return doubleEnumString_; 00830 } 00831 00832 /* \brief Gets the string representing the "string" preferred type enum */ 00833 static const std::string& getStringEnumString(){ 00834 static const std::string stringEnumString_ = TypeNameTraits<std::string>::name(); 00835 return stringEnumString_; 00836 } 00837 00838 00839 void finishInitialization(); 00840 00841 void throwTypeError( 00842 ParameterEntry const& entry, 00843 std::string const& paramName, 00844 std::string const& sublistName 00845 ) const; 00846 00847 }; 00848 00849 00850 // Nonmember helper functions 00851 00852 00857 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT RCP<AnyNumberParameterEntryValidator> 00858 anyNumberParameterEntryValidator(); 00859 00860 00865 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT RCP<AnyNumberParameterEntryValidator> 00866 anyNumberParameterEntryValidator( 00867 AnyNumberParameterEntryValidator::EPreferredType const preferredType, 00868 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes 00869 ); 00870 00876 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setIntParameter( 00877 std::string const& paramName, 00878 int const value, std::string const& docString, 00879 ParameterList *paramList, 00880 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes 00881 = AnyNumberParameterEntryValidator::AcceptedTypes() 00882 ); 00883 00884 00890 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setDoubleParameter( 00891 std::string const& paramName, 00892 double const& value, std::string const& docString, 00893 ParameterList *paramList, 00894 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes 00895 = AnyNumberParameterEntryValidator::AcceptedTypes() 00896 ); 00897 00898 00904 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void setNumericStringParameter( 00905 std::string const& paramName, 00906 std::string const& value, std::string const& docString, 00907 ParameterList *paramList, 00908 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes 00909 = AnyNumberParameterEntryValidator::AcceptedTypes() 00910 ); 00911 00912 00927 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT int getIntParameter( 00928 ParameterList const& paramList, std::string const& paramName 00929 ); 00930 00931 00946 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT double getDoubleParameter( 00947 ParameterList const& paramList, 00948 std::string const& paramName 00949 ); 00950 00951 00967 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT std::string getNumericStringParameter( 00968 ParameterList const& paramList, 00969 std::string const& paramName 00970 ); 00971 00977 template<> 00978 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT DummyObjectGetter<AnyNumberParameterEntryValidator>{ 00979 00980 public: 00981 00984 00988 static RCP<AnyNumberParameterEntryValidator > getDummyObject(); 00989 00991 00992 }; 00993 00994 00999 template <class T> 01000 struct UndefinedEnhancedNumberTraits{ 01002 static inline T notDefined() { 01003 return T::this_type_is_missing_a_specialization(); 01004 } 01005 }; 01006 01007 01028 template <class T> 01029 class EnhancedNumberTraits{ 01030 public: 01031 01033 static inline T min() 01034 { return UndefinedEnhancedNumberTraits<T>::notDefined(); } 01035 01037 static inline T max() 01038 { return UndefinedEnhancedNumberTraits<T>::notDefined(); } 01039 01042 static inline T defaultStep() 01043 { return UndefinedEnhancedNumberTraits<T>::notDefined(); } 01044 01047 static inline unsigned short defaultPrecision() 01048 { return UndefinedEnhancedNumberTraits<T>::notDefined(); } 01049 01050 }; 01051 01052 01053 template<> 01054 class EnhancedNumberTraits<short int>{ 01055 public: 01056 static inline short int min() { return std::numeric_limits<short int>::min(); } 01057 static inline short int max() { return std::numeric_limits<short int>::max(); } 01058 static inline short int defaultStep() { return 1; } 01059 static inline unsigned short defaultPrecision() { return 0; } 01060 }; 01061 01062 01063 template<> 01064 class EnhancedNumberTraits<short unsigned int>{ 01065 public: 01066 static inline short unsigned int min() { return std::numeric_limits<short unsigned int>::min(); } 01067 static inline short unsigned int max() { return std::numeric_limits<short unsigned int>::max(); } 01068 static inline short unsigned int defaultStep() { return 1; } 01069 static inline unsigned short defaultPrecision() { return 0; } 01070 }; 01071 01072 01073 template<> 01074 class EnhancedNumberTraits<int>{ 01075 public: 01076 static inline int min() { return std::numeric_limits<int>::min(); } 01077 static inline int max() { return std::numeric_limits<int>::max(); } 01078 static inline int defaultStep() { return 1; } 01079 static inline unsigned short defaultPrecision() { return 0; } 01080 }; 01081 01082 01083 template<> 01084 class EnhancedNumberTraits<unsigned int>{ 01085 public: 01086 static inline unsigned int min() { return std::numeric_limits<unsigned int>::min(); } 01087 static inline unsigned int max() { return std::numeric_limits<unsigned int>::max(); } 01088 static inline unsigned int defaultStep() { return 1; } 01089 static inline unsigned short defaultPrecision() { return 0; } 01090 }; 01091 01092 01093 template<> 01094 class EnhancedNumberTraits<long int>{ 01095 public: 01096 static inline long int min() { return std::numeric_limits<long int>::min(); } 01097 static inline long int max() { return std::numeric_limits<long int>::max(); } 01098 static inline long int defaultStep() { return 1; } 01099 static inline unsigned short defaultPrecision() { return 0; } 01100 }; 01101 01102 01103 template<> 01104 class EnhancedNumberTraits<long unsigned int>{ 01105 public: 01106 static inline long unsigned int min() { return std::numeric_limits<long unsigned int>::min(); } 01107 static inline long unsigned int max() { return std::numeric_limits<long unsigned int>::max(); } 01108 static inline long unsigned int defaultStep() { return 1; } 01109 static inline unsigned short defaultPrecision() { return 0; } 01110 }; 01111 01112 01113 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 01114 01115 01116 template<> 01117 class EnhancedNumberTraits<long long int>{ 01118 public: 01119 static inline long long int min() { return std::numeric_limits<long long int>::min(); } 01120 static inline long long int max() { return std::numeric_limits<long long int>::max(); } 01121 static inline long long int defaultStep() { return 1; } 01122 static inline unsigned short defaultPrecision() { return 0; } 01123 }; 01124 01125 01126 template<> 01127 class EnhancedNumberTraits<long long unsigned int>{ 01128 public: 01129 static inline long long unsigned int min() { return std::numeric_limits<long long unsigned int>::min(); } 01130 static inline long long unsigned int max() { return std::numeric_limits<long long unsigned int>::max(); } 01131 static inline long long unsigned int defaultStep() { return 1; } 01132 static inline unsigned short defaultPrecision() { return 0; } 01133 }; 01134 01135 01136 #endif // HAVE_TEUCHOS_LONG_LONG_INT 01137 01138 01139 template<> 01140 class EnhancedNumberTraits<double>{ 01141 public: 01142 static inline double min() { return -std::numeric_limits<double>::max(); } 01143 static inline double max() { return std::numeric_limits<double>::max(); } 01144 static inline double defaultStep() { return 1; } 01145 static inline unsigned short defaultPrecision() { return 100; } 01146 }; 01147 01148 01149 template<> 01150 class EnhancedNumberTraits<float>{ 01151 public: 01152 static inline float min() { return -std::numeric_limits<float>::max(); } 01153 static inline float max() { return std::numeric_limits<float>::max(); } 01154 static inline float defaultStep() { return 1; } 01155 static inline unsigned short defaultPrecision() { return 100; } 01156 }; 01157 01158 01164 template <class T> 01165 class EnhancedNumberValidator : public ParameterEntryValidator{ 01166 01167 public: 01168 01171 01186 EnhancedNumberValidator( 01187 T min, 01188 T max, 01189 T step=EnhancedNumberTraits<T>::defaultStep(), 01190 unsigned short precision=EnhancedNumberTraits<T>::defaultPrecision()): 01191 ParameterEntryValidator(), 01192 minVal(min), maxVal(max), step_(step), precision_(precision), 01193 containsMin(true), containsMax(true){} 01194 01198 EnhancedNumberValidator(): 01199 ParameterEntryValidator(), 01200 minVal(EnhancedNumberTraits<T>::min()), 01201 maxVal(EnhancedNumberTraits<T>::max()), 01202 step_(EnhancedNumberTraits<T>::defaultStep()), 01203 precision_(EnhancedNumberTraits<T>::defaultPrecision()), 01204 containsMin(false), 01205 containsMax(false){} 01206 01208 01210 01211 01216 void setMin(T min){ 01217 minVal = min; 01218 containsMin = true; 01219 } 01220 01225 void setMax(T max){ 01226 maxVal = max; 01227 containsMax = true; 01228 } 01229 01234 void setStep(T step){ 01235 step_ = step; 01236 } 01237 01242 void setPrecision(unsigned short precision){ 01243 precision_ = precision; 01244 } 01245 01247 01250 01255 T getMin() const{ 01256 return minVal; 01257 } 01258 01263 T getMax() const{ 01264 return maxVal; 01265 } 01266 01271 T getStep() const{ 01272 return step_; 01273 } 01274 01279 unsigned short getPrecision() const{ 01280 return precision_; 01281 } 01282 01284 01286 01287 01292 bool hasMin() const{ 01293 return containsMin; 01294 } 01295 01300 bool hasMax() const{ 01301 return containsMax; 01302 } 01303 01305 01308 01310 ValidStringsList validStringValues() const{ 01311 return null; 01312 } 01313 01315 void validate(ParameterEntry const &entry, std::string const ¶mName, 01316 std::string const &sublistName) const; 01317 01319 const std::string getXMLTypeName() const{ 01320 return "EnhancedNumberValidator(" + TypeNameTraits<T>::name()+ ")"; 01321 } 01322 01324 void printDoc(std::string const &docString, std::ostream &out) const{ 01325 StrUtils::printLines(out,"# ",docString); 01326 out << "#\tValidator Used: " << std::endl; 01327 out << "#\t\tNumber Validator" << std::endl; 01328 out << "#\t\tType: " << Teuchos::TypeNameTraits<T>::name() << 01329 std::endl; 01330 out << "#\t\tMin (inclusive): " << minVal << std::endl; 01331 out << "#\t\tMax (inclusive): " << maxVal << std::endl; 01332 } 01333 01335 01336 private: 01337 01340 01343 T minVal; 01344 01347 T maxVal; 01348 01351 T step_; 01352 01356 unsigned short precision_; 01357 01360 bool containsMin; 01361 01364 bool containsMax; 01365 01367 01368 }; 01369 01370 template<class T> 01371 void EnhancedNumberValidator<T>::validate(ParameterEntry const &entry, std::string const ¶mName, 01372 std::string const &sublistName) const 01373 { 01374 any anyValue = entry.getAny(true); 01375 const std::string &entryName = entry.getAny(false).typeName(); 01376 01377 TEUCHOS_TEST_FOR_EXCEPTION(anyValue.type() != typeid(T), 01378 Exceptions::InvalidParameterType, 01379 "The \"" << paramName << "\"" << 01380 " parameter in the \"" << sublistName << 01381 "\" sublist is has an error." << std::endl << std::endl << 01382 "Error: The value that you entered was the wrong type." << std::endl << 01383 "Parameter: " << paramName << std::endl << 01384 "Type specified: " << entryName << std::endl << 01385 "Type accepted: " << Teuchos::TypeNameTraits<T>::name() << std::endl); 01386 01387 bool isValueInRange; 01388 any_cast<T>(anyValue) >= minVal && any_cast<T>(anyValue) <= maxVal 01389 ? isValueInRange = true : isValueInRange=false; 01390 TEUCHOS_TEST_FOR_EXCEPTION(!(isValueInRange), 01391 Exceptions::InvalidParameterValue, 01392 "The \"" << paramName << "\"" << 01393 " parameter in the \"" << sublistName << 01394 "\" sublist is has an error." << std::endl << std::endl << 01395 "Error: The value that was entered doesn't fall with in " << 01396 "the range set by the validator" << std::endl << 01397 "Parameter: " << paramName << std::endl << 01398 "Min: " << minVal << std::endl << 01399 "Max: " << maxVal << std::endl << 01400 "Value entered: " << 01401 (any_cast<T>(anyValue)) << std::endl << std::endl); 01402 } 01403 01409 template<class T> 01410 class DummyObjectGetter<EnhancedNumberValidator<T> >{ 01411 01412 public: 01413 01416 01420 static RCP<EnhancedNumberValidator<T> > getDummyObject(); 01421 01423 }; 01424 01425 template<class T> 01426 RCP<EnhancedNumberValidator<T> > 01427 DummyObjectGetter<EnhancedNumberValidator<T> >::getDummyObject() 01428 { 01429 return rcp(new EnhancedNumberValidator<T>); 01430 } 01431 01440 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT FileNameValidator : public ParameterEntryValidator { 01441 01442 public: 01443 01446 01449 static bool mustAlreadyExistDefault() { return false; } 01450 01452 01455 01461 FileNameValidator(bool mustAlreadyExist = mustAlreadyExistDefault()); 01462 01464 01466 01467 01473 bool fileMustExist() const; 01474 01476 01477 01488 bool fileEmptyNameOK() const; 01489 01491 01493 01494 01503 bool setFileMustExist(bool shouldFileExist); 01504 01513 bool setFileEmptyNameOK(bool isEmptyNameOK); 01514 01516 01519 01521 ValidStringsList validStringValues() const; 01522 01524 void validate( 01525 ParameterEntry const &entry, 01526 std::string const ¶mName, 01527 std::string const &sublistName) const; 01528 01530 const std::string getXMLTypeName() const; 01531 01533 void printDoc(std::string const &docString, std::ostream &out) const; 01534 01536 01537 private: 01538 01541 01545 bool mustAlreadyExist_; 01546 bool EmptyNameOK_; 01547 01549 01550 }; 01551 01557 template<> 01558 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT DummyObjectGetter<FileNameValidator>{ 01559 01560 public: 01561 01564 01568 static RCP<FileNameValidator> getDummyObject(); 01569 01571 01572 }; 01573 01581 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT StringValidator : public ParameterEntryValidator { 01582 01583 public: 01584 01587 01590 StringValidator(); 01591 01596 StringValidator(const Teuchos::Array<std::string> &validStrings); 01597 01599 01601 01602 01610 ValidStringsList setValidStrings( 01611 const Teuchos::Array<std::string> &validStrings); 01612 01614 01617 01619 ValidStringsList validStringValues() const; 01620 01622 void validate(ParameterEntry const &entry, std::string const ¶mName, 01623 std::string const &sublistName) const; 01624 01626 const std::string getXMLTypeName() const; 01627 01629 void printDoc(std::string const &docString, std::ostream &out) const; 01630 01632 01633 private: 01634 01637 01640 ValidStringsList validStrings_; 01641 01643 01644 }; 01645 01651 template<> 01652 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT DummyObjectGetter<StringValidator>{ 01653 01654 public: 01655 01658 01662 static RCP<StringValidator> getDummyObject(); 01663 01665 01666 }; 01667 01668 01672 template<class ValidatorType, class EntryType> 01673 class AbstractArrayValidator : public ParameterEntryValidator { 01674 01675 public: 01676 01679 01686 AbstractArrayValidator(RCP<const ValidatorType> prototypeValidator): 01687 ParameterEntryValidator(), 01688 prototypeValidator_(prototypeValidator){} 01689 01691 01694 01696 RCP<const ValidatorType> getPrototype() const{ 01697 return prototypeValidator_; 01698 } 01699 01701 01704 01706 ValidStringsList validStringValues() const { 01707 return prototypeValidator_->validStringValues(); 01708 } 01709 01711 01712 private: 01713 01716 01719 RCP<const ValidatorType> prototypeValidator_; 01720 01722 AbstractArrayValidator<ValidatorType, EntryType>(); 01723 01725 01726 }; 01727 01740 template<class ValidatorType, class EntryType> 01741 class TwoDArrayValidator : public AbstractArrayValidator<ValidatorType, EntryType>{ 01742 public: 01745 01752 TwoDArrayValidator(RCP<const ValidatorType> prototypeValidator): 01753 AbstractArrayValidator<ValidatorType, EntryType>(prototypeValidator){} 01754 01756 01759 01761 virtual void validate(ParameterEntry const &entry, std::string const ¶mName, 01762 std::string const &sublistName) const; 01763 01765 const std::string getXMLTypeName() const{ 01766 return "TwoDArrayValidator(" + 01767 this->getPrototype()->getXMLTypeName() + ", " + 01768 TypeNameTraits<EntryType>::name() + ")"; 01769 } 01770 01772 virtual void printDoc(std::string const &docString, std::ostream &out) const 01773 { 01774 StrUtils::printLines(out,"# ",docString); 01775 std::string toPrint; 01776 toPrint += "TwoDArrayValidator:\n"; 01777 toPrint += "Prototype Validator:\n"; 01778 this->getPrototype()->printDoc(toPrint, out); 01779 } 01780 01782 01783 }; 01784 01785 template<class ValidatorType, class EntryType> 01786 void TwoDArrayValidator<ValidatorType, EntryType>::validate(ParameterEntry const &entry, std::string const ¶mName, 01787 std::string const &sublistName) const 01788 { 01789 any anyValue = entry.getAny(true); 01790 const std::string &entryName = entry.getAny(false).typeName(); 01791 TEUCHOS_TEST_FOR_EXCEPTION(anyValue.type() != typeid(TwoDArray<EntryType>), 01792 Exceptions::InvalidParameterType, 01793 "The \"" << paramName << "\"" << 01794 " parameter in the \"" << sublistName << 01795 "\" sublist is has an error." << std::endl << std::endl << 01796 "Error: The value you entered was the wrong type." << std::endl << 01797 "Parameter: " << paramName << std::endl << 01798 "Type specified: " << entryName << std::endl << 01799 "Type accepted: " << TypeNameTraits<TwoDArray<EntryType> >::name() << 01800 std::endl << std::endl); 01801 01802 TwoDArray<EntryType> extracted = 01803 getValue<Teuchos::TwoDArray<EntryType> >(entry); 01804 RCP<const ParameterEntryValidator> prototype = this->getPrototype(); 01805 for(int i = 0; i<extracted.getNumRows(); ++i){ 01806 for(int j = 0; j<extracted.getNumCols(); ++j){ 01807 ParameterEntry dummyParameter; 01808 dummyParameter.setValue(extracted(i,j)); 01809 try{ 01810 prototype->validate( 01811 dummyParameter, paramName, sublistName); 01812 } 01813 catch(Exceptions::InvalidParameterValue& e){ 01814 std::stringstream oss; 01815 oss << "TwoDArray Validator Exception:" << std::endl << 01816 "Bad Index: (" << i << "," << j << ")" << std::endl << e.what(); 01817 throw Exceptions::InvalidParameterValue(oss.str()); 01818 } 01819 } 01820 } 01821 } 01822 01823 01829 template<class ValidatorType, class EntryType> 01830 class DummyObjectGetter<TwoDArrayValidator<ValidatorType, EntryType> >{ 01831 01832 public: 01833 01836 01840 static RCP<TwoDArrayValidator<ValidatorType, EntryType> > getDummyObject(); 01841 01843 01844 }; 01845 01846 template<class ValidatorType, class EntryType> 01847 RCP<TwoDArrayValidator<ValidatorType, EntryType> > 01848 DummyObjectGetter<TwoDArrayValidator<ValidatorType, EntryType> >::getDummyObject() 01849 { 01850 return rcp(new TwoDArrayValidator<ValidatorType, EntryType>( 01851 DummyObjectGetter<ValidatorType>::getDummyObject())); 01852 } 01853 01857 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT TwoDArrayStringValidator : 01858 public TwoDArrayValidator<StringValidator, std::string>{ 01859 01860 public: 01861 01864 01866 TwoDArrayStringValidator(RCP<const StringValidator> prototypeValidator): 01867 TwoDArrayValidator<StringValidator, std::string>(prototypeValidator){} 01868 01870 01871 }; 01872 01873 01878 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT TwoDArrayFileNameValidator : 01879 public TwoDArrayValidator<FileNameValidator, std::string>{ 01880 01881 public: 01882 01885 01887 TwoDArrayFileNameValidator(RCP<const FileNameValidator> prototypeValidator): 01888 TwoDArrayValidator<FileNameValidator, std::string>(prototypeValidator){} 01889 01891 01892 }; 01893 01894 01898 template<class T> 01899 class TwoDArrayNumberValidator : public TwoDArrayValidator<EnhancedNumberValidator<T>, T>{ 01900 public: 01903 01905 TwoDArrayNumberValidator( 01906 RCP<const EnhancedNumberValidator<T> > prototypeValidator): 01907 TwoDArrayValidator<EnhancedNumberValidator<T>, T>(prototypeValidator){} 01908 01910 01911 }; 01912 01913 01925 template<class ValidatorType, class EntryType> 01926 class ArrayValidator : public AbstractArrayValidator<ValidatorType, EntryType>{ 01927 01928 public: 01929 01932 01938 ArrayValidator(RCP<const ValidatorType> prototypeValidator): 01939 AbstractArrayValidator<ValidatorType, EntryType>(prototypeValidator){} 01940 01942 01945 01947 virtual void validate(ParameterEntry const &entry, std::string const ¶mName, 01948 std::string const &sublistName) const; 01949 01951 const std::string getXMLTypeName() const{ 01952 return "ArrayValidator(" + 01953 this->getPrototype()->getXMLTypeName() + ", " + 01954 TypeNameTraits<EntryType>::name() + ")"; 01955 } 01956 01958 virtual void printDoc(std::string const &docString, std::ostream &out) const 01959 { 01960 StrUtils::printLines(out,"# ",docString); 01961 std::string toPrint; 01962 toPrint += "ArrayValidator:\n"; 01963 toPrint += "Prototype Validator:\n"; 01964 this->getPrototype()->printDoc(toPrint, out); 01965 } 01966 01968 01969 }; 01970 01971 template<class ValidatorType, class EntryType> 01972 void ArrayValidator<ValidatorType, EntryType>::validate(ParameterEntry const &entry, std::string const ¶mName, 01973 std::string const &sublistName) const 01974 { 01975 any anyValue = entry.getAny(true); 01976 const std::string &entryName = entry.getAny(false).typeName(); 01977 TEUCHOS_TEST_FOR_EXCEPTION(anyValue.type() != typeid(Array<EntryType>), 01978 Exceptions::InvalidParameterType, 01979 "The \"" << paramName << "\"" << 01980 " parameter in the \"" << sublistName << 01981 "\" sublist is has an error." << std::endl << std::endl << 01982 "Error: The value you entered was the wrong type." << std::endl << 01983 "Parameter: " << paramName << std::endl << 01984 "Type specified: " << entryName << std::endl << 01985 "Type accepted: " << TypeNameTraits<Array<EntryType> >::name() << 01986 std::endl << std::endl); 01987 01988 Array<EntryType> extracted = 01989 getValue<Teuchos::Array<EntryType> >(entry); 01990 RCP<const ParameterEntryValidator> prototype = this->getPrototype(); 01991 for(int i = 0; i<extracted.size(); ++i){ 01992 ParameterEntry dummyParameter; 01993 dummyParameter.setValue(extracted[i]); 01994 try{ 01995 prototype->validate( 01996 dummyParameter, paramName, sublistName); 01997 } 01998 catch(Exceptions::InvalidParameterValue& e){ 01999 std::stringstream oss; 02000 oss << "Array Validator Exception:" << std::endl << 02001 "Bad Index: " << i << std::endl << e.what(); 02002 throw Exceptions::InvalidParameterValue(oss.str()); 02003 } 02004 } 02005 } 02006 02012 template<class ValidatorType, class EntryType> 02013 class DummyObjectGetter<ArrayValidator<ValidatorType, EntryType> >{ 02014 02015 public: 02016 02019 02023 static RCP<ArrayValidator<ValidatorType, EntryType> > getDummyObject(); 02024 02026 02027 }; 02028 02029 template<class ValidatorType, class EntryType> 02030 RCP<ArrayValidator<ValidatorType, EntryType> > 02031 DummyObjectGetter<ArrayValidator<ValidatorType, EntryType> >::getDummyObject() 02032 { 02033 return rcp(new ArrayValidator<ValidatorType, EntryType>( 02034 DummyObjectGetter<ValidatorType>::getDummyObject())); 02035 } 02036 02037 02046 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT ArrayStringValidator : 02047 public ArrayValidator<StringValidator, std::string>{ 02048 02049 public: 02050 02053 02055 ArrayStringValidator(RCP<const StringValidator> prototypeValidator): 02056 ArrayValidator<StringValidator, std::string>(prototypeValidator){} 02057 02059 02060 }; 02061 02062 02071 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT ArrayFileNameValidator : public ArrayValidator<FileNameValidator, std::string>{ 02072 02073 public: 02074 02077 02079 ArrayFileNameValidator(RCP<const FileNameValidator> prototypeValidator): 02080 ArrayValidator<FileNameValidator, std::string>(prototypeValidator){} 02081 02083 02084 }; 02085 02086 02094 template<class T> 02095 class ArrayNumberValidator : public ArrayValidator<EnhancedNumberValidator<T>, T>{ 02096 public: 02099 02101 ArrayNumberValidator( 02102 RCP<const EnhancedNumberValidator<T> > prototypeValidator): 02103 ArrayValidator<EnhancedNumberValidator<T>, T>(prototypeValidator){} 02104 02106 02107 }; 02108 02109 02110 02111 // /////////////////////////// 02112 // Implementations 02113 02114 02115 // 02116 // StringToIntegralParameterEntryValidator 02117 // 02118 02119 02120 // Constructors 02121 02122 02123 template<class IntegralType> 02124 StringToIntegralParameterEntryValidator<IntegralType>:: 02125 StringToIntegralParameterEntryValidator (ArrayView<const std::string> const& strings, 02126 std::string const& defaultParameterName, 02127 const bool caseSensitive) : 02128 ParameterEntryValidator (), 02129 defaultParameterName_ (defaultParameterName), 02130 caseSensitive_ (caseSensitive) 02131 { 02132 typedef typename map_t::value_type val_t; 02133 for (int i = 0; i < static_cast<int> (strings.size ()); ++i) { 02134 const bool unique = caseSensitive_ ? 02135 map_.insert (val_t (strings[i], static_cast<IntegralType> (i))).second : 02136 map_.insert (val_t (upperCase (strings[i]), static_cast<IntegralType> (i))).second; 02137 TEUCHOS_TEST_FOR_EXCEPTION( 02138 ! unique, std::logic_error, 02139 "For parameter \"" << defaultParameterName_ << "\": " 02140 "strings[" << i << "] = \"" << strings[i] << "\" is a duplicate."); 02141 } 02142 setValidValues (strings); 02143 } 02144 02145 02146 template<class IntegralType> 02147 StringToIntegralParameterEntryValidator<IntegralType>:: 02148 StringToIntegralParameterEntryValidator (ArrayView<const std::string> const& strings, 02149 ArrayView<const IntegralType> const& integralValues, 02150 std::string const& defaultParameterName, 02151 const bool caseSensitive) : 02152 ParameterEntryValidator (), 02153 defaultParameterName_ (defaultParameterName), 02154 caseSensitive_ (caseSensitive) 02155 { 02156 #ifdef TEUCHOS_DEBUG 02157 TEUCHOS_ASSERT_EQUALITY( strings.size(), integralValues.size() ); 02158 #endif 02159 TEUCHOS_TEST_FOR_EXCEPTION( 02160 strings.size() != integralValues.size(), 02161 std::logic_error, 02162 "The input arrays strings and integralValues must have the same length."); 02163 02164 typedef typename map_t::value_type val_t; 02165 for (int i = 0; i < static_cast<int> (strings.size ()); ++i) { 02166 const bool unique = caseSensitive_ ? 02167 map_.insert (val_t (strings[i], integralValues[i])).second : 02168 map_.insert (val_t (upperCase (strings[i]), integralValues[i])).second; 02169 02170 TEUCHOS_TEST_FOR_EXCEPTION( 02171 ! unique, std::logic_error, 02172 "For parameter \"" << defaultParameterName_ << "\": " 02173 "strings[" << i << "] = \"" << strings[i] << "\" is a duplicate."); 02174 } 02175 setValidValues (strings); 02176 } 02177 02178 template<class IntegralType> 02179 StringToIntegralParameterEntryValidator<IntegralType>:: 02180 StringToIntegralParameterEntryValidator (ArrayView<const std::string> const& strings, 02181 ArrayView<const std::string> const& stringsDocs, 02182 ArrayView<const IntegralType> const& integralValues, 02183 std::string const& defaultParameterName, 02184 const bool caseSensitive) : 02185 ParameterEntryValidator (), 02186 defaultParameterName_ (defaultParameterName), 02187 caseSensitive_ (caseSensitive) 02188 { 02189 #ifdef TEUCHOS_DEBUG 02190 TEUCHOS_ASSERT_EQUALITY( strings.size(), stringsDocs.size() ); 02191 TEUCHOS_ASSERT_EQUALITY( strings.size(), integralValues.size() ); 02192 #endif 02193 02194 TEUCHOS_TEST_FOR_EXCEPTION( 02195 strings.size() != integralValues.size(), 02196 std::logic_error, 02197 "The input arrays strings and integralValues must have the same length."); 02198 02199 TEUCHOS_TEST_FOR_EXCEPTION( 02200 strings.size() != stringsDocs.size(), 02201 std::logic_error, 02202 "The input arrays strings and stringsDocs must have the same length."); 02203 02204 typedef typename map_t::value_type val_t; 02205 for (int i = 0; i < static_cast<int> (strings.size ()); ++i) { 02206 const bool unique = caseSensitive_ ? 02207 map_.insert (val_t (strings[i], integralValues[i])).second : 02208 map_.insert (val_t (upperCase (strings[i]), integralValues[i])).second; 02209 TEUCHOS_TEST_FOR_EXCEPTION( 02210 ! unique, std::logic_error, 02211 "For parameter \"" << defaultParameterName_ << "\": " 02212 "strings[" << i << "] = \"" << strings[i] << "\" is a duplicate."); 02213 } 02214 setValidValues(strings,&stringsDocs); 02215 } 02216 02217 // Lookup functions 02218 02219 02220 template<class IntegralType> 02221 IntegralType 02222 StringToIntegralParameterEntryValidator<IntegralType>::getIntegralValue( 02223 const std::string &str, const std::string ¶mName 02224 ,const std::string &sublistName 02225 ) const 02226 { 02227 typename map_t::const_iterator itr = map_.find (caseSensitive_ ? str : upperCase (str)); 02228 TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG( 02229 itr == map_.end(), Exceptions::InvalidParameterValue 02230 ,"Error, the value \"" << str << "\" is not recognized for the parameter \"" 02231 << ( paramName.length() ? paramName : defaultParameterName_ ) << "\"" 02232 << "\nin the sublist \"" << sublistName << "\"." 02233 << "\n\nValid values include:" 02234 << "\n {\n" 02235 << validValues_ 02236 << " }" 02237 ); 02238 return (*itr).second; 02239 } 02240 02241 02242 template<class IntegralType> 02243 IntegralType 02244 StringToIntegralParameterEntryValidator<IntegralType>::getIntegralValue( 02245 const ParameterEntry &entry, const std::string ¶mName 02246 ,const std::string &sublistName, const bool activeQuery 02247 ) const 02248 { 02249 const bool validType = ( entry.getAny(activeQuery).type() == typeid(std::string) ); 02250 TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG( 02251 !validType, Exceptions::InvalidParameterType 02252 ,"Error, the parameter {paramName=\""<<(paramName.length()?paramName:defaultParameterName_) 02253 << "\",type=\""<<entry.getAny(activeQuery).typeName()<<"\"}" 02254 << "\nin the sublist \"" << sublistName << "\"" 02255 << "\nhas the wrong type." 02256 << "\n\nThe correct type is \"string\"!" 02257 ); 02258 const std::string 02259 &strValue = any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail! 02260 return getIntegralValue(strValue,paramName,sublistName); // This will validate the value and throw! 02261 } 02262 02263 02264 template<class IntegralType> 02265 std::string 02266 StringToIntegralParameterEntryValidator<IntegralType>::getStringValue( 02267 const ParameterEntry &entry, const std::string ¶mName 02268 ,const std::string &sublistName, const bool activeQuery 02269 ) const 02270 { 02271 // Validate the parameter's type and value 02272 this->getIntegralValue(entry,paramName,sublistName,activeQuery); 02273 // Return the std::string value which is now validated! 02274 return any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail! 02275 } 02276 02277 02278 template<class IntegralType> 02279 IntegralType 02280 StringToIntegralParameterEntryValidator<IntegralType>::getIntegralValue( 02281 ParameterList ¶mList, const std::string ¶mName 02282 ,const std::string &defaultValue 02283 ) const 02284 { 02285 const std::string& strValue = 02286 paramList.get (paramName, 02287 caseSensitive_ ? defaultValue : upperCase (defaultValue)); 02288 return getIntegralValue (strValue, paramName, paramList.name ()); 02289 } 02290 02291 02292 template<class IntegralType> 02293 std::string 02294 StringToIntegralParameterEntryValidator<IntegralType>::getStringValue( 02295 ParameterList ¶mList, const std::string ¶mName 02296 ,const std::string &defaultValue 02297 ) const 02298 { 02299 const std::string& strValue = 02300 paramList.get (paramName, 02301 caseSensitive_ ? defaultValue : upperCase (defaultValue)); 02302 getIntegralValue(strValue,paramName,paramList.name()); // Validate! 02303 return strValue; 02304 } 02305 02306 template<class IntegralType> 02307 ParameterEntryValidator::ValidStringsList 02308 StringToIntegralParameterEntryValidator<IntegralType>::getStringDocs() const 02309 { 02310 return validStringValuesDocs_; 02311 } 02312 02313 template<class IntegralType> 02314 const std::string& 02315 StringToIntegralParameterEntryValidator<IntegralType>::getDefaultParameterName() const 02316 { 02317 return defaultParameterName_; 02318 } 02319 02320 template<class IntegralType> 02321 std::string 02322 StringToIntegralParameterEntryValidator<IntegralType>::validateString( 02323 const std::string &str, const std::string ¶mName 02324 ,const std::string &sublistName 02325 ) const 02326 { 02327 getIntegralValue (caseSensitive_ ? str : upperCase (str), 02328 paramName, 02329 sublistName); // Validate! 02330 return str; 02331 } 02332 02333 02334 // Overridden from ParameterEntryValidator 02335 02336 template<class IntegralType> 02337 const std::string 02338 StringToIntegralParameterEntryValidator<IntegralType>::getXMLTypeName() const{ 02339 return "StringIntegralValidator(" + TypeNameTraits<IntegralType>::name () + ")"; 02340 } 02341 02342 template<class IntegralType> 02343 void StringToIntegralParameterEntryValidator<IntegralType>::printDoc( 02344 std::string const& docString 02345 ,std::ostream & out 02346 ) const 02347 { 02348 StrUtils::printLines(out,"# ",docString); 02349 out << "# Valid std::string values:\n"; 02350 out << "# {\n"; 02351 if(validStringValuesDocs_.get()) { 02352 for( int i = 0; i < static_cast<int>(validStringValues_->size()); ++i ) { 02353 out << "# \"" << (*validStringValues_)[i] << "\"\n"; 02354 StrUtils::printLines(out,"# ",(*validStringValuesDocs_)[i] ); 02355 } 02356 } 02357 else { 02358 StrUtils::printLines(out,"# ",validValues_); 02359 // Note: Above validValues_ has for initial spaces already so indent should 02360 // be correct! 02361 } 02362 out << "# }\n"; 02363 } 02364 02365 02366 template<class IntegralType> 02367 ParameterEntryValidator::ValidStringsList 02368 StringToIntegralParameterEntryValidator<IntegralType>::validStringValues() const 02369 { 02370 return validStringValues_; 02371 } 02372 02373 02374 template<class IntegralType> 02375 void StringToIntegralParameterEntryValidator<IntegralType>::validate( 02376 ParameterEntry const& entry 02377 ,std::string const& paramName 02378 ,std::string const& sublistName 02379 ) const 02380 { 02381 this->getIntegralValue (entry, paramName, sublistName, false); 02382 } 02383 02384 02385 // private 02386 02387 template<class IntegralType> 02388 void StringToIntegralParameterEntryValidator<IntegralType>::setValidValues( 02389 ArrayView<const std::string> const& strings 02390 ,ArrayView<const std::string> const* stringsDocs 02391 ) 02392 { 02393 if (caseSensitive_) { 02394 validStringValues_ = rcp (new Array<std::string> (strings)); 02395 } 02396 else { 02397 RCP<Array<std::string> > vals (new Array<std::string> (strings.size ())); 02398 for (Array<std::string>::size_type i = 0; i < strings.size (); ++i) { 02399 (*vals)[i] = upperCase (strings[i]); 02400 } 02401 validStringValues_ = rcp_const_cast<const Array<std::string> > (vals); 02402 } 02403 02404 if (stringsDocs) { 02405 validStringValuesDocs_ = rcp (new Array<std::string> (*stringsDocs)); 02406 } 02407 // Build the list of valid values in the same order as passed in by the client. 02408 std::ostringstream oss; 02409 for (int i = 0; i < static_cast<int> (strings.size()); ++i) { 02410 oss << " \"" << strings[i] << "\"\n"; 02411 } 02412 // Note: Above four spaces is designed for the error output above. 02413 validValues_ = oss.str(); 02414 } 02415 02416 02417 } // namespace Teuchos 02418 02419 02420 // 02421 // Nonmember function implementations for StringToIntegralParameterEntryValidator 02422 // 02423 02424 02425 template<class IntegralType> 02426 inline 02427 Teuchos::RCP<Teuchos::StringToIntegralParameterEntryValidator<IntegralType> > 02428 Teuchos::stringToIntegralParameterEntryValidator( 02429 ArrayView<const std::string> const& strings, 02430 std::string const& defaultParameterName 02431 ) 02432 { 02433 return rcp( 02434 new StringToIntegralParameterEntryValidator<IntegralType>( 02435 strings, defaultParameterName 02436 ) 02437 ); 02438 } 02439 02440 02441 template<class IntegralType> 02442 inline 02443 Teuchos::RCP<Teuchos::StringToIntegralParameterEntryValidator<IntegralType> > 02444 Teuchos::stringToIntegralParameterEntryValidator( 02445 ArrayView<const std::string> const& strings, 02446 std::string const& defaultParameterName, 02447 const bool caseSensitive 02448 ) 02449 { 02450 typedef StringToIntegralParameterEntryValidator<IntegralType> ret_type; 02451 return rcp (new ret_type (strings, defaultParameterName, caseSensitive)); 02452 } 02453 02454 02455 02456 template<class IntegralType> 02457 inline 02458 Teuchos::RCP<Teuchos::StringToIntegralParameterEntryValidator<IntegralType> > 02459 Teuchos::stringToIntegralParameterEntryValidator( 02460 ArrayView<const std::string> const& strings, 02461 ArrayView<const IntegralType> const& integralValues, 02462 std::string const& defaultParameterName 02463 ) 02464 { 02465 return rcp( 02466 new StringToIntegralParameterEntryValidator<IntegralType>( 02467 strings, integralValues, defaultParameterName 02468 ) 02469 ); 02470 } 02471 02472 02473 template<class IntegralType> 02474 inline 02475 Teuchos::RCP<Teuchos::StringToIntegralParameterEntryValidator<IntegralType> > 02476 Teuchos::stringToIntegralParameterEntryValidator( 02477 ArrayView<const std::string> const& strings, 02478 ArrayView<const IntegralType> const& integralValues, 02479 std::string const& defaultParameterName, 02480 const bool caseSensitive) 02481 { 02482 typedef StringToIntegralParameterEntryValidator<IntegralType> ret_type; 02483 return rcp (new ret_type (strings, integralValues, 02484 defaultParameterName, caseSensitive)); 02485 } 02486 02487 02488 template<class IntegralType> 02489 inline 02490 Teuchos::RCP< Teuchos::StringToIntegralParameterEntryValidator<IntegralType> > 02491 Teuchos::stringToIntegralParameterEntryValidator( 02492 ArrayView<const std::string> const& strings, 02493 ArrayView<const std::string> const& stringsDocs, 02494 ArrayView<const IntegralType> const& integralValues, 02495 std::string const& defaultParameterName 02496 ) 02497 { 02498 return rcp( 02499 new StringToIntegralParameterEntryValidator<IntegralType>( 02500 strings, stringsDocs, integralValues, defaultParameterName 02501 ) 02502 ); 02503 } 02504 02505 02506 template<class IntegralType> 02507 inline 02508 Teuchos::RCP< Teuchos::StringToIntegralParameterEntryValidator<IntegralType> > 02509 Teuchos::stringToIntegralParameterEntryValidator( 02510 ArrayView<const std::string> const& strings, 02511 ArrayView<const std::string> const& stringsDocs, 02512 ArrayView<const IntegralType> const& integralValues, 02513 std::string const& defaultParameterName, 02514 const bool caseSensitive) 02515 { 02516 typedef StringToIntegralParameterEntryValidator<IntegralType> ret_type; 02517 return rcp (new ret_type (strings, stringsDocs, integralValues, 02518 defaultParameterName, caseSensitive)); 02519 } 02520 02521 02522 template<class IntegralType> 02523 void Teuchos::setStringToIntegralParameter( 02524 std::string const& paramName, 02525 std::string const& defaultValue, 02526 std::string const& docString, 02527 ArrayView<const std::string> const& strings, 02528 ParameterList * paramList 02529 ) 02530 { 02531 typedef ParameterEntryValidator PEV; 02532 TEUCHOS_TEST_FOR_EXCEPT(0==paramList); 02533 paramList->set( 02534 paramName, defaultValue, docString, 02535 rcp_implicit_cast<const PEV>( 02536 stringToIntegralParameterEntryValidator<IntegralType>( 02537 strings, paramName 02538 ) 02539 ) 02540 ); 02541 } 02542 02543 02544 template<class IntegralType> 02545 void Teuchos::setStringToIntegralParameter( 02546 std::string const& paramName, 02547 std::string const& defaultValue, 02548 std::string const& docString, 02549 ArrayView<const std::string> const& strings, 02550 ArrayView<const IntegralType> const& integralValues, 02551 ParameterList * paramList 02552 ) 02553 { 02554 typedef ParameterEntryValidator PEV; 02555 TEUCHOS_TEST_FOR_EXCEPT(0==paramList); 02556 paramList->set( 02557 paramName, defaultValue, docString, 02558 rcp_implicit_cast<const PEV>( 02559 stringToIntegralParameterEntryValidator<IntegralType>( 02560 strings, integralValues, paramName 02561 ) 02562 ) 02563 ); 02564 } 02565 02566 02567 template<class IntegralType> 02568 void Teuchos::setStringToIntegralParameter( 02569 std::string const& paramName, 02570 std::string const& defaultValue, 02571 std::string const& docString, 02572 ArrayView<const std::string> const& strings, 02573 ArrayView<const std::string> const& stringsDocs, 02574 ArrayView<const IntegralType> const& integralValues, 02575 ParameterList * paramList 02576 ) 02577 02578 { 02579 typedef ParameterEntryValidator PEV; 02580 TEUCHOS_TEST_FOR_EXCEPT(0==paramList); 02581 paramList->set( 02582 paramName, defaultValue, docString, 02583 rcp_implicit_cast<const PEV>( 02584 stringToIntegralParameterEntryValidator<IntegralType>( 02585 strings, stringsDocs, integralValues, paramName 02586 ) 02587 ) 02588 ); 02589 } 02590 02591 02592 template<class IntegralType> 02593 IntegralType Teuchos::getIntegralValue( 02594 ParameterList const& paramList, std::string const& paramName 02595 ) 02596 { 02597 const ParameterEntry &entry = paramList.getEntry(paramName); 02598 RCP<const StringToIntegralParameterEntryValidator<IntegralType> > 02599 integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>( 02600 entry, paramList, paramName 02601 ); 02602 return integralValidator->getIntegralValue( 02603 entry, paramName, paramList.name(), true ); 02604 } 02605 02606 02607 template<class IntegralType> 02608 std::string Teuchos::getStringValue( 02609 ParameterList const& paramList, std::string const& paramName 02610 ) 02611 { 02612 const ParameterEntry &entry = paramList.getEntry(paramName); 02613 RCP<const StringToIntegralParameterEntryValidator<IntegralType> > 02614 integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>( 02615 entry, paramList, paramName 02616 ); 02617 return integralValidator->getStringValue( 02618 entry, paramName, paramList.name(), true 02619 ); 02620 } 02621 02622 02623 template<class IntegralType> 02624 Teuchos::RCP<const Teuchos::StringToIntegralParameterEntryValidator<IntegralType> > 02625 Teuchos::getStringToIntegralParameterEntryValidator( 02626 ParameterEntry const& entry, ParameterList const& paramList, 02627 std::string const& paramName 02628 ) 02629 { 02630 const RCP<const ParameterEntryValidator> validator = entry.validator(); 02631 TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG( 02632 is_null(validator), Exceptions::InvalidParameterType, 02633 "Error! The parameter \""<<paramName<<"\" exists\n" 02634 "in the parameter (sub)list \""<<paramList.name()<<"\"\n" 02635 "but it does not contain any validator needed to extract\n" 02636 "an integral value of type \""<<TypeNameTraits<IntegralType>::name()<<"\"!" 02637 ); 02638 const RCP<const StringToIntegralParameterEntryValidator<IntegralType> > integralValidator = 02639 rcp_dynamic_cast<const StringToIntegralParameterEntryValidator<IntegralType> >( 02640 validator 02641 ); 02642 TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG( 02643 is_null(integralValidator), Exceptions::InvalidParameterType, 02644 "Error! The parameter \""<<paramName<<"\" exists\n" 02645 "in the parameter (sub)list \""<<paramList.name()<<"\"\n" 02646 "but it contains the wrong type of validator. The expected validator type\n" 02647 "is \""<<TypeNameTraits<StringToIntegralParameterEntryValidator<IntegralType> >::name()<<"\"\n" 02648 "but the contained validator type is \""<<typeName(*validator)<<"\"!" 02649 ); 02650 return integralValidator; 02651 } 02652 02653 02654 #endif // TEUCHOS_STANDARD_PARAMETER_ENTRY_VALIDATORS_H
1.7.6.1