Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_ParameterList.hpp
Go to the documentation of this file.
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 
00043 #ifndef TEUCHOS_PARAMETER_LIST_H
00044 #define TEUCHOS_PARAMETER_LIST_H
00045 
00050 #include "Teuchos_ParameterListExceptions.hpp"
00051 #include "Teuchos_ParameterEntry.hpp"
00052 #include "Teuchos_StringIndexedOrderedValueObjectContainer.hpp"
00053 #include "Teuchos_Assert.hpp"
00054 #include "Teuchos_RCP.hpp"
00055 #include "Teuchos_Array.hpp"
00056 #include "Teuchos_map.hpp"
00057 
00058 
00063 namespace Teuchos {
00064 
00068 enum EValidateUsed {
00069   VALIDATE_USED_ENABLED   
00072   ,VALIDATE_USED_DISABLED 
00075 };
00076 
00080 enum EValidateDefaults {
00081   VALIDATE_DEFAULTS_ENABLED   
00084   ,VALIDATE_DEFAULTS_DISABLED 
00087 };
00088 
00132 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT ParameterList {
00133 
00135   typedef StringIndexedOrderedValueObjectContainer<ParameterEntry> params_t;
00136 
00138   typedef params_t::Iterator Iterator;
00139   
00140 public:
00141 
00143 
00144 
00146   typedef params_t::ConstIterator ConstIterator;
00147 
00149   class PrintOptions {
00150   public:
00151     PrintOptions() : indent_(0), showTypes_(false), showFlags_(false), showDoc_(false) {}
00152     PrintOptions& indent(int _indent)        { indent_ = _indent; return *this; }
00153     PrintOptions& showTypes(bool _showTypes) { showTypes_ = _showTypes; return *this; }
00154     PrintOptions& showFlags(bool _showFlags) { showFlags_ = _showFlags; return *this; }
00155     PrintOptions& showDoc(bool _showDoc)     { showDoc_ = _showDoc; return *this; }
00156     PrintOptions& incrIndent(int indents)    { indent_ += indents; return *this; }
00157     int indent() const { return indent_; }
00158     bool showTypes() const { return showTypes_; }
00159     bool showFlags() const { return showFlags_; }
00160     bool showDoc() const { return showDoc_; }
00161     PrintOptions copy() const { return PrintOptions(*this); }
00162   private:
00163     int    indent_;
00164     bool   showTypes_;
00165     bool   showFlags_;
00166     bool   showDoc_;
00167   };
00168 
00170   
00172 
00173 
00175   ParameterList();
00176 
00178   ParameterList(const std::string &name);
00179   
00181   ParameterList(const ParameterList& source);
00182   
00184   virtual ~ParameterList();
00185 
00187   Ordinal numParams () const;
00188 
00190 
00191 
00192 
00194   ParameterList& setName( const std::string &name );
00195   
00199   ParameterList& operator= (const ParameterList& source);
00200   
00209   ParameterList& setParameters(const ParameterList& source);
00210   
00220   ParameterList& setParametersNotAlreadySet(const ParameterList& source);
00221 
00229   ParameterList& disableRecursiveValidation();
00230   
00249   template<typename T>
00250   ParameterList& set (std::string const& name, 
00251           T const& value, 
00252           std::string const& docString = "",
00253           RCP<const ParameterEntryValidator> const& validator = null);
00254 
00259   ParameterList& set(
00260     std::string const& name, char value[], std::string const& docString = "",
00261     RCP<const ParameterEntryValidator> const& validator = null
00262     );
00263 
00268   ParameterList& set(
00269     std::string const& name, const char value[], std::string const& docString = "",
00270     RCP<const ParameterEntryValidator> const& validator = null
00271     );
00272 
00276   ParameterList& set(
00277     std::string const& name, ParameterList const& value, std::string const& docString = ""
00278     );
00279 
00284   ParameterList& setEntry(const std::string& name, const ParameterEntry& entry);
00285 
00287 
00288 
00289 
00306   template<typename T>
00307   T& get(const std::string& name, T def_value);
00308 
00312   std::string& get(const std::string& name, char def_value[]);
00313   
00317   std::string& get(const std::string& name, const char def_value[]);
00318 
00349   template<typename T>
00350   T& get (const std::string& name);
00351 
00379   template<typename T>
00380   const T& get (const std::string& name) const;  
00381   
00387   template<typename T>
00388   inline
00389   T* getPtr(const std::string& name);
00390   
00396   template<typename T>
00397   inline
00398   const T* getPtr(const std::string& name) const;  
00399 
00400   // ToDo: Add getSafePtr() functions to return Ptr<T> instead of raw T*
00401   
00407   ParameterEntry& getEntry(const std::string& name);  
00408   
00414   inline
00415   const ParameterEntry& getEntry(const std::string& name) const;  
00416   
00419   inline
00420   ParameterEntry* getEntryPtr(const std::string& name);  
00421 
00422   // ToDo: Add function called getEntrySafePtr() to return Ptr<> as the main
00423   // implementation and deprecate getEntryPtr()
00424   
00427   inline
00428   const ParameterEntry* getEntryPtr(const std::string& name) const;  
00429 
00432   inline RCP<ParameterEntry> getEntryRCP(const std::string& name);  
00433   
00436   inline RCP<const ParameterEntry> getEntryRCP(const std::string& name) const;
00437 
00439 
00441 
00442  
00456   bool remove(
00457     std::string const& name, bool throwIfNotExists = true
00458     );
00459 
00461   
00463 
00464 
00470   ParameterList& sublist(
00471     const std::string& name, bool mustAlreadyExist = false
00472     ,const std::string& docString = ""
00473     );
00474   
00479   const ParameterList& sublist(const std::string& name) const;
00480 
00482   
00484 
00485 
00487   const std::string& name() const;
00488 
00493   bool isParameter (const std::string& name) const;
00494 
00499   bool isSublist (const std::string& name) const;
00500 
00506   template<typename T>
00507   bool isType (const std::string& name) const;
00508 
00509 #ifndef DOXYGEN_SHOULD_SKIP_THIS  
00510 
00520   template<typename T>
00521   bool isType(const std::string& name, T* ptr) const;
00522 #endif
00523 
00525   
00527 
00528 
00534   void print() const;
00535 
00538   std::ostream& print(std::ostream& os, const PrintOptions &printOptions) const;
00539 
00542   std::ostream& print(std::ostream& os, int indent = 0, bool showTypes = false, bool showFlags = true ) const;
00543   
00545   void unused(std::ostream& os) const;
00546 
00548   std::string currentParametersString() const;
00549 
00551 
00553 
00554 
00556   inline ConstIterator begin() const;
00557 
00559   inline ConstIterator end() const;
00560   
00562   inline const std::string& name(ConstIterator i) const;
00563 
00565   inline const ParameterEntry& entry(ConstIterator i) const;
00566 
00568 
00570 
00571 
00616   void validateParameters(
00617     ParameterList const& validParamList,
00618     int const depth = 1000,
00619     EValidateUsed const validateUsed = VALIDATE_USED_ENABLED,
00620     EValidateDefaults const validateDefaults = VALIDATE_DEFAULTS_ENABLED
00621     ) const;
00622 
00660   void validateParametersAndSetDefaults(
00661     ParameterList const& validParamList,
00662     int const depth = 1000
00663     );
00664 
00666   
00667 private: // Functions
00668 
00670   inline Iterator nonconstBegin();
00672   inline Iterator nonconstEnd();
00674   inline ParameterEntry& nonconstEntry(Iterator i);
00676   void validateEntryExists(const std::string &funcName, const std::string &name,
00677     const ParameterEntry *entry) const;
00678   // ToDo: Change above function to take Ptr<> instead of raw pointer.
00680   template<typename T>
00681   void validateEntryType(const std::string &funcName, const std::string &name,
00682     const ParameterEntry &entry ) const;
00684   void validateEntryIsList(const std::string &name, const ParameterEntry &entry) const;
00686   void validateMissingSublistMustExist(const std::string &baselist_name,
00687     const std::string &sublist_name, const bool mustAlreadyExist) const;
00689   void updateSubListNames(int depth = 0);
00690   
00691 private: // Data members
00692 
00694   std::string name_;
00695 
00697 //use pragmas to disable some false-positive warnings for windows sharedlibs export
00698 //#ifdef _MSC_VER
00699 //#pragma warning(push)
00700 //#pragma warning(disable:4251)
00701 //#endif
00702   params_t params_;
00703 //#ifdef _MSC_VER
00704 //#pragma warning(pop)
00705 //#endif
00706 
00708   bool disableRecursiveValidation_;
00709 
00710 };
00711 
00712 
00717 inline
00718 RCP<ParameterList> parameterList()
00719 {
00720   return rcp(new ParameterList);
00721 }
00722 
00723 
00728 inline
00729 RCP<ParameterList> parameterList(const std::string &name)
00730 {
00731   return rcp(new ParameterList(name));
00732 }
00733   
00734 
00739 inline
00740 RCP<ParameterList> parameterList(const ParameterList& source)
00741 {
00742   return rcp(new ParameterList(source));
00743 }
00744 
00745 
00750 inline
00751 RCP<ParameterList> createParameterList()
00752 {
00753   return rcp(new ParameterList);
00754 }
00755 
00756 
00761 inline
00762 RCP<ParameterList> createParameterList(const std::string &name)
00763 {
00764   return rcp(new ParameterList(name));
00765 }
00766 
00767 
00772 template<>
00773 class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT TypeNameTraits<ParameterList> {
00774 public:
00775   static std::string name() { return "ParameterList"; }
00776   static std::string concreteName( const ParameterList& /*t2*/ )
00777     { return name(); }
00778 };
00779 
00780 
00785 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT bool operator==( const ParameterList& list1, const ParameterList& list2 );
00786 
00787 
00792 inline
00793 bool operator!=( const ParameterList& list1, const ParameterList& list2 )
00794 {
00795   return !( list1 == list2 );
00796 }
00797 
00798 
00810 TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT bool haveSameValues( const ParameterList& list1, const ParameterList& list2 );
00811 
00812 
00813 // /////////////////////////////////////////////////////
00814 // Inline and Template Function Definitions
00815 
00816 
00817 inline
00818 ParameterList& ParameterList::setName( const std::string &name_in )
00819 {
00820   name_ = name_in;
00821   return *this;
00822 }
00823 
00824 
00825 // Set functions
00826 
00827 
00828 template<typename T>
00829 inline
00830 ParameterList& ParameterList::set(
00831   std::string const& name_in, T const& value_in, std::string const& docString_in,
00832   RCP<const ParameterEntryValidator> const& validator_in
00833   )
00834 {
00835   typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB;
00836   const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
00837   if (param_idx != SIOVOCB::getInvalidOrdinal()) {
00838     Ptr<ParameterEntry> param = params_.getNonconstObjPtr(param_idx);
00839     const std::string docString =
00840       (docString_in.length() ? docString_in : param->docString());
00841     const RCP<const ParameterEntryValidator> validator =
00842       (nonnull(validator_in) ? validator_in : param->validator());
00843      // Create temp param to validate before setting
00844     ParameterEntry param_new(value_in, false, false, docString, validator );
00845     if (nonnull(validator)) {
00846       validator->validate(param_new, name_in, this->name());
00847     }
00848     // Strong guarantee: (if exception is thrown, the value is not changed)
00849     *param = param_new;
00850   }
00851   else {
00852     ParameterEntry param_new(value_in, false, false, docString_in, validator_in);
00853     if (nonnull(param_new.validator())) {
00854       param_new.validator()->validate(param_new, name_in, this->name());
00855     }
00856     params_.setObj(name_in, param_new);
00857   }
00858   return *this;
00859 }
00860 
00861 
00862 inline
00863 ParameterList& ParameterList::set(
00864   std::string const& name_in, char value[], std::string const& docString
00865   ,RCP<const ParameterEntryValidator> const& validator
00866   ) 
00867 { return set(name_in, std::string(value), docString, validator); }
00868 
00869 
00870 inline
00871 ParameterList& ParameterList::set(
00872   const std::string& name_in, const char value[], const std::string &docString
00873   ,RCP<const ParameterEntryValidator> const& validator
00874   ) 
00875 { return set( name_in, std::string(value), docString, validator ); }
00876 
00877 
00878 inline
00879 ParameterList& ParameterList::set(
00880   std::string const& name_in, ParameterList const& value, std::string const& /*docString*/
00881   )
00882 {
00883   sublist(name_in) = value;
00884   return *this;
00885 }
00886 
00887 
00888 inline
00889 ParameterList& ParameterList::setEntry(std::string const& name_in, ParameterEntry const& entry_in)
00890 {
00891   params_.setObj(name_in, entry_in);
00892   return *this;
00893 }
00894 
00895 
00896 // Get functions
00897 
00898 
00899 template<typename T>
00900 T& ParameterList::get(const std::string& name_in, T def_value)
00901 {
00902   typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB;
00903   Ordinal param_idx = params_.getObjOrdinalIndex(name_in); 
00904   if (param_idx == SIOVOCB::getInvalidOrdinal()) {
00905     // Param does not exist
00906     param_idx = params_.setObj(name_in, ParameterEntry(def_value, true));
00907   }
00908   Ptr<ParameterEntry> param = params_.getNonconstObjPtr(param_idx);
00909   this->template validateEntryType<T>("get", name_in, *param);
00910   return getValue<T>(*param);
00911 }
00912 
00913 
00914 inline
00915 std::string& ParameterList::get(const std::string& name_in, char def_value[])
00916 { return get(name_in, std::string(def_value)); }
00917 
00918 
00919 inline
00920 std::string& ParameterList::get(const std::string& name_in, const char def_value[])
00921 { return get(name_in, std::string(def_value)); }
00922 
00923 
00924 template<typename T>
00925 T& ParameterList::get(const std::string& name_in) 
00926 {
00927   ParameterEntry *foundEntry = this->getEntryPtr(name_in);
00928   validateEntryExists("get",name_in,foundEntry);
00929   this->template validateEntryType<T>("get",name_in,*foundEntry);
00930   return getValue<T>(*foundEntry);
00931 }
00932 
00933   
00934 template<typename T>
00935 const T& ParameterList::get(const std::string& name_in) const
00936 {
00937   const ParameterEntry *foundEntry = this->getEntryPtr(name_in);
00938   validateEntryExists("get",name_in,foundEntry);
00939   this->template validateEntryType<T>("get",name_in,*foundEntry);
00940   return getValue<T>(*foundEntry);
00941 }
00942 
00943 
00944 template<typename T>
00945 inline
00946 T* ParameterList::getPtr(const std::string& name_in) 
00947 {
00948   typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB;
00949   const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
00950   if (param_idx != SIOVOCB::getInvalidOrdinal()) {
00951     const Ptr<ParameterEntry> param_ptr = params_.getNonconstObjPtr(param_idx);
00952     if (param_ptr->isType<T>()) {
00953       return &param_ptr->getValue<T>(0);
00954     }
00955     // Note: The above is inefficinet.  You have to do the dynamic_cast twice
00956     // (once to see if it is the type and once to do the cast).  This could be
00957     // made more efficinet by upgrading Teuchos::any to add a any_cast_ptr()
00958     // function but I don't think anyone actually uses this function.
00959     return 0;
00960   }
00961   return 0;
00962 }
00963 
00964   
00965 template<typename T>
00966 inline
00967 const T* ParameterList::getPtr(const std::string& name_in) const
00968 {
00969   typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB;
00970   const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
00971   if (param_idx != SIOVOCB::getInvalidOrdinal()) {
00972     const Ptr<const ParameterEntry> param_ptr = params_.getObjPtr(param_idx);
00973     if (param_ptr->isType<T>()) {
00974       return &param_ptr->getValue<T>(0);
00975     }
00976     // Note: The above is inefficinet, see above non-const getPtr() function.
00977     return 0;
00978   }
00979   return 0;
00980 }
00981 
00982 
00983 inline
00984 ParameterEntry& ParameterList::getEntry(const std::string& name_in)
00985 {
00986   ParameterEntry *foundEntry = this->getEntryPtr(name_in);
00987   validateEntryExists("get", name_in, foundEntry);
00988   return *foundEntry;
00989 }
00990 
00991   
00992 inline
00993 const ParameterEntry& ParameterList::getEntry(const std::string& name_in) const
00994 {
00995   const ParameterEntry *foundEntry = this->getEntryPtr(name_in);
00996   validateEntryExists("get", name_in, foundEntry);
00997   return *foundEntry;
00998 }
00999 
01000 
01001 inline
01002 ParameterEntry*
01003 ParameterList::getEntryPtr(const std::string& name_in)
01004 {
01005   typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB;
01006   const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
01007   if (param_idx != SIOVOCB::getInvalidOrdinal()) {
01008     return &*params_.getNonconstObjPtr(param_idx);
01009   }
01010   return 0;
01011 }
01012 
01013 
01014 inline
01015 const ParameterEntry*
01016 ParameterList::getEntryPtr(const std::string& name_in) const
01017 {
01018   typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB;
01019   const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
01020   if (param_idx != SIOVOCB::getInvalidOrdinal()) {
01021     return &*params_.getObjPtr(param_idx);
01022   }
01023   return 0;
01024 }
01025 
01026 
01027 inline RCP<ParameterEntry>
01028 ParameterList::getEntryRCP(const std::string& name_in)
01029 {
01030   typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB;
01031   const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
01032   if (param_idx != SIOVOCB::getInvalidOrdinal()) {
01033     return rcpFromPtr(params_.getNonconstObjPtr(param_idx));
01034   }
01035   return null;
01036 }
01037 
01038 
01039 inline RCP<const ParameterEntry>
01040 ParameterList::getEntryRCP(const std::string& name_in) const
01041 {
01042   typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB;
01043   const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
01044   if (param_idx != SIOVOCB::getInvalidOrdinal()) {
01045     return rcpFromPtr(params_.getObjPtr(param_idx));
01046   }
01047   return null;
01048 }
01049 
01050 
01051 // Attribute Functions
01052 
01053 
01054 inline
01055 const std::string& ParameterList::name() const
01056 {
01057   return name_;
01058 }
01059 
01060   
01061 #ifndef DOXYGEN_SHOULD_SKIP_THIS
01062 template<typename T>
01063 bool ParameterList::isType(const std::string& name_in, T* /*ptr*/) const
01064 {
01065   typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB;
01066   const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
01067   if (param_idx != SIOVOCB::getInvalidOrdinal()) {
01068     return params_.getObjPtr(param_idx)->isType<T>();
01069   }
01070   return false;
01071 }
01072 #endif
01073 
01074   
01075 template<typename T>
01076 bool ParameterList::isType(const std::string& name_in) const
01077 {
01078   return this->isType(name_in, static_cast<T*>(0));
01079 }
01080 
01081 
01082 // Read-only access to the iterator
01083 
01084 
01085 inline ParameterList::ConstIterator ParameterList::begin() const
01086 {
01087   return params_.begin();
01088 }
01089 
01090 
01091 inline ParameterList::ConstIterator ParameterList::end() const
01092 {
01093   return params_.end();
01094 }
01095 
01096 
01097 inline const std::string& ParameterList::name(ConstIterator i) const
01098 {
01099   return (i->first);
01100 }
01101 
01102 
01103 inline const ParameterEntry& ParameterList::entry(ConstIterator i) const
01104 {
01105   return (i->second);
01106 }
01107 
01108 
01109 // private
01110 
01111 
01112 inline ParameterList::Iterator ParameterList::nonconstBegin()
01113 {
01114   return params_.nonconstBegin();
01115 }
01116 
01117 
01118 inline ParameterList::Iterator ParameterList::nonconstEnd()
01119 {
01120   return params_.nonconstEnd();
01121 }
01122 
01123 
01124 inline ParameterEntry& ParameterList::nonconstEntry(Iterator i)
01125 {
01126   return (i->second);
01127 }
01128 
01129 
01130 template<typename T>
01131 inline
01132 void ParameterList::validateEntryType(
01133   const std::string &/*funcName*/, const std::string &name_in,
01134   const ParameterEntry &entry_in
01135   ) const
01136 {
01137   TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(
01138     entry_in.getAny().type() != typeid(T), Exceptions::InvalidParameterType
01139     ,"Error!  An attempt was made to access parameter \""<<name_in<<"\""
01140     " of type \""<<entry_in.getAny().typeName()<<"\""
01141     "\nin the parameter (sub)list \""<<this->name()<<"\""
01142     "\nusing the incorrect type \""<<TypeNameTraits<T>::name()<<"\"!"
01143     );
01144 }
01145 
01146 
01147 // //////////////////////////////////////
01148 // Helper functions
01149 
01150   
01157 template<typename T>
01158 T& getParameter( ParameterList& l, const std::string& name )
01159 {
01160   return l.template get<T>(name);
01161 }
01162 
01163   
01169 template<typename T>
01170 inline
01171 T& get( ParameterList& l, const std::string& name )
01172 {
01173   return getParameter<T>(l,name);
01174 }
01175 
01176   
01183 template<typename T>
01184 const T& getParameter( const ParameterList& l, const std::string& name )
01185 {
01186   return l.template get<T>(name);
01187 }
01188 
01189   
01197 template<typename T>
01198 inline
01199 T* getParameterPtr( ParameterList& l, const std::string& name )
01200 {
01201   return l.template getPtr<T>(name);
01202 }
01203 
01204   
01212 template<typename T>
01213 inline
01214 const T* getParameterPtr( const ParameterList& l, const std::string& name )
01215 {
01216   return l.template getPtr<T>(name);
01217 }
01218 
01219   
01226 template<typename T>
01227 inline
01228 bool isParameterType( ParameterList& l, const std::string& name )
01229 {
01230   return l.isType( name, (T*)NULL );
01231 }
01232 
01233   
01240 template<typename T>
01241 inline
01242 bool isParameterType( const ParameterList& l, const std::string& name )
01243 {
01244   return l.isType( name, (T*)NULL );
01245 }
01246 
01247   
01259 template<typename T>
01260 void setStringParameterFromArray(
01261   const std::string          &paramName
01262   ,const Array<T>       &array
01263   ,ParameterList        *paramList
01264   )
01265 {
01266   TEUCHOS_TEST_FOR_EXCEPT(!paramList);
01267   paramList->set(paramName,toString(array));
01268 }
01269 
01270   
01335 template<typename T>
01336 Array<T> getArrayFromStringParameter(
01337   const ParameterList   &paramList
01338   ,const std::string         &paramName
01339   ,const int            arrayDim        = -1
01340   ,const bool           mustExist       = true
01341   )
01342 {
01343   std::string arrayStr;
01344   if(mustExist) {
01345     arrayStr = getParameter<std::string>(paramList,paramName);
01346   }
01347   else {
01348     const std::string
01349       *arrayStrPtr = getParameterPtr<std::string>(paramList,paramName);
01350     if(arrayStrPtr) {
01351       arrayStr = *arrayStrPtr;
01352     }
01353     else {
01354       return Array<T>(); // Return an empty array
01355     }
01356   }
01357   Array<T> a;
01358   try {
01359     a = fromStringToArray<T>(arrayStr);
01360   }
01361   catch( const InvalidArrayStringRepresentation&) {
01362     TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(
01363       true, Exceptions::InvalidParameterValue
01364       ,"Error!  The parameter \""<<paramName<<"\"\n"
01365       "in the sublist \""<<paramList.name()<<"\"\n"
01366       "exists, but the std::string value:\n"
01367       "----------\n"
01368       <<arrayStr<<
01369       "\n----------\n"
01370       "is not a valid array represntation!"
01371       );
01372   }
01373   TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(
01374     ( ( a.size()>0 && arrayDim>=0 ) && static_cast<int>(a.size())!=arrayDim )
01375     ,Exceptions::InvalidParameterValue
01376     ,"Error!  The parameter \""<<paramName<<"\"\n"
01377     "in the sublist \""<<paramList.name()<<"\"\n"
01378     "exists and is a valid array, but the dimension of\n"
01379     "the read in array a.size() = " << a.size() << "\n"
01380     "was not equal to the expected size arrayDim = " << arrayDim << "!"
01381     );
01382   return a;
01383 }
01384 
01385 
01389 inline
01390 RCP<ParameterList> sublist(
01391   const RCP<ParameterList> &paramList, const std::string& name,
01392   bool mustAlreadyExist = false, const std::string& docString = ""
01393   )
01394 {
01395   return rcpWithEmbeddedObjPostDestroy(
01396     &paramList->sublist(name, mustAlreadyExist, docString), paramList, false );
01397 }
01398 
01399 
01403 inline
01404 RCP<const ParameterList> sublist(
01405   const RCP<const ParameterList> &paramList, const std::string& name
01406   )
01407 {
01408   return rcpWithEmbeddedObjPostDestroy(
01409     &paramList->sublist(name), paramList, false );
01410 }
01411 
01412   
01416 inline std::ostream& operator<<(std::ostream& os, const ParameterList& l)
01417 {
01418   return l.print(os);
01419 }
01420 
01421   
01422 } // end of Teuchos namespace
01423 
01424 
01425 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines