Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
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
44#include "Teuchos_as.hpp"
45
46
47namespace {
48
49
50int getFieldWidth(const Teuchos::TabularOutputter::EFieldType fieldType,
51 const int prec)
52{
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
67const 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
78namespace Teuchos {
79
80
81const std::string TabularOutputter::fieldSpacer_(" ");
82
83
85 timer_(""),
86 numLoops_(0)
87{
88 initialize();
90}
91
92
100
101
103{
104#ifdef TEUCHOS_DEBUG
105 out.assert_not_null();
106#endif
107 out_ = fancyOStream(out);
108}
109
110
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) {
121 !(as<int>(fieldName.size()) <= width),
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
138 const int prec )
139{
140 fieldTypePrecision_[fieldType] = prec;
141}
142
143
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
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
185void 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
196 !(currFieldIdx_ == numFields),
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
212{
213 std::fill( fieldTypePrecision_.begin(), fieldTypePrecision_.end(), 4 );
214 currFieldIdx_ = -1;
215}
216
217
218} // namespace Teuchos
#define TEUCHOS_MAX(x, y)
Definition of Teuchos::as, for conversions between types.
Smart reference counting pointer class for automatic garbage collection.
RCP< T > rcpFromRef(T &r)
Return a non-owning weak RCP object from a raw object reference for a defined type.
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.
static const std::string fieldSpacer_
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.
Tuple< int, numFieldTypes > fieldTypePrecision_
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.
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.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.