|
Teuchos Package Browser (Single Doxygen Collection) 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 // This library is free software; you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as 00012 // published by the Free Software Foundation; either version 2.1 of the 00013 // License, or (at your option) any later version. 00014 // 00015 // This library is distributed in the hope that it will be useful, but 00016 // WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this library; if not, write to the Free Software 00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00023 // USA 00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00025 // 00026 // *********************************************************************** 00027 // @HEADER 00028 00029 #ifndef TEUCHOS_STANDARD_PARAMETER_ENTRY_VALIDATORS_H 00030 #define TEUCHOS_STANDARD_PARAMETER_ENTRY_VALIDATORS_H 00031 00032 #include "Teuchos_ParameterEntryValidator.hpp" 00033 #include "Teuchos_ParameterList.hpp" 00034 #include "Teuchos_ParameterListExceptions.hpp" 00035 #include "Teuchos_VerbosityLevel.hpp" 00036 #include "Teuchos_Array.hpp" 00037 #include "Teuchos_Assert.hpp" 00038 #include "Teuchos_StrUtils.hpp" 00039 #include "Teuchos_TypeNameTraits.hpp" 00040 00041 00042 namespace Teuchos { 00043 00044 00054 template<class IntegralType> 00055 class StringToIntegralParameterEntryValidator : public ParameterEntryValidator { 00056 public: 00057 00060 00069 StringToIntegralParameterEntryValidator( 00070 ArrayView<const std::string> const& strings, 00071 std::string const& defaultParameterName 00072 ); 00073 00089 StringToIntegralParameterEntryValidator( 00090 ArrayView<const std::string> const& strings, 00091 ArrayView<const IntegralType> const& integralValues, 00092 std::string const& defaultParameterName 00093 ); 00094 00113 StringToIntegralParameterEntryValidator( 00114 ArrayView<const std::string> const& strings, 00115 ArrayView<const std::string> const& stringsDocs, 00116 ArrayView<const IntegralType> const& integralValues, 00117 std::string const& defaultParameterName 00118 ); 00119 00121 00124 00135 IntegralType getIntegralValue( 00136 const std::string &str, const std::string ¶mName = "", 00137 const std::string &sublistName = "" 00138 ) const; 00139 00153 IntegralType getIntegralValue( 00154 const ParameterEntry &entry, const std::string ¶mName = "", 00155 const std::string &sublistName = "", const bool activeQuery = true 00156 ) const; 00157 00172 std::string getStringValue( 00173 const ParameterEntry &entry, const std::string ¶mName = "", 00174 const std::string &sublistName = "", const bool activeQuery = true 00175 ) const; 00176 00181 IntegralType getIntegralValue( 00182 ParameterList ¶mList, const std::string ¶mName, 00183 const std::string &defaultValue 00184 ) const; 00185 00189 std::string getStringValue( 00190 ParameterList ¶mList, const std::string ¶mName, 00191 const std::string &defaultValue 00192 ) const; 00193 00203 std::string validateString( 00204 const std::string &str, const std::string ¶mName = "", 00205 const std::string &sublistName = "" 00206 ) const; 00207 00209 00212 00214 void printDoc( 00215 std::string const& docString, 00216 std::ostream & out 00217 ) const; 00218 00220 Teuchos::RCP<const Array<std::string> > 00221 validStringValues() const; 00222 00224 void validate( 00225 ParameterEntry const& entry, 00226 std::string const& paramName, 00227 std::string const& sublistName 00228 ) const; 00229 00231 00232 private: 00233 00234 typedef std::map<std::string,IntegralType> map_t; 00235 std::string defaultParameterName_; 00236 std::string validValues_; 00237 RCP<const Array<std::string> > validStringValues_; 00238 RCP<const Array<std::string> > validStringValuesDocs_; 00239 map_t map_; 00240 00241 void setValidValues( 00242 ArrayView<const std::string> const& strings, 00243 ArrayView<const std::string> const* stringsDocs = NULL 00244 ); 00245 00246 // Not defined and not to be called. 00247 StringToIntegralParameterEntryValidator(); 00248 00249 }; 00250 00251 00256 template<class IntegralType> 00257 RCP<StringToIntegralParameterEntryValidator<IntegralType> > 00258 stringToIntegralParameterEntryValidator( 00259 ArrayView<const std::string> const& strings, 00260 std::string const& defaultParameterName 00261 ); 00262 00263 00268 template<class IntegralType> 00269 RCP<StringToIntegralParameterEntryValidator<IntegralType> > 00270 stringToIntegralParameterEntryValidator( 00271 ArrayView<const std::string> const& strings, 00272 ArrayView<const IntegralType> const& integralValues, 00273 std::string const& defaultParameterName 00274 ); 00275 00276 00281 template<class IntegralType> 00282 RCP<StringToIntegralParameterEntryValidator<IntegralType> > 00283 stringToIntegralParameterEntryValidator( 00284 ArrayView<const std::string> const& strings, 00285 ArrayView<const std::string> const& stringsDocs, 00286 ArrayView<const IntegralType> const& integralValues, 00287 std::string const& defaultParameterName 00288 ); 00289 00290 00301 template<class IntegralType> 00302 void setStringToIntegralParameter( 00303 std::string const& paramName, 00304 std::string const& defaultValue, 00305 std::string const& docString, 00306 ArrayView<const std::string> const& strings, 00307 ParameterList * paramList 00308 ); 00309 00310 00321 template<class IntegralType> 00322 void setStringToIntegralParameter( 00323 std::string const& paramName, 00324 std::string const& defaultValue, 00325 std::string const& docString, 00326 ArrayView<const std::string> const& strings, 00327 ArrayView<const IntegralType> const& integralValues, 00328 ParameterList * paramList 00329 ); 00330 00331 00343 template<class IntegralType> 00344 void setStringToIntegralParameter( 00345 std::string const& paramName, 00346 std::string const& defaultValue, 00347 std::string const& docString, 00348 ArrayView<const std::string> const& strings, 00349 ArrayView<const std::string> const& stringsDocs, 00350 ArrayView<const IntegralType> const& integralValues, 00351 ParameterList * paramList 00352 ); 00353 00354 00364 template<class IntegralType> 00365 IntegralType getIntegralValue( 00366 ParameterList const& paramList, std::string const& paramName 00367 ); 00368 00369 00380 template<class IntegralType> 00381 std::string getStringValue( 00382 ParameterList const& paramList, std::string const& paramName 00383 ); 00384 00385 00391 template<class IntegralType> 00392 RCP<const StringToIntegralParameterEntryValidator<IntegralType> > 00393 getStringToIntegralParameterEntryValidator( 00394 ParameterEntry const& entry, ParameterList const& paramList, 00395 std::string const& paramName 00396 ); 00397 00398 00404 std::string getVerbosityLevelParameterValueName( 00405 const EVerbosityLevel verbLevel 00406 ); 00407 00408 00413 RCP<StringToIntegralParameterEntryValidator<EVerbosityLevel> > 00414 verbosityLevelParameterEntryValidator(std::string const& defaultParameterName); 00415 00416 00427 class TEUCHOS_LIB_DLL_EXPORT AnyNumberParameterEntryValidator : public ParameterEntryValidator { 00428 public: 00429 00432 00434 enum EPreferredType { PREFER_INT, PREFER_DOUBLE, PREFER_STRING }; 00435 00436 00438 class AcceptedTypes { 00439 public: 00441 AcceptedTypes( bool allowAllTypesByDefault = true ) 00442 :allowInt_(allowAllTypesByDefault),allowDouble_(allowAllTypesByDefault), 00443 allowString_(allowAllTypesByDefault) 00444 {} 00446 AcceptedTypes& allowInt( bool _allowInt ) 00447 { allowInt_ = _allowInt; return *this; } 00449 AcceptedTypes& allowDouble( bool _allowDouble ) 00450 { allowDouble_ = _allowDouble; return *this; } 00452 AcceptedTypes& allowString( bool _allowString ) 00453 { allowString_ = _allowString; return *this; } 00455 bool allowInt() const { return allowInt_; } 00457 bool allowDouble() const { return allowDouble_; } 00459 bool allowString() const { return allowString_; } 00460 private: 00461 bool allowInt_; 00462 bool allowDouble_; 00463 bool allowString_; 00464 }; 00465 00466 00467 00469 00472 00476 AnyNumberParameterEntryValidator(); 00477 00487 AnyNumberParameterEntryValidator( 00488 EPreferredType const preferredType, 00489 AcceptedTypes const& acceptedTypes 00490 ); 00491 00493 00496 00498 int getInt( 00499 const ParameterEntry &entry, const std::string ¶mName = "", 00500 const std::string &sublistName = "", const bool activeQuery = true 00501 ) const; 00502 00504 double getDouble( 00505 const ParameterEntry &entry, const std::string ¶mName = "", 00506 const std::string &sublistName = "", const bool activeQuery = true 00507 ) const; 00508 00510 std::string getString( 00511 const ParameterEntry &entry, const std::string ¶mName = "", 00512 const std::string &sublistName = "", const bool activeQuery = true 00513 ) const; 00514 00518 int getInt( 00519 ParameterList ¶mList, const std::string ¶mName, 00520 const int defaultValue 00521 ) const; 00522 00526 double getDouble( 00527 ParameterList ¶mList, const std::string ¶mName, 00528 const double defaultValue 00529 ) const; 00530 00534 std::string getString( 00535 ParameterList ¶mList, const std::string ¶mName, 00536 const std::string &defaultValue 00537 ) const; 00538 00540 00543 00545 void printDoc( 00546 std::string const& docString, 00547 std::ostream & out 00548 ) const; 00549 00551 Teuchos::RCP<const Array<std::string> > 00552 validStringValues() const; 00553 00555 void validate( 00556 ParameterEntry const& entry, 00557 std::string const& paramName, 00558 std::string const& sublistName 00559 ) const; 00560 00562 void validateAndModify( 00563 std::string const& paramName, 00564 std::string const& sublistName, 00565 ParameterEntry * entry 00566 ) const; 00567 00569 00570 private: 00571 00572 // //////////////////////////// 00573 // Private data members 00574 00575 EPreferredType preferredType_; 00576 std::string acceptedTypesString_; 00577 00578 //use pragmas to disable some false-positive warnings for windows sharedlibs export 00579 #ifdef _MSC_VER 00580 #pragma warning(push) 00581 #pragma warning(disable:4251) 00582 #endif 00583 const AcceptedTypes acceptedTypes_; 00584 #ifdef _MSC_VER 00585 #pragma warning(pop) 00586 #endif 00587 00588 // //////////////////////////// 00589 // Private member functions 00590 00591 void finishInitialization(); 00592 00593 void throwTypeError( 00594 ParameterEntry const& entry, 00595 std::string const& paramName, 00596 std::string const& sublistName 00597 ) const; 00598 00599 }; 00600 00601 00602 // Nonmember helper functions 00603 00604 00609 TEUCHOS_LIB_DLL_EXPORT RCP<AnyNumberParameterEntryValidator> 00610 anyNumberParameterEntryValidator( 00611 AnyNumberParameterEntryValidator::EPreferredType const preferredType, 00612 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes 00613 ); 00614 00615 00621 TEUCHOS_LIB_DLL_EXPORT void setIntParameter( 00622 std::string const& paramName, 00623 int const value, std::string const& docString, 00624 ParameterList *paramList, 00625 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes 00626 = AnyNumberParameterEntryValidator::AcceptedTypes() 00627 ); 00628 00629 00635 TEUCHOS_LIB_DLL_EXPORT void setDoubleParameter( 00636 std::string const& paramName, 00637 double const& value, std::string const& docString, 00638 ParameterList *paramList, 00639 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes 00640 = AnyNumberParameterEntryValidator::AcceptedTypes() 00641 ); 00642 00643 00649 TEUCHOS_LIB_DLL_EXPORT void setNumericStringParameter( 00650 std::string const& paramName, 00651 std::string const& value, std::string const& docString, 00652 ParameterList *paramList, 00653 AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes 00654 = AnyNumberParameterEntryValidator::AcceptedTypes() 00655 ); 00656 00657 00672 TEUCHOS_LIB_DLL_EXPORT int getIntParameter( 00673 ParameterList const& paramList, std::string const& paramName 00674 ); 00675 00676 00691 TEUCHOS_LIB_DLL_EXPORT double getDoubleParameter( 00692 ParameterList const& paramList, 00693 std::string const& paramName 00694 ); 00695 00696 00711 TEUCHOS_LIB_DLL_EXPORT std::string getNumericStringParameter( 00712 ParameterList const& paramList, 00713 std::string const& paramName 00714 ); 00715 00716 00717 // /////////////////////////// 00718 // Implementations 00719 00720 00721 // 00722 // StringToIntegralParameterEntryValidator 00723 // 00724 00725 00726 // Constructors 00727 00728 00729 template<class IntegralType> 00730 StringToIntegralParameterEntryValidator<IntegralType>::StringToIntegralParameterEntryValidator( 00731 ArrayView<const std::string> const& strings, std::string const& defaultParameterName 00732 ) 00733 :defaultParameterName_(defaultParameterName) 00734 { 00735 typedef typename map_t::value_type val_t; 00736 for( int i = 0; i < static_cast<int>(strings.size()); ++i ) { 00737 const bool unique = map_.insert( val_t( strings[i], i ) ).second; 00738 TEST_FOR_EXCEPTION( 00739 !unique, std::logic_error 00740 ,"Error, the std::string \"" << strings[i] << "\" is a duplicate for parameter \"" 00741 << defaultParameterName_ << "\"." 00742 ); 00743 } 00744 setValidValues(strings); 00745 } 00746 00747 00748 template<class IntegralType> 00749 StringToIntegralParameterEntryValidator<IntegralType>::StringToIntegralParameterEntryValidator( 00750 ArrayView<const std::string> const& strings, ArrayView<const IntegralType> const& integralValues 00751 ,std::string const& defaultParameterName 00752 ) 00753 :defaultParameterName_(defaultParameterName) 00754 { 00755 #ifdef TEUCHOS_DEBUG 00756 TEUCHOS_ASSERT_EQUALITY( strings.size(), integralValues.size() ); 00757 #endif 00758 typedef typename map_t::value_type val_t; 00759 for( int i = 0; i < static_cast<int>(strings.size()); ++i ) { 00760 const bool unique = map_.insert( val_t( strings[i], integralValues[i] ) ).second; 00761 TEST_FOR_EXCEPTION( 00762 !unique, std::logic_error 00763 ,"Error, the std::string \"" << strings[i] << "\" is a duplicate for parameter \"" 00764 << defaultParameterName_ << "\"" 00765 ); 00766 } 00767 setValidValues(strings); 00768 } 00769 00770 00771 template<class IntegralType> 00772 StringToIntegralParameterEntryValidator<IntegralType>::StringToIntegralParameterEntryValidator( 00773 ArrayView<const std::string> const& strings 00774 ,ArrayView<const std::string> const& stringsDocs 00775 ,ArrayView<const IntegralType> const& integralValues 00776 ,std::string const& defaultParameterName 00777 ) 00778 :defaultParameterName_(defaultParameterName) 00779 { 00780 #ifdef TEUCHOS_DEBUG 00781 TEUCHOS_ASSERT_EQUALITY( strings.size(), stringsDocs.size() ); 00782 TEUCHOS_ASSERT_EQUALITY( strings.size(), integralValues.size() ); 00783 #endif 00784 typedef typename map_t::value_type val_t; 00785 for( int i = 0; i < static_cast<int>(strings.size()); ++i ) { 00786 const bool unique = map_.insert( val_t( strings[i], integralValues[i] ) ).second; 00787 TEST_FOR_EXCEPTION( 00788 !unique, std::logic_error 00789 ,"Error, the std::string \"" << strings[i] << "\" is a duplicate for parameter \"" 00790 << defaultParameterName_ << "\"" 00791 ); 00792 } 00793 setValidValues(strings,&stringsDocs); 00794 } 00795 00796 00797 // Lookup functions 00798 00799 00800 template<class IntegralType> 00801 IntegralType 00802 StringToIntegralParameterEntryValidator<IntegralType>::getIntegralValue( 00803 const std::string &str, const std::string ¶mName 00804 ,const std::string &sublistName 00805 ) const 00806 { 00807 typename map_t::const_iterator itr = map_.find(str); 00808 TEST_FOR_EXCEPTION_PURE_MSG( 00809 itr == map_.end(), Exceptions::InvalidParameterValue 00810 ,"Error, the value \"" << str << "\" is not recognized for the parameter \"" 00811 << ( paramName.length() ? paramName : defaultParameterName_ ) << "\"" 00812 << "\nin the sublist \"" << sublistName << "\"." 00813 << "\n\nValid values include:" 00814 << "\n {\n" 00815 << validValues_ 00816 << " }" 00817 ); 00818 return (*itr).second; 00819 } 00820 00821 00822 template<class IntegralType> 00823 IntegralType 00824 StringToIntegralParameterEntryValidator<IntegralType>::getIntegralValue( 00825 const ParameterEntry &entry, const std::string ¶mName 00826 ,const std::string &sublistName, const bool activeQuery 00827 ) const 00828 { 00829 const bool validType = ( entry.getAny(activeQuery).type() == typeid(std::string) ); 00830 TEST_FOR_EXCEPTION_PURE_MSG( 00831 !validType, Exceptions::InvalidParameterType 00832 ,"Error, the parameter {paramName=\""<<(paramName.length()?paramName:defaultParameterName_) 00833 << "\",type=\""<<entry.getAny(activeQuery).typeName()<<"\"}" 00834 << "\nin the sublist \"" << sublistName << "\"" 00835 << "\nhas the wrong type." 00836 << "\n\nThe correct type is \"string\"!" 00837 ); 00838 const std::string 00839 &strValue = any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail! 00840 return getIntegralValue(strValue,paramName,sublistName); // This will validate the value and throw! 00841 } 00842 00843 00844 template<class IntegralType> 00845 std::string 00846 StringToIntegralParameterEntryValidator<IntegralType>::getStringValue( 00847 const ParameterEntry &entry, const std::string ¶mName 00848 ,const std::string &sublistName, const bool activeQuery 00849 ) const 00850 { 00851 // Validate the parameter's type and value 00852 this->getIntegralValue(entry,paramName,sublistName,activeQuery); 00853 // Return the std::string value which is now validated! 00854 return any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail! 00855 } 00856 00857 00858 template<class IntegralType> 00859 IntegralType 00860 StringToIntegralParameterEntryValidator<IntegralType>::getIntegralValue( 00861 ParameterList ¶mList, const std::string ¶mName 00862 ,const std::string &defaultValue 00863 ) const 00864 { 00865 const std::string 00866 &strValue = paramList.get(paramName,defaultValue); 00867 return getIntegralValue(strValue,paramName,paramList.name()); 00868 } 00869 00870 00871 template<class IntegralType> 00872 std::string 00873 StringToIntegralParameterEntryValidator<IntegralType>::getStringValue( 00874 ParameterList ¶mList, const std::string ¶mName 00875 ,const std::string &defaultValue 00876 ) const 00877 { 00878 const std::string 00879 &strValue = paramList.get(paramName,defaultValue); 00880 getIntegralValue(strValue,paramName,paramList.name()); // Validate! 00881 return strValue; 00882 } 00883 00884 00885 template<class IntegralType> 00886 std::string 00887 StringToIntegralParameterEntryValidator<IntegralType>::validateString( 00888 const std::string &str, const std::string ¶mName 00889 ,const std::string &sublistName 00890 ) const 00891 { 00892 getIntegralValue(str,paramName,sublistName); // Validate! 00893 return str; 00894 } 00895 00896 00897 // Overridden from ParameterEntryValidator 00898 00899 00900 template<class IntegralType> 00901 void StringToIntegralParameterEntryValidator<IntegralType>::printDoc( 00902 std::string const& docString 00903 ,std::ostream & out 00904 ) const 00905 { 00906 StrUtils::printLines(out,"# ",docString); 00907 out << "# Valid std::string values:\n"; 00908 out << "# {\n"; 00909 if(validStringValuesDocs_.get()) { 00910 for( int i = 0; i < static_cast<int>(validStringValues_->size()); ++i ) { 00911 out << "# \"" << (*validStringValues_)[i] << "\"\n"; 00912 StrUtils::printLines(out,"# ",(*validStringValuesDocs_)[i] ); 00913 } 00914 } 00915 else { 00916 StrUtils::printLines(out,"# ",validValues_); 00917 // Note: Above validValues_ has for initial spaces already so indent should 00918 // be correct! 00919 } 00920 out << "# }\n"; 00921 } 00922 00923 00924 template<class IntegralType> 00925 Teuchos::RCP<const Array<std::string> > 00926 StringToIntegralParameterEntryValidator<IntegralType>::validStringValues() const 00927 { 00928 return validStringValues_; 00929 } 00930 00931 00932 template<class IntegralType> 00933 void StringToIntegralParameterEntryValidator<IntegralType>::validate( 00934 ParameterEntry const& entry 00935 ,std::string const& paramName 00936 ,std::string const& sublistName 00937 ) const 00938 { 00939 this->getIntegralValue(entry,paramName,sublistName,false); 00940 } 00941 00942 00943 // private 00944 00945 00946 template<class IntegralType> 00947 void StringToIntegralParameterEntryValidator<IntegralType>::setValidValues( 00948 ArrayView<const std::string> const& strings 00949 ,ArrayView<const std::string> const* stringsDocs 00950 ) 00951 { 00952 validStringValues_ = rcp(new Array<std::string>(strings)); 00953 if(stringsDocs) 00954 validStringValuesDocs_ = rcp(new Array<std::string>(*stringsDocs)); 00955 // Here I build the list of valid values in the same order as passed in by 00956 // the client! 00957 std::ostringstream oss; 00958 typename map_t::const_iterator itr = map_.begin(); 00959 for( int i = 0; i < static_cast<int>(strings.size()); ++i ) { 00960 oss << " \""<<strings[i]<<"\"\n"; 00961 } 00962 // Note: Above four spaces is designed for the error output above. 00963 validValues_ = oss.str(); 00964 } 00965 00966 00967 } // namespace Teuchos 00968 00969 00970 // 00971 // Nonmember function implementations for StringToIntegralParameterEntryValidator 00972 // 00973 00974 00975 template<class IntegralType> 00976 inline 00977 Teuchos::RCP<Teuchos::StringToIntegralParameterEntryValidator<IntegralType> > 00978 Teuchos::stringToIntegralParameterEntryValidator( 00979 ArrayView<const std::string> const& strings, 00980 std::string const& defaultParameterName 00981 ) 00982 { 00983 return rcp( 00984 new StringToIntegralParameterEntryValidator<IntegralType>( 00985 strings, defaultParameterName 00986 ) 00987 ); 00988 } 00989 00990 00991 template<class IntegralType> 00992 inline 00993 Teuchos::RCP<Teuchos::StringToIntegralParameterEntryValidator<IntegralType> > 00994 Teuchos::stringToIntegralParameterEntryValidator( 00995 ArrayView<const std::string> const& strings, 00996 ArrayView<const IntegralType> const& integralValues, 00997 std::string const& defaultParameterName 00998 ) 00999 { 01000 return rcp( 01001 new StringToIntegralParameterEntryValidator<IntegralType>( 01002 strings, integralValues, defaultParameterName 01003 ) 01004 ); 01005 } 01006 01007 01008 template<class IntegralType> 01009 inline 01010 Teuchos::RCP< Teuchos::StringToIntegralParameterEntryValidator<IntegralType> > 01011 Teuchos::stringToIntegralParameterEntryValidator( 01012 ArrayView<const std::string> const& strings, 01013 ArrayView<const std::string> const& stringsDocs, 01014 ArrayView<const IntegralType> const& integralValues, 01015 std::string const& defaultParameterName 01016 ) 01017 { 01018 return rcp( 01019 new StringToIntegralParameterEntryValidator<IntegralType>( 01020 strings, stringsDocs, integralValues, defaultParameterName 01021 ) 01022 ); 01023 } 01024 01025 01026 template<class IntegralType> 01027 void Teuchos::setStringToIntegralParameter( 01028 std::string const& paramName, 01029 std::string const& defaultValue, 01030 std::string const& docString, 01031 ArrayView<const std::string> const& strings, 01032 ParameterList * paramList 01033 ) 01034 { 01035 typedef ParameterEntryValidator PEV; 01036 TEST_FOR_EXCEPT(0==paramList); 01037 paramList->set( 01038 paramName, defaultValue, docString, 01039 rcp_implicit_cast<const PEV>( 01040 stringToIntegralParameterEntryValidator<IntegralType>( 01041 strings, paramName 01042 ) 01043 ) 01044 ); 01045 } 01046 01047 01048 template<class IntegralType> 01049 void Teuchos::setStringToIntegralParameter( 01050 std::string const& paramName, 01051 std::string const& defaultValue, 01052 std::string const& docString, 01053 ArrayView<const std::string> const& strings, 01054 ArrayView<const IntegralType> const& integralValues, 01055 ParameterList * paramList 01056 ) 01057 { 01058 typedef ParameterEntryValidator PEV; 01059 TEST_FOR_EXCEPT(0==paramList); 01060 paramList->set( 01061 paramName, defaultValue, docString, 01062 rcp_implicit_cast<const PEV>( 01063 stringToIntegralParameterEntryValidator<IntegralType>( 01064 strings, integralValues, paramName 01065 ) 01066 ) 01067 ); 01068 } 01069 01070 01071 template<class IntegralType> 01072 void Teuchos::setStringToIntegralParameter( 01073 std::string const& paramName, 01074 std::string const& defaultValue, 01075 std::string const& docString, 01076 ArrayView<const std::string> const& strings, 01077 ArrayView<const std::string> const& stringsDocs, 01078 ArrayView<const IntegralType> const& integralValues, 01079 ParameterList * paramList 01080 ) 01081 01082 { 01083 typedef ParameterEntryValidator PEV; 01084 TEST_FOR_EXCEPT(0==paramList); 01085 paramList->set( 01086 paramName, defaultValue, docString, 01087 rcp_implicit_cast<const PEV>( 01088 stringToIntegralParameterEntryValidator<IntegralType>( 01089 strings, stringsDocs, integralValues, paramName 01090 ) 01091 ) 01092 ); 01093 } 01094 01095 01096 template<class IntegralType> 01097 IntegralType Teuchos::getIntegralValue( 01098 ParameterList const& paramList, std::string const& paramName 01099 ) 01100 { 01101 const ParameterEntry &entry = paramList.getEntry(paramName); 01102 RCP<const StringToIntegralParameterEntryValidator<IntegralType> > 01103 integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>( 01104 entry, paramList, paramName 01105 ); 01106 return integralValidator->getIntegralValue( 01107 entry, paramName, paramList.name(), true 01108 ); 01109 } 01110 01111 01112 template<class IntegralType> 01113 std::string Teuchos::getStringValue( 01114 ParameterList const& paramList, std::string const& paramName 01115 ) 01116 { 01117 const ParameterEntry &entry = paramList.getEntry(paramName); 01118 RCP<const StringToIntegralParameterEntryValidator<IntegralType> > 01119 integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>( 01120 entry, paramList, paramName 01121 ); 01122 return integralValidator->getStringValue( 01123 entry, paramName, paramList.name(), true 01124 ); 01125 } 01126 01127 01128 template<class IntegralType> 01129 Teuchos::RCP<const Teuchos::StringToIntegralParameterEntryValidator<IntegralType> > 01130 Teuchos::getStringToIntegralParameterEntryValidator( 01131 ParameterEntry const& entry, ParameterList const& paramList, 01132 std::string const& paramName 01133 ) 01134 { 01135 RCP<const ParameterEntryValidator> 01136 validator = entry.validator(); 01137 TEST_FOR_EXCEPTION_PURE_MSG( 01138 is_null(validator), Exceptions::InvalidParameterType, 01139 "Error! The parameter \""<<paramName<<"\" exists\n" 01140 "in the parameter (sub)list \""<<paramList.name()<<"\"\n" 01141 "but it does not contain any validator needed to extract\n" 01142 "an integral value of type \""<<TypeNameTraits<IntegralType>::name()<<"\"!" 01143 ); 01144 RCP<const StringToIntegralParameterEntryValidator<IntegralType> > 01145 integralValidator 01146 = 01147 rcp_dynamic_cast<const StringToIntegralParameterEntryValidator<IntegralType> >( 01148 validator 01149 ); 01150 TEST_FOR_EXCEPTION_PURE_MSG( 01151 is_null(integralValidator), Exceptions::InvalidParameterType, 01152 "Error! The parameter \""<<paramName<<"\" exists\n" 01153 "in the parameter (sub)list \""<<paramList.name()<<"\"\n" 01154 "but it contains the wrong type of validator. The expected validator type\n" 01155 "is \""<<TypeNameTraits<StringToIntegralParameterEntryValidator<IntegralType> >::name()<<"\"\n" 01156 "but the contained validator type is \""<<typeName(*validator)<<"\"!" 01157 ); 01158 return integralValidator; 01159 } 01160 01161 01162 #endif // TEUCHOS_STANDARD_PARAMETER_ENTRY_VALIDATORS_H
1.7.4