preCICE v3.1.1
Loading...
Searching...
No Matches
TableWriter.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <chrono>
5#include <iomanip>
6#include <iostream>
7#include <memory>
8#include <numeric>
9#include <stddef.h>
10#include <string>
11#include <utility>
12#include <vector>
13
14struct Column {
16 int width;
17 int precision = 6;
18
19 explicit Column(std::string const &name);
20
21 Column(std::string const &name, int width);
22
23 Column(std::string const &name, int width, int precision);
24};
25
26class Table {
27public:
30 char padding = ' ';
32
33 Table();
34
36
38 template <class... T>
39 void addColumn(T &&... arg)
40 {
41 cols.emplace_back(std::forward<T>(arg)...);
42 }
43
45 void printHeader();
46
48 template <class... Ts>
49 void printRow(Ts... args)
50 {
51 printRow(static_cast<size_t>(0), args...);
52 }
53
55 template <class T, class... Ts>
56 void printRow(size_t index, T a, Ts... args)
57 {
58 out << padding << std::setw(cols[index].width) << std::setprecision(cols[index].precision)
59 << a << padding << sepChar;
60 printRow(index + 1, args...);
61 }
62
64 template <class Rep, class Period, class... Ts>
65 void printRow(size_t index, std::chrono::duration<Rep, Period> duration, Ts... args)
66 {
67 double ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
68 out << padding << std::setw(cols[index].width) << std::setprecision(cols[index].precision)
69 << ms << padding << sepChar;
70 printRow(index + 1, args...);
71 }
72
74 template <class T>
75 void printRow(size_t index, T a)
76 {
77 out << padding << std::setw(cols[index].width) << std::setprecision(cols[index].precision)
78 << a << padding << sepChar << '\n';
79 }
80};
unsigned int index
std::vector< Column > cols
void printRow(size_t index, std::chrono::duration< Rep, Period > duration, Ts... args)
Prints a duration as milliseconds.
void printRow(size_t index, T a, Ts... args)
Prints a ostream convertible type.
std::ostream & out
void addColumn(T &&... arg)
Adds a column of given name, width and float precision.
std::string sepChar
char padding
void printRow(size_t index, T a)
Recursion anchor, prints the last entry and the endl.
void printHeader()
Prints the formatted header.
void printRow(Ts... args)
Prints a line, accepting arbitrary arguments.
T emplace_back(T... args)
T setprecision(T... args)
T setw(T... args)
int precision
std::string name
Column(std::string const &name)