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 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 
00030 #ifndef TEUCHOS_PARAMETER_LIST_H
00031 #define TEUCHOS_PARAMETER_LIST_H
00032 
00037 #include "Teuchos_ParameterEntry.hpp" // class data element 
00038 #include "Teuchos_TestForException.hpp"
00039 #include "Teuchos_ConfigDefs.hpp"
00040 #include "Teuchos_map.hpp"
00041 #include <typeinfo>
00042 
00060 namespace Teuchos {
00061 
00062 class ParameterList {
00063 
00065   typedef Teuchos::map<string, ParameterEntry> Map;
00066 
00068   typedef Map::iterator Iterator;
00069   
00070 public:
00071 
00072   /* Made this typedef public so that we can use iterators in 
00073    * converting to NOX parameter lists -- KL 7 August 2004 */
00075   typedef Map::const_iterator ConstIterator;
00076   
00078 
00079   ParameterList();
00080   
00082   ParameterList(const ParameterList& source);
00083   
00085   ~ParameterList();
00087   
00089   
00091   ParameterList& operator=(const ParameterList& source);
00092   
00101   template<typename T>
00102   void set(const string& name, T value);
00103 
00107   void set(const string& name, char* value) 
00108   { set( name, std::string(value) ); }
00109 
00113   void set(const string& name, const char* value) 
00114   { set( name, std::string(value) ); }
00115 
00118   void set(const string& name, ParameterList value)
00119   { sublist(name) = value; }
00120 
00125   void setEntry(const string& name, const ParameterEntry& entry)
00126   {params_[name] = entry;}
00128   
00130   
00140   template<typename T>
00141   T& get(const string& name, T def_value);
00142   
00146   std::string& get(const string& name, char* def_value)
00147   { return get(name, std::string(def_value)); }
00148 
00152   std::string& get(const string& name, const char* def_value)
00153   { return get(name, std::string(def_value)); }
00154   
00158   template<typename T>
00159   T& get(const string& name);
00160   
00164   template<typename T>
00165   const T& get(const string& name) const;  
00166   
00168   
00170 
00172   ParameterList& sublist(const string& name);
00173   
00176   const ParameterList& sublist(const string& name) const;
00178   
00180 
00183   bool isParameter(const string& name) const;
00184   
00188   bool isSublist(const string& name) const;
00189   
00194   template<typename T>
00195   bool isType(const string& name) const;
00196 
00197 #ifndef DOXYGEN_SHOULD_SKIP_THIS  
00198 
00203   template<typename T>
00204   bool isType(const string& name, T* ptr) const;
00205 #endif
00206 
00207   
00209 
00210   ostream& print(ostream& os, int indent = 0) const;
00211   
00213   void unused(ostream& os) const;
00215 
00216 
00217   /* Added these methods to facilitate conversion to NOX parameter lists 
00218    * -- KL 7 August 2004 */
00220 
00221   ConstIterator begin() const ;
00222 
00224   ConstIterator end() const ;
00225 
00227   const ParameterEntry& entry(ConstIterator i) const;
00228   
00230   const string& name(ConstIterator i) const;
00232   
00233 private:
00234   
00236   ParameterEntry& entry(Iterator i);
00237   
00238 private:
00239   
00241   Map params_;
00242 };
00243   
00244   
00245   template<typename T>
00246   void ParameterList::set(const string& name, T value)
00247   {
00248     params_[name].setValue(value);
00249   }
00250   
00251   template<typename T>
00252   T& ParameterList::get(const string& name, T def_value)
00253   {
00254     ConstIterator i = params_.find(name);
00255     
00256     // The parameter was not found, add it to the list
00257     if (i == params_.end()) {
00258       params_[name].setValue(def_value, true);
00259       i = params_.find(name);
00260     }
00261     // Return the value of the parameter
00262     return getValue<T>(entry(i));
00263   }
00264   
00265   template<typename T>
00266   T& ParameterList::get(const string& name) 
00267   {
00268     ConstIterator i = params_.find(name);
00269     
00270     // This parameter was not found or is wrong type, throw an exception
00271     TEST_FOR_EXCEPTION( i == params_.end(), std::runtime_error,
00272       "get ( " << name << " ) failed -- parameter does not exist! " );
00273     TEST_FOR_EXCEPTION( !isType( name, (T*)NULL ), std::runtime_error,
00274       "get ( " << name << " ) failed -- parameter is wrong type! " );
00275     
00276     // Return the value.
00277     return getValue<T>(entry(i));
00278   }
00279   
00280   template<typename T>
00281   const T& ParameterList::get(const string& name) const
00282   {
00283     ConstIterator i = params_.find(name);
00284     
00285     // This parameter was not found, throw and exception
00286     TEST_FOR_EXCEPTION( i == params_.end(), std::runtime_error,
00287       "get ( " << name << " ) failed -- parameter does not exist! " );
00288     TEST_FOR_EXCEPTION( !isType( name, (T*)NULL ), std::runtime_error,
00289       "get ( " << name << " ) failed -- parameter is wrong type! " );
00290     
00291     // Return the default value for this type
00292     return getValue<T>(entry(i));
00293   }
00294   
00295 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00296   template<typename T>
00297   bool ParameterList::isType(const string& name, T* ptr) const
00298   {
00299     ConstIterator i = params_.find(name);
00300     
00301     // If parameter doesn't exist, return false.
00302     if (i == params_.end()) 
00303       return false;
00304     // Try to cast the parameter to the type we think it should be.
00305     try {
00306       getValue<T>(entry(i));
00307     }
00308     catch( std::exception& e ) {
00309       return false;
00310   }
00311     // If no exception was thrown, we should be OK.
00312     return true;
00313   }
00314 #endif
00315   
00316   template<typename T>
00317   bool ParameterList::isType(const string& name) const
00318   {
00319     ConstIterator i = params_.find(name);
00320     
00321     // If parameter doesn't exist, return false.
00322     if (i == params_.end()) 
00323       return false;
00324     // Try to cast the parameter to the type we think it should be.
00325     try {
00326       getValue<T>(entry(i));
00327     }
00328     catch( std::exception& e ) {
00329       return false;
00330     }
00331     // If no exception was thrown, we should be OK.
00332     return true;
00333   }
00334   
00335   
00342   template<typename T>
00343   T& getParameter( ParameterList& l, const string& name )
00344   {
00345     T temp_var;
00346     // CAREFUL:  We need to be sure the parameter exists before using the temp variable to get the parameter.
00347     // This parameter was not found or is wrong type, throw an exception
00348     TEST_FOR_EXCEPTION( !l.isParameter( name ), std::runtime_error,
00349       "getParameter ( " << name << " ) failed -- parameter does not exist! " );
00350     TEST_FOR_EXCEPTION( !l.isType( name, &temp_var ), std::runtime_error,
00351       "getParameter ( " << name << " ) failed -- parameter is wrong type! " );
00352     //
00353     // This parameter exists and is of the right type, so we can retrieve it safely.    
00354     //
00355     return l.get( name, temp_var );
00356   }
00357   
00364   template<typename T>
00365   const T& getParameter( const ParameterList& l, const string& name )
00366   {
00367     T temp_var;
00368     // CAREFUL:  We need to be sure the parameter exists before using the temp variable to get the parameter.
00369     // This parameter was not found or is wrong type, throw an exception
00370     TEST_FOR_EXCEPTION( !l.isParameter( name ), std::runtime_error,
00371       "getParameter ( " << name << " ) failed -- parameter does not exist! " );
00372     TEST_FOR_EXCEPTION( !l.isType( name, &temp_var ), std::runtime_error,
00373       "getParameter ( " << name << " ) failed -- parameter is wrong type! " );
00374     //
00375     // This parameter exists and is of the right type, so we can retrieve it safely.    
00376     //
00377     return l.get( name, temp_var );
00378   }
00379   
00386   template<typename T>
00387   bool isParameterType( ParameterList& l, const string& name )
00388   {
00389     return l.isType( name, (T*)NULL );
00390   }
00391   
00398   template<typename T>
00399   bool isParameterType( const ParameterList& l, const string& name )
00400   {
00401     return l.isType( name, (T*)NULL );
00402   }
00403   
00407   inline ostream& operator<<(ostream& os, const ParameterList& l)
00408   {
00409     return l.print(os);
00410   }
00411   
00412 } // end of Teuchos namespace
00413 
00414 #endif
00415 
00416 

Generated on Thu Sep 18 12:41:17 2008 for Teuchos - Trilinos Tools Package by doxygen 1.3.9.1