libMesh
parameters.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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 // C++ includes
24 #include <typeinfo>
25 #include <string>
26 #include <map>
27 
28 // Local includes
29 #include "libmesh/libmesh_common.h"
30 #include "libmesh/reference_counted_object.h"
31 #include "libmesh/print_trace.h"
32 
33 // C++ includes
34 #include <cstddef>
35 #include <map>
36 #include <string>
37 #include <typeinfo>
38 #include <sstream>
39 
40 namespace libMesh
41 {
45 template<typename P>
46 void print_helper(std::ostream & os, const P * param);
47 
48 template<typename P>
49 void print_helper(std::ostream & os, const std::vector<P> * param);
50 
51 template<typename P>
52 void print_helper(std::ostream & os, const std::vector<std::vector<P>> * param);
53 
64 {
65 public:
66 
70  Parameters () {}
71 
75  Parameters (const Parameters &);
76 
80  virtual ~Parameters ();
81 
86  virtual Parameters & operator= (const Parameters & source);
87 
93  virtual Parameters & operator+= (const Parameters & source);
94 
102  template <typename T>
103  bool have_parameter (const std::string &) const;
104 
109  template <typename T>
110  const T & get (const std::string &) const;
111 
117  template <typename T>
118  void insert (const std::string &);
119 
126  template <typename T>
127  T & set (const std::string &);
128 
133  virtual void set_attributes(const std::string &, bool /*inserted_only*/) {}
134 
138  void remove (const std::string &);
139 
143  std::size_t n_parameters () const { return _values.size(); }
144 
145 #ifdef LIBMESH_HAVE_RTTI
146 
149  template <typename T>
150  unsigned int n_parameters () const;
151 #endif // LIBMESH_HAVE_RTTI
152 
156  virtual void clear ();
157 
161  void print (std::ostream & os=libMesh::out) const;
162 
163 private:
164 
168  class Value : public ReferenceCountedObject<Value>
169  {
170  public:
171 
175  virtual ~Value() {}
176 
177 #ifdef LIBMESH_HAVE_RTTI
178 
182  virtual std::string type () const = 0;
183 #endif // LIBMESH_HAVE_RTTI
184 
189  virtual void print(std::ostream &) const = 0;
190 
195  virtual Value * clone () const = 0;
196  };
197 
198 public:
199 
204  template <typename T>
205  class Parameter : public Value
206  {
207  public:
208 
212  const T & get () const { return _value; }
213 
217  T & set () { return _value; }
218 
219 #ifdef LIBMESH_HAVE_RTTI
220 
223  virtual std::string type () const;
224 #endif // LIBMESH_HAVE_RTTI
225 
229  virtual void print(std::ostream &) const;
230 
234  virtual Value * clone () const;
235 
236  private:
241  };
242 
246  typedef std::map<std::string, Value *>::iterator iterator;
247 
251  typedef std::map<std::string, Value *>::const_iterator const_iterator;
252 
256  iterator begin();
257 
261  const_iterator begin() const;
262 
266  iterator end();
267 
271  const_iterator end() const;
272 
273 protected:
274 
278  std::map<std::string, Value *> _values;
279 
280 };
281 
282 // ------------------------------------------------------------
283 // Parameters::Parameter<> class inline methods
284 
285 // This only works with Run-Time Type Information, even though
286 // typeid(T) *should* be determinable at compile time regardless...
287 #ifdef LIBMESH_HAVE_RTTI
288 template <typename T>
289 inline
290 std::string Parameters::Parameter<T>::type () const
291 {
292  return demangle(typeid(T).name());
293 }
294 #endif
295 
296 template <typename T>
297 inline
298 void Parameters::Parameter<T>::print (std::ostream & os) const
299 {
300  // Call helper function overloaded for basic scalar and vector types
301  print_helper(os, static_cast<const T *>(&_value));
302 }
303 
304 template <typename T>
305 inline
307 {
308  Parameter<T> * copy = new Parameter<T>;
309 
310  libmesh_assert(copy);
311 
312  copy->_value = _value;
313 
314  return copy;
315 }
316 
317 
318 // ------------------------------------------------------------
319 // Parameters class inline methods
320 inline
321 void Parameters::clear () // since this is inline we must define it
322 { // before its first use (for some compilers)
323  while (!_values.empty())
324  {
325  Parameters::iterator it = _values.begin();
326 
327  delete it->second;
328  it->second = libmesh_nullptr;
329 
330  _values.erase(it);
331  }
332 }
333 
334 
335 
336 inline
338 {
339  this->clear();
340  *this += source;
341 
342  return *this;
343 }
344 
345 inline
347 {
348  for (Parameters::const_iterator it = source._values.begin();
349  it != source._values.end(); ++it)
350  {
351  if (_values.find(it->first) != _values.end())
352  delete _values[it->first];
353  _values[it->first] = it->second->clone();
354  }
355 
356  return *this;
357 }
358 
359 inline
361 {
362  *this = p;
363 }
364 
365 
366 
367 inline
369 {
370  this->clear ();
371 }
372 
373 
374 
375 inline
376 void Parameters::print (std::ostream & os) const
377 {
378  Parameters::const_iterator it = _values.begin();
379 
380  os << "Name\t Type\t Value\n"
381  << "---------------------\n";
382  while (it != _values.end())
383  {
384  os << " " << it->first
385 #ifdef LIBMESH_HAVE_RTTI
386  << "\t " << it->second->type()
387 #endif // LIBMESH_HAVE_RTTI
388  << "\t "; it->second->print(os);
389  os << '\n';
390 
391  ++it;
392  }
393 }
394 
395 
396 
397 // Declare this now that Parameters::print() is defined.
398 // By declaring this early we can use it in subsequent
399 // methods. Required for gcc-4.0.2 -- 11/30/2005, BSK
400 inline
401 std::ostream & operator << (std::ostream & os, const Parameters & p)
402 {
403  p.print(os);
404  return os;
405 }
406 
407 
408 
409 template <typename T>
410 inline
411 bool Parameters::have_parameter (const std::string & name) const
412 {
413  Parameters::const_iterator it = _values.find(name);
414 
415  if (it != _values.end())
416 #ifdef LIBMESH_HAVE_RTTI
417  if (dynamic_cast<const Parameter<T> *>(it->second) != libmesh_nullptr)
418 #else // LIBMESH_HAVE_RTTI
419  if (cast_ptr<const Parameter<T> *>(it->second) != libmesh_nullptr)
420 #endif // LIBMESH_HAVE_RTTI
421  return true;
422 
423  return false;
424 }
425 
426 
427 
428 template <typename T>
429 inline
430 const T & Parameters::get (const std::string & name) const
431 {
432  if (!this->have_parameter<T>(name))
433  {
434  std::ostringstream oss;
435 
436  oss << "ERROR: no";
437 #ifdef LIBMESH_HAVE_RTTI
438  oss << ' ' << demangle(typeid(T).name());
439 #endif
440  oss << " parameter named \""
441  << name << "\" found.\n\n"
442  << "Known parameters:\n"
443  << *this;
444 
445  libmesh_error_msg(oss.str());
446  }
447 
448  Parameters::const_iterator it = _values.find(name);
449 
450  libmesh_assert(it != _values.end());
451  libmesh_assert(it->second);
452 
453  return cast_ptr<Parameter<T> *>(it->second)->get();
454 }
455 
456 template <typename T>
457 inline
458 void Parameters::insert (const std::string & name)
459 {
460  if (!this->have_parameter<T>(name))
461  _values[name] = new Parameter<T>;
462 
463  set_attributes(name, true);
464 }
465 
466 
467 template <typename T>
468 inline
469 T & Parameters::set (const std::string & name)
470 {
471  if (!this->have_parameter<T>(name))
472  _values[name] = new Parameter<T>;
473 
474  set_attributes(name, false);
475 
476  return cast_ptr<Parameter<T> *>(_values[name])->set();
477 }
478 
479 inline
480 void Parameters::remove (const std::string & name)
481 {
482  Parameters::iterator it = _values.find(name);
483 
484  if (it != _values.end())
485  {
486  delete it->second;
487  it->second = libmesh_nullptr;
488 
489  _values.erase(it);
490  }
491 }
492 
493 
494 
495 #ifdef LIBMESH_HAVE_RTTI
496 template <typename T>
497 inline
498 unsigned int Parameters::n_parameters () const
499 {
500  unsigned int cnt = 0;
501 
502  Parameters::const_iterator it = _values.begin();
503  const Parameters::const_iterator vals_end = _values.end();
504 
505  for (; it != vals_end; ++it)
506  if (dynamic_cast<Parameter<T> *>(it->second) != libmesh_nullptr)
507  cnt++;
508 
509  return cnt;
510 }
511 #endif
512 
513 inline
515 {
516  return _values.begin();
517 }
518 
519 inline
521 {
522  return _values.begin();
523 }
524 
525 inline
527 {
528  return _values.end();
529 }
530 
531 inline
533 {
534  return _values.end();
535 }
536 
537 //non-member scalar print function
538 template<typename P>
539 void print_helper(std::ostream & os, const P * param)
540 {
541  os << *param;
542 }
543 
544 template<>
545 inline
546 void print_helper(std::ostream & os, const char * param)
547 {
548  // Specialization so that we don't print out unprintable characters
549  os << static_cast<int>(*param);
550 }
551 
552 template<>
553 inline
554 void print_helper(std::ostream & os, const unsigned char * param)
555 {
556  // Specialization so that we don't print out unprintable characters
557  os << static_cast<int>(*param);
558 }
559 
560 //non-member vector print function
561 template<typename P>
562 void print_helper(std::ostream & os, const std::vector<P> * param)
563 {
564  for (std::size_t i=0; i<param->size(); ++i)
565  os << (*param)[i] << " ";
566 }
567 
568 //non-member vector<vector> print function
569 template<typename P>
570 void print_helper(std::ostream & os, const std::vector<std::vector<P>> * param)
571 {
572  for (std::size_t i=0; i<param->size(); ++i)
573  for (std::size_t j=0; j<(*param)[i].size(); ++j)
574  os << (*param)[i][j] << " ";
575 }
576 
577 } // namespace libMesh
578 
579 #endif // LIBMESH_PARAMETERS_H
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:39
std::size_t n_parameters() const
Definition: parameters.h:143
std::map< std::string, Value * >::const_iterator const_iterator
Constant parameter map iterator.
Definition: parameters.h:251
virtual ~Parameters()
Destructor.
Definition: parameters.h:368
Parameters()
Default constructor.
Definition: parameters.h:70
virtual Value * clone() const =0
Clone this value.
void print_helper(std::ostream &os, const P *param)
Helper functions for printing scalar, vector and vector<vector> types.
Definition: parameters.h:539
std::map< std::string, Value * >::iterator iterator
Parameter map iterator.
Definition: parameters.h:246
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition: parameters.h:63
virtual ~Value()
Destructor.
Definition: parameters.h:175
Tnew cast_ptr(Told *oldvar)
const class libmesh_nullptr_t libmesh_nullptr
std::ostream & operator<<(std::ostream &os, const OrderWrapper &order)
Overload stream operators.
Definition: fe_type.h:164
const T & get(const std::string &) const
Definition: parameters.h:430
virtual std::string type() const =0
String identifying the type of parameter stored.
virtual void print(std::ostream &) const
Prints the parameter value to the specified stream.
Definition: parameters.h:298
The libMesh namespace provides an interface to certain functionality in the library.
virtual Parameters & operator+=(const Parameters &source)
Addition/Assignment operator.
Definition: parameters.h:346
libmesh_assert(j)
Concrete definition of a parameter value for a specified type.
Definition: parameters.h:205
virtual Parameters & operator=(const Parameters &source)
Assignment operator.
Definition: parameters.h:337
iterator end()
Iterator pointing to the end of the set of parameters.
Definition: parameters.h:526
virtual std::string type() const
String identifying the type of parameter stored.
Definition: parameters.h:290
Abstract definition of a parameter value.
Definition: parameters.h:168
virtual void print(std::ostream &) const =0
Prints the parameter value to the specified stream.
void remove(const std::string &)
Removes the specified parameter from the list, if it exists.
Definition: parameters.h:480
iterator begin()
Iterator pointing to the beginning of the set of parameters.
Definition: parameters.h:514
This class implements reference counting.
std::string demangle(const char *name)
Mostly system independent demangler.
Definition: print_trace.C:250
T _value
Stored parameter value.
Definition: parameters.h:240
std::map< std::string, Value * > _values
Data structure to map names with values.
Definition: parameters.h:278
void print(std::ostream &os=libMesh::out) const
Prints the contents, by default to libMesh::out.
Definition: parameters.h:376
T & set(const std::string &)
Definition: parameters.h:469
OStreamProxy out
virtual void clear()
Clears internal data structures & frees any allocated memory.
Definition: parameters.h:321
bool have_parameter(const std::string &) const
Definition: parameters.h:411
void insert(const std::string &)
Inserts a new Parameter into the object but does not return a writable reference. ...
Definition: parameters.h:458
virtual Value * clone() const
Clone this value.
Definition: parameters.h:306
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