deal2lkit: A ToolKit library for Deal.II
parameter_acceptor.h
Go to the documentation of this file.
1 //-----------------------------------------------------------
2 //
3 // Copyright (C) 2014 - 2016 by the deal2lkit authors
4 //
5 // This file is part of the deal2lkit library.
6 //
7 // The deal2lkit library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal2lkit distribution.
13 //
14 //-----------------------------------------------------------
15 
16 #ifndef _d2k_parameter_acceptor_h
17 #define _d2k_parameter_acceptor_h
18 
19 #include <deal2lkit/config.h>
20 #include <deal2lkit/utilities.h>
23 #include <deal.II/base/logstream.h>
25 #include <boost/any.hpp>
26 #include <typeinfo>
27 
28 
29 
30 using namespace dealii;
31 
32 D2K_NAMESPACE_OPEN
33 
381 {
382 public:
389  ParameterAcceptor(const std::string section_name="");
390 
395  virtual ~ParameterAcceptor();
396 
404  static void initialize(const std::string filename="",
405  const std::string outfilename="");
406 
410  static void clear();
411 
417  virtual void parse_parameters(ParameterHandler &prm);
418 
429  virtual void parse_parameters_call_back();
430 
431 
450 
451 
457  static void parse_all_parameters(ParameterHandler &prm=ParameterAcceptor::prm);
458 
459 
463  static void log_info();
464 
465 
472  static void declare_all_parameters(ParameterHandler &prm=ParameterAcceptor::prm);
473 
474 
480  std::string get_section_name() const;
481 
486  std::vector<std::string> get_section_path() const;
487 
494  template <class T>
495  void add_parameter(ParameterHandler &prm, T *parameter,
496  const std::string &entry,
497  const std::string &default_value,
498  const Patterns::PatternBase &pattern=Patterns::Anything(),
499  const std::string &documentation=std::string())
500  {
502  ExcMessage("You tried to add a parameter using a const "
503  "variable. This is not allowed, since these "
504  "variables will be filled later on when "
505  "parsing the parameter."));
506 
507  prm.declare_entry(entry, default_value, pattern, documentation);
508  parameters[entry] = boost::any(parameter);
509  }
510 
511 
528  template <class T>
529  void add_parameter(T &parameter,
530  const std::string &entry,
531  const std::string &documentation=std::string(),
533  {
535  ExcMessage("You tried to add a parameter using a const "
536  "variable. This is not allowed, since these "
537  "variables will be filled later on when "
538  "parsing the parameter."));
539 
540  enter_my_subsection(prm);
541  prm.declare_entry(entry, to_string(parameter),
542  *to_pattern(parameter),
543  documentation);
544  leave_my_subsection(prm);
545  parameters[entry] = boost::any(&parameter);
546  }
547 
552  void enter_my_subsection(ParameterHandler &prm);
553 
558  void leave_my_subsection(ParameterHandler &prm);
559 
564  template <class T>
565  static std_cxx11::shared_ptr<Patterns::PatternBase> to_pattern(const T &);
566 
570  template <class T>
571  static T to_type(const std::string &);
572 
576  template <class T>
577  static std::string to_string(const T &);
578 
583 
584 private:
589  static std::vector<SmartPointer<ParameterAcceptor> > class_list;
590 
592  const unsigned int acceptor_id;
593 
598  mutable std::map<std::string, boost::any> parameters;
599 
603  static const char sep = '/';
604 
605 protected:
607  const std::string section_name;
608 };
609 
610 
611 D2K_NAMESPACE_CLOSE
612 
613 #endif
614 
A parameter acceptor base class.
static ParameterHandler prm
Static parameter.
virtual void declare_parameters(ParameterHandler &)
Generate entries in the given parameter file.
#define AssertThrow(cond, exc)
void add_parameter(T &parameter, const std::string &entry, const std::string &documentation=std::string(), ParameterHandler &prm=ParameterAcceptor::prm)
Add a parameter to the global parameter handler ParameterAcceptor::prm.
std::string to_string(const number value, const unsigned int digits=numbers::invalid_unsigned_int)
static::ExceptionBase & ExcMessage(std::string arg1)
const std::string section_name
The subsection name for this class.
static const bool value
void declare_entry(const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation=std::string())
static std::vector< SmartPointer< ParameterAcceptor > > class_list
A list containing all constructed classes of type ParameterAcceptor.
std::map< std::string, boost::any > parameters
A map of parameters that are initialized in this class with the functions add_parameters.
void add_parameter(ParameterHandler &prm, T *parameter, const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation=std::string())
Add a parameter the given parameter list.
const unsigned int acceptor_id
The index of this specific class within the class list.