Teuchos_ParameterList.cpp

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 #include "Teuchos_ParameterList.hpp"  // class definition
00030 
00031 /* NOTE: ASCI Red (TFLOP) does not support the i-> function for iterators 
00032  * in the STL.  Therefore when compiling for the TFLOP we must redefine the 
00033  * iterator from i-> to (*i). This slows things down on other platforms 
00034  * so we switch between the two when necessary.
00035  */
00036 using namespace Teuchos;
00037 
00038 ParameterList::ParameterList() {}
00039 
00040 ParameterList::ParameterList(const ParameterList& source) 
00041 {
00042   params_ = source.params_;
00043 }
00044 
00045 ParameterList& ParameterList::operator=(const ParameterList& source) 
00046 {
00047   if (&source == this)
00048     return *this;
00049 
00050   params_ = source.params_;
00051   return *this;
00052 }
00053 
00054 ParameterList::~ParameterList() 
00055 {
00056 }
00057 
00058 void ParameterList::unused(ostream& os) const
00059 {
00060   for (ConstIterator i = params_.begin(); i != params_.end(); ++i) {
00061     if (!(entry(i).isUsed())) {
00062       os << "WARNING: Parameter \"" << name(i) << "\" " << entry(i)
00063      << " is unused" << endl;
00064     }
00065   }
00066 }
00067 
00068 bool ParameterList::isSublist(const string& name) const
00069 {
00070   ConstIterator i = params_.find(name);
00071 
00072   if (i != params_.end())
00073     return (entry(i).isList());
00074 
00075   return false;
00076 }
00077 
00078 bool ParameterList::isParameter(const string& name) const
00079 {
00080   return (params_.find(name) != params_.end());
00081 }
00082 
00083 ParameterList& ParameterList::sublist(const string& name)
00084 {
00085   // Find name in list, if it exists.
00086   Iterator i = params_.find(name);
00087 
00088   // If it does exist and is a list, return the list value.
00089   // Otherwise, throw an error.
00090   if (i != params_.end()) {
00091      TEST_FOR_EXCEPTION( !entry(i).isList(), std::runtime_error,
00092   " Parameter " << name << " is not a list!" );
00093      return getValue<ParameterList>(entry(i));
00094   }
00095 
00096   // If it does not exist, create a new empty list and return a reference
00097   return params_[name].setList(true);
00098 }
00099 
00100 const ParameterList& ParameterList::sublist(const string& name) const
00101 {
00102   // Find name in list, if it exists.
00103   ConstIterator i = params_.find(name);
00104 
00105   // If it does not exist, throw an error
00106   TEST_FOR_EXCEPTION( i == params_.end(), std::runtime_error,
00107   " Parameter " << name << " is not a valid list!" );
00108 
00109   // If it does exist and is a list, return the list value.
00110   TEST_FOR_EXCEPTION( !entry(i).isList(), std::runtime_error,
00111   " Parameter " << name << " is not a list!" );
00112   return getValue<ParameterList>(entry(i));
00113 }
00114   
00115 ostream& ParameterList::print(ostream& os, int indent) const
00116 {
00117   if (params_.begin() == params_.end()) 
00118   {
00119     for (int j = 0; j < indent; j ++)
00120       os << ' ';
00121     os << "[empty list]" << endl;
00122   }
00123   else 
00124     for (ConstIterator i = params_.begin(); i != params_.end(); ++i) 
00125     {
00126       for (int j = 0; j < indent; j ++)
00127   os << ' ';
00128       if (entry(i).isList()) 
00129       {
00130   os << name(i) << " -> " << endl;
00131   getValue<ParameterList>(entry(i)).print(os, indent + 2);
00132       }
00133       else
00134   os << name(i) << " = " << entry(i) << endl;
00135     }
00136   return os;
00137 }
00138 
00139 ParameterList::ConstIterator ParameterList::begin() const
00140 {
00141   return params_.begin();
00142 }
00143 
00144 ParameterList::ConstIterator ParameterList::end() const
00145 {
00146   return params_.end();
00147 }
00148 
00149 
00150 
00151 #if defined(TFLOP)
00152 
00153 const string& ParameterList::name(ConstIterator i) const
00154 {
00155   return ((*i).first);
00156 }
00157 
00158 ParameterEntry& ParameterList::entry(Iterator i)
00159 {
00160   return ((*i).second);
00161 }
00162 
00163 const ParameterEntry& ParameterList::entry(ConstIterator i) const
00164 {
00165   return ((*i).second);
00166 }
00167 
00168 #else
00169 
00170 const string& ParameterList::name(ConstIterator i) const
00171 {
00172   return (i->first);
00173 }
00174 
00175 ParameterEntry& ParameterList::entry(Iterator i)
00176 {
00177   return (i->second);
00178 }
00179 
00180 const ParameterEntry& ParameterList::entry(ConstIterator i) const
00181 {
00182   return (i->second);
00183 }
00184 
00185 #endif
00186 

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