|
Teuchos - Trilinos Tools Package Version of the Day
|
00001 // @HEADER 00002 // *********************************************************************** 00003 // 00004 // Teuchos: Common Tools Package 00005 // Copyright (2004) Sandia Corporation 00006 // 00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive 00008 // license for use of this work by or on behalf of the U.S. Government. 00009 // 00010 // This library is free software; you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as 00012 // published by the Free Software Foundation; either version 2.1 of the 00013 // License, or (at your option) any later version. 00014 // 00015 // This library is distributed in the hope that it will be useful, but 00016 // WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this library; if not, write to the Free Software 00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00023 // USA 00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00025 // 00026 // *********************************************************************** 00027 // @HEADER 00028 00029 00030 #include "Teuchos_TabularOutputter.hpp" 00031 #include "Teuchos_as.hpp" 00032 00033 00034 namespace { 00035 00036 00037 int getFieldWidth(const Teuchos::TabularOutputter::EFieldType fieldType, 00038 const int prec) 00039 { 00040 typedef Teuchos::TabularOutputter TO; 00041 switch(fieldType) 00042 { 00043 case TO::DOUBLE: 00044 return prec + 8; // Leave room sign and exponent 00045 case TO::INT: 00046 return prec+1; // leave room for sign 00047 case TO::STRING: 00048 return prec; 00049 } 00050 return -1; // Will never be called 00051 } 00052 00053 00054 const std::string getFieldLine(const int width) 00055 { 00056 std::string line; 00057 line.append(width, '-'); 00058 return line; 00059 } 00060 00061 00062 } // namespace 00063 00064 00065 namespace Teuchos { 00066 00067 00068 const std::string TabularOutputter::fieldSpacer_(" "); 00069 00070 00071 TabularOutputter::TabularOutputter(std::ostream &out) 00072 :timer_("") 00073 { 00074 initialize(); 00075 setOStream(rcpFromRef(out)); 00076 } 00077 00078 00079 TabularOutputter::TabularOutputter(const RCP<std::ostream> &out) 00080 :timer_("") 00081 { 00082 initialize(); 00083 setOStream(out); 00084 } 00085 00086 00087 void TabularOutputter::setOStream( const RCP<std::ostream> &out ) 00088 { 00089 #ifdef TEUCHOS_DEBUG 00090 out.assert_not_null(); 00091 #endif 00092 out_ = fancyOStream(out); 00093 } 00094 00095 00096 void TabularOutputter::pushFieldSpec( 00097 const std::string &fieldName, const EFieldType fieldType, 00098 const EFieldJustification fieldJustification, 00099 const EFloatingOutputType floatingOutputType, 00100 const int width 00101 ) 00102 { 00103 #ifdef TEUCHOS_DEBUG 00104 if (width > 0) { 00105 TEST_FOR_EXCEPTION( 00106 !(as<int>(fieldName.size()) <= width), 00107 InvalidFieldSpecError, 00108 "Error, the length of the field name \""<<fieldName<<"\"\n" 00109 "is "<<fieldName.size()<<" which is larger than the\n" 00110 "specifically set field width "<<width<<"!" 00111 ); 00112 } 00113 #endif 00114 fieldSpecs_.push_back( 00115 FieldSpec(fieldName, fieldType, fieldJustification, floatingOutputType, 00116 TEUCHOS_MAX(as<int>(fieldName.size()), width) 00117 ) 00118 ); 00119 } 00120 00121 00122 void TabularOutputter::setFieldTypePrecision( const EFieldType fieldType, 00123 const int prec ) 00124 { 00125 fieldTypePrecision_[fieldType] = prec; 00126 } 00127 00128 00129 void TabularOutputter::outputHeader() 00130 { 00131 00132 using std::left; 00133 using std::setw; 00134 00135 const int numFields = fieldSpecs_.size(); 00136 00137 #ifdef TEUCHOS_DEBUG 00138 TEST_FOR_EXCEPTION( 00139 numFields==0, MissingFieldsError, 00140 "Error, you must add at least one field spec using pushFieldSpec(...)!" 00141 ); 00142 #endif 00143 00144 00145 for (int i = 0; i < numFields; ++i) { 00146 FieldSpec &fieldSpec = fieldSpecs_[i]; 00147 const EFieldType fieldType = fieldSpec.fieldType; 00148 const int fieldTypePrecision = fieldTypePrecision_[fieldType]; 00149 fieldSpec.precision = fieldTypePrecision; 00150 const int fieldPrecisionWidth = 00151 getFieldWidth(fieldType, fieldTypePrecision); 00152 if (fieldSpec.outputWidth < fieldPrecisionWidth) { 00153 fieldSpec.outputWidth = fieldPrecisionWidth; 00154 } 00155 *out_ << fieldSpacer_ << left << setw(fieldSpec.outputWidth) << fieldSpec.fieldName; 00156 } 00157 *out_ << "\n"; 00158 00159 for (int i = 0; i < numFields; ++i) { 00160 const FieldSpec &fieldSpec = fieldSpecs_[i]; 00161 *out_ << fieldSpacer_ << left << setw(fieldSpec.outputWidth) << getFieldLine(fieldSpec.outputWidth); 00162 } 00163 *out_ << "\n"; 00164 00165 currFieldIdx_ = 0; 00166 00167 } 00168 00169 00170 void TabularOutputter::nextRow(const bool allowRemainingFields) 00171 { 00172 const int numFields = fieldSpecs_.size(); 00173 if (allowRemainingFields) { 00174 while (currFieldIdx_ < numFields) { 00175 outputField("-"); 00176 } 00177 } 00178 else { 00179 #ifdef TEUCHOS_DEBUG 00180 TEST_FOR_EXCEPTION( 00181 !(currFieldIdx_ == numFields), 00182 InvalidFieldOutputError, 00183 "Error, you must call outputField(...) for every field in the row\n" 00184 "before you call nextRow()!" 00185 ); 00186 #endif 00187 } 00188 *out_ << "\n"; 00189 currFieldIdx_ = 0; 00190 } 00191 00192 00193 // Private member functions 00194 00195 00196 void TabularOutputter::initialize() 00197 { 00198 std::fill( fieldTypePrecision_.begin(), fieldTypePrecision_.end(), 4 ); 00199 currFieldIdx_ = -1; 00200 } 00201 00202 00203 } // namespace Teuchos
1.7.4