Teuchos Package Browser (Single Doxygen Collection)
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
core
src
Teuchos_TabularOutputter.cpp
Go to the documentation of this file.
1
// @HEADER
2
// ***********************************************************************
3
//
4
// Teuchos: Common Tools Package
5
// Copyright (2004) Sandia Corporation
6
//
7
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8
// license for use of this work by or on behalf of the U.S. Government.
9
//
10
// Redistribution and use in source and binary forms, with or without
11
// modification, are permitted provided that the following conditions are
12
// met:
13
//
14
// 1. Redistributions of source code must retain the above copyright
15
// notice, this list of conditions and the following disclaimer.
16
//
17
// 2. Redistributions in binary form must reproduce the above copyright
18
// notice, this list of conditions and the following disclaimer in the
19
// documentation and/or other materials provided with the distribution.
20
//
21
// 3. Neither the name of the Corporation nor the names of the
22
// contributors may be used to endorse or promote products derived from
23
// this software without specific prior written permission.
24
//
25
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
//
37
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38
//
39
// ***********************************************************************
40
// @HEADER
41
42
43
#include "
Teuchos_TabularOutputter.hpp
"
44
#include "
Teuchos_as.hpp
"
45
46
47
namespace
{
48
49
50
int
getFieldWidth(
const
Teuchos::TabularOutputter::EFieldType
fieldType,
51
const
int
prec)
52
{
53
typedef
Teuchos::TabularOutputter
TO;
54
switch
(fieldType)
55
{
56
case
TO::DOUBLE:
57
return
prec + 8;
// Leave room sign and exponent
58
case
TO::INT:
59
return
prec+1;
// leave room for sign
60
case
TO::STRING:
61
return
prec;
62
}
63
return
-1;
// Will never be called
64
}
65
66
67
const
std::string getFieldLine(
const
int
width)
68
{
69
std::string line;
70
line.append(width,
'-'
);
71
return
line;
72
}
73
74
75
}
// namespace
76
77
78
namespace
Teuchos
{
79
80
81
const
std::string
TabularOutputter::fieldSpacer_
(
" "
);
82
83
84
TabularOutputter::TabularOutputter
(std::ostream &out):
85
timer_
(
""
),
86
numLoops_
(0)
87
{
88
initialize
();
89
setOStream
(
rcpFromRef
(out));
90
}
91
92
93
TabularOutputter::TabularOutputter
(
const
RCP<std::ostream>
&out):
94
timer_
(
""
),
95
numLoops_
(0)
96
{
97
initialize
();
98
setOStream
(out);
99
}
100
101
102
void
TabularOutputter::setOStream
(
const
RCP<std::ostream>
&out )
103
{
104
#ifdef TEUCHOS_DEBUG
105
out.
assert_not_null
();
106
#endif
107
out_
=
fancyOStream
(out);
108
}
109
110
111
void
TabularOutputter::pushFieldSpec
(
112
const
std::string &fieldName,
const
EFieldType
fieldType,
113
const
EFieldJustification
fieldJustification,
114
const
EFloatingOutputType
floatingOutputType,
115
const
int
width
116
)
117
{
118
#ifdef TEUCHOS_DEBUG
119
if
(width > 0) {
120
TEUCHOS_TEST_FOR_EXCEPTION
(
121
!(
as<int>
(fieldName.size()) <= width),
122
InvalidFieldSpecError
,
123
"Error, the length of the field name \""
<<fieldName<<
"\"\n"
124
"is "
<<fieldName.size()<<
" which is larger than the\n"
125
"specifically set field width "
<<width<<
"!"
126
);
127
}
128
#endif
129
fieldSpecs_
.push_back(
130
FieldSpec
(fieldName, fieldType, fieldJustification, floatingOutputType,
131
TEUCHOS_MAX
(
as<int>
(fieldName.size()), width)
132
)
133
);
134
}
135
136
137
void
TabularOutputter::setFieldTypePrecision
(
const
EFieldType
fieldType,
138
const
int
prec )
139
{
140
fieldTypePrecision_
[fieldType] = prec;
141
}
142
143
144
void
TabularOutputter::outputHeader
()
145
{
146
147
using
std::left;
148
using
std::setw;
149
150
const
int
numFields =
static_cast<
int
>
(
fieldSpecs_
.size());
151
152
#ifdef TEUCHOS_DEBUG
153
TEUCHOS_TEST_FOR_EXCEPTION
(
154
numFields==0,
MissingFieldsError
,
155
"Error, you must add at least one field spec using pushFieldSpec(...)!"
156
);
157
#endif
158
159
160
for
(
int
i = 0; i < numFields; ++i) {
161
FieldSpec
&fieldSpec =
fieldSpecs_
[i];
162
const
EFieldType
fieldType = fieldSpec.
fieldType
;
163
const
int
fieldTypePrecision =
fieldTypePrecision_
[fieldType];
164
fieldSpec.
precision
= fieldTypePrecision;
165
const
int
fieldPrecisionWidth =
166
getFieldWidth(fieldType, fieldTypePrecision);
167
if
(fieldSpec.
outputWidth
< fieldPrecisionWidth) {
168
fieldSpec.
outputWidth
= fieldPrecisionWidth;
169
}
170
*
out_
<<
fieldSpacer_
<< left << setw(fieldSpec.
outputWidth
) << fieldSpec.
fieldName
;
171
}
172
*
out_
<<
"\n"
;
173
174
for
(
int
i = 0; i < numFields; ++i) {
175
const
FieldSpec
&fieldSpec =
fieldSpecs_
[i];
176
*
out_
<<
fieldSpacer_
<< left << setw(fieldSpec.
outputWidth
) << getFieldLine(fieldSpec.
outputWidth
);
177
}
178
*
out_
<<
"\n"
;
179
180
currFieldIdx_
= 0;
181
182
}
183
184
185
void
TabularOutputter::nextRow
(
const
bool
allowRemainingFields)
186
{
187
const
int
numFields =
static_cast<
int
>
(
fieldSpecs_
.size());
188
if
(allowRemainingFields) {
189
while
(
currFieldIdx_
< numFields) {
190
outputField
(
"-"
);
191
}
192
}
193
else
{
194
#ifdef TEUCHOS_DEBUG
195
TEUCHOS_TEST_FOR_EXCEPTION
(
196
!(
currFieldIdx_
== numFields),
197
InvalidFieldOutputError
,
198
"Error, you must call outputField(...) for every field in the row\n"
199
"before you call nextRow()!"
200
);
201
#endif
202
}
203
*
out_
<<
"\n"
;
204
currFieldIdx_
= 0;
205
}
206
207
208
// Private member functions
209
210
211
void
TabularOutputter::initialize
()
212
{
213
std::fill(
fieldTypePrecision_
.begin(),
fieldTypePrecision_
.end(), 4 );
214
currFieldIdx_
= -1;
215
}
216
217
218
}
// namespace Teuchos
TEUCHOS_MAX
#define TEUCHOS_MAX(x, y)
Definition
Teuchos_ConfigDefs.hpp:161
Teuchos_TabularOutputter.hpp
Teuchos_as.hpp
Definition of Teuchos::as, for conversions between types.
Teuchos::RCP
Smart reference counting pointer class for automatic garbage collection.
Definition
Teuchos_RCPDecl.hpp:429
Teuchos::RCP::rcpFromRef
RCP< T > rcpFromRef(T &r)
Return a non-owning weak RCP object from a raw object reference for a defined type.
Teuchos::RCP::assert_not_null
const RCP< T > & assert_not_null() const
Throws NullReferenceError if this->get()==NULL, otherwise returns reference to *this.
Definition
Teuchos_RCP.hpp:570
Teuchos::TabularOutputter::InvalidFieldOutputError
Definition
Teuchos_TabularOutputter.hpp:93
Teuchos::TabularOutputter::InvalidFieldSpecError
Definition
Teuchos_TabularOutputter.hpp:85
Teuchos::TabularOutputter::MissingFieldsError
Definition
Teuchos_TabularOutputter.hpp:81
Teuchos::TabularOutputter
Utility class that makes it easy to create formatted tables of output.
Definition
Teuchos_TabularOutputter.hpp:61
Teuchos::TabularOutputter::EFieldJustification
EFieldJustification
Definition
Teuchos_TabularOutputter.hpp:72
Teuchos::TabularOutputter::timer_
Time timer_
Definition
Teuchos_TabularOutputter.hpp:172
Teuchos::TabularOutputter::initialize
void initialize()
Definition
Teuchos_TabularOutputter.cpp:211
Teuchos::TabularOutputter::fieldSpacer_
static const std::string fieldSpacer_
Definition
Teuchos_TabularOutputter.hpp:156
Teuchos::TabularOutputter::pushFieldSpec
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.
Definition
Teuchos_TabularOutputter.cpp:111
Teuchos::TabularOutputter::nextRow
void nextRow(const bool allowRemainingFields=false)
Finalize the row of output.
Definition
Teuchos_TabularOutputter.cpp:185
Teuchos::TabularOutputter::EFloatingOutputType
EFloatingOutputType
Definition
Teuchos_TabularOutputter.hpp:76
Teuchos::TabularOutputter::TabularOutputter
TabularOutputter()
Teuchos::TabularOutputter::numLoops_
int numLoops_
Definition
Teuchos_TabularOutputter.hpp:173
Teuchos::TabularOutputter::outputHeader
void outputHeader()
Output the headers.
Definition
Teuchos_TabularOutputter.cpp:144
Teuchos::TabularOutputter::setFieldTypePrecision
void setFieldTypePrecision(const EFieldType fieldType, const int prec)
Set the precision of output for a field.
Definition
Teuchos_TabularOutputter.cpp:137
Teuchos::TabularOutputter::fieldSpecs_
Array< FieldSpec > fieldSpecs_
Definition
Teuchos_TabularOutputter.hpp:163
Teuchos::TabularOutputter::fieldTypePrecision_
Tuple< int, numFieldTypes > fieldTypePrecision_
Definition
Teuchos_TabularOutputter.hpp:165
Teuchos::TabularOutputter::setOStream
void setOStream(const RCP< std::ostream > &out)
Set the ostream that all output will be sent to.
Definition
Teuchos_TabularOutputter.cpp:102
Teuchos::TabularOutputter::EFieldType
EFieldType
Definition
Teuchos_TabularOutputter.hpp:68
Teuchos::TabularOutputter::outputField
void outputField(const T &t)
Output to the next field.
Definition
Teuchos_TabularOutputter.hpp:245
Teuchos::TabularOutputter::currFieldIdx_
int currFieldIdx_
Definition
Teuchos_TabularOutputter.hpp:170
Teuchos::TabularOutputter::out_
RCP< FancyOStream > out_
Definition
Teuchos_TabularOutputter.hpp:164
Teuchos::basic_FancyOStream::fancyOStream
RCP< basic_FancyOStream< char > > fancyOStream(const RCP< std::basic_ostream< char > > &oStream, const std::basic_string< char > &tabIndentStr=" ", const int startingTab=0, const bool showLinePrefix=false, const int maxLenLinePrefix=10, const bool showTabCount=false, const bool showProcRank=false)
Dynamically allocate a FancyOStream and return it wrapped in an RCP object.
Definition
Teuchos_FancyOStream.hpp:597
TEUCHOS_TEST_FOR_EXCEPTION
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Definition
Teuchos_TestForException.hpp:178
Teuchos
Definition
Teuchos_AbstractFactory.hpp:47
Teuchos::as
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
Definition
Teuchos_as.hpp:2840
Teuchos::TabularOutputter::FieldSpec
Definition
Teuchos_TabularOutputter.hpp:134
Teuchos::TabularOutputter::FieldSpec::fieldName
std::string fieldName
Definition
Teuchos_TabularOutputter.hpp:146
Teuchos::TabularOutputter::FieldSpec::fieldType
EFieldType fieldType
Definition
Teuchos_TabularOutputter.hpp:147
Teuchos::TabularOutputter::FieldSpec::precision
int precision
Definition
Teuchos_TabularOutputter.hpp:151
Teuchos::TabularOutputter::FieldSpec::outputWidth
int outputWidth
Definition
Teuchos_TabularOutputter.hpp:150
Generated by
1.17.0