|
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 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are 00012 // met: 00013 // 00014 // 1. Redistributions of source code must retain the above copyright 00015 // notice, this list of conditions and the following disclaimer. 00016 // 00017 // 2. Redistributions in binary form must reproduce the above copyright 00018 // notice, this list of conditions and the following disclaimer in the 00019 // documentation and/or other materials provided with the distribution. 00020 // 00021 // 3. Neither the name of the Corporation nor the names of the 00022 // contributors may be used to endorse or promote products derived from 00023 // this software without specific prior written permission. 00024 // 00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY 00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE 00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00036 // 00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00038 // 00039 // *********************************************************************** 00040 // @HEADER 00041 00042 00043 #include "Teuchos_TabularOutputter.hpp" 00044 #include "Teuchos_as.hpp" 00045 00046 00047 namespace { 00048 00049 00050 int getFieldWidth(const Teuchos::TabularOutputter::EFieldType fieldType, 00051 const int prec) 00052 { 00053 typedef Teuchos::TabularOutputter TO; 00054 switch(fieldType) 00055 { 00056 case TO::DOUBLE: 00057 return prec + 8; // Leave room sign and exponent 00058 case TO::INT: 00059 return prec+1; // leave room for sign 00060 case TO::STRING: 00061 return prec; 00062 } 00063 return -1; // Will never be called 00064 } 00065 00066 00067 const std::string getFieldLine(const int width) 00068 { 00069 std::string line; 00070 line.append(width, '-'); 00071 return line; 00072 } 00073 00074 00075 } // namespace 00076 00077 00078 namespace Teuchos { 00079 00080 00081 const std::string TabularOutputter::fieldSpacer_(" "); 00082 00083 00084 TabularOutputter::TabularOutputter(std::ostream &out) 00085 :timer_("") 00086 { 00087 initialize(); 00088 setOStream(rcpFromRef(out)); 00089 } 00090 00091 00092 TabularOutputter::TabularOutputter(const RCP<std::ostream> &out) 00093 :timer_("") 00094 { 00095 initialize(); 00096 setOStream(out); 00097 } 00098 00099 00100 void TabularOutputter::setOStream( const RCP<std::ostream> &out ) 00101 { 00102 #ifdef TEUCHOS_DEBUG 00103 out.assert_not_null(); 00104 #endif 00105 out_ = fancyOStream(out); 00106 } 00107 00108 00109 void TabularOutputter::pushFieldSpec( 00110 const std::string &fieldName, const EFieldType fieldType, 00111 const EFieldJustification fieldJustification, 00112 const EFloatingOutputType floatingOutputType, 00113 const int width 00114 ) 00115 { 00116 #ifdef TEUCHOS_DEBUG 00117 if (width > 0) { 00118 TEUCHOS_TEST_FOR_EXCEPTION( 00119 !(as<int>(fieldName.size()) <= width), 00120 InvalidFieldSpecError, 00121 "Error, the length of the field name \""<<fieldName<<"\"\n" 00122 "is "<<fieldName.size()<<" which is larger than the\n" 00123 "specifically set field width "<<width<<"!" 00124 ); 00125 } 00126 #endif 00127 fieldSpecs_.push_back( 00128 FieldSpec(fieldName, fieldType, fieldJustification, floatingOutputType, 00129 TEUCHOS_MAX(as<int>(fieldName.size()), width) 00130 ) 00131 ); 00132 } 00133 00134 00135 void TabularOutputter::setFieldTypePrecision( const EFieldType fieldType, 00136 const int prec ) 00137 { 00138 fieldTypePrecision_[fieldType] = prec; 00139 } 00140 00141 00142 void TabularOutputter::outputHeader() 00143 { 00144 00145 using std::left; 00146 using std::setw; 00147 00148 const int numFields = fieldSpecs_.size(); 00149 00150 #ifdef TEUCHOS_DEBUG 00151 TEUCHOS_TEST_FOR_EXCEPTION( 00152 numFields==0, MissingFieldsError, 00153 "Error, you must add at least one field spec using pushFieldSpec(...)!" 00154 ); 00155 #endif 00156 00157 00158 for (int i = 0; i < numFields; ++i) { 00159 FieldSpec &fieldSpec = fieldSpecs_[i]; 00160 const EFieldType fieldType = fieldSpec.fieldType; 00161 const int fieldTypePrecision = fieldTypePrecision_[fieldType]; 00162 fieldSpec.precision = fieldTypePrecision; 00163 const int fieldPrecisionWidth = 00164 getFieldWidth(fieldType, fieldTypePrecision); 00165 if (fieldSpec.outputWidth < fieldPrecisionWidth) { 00166 fieldSpec.outputWidth = fieldPrecisionWidth; 00167 } 00168 *out_ << fieldSpacer_ << left << setw(fieldSpec.outputWidth) << fieldSpec.fieldName; 00169 } 00170 *out_ << "\n"; 00171 00172 for (int i = 0; i < numFields; ++i) { 00173 const FieldSpec &fieldSpec = fieldSpecs_[i]; 00174 *out_ << fieldSpacer_ << left << setw(fieldSpec.outputWidth) << getFieldLine(fieldSpec.outputWidth); 00175 } 00176 *out_ << "\n"; 00177 00178 currFieldIdx_ = 0; 00179 00180 } 00181 00182 00183 void TabularOutputter::nextRow(const bool allowRemainingFields) 00184 { 00185 const int numFields = fieldSpecs_.size(); 00186 if (allowRemainingFields) { 00187 while (currFieldIdx_ < numFields) { 00188 outputField("-"); 00189 } 00190 } 00191 else { 00192 #ifdef TEUCHOS_DEBUG 00193 TEUCHOS_TEST_FOR_EXCEPTION( 00194 !(currFieldIdx_ == numFields), 00195 InvalidFieldOutputError, 00196 "Error, you must call outputField(...) for every field in the row\n" 00197 "before you call nextRow()!" 00198 ); 00199 #endif 00200 } 00201 *out_ << "\n"; 00202 currFieldIdx_ = 0; 00203 } 00204 00205 00206 // Private member functions 00207 00208 00209 void TabularOutputter::initialize() 00210 { 00211 std::fill( fieldTypePrecision_.begin(), fieldTypePrecision_.end(), 4 ); 00212 currFieldIdx_ = -1; 00213 } 00214 00215 00216 } // namespace Teuchos
1.7.6.1