Teuchos - Trilinos Tools Package Version of the Day
Teuchos_StandardParameterEntryValidators.hpp
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 &paramName = "",
00154     const std::string &sublistName = ""
00155     ) const;
00156 
00170   IntegralType getIntegralValue(
00171     const ParameterEntry &entry, const std::string &paramName = "",
00172     const std::string &sublistName = "", const bool activeQuery = true
00173     ) const;
00174 
00189   std::string getStringValue(
00190     const ParameterEntry &entry, const std::string &paramName = "",
00191     const std::string &sublistName = "", const bool activeQuery = true
00192     ) const;
00193 
00198   IntegralType getIntegralValue(
00199     ParameterList &paramList, const std::string &paramName,
00200     const std::string &defaultValue
00201     ) const;
00202 
00206   std::string getStringValue(
00207     ParameterList &paramList, const std::string &paramName,
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 &paramName = "",
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 TEUCHOSPARAMETERLIST_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 &paramName = "",
00573     const std::string &sublistName = "", const bool activeQuery = true
00574     ) const;
00575 
00577   double getDouble(
00578     const ParameterEntry &entry, const std::string &paramName = "",
00579     const std::string &sublistName = "", const bool activeQuery = true
00580     ) const;
00581 
00583   std::string getString(
00584     const ParameterEntry &entry, const std::string &paramName = "",
00585     const std::string &sublistName = "", const bool activeQuery = true
00586     ) const;
00587 
00591   int getInt(
00592     ParameterList &paramList, const std::string &paramName,
00593     const int defaultValue
00594     ) const;
00595 
00599   double getDouble(
00600     ParameterList &paramList, const std::string &paramName,
00601     const double defaultValue
00602     ) const;
00603 
00607   std::string getString(
00608     ParameterList &paramList, const std::string &paramName,
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 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT RCP<AnyNumberParameterEntryValidator>
00763 anyNumberParameterEntryValidator();
00764 
00765 
00770 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT RCP<AnyNumberParameterEntryValidator>
00771 anyNumberParameterEntryValidator(
00772   AnyNumberParameterEntryValidator::EPreferredType const preferredType,
00773   AnyNumberParameterEntryValidator::AcceptedTypes const& acceptedTypes
00774   );
00775 
00781 TEUCHOSPARAMETERLIST_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 TEUCHOSPARAMETERLIST_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 TEUCHOSPARAMETERLIST_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 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT int getIntParameter(
00833   ParameterList const& paramList, std::string const& paramName
00834   );
00835 
00836 
00851 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT double getDoubleParameter(
00852   ParameterList const& paramList,
00853   std::string const& paramName
00854   );
00855 
00856 
00872 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT std::string getNumericStringParameter(
00873   ParameterList const& paramList,
00874   std::string const& paramName
00875   );
00876 
00882 template<>
00883 class TEUCHOSPARAMETERLIST_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 &paramName,
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 &paramName,
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     "The \"" << paramName << "\"" <<
01285     " parameter in the \"" << sublistName <<
01286     "\" sublist is has an error." << std::endl << std::endl <<
01287     "Error: The value that you entered was the wrong type." << std::endl <<
01288     "Parameter: " << paramName << std::endl <<
01289     "Type specified: " << entryName << std::endl <<
01290     "Type accepted: " << Teuchos::TypeNameTraits<T>::name() << std::endl);
01291 
01292   bool isValueInRange;
01293   any_cast<T>(anyValue) >= minVal && any_cast<T>(anyValue) <= maxVal
01294     ? isValueInRange = true : isValueInRange=false;
01295   TEUCHOS_TEST_FOR_EXCEPTION(!(isValueInRange),
01296     Exceptions::InvalidParameterValue,
01297     "The \"" << paramName << "\"" <<
01298     " parameter in the \"" << sublistName <<
01299     "\" sublist is has an error." << std::endl << std::endl <<
01300     "Error: The value that was entered doesn't fall with in " <<
01301     "the range set by the validator" << std::endl <<
01302     "Parameter: " << paramName  << std::endl <<
01303     "Min: " << minVal << std::endl <<
01304     "Max: " << maxVal << std::endl <<
01305     "Value entered: " <<
01306     (any_cast<T>(anyValue)) << std::endl << std::endl);
01307 }
01308 
01314 template<class T>
01315 class DummyObjectGetter<EnhancedNumberValidator<T> >{
01316 
01317 public:
01318 
01321 
01325   static RCP<EnhancedNumberValidator<T> > getDummyObject();
01326 
01328 };
01329 
01330 template<class T>
01331 RCP<EnhancedNumberValidator<T> >
01332   DummyObjectGetter<EnhancedNumberValidator<T> >::getDummyObject()
01333 {
01334   return rcp(new EnhancedNumberValidator<T>);
01335 }
01336 
01345 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT FileNameValidator : public ParameterEntryValidator {
01346 
01347 public:
01348 
01351 
01354   static bool mustAlreadyExistDefault() { return false; }
01355 
01357 
01360 
01366   FileNameValidator(bool mustAlreadyExist = mustAlreadyExistDefault());
01367 
01369 
01371 
01372 
01378   bool fileMustExist() const;
01379 
01381 
01383 
01384 
01393   bool setFileMustExist(bool shouldFileExist);
01394 
01396 
01399 
01401   ValidStringsList validStringValues() const;
01402 
01404   void validate(
01405     ParameterEntry const &entry,
01406     std::string const &paramName,
01407     std::string const &sublistName) const;
01408 
01410   const std::string getXMLTypeName() const;
01411 
01413   void printDoc(std::string const &docString, std::ostream &out) const;
01414 
01416 
01417 private:
01418 
01421 
01425   bool mustAlreadyExist_;
01426 
01428 
01429 };
01430 
01436 template<>
01437 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT DummyObjectGetter<FileNameValidator>{
01438 
01439 public:
01440 
01443 
01447   static RCP<FileNameValidator> getDummyObject();
01448 
01450 
01451 };
01452 
01460 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT StringValidator : public ParameterEntryValidator {
01461 
01462 public:
01463 
01466 
01469   StringValidator();
01470 
01475   StringValidator(const Teuchos::Array<std::string> &validStrings);
01476 
01478 
01480 
01481 
01489   ValidStringsList setValidStrings(
01490     const Teuchos::Array<std::string> &validStrings);
01491 
01493 
01496 
01498   ValidStringsList validStringValues() const;
01499 
01501   void validate(ParameterEntry const &entry, std::string const &paramName,
01502     std::string const &sublistName) const;
01503 
01505   const std::string getXMLTypeName() const;
01506 
01508   void printDoc(std::string const &docString, std::ostream &out) const;
01509 
01511 
01512 private:
01513 
01516 
01519   ValidStringsList validStrings_;
01520 
01522 
01523 };
01524 
01530 template<>
01531 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT DummyObjectGetter<StringValidator>{
01532 
01533 public:
01534 
01537 
01541   static RCP<StringValidator> getDummyObject();
01542 
01544 
01545 };
01546 
01547 
01551 template<class ValidatorType, class EntryType>
01552 class AbstractArrayValidator : public ParameterEntryValidator {
01553 
01554 public:
01555 
01558 
01565   AbstractArrayValidator(RCP<const ValidatorType> prototypeValidator):
01566     ParameterEntryValidator(),
01567     prototypeValidator_(prototypeValidator){}
01568 
01570 
01573 
01575   RCP<const ValidatorType> getPrototype() const{
01576     return prototypeValidator_;
01577   }
01578 
01580 
01583 
01585   ValidStringsList validStringValues() const {
01586     return prototypeValidator_->validStringValues();
01587   }
01588 
01590 
01591 private:
01592 
01595 
01598   RCP<const ValidatorType> prototypeValidator_;
01599 
01601   AbstractArrayValidator<ValidatorType, EntryType>();
01602 
01604 
01605 };
01606 
01619 template<class ValidatorType, class EntryType>
01620 class TwoDArrayValidator : public AbstractArrayValidator<ValidatorType, EntryType>{
01621 public:
01624 
01631   TwoDArrayValidator(RCP<const ValidatorType> prototypeValidator):
01632     AbstractArrayValidator<ValidatorType, EntryType>(prototypeValidator){}
01633 
01635 
01638 
01640   virtual void validate(ParameterEntry const &entry, std::string const &paramName,
01641     std::string const &sublistName) const;
01642 
01644   const std::string getXMLTypeName() const{
01645     return "TwoDArrayValidator(" +
01646       this->getPrototype()->getXMLTypeName() + ", " +
01647       TypeNameTraits<EntryType>::name() + ")";
01648   }
01649 
01651   virtual void printDoc(std::string const &docString, std::ostream &out) const
01652   {
01653     StrUtils::printLines(out,"# ",docString);
01654     std::string toPrint;
01655     toPrint += "TwoDArrayValidator:\n";
01656     toPrint += "Prototype Validator:\n";
01657     this->getPrototype()->printDoc(toPrint, out);
01658   }
01659 
01661 
01662 };
01663 
01664 template<class ValidatorType, class EntryType>
01665 void TwoDArrayValidator<ValidatorType, EntryType>::validate(ParameterEntry const &entry, std::string const &paramName,
01666   std::string const &sublistName) const
01667 {
01668   any anyValue = entry.getAny(true);
01669   const std::string &entryName = entry.getAny(false).typeName();
01670   TEUCHOS_TEST_FOR_EXCEPTION(anyValue.type() != typeid(TwoDArray<EntryType>),
01671     Exceptions::InvalidParameterType,
01672     "The \"" << paramName << "\"" <<
01673     " parameter in the \"" << sublistName <<
01674     "\" sublist is has an error." << std::endl << std::endl <<
01675     "Error: The value you entered was the wrong type." << std::endl <<
01676     "Parameter: " << paramName << std::endl <<
01677     "Type specified: " << entryName << std::endl <<
01678     "Type accepted: " << TypeNameTraits<TwoDArray<EntryType> >::name() <<
01679     std::endl << std::endl);
01680 
01681   TwoDArray<EntryType> extracted =
01682     getValue<Teuchos::TwoDArray<EntryType> >(entry);
01683   RCP<const ParameterEntryValidator> prototype = this->getPrototype();
01684   for(int i = 0; i<extracted.getNumRows(); ++i){
01685     for(int j = 0; j<extracted.getNumCols(); ++j){
01686       ParameterEntry dummyParameter;
01687       dummyParameter.setValue(extracted(i,j));
01688       try{
01689         prototype->validate(
01690           dummyParameter, paramName, sublistName);
01691       }
01692       catch(Exceptions::InvalidParameterValue& e){
01693         std::stringstream oss;
01694         oss << "TwoDArray Validator Exception:" << std::endl <<
01695         "Bad Index: (" << i << "," << j << ")" << std::endl << e.what();
01696         throw Exceptions::InvalidParameterValue(oss.str());
01697       }
01698     }
01699   }
01700 }
01701 
01702 
01708 template<class ValidatorType, class EntryType>
01709 class DummyObjectGetter<TwoDArrayValidator<ValidatorType, EntryType> >{
01710 
01711 public:
01712 
01715 
01719   static RCP<TwoDArrayValidator<ValidatorType, EntryType> > getDummyObject();
01720 
01722 
01723 };
01724 
01725 template<class ValidatorType, class EntryType>
01726 RCP<TwoDArrayValidator<ValidatorType, EntryType> >
01727   DummyObjectGetter<TwoDArrayValidator<ValidatorType, EntryType> >::getDummyObject()
01728 {
01729   return rcp(new TwoDArrayValidator<ValidatorType, EntryType>(
01730     DummyObjectGetter<ValidatorType>::getDummyObject()));
01731 }
01732 
01736 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT TwoDArrayStringValidator :
01737   public TwoDArrayValidator<StringValidator, std::string>{
01738 
01739 public:
01740 
01743 
01745   TwoDArrayStringValidator(RCP<const StringValidator> prototypeValidator):
01746     TwoDArrayValidator<StringValidator, std::string>(prototypeValidator){}
01747 
01749 
01750 };
01751 
01752 
01757 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT TwoDArrayFileNameValidator :
01758   public TwoDArrayValidator<FileNameValidator, std::string>{
01759 
01760 public:
01761 
01764 
01766   TwoDArrayFileNameValidator(RCP<const FileNameValidator> prototypeValidator):
01767     TwoDArrayValidator<FileNameValidator, std::string>(prototypeValidator){}
01768 
01770 
01771 };
01772 
01773 
01777 template<class T>
01778 class TwoDArrayNumberValidator : public TwoDArrayValidator<EnhancedNumberValidator<T>, T>{
01779 public:
01782 
01784   TwoDArrayNumberValidator(
01785     RCP<const EnhancedNumberValidator<T> > prototypeValidator):
01786     TwoDArrayValidator<EnhancedNumberValidator<T>, T>(prototypeValidator){}
01787 
01789 
01790 };
01791 
01792 
01804 template<class ValidatorType, class EntryType>
01805 class ArrayValidator : public AbstractArrayValidator<ValidatorType, EntryType>{
01806 
01807 public:
01808 
01811 
01817   ArrayValidator(RCP<const ValidatorType> prototypeValidator):
01818     AbstractArrayValidator<ValidatorType, EntryType>(prototypeValidator){}
01819 
01821 
01824 
01826   virtual void validate(ParameterEntry const &entry, std::string const &paramName,
01827     std::string const &sublistName) const;
01828 
01830   const std::string getXMLTypeName() const{
01831     return "ArrayValidator(" +
01832       this->getPrototype()->getXMLTypeName() + ", " +
01833       TypeNameTraits<EntryType>::name() + ")";
01834   }
01835 
01837   virtual void printDoc(std::string const &docString, std::ostream &out) const
01838   {
01839     StrUtils::printLines(out,"# ",docString);
01840     std::string toPrint;
01841     toPrint += "ArrayValidator:\n";
01842     toPrint += "Prototype Validator:\n";
01843     this->getPrototype()->printDoc(toPrint, out);
01844   }
01845 
01847 
01848 };
01849 
01850 template<class ValidatorType, class EntryType>
01851 void ArrayValidator<ValidatorType, EntryType>::validate(ParameterEntry const &entry, std::string const &paramName,
01852   std::string const &sublistName) const
01853 {
01854   any anyValue = entry.getAny(true);
01855   const std::string &entryName = entry.getAny(false).typeName();
01856   TEUCHOS_TEST_FOR_EXCEPTION(anyValue.type() != typeid(Array<EntryType>),
01857     Exceptions::InvalidParameterType,
01858     "The \"" << paramName << "\"" <<
01859     " parameter in the \"" << sublistName <<
01860     "\" sublist is has an error." << std::endl << std::endl <<
01861     "Error: The value you entered was the wrong type." << std::endl <<
01862     "Parameter: " << paramName << std::endl <<
01863     "Type specified: " << entryName << std::endl <<
01864     "Type accepted: " << TypeNameTraits<Array<EntryType> >::name() <<
01865     std::endl << std::endl);
01866 
01867   Array<EntryType> extracted =
01868     getValue<Teuchos::Array<EntryType> >(entry);
01869   RCP<const ParameterEntryValidator> prototype = this->getPrototype();
01870   for(int i = 0; i<extracted.size(); ++i){
01871     ParameterEntry dummyParameter;
01872     dummyParameter.setValue(extracted[i]);
01873     try{
01874       prototype->validate(
01875         dummyParameter, paramName, sublistName);
01876     }
01877     catch(Exceptions::InvalidParameterValue& e){
01878       std::stringstream oss;
01879       oss << "Array Validator Exception:" << std::endl <<
01880       "Bad Index: " << i << std::endl << e.what();
01881       throw Exceptions::InvalidParameterValue(oss.str());
01882     }
01883   }
01884 }
01885 
01891 template<class ValidatorType, class EntryType>
01892 class DummyObjectGetter<ArrayValidator<ValidatorType, EntryType> >{
01893 
01894 public:
01895 
01898 
01902   static RCP<ArrayValidator<ValidatorType, EntryType> > getDummyObject();
01903 
01905 
01906 };
01907 
01908 template<class ValidatorType, class EntryType>
01909 RCP<ArrayValidator<ValidatorType, EntryType> >
01910   DummyObjectGetter<ArrayValidator<ValidatorType, EntryType> >::getDummyObject()
01911 {
01912   return rcp(new ArrayValidator<ValidatorType, EntryType>(
01913     DummyObjectGetter<ValidatorType>::getDummyObject()));
01914 }
01915 
01916 
01925 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT ArrayStringValidator :
01926   public ArrayValidator<StringValidator, std::string>{
01927 
01928 public:
01929 
01932 
01934   ArrayStringValidator(RCP<const StringValidator> prototypeValidator):
01935     ArrayValidator<StringValidator, std::string>(prototypeValidator){}
01936 
01938 
01939 };
01940 
01941 
01950 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT ArrayFileNameValidator : public ArrayValidator<FileNameValidator, std::string>{
01951 
01952 public:
01953 
01956 
01958   ArrayFileNameValidator(RCP<const FileNameValidator> prototypeValidator):
01959     ArrayValidator<FileNameValidator, std::string>(prototypeValidator){}
01960 
01962 
01963 };
01964 
01965 
01973 template<class T>
01974 class ArrayNumberValidator : public ArrayValidator<EnhancedNumberValidator<T>, T>{
01975 public:
01978 
01980   ArrayNumberValidator(
01981     RCP<const EnhancedNumberValidator<T> > prototypeValidator):
01982     ArrayValidator<EnhancedNumberValidator<T>, T>(prototypeValidator){}
01983 
01985 
01986 };
01987 
01988 
01989 
01990 // ///////////////////////////
01991 // Implementations
01992 
01993 
01994 //
01995 // StringToIntegralParameterEntryValidator
01996 //
01997 
01998 
01999 // Constructors
02000 
02001 
02002 template<class IntegralType>
02003 StringToIntegralParameterEntryValidator<IntegralType>::StringToIntegralParameterEntryValidator(
02004   ArrayView<const std::string> const& strings, std::string const& defaultParameterName
02005   ):
02006   ParameterEntryValidator(),
02007   defaultParameterName_(defaultParameterName)
02008 {
02009   typedef typename map_t::value_type val_t;
02010   for( int i = 0; i < static_cast<int>(strings.size()); ++i ) {
02011     const bool unique = map_.insert( val_t( strings[i], (IntegralType)i ) ).second;
02012     TEUCHOS_TEST_FOR_EXCEPTION(
02013       !unique, std::logic_error
02014       ,"Error, the std::string \"" << strings[i] << "\" is a duplicate for parameter \""
02015       << defaultParameterName_ << "\"."
02016       );
02017   }
02018   setValidValues(strings);
02019 }
02020 
02021 
02022 template<class IntegralType>
02023 StringToIntegralParameterEntryValidator<IntegralType>::StringToIntegralParameterEntryValidator(
02024   ArrayView<const std::string> const& strings, ArrayView<const IntegralType> const& integralValues
02025   ,std::string const& defaultParameterName
02026   ):
02027   ParameterEntryValidator(),
02028   defaultParameterName_(defaultParameterName)
02029 {
02030 #ifdef TEUCHOS_DEBUG
02031   TEUCHOS_ASSERT_EQUALITY( strings.size(), integralValues.size() );
02032 #endif
02033   TEUCHOS_TEST_FOR_EXCEPTION(
02034     strings.size() != integralValues.size(),
02035   std::logic_error,
02036   "Error, strings and integraValues must be of the same length."
02037   );
02038   typedef typename map_t::value_type val_t;
02039   for( int i = 0; i < static_cast<int>(strings.size()); ++i ) {
02040     const bool unique = map_.insert( val_t( strings[i], integralValues[i] ) ).second;
02041     TEUCHOS_TEST_FOR_EXCEPTION(
02042       !unique, std::logic_error
02043       ,"Error, the std::string \"" << strings[i] << "\" is a duplicate for parameter \""
02044       << defaultParameterName_ << "\""
02045       );
02046   }
02047   setValidValues(strings);
02048 }
02049 
02050 template<class IntegralType>
02051 StringToIntegralParameterEntryValidator<IntegralType>::StringToIntegralParameterEntryValidator(
02052   ArrayView<const std::string>    const& strings
02053   ,ArrayView<const std::string>   const& stringsDocs
02054   ,ArrayView<const IntegralType>  const& integralValues
02055   ,std::string          const& defaultParameterName
02056   ):
02057   ParameterEntryValidator(),
02058   defaultParameterName_(defaultParameterName)
02059 {
02060 #ifdef TEUCHOS_DEBUG
02061   TEUCHOS_ASSERT_EQUALITY( strings.size(), stringsDocs.size() );
02062   TEUCHOS_ASSERT_EQUALITY( strings.size(), integralValues.size() );
02063 #endif
02064   TEUCHOS_TEST_FOR_EXCEPTION(
02065     strings.size() != integralValues.size(),
02066   std::logic_error,
02067   "Error, strings and integraValues must be of the same length."
02068   );
02069   TEUCHOS_TEST_FOR_EXCEPTION(
02070     strings.size() != stringsDocs.size(),
02071   std::logic_error,
02072   "Error, strings and stringsDocs must be of the same length."
02073   );
02074   typedef typename map_t::value_type val_t;
02075   for( int i = 0; i < static_cast<int>(strings.size()); ++i ) {
02076     const bool unique = map_.insert( val_t( strings[i], integralValues[i] ) ).second;
02077     TEUCHOS_TEST_FOR_EXCEPTION(
02078       !unique, std::logic_error
02079       ,"Error, the std::string \"" << strings[i] << "\" is a duplicate for parameter \""
02080       << defaultParameterName_ << "\""
02081       );
02082   }
02083   setValidValues(strings,&stringsDocs);
02084 }
02085 
02086 // Lookup functions
02087 
02088 
02089 template<class IntegralType>
02090 IntegralType
02091 StringToIntegralParameterEntryValidator<IntegralType>::getIntegralValue(
02092   const std::string &str, const std::string &paramName
02093   ,const std::string &sublistName
02094   ) const
02095 {
02096   typename map_t::const_iterator itr = map_.find(str);
02097   TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(
02098     itr == map_.end(), Exceptions::InvalidParameterValue
02099     ,"Error, the value \"" << str << "\" is not recognized for the parameter \""
02100     << ( paramName.length() ? paramName : defaultParameterName_ ) << "\""
02101     << "\nin the sublist \"" << sublistName << "\"."
02102     << "\n\nValid values include:"
02103     << "\n  {\n"
02104     << validValues_
02105     << "  }"
02106     );
02107   return (*itr).second;
02108 }
02109 
02110 
02111 template<class IntegralType>
02112 IntegralType
02113 StringToIntegralParameterEntryValidator<IntegralType>::getIntegralValue(
02114   const ParameterEntry &entry, const std::string &paramName
02115   ,const std::string &sublistName, const bool activeQuery
02116   ) const
02117 {
02118   const bool validType = ( entry.getAny(activeQuery).type() == typeid(std::string) );
02119   TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(
02120     !validType, Exceptions::InvalidParameterType
02121     ,"Error, the parameter {paramName=\""<<(paramName.length()?paramName:defaultParameterName_)
02122     << "\",type=\""<<entry.getAny(activeQuery).typeName()<<"\"}"
02123     << "\nin the sublist \"" << sublistName << "\""
02124     << "\nhas the wrong type."
02125     << "\n\nThe correct type is \"string\"!"
02126     );
02127   const std::string
02128     &strValue = any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail!
02129   return getIntegralValue(strValue,paramName,sublistName); // This will validate the value and throw!
02130 }
02131 
02132 
02133 template<class IntegralType>
02134 std::string
02135 StringToIntegralParameterEntryValidator<IntegralType>::getStringValue(
02136   const ParameterEntry &entry, const std::string &paramName
02137   ,const std::string &sublistName, const bool activeQuery
02138   ) const
02139 {
02140   // Validate the parameter's type and value
02141   this->getIntegralValue(entry,paramName,sublistName,activeQuery);
02142   // Return the std::string value which is now validated!
02143   return any_cast<std::string>(entry.getAny(activeQuery)); // This cast should not fail!
02144 }
02145 
02146 
02147 template<class IntegralType>
02148 IntegralType
02149 StringToIntegralParameterEntryValidator<IntegralType>::getIntegralValue(
02150   ParameterList &paramList, const std::string &paramName
02151   ,const std::string &defaultValue
02152   ) const
02153 {
02154   const std::string
02155     &strValue = paramList.get(paramName,defaultValue);
02156   return getIntegralValue(strValue,paramName,paramList.name());
02157 }
02158 
02159 
02160 template<class IntegralType>
02161 std::string
02162 StringToIntegralParameterEntryValidator<IntegralType>::getStringValue(
02163   ParameterList &paramList, const std::string &paramName
02164   ,const std::string &defaultValue
02165   ) const
02166 {
02167   const std::string
02168     &strValue = paramList.get(paramName,defaultValue);
02169   getIntegralValue(strValue,paramName,paramList.name()); // Validate!
02170   return strValue;
02171 }
02172 
02173 template<class IntegralType>
02174 ParameterEntryValidator::ValidStringsList
02175 StringToIntegralParameterEntryValidator<IntegralType>::getStringDocs() const
02176 {
02177   return validStringValuesDocs_;
02178 }
02179 
02180 template<class IntegralType>
02181 const std::string&
02182 StringToIntegralParameterEntryValidator<IntegralType>::getDefaultParameterName() const
02183 {
02184   return defaultParameterName_;
02185 }
02186 
02187 template<class IntegralType>
02188 std::string
02189 StringToIntegralParameterEntryValidator<IntegralType>::validateString(
02190   const std::string &str, const std::string &paramName
02191   ,const std::string &sublistName
02192   ) const
02193 {
02194   getIntegralValue(str,paramName,sublistName); // Validate!
02195   return str;
02196 }
02197 
02198 
02199 // Overridden from ParameterEntryValidator
02200 
02201 template<class IntegralType>
02202 const std::string
02203 StringToIntegralParameterEntryValidator<IntegralType>::getXMLTypeName() const{
02204   return "StringIntegralValidator(" +
02205     TypeNameTraits<IntegralType>::name() +
02206     ")";
02207 }
02208 
02209 template<class IntegralType>
02210 void StringToIntegralParameterEntryValidator<IntegralType>::printDoc(
02211   std::string         const& docString
02212   ,std::ostream            & out
02213   ) const
02214 {
02215   StrUtils::printLines(out,"# ",docString);
02216   out << "#   Valid std::string values:\n";
02217   out << "#     {\n";
02218   if(validStringValuesDocs_.get()) {
02219     for( int i = 0; i < static_cast<int>(validStringValues_->size()); ++i ) {
02220       out << "#       \"" << (*validStringValues_)[i] << "\"\n";
02221       StrUtils::printLines(out,"#          ",(*validStringValuesDocs_)[i] );
02222     }
02223   }
02224   else {
02225     StrUtils::printLines(out,"#   ",validValues_);
02226     // Note: Above validValues_ has for initial spaces already so indent should
02227     // be correct!
02228   }
02229   out << "#     }\n";
02230 }
02231 
02232 
02233 template<class IntegralType>
02234 ParameterEntryValidator::ValidStringsList
02235 StringToIntegralParameterEntryValidator<IntegralType>::validStringValues() const
02236 {
02237   return validStringValues_;
02238 }
02239 
02240 
02241 template<class IntegralType>
02242 void StringToIntegralParameterEntryValidator<IntegralType>::validate(
02243   ParameterEntry  const& entry
02244   ,std::string    const& paramName
02245   ,std::string    const& sublistName
02246   ) const
02247 {
02248   this->getIntegralValue(entry,paramName,sublistName,false);
02249 }
02250 
02251 
02252 // private
02253 
02254 template<class IntegralType>
02255 void StringToIntegralParameterEntryValidator<IntegralType>::setValidValues(
02256   ArrayView<const std::string>   const& strings
02257   ,ArrayView<const std::string>  const* stringsDocs
02258   )
02259 {
02260   validStringValues_ = rcp(new Array<std::string>(strings));
02261   if(stringsDocs)
02262     validStringValuesDocs_ = rcp(new Array<std::string>(*stringsDocs));
02263   // Here I build the list of valid values in the same order as passed in by
02264   // the client!
02265   std::ostringstream oss;
02266   for( int i = 0; i < static_cast<int>(strings.size()); ++i ) {
02267     oss << "    \""<<strings[i]<<"\"\n";
02268   }
02269   // Note: Above four spaces is designed for the error output above.
02270   validValues_ = oss.str();
02271 }
02272 
02273 
02274 } // namespace Teuchos
02275 
02276 
02277 //
02278 // Nonmember function implementations for StringToIntegralParameterEntryValidator
02279 //
02280 
02281 
02282 template<class IntegralType>
02283 inline
02284 Teuchos::RCP<Teuchos::StringToIntegralParameterEntryValidator<IntegralType> >
02285 Teuchos::stringToIntegralParameterEntryValidator(
02286   ArrayView<const std::string> const& strings,
02287   std::string const& defaultParameterName
02288   )
02289 {
02290   return rcp(
02291     new StringToIntegralParameterEntryValidator<IntegralType>(
02292       strings, defaultParameterName
02293       )
02294     );
02295 }
02296 
02297 
02298 template<class IntegralType>
02299 inline
02300 Teuchos::RCP<Teuchos::StringToIntegralParameterEntryValidator<IntegralType> >
02301 Teuchos::stringToIntegralParameterEntryValidator(
02302   ArrayView<const std::string> const& strings,
02303   ArrayView<const IntegralType> const& integralValues,
02304   std::string const& defaultParameterName
02305   )
02306 {
02307   return rcp(
02308     new StringToIntegralParameterEntryValidator<IntegralType>(
02309       strings, integralValues, defaultParameterName
02310       )
02311     );
02312 }
02313 
02314 
02315 template<class IntegralType>
02316 inline
02317 Teuchos::RCP< Teuchos::StringToIntegralParameterEntryValidator<IntegralType> >
02318 Teuchos::stringToIntegralParameterEntryValidator(
02319   ArrayView<const std::string> const& strings,
02320   ArrayView<const std::string> const& stringsDocs,
02321   ArrayView<const IntegralType> const& integralValues,
02322   std::string const& defaultParameterName
02323   )
02324 {
02325   return rcp(
02326     new StringToIntegralParameterEntryValidator<IntegralType>(
02327       strings, stringsDocs, integralValues, defaultParameterName
02328       )
02329     );
02330 }
02331 
02332 template<class IntegralType>
02333 void Teuchos::setStringToIntegralParameter(
02334   std::string const& paramName,
02335   std::string const& defaultValue,
02336   std::string const& docString,
02337   ArrayView<const std::string> const& strings,
02338   ParameterList * paramList
02339   )
02340 {
02341   typedef ParameterEntryValidator PEV;
02342   TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
02343   paramList->set(
02344     paramName, defaultValue, docString,
02345     rcp_implicit_cast<const PEV>(
02346       stringToIntegralParameterEntryValidator<IntegralType>(
02347         strings, paramName
02348         )
02349       )
02350     );
02351 }
02352 
02353 
02354 template<class IntegralType>
02355 void Teuchos::setStringToIntegralParameter(
02356   std::string const& paramName,
02357   std::string const& defaultValue,
02358   std::string const& docString,
02359   ArrayView<const std::string> const& strings,
02360   ArrayView<const IntegralType> const& integralValues,
02361   ParameterList * paramList
02362   )
02363 {
02364   typedef ParameterEntryValidator PEV;
02365   TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
02366   paramList->set(
02367     paramName, defaultValue, docString,
02368     rcp_implicit_cast<const PEV>(
02369       stringToIntegralParameterEntryValidator<IntegralType>(
02370         strings, integralValues, paramName
02371         )
02372       )
02373     );
02374 }
02375 
02376 
02377 template<class IntegralType>
02378 void Teuchos::setStringToIntegralParameter(
02379   std::string const& paramName,
02380   std::string const& defaultValue,
02381   std::string const& docString,
02382   ArrayView<const std::string> const& strings,
02383   ArrayView<const std::string> const& stringsDocs,
02384   ArrayView<const IntegralType> const& integralValues,
02385   ParameterList * paramList
02386   )
02387 
02388 {
02389   typedef ParameterEntryValidator PEV;
02390   TEUCHOS_TEST_FOR_EXCEPT(0==paramList);
02391   paramList->set(
02392     paramName, defaultValue, docString,
02393     rcp_implicit_cast<const PEV>(
02394       stringToIntegralParameterEntryValidator<IntegralType>(
02395         strings, stringsDocs, integralValues, paramName
02396         )
02397       )
02398     );
02399 }
02400 
02401 
02402 template<class IntegralType>
02403 IntegralType Teuchos::getIntegralValue(
02404   ParameterList const& paramList, std::string const& paramName
02405   )
02406 {
02407   const ParameterEntry &entry = paramList.getEntry(paramName);
02408   RCP<const StringToIntegralParameterEntryValidator<IntegralType> >
02409     integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>(
02410       entry, paramList, paramName
02411       );
02412   return integralValidator->getIntegralValue(
02413     entry, paramName, paramList.name(), true );
02414 }
02415 
02416 
02417 template<class IntegralType>
02418 std::string Teuchos::getStringValue(
02419   ParameterList const& paramList, std::string const& paramName
02420   )
02421 {
02422   const ParameterEntry &entry = paramList.getEntry(paramName);
02423   RCP<const StringToIntegralParameterEntryValidator<IntegralType> >
02424     integralValidator = getStringToIntegralParameterEntryValidator<IntegralType>(
02425       entry, paramList, paramName
02426       );
02427   return integralValidator->getStringValue(
02428     entry, paramName, paramList.name(), true
02429     );
02430 }
02431 
02432 
02433 template<class IntegralType>
02434 Teuchos::RCP<const Teuchos::StringToIntegralParameterEntryValidator<IntegralType> >
02435 Teuchos::getStringToIntegralParameterEntryValidator(
02436   ParameterEntry const& entry, ParameterList const& paramList,
02437   std::string const& paramName
02438   )
02439 {
02440   const RCP<const ParameterEntryValidator> validator = entry.validator();
02441   TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(
02442     is_null(validator), Exceptions::InvalidParameterType,
02443     "Error!  The parameter \""<<paramName<<"\" exists\n"
02444     "in the parameter (sub)list \""<<paramList.name()<<"\"\n"
02445     "but it does not contain any validator needed to extract\n"
02446     "an integral value of type \""<<TypeNameTraits<IntegralType>::name()<<"\"!"
02447     );
02448   const RCP<const StringToIntegralParameterEntryValidator<IntegralType> > integralValidator =
02449     rcp_dynamic_cast<const StringToIntegralParameterEntryValidator<IntegralType> >(
02450       validator
02451       );
02452   TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(
02453     is_null(integralValidator), Exceptions::InvalidParameterType,
02454     "Error!  The parameter \""<<paramName<<"\" exists\n"
02455     "in the parameter (sub)list \""<<paramList.name()<<"\"\n"
02456     "but it contains the wrong type of validator.  The expected validator type\n"
02457     "is \""<<TypeNameTraits<StringToIntegralParameterEntryValidator<IntegralType> >::name()<<"\"\n"
02458     "but the contained validator type is \""<<typeName(*validator)<<"\"!"
02459     );
02460   return integralValidator;
02461 }
02462 
02463 
02464 #endif // TEUCHOS_STANDARD_PARAMETER_ENTRY_VALIDATORS_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines