xmodel.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "xdata.h"
4 #include "xdefs.h"
5 #include "xtools.h"
6 #include "xunits.h"
7 #include <cctype>
8 #include <cmath>
9 #include <gsl/gsl_const_mksa.h>
10 #include <optional>
11 #include <string>
12 #include <tuple>
13 #include <unordered_map>
14 #include <vector>
15 
16 namespace nmx {
17 
22 class XModel
23 {
24 private:
25  //speichere ID für alle Instanzen dieser Klasse
26  static inline std::string _name;
27 
28 public:
32  XModel() = delete;
33 
38  XModel(const char *name) {
39  if (_name.empty()) {
40  _name = name;
41  std::transform(ALL(_name), _name.begin(), tolower);
42  }
43  }
49  inline static std::ofstream get_output_stream(Format fmt) {
50  return Output::get_stream(class_name(), fmt);
51  }
52 
59  template<class T>
60  inline static std::ofstream get_output_stream(Format fmt, const T &id) {
61  return Output::get_stream(class_name(), fmt, id);
62  }
63 
69  template<class T>
70  inline static void save(const T &data, Format fmt) {
71  std::ofstream output = get_output_stream(fmt);
72  data.save(output, fmt);
73  }
74 
81  template<class T, class ID>
82  inline static void save(const T &data, Format fmt, const ID &id) {
83  std::ofstream output = get_output_stream(fmt, id);
84  data.save(output, fmt);
85  }
86 
91  inline static std::string class_name() { //
92  if (_name.empty()) {
93  throw std::invalid_argument(nmx_msg);
94  }
95  return _name;
96  }
97 }; //XModel
98 
105 template<size_t N, class IDX, class VTYPE = double>
106 class CResult
107 {
108 private:
109  //speichere Elemente vom Typ VTYPE
110  std::array<VTYPE, N> _result;
111 
112 protected:
118  inline void set_result(IDX idx, VTYPE val) { //
119  _result[static_cast<size_t>(idx)] = val;
120  }
121 
126  template<class T>
127  inline void set_result(const T &v) {
128  //der Container muss die gleiche Länge wie das intern
129  //gespeicherte Feld haben
130  Check::error_if<std::out_of_range>(nmx_msg, v.size() != N);
131  std::copy(v.begin(), v.end(), std::begin(_result));
132  }
133 
138  inline void set_result(std::initializer_list<VTYPE> lst) { //
139  Check::error_if<std::out_of_range>(nmx_msg, lst.size() != N);
140  std::copy(lst.begin(), lst.end(), std::begin(_result));
141  }
142 
147  inline void set_result(std::unordered_map<IDX, VTYPE> args) {
148  Check::error_if<std::out_of_range>(nmx_msg, args.size() < N);
149  for (const auto &val : args) {
150  set_result(val.first, val.second);
151  }
152  }
153 
154 public:
160  inline auto operator()(IDX idx) const { //
161  return _result[static_cast<size_t>(idx)];
162  }
163 
169  inline auto get(IDX idx) const { //
170  return _result[static_cast<size_t>(idx)];
171  }
172 
177  inline const auto &result() const { return _result; }
178 };
179 
180 } // namespace nmx
XModel(const char *name)
XModelBase Konstruktor.
Definition: xmodel.h:38
const auto & result() const
result
Definition: xmodel.h:177
void set_result(std::initializer_list< VTYPE > lst)
set_result allen Elementen werden Werte zugewiesen
Definition: xmodel.h:138
The XModel class Basisklasse speichert eine ID in Form einer Zeichenkette enthält Hilfsfunktionen zur...
Definition: xmodel.h:22
#define nmx_msg
Definition: xerror.h:113
void set_result(std::unordered_map< IDX, VTYPE > args)
set_result setze Werte über assoziativen Container
Definition: xmodel.h:147
static std::ofstream get_output_stream(Format fmt, const T &id)
get_output_stream
Definition: xmodel.h:60
static std::ofstream get_stream(const std::string &fname)
get_output_stream Öffnen einer Ausgabedatei falls die Datei nicht geöffnet werden kann wird ein Fehle...
Definition: xoutput.h:29
static void save(const T &data, Format fmt, const ID &id)
save Speicherung von Daten in eine Datei
Definition: xmodel.h:82
auto operator()(IDX idx) const
operator () Zugriff auf Element
Definition: xmodel.h:160
XModel()=delete
XModelBase kein Standardkonstruktor.
static void save(const T &data, Format fmt)
save Speicherung von Daten in eine Datei
Definition: xmodel.h:70
static std::ofstream get_output_stream(Format fmt)
get_output_stream
Definition: xmodel.h:49
static std::string class_name()
class_name
Definition: xmodel.h:91
The Format class Formatierte Ausgabe von Arrays.
Definition: xfmt.h:10
#define ALL(x)
The CResult class Speicherung von Rechenergebnissen.
Definition: xmodel.h:106
void set_result(IDX idx, VTYPE val)
operator () einem Element wird ein Wert zugewiesen
Definition: xmodel.h:118
void set_result(const T &v)
set_result allen Elementen werden Werte zugewiesen
Definition: xmodel.h:127