Teuchos_VerboseObject.hpp

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_VERBOSE_OBJECT_HPP
00030 #define TEUCHOS_VERBOSE_OBJECT_HPP
00031 
00032 #include "Teuchos_RefCountPtr.hpp"
00033 #include "Teuchos_FancyOStream.hpp"
00034 #include "Teuchos_VerbosityLevel.hpp"
00035 
00036 namespace Teuchos {
00037 
00051 class VerboseObjectBase {
00052 public:
00053 
00055 
00056 
00062   static void setDefaultOStream( const RefCountPtr<FancyOStream> &defaultOStream );
00063 
00065   static RefCountPtr<FancyOStream> getDefaultOStream();
00066 
00068 
00070 
00071   
00073   virtual ~VerboseObjectBase() {}
00074   
00077   explicit
00078   VerboseObjectBase(
00079     const RefCountPtr<FancyOStream>   &oStream  = Teuchos::null
00080     );
00081   
00084   virtual void initializeVerboseObjectBase(
00085     const RefCountPtr<FancyOStream>   &oStream  = Teuchos::null
00086     );
00087 
00089   virtual const VerboseObjectBase& setOStream(const RefCountPtr<FancyOStream> &oStream) const;
00090 
00092   virtual VerboseObjectBase& setLinePrefix(const std::string &linePrefix);
00093 
00095 
00097 
00098 
00103   virtual RefCountPtr<FancyOStream> getOStream() const;
00104 
00106   virtual std::string getLinePrefix() const;
00107 
00109 
00111 
00112 
00126   virtual OSTab getOSTab(const int tabs = 1, const std::string &linePrefix = "") const;
00127 
00129   
00130 private:
00131 
00132   mutable RefCountPtr<FancyOStream>   thisOStream_;
00133   std::string                         thisLinePrefix_;
00134 
00135   static RefCountPtr<FancyOStream>& privateDefaultOStream();
00136 
00137 };
00138 
00159 template<class ObjectType>
00160 class VerboseObject : virtual public VerboseObjectBase {
00161 public:
00162 
00164 
00165 
00170   static void setDefaultVerbLevel( const EVerbosityLevel defaultVerbLevel);
00171 
00173   static EVerbosityLevel getDefaultVerbLevel();
00174 
00176 
00178 
00179   
00182   explicit
00183   VerboseObject(
00184     const EVerbosityLevel              verbLevel = VERB_DEFAULT  // Note, this must be the same as the default value for defaultVerbLevel_
00185     ,const RefCountPtr<FancyOStream>   &oStream  = Teuchos::null
00186     );
00187   
00190   virtual void initializeVerboseObject(
00191     const EVerbosityLevel              verbLevel = VERB_DEFAULT  // Note, this must be the same as the default value for defaultVerbLevel_
00192     ,const RefCountPtr<FancyOStream>   &oStream  = Teuchos::null
00193     );
00194 
00196   virtual const VerboseObject& setVerbLevel(const EVerbosityLevel verbLevel) const;
00197 
00199 
00201 
00202 
00204   virtual EVerbosityLevel getVerbLevel() const;
00205 
00207   
00208 private:
00209   
00210   mutable EVerbosityLevel   thisVerbLevel_;
00211   
00212   static EVerbosityLevel&  privateDefaultVerbLevel();
00213 
00214 };
00215 
00219 template<class ObjectType>
00220 class VerboseObjectTempState {
00221 public:
00223   VerboseObjectTempState(
00224     const RefCountPtr<const VerboseObject<ObjectType> >    &verboseObject
00225     ,const RefCountPtr<FancyOStream>                       &newOStream
00226     ,const EVerbosityLevel                                 newVerbLevel
00227     )
00228     :verboseObject_(verboseObject)
00229     {
00230       if(verboseObject_.get()) {
00231         oldOStream_ = verboseObject_->getOStream();
00232         oldVerbLevel_ = verboseObject_->getVerbLevel();
00233         verboseObject_->setOStream(newOStream);
00234         verboseObject_->setVerbLevel(newVerbLevel);
00235       }
00236     }
00238   ~VerboseObjectTempState()
00239     {
00240       if(verboseObject_.get()) {
00241         verboseObject_->setOStream(oldOStream_);
00242         verboseObject_->setVerbLevel(oldVerbLevel_);
00243       }
00244     }
00245 private:
00246   RefCountPtr<const VerboseObject<ObjectType> >    verboseObject_;
00247   RefCountPtr<FancyOStream>                        oldOStream_;
00248   EVerbosityLevel                                  oldVerbLevel_;
00249   // Not defined and not to be called
00250   VerboseObjectTempState();
00251   VerboseObjectTempState(const VerboseObjectTempState&);
00252   VerboseObjectTempState& operator=(const VerboseObjectTempState&);
00253 };
00254 
00255 
00256 // //////////////////////////////////
00257 // Template defintions
00258 
00259 //
00260 // VerboseObject
00261 //
00262 
00263 // Public static member functions
00264 
00265 template<class ObjectType>
00266 void VerboseObject<ObjectType>::setDefaultVerbLevel( const EVerbosityLevel defaultVerbLevel)
00267 {
00268   privateDefaultVerbLevel() = defaultVerbLevel;
00269 }
00270 
00271 template<class ObjectType>
00272 EVerbosityLevel VerboseObject<ObjectType>::getDefaultVerbLevel()
00273 {
00274   return privateDefaultVerbLevel();
00275 }
00276 
00277 // Constructors/Initializers
00278 
00279 template<class ObjectType>
00280 VerboseObject<ObjectType>::VerboseObject(
00281   const EVerbosityLevel              verbLevel
00282   ,const RefCountPtr<FancyOStream>   &oStream
00283   )
00284 {
00285   this->initializeVerboseObject(verbLevel,oStream);
00286 }
00287 
00288 template<class ObjectType>
00289 void VerboseObject<ObjectType>::initializeVerboseObject(
00290   const EVerbosityLevel              verbLevel
00291   ,const RefCountPtr<FancyOStream>   &oStream
00292   )
00293 {
00294   thisVerbLevel_ = verbLevel;
00295   this->initializeVerboseObjectBase(oStream);
00296 }
00297 
00298 template<class ObjectType>
00299 const VerboseObject<ObjectType>&
00300 VerboseObject<ObjectType>::setVerbLevel(const EVerbosityLevel verbLevel) const
00301 {
00302   thisVerbLevel_ = verbLevel;
00303   return *this;
00304 }
00305 
00306 // Query functions
00307 
00308 template<class ObjectType>
00309 EVerbosityLevel VerboseObject<ObjectType>::getVerbLevel() const
00310 {
00311   if(thisVerbLevel_ == VERB_DEFAULT)
00312     return getDefaultVerbLevel();
00313   return thisVerbLevel_;
00314 }
00315 
00316 // Private static members
00317 
00318 template<class ObjectType>
00319 EVerbosityLevel& VerboseObject<ObjectType>::privateDefaultVerbLevel()
00320 {
00321   static EVerbosityLevel defaultVerbLevel = VERB_DEFAULT;
00322   return defaultVerbLevel;
00323 }
00324 
00325 } // namespace Teuchos
00326 
00327 #endif // TEUCHOS_VERBOSE_OBJECT_HPP

Generated on Thu Sep 18 12:30:31 2008 for Teuchos - Trilinos Tools Package by doxygen 1.3.9.1