Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Teuchos_TabularOutputter.cpp
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   numLoops_(0)
00087 {
00088   initialize();
00089   setOStream(rcpFromRef(out));
00090 }
00091 
00092 
00093 TabularOutputter::TabularOutputter(const RCP<std::ostream> &out):
00094   timer_(""),
00095   numLoops_(0)
00096 {
00097   initialize();
00098   setOStream(out);
00099 }
00100 
00101 
00102 void TabularOutputter::setOStream( const RCP<std::ostream> &out )
00103 {
00104 #ifdef TEUCHOS_DEBUG
00105   out.assert_not_null();
00106 #endif
00107   out_ = fancyOStream(out);
00108 }
00109 
00110 
00111 void TabularOutputter::pushFieldSpec(
00112   const std::string &fieldName, const EFieldType fieldType,
00113   const EFieldJustification fieldJustification,
00114   const EFloatingOutputType floatingOutputType,
00115   const int width
00116   )
00117 {
00118 #ifdef TEUCHOS_DEBUG
00119   if (width > 0) {
00120     TEUCHOS_TEST_FOR_EXCEPTION(
00121       !(as<int>(fieldName.size()) <= width),
00122       InvalidFieldSpecError,
00123       "Error, the length of the field name \""<<fieldName<<"\"\n"
00124       "is "<<fieldName.size()<<" which is larger than the\n"
00125       "specifically set field width "<<width<<"!"
00126       );
00127   }
00128 #endif
00129   fieldSpecs_.push_back(
00130     FieldSpec(fieldName, fieldType, fieldJustification, floatingOutputType,
00131       TEUCHOS_MAX(as<int>(fieldName.size()), width)
00132       )
00133     );
00134 }
00135 
00136 
00137 void TabularOutputter::setFieldTypePrecision( const EFieldType fieldType,
00138   const int prec )
00139 {
00140   fieldTypePrecision_[fieldType] = prec;
00141 }
00142 
00143 
00144 void TabularOutputter::outputHeader()
00145 {
00146 
00147   using std::left;
00148   using std::setw;
00149 
00150   const int numFields = fieldSpecs_.size();
00151 
00152 #ifdef TEUCHOS_DEBUG
00153   TEUCHOS_TEST_FOR_EXCEPTION(
00154     numFields==0, MissingFieldsError,
00155     "Error, you must add at least one field spec using pushFieldSpec(...)!"
00156     );
00157 #endif
00158 
00159 
00160   for (int i = 0; i < numFields; ++i) {
00161     FieldSpec &fieldSpec = fieldSpecs_[i];
00162     const EFieldType fieldType = fieldSpec.fieldType;
00163     const int fieldTypePrecision = fieldTypePrecision_[fieldType];
00164     fieldSpec.precision = fieldTypePrecision;
00165     const int fieldPrecisionWidth =
00166       getFieldWidth(fieldType, fieldTypePrecision);
00167     if (fieldSpec.outputWidth < fieldPrecisionWidth) {
00168       fieldSpec.outputWidth = fieldPrecisionWidth;
00169     }
00170     *out_ << fieldSpacer_ << left << setw(fieldSpec.outputWidth) << fieldSpec.fieldName;
00171   }
00172   *out_ << "\n";
00173 
00174   for (int i = 0; i < numFields; ++i) {
00175     const FieldSpec &fieldSpec = fieldSpecs_[i];
00176     *out_ << fieldSpacer_ << left << setw(fieldSpec.outputWidth) << getFieldLine(fieldSpec.outputWidth);
00177   }
00178   *out_ << "\n";
00179 
00180   currFieldIdx_ = 0;
00181 
00182 }
00183 
00184 
00185 void TabularOutputter::nextRow(const bool allowRemainingFields)
00186 {
00187   const int numFields = fieldSpecs_.size();
00188   if (allowRemainingFields) {
00189     while (currFieldIdx_ < numFields) {
00190       outputField("-");
00191     }
00192   }
00193   else {
00194 #ifdef TEUCHOS_DEBUG
00195     TEUCHOS_TEST_FOR_EXCEPTION(
00196       !(currFieldIdx_ == numFields),
00197       InvalidFieldOutputError,
00198       "Error, you must call outputField(...) for every field in the row\n"
00199       "before you call nextRow()!"
00200       );
00201 #endif
00202   }
00203   *out_ << "\n";
00204   currFieldIdx_ = 0;
00205 }
00206 
00207 
00208 // Private member functions
00209 
00210 
00211 void TabularOutputter::initialize()
00212 {
00213   std::fill( fieldTypePrecision_.begin(), fieldTypePrecision_.end(), 4 );
00214   currFieldIdx_ = -1;
00215 }
00216 
00217 
00218 } // namespace Teuchos
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines