Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
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     oldVerbLevel_(VERB_DEFAULT)
00321     {
00322       if(verboseObject_.get()) {
00323         oldOStream_ = verboseObject_->getOStream();
00324         oldVerbLevel_ = verboseObject_->getVerbLevel();
00325         verboseObject_->setOStream(newOStream);
00326         verboseObject_->setVerbLevel(newVerbLevel);
00327       }
00328     }
00330   ~VerboseObjectTempState()
00331     {
00332       if(verboseObject_.get()) {
00333         verboseObject_->setOStream(oldOStream_);
00334         verboseObject_->setVerbLevel(oldVerbLevel_);
00335       }
00336     }
00337 private:
00338   RCP<const VerboseObject<ObjectType> > verboseObject_;
00339   RCP<FancyOStream> oldOStream_;
00340   EVerbosityLevel oldVerbLevel_;
00341   // Not defined and not to be called
00342   VerboseObjectTempState();
00343   VerboseObjectTempState(const VerboseObjectTempState&);
00344   VerboseObjectTempState& operator=(const VerboseObjectTempState&);
00345 };
00346 
00347 
00348 // //////////////////////////////////
00349 // Template defintions
00350 
00351 
00352 //
00353 // VerboseObject
00354 //
00355 
00356 
00357 // Public static member functions
00358 
00359 
00360 template<class ObjectType>
00361 void VerboseObject<ObjectType>::setDefaultVerbLevel( const EVerbosityLevel defaultVerbLevel)
00362 {
00363   privateDefaultVerbLevel() = defaultVerbLevel;
00364 }
00365 
00366 
00367 template<class ObjectType>
00368 EVerbosityLevel VerboseObject<ObjectType>::getDefaultVerbLevel()
00369 {
00370   return privateDefaultVerbLevel();
00371 }
00372 
00373 
00374 // Constructors/Initializers
00375 
00376 
00377 template<class ObjectType>
00378 VerboseObject<ObjectType>::VerboseObject(
00379   const EVerbosityLevel verbLevel,
00380   const RCP<FancyOStream> &oStream
00381   )
00382   : thisOverridingVerbLevel_(VERB_DEFAULT)
00383 {
00384   this->initializeVerboseObject(verbLevel,oStream);
00385 }
00386 
00387 
00388 template<class ObjectType>
00389 void VerboseObject<ObjectType>::initializeVerboseObject(
00390   const EVerbosityLevel verbLevel,
00391   const RCP<FancyOStream> &oStream
00392   )
00393 {
00394   thisVerbLevel_ = verbLevel;
00395   this->initializeVerboseObjectBase(oStream);
00396 }
00397 
00398 
00399 template<class ObjectType>
00400 const VerboseObject<ObjectType>&
00401 VerboseObject<ObjectType>::setVerbLevel(const EVerbosityLevel verbLevel) const
00402 {
00403   thisVerbLevel_ = verbLevel;
00404   informUpdatedVerbosityState();
00405   return *this;
00406 }
00407 
00408 
00409 template<class ObjectType>
00410 const VerboseObject<ObjectType>&
00411 VerboseObject<ObjectType>::setOverridingVerbLevel(
00412   const EVerbosityLevel verbLevel
00413   ) const
00414 {
00415   thisOverridingVerbLevel_ = verbLevel;
00416   informUpdatedVerbosityState();
00417   return *this;
00418 }
00419 
00420 
00421 // Query functions
00422 
00423 
00424 template<class ObjectType>
00425 EVerbosityLevel VerboseObject<ObjectType>::getVerbLevel() const
00426 {
00427   if (VERB_DEFAULT != thisOverridingVerbLevel_)
00428     return thisOverridingVerbLevel_;
00429   if (VERB_DEFAULT == thisVerbLevel_)
00430     return getDefaultVerbLevel();
00431   return thisVerbLevel_;
00432 }
00433 
00434 
00435 // Private static members
00436 
00437 
00438 template<class ObjectType>
00439 EVerbosityLevel& VerboseObject<ObjectType>::privateDefaultVerbLevel()
00440 {
00441   static EVerbosityLevel defaultVerbLevel = VERB_DEFAULT;
00442   return defaultVerbLevel;
00443 }
00444 
00445 
00446 } // namespace Teuchos
00447 
00448 
00449 #endif // TEUCHOS_VERBOSE_OBJECT_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines