IOSS 2.0
Loading...
Searching...
No Matches
Iohb_Layout.h
Go to the documentation of this file.
1// Copyright(C) 1999-2024 National Technology & Engineering Solutions
2// of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3// NTESS, the U.S. Government retains certain rights in this software.
4//
5// See packages/seacas/LICENSE for details
6
7#pragma once
8
9#include <fmt/format.h>
10#include <fmt/ostream.h>
11#include <fmt/ranges.h>
12#include <sstream>
13#include <string>
14#include <vector>
15
16#include "iohb_export.h"
17
18namespace Iohb {
19 class IOHB_EXPORT Layout
20 {
21 public:
22 Layout(bool show_labels, int precision, std::string separator, int field_width);
23 Layout(const Layout &) = delete;
24 Layout &operator=(const Layout &) = delete;
25
26 const std::string layout() const { return layout_.str(); }
27
28 void add_literal(const std::string &label);
29 void add_legend(const std::string &label);
30
31 template <typename T> void add(const std::string &name, const T &value);
32 template <typename T> void add(const std::string &name, const std::vector<T> &value);
33
34 private:
35 void output_common(const std::string &name);
36 std::ostringstream layout_{};
37 std::string separator_{", "};
38
39 int precision_{5};
40 int count_{0}; // Number of fields on current line...
41 int fieldWidth_{0};
42 bool showLabels{true};
43 bool legendStarted{false};
44 };
45
46 inline void Layout::output_common(const std::string &name)
47 {
48 if (count_++ > 0 && !separator_.empty()) {
49 fmt::print(layout_, "{}", separator_);
50 }
51
52 if (showLabels && !name.empty()) {
53 fmt::print(layout_, "{}=", name);
54 }
55 }
56
57 template <typename T> inline void Layout::add(const std::string &name, const T &value)
58 {
59 output_common(name);
60 if (!showLabels && fieldWidth_ > 0) {
61 fmt::print(layout_, "{0:{1}}", value, fieldWidth_);
62 }
63 else {
64 fmt::print(layout_, "{}", value);
65 }
66 }
67
68 template <> inline void Layout::add(const std::string &name, const double &value)
69 {
70 output_common(name);
71 if (precision_ == -1) {
72 // Use lib::fmt full precision output -- as many digits as needed to fully represent the
73 // double
74 fmt::print(layout_, "{}", value);
75 }
76 else if (!showLabels && fieldWidth_ > 0) {
77 fmt::print(layout_, "{0:{1}.{2}e}", value, fieldWidth_, precision_);
78 }
79 else {
80 fmt::print(layout_, "{0:.{1}e}", value, precision_);
81 }
82 }
83
84 template <typename T>
85 inline void Layout::add(const std::string &name, const std::vector<T> &value)
86 {
87 if (value.size() == 1) {
88 add(name, value[0]);
89 }
90 else {
91 output_common(name);
92 if (!showLabels && fieldWidth_ > 0) {
93 fmt::print(layout_, "{0:{1}}", fmt::join(value, separator_), fieldWidth_);
94 }
95 else {
96 fmt::print(layout_, "{}", fmt::join(value, separator_));
97 }
98 }
99 }
100
101 template <> inline void Layout::add(const std::string &name, const std::vector<double> &value)
102 {
103 if (value.size() == 1) {
104 add(name, value[0]);
105 }
106 else {
107 output_common(name);
108 if (precision_ == -1) {
109 // Use lib::fmt full precision output -- as many digits as needed to fully represent the
110 // double
111 fmt::print(layout_, "{}", fmt::join(value, separator_));
112 }
113 else if (!showLabels && fieldWidth_ > 0) {
114 fmt::print(layout_, "{0:{2}.{1}e}", fmt::join(value, separator_), precision_, fieldWidth_);
115 }
116 else {
117 fmt::print(layout_, "{0:.{1}e}", fmt::join(value, separator_), precision_);
118 }
119 }
120 }
121
122} // namespace Iohb
Definition Iohb_Layout.h:20
bool showLabels
Definition Iohb_Layout.h:42
std::string separator_
Definition Iohb_Layout.h:37
int count_
Definition Iohb_Layout.h:40
void output_common(const std::string &name)
Definition Iohb_Layout.h:46
Layout & operator=(const Layout &)=delete
Layout(const Layout &)=delete
int fieldWidth_
Definition Iohb_Layout.h:41
std::ostringstream layout_
Definition Iohb_Layout.h:36
int precision_
Definition Iohb_Layout.h:39
const std::string layout() const
Definition Iohb_Layout.h:26
void add(const std::string &name, const T &value)
Definition Iohb_Layout.h:57
A namespace for the heartbeat database format.
Definition Iohb_DatabaseIO.C:102