FEI Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fei_ParameterSet.hpp
Go to the documentation of this file.
1 /*--------------------------------------------------------------------*/
2 /* Copyright 2005 Sandia Corporation. */
3 /* Under the terms of Contract DE-AC04-94AL85000, there is a */
4 /* non-exclusive license for use of this work by or on behalf */
5 /* of the U.S. Government. Export of this program may require */
6 /* a license from the United States Government. */
7 /*--------------------------------------------------------------------*/
8 
9 #ifndef _fei_ParameterSet_hpp_
10 #define _fei_ParameterSet_hpp_
11 
12 #include "fei_macros.hpp"
13 #include "fei_Param.hpp"
14 
15 #include <vector>
16 
17 namespace fei {
18 
46 class ParameterSet {
47  public:
49  ParameterSet();
50 
52  virtual ~ParameterSet();
53 
66  void add(const Param& param,
67  bool maintain_unique_keys=true);
68 
76  const Param* get(const char* name) const;
77 
79  int size() const;
80 
83  public:
86  dummyParam_((const char*)NULL, (const char*)NULL),
87  offset_(0) {}
89  const_iterator(int offset, std::vector<const Param*>* params)
90  : paramarray_(params), dummyParam_((const char*)NULL, (const char*)NULL),
91  offset_(offset)
92  {
93  if (params != NULL) {
94  if (params->empty()) {paramarray_ = NULL; offset_ = 0;}
95  }
96  }
97 
100 
102  const Param& operator*() const
103  {
104  if (paramarray_ == NULL) return(dummyParam_);
105  if (offset_ >= paramarray_->size()) return(dummyParam_);
106  return( *((*paramarray_)[offset_]) );
107  }
108 
111  {
112  if (paramarray_ != NULL) {
113  if (offset_ < paramarray_->size()) {
114  ++offset_;
115  }
116  if (offset_ == paramarray_->size()) {
117  offset_ = 0;
118  paramarray_ = NULL;
119  }
120  }
121  return( *this );
122  }
123 
126  {
127  paramarray_ = src.paramarray_;
128  offset_ = src.offset_;
129  return( *this );
130  }
131 
133  bool operator==(const const_iterator& rhs)
134  {
135  return( paramarray_ == rhs.paramarray_ && offset_ == rhs.offset_ );
136  }
137 
139  bool operator!=(const const_iterator& rhs)
140  {
141  return( !(*this == rhs) );
142  }
143 
144  private:
145  std::vector<const fei::Param*>* paramarray_;
147  unsigned offset_;
148  };
149 
151  const_iterator begin() const;
152 
154  const_iterator end() const;
155 
159  int getIntParamValue(const char* name,
160  int& paramValue) const;
161 
166  int getDoubleParamValue(const char* name,
167  double& paramValue) const;
168 
172  int getStringParamValue(const char* name,
173  std::string& paramValue) const;
174 
178  int getBoolParamValue(const char* name,
179  bool& paramValue) const;
180 
184  int getVoidParamValue(const char* name,
185  const void*& paramValue) const;
186 
187  private:
188  int findOffset(const fei::Param* param) const;
189  int findOffset(const char* name) const;
190  std::vector<const Param*>* params_;
191 };//class ParameterSet
192 
193 }//namespace fei
194 
195 inline
196 int fei::ParameterSet::findOffset(const char* name) const
197 {
198  if (params_->empty() || name == NULL) return( -1 );
199 
200  std::vector<const Param*>::const_iterator
201  p_iter = params_->begin(),
202  p_end = params_->end();
203 
204  int i = 0;
205  for(; p_iter != p_end; ++p_iter, ++i) {
206  const Param* prm = *p_iter;
207  if (prm->getName() == name) {
208  return(i);
209  }
210  }
211  return(-1);
212 }
213 
214 inline
216 {
217  if (param == NULL) return( -1 );
218 
219  return( findOffset( param->getName().c_str() ) );
220 }
221 
222 inline
224 {
225  return( const_iterator(0, params_) );
226 }
227 
228 inline
230 {
231  return( const_iterator(0, NULL) );
232 }
233 
234 inline
235 void fei::ParameterSet::add(const fei::Param& param, bool maintain_unique_keys)
236 {
237  int index = findOffset(&param);
238  const fei::Param* newparam = new fei::Param(param);
239  if (index < 0) {
240  params_->push_back(newparam);
241  }
242  else {
243  if (maintain_unique_keys) {
244  delete (*params_)[index];
245  (*params_)[index] = newparam;
246  }
247  else {
248  params_->push_back(newparam);
249  }
250  }
251 }
252 
253 inline
255 {
256  return( params_->size() );
257 }
258 
259 inline
260 const fei::Param* fei::ParameterSet::get(const char* name) const
261 {
262  if (params_ == NULL) return(NULL);
263 
264  int index = findOffset(name);
265  if (index < 0) return(NULL);
266  return( (*params_)[index] );
267 }
268 
269 #endif
const Param * get(const char *name) const
int getVoidParamValue(const char *name, const void *&paramValue) const
bool operator==(const const_iterator &rhs)
int getBoolParamValue(const char *name, bool &paramValue) const
int getStringParamValue(const char *name, std::string &paramValue) const
bool operator!=(const const_iterator &rhs)
void add(const Param &param, bool maintain_unique_keys=true)
int getDoubleParamValue(const char *name, double &paramValue) const
int findOffset(const fei::Param *param) const
const_iterator(int offset, std::vector< const Param * > *params)
const std::string & getName() const
Definition: fei_Param.hpp:92
const_iterator end() const
const_iterator & operator=(const const_iterator &src)
std::vector< const Param * > * params_
const_iterator begin() const
std::vector< const fei::Param * > * paramarray_
int getIntParamValue(const char *name, int &paramValue) const