parameters.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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"
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 
166  class Value : public ReferenceCountedObject<Value>
167  {
168  public:
169 
173  virtual ~Value() {}
174 
175 #ifdef LIBMESH_HAVE_RTTI
176 
180  virtual std::string type () const = 0;
181 #endif // LIBMESH_HAVE_RTTI
182 
187  virtual void print(std::ostream &) const = 0;
188 
193  virtual Value * clone () const = 0;
194  };
195 
200  template <typename T>
201  class Parameter : public Value
202  {
203  public:
204 
208  const T & get () const { return _value; }
209 
213  T & set () { return _value; }
214 
215 #ifdef LIBMESH_HAVE_RTTI
216 
219  virtual std::string type () const;
220 #endif // LIBMESH_HAVE_RTTI
221 
225  virtual void print(std::ostream &) const;
226 
230  virtual Value * clone () const;
231 
232  private:
237  };
238 
242  typedef std::map<std::string, Value *>::iterator iterator;
243 
247  typedef std::map<std::string, Value *>::const_iterator const_iterator;
248 
252  iterator begin();
253 
257  const_iterator begin() const;
258 
262  iterator end();
263 
267  const_iterator end() const;
268 
269 protected:
270 
274  std::map<std::string, Value *> _values;
275 
276 };
277 
278 // ------------------------------------------------------------
279 // Parameters::Parameter<> class inline methods
280 
281 // This only works with Run-Time Type Information, even though
282 // typeid(T) *should* be determinable at compile time regardless...
283 #ifdef LIBMESH_HAVE_RTTI
284 template <typename T>
285 inline
286 std::string Parameters::Parameter<T>::type () const
287 {
288  return demangle(typeid(T).name());
289 }
290 #endif
291 
292 template <typename T>
293 inline
294 void Parameters::Parameter<T>::print (std::ostream & os) const
295 {
296  // Call helper function overloaded for basic scalar and vector types
297  print_helper(os, static_cast<const T *>(&_value));
298 }
299 
300 template <typename T>
301 inline
303 {
304  Parameter<T> * copy = new Parameter<T>;
305 
306  libmesh_assert(copy);
307 
308  copy->_value = _value;
309 
310  return copy;
311 }
312 
313 
314 // ------------------------------------------------------------
315 // Parameters class inline methods
316 inline
317 void Parameters::clear () // since this is inline we must define it
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 }
329 
330 
331 
332 inline
334 {
335  this->clear();
336  *this += source;
337 
338  return *this;
339 }
340 
341 inline
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 }
353 
354 inline
356 {
357  *this = p;
358 }
359 
360 
361 
362 inline
364 {
365  this->clear ();
366 }
367 
368 
369 
370 inline
371 void Parameters::print (std::ostream & os) const
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 }
389 
390 
391 
392 // Declare this now that Parameters::print() is defined.
393 // By declaring this early we can use it in subsequent
394 // methods. Required for gcc-4.0.2 -- 11/30/2005, BSK
395 inline
396 std::ostream & operator << (std::ostream & os, const Parameters & p)
397 {
398  p.print(os);
399  return os;
400 }
401 
402 
403 
404 template <typename T>
405 inline
406 bool Parameters::have_parameter (const std::string & name) const
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 }
420 
421 
422 
423 template <typename T>
424 inline
425 const T & Parameters::get (const std::string & name) const
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 }
450 
451 template <typename T>
452 inline
453 void Parameters::insert (const std::string & name)
454 {
455  if (!this->have_parameter<T>(name))
456  _values[name] = new Parameter<T>;
457 
458  set_attributes(name, true);
459 }
460 
461 
462 template <typename T>
463 inline
464 T & Parameters::set (const std::string & name)
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 }
473 
474 inline
475 void Parameters::remove (const std::string & 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 }
487 
488 
489 
490 #ifdef LIBMESH_HAVE_RTTI
491 template <typename T>
492 inline
493 unsigned int Parameters::n_parameters () const
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 }
506 #endif
507 
508 inline
510 {
511  return _values.begin();
512 }
513 
514 inline
516 {
517  return _values.begin();
518 }
519 
520 inline
522 {
523  return _values.end();
524 }
525 
526 inline
528 {
529  return _values.end();
530 }
531 
532 //non-member scalar print function
533 template<typename P>
534 void print_helper(std::ostream & os, const P * param)
535 {
536  os << *param;
537 }
538 
539 template<>
540 inline
541 void print_helper(std::ostream & os, const char * param)
542 {
543  // Specialization so that we don't print out unprintable characters
544  os << static_cast<int>(*param);
545 }
546 
547 template<>
548 inline
549 void print_helper(std::ostream & os, const unsigned char * param)
550 {
551  // Specialization so that we don't print out unprintable characters
552  os << static_cast<int>(*param);
553 }
554 
555 //non-member vector print function
556 template<typename P>
557 void print_helper(std::ostream & os, const std::vector<P> * param)
558 {
559  for (std::size_t i=0; i<param->size(); ++i)
560  os << (*param)[i] << " ";
561 }
562 
563 //non-member vector<vector> print function
564 template<typename P>
565 void print_helper(std::ostream & os, const std::vector<std::vector<P>> * param)
566 {
567  for (std::size_t i=0; i<param->size(); ++i)
568  for (std::size_t j=0; j<(*param)[i].size(); ++j)
569  os << (*param)[i][j] << " ";
570 }
571 
572 } // namespace libMesh
573 
574 #endif // LIBMESH_PARAMETERS_H
std::string name(const ElemQuality q)
Definition: elem_quality.C:42
std::map< std::string, Value * >::const_iterator const_iterator
Definition: parameters.h:247
virtual ~Parameters()
Definition: parameters.h:363
virtual void print(std::ostream &) const =0
virtual std::string type() const =0
void print_helper(std::ostream &os, const P *param)
Definition: parameters.h:534
std::map< std::string, Value * >::iterator iterator
Definition: parameters.h:242
Tnew cast_ptr(Told *oldvar)
void print(std::ostream &os=libMesh::out) const
Definition: parameters.h:371
virtual Parameters & operator+=(const Parameters &source)
Definition: parameters.h:342
virtual Parameters & operator=(const Parameters &source)
Definition: parameters.h:333
void remove(const std::string &)
Definition: parameters.h:475
iterator begin()
Definition: parameters.h:509
std::string demangle(const char *name)
Definition: print_trace.C:250
virtual Value * clone() const
Definition: parameters.h:302
std::map< std::string, Value * > _values
Definition: parameters.h:274
T & set(const std::string &)
Definition: parameters.h:464
virtual void clear()
Definition: parameters.h:317
const T & get(const std::string &) const
Definition: parameters.h:425
void insert(const std::string &)
Definition: parameters.h:453
virtual Value * clone() const =0
std::size_t n_parameters() const
Definition: parameters.h:143
virtual void print(std::ostream &) const
Definition: parameters.h:294
bool have_parameter(const std::string &) const
Definition: parameters.h:406
OStreamProxy out(std::cout)
virtual std::string type() const
Definition: parameters.h:286
std::ostream & operator<<(std::ostream &os, const FEAbstract &fe)
Definition: fe_abstract.C:809
virtual void set_attributes(const std::string &, bool)
Definition: parameters.h:133