Initfile.hpp 2.08 KB
Newer Older
1
2
3
4
#pragma once

#include <string>

5
6
7
#include <dune/common/parametertree.hh>

#include <dune/common/std/optional.hh>
8

9
10
11

namespace AMDiS
{
12
  class Initfile
13
  {
14
15
  public:
    /// initialize singleton object and global parameters
16
17
    static void init(std::string in);

18
19
20
21
22
23
24
    /// \brief Get parameter-values from parameter-tree
    /**
     * Looks for the `key` in the parameter-tree and returns
     * the stored and parsed value if found and parsable.
     *
     * Does not thrown an exception if something goes wrong!
     **/
25
    template <class T>
26
    static Dune::Std::optional<T> get(std::string key)
27
    {
28
      try {
29
        return pt().get<T>(key);
30
31
32
33
      }
      catch (...) {
        return Dune::Std::nullopt;
      }
34
35
    }

36
37
38
39
40
41
42
43
    /// \brief Get parameter-values from parameter-tree with default value
    /**
     *  initialized in init()-method.
     *  Cast the value to the desired type.
     *
     *  \param key: The tag to look for
     *  \param value: The default value and result.
     **/
44
    template <class T>
45
    static void get(std::string key, T& value)
46
    {
47
      value = pt().get(key, value);
48
49
50
51
52
    }

    /// Returns specified info level
    static int getMsgInfo()
    {
53
      return singlett().msgInfo;
54
55
56
57
58
    }

    /// print all data in singleton to std::cout
    static void printParameters();

59
    static void clearData() {}
60
61
62
63

  protected:
    Initfile() = default;

64
    /// Return the singleton that contains the data
65
    static Initfile& singlett()
66
    {
67
68
      static Initfile initfile;
      return initfile;
69
70
    }

71
72
73
74
75
    /// Return the parameter-tree
    static Dune::ParameterTree& pt()
    {
      return singlett().pt_;
    }
76

77
78
    /// Fill an parametr-tree from a file with filename fn
    void read(std::string fn, bool force = false);
79
80
    void write(std::string fn);

81
    /// read standard values for output and information of parameter-values
82
83
84
85
86
87
    void getInternalParameters();

    int msgInfo = 0;
    int paramInfo = 1;
    int breakOnMissingTag = 0;

88
    /// ParameterTree to read/store parameter values
89
    Dune::ParameterTree pt_;
90
91
92
93
94
  };

  using Parameters = Initfile;

} // end namespace AMDiS