Teuchos - Trilinos Tools Package 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   /*< \brief Validate that parameters in <tt>*this</tt> list
00070                               set using the default value are present in
00071                               the validation list */
00072   ,VALIDATE_USED_DISABLED /*< \brief Do not validate that parameters in <tt>*this</tt> list
00073                               set using the default value are present in
00074                               the validation list */
00075 };
00076 
00080 enum EValidateDefaults {
00081   VALIDATE_DEFAULTS_ENABLED   /*< \brief Validate that parameters in <tt>*this</tt> list
00082                                   set using the default value are present in
00083                                    the validation list */
00084   ,VALIDATE_DEFAULTS_DISABLED /*< \brief Do not validate that parameters in <tt>*this</tt> list
00085                                   set using the default value are present in
00086                                   the validation list */
00087 };
00088 
00102 class TEUCHOS_LIB_DLL_EXPORT ParameterList {
00103 
00105   typedef StringIndexedOrderedValueObjectContainer<ParameterEntry> params_t;
00106 
00108   typedef params_t::Iterator Iterator;
00109   
00110 public:
00111 
00113 
00114 
00116   typedef params_t::ConstIterator ConstIterator;
00117 
00119   class PrintOptions {
00120   public:
00121     PrintOptions() : indent_(0), showTypes_(false), showFlags_(false), showDoc_(false) {}
00122     PrintOptions& indent(int _indent)        { indent_ = _indent; return *this; }
00123     PrintOptions& showTypes(bool _showTypes) { showTypes_ = _showTypes; return *this; }
00124     PrintOptions& showFlags(bool _showFlags) { showFlags_ = _showFlags; return *this; }
00125     PrintOptions& showDoc(bool _showDoc)     { showDoc_ = _showDoc; return *this; }
00126     PrintOptions& incrIndent(int indents)    { indent_ += indents; return *this; }
00127     int indent() const { return indent_; }
00128     bool showTypes() const { return showTypes_; }
00129     bool showFlags() const { return showFlags_; }
00130     bool showDoc() const { return showDoc_; }
00131     PrintOptions copy() const { return PrintOptions(*this); }
00132   private:
00133     int    indent_;
00134     bool   showTypes_;
00135     bool   showFlags_;
00136     bool   showDoc_;
00137   };
00138 
00140   
00142 
00143 
00145   ParameterList();
00146 
00148   ParameterList(const std::string &name);
00149   
00151   ParameterList(const ParameterList& source);
00152   
00154   virtual ~ParameterList();
00155 
00157   Ordinal numParams() const;
00158 
00160   
00162 
00163 
00166   ParameterList& setName( const std::string &name );
00167   
00171   ParameterList& operator=(const ParameterList& source);
00172   
00180   ParameterList& setParameters(const ParameterList& source);
00181   
00191   ParameterList& setParametersNotAlreadySet(const ParameterList& source);
00192 
00200   ParameterList& disableRecursiveValidation();
00201   
00210   template<typename T>
00211   ParameterList& set(
00212     std::string const& name, T const& value, std::string const& docString = "",
00213     RCP<const ParameterEntryValidator> const& validator = null
00214     );
00215 
00219   ParameterList& set(
00220     std::string const& name, char value[], std::string const& docString = "",
00221     RCP<const ParameterEntryValidator> const& validator = null
00222     );
00223 
00227   ParameterList& set(
00228     std::string const& name, const char value[], std::string const& docString = "",
00229     RCP<const ParameterEntryValidator> const& validator = null
00230     );
00231 
00234   ParameterList& set(
00235     std::string const& name, ParameterList const& value, std::string const& docString = ""
00236     );
00237 
00242   ParameterList& setEntry(const std::string& name, const ParameterEntry& entry);
00243 
00245   
00247 
00248   
00259   template<typename T>
00260   T& get(const std::string& name, T def_value);
00261 
00265   std::string& get(const std::string& name, char def_value[]);
00266   
00270   std::string& get(const std::string& name, const char def_value[]);
00271   
00279   template<typename T>
00280   T& get(const std::string& name);
00281   
00289   template<typename T>
00290   const T& get(const std::string& name) const;  
00291   
00297   template<typename T>
00298   inline
00299   T* getPtr(const std::string& name);
00300   
00306   template<typename T>
00307   inline
00308   const T* getPtr(const std::string& name) const;  
00309 
00310   // ToDo: Add getSafePtr() functions to return Ptr<T> instead of raw T*
00311   
00317   ParameterEntry& getEntry(const std::string& name);  
00318   
00324   inline
00325   const ParameterEntry& getEntry(const std::string& name) const;  
00326   
00329   inline
00330   ParameterEntry* getEntryPtr(const std::string& name);  
00331 
00332   // ToDo: Add function called getEntrySafePtr() to return Ptr<> as the main
00333   // implementation and deprecate getEntryPtr()
00334   
00337   inline
00338   const ParameterEntry* getEntryPtr(const std::string& name) const;  
00339 
00342   inline RCP<ParameterEntry> getEntryRCP(const std::string& name);  
00343   
00346   inline RCP<const ParameterEntry> getEntryRCP(const std::string& name) const;
00347 
00349 
00351 
00352  
00366   bool remove(
00367     std::string const& name, bool throwIfNotExists = true
00368     );
00369 
00371   
00373 
00374 
00380   ParameterList& sublist(
00381     const std::string& name, bool mustAlreadyExist = false
00382     ,const std::string& docString = ""
00383     );
00384   
00389   const ParameterList& sublist(const std::string& name) const;
00390 
00392   
00394 
00395 
00397   const std::string& name() const;
00398 
00405   bool isParameter(const std::string& name) const;
00406   
00412   bool isSublist(const std::string& name) const;
00413   
00420   template<typename T>
00421   bool isType(const std::string& name) const;
00422 
00423 #ifndef DOXYGEN_SHOULD_SKIP_THIS  
00424 
00434   template<typename T>
00435   bool isType(const std::string& name, T* ptr) const;
00436 #endif
00437 
00439   
00441 
00442 
00448   void print() const;
00449 
00452   std::ostream& print(std::ostream& os, const PrintOptions &printOptions) const;
00453 
00456   std::ostream& print(std::ostream& os, int indent = 0, bool showTypes = false, bool showFlags = true ) const;
00457   
00459   void unused(std::ostream& os) const;
00460 
00462   std::string currentParametersString() const;
00463 
00465 
00467 
00468 
00470   inline ConstIterator begin() const;
00471 
00473   inline ConstIterator end() const;
00474   
00476   inline const std::string& name(ConstIterator i) const;
00477 
00479   inline const ParameterEntry& entry(ConstIterator i) const;
00480 
00482 
00484 
00485 
00530   void validateParameters(
00531     ParameterList const& validParamList,
00532     int const depth = 1000,
00533     EValidateUsed const validateUsed = VALIDATE_USED_ENABLED,
00534     EValidateDefaults const validateDefaults = VALIDATE_DEFAULTS_ENABLED
00535     ) const;
00536 
00574   void validateParametersAndSetDefaults(
00575     ParameterList const& validParamList,
00576     int const depth = 1000
00577     );
00578 
00580   
00581 private: // Functions
00582 
00584   inline Iterator nonconstBegin();
00586   inline Iterator nonconstEnd();
00588   inline ParameterEntry& nonconstEntry(Iterator i);
00590   void validateEntryExists(const std::string &funcName, const std::string &name,
00591     const ParameterEntry *entry) const;
00592   // ToDo: Change above function to take Ptr<> instead of raw pointer.
00594   template<typename T>
00595   void validateEntryType(const std::string &funcName, const std::string &name,
00596     const ParameterEntry &entry ) const;
00598   void validateEntryIsList(const std::string &name, const ParameterEntry &entry) const;
00600   void validateMissingSublistMustExist(const std::string &baselist_name,
00601     const std::string &sublist_name, const bool mustAlreadyExist) const;
00603   void updateSubListNames(int depth = 0);
00604   
00605 private: // Data members
00606 
00608   std::string name_;
00609 
00611 //use pragmas to disable some false-positive warnings for windows sharedlibs export
00612 //#ifdef _MSC_VER
00613 //#pragma warning(push)
00614 //#pragma warning(disable:4251)
00615 //#endif
00616   params_t params_;
00617 //#ifdef _MSC_VER
00618 //#pragma warning(pop)
00619 //#endif
00620 
00622   bool disableRecursiveValidation_;
00623 
00624 };
00625 
00626 
00631 inline
00632 RCP<ParameterList> parameterList()
00633 {
00634   return rcp(new ParameterList);
00635 }
00636 
00637 
00642 inline
00643 RCP<ParameterList> parameterList(const std::string &name)
00644 {
00645   return rcp(new ParameterList(name));
00646 }
00647   
00648 
00653 inline
00654 RCP<ParameterList> parameterList(const ParameterList& source)
00655 {
00656   return rcp(new ParameterList(source));
00657 }
00658 
00659 
00664 inline
00665 RCP<ParameterList> createParameterList()
00666 {
00667   return rcp(new ParameterList);
00668 }
00669 
00670 
00675 inline
00676 RCP<ParameterList> createParameterList(const std::string &name)
00677 {
00678   return rcp(new ParameterList(name));
00679 }
00680 
00681 
00686 template<>
00687 class TEUCHOS_LIB_DLL_EXPORT TypeNameTraits<ParameterList> {
00688 public:
00689   static std::string name() { return "ParameterList"; }
00690   static std::string concreteName( const ParameterList& /*t2*/ )
00691     { return name(); }
00692 };
00693 
00694 
00699 TEUCHOS_LIB_DLL_EXPORT bool operator==( const ParameterList& list1, const ParameterList& list2 );
00700 
00701 
00706 inline
00707 bool operator!=( const ParameterList& list1, const ParameterList& list2 )
00708 {
00709   return !( list1 == list2 );
00710 }
00711 
00712 
00721 TEUCHOS_LIB_DLL_EXPORT bool haveSameValues( const ParameterList& list1, const ParameterList& list2 );
00722 
00723 
00724 // /////////////////////////////////////////////////////
00725 // Inline and Template Function Definitions
00726 
00727 
00728 inline
00729 ParameterList& ParameterList::setName( const std::string &name_in )
00730 {
00731   name_ = name_in;
00732   return *this;
00733 }
00734 
00735 
00736 // Set functions
00737 
00738 
00739 template<typename T>
00740 inline
00741 ParameterList& ParameterList::set(
00742   std::string const& name_in, T const& value_in, std::string const& docString_in,
00743   RCP<const ParameterEntryValidator> const& validator_in
00744   )
00745 {
00746   typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB;
00747   const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
00748   if (param_idx != SIOVOCB::getInvalidOrdinal()) {
00749     Ptr<ParameterEntry> param = params_.getNonconstObjPtr(param_idx);
00750     const std::string docString =
00751       (docString_in.length() ? docString_in : param->docString());
00752     const RCP<const ParameterEntryValidator> validator =
00753       (nonnull(validator_in) ? validator_in : param->validator());
00754      // Create temp param to validate before setting
00755     ParameterEntry param_new(value_in, false, false, docString, validator );
00756     if (nonnull(validator)) {
00757       validator->validate(param_new, name_in, this->name());
00758     }
00759     // Strong guarantee: (if exception is thrown, the value is not changed)
00760     *param = param_new;
00761   }
00762   else {
00763     ParameterEntry param_new(value_in, false, false, docString_in, validator_in);
00764     if (nonnull(param_new.validator())) {
00765       param_new.validator()->validate(param_new, name_in, this->name());
00766     }
00767     params_.setObj(name_in, param_new);
00768   }
00769   return *this;
00770 }
00771 
00772 
00773 inline
00774 ParameterList& ParameterList::set(
00775   std::string const& name_in, char value[], std::string const& docString
00776   ,RCP<const ParameterEntryValidator> const& validator
00777   ) 
00778 { return set(name_in, std::string(value), docString, validator); }
00779 
00780 
00781 inline
00782 ParameterList& ParameterList::set(
00783   const std::string& name_in, const char value[], const std::string &docString
00784   ,RCP<const ParameterEntryValidator> const& validator
00785   ) 
00786 { return set( name_in, std::string(value), docString, validator ); }
00787 
00788 
00789 inline
00790 ParameterList& ParameterList::set(
00791   std::string const& name_in, ParameterList const& value, std::string const& /*docString*/
00792   )
00793 {
00794   sublist(name_in) = value;
00795   return *this;
00796 }
00797 
00798 
00799 inline
00800 ParameterList& ParameterList::setEntry(std::string const& name_in, ParameterEntry const& entry_in)
00801 {
00802   params_.setObj(name_in, entry_in);
00803   return *this;
00804 }
00805 
00806 
00807 // Get functions
00808 
00809 
00810 template<typename T>
00811 T& ParameterList::get(const std::string& name_in, T def_value)
00812 {
00813   typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB;
00814   Ordinal param_idx = params_.getObjOrdinalIndex(name_in); 
00815   if (param_idx == SIOVOCB::getInvalidOrdinal()) {
00816     // Param does not exist
00817     param_idx = params_.setObj(name_in, ParameterEntry(def_value, true));
00818   }
00819   Ptr<ParameterEntry> param = params_.getNonconstObjPtr(param_idx);
00820   this->template validateEntryType<T>("get", name_in, *param);
00821   return getValue<T>(*param);
00822 }
00823 
00824 
00825 inline
00826 std::string& ParameterList::get(const std::string& name_in, char def_value[])
00827 { return get(name_in, std::string(def_value)); }
00828 
00829 
00830 inline
00831 std::string& ParameterList::get(const std::string& name_in, const char def_value[])
00832 { return get(name_in, std::string(def_value)); }
00833 
00834 
00835 template<typename T>
00836 T& ParameterList::get(const std::string& name_in) 
00837 {
00838   ParameterEntry *foundEntry = this->getEntryPtr(name_in);
00839   validateEntryExists("get",name_in,foundEntry);
00840   this->template validateEntryType<T>("get",name_in,*foundEntry);
00841   return getValue<T>(*foundEntry);
00842 }
00843 
00844   
00845 template<typename T>
00846 const T& ParameterList::get(const std::string& name_in) const
00847 {
00848   const ParameterEntry *foundEntry = this->getEntryPtr(name_in);
00849   validateEntryExists("get",name_in,foundEntry);
00850   this->template validateEntryType<T>("get",name_in,*foundEntry);
00851   return getValue<T>(*foundEntry);
00852 }
00853 
00854 
00855 template<typename T>
00856 inline
00857 T* ParameterList::getPtr(const std::string& name_in) 
00858 {
00859   typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB;
00860   const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
00861   if (param_idx != SIOVOCB::getInvalidOrdinal()) {
00862     const Ptr<ParameterEntry> param_ptr = params_.getNonconstObjPtr(param_idx);
00863     if (param_ptr->isType<T>()) {
00864       return &param_ptr->getValue<T>(0);
00865     }
00866     // Note: The above is inefficinet.  You have to do the dynamic_cast twice
00867     // (once to see if it is the type and once to do the cast).  This could be
00868     // made more efficinet by upgrading Teuchos::any to add a any_cast_ptr()
00869     // function but I don't think anyone actually uses this function.
00870     return 0;
00871   }
00872   return 0;
00873 }
00874 
00875   
00876 template<typename T>
00877 inline
00878 const T* ParameterList::getPtr(const std::string& name_in) const
00879 {
00880   typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB;
00881   const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
00882   if (param_idx != SIOVOCB::getInvalidOrdinal()) {
00883     const Ptr<const ParameterEntry> param_ptr = params_.getObjPtr(param_idx);
00884     if (param_ptr->isType<T>()) {
00885       return &param_ptr->getValue<T>(0);
00886     }
00887     // Note: The above is inefficinet, see above non-const getPtr() function.
00888     return 0;
00889   }
00890   return 0;
00891 }
00892 
00893 
00894 inline
00895 ParameterEntry& ParameterList::getEntry(const std::string& name_in)
00896 {
00897   ParameterEntry *foundEntry = this->getEntryPtr(name_in);
00898   validateEntryExists("get", name_in, foundEntry);
00899   return *foundEntry;
00900 }
00901 
00902   
00903 inline
00904 const ParameterEntry& ParameterList::getEntry(const std::string& name_in) const
00905 {
00906   const ParameterEntry *foundEntry = this->getEntryPtr(name_in);
00907   validateEntryExists("get", name_in, foundEntry);
00908   return *foundEntry;
00909 }
00910 
00911 
00912 inline
00913 ParameterEntry*
00914 ParameterList::getEntryPtr(const std::string& name_in)
00915 {
00916   typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB;
00917   const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
00918   if (param_idx != SIOVOCB::getInvalidOrdinal()) {
00919     return &*params_.getNonconstObjPtr(param_idx);
00920   }
00921   return 0;
00922 }
00923 
00924 
00925 inline
00926 const ParameterEntry*
00927 ParameterList::getEntryPtr(const std::string& name_in) const
00928 {
00929   typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB;
00930   const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
00931   if (param_idx != SIOVOCB::getInvalidOrdinal()) {
00932     return &*params_.getObjPtr(param_idx);
00933   }
00934   return 0;
00935 }
00936 
00937 
00938 inline RCP<ParameterEntry>
00939 ParameterList::getEntryRCP(const std::string& name_in)
00940 {
00941   typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB;
00942   const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
00943   if (param_idx != SIOVOCB::getInvalidOrdinal()) {
00944     return rcpFromPtr(params_.getNonconstObjPtr(param_idx));
00945   }
00946   return null;
00947 }
00948 
00949 
00950 inline RCP<const ParameterEntry>
00951 ParameterList::getEntryRCP(const std::string& name_in) const
00952 {
00953   typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB;
00954   const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
00955   if (param_idx != SIOVOCB::getInvalidOrdinal()) {
00956     return rcpFromPtr(params_.getObjPtr(param_idx));
00957   }
00958   return null;
00959 }
00960 
00961 
00962 // Attribute Functions
00963 
00964 
00965 inline
00966 const std::string& ParameterList::name() const
00967 {
00968   return name_;
00969 }
00970 
00971   
00972 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00973 template<typename T>
00974 bool ParameterList::isType(const std::string& name_in, T* /*ptr*/) const
00975 {
00976   typedef StringIndexedOrderedValueObjectContainerBase SIOVOCB;
00977   const Ordinal param_idx = params_.getObjOrdinalIndex(name_in);
00978   if (param_idx != SIOVOCB::getInvalidOrdinal()) {
00979     return params_.getObjPtr(param_idx)->isType<T>();
00980   }
00981   return false;
00982 }
00983 #endif
00984 
00985   
00986 template<typename T>
00987 bool ParameterList::isType(const std::string& name_in) const
00988 {
00989   return this->isType(name_in, static_cast<T*>(0));
00990 }
00991 
00992 
00993 // Read-only access to the iterator
00994 
00995 
00996 inline ParameterList::ConstIterator ParameterList::begin() const
00997 {
00998   return params_.begin();
00999 }
01000 
01001 
01002 inline ParameterList::ConstIterator ParameterList::end() const
01003 {
01004   return params_.end();
01005 }
01006 
01007 
01008 inline const std::string& ParameterList::name(ConstIterator i) const
01009 {
01010   return (i->first);
01011 }
01012 
01013 
01014 inline const ParameterEntry& ParameterList::entry(ConstIterator i) const
01015 {
01016   return (i->second);
01017 }
01018 
01019 
01020 // private
01021 
01022 
01023 inline ParameterList::Iterator ParameterList::nonconstBegin()
01024 {
01025   return params_.nonconstBegin();
01026 }
01027 
01028 
01029 inline ParameterList::Iterator ParameterList::nonconstEnd()
01030 {
01031   return params_.nonconstEnd();
01032 }
01033 
01034 
01035 inline ParameterEntry& ParameterList::nonconstEntry(Iterator i)
01036 {
01037   return (i->second);
01038 }
01039 
01040 
01041 template<typename T>
01042 inline
01043 void ParameterList::validateEntryType(
01044   const std::string &/*funcName*/, const std::string &name_in,
01045   const ParameterEntry &entry_in
01046   ) const
01047 {
01048   TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(
01049     entry_in.getAny().type() != typeid(T), Exceptions::InvalidParameterType
01050     ,"Error!  An attempt was made to access parameter \""<<name_in<<"\""
01051     " of type \""<<entry_in.getAny().typeName()<<"\""
01052     "\nin the parameter (sub)list \""<<this->name()<<"\""
01053     "\nusing the incorrect type \""<<TypeNameTraits<T>::name()<<"\"!"
01054     );
01055 }
01056 
01057 
01058 // //////////////////////////////////////
01059 // Helper functions
01060 
01061   
01068 template<typename T>
01069 T& getParameter( ParameterList& l, const std::string& name )
01070 {
01071   return l.template get<T>(name);
01072 }
01073 
01074   
01080 template<typename T>
01081 inline
01082 T& get( ParameterList& l, const std::string& name )
01083 {
01084   return getParameter<T>(l,name);
01085 }
01086 
01087   
01094 template<typename T>
01095 const T& getParameter( const ParameterList& l, const std::string& name )
01096 {
01097   return l.template get<T>(name);
01098 }
01099 
01100   
01108 template<typename T>
01109 inline
01110 T* getParameterPtr( ParameterList& l, const std::string& name )
01111 {
01112   return l.template getPtr<T>(name);
01113 }
01114 
01115   
01123 template<typename T>
01124 inline
01125 const T* getParameterPtr( const ParameterList& l, const std::string& name )
01126 {
01127   return l.template getPtr<T>(name);
01128 }
01129 
01130   
01137 template<typename T>
01138 inline
01139 bool isParameterType( ParameterList& l, const std::string& name )
01140 {
01141   return l.isType( name, (T*)NULL );
01142 }
01143 
01144   
01151 template<typename T>
01152 inline
01153 bool isParameterType( const ParameterList& l, const std::string& name )
01154 {
01155   return l.isType( name, (T*)NULL );
01156 }
01157 
01158   
01170 template<typename T>
01171 void setStringParameterFromArray(
01172   const std::string          &paramName
01173   ,const Array<T>       &array
01174   ,ParameterList        *paramList
01175   )
01176 {
01177   TEUCHOS_TEST_FOR_EXCEPT(!paramList);
01178   paramList->set(paramName,toString(array));
01179 }
01180 
01181   
01246 template<typename T>
01247 Array<T> getArrayFromStringParameter(
01248   const ParameterList   &paramList
01249   ,const std::string         &paramName
01250   ,const int            arrayDim        = -1
01251   ,const bool           mustExist       = true
01252   )
01253 {
01254   std::string arrayStr;
01255   if(mustExist) {
01256     arrayStr = getParameter<std::string>(paramList,paramName);
01257   }
01258   else {
01259     const std::string
01260       *arrayStrPtr = getParameterPtr<std::string>(paramList,paramName);
01261     if(arrayStrPtr) {
01262       arrayStr = *arrayStrPtr;
01263     }
01264     else {
01265       return Array<T>(); // Return an empty array
01266     }
01267   }
01268   Array<T> a;
01269   try {
01270     a = fromStringToArray<T>(arrayStr);
01271   }
01272   catch( const InvalidArrayStringRepresentation&) {
01273     TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(
01274       true, Exceptions::InvalidParameterValue
01275       ,"Error!  The parameter \""<<paramName<<"\"\n"
01276       "in the sublist \""<<paramList.name()<<"\"\n"
01277       "exists, but the std::string value:\n"
01278       "----------\n"
01279       <<arrayStr<<
01280       "\n----------\n"
01281       "is not a valid array represntation!"
01282       );
01283   }
01284   TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(
01285     ( ( a.size()>0 && arrayDim>=0 ) && static_cast<int>(a.size())!=arrayDim )
01286     ,Exceptions::InvalidParameterValue
01287     ,"Error!  The parameter \""<<paramName<<"\"\n"
01288     "in the sublist \""<<paramList.name()<<"\"\n"
01289     "exists and is a valid array, but the dimension of\n"
01290     "the read in array a.size() = " << a.size() << "\n"
01291     "was not equal to the expected size arrayDim = " << arrayDim << "!"
01292     );
01293   return a;
01294 }
01295 
01296 
01300 inline
01301 RCP<ParameterList> sublist(
01302   const RCP<ParameterList> &paramList, const std::string& name,
01303   bool mustAlreadyExist = false, const std::string& docString = ""
01304   )
01305 {
01306   return rcpWithEmbeddedObjPostDestroy(
01307     &paramList->sublist(name, mustAlreadyExist, docString), paramList, false );
01308 }
01309 
01310 
01314 inline
01315 RCP<const ParameterList> sublist(
01316   const RCP<const ParameterList> &paramList, const std::string& name
01317   )
01318 {
01319   return rcpWithEmbeddedObjPostDestroy(
01320     &paramList->sublist(name), paramList, false );
01321 }
01322 
01323   
01327 inline std::ostream& operator<<(std::ostream& os, const ParameterList& l)
01328 {
01329   return l.print(os);
01330 }
01331 
01332   
01333 } // end of Teuchos namespace
01334 
01335 
01336 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines