FEI Version of the Day
fei_ParameterSet.hpp
00001 /*--------------------------------------------------------------------*/
00002 /*    Copyright 2005 Sandia Corporation.                              */
00003 /*    Under the terms of Contract DE-AC04-94AL85000, there is a       */
00004 /*    non-exclusive license for use of this work by or on behalf      */
00005 /*    of the U.S. Government.  Export of this program may require     */
00006 /*    a license from the United States Government.                    */
00007 /*--------------------------------------------------------------------*/
00008 
00009 #ifndef _fei_ParameterSet_hpp_
00010 #define _fei_ParameterSet_hpp_
00011 
00012 #include "fei_macros.hpp"
00013 #include "fei_Param.hpp"
00014 
00015 #include <vector>
00016 
00017 namespace fei {
00018 
00046 class ParameterSet {
00047  public:
00049   ParameterSet();
00050 
00052   virtual ~ParameterSet();
00053 
00066   void add(const Param& param,
00067      bool maintain_unique_keys=true);
00068 
00076   const Param* get(const char* name) const;
00077 
00079   int size() const;
00080 
00082   class const_iterator {
00083   public:
00085     const_iterator() : paramarray_(NULL),
00086                        dummyParam_((const char*)NULL, (const char*)NULL),
00087                        offset_(0) {}
00089     const_iterator(int offset, std::vector<const Param*>* params)
00090      :  paramarray_(params), dummyParam_((const char*)NULL, (const char*)NULL),
00091        offset_(offset)
00092       {
00093   if (params != NULL) {
00094     if (params->empty()) {paramarray_ = NULL; offset_ = 0;}
00095   }
00096       }
00097 
00099     ~const_iterator(){}
00100 
00102     const Param& operator*() const
00103     {
00104       if (paramarray_ == NULL) return(dummyParam_);
00105       if (offset_ >= paramarray_->size()) return(dummyParam_);
00106       return( *((*paramarray_)[offset_]) );
00107     }
00108 
00110     const_iterator& operator++()
00111     {
00112       if (paramarray_ != NULL) {
00113         if (offset_ < paramarray_->size()) {
00114           ++offset_;
00115         }
00116         if (offset_ == paramarray_->size()) {
00117           offset_ = 0;
00118           paramarray_ = NULL;
00119         }
00120       }
00121       return( *this );
00122     }
00123 
00125     const_iterator& operator=(const const_iterator& src)
00126     {
00127       paramarray_ = src.paramarray_;
00128       offset_ = src.offset_;
00129       return( *this );
00130     }
00131 
00133     bool operator==(const const_iterator& rhs)
00134     {
00135       return( paramarray_ == rhs.paramarray_ && offset_ == rhs.offset_ );
00136     }
00137 
00139     bool operator!=(const const_iterator& rhs)
00140     {
00141       return( !(*this == rhs) );
00142     }
00143 
00144   private:
00145     std::vector<const fei::Param*>* paramarray_;
00146     const fei::Param dummyParam_;
00147     unsigned offset_;
00148   };
00149 
00151   const_iterator begin() const;
00152 
00154   const_iterator end() const;
00155 
00159   int getIntParamValue(const char* name,
00160            int& paramValue) const;
00161 
00166   int getDoubleParamValue(const char* name,
00167         double& paramValue) const;
00168 
00172   int getStringParamValue(const char* name,
00173         std::string& paramValue) const;
00174 
00178   int getBoolParamValue(const char* name,
00179       bool& paramValue) const;
00180 
00184   int getVoidParamValue(const char* name,
00185       const void*& paramValue) const;
00186 
00187  private:
00188   int findOffset(const fei::Param* param) const;
00189   int findOffset(const char* name) const;
00190   std::vector<const Param*>* params_;
00191 };//class ParameterSet
00192 
00193 }//namespace fei
00194 
00195 inline
00196 int fei::ParameterSet::findOffset(const char* name) const
00197 {
00198   if (params_->empty() || name == NULL) return( -1 );
00199 
00200   std::vector<const Param*>::const_iterator
00201     p_iter = params_->begin(),
00202     p_end = params_->end();
00203 
00204   int i = 0;
00205   for(; p_iter != p_end; ++p_iter, ++i) {
00206     const Param* prm = *p_iter;
00207     if (prm->getName() == name) {
00208       return(i);
00209     }
00210   }
00211   return(-1);
00212 }
00213 
00214 inline
00215 int fei::ParameterSet::findOffset(const fei::Param* param) const
00216 {
00217   if (param == NULL) return( -1 );
00218 
00219   return( findOffset( param->getName().c_str() ) );
00220 }
00221 
00222 inline
00223 fei::ParameterSet::const_iterator fei::ParameterSet::begin() const
00224 {
00225   return( const_iterator(0, params_) );
00226 }
00227 
00228 inline
00229 fei::ParameterSet::const_iterator fei::ParameterSet::end() const
00230 {
00231   return( const_iterator(0, NULL) );
00232 }
00233 
00234 inline
00235 void fei::ParameterSet::add(const fei::Param& param, bool maintain_unique_keys)
00236 {
00237   int index = findOffset(&param);
00238   const fei::Param* newparam = new fei::Param(param);
00239   if (index < 0) {
00240     params_->push_back(newparam);
00241   }
00242   else {
00243     if (maintain_unique_keys) {
00244       delete (*params_)[index];
00245       (*params_)[index] = newparam;
00246     }
00247     else {
00248       params_->push_back(newparam);
00249     }
00250   }
00251 }
00252 
00253 inline
00254 int fei::ParameterSet::size() const
00255 {
00256   return( params_->size() );
00257 }
00258 
00259 inline
00260 const fei::Param* fei::ParameterSet::get(const char* name) const
00261 {
00262   if (params_ == NULL) return(NULL);
00263 
00264   int index = findOffset(name);
00265   if (index < 0) return(NULL);
00266   return( (*params_)[index] );
00267 }
00268 
00269 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends