Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_VerboseObject.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 // 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 #ifndef TEUCHOS_VERBOSE_OBJECT_HPP
00043 #define TEUCHOS_VERBOSE_OBJECT_HPP
00044 
00045 #include "Teuchos_RCP.hpp"
00046 #include "Teuchos_FancyOStream.hpp"
00047 #include "Teuchos_VerbosityLevel.hpp"
00048 
00049 
00050 namespace Teuchos {
00051 
00052 
00066 class TEUCHOSCORE_LIB_DLL_EXPORT VerboseObjectBase {
00067 public:
00068 
00070 
00071 
00077   static void setDefaultOStream( const RCP<FancyOStream> &defaultOStream );
00078 
00080   static RCP<FancyOStream> getDefaultOStream();
00081 
00083 
00085 
00086   
00088   virtual ~VerboseObjectBase();
00089 
00092   explicit
00093   VerboseObjectBase(
00094     const RCP<FancyOStream> &oStream = Teuchos::null
00095     );
00096   
00099   virtual void initializeVerboseObjectBase(
00100     const RCP<FancyOStream> &oStream = Teuchos::null
00101     );
00102 
00108   virtual const VerboseObjectBase& setOStream(
00109     const RCP<FancyOStream> &oStream) const;
00110 
00117   virtual const VerboseObjectBase& setOverridingOStream(
00118     const RCP<FancyOStream> &oStream) const;
00119 
00121   virtual VerboseObjectBase& setLinePrefix(const std::string &linePrefix);
00122 
00124 
00126 
00127 
00131   virtual RCP<FancyOStream> getOStream() const;
00132 
00139   virtual RCP<FancyOStream> getOverridingOStream() const;
00140 
00142   virtual std::string getLinePrefix() const;
00143 
00145 
00147 
00148 
00162   virtual OSTab getOSTab(const int tabs = 1, const std::string &linePrefix = "") const;
00163 
00165 
00166 protected:
00167   
00176   virtual void informUpdatedVerbosityState() const;
00177 
00178 private:
00179 
00180   std::string thisLinePrefix_;
00181 
00182 //use pragmas to disable some false-positive warnings for windows sharedlibs export
00183 #ifdef _MSC_VER
00184 #pragma warning(push)
00185 #pragma warning(disable:4251)
00186 #endif
00187   mutable RCP<FancyOStream> thisOStream_;
00188   mutable RCP<FancyOStream> thisOverridingOStream_;
00189 #ifdef _MSC_VER
00190 #pragma warning(pop)
00191 #endif 
00192 
00193   static RCP<FancyOStream>& privateDefaultOStream();
00194 
00195 };
00196 
00197 
00233 template<class ObjectType>
00234 class VerboseObject : virtual public VerboseObjectBase {
00235 public:
00237 
00238 
00243   static void setDefaultVerbLevel( const EVerbosityLevel defaultVerbLevel);
00244 
00246   static EVerbosityLevel getDefaultVerbLevel();
00247 
00249 
00250 
00251   
00253   explicit
00254   VerboseObject(
00255     const EVerbosityLevel verbLevel = VERB_DEFAULT,  // Note, this must be the same as the default value for defaultVerbLevel_
00256     const RCP<FancyOStream> &oStream  = Teuchos::null
00257     );
00258   
00266   virtual void initializeVerboseObject(
00267     const EVerbosityLevel verbLevel = VERB_DEFAULT,  
00268     const RCP<FancyOStream> &oStream  = Teuchos::null
00269     );
00270 
00276   virtual const VerboseObject& setVerbLevel(
00277     const EVerbosityLevel verbLevel) const;
00278 
00285   virtual const VerboseObject& setOverridingVerbLevel(
00286     const EVerbosityLevel verbLevel) const;
00287 
00289 
00290 
00291 
00293   virtual EVerbosityLevel getVerbLevel() const;
00294 
00296   
00297 private:
00298   
00299   mutable EVerbosityLevel thisVerbLevel_;
00300   mutable EVerbosityLevel thisOverridingVerbLevel_;
00301   
00302   static EVerbosityLevel& privateDefaultVerbLevel();
00303 
00304 };
00305 
00306 
00310 template<class ObjectType>
00311 class VerboseObjectTempState {
00312 public:
00314   VerboseObjectTempState(
00315     const RCP<const VerboseObject<ObjectType> > &verboseObject,
00316     const RCP<FancyOStream> &newOStream,
00317     const EVerbosityLevel newVerbLevel
00318     )
00319     :verboseObject_(verboseObject)
00320     {
00321       if(verboseObject_.get()) {
00322         oldOStream_ = verboseObject_->getOStream();
00323         oldVerbLevel_ = verboseObject_->getVerbLevel();
00324         verboseObject_->setOStream(newOStream);
00325         verboseObject_->setVerbLevel(newVerbLevel);
00326       }
00327     }
00329   ~VerboseObjectTempState()
00330     {
00331       if(verboseObject_.get()) {
00332         verboseObject_->setOStream(oldOStream_);
00333         verboseObject_->setVerbLevel(oldVerbLevel_);
00334       }
00335     }
00336 private:
00337   RCP<const VerboseObject<ObjectType> > verboseObject_;
00338   RCP<FancyOStream> oldOStream_;
00339   EVerbosityLevel oldVerbLevel_;
00340   // Not defined and not to be called
00341   VerboseObjectTempState();
00342   VerboseObjectTempState(const VerboseObjectTempState&);
00343   VerboseObjectTempState& operator=(const VerboseObjectTempState&);
00344 };
00345 
00346 
00347 // //////////////////////////////////
00348 // Template defintions
00349 
00350 
00351 //
00352 // VerboseObject
00353 //
00354 
00355 
00356 // Public static member functions
00357 
00358 
00359 template<class ObjectType>
00360 void VerboseObject<ObjectType>::setDefaultVerbLevel( const EVerbosityLevel defaultVerbLevel)
00361 {
00362   privateDefaultVerbLevel() = defaultVerbLevel;
00363 }
00364 
00365 
00366 template<class ObjectType>
00367 EVerbosityLevel VerboseObject<ObjectType>::getDefaultVerbLevel()
00368 {
00369   return privateDefaultVerbLevel();
00370 }
00371 
00372 
00373 // Constructors/Initializers
00374 
00375 
00376 template<class ObjectType>
00377 VerboseObject<ObjectType>::VerboseObject(
00378   const EVerbosityLevel verbLevel,
00379   const RCP<FancyOStream> &oStream
00380   )
00381   : thisOverridingVerbLevel_(VERB_DEFAULT)
00382 {
00383   this->initializeVerboseObject(verbLevel,oStream);
00384 }
00385 
00386 
00387 template<class ObjectType>
00388 void VerboseObject<ObjectType>::initializeVerboseObject(
00389   const EVerbosityLevel verbLevel,
00390   const RCP<FancyOStream> &oStream
00391   )
00392 {
00393   thisVerbLevel_ = verbLevel;
00394   this->initializeVerboseObjectBase(oStream);
00395 }
00396 
00397 
00398 template<class ObjectType>
00399 const VerboseObject<ObjectType>&
00400 VerboseObject<ObjectType>::setVerbLevel(const EVerbosityLevel verbLevel) const
00401 {
00402   thisVerbLevel_ = verbLevel;
00403   informUpdatedVerbosityState();
00404   return *this;
00405 }
00406 
00407 
00408 template<class ObjectType>
00409 const VerboseObject<ObjectType>&
00410 VerboseObject<ObjectType>::setOverridingVerbLevel(
00411   const EVerbosityLevel verbLevel
00412   ) const
00413 {
00414   thisOverridingVerbLevel_ = verbLevel;
00415   informUpdatedVerbosityState();
00416   return *this;
00417 }
00418 
00419 
00420 // Query functions
00421 
00422 
00423 template<class ObjectType>
00424 EVerbosityLevel VerboseObject<ObjectType>::getVerbLevel() const
00425 {
00426   if (VERB_DEFAULT != thisOverridingVerbLevel_)
00427     return thisOverridingVerbLevel_;
00428   if (VERB_DEFAULT == thisVerbLevel_)
00429     return getDefaultVerbLevel();
00430   return thisVerbLevel_;
00431 }
00432 
00433 
00434 // Private static members
00435 
00436 
00437 template<class ObjectType>
00438 EVerbosityLevel& VerboseObject<ObjectType>::privateDefaultVerbLevel()
00439 {
00440   static EVerbosityLevel defaultVerbLevel = VERB_DEFAULT;
00441   return defaultVerbLevel;
00442 }
00443 
00444 
00445 } // namespace Teuchos
00446 
00447 
00448 #endif // TEUCHOS_VERBOSE_OBJECT_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines