libMesh
Classes | Public Types | Public Member Functions | Protected Attributes | List of all members
libMesh::Parameters Class Reference

This class provides the ability to map between arbitrary, user-defined strings and several data types. More...

#include <parameters.h>

Classes

class  Parameter
 Concrete definition of a parameter value for a specified type. More...
 
class  Value
 Abstract definition of a parameter value. More...
 

Public Types

typedef std::map< std::string, Value * >::iterator iterator
 Parameter map iterator. More...
 
typedef std::map< std::string, Value * >::const_iterator const_iterator
 Constant parameter map iterator. More...
 

Public Member Functions

 Parameters ()
 Default constructor. More...
 
 Parameters (const Parameters &)
 Copy constructor. More...
 
virtual ~Parameters ()
 Destructor. More...
 
virtual Parametersoperator= (const Parameters &source)
 Assignment operator. More...
 
virtual Parametersoperator+= (const Parameters &source)
 Addition/Assignment operator. More...
 
template<typename T >
bool have_parameter (const std::string &) const
 
template<typename T >
const T & get (const std::string &) const
 
template<typename T >
void insert (const std::string &)
 Inserts a new Parameter into the object but does not return a writable reference. More...
 
template<typename T >
T & set (const std::string &)
 
virtual void set_attributes (const std::string &, bool)
 Overridable function to set any extended attributes for classes inheriting from this class. More...
 
void remove (const std::string &)
 Removes the specified parameter from the list, if it exists. More...
 
std::size_t n_parameters () const
 
template<typename T >
unsigned int n_parameters () const
 
virtual void clear ()
 Clears internal data structures & frees any allocated memory. More...
 
void print (std::ostream &os=libMesh::out) const
 Prints the contents, by default to libMesh::out. More...
 
iterator begin ()
 Iterator pointing to the beginning of the set of parameters. More...
 
const_iterator begin () const
 Iterator pointing to the beginning of the set of parameters. More...
 
iterator end ()
 Iterator pointing to the end of the set of parameters. More...
 
const_iterator end () const
 Iterator pointing to the end of the set of parameters. More...
 

Protected Attributes

std::map< std::string, Value * > _values
 Data structure to map names with values. More...
 

Detailed Description

This class provides the ability to map between arbitrary, user-defined strings and several data types.

This can be used to provide arbitrary user-specified options.

Author
Benjamin S. Kirk
Date
2004

Definition at line 63 of file parameters.h.

Member Typedef Documentation

◆ const_iterator

typedef std::map<std::string, Value *>::const_iterator libMesh::Parameters::const_iterator

Constant parameter map iterator.

Definition at line 247 of file parameters.h.

◆ iterator

typedef std::map<std::string, Value *>::iterator libMesh::Parameters::iterator

Parameter map iterator.

Definition at line 242 of file parameters.h.

Constructor & Destructor Documentation

◆ Parameters() [1/2]

libMesh::Parameters::Parameters ( )

Default constructor.

Does nothing.

Definition at line 70 of file parameters.h.

70 {}

◆ Parameters() [2/2]

libMesh::Parameters::Parameters ( const Parameters p)

Copy constructor.

Definition at line 355 of file parameters.h.

356 {
357  *this = p;
358 }

◆ ~Parameters()

libMesh::Parameters::~Parameters ( )
virtual

Destructor.

Clears any allocated memory.

Definition at line 363 of file parameters.h.

References clear().

364 {
365  this->clear ();
366 }
virtual void clear()
Clears internal data structures & frees any allocated memory.
Definition: parameters.h:317

Member Function Documentation

◆ begin() [1/2]

Parameters::iterator libMesh::Parameters::begin ( )

Iterator pointing to the beginning of the set of parameters.

Definition at line 509 of file parameters.h.

References _values.

510 {
511  return _values.begin();
512 }
std::map< std::string, Value * > _values
Data structure to map names with values.
Definition: parameters.h:274

◆ begin() [2/2]

Parameters::const_iterator libMesh::Parameters::begin ( ) const

Iterator pointing to the beginning of the set of parameters.

Definition at line 515 of file parameters.h.

References _values.

516 {
517  return _values.begin();
518 }
std::map< std::string, Value * > _values
Data structure to map names with values.
Definition: parameters.h:274

◆ clear()

void libMesh::Parameters::clear ( )
virtual

Clears internal data structures & frees any allocated memory.

Definition at line 317 of file parameters.h.

References _values.

Referenced by operator=(), and ~Parameters().

318 { // before its first use (for some compilers)
319  while (!_values.empty())
320  {
321  Parameters::iterator it = _values.begin();
322 
323  delete it->second;
324  it->second = nullptr;
325 
326  _values.erase(it);
327  }
328 }
std::map< std::string, Value * >::iterator iterator
Parameter map iterator.
Definition: parameters.h:242
std::map< std::string, Value * > _values
Data structure to map names with values.
Definition: parameters.h:274

◆ end() [1/2]

Parameters::iterator libMesh::Parameters::end ( )

Iterator pointing to the end of the set of parameters.

Definition at line 521 of file parameters.h.

References _values.

522 {
523  return _values.end();
524 }
std::map< std::string, Value * > _values
Data structure to map names with values.
Definition: parameters.h:274

◆ end() [2/2]

Parameters::const_iterator libMesh::Parameters::end ( ) const

Iterator pointing to the end of the set of parameters.

Definition at line 527 of file parameters.h.

References _values.

528 {
529  return _values.end();
530 }
std::map< std::string, Value * > _values
Data structure to map names with values.
Definition: parameters.h:274

◆ get()

template<typename T >
const T & libMesh::Parameters::get ( const std::string &  name) const
Returns
A constant reference to the specified parameter value. Requires, of course, that the parameter exists.

Definition at line 425 of file parameters.h.

References _values, libMesh::demangle(), and libMesh::Quality::name().

426 {
427  if (!this->have_parameter<T>(name))
428  {
429  std::ostringstream oss;
430 
431  oss << "ERROR: no";
432 #ifdef LIBMESH_HAVE_RTTI
433  oss << ' ' << demangle(typeid(T).name());
434 #endif
435  oss << " parameter named \""
436  << name << "\" found.\n\n"
437  << "Known parameters:\n"
438  << *this;
439 
440  libmesh_error_msg(oss.str());
441  }
442 
444 
445  libmesh_assert(it != _values.end());
446  libmesh_assert(it->second);
447 
448  return cast_ptr<Parameter<T> *>(it->second)->get();
449 }
std::string name(const ElemQuality q)
std::map< std::string, Value * >::const_iterator const_iterator
Constant parameter map iterator.
Definition: parameters.h:247
std::string demangle(const char *name)
Mostly system independent demangler.
std::map< std::string, Value * > _values
Data structure to map names with values.
Definition: parameters.h:274

◆ have_parameter()

template<typename T >
bool libMesh::Parameters::have_parameter ( const std::string &  name) const
Returns
true if a parameter of type T with a specified name exists, false otherwise.

If RTTI has been disabled then we return true if a parameter of specified name exists regardless of its type.

Definition at line 406 of file parameters.h.

References _values, libMesh::cast_ptr(), and libMesh::Quality::name().

407 {
409 
410  if (it != _values.end())
411 #ifdef LIBMESH_HAVE_RTTI
412  if (dynamic_cast<const Parameter<T> *>(it->second) != nullptr)
413 #else // LIBMESH_HAVE_RTTI
414  if (cast_ptr<const Parameter<T> *>(it->second) != nullptr)
415 #endif // LIBMESH_HAVE_RTTI
416  return true;
417 
418  return false;
419 }
std::string name(const ElemQuality q)
std::map< std::string, Value * >::const_iterator const_iterator
Constant parameter map iterator.
Definition: parameters.h:247
Tnew cast_ptr(Told *oldvar)
std::map< std::string, Value * > _values
Data structure to map names with values.
Definition: parameters.h:274

◆ insert()

template<typename T >
void libMesh::Parameters::insert ( const std::string &  name)

Inserts a new Parameter into the object but does not return a writable reference.

The value of the newly inserted parameter may not be valid.

Definition at line 453 of file parameters.h.

References _values, libMesh::Quality::name(), and set_attributes().

454 {
455  if (!this->have_parameter<T>(name))
456  _values[name] = new Parameter<T>;
457 
458  set_attributes(name, true);
459 }
std::string name(const ElemQuality q)
std::map< std::string, Value * > _values
Data structure to map names with values.
Definition: parameters.h:274
virtual void set_attributes(const std::string &, bool)
Overridable function to set any extended attributes for classes inheriting from this class...
Definition: parameters.h:133

◆ n_parameters() [1/2]

std::size_t libMesh::Parameters::n_parameters ( ) const
Returns
The total number of parameters.

Definition at line 143 of file parameters.h.

References _values.

143 { return _values.size(); }
std::map< std::string, Value * > _values
Data structure to map names with values.
Definition: parameters.h:274

◆ n_parameters() [2/2]

template<typename T >
unsigned int libMesh::Parameters::n_parameters ( ) const
Returns
The number of parameters of the requested type.

Definition at line 493 of file parameters.h.

References _values.

494 {
495  unsigned int cnt = 0;
496 
497  Parameters::const_iterator it = _values.begin();
498  const Parameters::const_iterator vals_end = _values.end();
499 
500  for (; it != vals_end; ++it)
501  if (dynamic_cast<Parameter<T> *>(it->second) != nullptr)
502  cnt++;
503 
504  return cnt;
505 }
std::map< std::string, Value * >::const_iterator const_iterator
Constant parameter map iterator.
Definition: parameters.h:247
std::map< std::string, Value * > _values
Data structure to map names with values.
Definition: parameters.h:274

◆ operator+=()

Parameters & libMesh::Parameters::operator+= ( const Parameters source)
virtual

Addition/Assignment operator.

Inserts copies of all parameters from source. Any parameters of the same name already in this are replaced.

Definition at line 342 of file parameters.h.

References _values.

343 {
344  for (const auto & pr : source._values)
345  {
346  if (_values.find(pr.first) != _values.end())
347  delete _values[pr.first];
348  _values[pr.first] = pr.second->clone();
349  }
350 
351  return *this;
352 }
std::map< std::string, Value * > _values
Data structure to map names with values.
Definition: parameters.h:274

◆ operator=()

Parameters & libMesh::Parameters::operator= ( const Parameters source)
virtual

Assignment operator.

Removes all parameters in this and inserts copies of all parameters from source

Definition at line 333 of file parameters.h.

References clear().

334 {
335  this->clear();
336  *this += source;
337 
338  return *this;
339 }
virtual void clear()
Clears internal data structures & frees any allocated memory.
Definition: parameters.h:317

◆ print()

void libMesh::Parameters::print ( std::ostream &  os = libMesh::out) const

Prints the contents, by default to libMesh::out.

Definition at line 371 of file parameters.h.

References _values.

Referenced by libMesh::operator<<().

372 {
373  Parameters::const_iterator it = _values.begin();
374 
375  os << "Name\t Type\t Value\n"
376  << "---------------------\n";
377  while (it != _values.end())
378  {
379  os << " " << it->first
380 #ifdef LIBMESH_HAVE_RTTI
381  << "\t " << it->second->type()
382 #endif // LIBMESH_HAVE_RTTI
383  << "\t "; it->second->print(os);
384  os << '\n';
385 
386  ++it;
387  }
388 }
std::map< std::string, Value * >::const_iterator const_iterator
Constant parameter map iterator.
Definition: parameters.h:247
std::map< std::string, Value * > _values
Data structure to map names with values.
Definition: parameters.h:274

◆ remove()

void libMesh::Parameters::remove ( const std::string &  name)

Removes the specified parameter from the list, if it exists.

Definition at line 475 of file parameters.h.

References _values, and libMesh::Quality::name().

476 {
477  Parameters::iterator it = _values.find(name);
478 
479  if (it != _values.end())
480  {
481  delete it->second;
482  it->second = nullptr;
483 
484  _values.erase(it);
485  }
486 }
std::string name(const ElemQuality q)
std::map< std::string, Value * >::iterator iterator
Parameter map iterator.
Definition: parameters.h:242
std::map< std::string, Value * > _values
Data structure to map names with values.
Definition: parameters.h:274

◆ set()

template<typename T >
T & libMesh::Parameters::set ( const std::string &  name)
Returns
A writable reference to the specified parameter. This method will create the parameter if it does not exist, so it can be used to define parameters which will later be accessed with the get() member.

Definition at line 464 of file parameters.h.

References _values, libMesh::Quality::name(), and set_attributes().

465 {
466  if (!this->have_parameter<T>(name))
467  _values[name] = new Parameter<T>;
468 
469  set_attributes(name, false);
470 
471  return cast_ptr<Parameter<T> *>(_values[name])->set();
472 }
std::string name(const ElemQuality q)
std::map< std::string, Value * > _values
Data structure to map names with values.
Definition: parameters.h:274
virtual void set_attributes(const std::string &, bool)
Overridable function to set any extended attributes for classes inheriting from this class...
Definition: parameters.h:133

◆ set_attributes()

virtual void libMesh::Parameters::set_attributes ( const std::string &  ,
bool   
)
virtual

Overridable function to set any extended attributes for classes inheriting from this class.

Definition at line 133 of file parameters.h.

Referenced by insert(), and set().

133 {}

Member Data Documentation

◆ _values

std::map<std::string, Value *> libMesh::Parameters::_values
protected

Data structure to map names with values.

Definition at line 274 of file parameters.h.

Referenced by begin(), clear(), end(), get(), have_parameter(), insert(), n_parameters(), operator+=(), print(), remove(), and set().


The documentation for this class was generated from the following file: