Initfile.hpp 4.94 KB
Newer Older
1
2
#pragma once

3
#include <array>
4
5
6
#include <string>
#include <iostream>
#include <type_traits>
7
8
#include <list>
#include <vector>
9
10
11
12
13

#include <boost/lexical_cast.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <boost/tokenizer.hpp>

14
#include <dune/common/fvector.hh>
15
16
17
18
#include <dune/common/parametertree.hh>
// #include <boost/property_tree/ptree.hpp>

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

20
#include <dune/amdis/Output.hpp>
21
#include <dune/amdis/common/Math.hpp>
22
23
24

namespace AMDiS
{
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  // /// output-stream for std::list
  // template <class T, class Alloc>
  // std::ostream& operator<<(std::ostream& out, std::list<T,Alloc> const& l)
  // {
  //   auto it = l.begin();
  //   out << "[";
  //   if (l.size() > 0)
  //     out << *it;
  //   for (; it != l.end(); ++it)
  //     out << ", " << *it;
  //   out << "]";
  //   return out;
  // }


  // /// output-stream for std::vector
  // template <class T, class Alloc>
  // std::ostream& operator<<(std::ostream& out, std::vector<T,Alloc> const& l)
  // {
  //   auto it = l.begin();
  //   out << "[";
  //   if (l.size() > 0)
  //     out << *it;
  //   for (; it != l.end(); ++it)
  //     out << ", " << *it;
  //   out << "]";
  //   return out;
  // }
53
54
55
56
57

  inline void replaceAll(std::string& str, std::string const& from, std::string const& to)
  {
    if (from.empty())
      return;
58
    std::size_t start_pos = 0;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    while ((start_pos = str.find(from, start_pos)) != std::string::npos)
    {
      str.replace(start_pos, from.length(), to);
      start_pos += to.length();
    }
  }

  // _____________________________________________________________________________

  /** Basis data container as a map of tag on a value as strings. The container
  *  throws an exception, if the tag was not found.
  */
  struct Initfile
  {
    using Self = Initfile;

    /** initialize init-file from file with filename in, read data and save it
    *  to singleton-map
    *  @param in: filename string
    */
    static void init(std::string in);


    template <class T>
83
    static Dune::Std::optional<T> get(std::string key)
84
    {
85
86
87
88
89
90
91
      replaceAll(key, "->", ".");
      try {
        return singlett().pt.get<T>(key);
      }
      catch (...) {
        return Dune::Std::nullopt;
      }
92
93
94
    }


95
96
97
98
99
100
101
    /** \brief Static get routine for getting parameter-values from init-file
    *  initialized in init()-method.
    *  Cast the value to the desired type.
    *
    *  \param tag: The tag to look for
    *  \param value: The result.
    */
102
    template <class T>
103
    static void get(std::string key, T& value)
104
    {
105
106
      replaceAll(key, "->", ".");
      value = singlett().pt.get(key, value);
107
108
109
110
111
112
113
114
    }


    /// update map tag->value_old to tag->value in singleton
    template <class T>
    static void set(std::string tag, T& value, int debugInfo=  -1)
    {
      if (debugInfo == -1)
115
        debugInfo = singlett().getMsgInfo();
116

117
118
119
      replaceAll(tag, "->", ".");
      // auto tagPath = path(tag, '>');
      // singlett().pt.put(tagPath, value);
120
121

      // update msg parameters msgInfo, msgWait, paramInfo
122
      singlett().getInternalParameters();
123
124
125
//       if (debugInfo == 2)
//         std::cout << "Parameter '" << tag << "'"
//                   << " set to: " << value << std::endl;
126
127
128
129
130
131
132
133
134
135
136
137
138
    }


    /// add map tag->value to data in singleton
    template <class T>
    static void add(std::string tag, T& value, int debugInfo = -1)
    {
      set(tag, value, debugInfo);
    }

    /// Returns specified info level
    static int getMsgInfo()
    {
139
      return singlett().msgInfo;
140
141
142
143
144
145
146
147
148
149
    }


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


    /// clear data in singleton
    static void clearData()
    {
150
      // singlett().clear();
151
152
153
154
155
156
    }


    /// save singlett-data to file with filename fn
    static void save(std::string /*fn*/)
    {
157
      // using boost::property_tree::json_parser;
158
      // json_parser::write_jason(fn, singlett().pt);
159
160
161
162
163
    }

  protected:
    Initfile() = default;

164
165
    /// return the singleton that contains the data
    static Initfile& singlett()
166
    {
167
168
      static Initfile initfile;
      return initfile;
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    }

    /** Fill the initfile from an input stream.
    * @param in: the stream to fill the data from.
    * Current dataformat: tag:value
    * Comment char: percent '%'
    * Include files: #include "filename" or #include <filename>
    */
    void read(std::string fn, bool force = false);

    /// Write data-map to initfile with filename fn
    void write(std::string fn);

    /// read parameters for msgInfo, msgWait, paramInfo
    void getInternalParameters();

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

189
190
    /// ParameterTree to read/store parameter values
    Dune::ParameterTree pt;
191
192
193
194
195
  };

  using Parameters = Initfile;

#ifndef AMDIS_NO_EXTERN_INITFILE
196
197
198
  extern template void Initfile::get(std::string, int&);
  extern template void Initfile::get(std::string, double&);
  extern template void Initfile::get(std::string, std::string&);
199
200
201
#endif

} // end namespace AMDiS