xfmt.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iomanip>
4 #include <string>
5 
6 namespace nmx {
10 class Format
11 {
12  //Name, Trennzeichen,Zeichen am Ende jeder Zeile,Zusatzname
13  std::string _name, _sep, _lend, _extension;
14 
15 public:
16  //Anzahl der Ziffern
17  std::streamsize precision = 3;
18 
25  inline Format(const std::string &sep, //
26  const std::string &lend,
27  std::streamsize prec = 3)
28  : _name{ "none" }
29  , _sep{ sep }
30  , _lend{ lend }
31  , _extension{ "none" }
32  , precision{ prec } {}
33 
42  inline Format(const std::string &name, //
43  const std::string &sep,
44  const std::string &lend,
45  const std::string &ext,
46  std::streamsize prec = 3)
47  : _name{ name }
48  , _sep{ sep }
49  , _lend{ lend }
50  , _extension{ ext }
51  , precision{ prec } {}
52 
57  inline auto operator()() const { return std::make_pair(_sep, _lend); }
58 
65  template<class T>
66  void write_line(std::ostream &ofs, const T &crow) const {
67  ofs << crow[0];
68  for (size_t idx = 1; idx < crow.size(); idx++) {
69  ofs << _sep << crow[idx];
70  }
71  ofs << _lend;
72  }
73 
80  template<class X, class... Y>
81  void as_rows(std::ostream &ofs, const X &x, const Y &... y) const {
82  write_line(ofs, x);
83  //sizeof... : die Anzahl der Elemente vom Typ Y
84  if constexpr (sizeof...(y) > 0) {
85  //iteriere über alle Felder
86  for (const auto &arg : { y... }) {
87  //eine Zeile für jeden Container
88  write_line(ofs, arg);
89  }
90  }
91  }
92 
99  template<class X, class... Y>
100  void as_columns(std::ostream &ofs, const X &x, const Y &... y) const {
101  for (size_t idx = 0; idx < x.size(); idx++) {
102  ofs << x[idx];
103  if constexpr (sizeof...(y) > 0) {
104  for (const auto &arg : { y... }) {
105  ofs << _sep << arg[idx];
106  }
107  }
108  ofs << _lend;
109  }
110  }
111 
117  template<class T>
118  void as_columns(std::ostream &ofs, const T &data) const {
119  const size_t dim = data[0].size();
120  for (size_t idx = 0; idx < dim; idx++) {
121  ofs << data[0][idx];
122  for (size_t jdx = 1; jdx < data.size(); jdx++) {
123  ofs << _sep << data[jdx][idx];
124  }
125  ofs << _lend;
126  }
127  }
128 
133  void set_defaults(std::ostream &ofs) const {
134  ofs << std::scientific; // wissenschaftliche Darstellung
135  ofs.precision(precision); //Anzahl der Ziffern
136  }
137  //**
138  //Ausgabe der intern gespeicherten Variablen
139  inline const std::string &sep() const { return _sep; }
140  inline const std::string &lend() const { return _lend; }
141  inline const std::string &name() const { return _name; }
142  inline const std::string &ext() const { return _extension; }
143 };
144 } // namespace nmx
Format(const std::string &name, const std::string &sep, const std::string &lend, const std::string &ext, std::streamsize prec=3)
Format Konstruktor.
Definition: xfmt.h:42
std::streamsize precision
Definition: xfmt.h:17
const std::string & ext() const
Definition: xfmt.h:142
void set_defaults(std::ostream &ofs) const
set_defaults Formatierung der Zahlen
Definition: xfmt.h:133
void as_columns(std::ostream &ofs, const X &x, const Y &... y) const
as_columns beliebige Anzahl von Arrays werden als Spalten einer Tabelle dargestellt ...
Definition: xfmt.h:100
const std::string & lend() const
Definition: xfmt.h:140
void write_line(std::ostream &ofs, const T &crow) const
write_line Daten in einem Array werden in einer Zeile ausgegeben
Definition: xfmt.h:66
Format(const std::string &sep, const std::string &lend, std::streamsize prec=3)
Format Konstruktor.
Definition: xfmt.h:25
void as_rows(std::ostream &ofs, const X &x, const Y &... y) const
as_rows beliebige Anzahl von Arrays werden als Reihen ausgegeben
Definition: xfmt.h:81
The Format class Formatierte Ausgabe von Arrays.
Definition: xfmt.h:10
const std::string & name() const
Definition: xfmt.h:141
void as_columns(std::ostream &ofs, const T &data) const
as_columns Array von Arrays wird als Tabelle ausgegeben.
Definition: xfmt.h:118
const std::string & sep() const
Definition: xfmt.h:139
auto operator()() const
operator () Ausgabe von Trennzeichen,Zeichen am Ende jeder Zeile
Definition: xfmt.h:57