Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_Describable.hpp
Go to the documentation of this file.
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 #ifndef TEUCHOS_DESCRIBABLE_HPP
00030 #define TEUCHOS_DESCRIBABLE_HPP
00031 
00032 #include "Teuchos_VerbosityLevel.hpp"
00033 #include "Teuchos_FancyOStream.hpp"
00034 #include "Teuchos_LabeledObject.hpp"
00035 
00036 
00037 namespace Teuchos {
00038 
00039 
00068 class TEUCHOS_LIB_DLL_EXPORT Describable : virtual public LabeledObject {
00069 public:
00070 
00072   static const EVerbosityLevel   verbLevel_default;
00073 
00075 
00076 
00087   virtual std::string description() const;
00088 
00124   virtual void describe(
00125     FancyOStream &out,
00126     const EVerbosityLevel verbLevel = verbLevel_default
00127     ) const;
00128   
00129 };
00130 
00131 
00132 // Describable stream manipulator state class
00133 //
00134 // This is not a class that a user needs to see and that is why it is not
00135 // being given doxygen documentation!
00136 struct DescribableStreamManipulatorState {
00137   const Describable &describable;
00138   const EVerbosityLevel verbLevel;
00139   DescribableStreamManipulatorState(
00140     const Describable &_describable,
00141     const EVerbosityLevel _verbLevel = VERB_MEDIUM
00142     )
00143     :describable(_describable)
00144     ,verbLevel(_verbLevel)
00145     {}
00146 };
00147 
00148 
00170 inline DescribableStreamManipulatorState describe(
00171   const Describable &describable,
00172   const EVerbosityLevel verbLevel = Describable::verbLevel_default
00173   )
00174 {
00175   return DescribableStreamManipulatorState(describable,verbLevel);
00176 }
00177 
00178 
00205 inline
00206 std::ostream& operator<<(
00207   std::ostream& os, const DescribableStreamManipulatorState& d
00208   )
00209 {
00210   d.describable.describe(*getFancyOStream(Teuchos::rcp(&os,false)),d.verbLevel);
00211   return os;
00212 }
00213 
00214 //
00215 // RAB: Note: The above function works with an std::ostream object even
00216 // through Describable::describe(...) requires a FancyOStream object.  We must
00217 // write the stream manipulator in terms of std::ostream, or compound output
00218 // statements like:
00219 //
00220 //  void foo( FancyOStream &out, Describable &d, EVerbLevel verbLevel )
00221 //    {
00222 //      out << "\nThis is the describable object d:" << describe(d,verbLevel);
00223 //    }
00224 //
00225 // will not work correctly.  The problem is that the first output
00226 //
00227 //   out << "\nThis is the describable object d:"
00228 //
00229 //  must return a reference to an std::ostream object.  This should mean that
00230 //  the next statement, which is basically:
00231 //
00232 //    static_cast<std::ostream&>(out) << DescribableStreamManipulatorState
00233 //
00234 // should not even compile.  However, under gcc 3.4.3, the code did compile
00235 // but did not call the above function.  Instead, it set up some type of
00236 // infinite recursion that resulted in a segfault due to the presence of the
00237 // Teuchos::any class!
00238 //
00239 
00240 
00241 } // namespace Teuchos
00242 
00243 #endif // TEUCHOS_DESCRIBABLE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines