Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_TabularOutputter.cpp
1// @HEADER
2// *****************************************************************************
3// Teuchos: Common Tools Package
4//
5// Copyright 2004 NTESS and the Teuchos contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#include "Teuchos_TabularOutputter.hpp"
11#include "Teuchos_as.hpp"
12
13
14namespace {
15
16
17int getFieldWidth(const Teuchos::TabularOutputter::EFieldType fieldType,
18 const int prec)
19{
21 switch(fieldType)
22 {
23 case TO::DOUBLE:
24 return prec + 8; // Leave room sign and exponent
25 case TO::INT:
26 return prec+1; // leave room for sign
27 case TO::STRING:
28 return prec;
29 }
30 return -1; // Will never be called
31}
32
33
34const std::string getFieldLine(const int width)
35{
36 std::string line;
37 line.append(width, '-');
38 return line;
39}
40
41
42} // namespace
43
44
45namespace Teuchos {
46
47
48const std::string TabularOutputter::fieldSpacer_(" ");
49
50
51TabularOutputter::TabularOutputter(std::ostream &out):
52 timer_(""),
53 numLoops_(0)
54{
55 initialize();
56 setOStream(rcpFromRef(out));
57}
58
59
60TabularOutputter::TabularOutputter(const RCP<std::ostream> &out):
61 timer_(""),
62 numLoops_(0)
63{
64 initialize();
65 setOStream(out);
66}
67
68
70{
71#ifdef TEUCHOS_DEBUG
72 out.assert_not_null();
73#endif
74 out_ = fancyOStream(out);
75}
76
77
79 const std::string &fieldName, const EFieldType fieldType,
80 const EFieldJustification fieldJustification,
81 const EFloatingOutputType floatingOutputType,
82 const int width
83 )
84{
85#ifdef TEUCHOS_DEBUG
86 if (width > 0) {
88 !(as<int>(fieldName.size()) <= width),
90 "Error, the length of the field name \""<<fieldName<<"\"\n"
91 "is "<<fieldName.size()<<" which is larger than the\n"
92 "specifically set field width "<<width<<"!"
93 );
94 }
95#endif
96 fieldSpecs_.push_back(
97 FieldSpec(fieldName, fieldType, fieldJustification, floatingOutputType,
98 TEUCHOS_MAX(as<int>(fieldName.size()), width)
99 )
100 );
101}
102
103
105 const int prec )
106{
107 fieldTypePrecision_[fieldType] = prec;
108}
109
110
112{
113
114 using std::left;
115 using std::setw;
116
117 const int numFields = static_cast<int>(fieldSpecs_.size());
118
119#ifdef TEUCHOS_DEBUG
122 "Error, you must add at least one field spec using pushFieldSpec(...)!"
123 );
124#endif
125
126
127 for (int i = 0; i < numFields; ++i) {
128 FieldSpec &fieldSpec = fieldSpecs_[i];
129 const EFieldType fieldType = fieldSpec.fieldType;
130 const int fieldTypePrecision = fieldTypePrecision_[fieldType];
131 fieldSpec.precision = fieldTypePrecision;
132 const int fieldPrecisionWidth =
134 if (fieldSpec.outputWidth < fieldPrecisionWidth) {
135 fieldSpec.outputWidth = fieldPrecisionWidth;
136 }
137 *out_ << fieldSpacer_ << left << setw(fieldSpec.outputWidth) << fieldSpec.fieldName;
138 }
139 *out_ << "\n";
140
141 for (int i = 0; i < numFields; ++i) {
142 const FieldSpec &fieldSpec = fieldSpecs_[i];
143 *out_ << fieldSpacer_ << left << setw(fieldSpec.outputWidth) << getFieldLine(fieldSpec.outputWidth);
144 }
145 *out_ << "\n";
146
147 currFieldIdx_ = 0;
148
149}
150
151
153{
154 const int numFields = static_cast<int>(fieldSpecs_.size());
156 while (currFieldIdx_ < numFields) {
157 outputField("-");
158 }
159 }
160 else {
161#ifdef TEUCHOS_DEBUG
163 !(currFieldIdx_ == numFields),
165 "Error, you must call outputField(...) for every field in the row\n"
166 "before you call nextRow()!"
167 );
168#endif
169 }
170 *out_ << "\n";
171 currFieldIdx_ = 0;
172}
173
174
175// Private member functions
176
177
178void TabularOutputter::initialize()
179{
180 std::fill( fieldTypePrecision_.begin(), fieldTypePrecision_.end(), 4 );
181 currFieldIdx_ = -1;
182}
183
184
185} // namespace Teuchos
Definition of Teuchos::as, for conversions between types.
iterator end() const
Return an iterator to past the end of the array of data.
iterator begin() const
Return an iterator to beginning of the array of data.
size_type size() const
void push_back(const value_type &x)
Smart reference counting pointer class for automatic garbage collection.
const RCP< T > & assert_not_null() const
Throws NullReferenceError if this->get()==NULL, otherwise returns reference to *this.
Utility class that makes it easy to create formatted tables of output.
void pushFieldSpec(const std::string &fieldName, const EFieldType fieldType=DOUBLE, const EFieldJustification fieldJustification=RIGHT, const EFloatingOutputType floatingOutputType=SCIENTIFIC, const int width=-1)
Add a new field to be output.
void nextRow(const bool allowRemainingFields=false)
Finalize the row of output.
void outputHeader()
Output the headers.
void setFieldTypePrecision(const EFieldType fieldType, const int prec)
Set the precision of output for a field.
void setOStream(const RCP< std::ostream > &out)
Set the ostream that all output will be sent to.
void outputField(const T &t)
Output to the next field.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...