libMesh
parameters.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_PARAMETERS_H
21 #define LIBMESH_PARAMETERS_H
22 
23 // Local includes
24 #include "libmesh/libmesh_common.h"
25 #include "libmesh/reference_counted_object.h"
26 #include "libmesh/print_trace.h"
27 
28 // C++ includes
29 #include <cstddef>
30 #include <map>
31 #include <sstream>
32 #include <string>
33 #include <typeinfo>
34 #include <vector>
35 
36 namespace libMesh
37 {
41 template<typename P>
42 void print_helper(std::ostream & os, const P * param);
43 
44 template<typename P>
45 void print_helper(std::ostream & os, const std::vector<P> * param);
46 
47 template<typename P>
48 void print_helper(std::ostream & os, const std::vector<std::vector<P>> * param);
49 
60 {
61 public:
62 
66  Parameters () {}
67 
71  Parameters (const Parameters &);
72 
76  virtual ~Parameters ();
77 
82  virtual Parameters & operator= (const Parameters & source);
83 
89  virtual Parameters & operator+= (const Parameters & source);
90 
98  template <typename T>
99  bool have_parameter (const std::string &) const;
100 
105  template <typename T>
106  const T & get (const std::string &) const;
107 
113  template <typename T>
114  void insert (const std::string &);
115 
122  template <typename T>
123  T & set (const std::string &);
124 
129  virtual void set_attributes(const std::string &, bool /*inserted_only*/) {}
130 
134  void remove (const std::string &);
135 
139  std::size_t n_parameters () const { return _values.size(); }
140 
141 #ifdef LIBMESH_HAVE_RTTI
142 
145  template <typename T>
146  unsigned int n_parameters () const;
147 #endif // LIBMESH_HAVE_RTTI
148 
152  virtual void clear ();
153 
157  void print (std::ostream & os=libMesh::out) const;
158 
162  class Value : public ReferenceCountedObject<Value>
163  {
164  public:
165 
169  virtual ~Value() {}
170 
171 #ifdef LIBMESH_HAVE_RTTI
172 
176  virtual std::string type () const = 0;
177 #endif // LIBMESH_HAVE_RTTI
178 
183  virtual void print(std::ostream &) const = 0;
184 
189  virtual Value * clone () const = 0;
190  };
191 
196  template <typename T>
197  class Parameter : public Value
198  {
199  public:
200 
204  const T & get () const { return _value; }
205 
209  T & set () { return _value; }
210 
211 #ifdef LIBMESH_HAVE_RTTI
212 
215  virtual std::string type () const;
216 #endif // LIBMESH_HAVE_RTTI
217 
221  virtual void print(std::ostream &) const;
222 
226  virtual Value * clone () const;
227 
228  private:
233  };
234 
238  typedef std::map<std::string, Value *>::iterator iterator;
239 
243  typedef std::map<std::string, Value *>::const_iterator const_iterator;
244 
248  iterator begin();
249 
253  const_iterator begin() const;
254 
258  iterator end();
259 
263  const_iterator end() const;
264 
265 protected:
266 
270  std::map<std::string, Value *> _values;
271 
272 };
273 
274 // ------------------------------------------------------------
275 // Parameters::Parameter<> class inline methods
276 
277 // This only works with Run-Time Type Information, even though
278 // typeid(T) *should* be determinable at compile time regardless...
279 #ifdef LIBMESH_HAVE_RTTI
280 template <typename T>
281 inline
282 std::string Parameters::Parameter<T>::type () const
283 {
284  return demangle(typeid(T).name());
285 }
286 #endif
287 
288 template <typename T>
289 inline
290 void Parameters::Parameter<T>::print (std::ostream & os) const
291 {
292  // Call helper function overloaded for basic scalar and vector types
293  print_helper(os, static_cast<const T *>(&_value));
294 }
295 
296 template <typename T>
297 inline
299 {
300  Parameter<T> * copy = new Parameter<T>;
301 
302  libmesh_assert(copy);
303 
304  copy->_value = _value;
305 
306  return copy;
307 }
308 
309 
310 // ------------------------------------------------------------
311 // Parameters class inline methods
312 inline
313 void Parameters::clear () // since this is inline we must define it
314 { // before its first use (for some compilers)
315  while (!_values.empty())
316  {
317  Parameters::iterator it = _values.begin();
318 
319  delete it->second;
320  it->second = nullptr;
321 
322  _values.erase(it);
323  }
324 }
325 
326 
327 
328 inline
330 {
331  this->clear();
332  *this += source;
333 
334  return *this;
335 }
336 
337 inline
339 {
340  for (const auto & pr : source._values)
341  {
342  if (_values.find(pr.first) != _values.end())
343  delete _values[pr.first];
344  _values[pr.first] = pr.second->clone();
345  }
346 
347  return *this;
348 }
349 
350 inline
352 {
353  *this = p;
354 }
355 
356 
357 
358 inline
360 {
361  this->clear ();
362 }
363 
364 
365 
366 inline
367 void Parameters::print (std::ostream & os) const
368 {
369  Parameters::const_iterator it = _values.begin();
370 
371  os << "Name\t Type\t Value\n"
372  << "---------------------\n";
373  while (it != _values.end())
374  {
375  os << " " << it->first
376 #ifdef LIBMESH_HAVE_RTTI
377  << "\t " << it->second->type()
378 #endif // LIBMESH_HAVE_RTTI
379  << "\t "; it->second->print(os);
380  os << '\n';
381 
382  ++it;
383  }
384 }
385 
386 
387 
388 // Declare this now that Parameters::print() is defined.
389 // By declaring this early we can use it in subsequent
390 // methods. Required for gcc-4.0.2 -- 11/30/2005, BSK
391 inline
392 std::ostream & operator << (std::ostream & os, const Parameters & p)
393 {
394  p.print(os);
395  return os;
396 }
397 
398 
399 
400 template <typename T>
401 inline
402 bool Parameters::have_parameter (const std::string & name) const
403 {
405 
406  if (it != _values.end())
407 #ifdef LIBMESH_HAVE_RTTI
408  if (dynamic_cast<const Parameter<T> *>(it->second) != nullptr)
409 #else // LIBMESH_HAVE_RTTI
410  if (cast_ptr<const Parameter<T> *>(it->second) != nullptr)
411 #endif // LIBMESH_HAVE_RTTI
412  return true;
413 
414  return false;
415 }
416 
417 
418 
419 template <typename T>
420 inline
421 const T & Parameters::get (const std::string & name) const
422 {
423  if (!this->have_parameter<T>(name))
424  {
425  std::ostringstream oss;
426 
427  oss << "ERROR: no";
428 #ifdef LIBMESH_HAVE_RTTI
429  oss << ' ' << demangle(typeid(T).name());
430 #endif
431  oss << " parameter named \""
432  << name << "\" found.\n\n"
433  << "Known parameters:\n"
434  << *this;
435 
436  libmesh_error_msg(oss.str());
437  }
438 
440 
441  libmesh_assert(it != _values.end());
442  libmesh_assert(it->second);
443 
444  return cast_ptr<Parameter<T> *>(it->second)->get();
445 }
446 
447 template <typename T>
448 inline
449 void Parameters::insert (const std::string & name)
450 {
451  if (!this->have_parameter<T>(name))
452  _values[name] = new Parameter<T>;
453 
454  set_attributes(name, true);
455 }
456 
457 
458 template <typename T>
459 inline
460 T & Parameters::set (const std::string & name)
461 {
462  if (!this->have_parameter<T>(name))
463  _values[name] = new Parameter<T>;
464 
465  set_attributes(name, false);
466 
467  return cast_ptr<Parameter<T> *>(_values[name])->set();
468 }
469 
470 inline
471 void Parameters::remove (const std::string & name)
472 {
473  Parameters::iterator it = _values.find(name);
474 
475  if (it != _values.end())
476  {
477  delete it->second;
478  it->second = nullptr;
479 
480  _values.erase(it);
481  }
482 }
483 
484 
485 
486 #ifdef LIBMESH_HAVE_RTTI
487 template <typename T>
488 inline
489 unsigned int Parameters::n_parameters () const
490 {
491  unsigned int cnt = 0;
492 
493  Parameters::const_iterator it = _values.begin();
494  const Parameters::const_iterator vals_end = _values.end();
495 
496  for (; it != vals_end; ++it)
497  if (dynamic_cast<Parameter<T> *>(it->second) != nullptr)
498  cnt++;
499 
500  return cnt;
501 }
502 #endif
503 
504 inline
506 {
507  return _values.begin();
508 }
509 
510 inline
512 {
513  return _values.begin();
514 }
515 
516 inline
518 {
519  return _values.end();
520 }
521 
522 inline
524 {
525  return _values.end();
526 }
527 
528 //non-member scalar print function
529 template<typename P>
530 void print_helper(std::ostream & os, const P * param)
531 {
532  os << *param;
533 }
534 
535 template<>
536 inline
537 void print_helper(std::ostream & os, const char * param)
538 {
539  // Specialization so that we don't print out unprintable characters
540  os << static_cast<int>(*param);
541 }
542 
543 template<>
544 inline
545 void print_helper(std::ostream & os, const unsigned char * param)
546 {
547  // Specialization so that we don't print out unprintable characters
548  os << static_cast<int>(*param);
549 }
550 
551 //non-member vector print function
552 template<typename P>
553 void print_helper(std::ostream & os, const std::vector<P> * param)
554 {
555  for (const auto & p : *param)
556  os << p << " ";
557 }
558 
559 //non-member vector<vector> print function
560 template<typename P>
561 void print_helper(std::ostream & os, const std::vector<std::vector<P>> * param)
562 {
563  for (const auto & pv : *param)
564  for (const auto & p : pv)
565  os << p << " ";
566 }
567 
568 } // namespace libMesh
569 
570 #endif // LIBMESH_PARAMETERS_H
libMesh::Parameters::Parameter
Concrete definition of a parameter value for a specified type.
Definition: parameters.h:197
libMesh::Parameters::Value::print
virtual void print(std::ostream &) const =0
Prints the parameter value to the specified stream.
libMesh::Parameters::clear
virtual void clear()
Clears internal data structures & frees any allocated memory.
Definition: parameters.h:313
libMesh::Parameters::remove
void remove(const std::string &)
Removes the specified parameter from the list, if it exists.
Definition: parameters.h:471
libMesh::Parameters::print
void print(std::ostream &os=libMesh::out) const
Prints the contents, by default to libMesh::out.
Definition: parameters.h:367
libMesh::Parameters::Parameter::clone
virtual Value * clone() const
Clone this value.
Definition: parameters.h:298
libMesh::Parameters::begin
iterator begin()
Iterator pointing to the beginning of the set of parameters.
Definition: parameters.h:505
libMesh::Parameters::Parameter::_value
T _value
Stored parameter value.
Definition: parameters.h:232
libMesh::print_helper
void print_helper(std::ostream &os, const P *param)
Helper functions for printing scalar, vector and vector<vector> types.
Definition: parameters.h:530
libMesh::ReferenceCountedObject
This class implements reference counting.
Definition: reference_counted_object.h:65
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::Parameters::operator+=
virtual Parameters & operator+=(const Parameters &source)
Addition/Assignment operator.
Definition: parameters.h:338
libMesh::operator<<
std::ostream & operator<<(std::ostream &os, const OrderWrapper &order)
Overload stream operators.
Definition: fe_type.h:164
libMesh::cast_ptr
Tnew cast_ptr(Told *oldvar)
Definition: libmesh_common.h:573
libMesh::Parameters::Value::type
virtual std::string type() const =0
String identifying the type of parameter stored.
libMesh::Parameters::Parameter::type
virtual std::string type() const
String identifying the type of parameter stored.
Definition: parameters.h:282
libMesh::Parameters::Parameter::print
virtual void print(std::ostream &) const
Prints the parameter value to the specified stream.
Definition: parameters.h:290
libMesh::Parameters::have_parameter
bool have_parameter(const std::string &) const
Definition: parameters.h:402
libMesh::libmesh_assert
libmesh_assert(ctx)
libMesh::Parameters::end
iterator end()
Iterator pointing to the end of the set of parameters.
Definition: parameters.h:517
libMesh::Parameters::operator=
virtual Parameters & operator=(const Parameters &source)
Assignment operator.
Definition: parameters.h:329
libMesh::Parameters::n_parameters
std::size_t n_parameters() const
Definition: parameters.h:139
libMesh::demangle
std::string demangle(const char *name)
Mostly system independent demangler.
Definition: print_trace.C:250
libMesh::Parameters::Parameters
Parameters()
Default constructor.
Definition: parameters.h:66
libMesh::Parameters::Value
Abstract definition of a parameter value.
Definition: parameters.h:162
libMesh::Parameters::iterator
std::map< std::string, Value * >::iterator iterator
Parameter map iterator.
Definition: parameters.h:238
libMesh::Parameters::set
T & set(const std::string &)
Definition: parameters.h:460
libMesh::Parameters::set_attributes
virtual void set_attributes(const std::string &, bool)
Overridable function to set any extended attributes for classes inheriting from this class.
Definition: parameters.h:129
libMesh::Parameters::Parameter::get
const T & get() const
Definition: parameters.h:204
libMesh::Parameters::_values
std::map< std::string, Value * > _values
Data structure to map names with values.
Definition: parameters.h:270
libMesh::Parameters::Parameter::set
T & set()
Definition: parameters.h:209
libMesh::Parameters::insert
void insert(const std::string &)
Inserts a new Parameter into the object but does not return a writable reference.
Definition: parameters.h:449
libMesh::Parameters::get
const T & get(const std::string &) const
Definition: parameters.h:421
libMesh::out
OStreamProxy out
libMesh::Parameters::Value::~Value
virtual ~Value()
Destructor.
Definition: parameters.h:169
libMesh::Parameters::const_iterator
std::map< std::string, Value * >::const_iterator const_iterator
Constant parameter map iterator.
Definition: parameters.h:243
libMesh::Parameters::~Parameters
virtual ~Parameters()
Destructor.
Definition: parameters.h:359
libMesh::Parameters
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition: parameters.h:59
libMesh::Quality::name
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
libMesh::Parameters::Value::clone
virtual Value * clone() const =0
Clone this value.