Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
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 
85  :timer_("")
86 {
87  initialize();
88  setOStream(rcpFromRef(out));
89 }
90 
91 
93  :timer_("")
94 {
95  initialize();
96  setOStream(out);
97 }
98 
99 
101 {
102 #ifdef TEUCHOS_DEBUG
103  out.assert_not_null();
104 #endif
105  out_ = fancyOStream(out);
106 }
107 
108 
110  const std::string &fieldName, const EFieldType fieldType,
111  const EFieldJustification fieldJustification,
112  const EFloatingOutputType floatingOutputType,
113  const int width
114  )
115 {
116 #ifdef TEUCHOS_DEBUG
117  if (width > 0) {
119  !(as<int>(fieldName.size()) <= width),
121  "Error, the length of the field name \""<<fieldName<<"\"\n"
122  "is "<<fieldName.size()<<" which is larger than the\n"
123  "specifically set field width "<<width<<"!"
124  );
125  }
126 #endif
127  fieldSpecs_.push_back(
128  FieldSpec(fieldName, fieldType, fieldJustification, floatingOutputType,
129  TEUCHOS_MAX(as<int>(fieldName.size()), width)
130  )
131  );
132 }
133 
134 
136  const int prec )
137 {
138  fieldTypePrecision_[fieldType] = prec;
139 }
140 
141 
143 {
144 
145  using std::left;
146  using std::setw;
147 
148  const int numFields = fieldSpecs_.size();
149 
150 #ifdef TEUCHOS_DEBUG
152  numFields==0, MissingFieldsError,
153  "Error, you must add at least one field spec using pushFieldSpec(...)!"
154  );
155 #endif
156 
157 
158  for (int i = 0; i < numFields; ++i) {
159  FieldSpec &fieldSpec = fieldSpecs_[i];
160  const EFieldType fieldType = fieldSpec.fieldType;
161  const int fieldTypePrecision = fieldTypePrecision_[fieldType];
162  fieldSpec.precision = fieldTypePrecision;
163  const int fieldPrecisionWidth =
164  getFieldWidth(fieldType, fieldTypePrecision);
165  if (fieldSpec.outputWidth < fieldPrecisionWidth) {
166  fieldSpec.outputWidth = fieldPrecisionWidth;
167  }
168  *out_ << fieldSpacer_ << left << setw(fieldSpec.outputWidth) << fieldSpec.fieldName;
169  }
170  *out_ << "\n";
171 
172  for (int i = 0; i < numFields; ++i) {
173  const FieldSpec &fieldSpec = fieldSpecs_[i];
174  *out_ << fieldSpacer_ << left << setw(fieldSpec.outputWidth) << getFieldLine(fieldSpec.outputWidth);
175  }
176  *out_ << "\n";
177 
178  currFieldIdx_ = 0;
179 
180 }
181 
182 
183 void TabularOutputter::nextRow(const bool allowRemainingFields)
184 {
185  const int numFields = fieldSpecs_.size();
186  if (allowRemainingFields) {
187  while (currFieldIdx_ < numFields) {
188  outputField("-");
189  }
190  }
191  else {
192 #ifdef TEUCHOS_DEBUG
194  !(currFieldIdx_ == numFields),
196  "Error, you must call outputField(...) for every field in the row\n"
197  "before you call nextRow()!"
198  );
199 #endif
200  }
201  *out_ << "\n";
202  currFieldIdx_ = 0;
203 }
204 
205 
206 // Private member functions
207 
208 
210 {
211  std::fill( fieldTypePrecision_.begin(), fieldTypePrecision_.end(), 4 );
212  currFieldIdx_ = -1;
213 }
214 
215 
216 } // 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.
Tuple< int, numFieldTypes > fieldTypePrecision_
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.
#define TEUCHOS_MAX(x, y)
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.
iterator end() const
Return an iterator to past the end of the array of data.
static const std::string fieldSpacer_
Smart reference counting pointer class for automatic garbage collection.
Definition of Teuchos::as, for conversions between types.
void nextRow(const bool allowRemainingFields=false)
Finalize the row of output.