FEI Version of the Day
fei_ParameterSet.hpp
00001 /*
00002 // @HEADER
00003 // ************************************************************************
00004 //             FEI: Finite Element Interface to Linear Solvers
00005 //                  Copyright (2005) Sandia Corporation.
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, the
00008 // U.S. Government retains certain rights in this software.
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 Alan Williams (william@sandia.gov) 
00038 //
00039 // ************************************************************************
00040 // @HEADER
00041 */
00042 
00043 
00044 #ifndef _fei_ParameterSet_hpp_
00045 #define _fei_ParameterSet_hpp_
00046 
00047 #include "fei_macros.hpp"
00048 #include "fei_Param.hpp"
00049 
00050 #include <vector>
00051 
00052 namespace fei {
00053 
00081 class ParameterSet {
00082  public:
00084   ParameterSet();
00085 
00087   virtual ~ParameterSet();
00088 
00101   void add(const Param& param,
00102      bool maintain_unique_keys=true);
00103 
00111   const Param* get(const char* name) const;
00112 
00114   int size() const;
00115 
00117   class const_iterator {
00118   public:
00120     const_iterator() : paramarray_(NULL),
00121                        dummyParam_((const char*)NULL, (const char*)NULL),
00122                        offset_(0) {}
00124     const_iterator(int offset, std::vector<const Param*>* params)
00125      :  paramarray_(params), dummyParam_((const char*)NULL, (const char*)NULL),
00126        offset_(offset)
00127       {
00128   if (params != NULL) {
00129     if (params->empty()) {paramarray_ = NULL; offset_ = 0;}
00130   }
00131       }
00132 
00134     ~const_iterator(){}
00135 
00137     const Param& operator*() const
00138     {
00139       if (paramarray_ == NULL) return(dummyParam_);
00140       if (offset_ >= paramarray_->size()) return(dummyParam_);
00141       return( *((*paramarray_)[offset_]) );
00142     }
00143 
00145     const_iterator& operator++()
00146     {
00147       if (paramarray_ != NULL) {
00148         if (offset_ < paramarray_->size()) {
00149           ++offset_;
00150         }
00151         if (offset_ == paramarray_->size()) {
00152           offset_ = 0;
00153           paramarray_ = NULL;
00154         }
00155       }
00156       return( *this );
00157     }
00158 
00160     const_iterator& operator=(const const_iterator& src)
00161     {
00162       paramarray_ = src.paramarray_;
00163       offset_ = src.offset_;
00164       return( *this );
00165     }
00166 
00168     bool operator==(const const_iterator& rhs)
00169     {
00170       return( paramarray_ == rhs.paramarray_ && offset_ == rhs.offset_ );
00171     }
00172 
00174     bool operator!=(const const_iterator& rhs)
00175     {
00176       return( !(*this == rhs) );
00177     }
00178 
00179   private:
00180     std::vector<const fei::Param*>* paramarray_;
00181     const fei::Param dummyParam_;
00182     unsigned offset_;
00183   };
00184 
00186   const_iterator begin() const;
00187 
00189   const_iterator end() const;
00190 
00194   int getIntParamValue(const char* name,
00195            int& paramValue) const;
00196 
00201   int getDoubleParamValue(const char* name,
00202         double& paramValue) const;
00203 
00207   int getStringParamValue(const char* name,
00208         std::string& paramValue) const;
00209 
00213   int getBoolParamValue(const char* name,
00214       bool& paramValue) const;
00215 
00219   int getVoidParamValue(const char* name,
00220       const void*& paramValue) const;
00221 
00222  private:
00223   int findOffset(const fei::Param* param) const;
00224   int findOffset(const char* name) const;
00225   std::vector<const Param*>* params_;
00226 };//class ParameterSet
00227 
00228 }//namespace fei
00229 
00230 inline
00231 int fei::ParameterSet::findOffset(const char* name) const
00232 {
00233   if (params_->empty() || name == NULL) return( -1 );
00234 
00235   std::vector<const Param*>::const_iterator
00236     p_iter = params_->begin(),
00237     p_end = params_->end();
00238 
00239   int i = 0;
00240   for(; p_iter != p_end; ++p_iter, ++i) {
00241     const Param* prm = *p_iter;
00242     if (prm->getName() == name) {
00243       return(i);
00244     }
00245   }
00246   return(-1);
00247 }
00248 
00249 inline
00250 int fei::ParameterSet::findOffset(const fei::Param* param) const
00251 {
00252   if (param == NULL) return( -1 );
00253 
00254   return( findOffset( param->getName().c_str() ) );
00255 }
00256 
00257 inline
00258 fei::ParameterSet::const_iterator fei::ParameterSet::begin() const
00259 {
00260   return( const_iterator(0, params_) );
00261 }
00262 
00263 inline
00264 fei::ParameterSet::const_iterator fei::ParameterSet::end() const
00265 {
00266   return( const_iterator(0, NULL) );
00267 }
00268 
00269 inline
00270 void fei::ParameterSet::add(const fei::Param& param, bool maintain_unique_keys)
00271 {
00272   int index = findOffset(&param);
00273   const fei::Param* newparam = new fei::Param(param);
00274   if (index < 0) {
00275     params_->push_back(newparam);
00276   }
00277   else {
00278     if (maintain_unique_keys) {
00279       delete (*params_)[index];
00280       (*params_)[index] = newparam;
00281     }
00282     else {
00283       params_->push_back(newparam);
00284     }
00285   }
00286 }
00287 
00288 inline
00289 int fei::ParameterSet::size() const
00290 {
00291   return( params_->size() );
00292 }
00293 
00294 inline
00295 const fei::Param* fei::ParameterSet::get(const char* name) const
00296 {
00297   if (params_ == NULL) return(NULL);
00298 
00299   int index = findOffset(name);
00300   if (index < 0) return(NULL);
00301   return( (*params_)[index] );
00302 }
00303 
00304 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends