MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
logtable.h
Go to the documentation of this file.
1// Copyright (C) 2024 The m-AIA AUTHORS
2//
3// This file is part of m-AIA (https://git.rwth-aachen.de/aia/m-AIA/m-AIA)
4//
5// SPDX-License-Identifier: LGPL-3.0-only
6
7#ifndef LOGTABLE_H
8#define LOGTABLE_H
9
10#include <iomanip>
11#include <iostream>
12#include <memory>
13#include <sstream>
14#include <string>
15#include <vector>
16
17#include "INCLUDE/maiatypes.h"
18
19namespace maia::logtable {
20
21const MInt width = 80;
22
23struct FrameEntry {
25
26 FrameEntry(MString title_) : title(title_){};
27 virtual ~FrameEntry() = default;
28
29 virtual MString buildString(const int shift = 0) = 0;
30};
31
32struct Data final : FrameEntry {
34
36
37 enum { LEFT, RIGHT };
38
39 std::vector<std::shared_ptr<FrameEntry>> entries;
40
41 // String version
42 Data(MString title_, MString data_) : FrameEntry(title_), data(data_), alignment(LEFT){};
43
44 // Integer version
45 Data(MString title_, MInt data_) : FrameEntry(title_), data(std::to_string(data_)), alignment(RIGHT){};
46
47 // Long version
48 Data(MString title_, MLong data_) : FrameEntry(title_), data(std::to_string(data_)), alignment(RIGHT){};
49
50 // Bool version
51 Data(MString title_, MBool data_) : FrameEntry(title_), data(data_ ? "yes" : "no"), alignment(RIGHT){};
52
53 // Floating point version
54 Data(MString title_, MFloat data_) : FrameEntry(title_), data(std::to_string(data_)), alignment(RIGHT){};
55
56 MString buildString(const int shift = 0) final {
57 std::stringstream ss;
58
59 ss << "| " << std::setw(width / 2 + shift) << std::left << title;
60 if(alignment == LEFT) {
61 ss << "| " << std::setw(width / 2 - 6) << std::left << data;
62 } else if(alignment == RIGHT) {
63 ss << "| " << std::setw(width / 2 - 6) << std::right << data;
64 }
65 ss << " |" << '\n';
66
67 for(auto&& entry : entries) {
68 ss << "| " << entry->buildString(shift - 2);
69 }
70
71 return ss.str();
72 }
73
74 template <typename T>
75 Data& addData(const std::string& title_, T data_) {
76 std::shared_ptr<Data> d = std::make_shared<Data>(title_, data_);
77 entries.push_back(d);
78 return *d;
79 }
80
81 void addBlank() {
82 std::shared_ptr<Data> d = std::make_shared<Data>("", std::string(""));
83 entries.push_back(d);
84 }
85
86 ~Data() final {
87 for(auto&& en : entries) {
88 en.reset();
89 }
90 }
91};
92
93struct Group : FrameEntry {
94 std::vector<std::shared_ptr<FrameEntry>> entries;
95
96 Group(MString title_) : FrameEntry(title_){};
97
98 MString buildString(const int shift = 0) final {
99 std::stringstream ss;
100
101 ss << title << '\n';
102
103 for(auto&& entry : entries) {
104 ss << entry->buildString(shift);
105 }
106
107 return ss.str();
108 }
109
110 template <typename T>
111 Data& addData(const std::string& title_, T data_) {
112 std::shared_ptr<Data> d = std::make_shared<Data>(title_, data_);
113 entries.push_back(d);
114 return *d;
115 }
116
117 void addBlank() {
118 std::shared_ptr<Data> d = std::make_shared<Data>("", std::string(""));
119 entries.push_back(d);
120 }
121};
122
123struct Frame {
124 const MString bar = [&]() {
125 std::stringstream tmp;
126 for(int i = 0; i < width; i++) {
127 tmp << "-";
128 }
129 return tmp.str();
130 }();
131
133
134 std::vector<std::shared_ptr<FrameEntry>> entries;
135
136 Frame(MString title_) : title(title_){};
137
139 std::stringstream ss;
140
141 ss << bar << '\n';
142 ss << title << '\n';
143 ss << bar << '\n';
144
145 MBool first = true;
146 for(auto&& entry : entries) {
147 if(!first) {
148 ss << '\n';
149 }
150 ss << entry->buildString();
151 first = false;
152 }
153
154 ss << bar << '\n';
155
156 return ss.str();
157 }
158
159 Group& addGroup(const std::string& title_) {
160 std::shared_ptr<Group> g = std::make_shared<Group>(title_);
161 entries.push_back(g);
162 return *g;
163 }
164};
165
166} // namespace maia::logtable
167
168#endif
int32_t MInt
Definition: maiatypes.h:62
std::basic_string< char > MString
Definition: maiatypes.h:55
double MFloat
Definition: maiatypes.h:52
int64_t MLong
Definition: maiatypes.h:64
bool MBool
Definition: maiatypes.h:58
const MInt width
Definition: logtable.h:21
Data(MString title_, MInt data_)
Definition: logtable.h:45
const MInt alignment
Definition: logtable.h:35
std::vector< std::shared_ptr< FrameEntry > > entries
Definition: logtable.h:39
MString buildString(const int shift=0) final
Definition: logtable.h:56
Data(MString title_, MString data_)
Definition: logtable.h:42
Data(MString title_, MBool data_)
Definition: logtable.h:51
Data & addData(const std::string &title_, T data_)
Definition: logtable.h:75
const MString data
Definition: logtable.h:33
Data(MString title_, MLong data_)
Definition: logtable.h:48
Data(MString title_, MFloat data_)
Definition: logtable.h:54
Definition: logtable.h:23
virtual MString buildString(const int shift=0)=0
const MString title
Definition: logtable.h:24
FrameEntry(MString title_)
Definition: logtable.h:26
virtual ~FrameEntry()=default
Group & addGroup(const std::string &title_)
Definition: logtable.h:159
MString buildString()
Definition: logtable.h:138
std::vector< std::shared_ptr< FrameEntry > > entries
Definition: logtable.h:134
const MString bar
Definition: logtable.h:124
const MString title
Definition: logtable.h:132
Frame(MString title_)
Definition: logtable.h:136
Data & addData(const std::string &title_, T data_)
Definition: logtable.h:111
Group(MString title_)
Definition: logtable.h:96
std::vector< std::shared_ptr< FrameEntry > > entries
Definition: logtable.h:94
MString buildString(const int shift=0) final
Definition: logtable.h:98