Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_TabularOutputter.cpp
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 
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 
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
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 
211 void TabularOutputter::initialize()
212 {
213  std::fill( fieldTypePrecision_.begin(), fieldTypePrecision_.end(), 4 );
214  currFieldIdx_ = -1;
215 }
216 
217 
218 } // namespace Teuchos
iterator begin() const
Return an iterator to beginning of the array of data.
void outputHeader()
Output the headers.
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.
Utility class that makes it easy to create formatted tables of output.
void setFieldTypePrecision(const EFieldType fieldType, const int prec)
Set the precision of output for a field.
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 setOStream(const RCP< std::ostream > &out)
Set the ostream that all output will be sent to.
const RCP< T > & assert_not_null() const
Throws NullReferenceError if this-&gt;get()==NULL, otherwise returns reference to *this.
void push_back(const value_type &x)
iterator end() const
Return an iterator to past the end of the array of data.
size_type size() const
Definition of Teuchos::as, for conversions between types.
void nextRow(const bool allowRemainingFields=false)
Finalize the row of output.