libMesh
parameters.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 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 #include <memory>
36 
37 namespace libMesh
38 {
43 template<typename P>
44 void print_helper(std::ostream & os, const P * param);
45 
46 template<typename P>
47 void print_helper(std::ostream & os, const std::vector<P> * param);
48 
49 template<typename P>
50 void print_helper(std::ostream & os, const std::vector<std::vector<P>> * param);
51 
52 template<typename P>
53 void print_helper(std::ostream & os, const std::vector<std::vector<std::vector<P>>> * param);
54 
55 template<typename P1, typename P2, typename C, typename A>
56 void print_helper(std::ostream & os, const std::map<P1, P2, C, A> * param);
57 
68 {
69 public:
70 
74  Parameters () = default;
75 
80  Parameters (const Parameters &);
81 
85  virtual ~Parameters () = default;
86 
91  virtual Parameters & operator= (const Parameters & source);
92 
98  virtual Parameters & operator+= (const Parameters & source);
99 
107  template <typename T>
108  bool have_parameter (std::string_view) const;
109 
114  template <typename T>
115  const T & get (std::string_view) const;
116 
122  template <typename T>
123  void insert (const std::string &);
124 
131  template <typename T>
132  T & set (const std::string &);
133 
138  virtual void set_attributes(const std::string &, bool /*inserted_only*/) {}
139 
143  void remove (std::string_view);
144 
148  std::size_t n_parameters () const { return _values.size(); }
149 
150 #ifdef LIBMESH_HAVE_RTTI
151 
154  template <typename T>
155  unsigned int n_parameters () const;
156 #endif // LIBMESH_HAVE_RTTI
157 
161  virtual void clear ();
162 
166  void print (std::ostream & os=libMesh::out) const;
167 
171  class Value : public ReferenceCountedObject<Value>
172  {
173  public:
174 
178  virtual ~Value() = default;
179 
180 #ifdef LIBMESH_HAVE_RTTI
181 
185  virtual std::string type () const = 0;
186 #endif // LIBMESH_HAVE_RTTI
187 
192  virtual void print(std::ostream &) const = 0;
193 
198  virtual std::unique_ptr<Value> clone () const = 0;
199  };
200 
205  template <typename T>
206  class Parameter : public Value
207  {
208  public:
209 
213  const T & get () const { return _value; }
214 
218  T & set () { return _value; }
219 
220 #ifdef LIBMESH_HAVE_RTTI
221 
224  virtual std::string type () const override;
225 #endif // LIBMESH_HAVE_RTTI
226 
230  virtual void print(std::ostream &) const override;
231 
235  virtual std::unique_ptr<Value> clone () const override;
236 
237  private:
242  };
243 
247  typedef std::map<std::string, std::unique_ptr<Value>, std::less<>> map_type;
248 
252  typedef map_type::iterator iterator;
253 
257  typedef map_type::const_iterator const_iterator;
258 
262  iterator begin();
263 
267  const_iterator begin() const;
268 
272  iterator end();
273 
277  const_iterator end() const;
278 
279 protected:
280 
285 };
286 
287 // ------------------------------------------------------------
288 // Parameters::Parameter<> class inline methods
289 
290 // This only works with Run-Time Type Information, even though
291 // typeid(T) *should* be determinable at compile time regardless...
292 #ifdef LIBMESH_HAVE_RTTI
293 template <typename T>
294 inline
295 std::string Parameters::Parameter<T>::type () const
296 {
297  return demangle(typeid(T).name());
298 }
299 #endif
300 
301 template <typename T>
302 inline
303 void Parameters::Parameter<T>::print (std::ostream & os) const
304 {
305  // Call helper function overloaded for basic scalar and vector types
306  print_helper(os, static_cast<const T *>(&_value));
307 }
308 
309 template <typename T>
310 inline
311 std::unique_ptr<Parameters::Value> Parameters::Parameter<T>::clone () const
312 {
313  auto copy = std::make_unique<Parameter<T>>();
314 
315  copy->_value = this->_value; // assign value
316 
317  return copy; // as unique_ptr to base class
318 }
319 
320 
321 // ------------------------------------------------------------
322 // Parameters class inline methods
323 inline
325 {
326  _values.clear();
327 }
328 
329 
330 
331 inline
333 {
334  this->Parameters::clear();
335  *this += source;
336 
337  return *this;
338 }
339 
340 inline
342 {
343  // Overwrite each value (if it exists) or create a new entry
344  for (const auto & [key, value] : source._values)
345  _values[key] = value->clone();
346 
347  return *this;
348 }
349 
350 inline
352 {
353  // calls assignment operator
354  *this = p;
355 }
356 
357 
358 
359 inline
360 void Parameters::print (std::ostream & os) const
361 {
362  Parameters::const_iterator it = _values.begin();
363 
364  os << "Name\t Type\t Value\n"
365  << "---------------------\n";
366  while (it != _values.end())
367  {
368  os << " " << it->first
369 #ifdef LIBMESH_HAVE_RTTI
370  << "\t " << it->second->type()
371 #endif // LIBMESH_HAVE_RTTI
372  << "\t "; it->second->print(os);
373  os << '\n';
374 
375  ++it;
376  }
377 }
378 
379 
380 
381 // Declare this now that Parameters::print() is defined.
382 // By declaring this early we can use it in subsequent
383 // methods. Required for gcc-4.0.2 -- 11/30/2005, BSK
384 inline
385 std::ostream & operator << (std::ostream & os, const Parameters & p)
386 {
387  p.print(os);
388  return os;
389 }
390 
391 
392 
393 template <typename T>
394 inline
395 bool Parameters::have_parameter (std::string_view name) const
396 {
398 
399  if (it != _values.end())
400  {
401 #ifdef LIBMESH_HAVE_RTTI
402 
403  if (dynamic_cast<const Parameter<T> *>(it->second.get()))
404  return true;
405 
406 #else // !LIBMESH_HAVE_RTTI
407 
408  // cast_ptr will simply do a static_cast here when RTTI is not
409  // enabled, and it will return a non-nullptr regardless of
410  // whether or not the cast actually succeeds.
411  libmesh_warning("Parameters::have_parameter() may return false positives when RTTI is not enabled.");
412 
413  if (cast_ptr<const Parameter<T> *>(it->second.get()))
414  return true;
415 
416 #endif
417  }
418 
419  return false;
420 }
421 
422 
423 
424 template <typename T>
425 inline
426 const T & Parameters::get (std::string_view name) const
427 {
428  if (!this->have_parameter<T>(name))
429  {
430  std::ostringstream oss;
431 
432  oss << "ERROR: no";
433 #ifdef LIBMESH_HAVE_RTTI
434  oss << ' ' << demangle(typeid(T).name());
435 #endif
436  oss << " parameter named \""
437  << name << "\" found.\n\n"
438  << "Known parameters:\n"
439  << *this;
440 
441  libmesh_error_msg(oss.str());
442  }
443 
445 
446  libmesh_assert(it != _values.end());
447  libmesh_assert(it->second);
448 
449  // Get pointer to derived type
450  auto ptr = cast_ptr<Parameter<T> *>(it->second.get());
451 
452  // Return const reference
453  return ptr->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] = std::make_unique<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] = std::make_unique<Parameter<T>>();
473 
474  set_attributes(name, false);
475 
476  // Get pointer to existing or just-added entry
477  auto ptr = cast_ptr<Parameter<T> *>(_values[name].get());
478 
479  // Return writeable reference
480  return ptr->set();
481 }
482 
483 inline
484 void Parameters::remove (std::string_view name)
485 {
486  Parameters::iterator it = _values.find(name);
487 
488  if (it != _values.end())
489  _values.erase(it);
490 }
491 
492 
493 
494 #ifdef LIBMESH_HAVE_RTTI
495 template <typename T>
496 inline
497 unsigned int Parameters::n_parameters () const
498 {
499  unsigned int cnt = 0;
500 
501  for (const auto & pr : _values)
502  if (dynamic_cast<Parameter<T> *>(pr.second.get()))
503  cnt++;
504 
505  return cnt;
506 }
507 #endif
508 
509 inline
511 {
512  return _values.begin();
513 }
514 
515 inline
517 {
518  return _values.begin();
519 }
520 
521 inline
523 {
524  return _values.end();
525 }
526 
527 inline
529 {
530  return _values.end();
531 }
532 
533 //non-member scalar print function
534 template<typename P>
535 void print_helper(std::ostream & os, const P * param)
536 {
537  os << *param;
538 }
539 
540 template<>
541 inline
542 void print_helper(std::ostream & os, const char * param)
543 {
544  // Specialization so that we don't print out unprintable characters
545  os << static_cast<int>(*param);
546 }
547 
548 template<>
549 inline
550 void print_helper(std::ostream & os, const unsigned char * param)
551 {
552  // Specialization so that we don't print out unprintable characters
553  os << static_cast<int>(*param);
554 }
555 
556 //non-member vector print function
557 template<typename P>
558 void print_helper(std::ostream & os, const std::vector<P> * param)
559 {
560  for (const auto & p : *param)
561  os << p << " ";
562 }
563 
564 //non-member vector<vector> print function
565 template<typename P>
566 void print_helper(std::ostream & os, const std::vector<std::vector<P>> * param)
567 {
568  for (const auto & pv : *param)
569  for (const auto & p : pv)
570  os << p << " ";
571 }
572 
573 //non-member vector<vector<vector>> print function
574 template<typename P>
575 void print_helper(std::ostream & os, const std::vector<std::vector<std::vector<P>>> * param)
576 {
577  for (const auto & pvv : *param)
578  for (const auto & pv : pvv)
579  for (const auto & p : pv)
580  os << p << " ";
581 }
582 
583 //non-member map print function
584 template<typename P1, typename P2, typename C, typename A>
585 void print_helper(std::ostream & os, const std::map<P1, P2, C, A> * param)
586 {
587  os << '{';
588  std::size_t sz = param->size();
589  for (auto KV : *param)
590  {
591  os << '\'' << KV.first << "\' => \'" << KV.second << '\'';
592  if (--sz)
593  os << ", ";
594  }
595  os << '}';
596 }
597 
598 
599 } // namespace libMesh
600 
601 #endif // LIBMESH_PARAMETERS_H
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
bool have_parameter(std::string_view) const
Definition: parameters.h:395
virtual void print(std::ostream &) const =0
Prints the parameter value to the specified stream.
virtual ~Parameters()=default
Destructor.
virtual std::string type() const =0
String identifying the type of parameter stored.
void print_helper(std::ostream &os, const P *param)
Helper functions for printing scalar, vector, vector<vector> and vector<vector<vector>> types...
Definition: parameters.h:535
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition: parameters.h:67
map_type _values
Data structure to map names with values.
Definition: parameters.h:284
Tnew cast_ptr(Told *oldvar)
void print(std::ostream &os=libMesh::out) const
Prints the contents, by default to libMesh::out.
Definition: parameters.h:360
virtual std::unique_ptr< Value > clone() const override
Clone this value.
Definition: parameters.h:311
std::ostream & operator<<(std::ostream &os, const OrderWrapper &order)
Overload stream operators.
Definition: fe_type.h:182
std::map< std::string, std::unique_ptr< Value >, std::less<> > map_type
The type of the map that we store internally.
Definition: parameters.h:247
The libMesh namespace provides an interface to certain functionality in the library.
virtual Parameters & operator+=(const Parameters &source)
Addition/Assignment operator.
Definition: parameters.h:341
virtual std::unique_ptr< Value > clone() const =0
Clone this value.
Concrete definition of a parameter value for a specified type.
Definition: parameters.h:206
virtual Parameters & operator=(const Parameters &source)
Assignment operator.
Definition: parameters.h:332
iterator end()
Iterator pointing to the end of the set of parameters.
Definition: parameters.h:522
const T & get(std::string_view) const
Definition: parameters.h:426
void remove(std::string_view)
Removes the specified parameter from the list, if it exists.
Definition: parameters.h:484
Abstract definition of a parameter value.
Definition: parameters.h:171
libmesh_assert(ctx)
virtual ~Value()=default
Destructor.
iterator begin()
Iterator pointing to the beginning of the set of parameters.
Definition: parameters.h:510
This class implements reference counting.
std::string demangle(const char *name)
Mostly system independent demangler.
T _value
Stored parameter value.
Definition: parameters.h:241
virtual std::string type() const override
String identifying the type of parameter stored.
Definition: parameters.h:295
virtual void print(std::ostream &) const override
Prints the parameter value to the specified stream.
Definition: parameters.h:303
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:324
static const bool value
Definition: xdr_io.C:54
void insert(const std::string &)
Inserts a new Parameter into the object but does not return a writable reference. ...
Definition: parameters.h:458
std::size_t n_parameters() const
Definition: parameters.h:148
Parameters()=default
Default constructor.
map_type::const_iterator const_iterator
Constant parameter map iterator.
Definition: parameters.h:257
virtual void set_attributes(const std::string &, bool)
Overridable function to set any extended attributes for classes inheriting from this class...
Definition: parameters.h:138
map_type::iterator iterator
Parameter map iterator.
Definition: parameters.h:252