|
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 namespace Teuchos { 00056 00057 00070 template<class IntegralType> 00071 class StringToIntegralParameterEntryValidator : public ParameterEntryValidator { 00072 public: 00075 00084 StringToIntegralParameterEntryValidator( 00085 ArrayView<const std::string> const& strings, 00086 std::string const& defaultParameterName 00087 ); 00088 00104 StringToIntegralParameterEntryValidator( 00105 ArrayView<const std::string> const& strings, 00106 ArrayView<const IntegralType> const& integralValues, 00107 std::string const& defaultParameterName 00108 ); 00109 00129 StringToIntegralParameterEntryValidator( 00130 ArrayView<const std::string> const& strings, 00131 ArrayView<const std::string> const& stringsDocs, 00132 ArrayView<const IntegralType> const& integralValues, 00133 std::string const& defaultParameterName 00134 ); 00135 00137 00140 00152 IntegralType getIntegralValue( 00153 const std::string &str, const std::string ¶mName = "", 00154 const std::string &sublistName = "" 00155 ) const; 00156 00170 IntegralType getIntegralValue( 00171 const ParameterEntry &entry, const std::string ¶mName = "", 00172 const std::string &sublistName = "", const bool activeQuery = true 00173 ) const; 00174 00189 std::string getStringValue( 00190 const ParameterEntry &entry, const std::string ¶mName = "", 00191 const std::string &sublistName = "", const bool activeQuery = true 00192 ) const; 00193 00198 IntegralType getIntegralValue( 00199 ParameterList ¶mList, const std::string ¶mName, 00200 const std::string &defaultValue 00201 ) const; 00202 00206 std::string getStringValue( 00207 ParameterList ¶mList, const std::string ¶mName, 00208 const std::string &defaultValue 00209 ) const; 00210 00216 ValidStringsList getStringDocs() const; 00217 00222 const std::string& getDefaultParameterName() const; 00223 00235 std::string validateString( 00236 const std::string &str, const std::string ¶mName = "", 00237 const std::string &sublistName = "" 00238 ) const; 00239 00241 00244 00246 const std::string getXMLTypeName() const; 00247 00249 void printDoc( 00250 std::string const& docString, 00251 std::ostream & out 00252 ) const; 00253 00255 ValidStringsList 00256 validStringValues() const; 00257 00259 void validate( 00260 ParameterEntry const& entry, 00261 std::string const& paramName, 00262 std::string const& sublistName 00263 ) const; 00264 00266 00267 private: 00268 00269 typedef std::map<std::string,IntegralType> map_t; 00270 std::string defaultParameterName_; 00271 std::string validValues_; 00272 ValidStringsList validStringValues_; 00273 ValidStringsList validStringValuesDocs_; 00274 map_t map_; 00275 00276 void setValidValues( 00277 ArrayView<const std::string> const& strings, 00278 ArrayView<const std::string> const* stringsDocs = NULL 00279 ); 00280 00281 // Not defined and not to be called. 00282 StringToIntegralParameterEntryValidator(); 00283 00284 }; 00285 00286 00291 template<class IntegralType> 00292 RCP<StringToIntegralParameterEntryValidator<IntegralType> > 00293 stringToIntegralParameterEntryValidator( 00294 ArrayView<const std::string> const& strings, 00295 std::string const& defaultParameterName 00296 ); 00297 00298 00303 template<class IntegralType> 00304 RCP<StringToIntegralParameterEntryValidator<IntegralType> > 00305 stringToIntegralParameterEntryValidator( 00306 ArrayView<const std::string> const& strings, 00307 ArrayView<const IntegralType> const& integralValues, 00308 std::string const& defaultParameterName 00309 ); 00310 00311 00316 template<class IntegralType> 00317 RCP<StringToIntegralParameterEntryValidator<IntegralType> > 00318 stringToIntegralParameterEntryValidator( 00319 ArrayView<const std::string> const& strings, 00320 ArrayView<const std::string> const& stringsDocs, 00321 ArrayView<const IntegralType> const& integralValues, 00322 std::string const& defaultParameterName 00323 ); 00324 00335 template<class IntegralType> 00336 void setStringToIntegralParameter( 00337 std::string const& paramName, 00338 std::string const& defaultValue, 00339 std::string const& docString, 00340 ArrayView<const std::string> const& strings, 00341 ParameterList * paramList 00342 ); 00343 00344 00356 template<class IntegralType> 00357 void setStringToIntegralParameter( 00358 std::string const& paramName, 00359 std::string const& defaultValue, 00360 std::string const& docString, 00361 ArrayView<const std::string> const& strings, 00362 ArrayView<const IntegralType> const& integralValues, 00363 ParameterList * paramList 00364 ); 00365 00366 00378 template<class IntegralType> 00379 void setStringToIntegralParameter( 00380 std::string const& paramName, 00381 std::string const& defaultValue, 00382 std::string const& docString, 00383 ArrayView<const std::string> const& strings, 00384 ArrayView<const std::string> const& stringsDocs, 00385 ArrayView<const IntegralType> const& integralValues, 00386 ParameterList * paramList 00387 ); 00388 00389 00400 template<class IntegralType> 00401 IntegralType getIntegralValue( 00402 ParameterList const& paramList, std::string const& paramName 00403 ); 00404 00405 00417 template<class IntegralType> 00418 std::string getStringValue( 00419 ParameterList const& paramList, std::string const& paramName 00420 ); 00421 00422 00428 template<class IntegralType> 00429 RCP<const StringToIntegralParameterEntryValidator<IntegralType> > 00430 getStringToIntegralParameterEntryValidator( 00431 ParameterEntry const& entry, ParameterList const& paramList, 00432 std::string const& paramName 00433 ); 00434 00435 00441 std::string getVerbosityLevelParameterValueName( 00442 const EVerbosityLevel verbLevel 00443 ); 00444 00445 00450 RCP<StringToIntegralParameterEntryValidator<EVerbosityLevel> > 00451 verbosityLevelParameterEntryValidator(std::string const& defaultParameterName); 00452 00458 template<class IntegralType> 00459 class DummyObjectGetter<StringToIntegralParameterEntryValidator<IntegralType> >{ 00460 00461 public: 00462 00465 00469 static RCP<StringToIntegralParameterEntryValidator<IntegralType> > 00470 getDummyObject(); 00471 00473 }; 00474 00475 template<class IntegralType> 00476 RCP<StringToIntegralParameterEntryValidator<IntegralType> > 00477 DummyObjectGetter<StringToIntegralParameterEntryValidator<IntegralType> >::getDummyObject() 00478 { 00479 return stringToIntegralParameterEntryValidator<IntegralType>( 00480 tuple<std::string>(""), tuple<std::string>(""), 00481 tuple<IntegralType>((IntegralType)1), ""); 00482 } 00483 00484 00485 00499 class TEUCHOS_LIB_DLL_EXPORT AnyNumberParameterEntryValidator 00500 : public ParameterEntryValidator 00501 { 00502 public: 00503 00506 00508 enum EPreferredType { PREFER_INT, PREFER_DOUBLE, PREFER_STRING }; 00509 00510 00512 class AcceptedTypes { 00513 public: 00515 AcceptedTypes( bool allowAllTypesByDefault = true ) 00516 :allowInt_(allowAllTypesByDefault),allowDouble_(allowAllTypesByDefault), 00517 allowString_(allowAllTypesByDefault) 00518 {} 00520 AcceptedTypes& allowInt( bool _allowInt ) 00521 { allowInt_ = _allowInt; return *this; } 00523 AcceptedTypes& allowDouble( bool _allowDouble ) 00524 { allowDouble_ = _allowDouble; return *this; } 00526 AcceptedTypes& allowString( bool _allowString ) 00527 { allowString_ = _allowString; return *this; } 00529 bool allowInt() const { return allowInt_; } 00531 bool allowDouble() const { return allowDouble_; } 00533 bool allowString() const { return allowString_; } 00534 private: 00535 bool allowInt_; 00536 bool allowDouble_; 00537 bool allowString_; 00538 }; 00539 00541 00544 00548 AnyNumberParameterEntryValidator(); 00549 00560 AnyNumberParameterEntryValidator( 00561 EPreferredType const preferredType, 00562 AcceptedTypes const& acceptedTypes 00563 ); 00564 00566 00569 00571 int getInt( 00572 const ParameterEntry &entry, const std::string ¶mName = "", 00573 const std::string &sublistName = "", const bool activeQuery = true 00574 ) const; 00575 00577 double getDouble( 00578 const ParameterEntry &entry, const std::string ¶mName = "", 00579 const std::string &sublistName = "", const bool activeQuery = true 00580 ) const; 00581 00583 std::string getString( 00584 const ParameterEntry &entry, const std::string ¶mName = "", 00585 const std::string &sublistName = "", const bool activeQuery = true 00586 ) const; 00587 00591 int getInt( 00592 ParameterList ¶mList, const std::string ¶mName, 00593 const int defaultValue 00594 ) const; 00595 00599 double getDouble( 00600 ParameterList ¶mList, const std::string ¶mName, 00601 const double defaultValue 00602 ) const; 00603 00607 std::string getString( 00608 ParameterList ¶mList, const std::string ¶mName, 00609 const std::string &defaultValue 00610 ) const; 00611 00614 bool isDoubleAllowed() const; 00615 00618 bool isIntAllowed() const; 00619 00622 bool isStringAllowed() const; 00623 00626 EPreferredType getPreferredType() const; 00627 00629 static const std::string& 00630 getPrefferedTypeString(EPreferredType enumValue) 00631 { 00632 switch(enumValue){ 00633 case PREFER_INT: 00634 return getIntEnumString(); 00635 break; 00636 case PREFER_DOUBLE: 00637 return getDoubleEnumString(); 00638 break; 00639 case PREFER_STRING: 00640 return getStringEnumString(); 00641 break; 00642 default: 00643 static const std::string typeString(toString(enumValue)); 00644 throw std::runtime_error("Cannot convert enumValue: " + typeString + " to a string"); 00645 //Should never get here. This code is here so that a warning is not generated. 00646 return typeString; 00647 } 00648 } 00649 00651 static EPreferredType getPrefferedTypeStringEnum(const std::string& enumString) 00652 { 00653 if(enumString == getIntEnumString()){ 00654 return PREFER_INT; 00655 } 00656 else if(enumString == getDoubleEnumString()){ 00657 return PREFER_DOUBLE; 00658 } 00659 else if(enumString == getStringEnumString()){ 00660 return PREFER_STRING; 00661 } 00662 else{ 00663 throw std::runtime_error("Cannot convert enumString: " + enumString + " to an enum"); 00664 } 00665 //Should never get here. This code is here so that a warning is not generated. 00666 return (EPreferredType)-1; 00667 } 00668 00670 00673 00675 const std::string getXMLTypeName() const; 00676 00678 void printDoc( 00679 std::string const& docString, 00680 std::ostream & out 00681 ) const; 00682 00684 ValidStringsList 00685 validStringValues() const; 00686 00688 void validate( 00689 ParameterEntry const& entry, 00690 std::string const& paramName, 00691 std::string const& sublistName 00692 ) const; 00693 00695 void validateAndModify( 00696 std::string const& paramName, 00697 std::string const& sublistName, 00698 ParameterEntry * entry 00699 ) const; 00700 00701 00703 00704 private: 00705 00706 // //////////////////////////// 00707 // Private data members 00708 00709 EPreferredType preferredType_; 00710 std::string acceptedTypesString_; 00711 00712 //use pragmas to disable some false-positive warnings for windows sharedlibs export 00713 #ifdef _MSC_VER 00714 #pragma warning(push) 00715 #pragma warning(disable:4251) 00716 #endif 00717 const AcceptedTypes acceptedTypes_; 00718 #ifdef _MSC_VER 00719 #pragma warning(pop) 00720 #endif 00721 00722 // //////////////////////////// 00723 // Private member functions 00724 00725 /* \brief Gets the string representing the "int" preferred type enum */ 00726 static const std::string& getIntEnumString(){ 00727 static const std::string intEnumString_ = TypeNameTraits<int>::name(); 00728 return intEnumString_; 00729 } 00730 00731 /* \brief Gets the string representing the "double" preferred type enum */ 00732 static const std::string& getDoubleEnumString(){ 00733 static const std::string doubleEnumString_ = TypeNameTraits<double>::name(); 00734 return doubleEnumString_; 00735 } 00736 00737 /* \brief Gets the string representing the "string" preferred type enum */ 00738 static const std::string& getStringEnumString(){ 00739 static const std::string stringEnumString_ = TypeNameTraits<std::string>::name(); 00740 return stringEnumString_; 00741 } 00742 00743 00744 void finishInitialization(); 00745 00746 void throwTypeError( 00747 ParameterEntry const& entry, 00748 std::string const& paramName, 00749 std::string const& sublistName 00750 ) const; 00751 00752 }; 00753 00754 00755 // Nonmember helper functions 00756 00757 00762 TEUCHOS_LIB_DLL_EXPORT RCP<AnyNumberParameterEntryValidator> 00763 anyNumberParameterEntryValidator(); 00764 00765 00770 TEUCHOS_LIB_DLL_EXPORT RCP<AnyNumberParameterEntryValidator> 00771 anyNumberParameterEntryValidator( 00772 AnyNumberParameterEntryValidator::EPreferredType const preferredType, 00773 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes 00774 ); 00775 00781 TEUCHOS_LIB_DLL_EXPORT void setIntParameter( 00782 std::string const& paramName, 00783 int const value, std::string const& docString, 00784 ParameterList *paramList, 00785 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes 00786 = AnyNumberParameterEntryValidator::AcceptedTypes() 00787 ); 00788 00789 00795 TEUCHOS_LIB_DLL_EXPORT void setDoubleParameter( 00796 std::string const& paramName, 00797 double const& value, std::string const& docString, 00798 ParameterList *paramList, 00799 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes 00800 = AnyNumberParameterEntryValidator::AcceptedTypes() 00801 ); 00802 00803 00809 TEUCHOS_LIB_DLL_EXPORT void setNumericStringParameter( 00810 std::string const& paramName, 00811 std::string const& value, std::string const& docString, 00812 ParameterList *paramList, 00813 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes 00814 = AnyNumberParameterEntryValidator::AcceptedTypes() 00815 ); 00816 00817 00832 TEUCHOS_LIB_DLL_EXPORT int getIntParameter( 00833 ParameterList const& paramList, std::string const& paramName 00834 ); 00835 00836 00851 TEUCHOS_LIB_DLL_EXPORT double getDoubleParameter( 00852 ParameterList const& paramList, 00853 std::string const& paramName 00854 ); 00855 00856 00872 TEUCHOS_LIB_DLL_EXPORT std::string getNumericStringParameter( 00873 ParameterList const& paramList, 00874 std::string const& paramName 00875 ); 00876 00882 template<> 00883 class TEUCHOS_LIB_DLL_EXPORT TEUCHOS_LIB_DLL_EXPORT DummyObjectGetter<AnyNumberParameterEntryValidator>{ 00884 00885 public: 00886 00889 00893 static RCP<AnyNumberParameterEntryValidator > getDummyObject(); 00894 00896 00897 }; 00898 00899 00904 template <class T> 00905 struct UndefinedEnhancedNumberTraits{ 00907 static inline T notDefined() { 00908 return T::this_type_is_missing_a_specialization(); 00909 } 00910 }; 00911 00912 00933 template <class T> 00934 class EnhancedNumberTraits{ 00935 public: 00936 00938 static inline T min() 00939 { return UndefinedEnhancedNumberTraits<T>::notDefined(); } 00940 00942 static inline T max() 00943 { return UndefinedEnhancedNumberTraits<T>::notDefined(); } 00944 00947 static inline T defaultStep() 00948 { return UndefinedEnhancedNumberTraits<T>::notDefined(); } 00949 00952 static inline unsigned short defaultPrecision() 00953 { return UndefinedEnhancedNumberTraits<T>::notDefined(); } 00954 00955 }; 00956 00957 00958 template<> 00959 class EnhancedNumberTraits<short int>{ 00960 public: 00961 static inline short int min() { return std::numeric_limits<short int>::min(); } 00962 static inline short int max() { return std::numeric_limits<short int>::max(); } 00963 static inline short int defaultStep() { return 1; } 00964 static inline unsigned short defaultPrecision() { return 0; } 00965 }; 00966 00967 00968 template<> 00969 class EnhancedNumberTraits<short unsigned int>{ 00970 public: 00971 static inline short unsigned int min() { return std::numeric_limits<short unsigned int>::min(); } 00972 static inline short unsigned int max() { return std::numeric_limits<short unsigned int>::max(); } 00973 static inline short unsigned int defaultStep() { return 1; } 00974 static inline unsigned short defaultPrecision() { return 0; } 00975 }; 00976 00977 00978 template<> 00979 class EnhancedNumberTraits<int>{ 00980 public: 00981 static inline int min() { return std::numeric_limits<int>::min(); } 00982 static inline int max() { return std::numeric_limits<int>::max(); } 00983 static inline int defaultStep() { return 1; } 00984 static inline unsigned short defaultPrecision() { return 0; } 00985 }; 00986 00987 00988 template<> 00989 class EnhancedNumberTraits<unsigned int>{ 00990 public: 00991 static inline unsigned int min() { return std::numeric_limits<unsigned int>::min(); } 00992 static inline unsigned int max() { return std::numeric_limits<unsigned int>::max(); } 00993 static inline unsigned int defaultStep() { return 1; } 00994 static inline unsigned short defaultPrecision() { return 0; } 00995 }; 00996 00997 00998 template<> 00999 class EnhancedNumberTraits<long int>{ 01000 public: 01001 static inline long int min() { return std::numeric_limits<long int>::min(); } 01002 static inline long int max() { return std::numeric_limits<long int>::max(); } 01003 static inline long int defaultStep() { return 1; } 01004 static inline unsigned short defaultPrecision() { return 0; } 01005 }; 01006 01007 01008 template<> 01009 class EnhancedNumberTraits<long unsigned int>{ 01010 public: 01011 static inline long unsigned int min() { return std::numeric_limits<long unsigned int>::min(); } 01012 static inline long unsigned int max() { return std::numeric_limits<long unsigned int>::max(); } 01013 static inline long unsigned int defaultStep() { return 1; } 01014 static inline unsigned short defaultPrecision() { return 0; } 01015 }; 01016 01017 01018 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 01019 01020 01021 template<> 01022 class EnhancedNumberTraits<long long int>{ 01023 public: 01024 static inline long long int min() { return std::numeric_limits<long long int>::min(); } 01025 static inline long long int max() { return std::numeric_limits<long long int>::max(); } 01026 static inline long long int defaultStep() { return 1; } 01027 static inline unsigned short defaultPrecision() { return 0; } 01028 }; 01029 01030 01031 template<> 01032 class EnhancedNumberTraits<long long unsigned int>{ 01033 public: 01034 static inline long long unsigned int min() { return std::numeric_limits<long long unsigned int>::min(); } 01035 static inline long long unsigned int max() { return std::numeric_limits<long long unsigned int>::max(); } 01036 static inline long long unsigned int defaultStep() { return 1; } 01037 static inline unsigned short defaultPrecision() { return 0; } 01038 }; 01039 01040 01041 #endif // HAVE_TEUCHOS_LONG_LONG_INT 01042 01043 01044 template<> 01045 class EnhancedNumberTraits<double>{ 01046 public: 01047 static inline double min() { return -std::numeric_limits<double>::max(); } 01048 static inline double max() { return std::numeric_limits<double>::max(); } 01049 static inline double defaultStep() { return 1; } 01050 static inline unsigned short defaultPrecision() { return 100; } 01051 }; 01052 01053 01054 template<> 01055 class EnhancedNumberTraits<float>{ 01056 public: 01057 static inline float min() { return -std::numeric_limits<float>::max(); } 01058 static inline float max() { return std::numeric_limits<float>::max(); } 01059 static inline float defaultStep() { return 1; } 01060 static inline unsigned short defaultPrecision() { return 100; } 01061 }; 01062 01063 01069 template <class T> 01070 class EnhancedNumberValidator : public ParameterEntryValidator{ 01071 01072 public: 01073 01076 01091 EnhancedNumberValidator( 01092 T min, 01093 T max, 01094 T step=EnhancedNumberTraits<T>::defaultStep(), 01095 unsigned short precision=EnhancedNumberTraits<T>::defaultPrecision()): 01096 ParameterEntryValidator(), 01097 minVal(min), maxVal(max), step_(step), precision_(precision), 01098 containsMin(true), containsMax(true){} 01099 01103 EnhancedNumberValidator(): 01104 ParameterEntryValidator(), 01105 minVal(EnhancedNumberTraits<T>::min()), 01106 maxVal(EnhancedNumberTraits<T>::max()), 01107 step_(EnhancedNumberTraits<T>::defaultStep()), 01108 precision_(EnhancedNumberTraits<T>::defaultPrecision()), 01109 containsMin(false), 01110 containsMax(false){} 01111 01113 01115 01116 01121 void setMin(T min){ 01122 minVal = min; 01123 containsMin = true; 01124 } 01125 01130 void setMax(T max){ 01131 maxVal = max; 01132 containsMax = true; 01133 } 01134 01139 void setStep(T step){ 01140 step_ = step; 01141 } 01142 01147 void setPrecision(unsigned short precision){ 01148 precision_ = precision; 01149 } 01150 01152 01155 01160 T getMin() const{ 01161 return minVal; 01162 } 01163 01168 T getMax() const{ 01169 return maxVal; 01170 } 01171 01176 T getStep() const{ 01177 return step_; 01178 } 01179 01184 unsigned short getPrecision() const{ 01185 return precision_; 01186 } 01187 01189 01191 01192 01197 bool hasMin() const{ 01198 return containsMin; 01199 } 01200 01205 bool hasMax() const{ 01206 return containsMax; 01207 } 01208 01210 01213 01215 ValidStringsList validStringValues() const{ 01216 return null; 01217 } 01218 01220 void validate(ParameterEntry const &entry, std::string const ¶mName, 01221 std::string const &sublistName) const; 01222 01224 const std::string getXMLTypeName() const{ 01225 return "EnhancedNumberValidator(" + TypeNameTraits<T>::name()+ ")"; 01226 } 01227 01229 void printDoc(std::string const &docString, std::ostream &out) const{ 01230 StrUtils::printLines(out,"# ",docString); 01231 out << "#\tValidator Used: " << std::endl; 01232 out << "#\t\tNumber Validator" << std::endl; 01233 out << "#\t\tType: " << Teuchos::TypeNameTraits<T>::name() << 01234 std::endl; 01235 out << "#\t\tMin (inclusive): " << minVal << std::endl; 01236 out << "#\t\tMax (inclusive): " << maxVal << std::endl; 01237 } 01238 01240 01241 private: 01242 01245 01248 T minVal; 01249 01252 T maxVal; 01253 01256 T step_; 01257 01261 unsigned short precision_; 01262 01265 bool containsMin; 01266 01269 bool containsMax; 01270 01272 01273 }; 01274 01275 template<class T> 01276 void EnhancedNumberValidator<T>::validate(ParameterEntry const &entry, std::string const ¶mName, 01277 std::string const &sublistName) const 01278 { 01279 any anyValue = entry.getAny(true); 01280 const std::string &entryName = entry.getAny(false).typeName(); 01281 01282 TEUCHOS_TEST_FOR_EXCEPTION(anyValue.type() != typeid(T), 01283 Exceptions::InvalidParameterType, 01284 "Aww shoot! Sorry bud, but it looks like the \"" << 01285 paramName << "\"" << 01286 " parameter in the \"" << sublistName 01287 << "\" sublist didn't quite work out." << std::endl << 01288 "No need to fret though. I'm sure it's just a small mistake. " 01289 "Maybe the information below "<< 01290 "can help you figure out what went wrong." << std::endl << std::endl << 01291 "Error: The value that you entered was the wrong type." << std::endl << 01292 "Parameter: " << paramName << std::endl << 01293 "Type specified: " << entryName << std::endl << 01294 "Type accepted: " << Teuchos::TypeNameTraits<T>::name() << std::endl); 01295 01296 bool isValueInRange; 01297 any_cast<T>(anyValue) >= minVal && any_cast<T>(anyValue) <= maxVal 01298 ? isValueInRange = true : isValueInRange=false; 01299 TEUCHOS_TEST_FOR_EXCEPTION(!(isValueInRange), 01300 Exceptions::InvalidParameterValue, 01301 "Aww shoot! Sorry bud, but it looks like the \"" << 01302 paramName << "\"" << 01303 " parameter in the \"" << sublistName << 01304 "\" sublist didn't quite work " 01305 "out." << std::endl << 01306 "No need to fret though. I'm sure it's just a small mistake. " 01307 "Maybe the information below "<< 01308 "can help you figure out what went wrong." << std::endl << 01309 std::endl << 01310 "Error: The value that was entered doesn't fall with in " << 01311 "the range set by the validator" << std::endl << 01312 "Parameter: " << paramName << std::endl << 01313 "Min: " << minVal << std::endl << 01314 "Max: " << maxVal << std::endl << 01315 "Value entered: " << 01316 (any_cast<T>(anyValue)) << std::endl << std::endl); 01317 } 01318 01324 template<class T> 01325 class DummyObjectGetter<EnhancedNumberValidator<T> >{ 01326 01327 public: 01328 01331 01335 static RCP<EnhancedNumberValidator<T> > getDummyObject(); 01336 01338 }; 01339 01340 template<class T> 01341 RCP<EnhancedNumberValidator<T> > 01342 DummyObjectGetter<EnhancedNumberValidator<T> >::getDummyObject() 01343 { 01344 return rcp(new EnhancedNumberValidator<T>); 01345 } 01346 01355 class TEUCHOS_LIB_DLL_EXPORT FileNameValidator : public ParameterEntryValidator { 01356 01357 public: 01358 01361 01364 static bool mustAlreadyExistDefault() { return false; } 01365 01367 01370 01376 FileNameValidator(bool mustAlreadyExist = mustAlreadyExistDefault()); 01377 01379 01381 01382 01388 bool fileMustExist() const; 01389 01391 01393 01394 01403 bool setFileMustExist(bool shouldFileExist); 01404 01406 01409 01411 ValidStringsList validStringValues() const; 01412 01414 void validate( 01415 ParameterEntry const &entry, 01416 std::string const ¶mName, 01417 std::string const &sublistName) const; 01418 01420 const std::string getXMLTypeName() const; 01421 01423 void printDoc(std::string const &docString, std::ostream &out) const; 01424 01426 01427 private: 01428 01431 01435 bool mustAlreadyExist_; 01436 01438 01439 }; 01440 01446 template<> 01447 class TEUCHOS_LIB_DLL_EXPORT DummyObjectGetter<FileNameValidator>{ 01448 01449 public: 01450 01453 01457 static RCP<FileNameValidator> getDummyObject(); 01458 01460 01461 }; 01462 01470 class TEUCHOS_LIB_DLL_EXPORT StringValidator : public ParameterEntryValidator { 01471 01472 public: 01473 01476 01479 StringValidator(); 01480 01485 StringValidator(const Teuchos::Array<std::string> &validStrings); 01486 01488 01490 01491 01499 ValidStringsList setValidStrings( 01500 const Teuchos::Array<std::string> &validStrings); 01501 01503 01506 01508 ValidStringsList validStringValues() const; 01509 01511 void validate(ParameterEntry const &entry, std::string const ¶mName, 01512 std::string const &sublistName) const; 01513 01515 const std::string getXMLTypeName() const; 01516 01518 void printDoc(std::string const &docString, std::ostream &out) const; 01519 01521 01522 private: 01523 01526 01529 ValidStringsList validStrings_; 01530 01532 01533 }; 01534 01540 template<> 01541 class TEUCHOS_LIB_DLL_EXPORT DummyObjectGetter<StringValidator>{ 01542 01543 public: 01544 01547 01551 static RCP<StringValidator> getDummyObject(); 01552 01554 01555 }; 01556 01557 01561 template<class ValidatorType, class EntryType> 01562 class AbstractArrayValidator : public ParameterEntryValidator { 01563 01564 public: 01565 01568 01575 AbstractArrayValidator(RCP<const ValidatorType> prototypeValidator): 01576 ParameterEntryValidator(), 01577 prototypeValidator_(prototypeValidator){} 01578 01580 01583 01585 RCP<const ValidatorType> getPrototype() const{ 01586 return prototypeValidator_; 01587 } 01588 01590 01593 01595 ValidStringsList validStringValues() const { 01596 return prototypeValidator_->validStringValues(); 01597 } 01598 01600 01601 private: 01602 01605 01608 RCP<const ValidatorType> prototypeValidator_; 01609 01611 AbstractArrayValidator<ValidatorType, EntryType>(); 01612 01614 01615 }; 01616 01629 template<class ValidatorType, class EntryType> 01630 class TwoDArrayValidator : public AbstractArrayValidator<ValidatorType, EntryType>{ 01631 public: 01634 01641 TwoDArrayValidator(RCP<const ValidatorType> prototypeValidator): 01642 AbstractArrayValidator<ValidatorType, EntryType>(prototypeValidator){} 01643 01645 01648 01650 virtual void validate(ParameterEntry const &entry, std::string const ¶mName, 01651 std::string const &sublistName) const; 01652 01654 const std::string getXMLTypeName() const{ 01655 return "TwoDArrayValidator(" + 01656 this->getPrototype()->getXMLTypeName() + ", " + 01657 TypeNameTraits<EntryType>::name() + ")"; 01658 } 01659 01661 virtual void printDoc(std::string const &docString, std::ostream &out) const 01662 { 01663 StrUtils::printLines(out,"# ",docString); 01664 std::string toPrint; 01665 toPrint += "TwoDArrayValidator:\n"; 01666 toPrint += "Prototype Validator:\n"; 01667 this->getPrototype()->printDoc(toPrint, out); 01668 } 01669 01671 01672 }; 01673 01674 template<class ValidatorType, class EntryType> 01675 void TwoDArrayValidator<ValidatorType, EntryType>::validate(ParameterEntry const &entry, std::string const ¶mName, 01676 std::string const &sublistName) const 01677 { 01678 any anyValue = entry.getAny(true); 01679 const std::string &entryName = entry.getAny(false).typeName(); 01680 TEUCHOS_TEST_FOR_EXCEPTION(anyValue.type() != typeid(TwoDArray<EntryType>), 01681 Exceptions::InvalidParameterType, 01682 "Aww shoot! Sorry bud, but it looks like the \"" << 01683 paramName << "\"" << 01684 " parameter in the \"" << sublistName << 01685 "\" sublist didn't quite work out." << std::endl << 01686 "No need to fret though. I'm sure it's just a small mistake. " 01687 "Maybe the information below "<< 01688 "can help you figure out what went wrong." << std::endl << 01689 std::endl << 01690 "Error: The value you entered was the wrong type." << std::endl << 01691 "Parameter: " << paramName << std::endl << 01692 "Type specified: " << entryName << std::endl << 01693 "Type accepted: " << TypeNameTraits<TwoDArray<EntryType> >::name() << 01694 std::endl << std::endl); 01695 01696 TwoDArray<EntryType> extracted = 01697 getValue<Teuchos::TwoDArray<EntryType> >(entry); 01698 RCP<const ParameterEntryValidator> prototype = this->getPrototype(); 01699 for(int i = 0; i<extracted.getNumRows(); ++i){ 01700 for(int j = 0; j<extracted.getNumCols(); ++j){ 01701 ParameterEntry dummyParameter; 01702 dummyParameter.setValue(extracted(i,j)); 01703 try{ 01704 prototype->validate( 01705 dummyParameter, paramName, sublistName); 01706 } 01707 catch(Exceptions::InvalidParameterValue& e){ 01708 std::stringstream oss; 01709 oss << "TwoDArray Validator Exception:" << std::endl << 01710 "Bad Index: (" << i << "," << j << ")" << std::endl << e.what(); 01711 throw Exceptions::InvalidParameterValue(oss.str()); 01712 } 01713 } 01714 } 01715 } 01716 01717 01723 template<class ValidatorType, class EntryType> 01724 class DummyObjectGetter<TwoDArrayValidator<ValidatorType, EntryType> >{ 01725 01726 public: 01727 01730 01734 static RCP<TwoDArrayValidator<ValidatorType, EntryType> > getDummyObject(); 01735 01737 01738 }; 01739 01740 template<class ValidatorType, class EntryType> 01741 RCP<TwoDArrayValidator<ValidatorType, EntryType> > 01742 DummyObjectGetter<TwoDArrayValidator<ValidatorType, EntryType> >::getDummyObject() 01743 { 01744 return rcp(new TwoDArrayValidator<ValidatorType, EntryType>( 01745 DummyObjectGetter<ValidatorType>::getDummyObject())); 01746 } 01747 01751 class TEUCHOS_LIB_DLL_EXPORT TwoDArrayStringValidator : 01752 public TwoDArrayValidator<StringValidator, std::string>{ 01753 01754 public: 01755 01758 01760 TwoDArrayStringValidator(RCP<const StringValidator> prototypeValidator): 01761 TwoDArrayValidator<StringValidator, std::string>(prototypeValidator){} 01762 01764 01765 }; 01766 01767 01772 class TEUCHOS_LIB_DLL_EXPORT TwoDArrayFileNameValidator : 01773 public TwoDArrayValidator<FileNameValidator, std::string>{ 01774 01775 public: 01776 01779 01781 TwoDArrayFileNameValidator(RCP<const FileNameValidator> prototypeValidator): 01782 TwoDArrayValidator<FileNameValidator, std::string>(prototypeValidator){} 01783 01785 01786 }; 01787 01788 01792 template<class T> 01793 class TwoDArrayNumberValidator : public TwoDArrayValidator<EnhancedNumberValidator<T>, T>{ 01794 public: 01797 01799 TwoDArrayNumberValidator( 01800 RCP<const EnhancedNumberValidator<T> > prototypeValidator): 01801 TwoDArrayValidator<EnhancedNumberValidator<T>, T>(prototypeValidator){} 01802 01804 01805 }; 01806 01807 01819 template<class ValidatorType, class EntryType> 01820 class ArrayValidator : public AbstractArrayValidator<ValidatorType, EntryType>{ 01821 01822 public: 01823 01826 01832 ArrayValidator(RCP<const ValidatorType> prototypeValidator): 01833 AbstractArrayValidator<ValidatorType, EntryType>(prototypeValidator){} 01834 01836 01839 01841 virtual void validate(ParameterEntry const &entry, std::string const ¶mName, 01842 std::string const &sublistName) const; 01843 01845 const std::string getXMLTypeName() const{ 01846 return "ArrayValidator(" + 01847 this->getPrototype()->getXMLTypeName() + ", " + 01848 TypeNameTraits<EntryType>::name() + ")"; 01849 } 01850 01852 virtual void printDoc(std::string const &docString, std::ostream &out) const 01853 { 01854 StrUtils::printLines(out,"# ",docString); 01855 std::string toPrint; 01856 toPrint += "ArrayValidator:\n"; 01857 toPrint += "Prototype Validator:\n"; 01858 this->getPrototype()->printDoc(toPrint, out); 01859 } 01860 01862 01863 }; 01864 01865 template<class ValidatorType, class EntryType> 01866 void ArrayValidator<ValidatorType, EntryType>::validate(ParameterEntry const &entry, std::string const ¶mName, 01867 std::string const &sublistName) const 01868 { 01869 any anyValue = entry.getAny(true); 01870 const std::string &entryName = entry.getAny(false).typeName(); 01871 TEUCHOS_TEST_FOR_EXCEPTION(anyValue.type() != typeid(Array<EntryType>), 01872 Exceptions::InvalidParameterType, 01873 "Aww shoot! Sorry bud, but it looks like the \"" << 01874 paramName << "\"" << 01875 " parameter in the \"" << sublistName << 01876 "\" sublist didn't quite work out." << std::endl << 01877 "No need to fret though. I'm sure it's just a small mistake. " 01878 "Maybe the information below "<< 01879 "can help you figure out what went wrong." << std::endl << 01880 std::endl << 01881 "Error: The value you entered was the wrong type." << std::endl << 01882 "Parameter: " << paramName << std::endl << 01883 "Type specified: " << entryName << std::endl << 01884 "Type accepted: " << TypeNameTraits<Array<EntryType> >::name() << 01885 std::endl << std::endl); 01886 01887 Array<EntryType> extracted = 01888 getValue<Teuchos::Array<EntryType> >(entry); 01889 RCP<const ParameterEntryValidator> prototype = this->getPrototype(); 01890 for(int i = 0; i<extracted.size(); ++i){ 01891 ParameterEntry dummyParameter; 01892 dummyParameter.setValue(extracted[i]); 01893 try{ 01894 prototype->validate( 01895 dummyParameter, paramName, sublistName); 01896 } 01897 catch(Exceptions::InvalidParameterValue& e){ 01898 std::stringstream oss; 01899 oss << "Array Validator Exception:" << std::endl << 01900 "Bad Index: " << i << std::endl << e.what(); 01901 throw Exceptions::InvalidParameterValue(oss.str()); 01902 } 01903 } 01904 } 01905 01911 template<class ValidatorType, class EntryType> 01912 class DummyObjectGetter<ArrayValidator<ValidatorType, EntryType> >{ 01913 01914 public: 01915 01918 01922 static RCP<ArrayValidator<ValidatorType, EntryType> > getDummyObject(); 01923 01925 01926 }; 01927 01928 template<class ValidatorType, class EntryType> 01929 RCP<ArrayValidator<ValidatorType, EntryType> > 01930 DummyObjectGetter<ArrayValidator<ValidatorType, EntryType> >::getDummyObject() 01931 { 01932 return rcp(new ArrayValidator<ValidatorType, EntryType>( 01933 DummyObjectGetter<ValidatorType>::getDummyObject())); 01934 } 01935 01936 01945 class TEUCHOS_LIB_DLL_EXPORT ArrayStringValidator : 01946 public ArrayValidator<StringValidator, std::string>{ 01947 01948 public: 01949 01952 01954 ArrayStringValidator(RCP<const StringValidator> prototypeValidator): 01955 ArrayValidator<StringValidator, std::string>(prototypeValidator){} 01956 01958 01959 }; 01960 01961 01970 class TEUCHOS_LIB_DLL_EXPORT ArrayFileNameValidator : public ArrayValidator<FileNameValidator, std::string>{ 01971 01972 public: 01973 01976 01978 ArrayFileNameValidator(RCP<const FileNameValidator> prototypeValidator): 01979 ArrayValidator<FileNameValidator, std::string>(prototypeValidator){} 01980 01982 01983 }; 01984 01985 01993 template<class T> 01994 class ArrayNumberValidator : public ArrayValidator<EnhancedNumberValidator<T>, T>{ 01995 public: 01998 02000 ArrayNumberValidator( 02001 RCP<const EnhancedNumberValidator<T> > prototypeValidator): 02002 ArrayValidator<EnhancedNumberValidator<T>, T>(prototypeValidator){} 02003 02005 02006 }; 02007 02008 02009 02010 // /////////////////////////// 02011 // Implementations 02012 02013 02014 // 02015 // StringToIntegralParameterEntryValidator 02016 // 02017 02018 02019 // Constructors 02020 02021 02022 template<class IntegralType> 02023 StringToIntegralParameterEntryValidator<IntegralType>::StringToIntegralParameterEntryValidator( 02024 ArrayView<const std::string> const& strings, std::string const& defaultParameterName 02025 ): 02026 ParameterEntryValidator(), 02027 defaultParameterName_(defaultParameterName) 02028 { 02029 typedef typename map_t::value_type val_t; 02030 for( int i = 0; i < static_cast<int>(strings.size()); ++i ) { 02031 const bool unique = map_.insert( val_t( strings[i], (IntegralType)i ) ).second; 02032 TEUCHOS_TEST_FOR_EXCEPTION( 02033 !unique, std::logic_error 02034 ,"Error, the std::string \"" << strings[i] << "\" is a duplicate for parameter \"" 02035 << defaultParameterName_ << "\"." 02036 ); 02037 } 02038 setValidValues(strings); 02039 } 02040 02041 02042 template<class IntegralType> 02043 StringToIntegralParameterEntryValidator<IntegralType>::StringToIntegralParameterEntryValidator( 02044 ArrayView<const std::string> const& strings, ArrayView<const IntegralType> const& integralValues 02045 ,std::string const& defaultParameterName 02046 ): 02047 ParameterEntryValidator(), 02048 defaultParameterName_(defaultParameterName) 02049 { 02050 #ifdef TEUCHOS_DEBUG 02051 TEUCHOS_ASSERT_EQUALITY( strings.size(), integralValues.size() ); 02052 #endif 02053 TEUCHOS_TEST_FOR_EXCEPTION( 02054 strings.size() != integralValues.size(), 02055 std::logic_error, 02056 "Error, strings and integraValues must be of the same length." 02057 ); 02058 typedef typename map_t::value_type val_t; 02059 for( int i = 0; i < static_cast<int>(strings.size()); ++i ) { 02060 const bool unique = map_.insert( val_t( strings[i], integralValues[i] ) ).second; 02061 TEUCHOS_TEST_FOR_EXCEPTION( 02062 !unique, std::logic_error 02063 ,"Error, the std::string \"" << strings[i] << "\" is a duplicate for parameter \"" 02064 << defaultParameterName_ << "\"" 02065 ); 02066 } 02067 setValidValues(strings); 02068 } 02069 02070 template<class IntegralType> 02071 StringToIntegralParameterEntryValidator<IntegralType>::StringToIntegralParameterEntryValidator( 02072 ArrayView<const std::string> const& strings 02073 ,ArrayView<const std::string> const& stringsDocs 02074 ,ArrayView<const IntegralType> const& integralValues 02075 ,std::string const& defaultParameterName 02076 ): 02077 ParameterEntryValidator(), 02078 defaultParameterName_(defaultParameterName) 02079 { 02080 #ifdef TEUCHOS_DEBUG 02081 TEUCHOS_ASSERT_EQUALITY( strings.size(), stringsDocs.size() ); 02082 TEUCHOS_ASSERT_EQUALITY( strings.size(), integralValues.size() ); 02083 #endif 02084 TEUCHOS_TEST_FOR_EXCEPTION( 02085 strings.size() != integralValues.size(), 02086 std::logic_error, 02087 "Error, strings and integraValues must be of the same length." 02088 ); 02089 TEUCHOS_TEST_FOR_EXCEPTION( 02090 strings.size() != stringsDocs.size(), 02091 std::logic_error, 02092 "Error, strings and stringsDocs must be of the same length." 02093 ); 02094 typedef typename map_t::value_type val_t; 02095 for( int i = 0; i < static_cast<int>(strings.size()); ++i ) { 02096 const bool unique = map_.insert( val_t( strings[i], integralValues[i] ) ).second; 02097 TEUCHOS_TEST_FOR_EXCEPTION( 02098 !unique, std::logic_error 02099 ,"Error, the std::string \"" << strings[i] << "\" is a duplicate for parameter \"" 02100 << defaultParameterName_ << "\"" 02101 ); 02102 } 02103 setValidValues(strings,&stringsDocs); 02104 } 02105 02106 // Lookup functions 02107 02108 02109 template<class IntegralType> 02110 IntegralType 02111 StringToIntegralParameterEntryValidator<IntegralType>::getIntegralValue( 02112 const std::string &str, const std::string ¶mName 02113 ,const std::string &sublistName 02114 ) const 02115 { 02116 typename map_t::const_iterator itr = map_.find(str); 02117 TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG( 02118 itr == map_.end(), Exceptions::InvalidParameterValue 02119 ,"Error, the value \"" << str << "\" is not recognized for the parameter \"" 02120 << ( paramName.length() ? paramName : defaultParameterName_ ) << "\"" 02121 << "\nin the sublist \"" << sublistName << "\"." 02122 << "\n\nValid values include:" 02123 << "\n {\n" 02124 << validValues_ 02125 << " }" 02126 ); 02127 return (*itr).second; 02128 } 02129 02130 02131 template<class IntegralType> 02132 IntegralType 02133 StringToIntegralParameterEntryValidator<IntegralType>::getIntegralValue( 02134 const ParameterEntry &entry, const std::string ¶mName 02135 ,const std::string &sublistName, const bool activeQuery 02136 ) const 02137 { 02138 const bool validType = ( entry.getAny(activeQuery).type() == typeid(std::string) ); 02139 TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG( 02140 !validType, Exceptions::InvalidParameterType 02141 ,"Error, the parameter {paramName=\""<<(paramName.length()?paramName:defaultParameterName_) 02142 << "\",type=\""<<entry.getAny(activeQuery).typeName()<<"\"}" 02143 << "\nin the sublist \"" << sublistName << "\"" 02144 << "\nhas the wrong type." 02145 << "\n\nThe correct type is \"string\"!" 02146 ); 02147 const std::string 02148 &strValue = any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail! 02149 return getIntegralValue(strValue,paramName,sublistName); // This will validate the value and throw! 02150 } 02151 02152 02153 template<class IntegralType> 02154 std::string 02155 StringToIntegralParameterEntryValidator<IntegralType>::getStringValue( 02156 const ParameterEntry &entry, const std::string ¶mName 02157 ,const std::string &sublistName, const bool activeQuery 02158 ) const 02159 { 02160 // Validate the parameter's type and value 02161 this->getIntegralValue(entry,paramName,sublistName,activeQuery); 02162 // Return the std::string value which is now validated! 02163 return any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail! 02164 } 02165 02166 02167 template<class IntegralType> 02168 IntegralType 02169 StringToIntegralParameterEntryValidator<IntegralType>::getIntegralValue( 02170 ParameterList ¶mList, const std::string ¶mName 02171 ,const std::string &defaultValue 02172 ) const 02173 { 02174 const std::string 02175 &strValue = paramList.get(paramName,defaultValue); 02176 return getIntegralValue(strValue,paramName,paramList.name()); 02177 } 02178 02179 02180 template<class IntegralType> 02181 std::string 02182 StringToIntegralParameterEntryValidator<IntegralType>::getStringValue( 02183 ParameterList ¶mList, const std::string ¶mName 02184 ,const std::string &defaultValue 02185 ) const 02186 { 02187 const std::string 02188 &strValue = paramList.get(paramName,defaultValue); 02189 getIntegralValue(strValue,paramName,paramList.name()); // Validate! 02190 return strValue; 02191 } 02192 02193 template<class IntegralType> 02194 ParameterEntryValidator::ValidStringsList 02195 StringToIntegralParameterEntryValidator<IntegralType>::getStringDocs() const 02196 { 02197 return validStringValuesDocs_; 02198 } 02199 02200 template<class IntegralType> 02201 const std::string& 02202 StringToIntegralParameterEntryValidator<IntegralType>::getDefaultParameterName() const 02203 { 02204 return defaultParameterName_; 02205 } 02206 02207 template<class IntegralType> 02208 std::string 02209 StringToIntegralParameterEntryValidator<IntegralType>::validateString( 02210 const std::string &str, const std::string ¶mName 02211 ,const std::string &sublistName 02212 ) const 02213 { 02214 getIntegralValue(str,paramName,sublistName); // Validate! 02215 return str; 02216 } 02217 02218 02219 // Overridden from ParameterEntryValidator 02220 02221 template<class IntegralType> 02222 const std::string 02223 StringToIntegralParameterEntryValidator<IntegralType>::getXMLTypeName() const{ 02224 return "StringIntegralValidator(" + 02225 TypeNameTraits<IntegralType>::name() + 02226 ")"; 02227 } 02228 02229 template<class IntegralType> 02230 void StringToIntegralParameterEntryValidator<IntegralType>::printDoc( 02231 std::string const& docString 02232 ,std::ostream & out 02233 ) const 02234 { 02235 StrUtils::printLines(out,"# ",docString); 02236 out << "# Valid std::string values:\n"; 02237 out << "# {\n"; 02238 if(validStringValuesDocs_.get()) { 02239 for( int i = 0; i < static_cast<int>(validStringValues_->size()); ++i ) { 02240 out << "# \"" << (*validStringValues_)[i] << "\"\n"; 02241 StrUtils::printLines(out,"# ",(*validStringValuesDocs_)[i] ); 02242 } 02243 } 02244 else { 02245 StrUtils::printLines(out,"# ",validValues_); 02246 // Note: Above validValues_ has for initial spaces already so indent should 02247 // be correct! 02248 } 02249 out << "# }\n"; 02250 } 02251 02252 02253 template<class IntegralType> 02254 ParameterEntryValidator::ValidStringsList 02255 StringToIntegralParameterEntryValidator<IntegralType>::validStringValues() const 02256 { 02257 return validStringValues_; 02258 } 02259 02260 02261 template<class IntegralType> 02262 void StringToIntegralParameterEntryValidator<IntegralType>::validate( 02263 ParameterEntry const& entry 02264 ,std::string const& paramName 02265 ,std::string const& sublistName 02266 ) const 02267 { 02268 this->getIntegralValue(entry,paramName,sublistName,false); 02269 } 02270 02271 02272 // private 02273 02274 template<class IntegralType> 02275 void StringToIntegralParameterEntryValidator<IntegralType>::setValidValues( 02276 ArrayView<const std::string> const& strings 02277 ,ArrayView<const std::string> const* stringsDocs 02278 ) 02279 { 02280 validStringValues_ = rcp(new Array<std::string>(strings)); 02281 if(stringsDocs) 02282 validStringValuesDocs_ = rcp(new Array<std::string>(*stringsDocs)); 02283 // Here I build the list of valid values in the same order as passed in by 02284 // the client! 02285 std::ostringstream oss; 02286 for( int i = 0; i < static_cast<int>(strings.size()); ++i ) { 02287 oss << " \""<<strings[i]<<"\"\n"; 02288 } 02289 // Note: Above four spaces is designed for the error output above. 02290 validValues_ = oss.str(); 02291 } 02292 02293 02294 } // namespace Teuchos 02295 02296 02297 // 02298 // Nonmember function implementations for StringToIntegralParameterEntryValidator 02299 // 02300 02301 02302 template<class IntegralType> 02303 inline 02304 Teuchos::RCP<Teuchos::StringToIntegralParameterEntryValidator<IntegralType> > 02305 Teuchos::stringToIntegralParameterEntryValidator( 02306 ArrayView<const std::string> const& strings, 02307 std::string const& defaultParameterName 02308 ) 02309 { 02310 return rcp( 02311 new StringToIntegralParameterEntryValidator<IntegralType>( 02312 strings, defaultParameterName 02313 ) 02314 ); 02315 } 02316 02317 02318 template<class IntegralType> 02319 inline 02320 Teuchos::RCP<Teuchos::StringToIntegralParameterEntryValidator<IntegralType> > 02321 Teuchos::stringToIntegralParameterEntryValidator( 02322 ArrayView<const std::string> const& strings, 02323 ArrayView<const IntegralType> const& integralValues, 02324 std::string const& defaultParameterName 02325 ) 02326 { 02327 return rcp( 02328 new StringToIntegralParameterEntryValidator<IntegralType>( 02329 strings, integralValues, defaultParameterName 02330 ) 02331 ); 02332 } 02333 02334 02335 template<class IntegralType> 02336 inline 02337 Teuchos::RCP< Teuchos::StringToIntegralParameterEntryValidator<IntegralType> > 02338 Teuchos::stringToIntegralParameterEntryValidator( 02339 ArrayView<const std::string> const& strings, 02340 ArrayView<const std::string> const& stringsDocs, 02341 ArrayView<const IntegralType> const& integralValues, 02342 std::string const& defaultParameterName 02343 ) 02344 { 02345 return rcp( 02346 new StringToIntegralParameterEntryValidator<IntegralType>( 02347 strings, stringsDocs, integralValues, defaultParameterName 02348 ) 02349 ); 02350 } 02351 02352 template<class IntegralType> 02353 void Teuchos::setStringToIntegralParameter( 02354 std::string const& paramName, 02355 std::string const& defaultValue, 02356 std::string const& docString, 02357 ArrayView<const std::string> const& strings, 02358 ParameterList * paramList 02359 ) 02360 { 02361 typedef ParameterEntryValidator PEV; 02362 TEUCHOS_TEST_FOR_EXCEPT(0==paramList); 02363 paramList->set( 02364 paramName, defaultValue, docString, 02365 rcp_implicit_cast<const PEV>( 02366 stringToIntegralParameterEntryValidator<IntegralType>( 02367 strings, paramName 02368 ) 02369 ) 02370 ); 02371 } 02372 02373 02374 template<class IntegralType> 02375 void Teuchos::setStringToIntegralParameter( 02376 std::string const& paramName, 02377 std::string const& defaultValue, 02378 std::string const& docString, 02379 ArrayView<const std::string> const& strings, 02380 ArrayView<const IntegralType> const& integralValues, 02381 ParameterList * paramList 02382 ) 02383 { 02384 typedef ParameterEntryValidator PEV; 02385 TEUCHOS_TEST_FOR_EXCEPT(0==paramList); 02386 paramList->set( 02387 paramName, defaultValue, docString, 02388 rcp_implicit_cast<const PEV>( 02389 stringToIntegralParameterEntryValidator<IntegralType>( 02390 strings, integralValues, paramName 02391 ) 02392 ) 02393 ); 02394 } 02395 02396 02397 template<class IntegralType> 02398 void Teuchos::setStringToIntegralParameter( 02399 std::string const& paramName, 02400 std::string const& defaultValue, 02401 std::string const& docString, 02402 ArrayView<const std::string> const& strings, 02403 ArrayView<const std::string> const& stringsDocs, 02404 ArrayView<const IntegralType> const& integralValues, 02405 ParameterList * paramList 02406 ) 02407 02408 { 02409 typedef ParameterEntryValidator PEV; 02410 TEUCHOS_TEST_FOR_EXCEPT(0==paramList); 02411 paramList->set( 02412 paramName, defaultValue, docString, 02413 rcp_implicit_cast<const PEV>( 02414 stringToIntegralParameterEntryValidator<IntegralType>( 02415 strings, stringsDocs, integralValues, paramName 02416 ) 02417 ) 02418 ); 02419 } 02420 02421 02422 template<class IntegralType> 02423 IntegralType Teuchos::getIntegralValue( 02424 ParameterList const& paramList, std::string const& paramName 02425 ) 02426 { 02427 const ParameterEntry &entry = paramList.getEntry(paramName); 02428 RCP<const StringToIntegralParameterEntryValidator<IntegralType> > 02429 integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>( 02430 entry, paramList, paramName 02431 ); 02432 return integralValidator->getIntegralValue( 02433 entry, paramName, paramList.name(), true ); 02434 } 02435 02436 02437 template<class IntegralType> 02438 std::string Teuchos::getStringValue( 02439 ParameterList const& paramList, std::string const& paramName 02440 ) 02441 { 02442 const ParameterEntry &entry = paramList.getEntry(paramName); 02443 RCP<const StringToIntegralParameterEntryValidator<IntegralType> > 02444 integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>( 02445 entry, paramList, paramName 02446 ); 02447 return integralValidator->getStringValue( 02448 entry, paramName, paramList.name(), true 02449 ); 02450 } 02451 02452 02453 template<class IntegralType> 02454 Teuchos::RCP<const Teuchos::StringToIntegralParameterEntryValidator<IntegralType> > 02455 Teuchos::getStringToIntegralParameterEntryValidator( 02456 ParameterEntry const& entry, ParameterList const& paramList, 02457 std::string const& paramName 02458 ) 02459 { 02460 const RCP<const ParameterEntryValidator> validator = entry.validator(); 02461 TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG( 02462 is_null(validator), Exceptions::InvalidParameterType, 02463 "Error! The parameter \""<<paramName<<"\" exists\n" 02464 "in the parameter (sub)list \""<<paramList.name()<<"\"\n" 02465 "but it does not contain any validator needed to extract\n" 02466 "an integral value of type \""<<TypeNameTraits<IntegralType>::name()<<"\"!" 02467 ); 02468 const RCP<const StringToIntegralParameterEntryValidator<IntegralType> > integralValidator = 02469 rcp_dynamic_cast<const StringToIntegralParameterEntryValidator<IntegralType> >( 02470 validator 02471 ); 02472 TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG( 02473 is_null(integralValidator), Exceptions::InvalidParameterType, 02474 "Error! The parameter \""<<paramName<<"\" exists\n" 02475 "in the parameter (sub)list \""<<paramList.name()<<"\"\n" 02476 "but it contains the wrong type of validator. The expected validator type\n" 02477 "is \""<<TypeNameTraits<StringToIntegralParameterEntryValidator<IntegralType> >::name()<<"\"\n" 02478 "but the contained validator type is \""<<typeName(*validator)<<"\"!" 02479 ); 02480 return integralValidator; 02481 } 02482 02483 02484 #endif // TEUCHOS_STANDARD_PARAMETER_ENTRY_VALIDATORS_H
1.7.4