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 // 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 
14 namespace {
15 
16 
17 int getFieldWidth(const Teuchos::TabularOutputter::EFieldType fieldType,
18  const int prec)
19 {
20  typedef Teuchos::TabularOutputter TO;
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 
34 const 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 
45 namespace Teuchos {
46 
47 
48 const std::string TabularOutputter::fieldSpacer_(" ");
49 
50 
51 TabularOutputter::TabularOutputter(std::ostream &out):
52  timer_(""),
53  numLoops_(0)
54 {
55  initialize();
56  setOStream(rcpFromRef(out));
57 }
58 
59 
60 TabularOutputter::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
121  numFields==0, MissingFieldsError,
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 =
133  getFieldWidth(fieldType, fieldTypePrecision);
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 
152 void TabularOutputter::nextRow(const bool allowRemainingFields)
153 {
154  const int numFields = static_cast<int>(fieldSpecs_.size());
155  if (allowRemainingFields) {
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 
178 void TabularOutputter::initialize()
179 {
180  std::fill( fieldTypePrecision_.begin(), fieldTypePrecision_.end(), 4 );
181  currFieldIdx_ = -1;
182 }
183 
184 
185 } // 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.