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 
00059 namespace Teuchos {
00060 
00061 class ParameterList {
00062 
00064   typedef Teuchos::map<string, ParameterEntry> Map;
00065 
00067   typedef Map::iterator Iterator;
00068   
00069 public:
00070 
00071   /* Made this typedef public so that we can use iterators in 
00072    * converting to NOX parameter lists -- KL 7 August 2004 */
00074   typedef Map::const_iterator ConstIterator;
00075   
00077 
00078   ParameterList();
00079   
00081   ParameterList(const ParameterList& source);
00082   
00084   ~ParameterList();
00086   
00088   
00090   ParameterList& operator=(const ParameterList& source);
00091   
00100   template<typename T>
00101   void set(const string& name, T value);
00102 
00106   void set(const string& name, char* value) 
00107   { set( name, std::string(value) ); }
00108 
00112   void set(const string& name, const char* value) 
00113   { set( name, std::string(value) ); }
00114 
00117   void set(const string& name, ParameterList value)
00118   { sublist(name) = value; }
00119 
00124   void setEntry(const string& name, const ParameterEntry& entry)
00125   {params_[name] = entry;}
00127   
00129   
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     } else {
00261       // The parameter was found, make sure it is the same type as T.
00262       TEST_FOR_EXCEPTION( !isType( name, (T*)NULL ), std::runtime_error,
00263         "get ( " << name << ", T def_value ) failed -- parameter is wrong type! " );
00264     }
00265 
00266     // Return the value of the parameter
00267     return getValue<T>(entry(i));
00268   }
00269   
00270   template<typename T>
00271   T& ParameterList::get(const string& name) 
00272   {
00273     ConstIterator i = params_.find(name);
00274     
00275     // This parameter was not found or is wrong type, throw an exception
00276     TEST_FOR_EXCEPTION( i == params_.end(), std::runtime_error,
00277       "get ( " << name << " ) failed -- parameter does not exist! " );
00278     TEST_FOR_EXCEPTION( !isType( name, (T*)NULL ), std::runtime_error,
00279       "get ( " << name << " ) failed -- parameter is wrong type! " );
00280     
00281     // Return the value.
00282     return getValue<T>(entry(i));
00283   }
00284   
00285   template<typename T>
00286   const T& ParameterList::get(const string& name) const
00287   {
00288     ConstIterator i = params_.find(name);
00289     
00290     // This parameter was not found, throw and exception
00291     TEST_FOR_EXCEPTION( i == params_.end(), std::runtime_error,
00292       "get ( " << name << " ) failed -- parameter does not exist! " );
00293     TEST_FOR_EXCEPTION( !isType( name, (T*)NULL ), std::runtime_error,
00294       "get ( " << name << " ) failed -- parameter is wrong type! " );
00295     
00296     // Return the default value for this type
00297     return getValue<T>(entry(i));
00298   }
00299   
00300 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00301   template<typename T>
00302   bool ParameterList::isType(const string& name, T* ptr) const
00303   {
00304     ConstIterator i = params_.find(name);
00305     
00306     // If parameter doesn't exist, return false.
00307     if (i == params_.end()) 
00308       return false;
00309     // Try to cast the parameter to the type we think it should be.
00310     try {
00311       getValue<T>(entry(i));
00312     }
00313     catch( std::exception& e ) {
00314       return false;
00315   }
00316     // If no exception was thrown, we should be OK.
00317     return true;
00318   }
00319 #endif
00320   
00321   template<typename T>
00322   bool ParameterList::isType(const string& name) const
00323   {
00324     ConstIterator i = params_.find(name);
00325     
00326     // If parameter doesn't exist, return false.
00327     if (i == params_.end()) 
00328       return false;
00329     // Try to cast the parameter to the type we think it should be.
00330     try {
00331       getValue<T>(entry(i));
00332     }
00333     catch( std::exception& e ) {
00334       return false;
00335     }
00336     // If no exception was thrown, we should be OK.
00337     return true;
00338   }
00339   
00340   
00347   template<typename T>
00348   T& getParameter( ParameterList& l, const string& name )
00349   {
00350     T temp_var;
00351     // CAREFUL:  We need to be sure the parameter exists before using the temp variable to get the parameter.
00352     // This parameter was not found or is wrong type, throw an exception
00353     TEST_FOR_EXCEPTION( !l.isParameter( name ), std::runtime_error,
00354       "getParameter ( " << name << " ) failed -- parameter does not exist! " );
00355     TEST_FOR_EXCEPTION( !l.isType( name, &temp_var ), std::runtime_error,
00356       "getParameter ( " << name << " ) failed -- parameter is wrong type! " );
00357     //
00358     // This parameter exists and is of the right type, so we can retrieve it safely.    
00359     //
00360     return l.get( name, temp_var );
00361   }
00362   
00369   template<typename T>
00370   const T& getParameter( const ParameterList& l, const string& name )
00371   {
00372     T temp_var;
00373     // CAREFUL:  We need to be sure the parameter exists before using the temp variable to get the parameter.
00374     // This parameter was not found or is wrong type, throw an exception
00375     TEST_FOR_EXCEPTION( !l.isParameter( name ), std::runtime_error,
00376       "getParameter ( " << name << " ) failed -- parameter does not exist! " );
00377     TEST_FOR_EXCEPTION( !l.isType( name, &temp_var ), std::runtime_error,
00378       "getParameter ( " << name << " ) failed -- parameter is wrong type! " );
00379     //
00380     // This parameter exists and is of the right type, so we can retrieve it safely.    
00381     //
00382     return l.get( name, temp_var );
00383   }
00384   
00391   template<typename T>
00392   bool isParameterType( ParameterList& l, const string& name )
00393   {
00394     return l.isType( name, (T*)NULL );
00395   }
00396   
00403   template<typename T>
00404   bool isParameterType( const ParameterList& l, const string& name )
00405   {
00406     return l.isType( name, (T*)NULL );
00407   }
00408   
00412   inline ostream& operator<<(ostream& os, const ParameterList& l)
00413   {
00414     return l.print(os);
00415   }
00416   
00417 } // end of Teuchos namespace
00418 
00419 #endif
00420 
00421 

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