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::const_iterator ConstIterator;
00069 
00071   typedef Map::iterator Iterator;
00072   
00073 public:
00074   
00076 
00077   ParameterList();
00078   
00080   ParameterList(const ParameterList& source);
00081   
00083   ~ParameterList();
00085   
00087   
00089   ParameterList& operator=(const ParameterList& source);
00090   
00099   template<typename T>
00100   void set(const string& name, T value);
00101 
00105   void set(const string& name, char* value) 
00106   { set( name, std::string(value) ); }
00107 
00111   void set(const string& name, const char* value) 
00112   { set( name, std::string(value) ); }
00113 
00116   void set(const string& name, ParameterList value)
00117   { sublist(name) = value; }
00118   
00120   
00122   
00132   template<typename T>
00133   T& get(const string& name, T def_value);
00134   
00138   std::string& get(const string& name, char* def_value)
00139   { return get(name, std::string(def_value)); }
00140 
00144   std::string& get(const string& name, const char* def_value)
00145   { return get(name, std::string(def_value)); }
00146   
00150   template<typename T>
00151   T& get(const string& name);
00152   
00156   template<typename T>
00157   const T& get(const string& name) const;  
00158   
00160   
00162 
00164   ParameterList& sublist(const string& name);
00165   
00168   const ParameterList& sublist(const string& name) const;
00170   
00172 
00175   bool isParameter(const string& name) const;
00176   
00180   bool isSublist(const string& name) const;
00181   
00186   template<typename T>
00187   bool isType(const string& name) const;
00188 
00189 #ifndef DOXYGEN_SHOULD_SKIP_THIS  
00190 
00195   template<typename T>
00196   bool isType(const string& name, T* ptr) const;
00197 #endif
00198 
00199   
00201 
00202   ostream& print(ostream& os, int indent = 0) const;
00203   
00205   void unused(ostream& os) const;
00207   
00208 private:
00209   
00211   const string& name(ConstIterator i) const;
00212   
00214   ParameterEntry& entry(Iterator i);
00215   
00217   const ParameterEntry& entry(ConstIterator i) const;
00218   
00219 private:
00220   
00222   Map params_;
00223 };
00224   
00225   
00226   template<typename T>
00227   void ParameterList::set(const string& name, T value)
00228   {
00229     params_[name].setValue(value);
00230   }
00231   
00232   template<typename T>
00233   T& ParameterList::get(const string& name, T def_value)
00234   {
00235     ConstIterator i = params_.find(name);
00236     
00237     // The parameter was not found, add it to the list
00238     if (i == params_.end()) {
00239       params_[name].setValue(def_value, true);
00240       i = params_.find(name);
00241     }
00242     // Return the value of the parameter
00243     return getValue<T>(entry(i));
00244   }
00245   
00246   template<typename T>
00247   T& ParameterList::get(const string& name) 
00248   {
00249     ConstIterator i = params_.find(name);
00250     
00251     // This parameter was not found or is wrong type, throw an exception
00252     TEST_FOR_EXCEPTION( i == params_.end(), std::runtime_error,
00253       "get ( " << name << " ) failed -- parameter does not exist! " );
00254     TEST_FOR_EXCEPTION( !isType( name, (T*)NULL ), std::runtime_error,
00255       "get ( " << name << " ) failed -- parameter is wrong type! " );
00256     
00257     // Return the value.
00258     return getValue<T>(entry(i));
00259   }
00260   
00261   template<typename T>
00262   const T& ParameterList::get(const string& name) const
00263   {
00264     ConstIterator i = params_.find(name);
00265     
00266     // This parameter was not found, throw and exception
00267     TEST_FOR_EXCEPTION( i == params_.end(), std::runtime_error,
00268       "get ( " << name << " ) failed -- parameter does not exist! " );
00269     TEST_FOR_EXCEPTION( !isType( name, (T*)NULL ), std::runtime_error,
00270       "get ( " << name << " ) failed -- parameter is wrong type! " );
00271     
00272     // Return the default value for this type
00273     return getValue<T>(entry(i));
00274   }
00275   
00276 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00277   template<typename T>
00278   bool ParameterList::isType(const string& name, T* ptr) const
00279   {
00280     ConstIterator i = params_.find(name);
00281     
00282     // If parameter doesn't exist, return false.
00283     if (i == params_.end()) 
00284       return false;
00285     // Try to cast the parameter to the type we think it should be.
00286     try {
00287       getValue<T>(entry(i));
00288     }
00289     catch( std::exception& e ) {
00290       return false;
00291   }
00292     // If no exception was thrown, we should be OK.
00293     return true;
00294   }
00295 #endif
00296   
00297   template<typename T>
00298   bool ParameterList::isType(const string& name) const
00299   {
00300     ConstIterator i = params_.find(name);
00301     
00302     // If parameter doesn't exist, return false.
00303     if (i == params_.end()) 
00304       return false;
00305     // Try to cast the parameter to the type we think it should be.
00306     try {
00307       getValue<T>(entry(i));
00308     }
00309     catch( std::exception& e ) {
00310       return false;
00311     }
00312     // If no exception was thrown, we should be OK.
00313     return true;
00314   }
00315   
00316   
00323   template<typename T>
00324   T& getParameter( ParameterList& l, const string& name )
00325   {
00326     T temp_var;
00327     // CAREFUL:  We need to be sure the parameter exists before using the temp variable to get the parameter.
00328     // This parameter was not found or is wrong type, throw an exception
00329     TEST_FOR_EXCEPTION( !l.isParameter( name ), std::runtime_error,
00330       "getParameter ( " << name << " ) failed -- parameter does not exist! " );
00331     TEST_FOR_EXCEPTION( !l.isType( name, &temp_var ), std::runtime_error,
00332       "getParameter ( " << name << " ) failed -- parameter is wrong type! " );
00333     //
00334     // This parameter exists and is of the right type, so we can retrieve it safely.    
00335     //
00336     return l.get( name, temp_var );
00337   }
00338   
00345   template<typename T>
00346   const T& getParameter( const ParameterList& l, const string& name )
00347   {
00348     T temp_var;
00349     // CAREFUL:  We need to be sure the parameter exists before using the temp variable to get the parameter.
00350     // This parameter was not found or is wrong type, throw an exception
00351     TEST_FOR_EXCEPTION( !l.isParameter( name ), std::runtime_error,
00352       "getParameter ( " << name << " ) failed -- parameter does not exist! " );
00353     TEST_FOR_EXCEPTION( !l.isType( name, &temp_var ), std::runtime_error,
00354       "getParameter ( " << name << " ) failed -- parameter is wrong type! " );
00355     //
00356     // This parameter exists and is of the right type, so we can retrieve it safely.    
00357     //
00358     return l.get( name, temp_var );
00359   }
00360   
00367   template<typename T>
00368   bool isParameterType( ParameterList& l, const string& name )
00369   {
00370     return l.isType( name, (T*)NULL );
00371   }
00372   
00379   template<typename T>
00380   bool isParameterType( const ParameterList& l, const string& name )
00381   {
00382     return l.isType( name, (T*)NULL );
00383   }
00384   
00388   inline ostream& operator<<(ostream& os, const ParameterList& l)
00389   {
00390     return l.print(os);
00391   }
00392   
00393 } // end of Teuchos namespace
00394 
00395 #endif
00396 
00397 

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