Teuchos - Trilinos Tools Package Version of the Day
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 // 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 TEUCHOS_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 
00218 template<class ObjectType>
00219 class VerboseObject : virtual public VerboseObjectBase {
00220 public:
00221 
00223 
00224 
00229   static void setDefaultVerbLevel( const EVerbosityLevel defaultVerbLevel);
00230 
00232   static EVerbosityLevel getDefaultVerbLevel();
00233 
00235 
00237 
00238   
00241   explicit
00242   VerboseObject(
00243     const EVerbosityLevel verbLevel = VERB_DEFAULT,  // Note, this must be the same as the default value for defaultVerbLevel_
00244     const RCP<FancyOStream> &oStream  = Teuchos::null
00245     );
00246   
00249   virtual void initializeVerboseObject(
00250     const EVerbosityLevel verbLevel = VERB_DEFAULT,  // Note, this must be the same as the default value for defaultVerbLevel_
00251     const RCP<FancyOStream> &oStream  = Teuchos::null
00252     );
00253 
00260   virtual const VerboseObject& setVerbLevel(
00261     const EVerbosityLevel verbLevel) const;
00262 
00269   virtual const VerboseObject& setOverridingVerbLevel(
00270     const EVerbosityLevel verbLevel) const;
00271 
00273 
00275 
00276 
00278   virtual EVerbosityLevel getVerbLevel() const;
00279 
00281   
00282 private:
00283   
00284   mutable EVerbosityLevel thisVerbLevel_;
00285   mutable EVerbosityLevel thisOverridingVerbLevel_;
00286   
00287   static EVerbosityLevel& privateDefaultVerbLevel();
00288 
00289 };
00290 
00291 
00295 template<class ObjectType>
00296 class VerboseObjectTempState {
00297 public:
00299   VerboseObjectTempState(
00300     const RCP<const VerboseObject<ObjectType> > &verboseObject,
00301     const RCP<FancyOStream> &newOStream,
00302     const EVerbosityLevel newVerbLevel
00303     )
00304     :verboseObject_(verboseObject)
00305     {
00306       if(verboseObject_.get()) {
00307         oldOStream_ = verboseObject_->getOStream();
00308         oldVerbLevel_ = verboseObject_->getVerbLevel();
00309         verboseObject_->setOStream(newOStream);
00310         verboseObject_->setVerbLevel(newVerbLevel);
00311       }
00312     }
00314   ~VerboseObjectTempState()
00315     {
00316       if(verboseObject_.get()) {
00317         verboseObject_->setOStream(oldOStream_);
00318         verboseObject_->setVerbLevel(oldVerbLevel_);
00319       }
00320     }
00321 private:
00322   RCP<const VerboseObject<ObjectType> > verboseObject_;
00323   RCP<FancyOStream> oldOStream_;
00324   EVerbosityLevel oldVerbLevel_;
00325   // Not defined and not to be called
00326   VerboseObjectTempState();
00327   VerboseObjectTempState(const VerboseObjectTempState&);
00328   VerboseObjectTempState& operator=(const VerboseObjectTempState&);
00329 };
00330 
00331 
00332 // //////////////////////////////////
00333 // Template defintions
00334 
00335 
00336 //
00337 // VerboseObject
00338 //
00339 
00340 
00341 // Public static member functions
00342 
00343 
00344 template<class ObjectType>
00345 void VerboseObject<ObjectType>::setDefaultVerbLevel( const EVerbosityLevel defaultVerbLevel)
00346 {
00347   privateDefaultVerbLevel() = defaultVerbLevel;
00348 }
00349 
00350 
00351 template<class ObjectType>
00352 EVerbosityLevel VerboseObject<ObjectType>::getDefaultVerbLevel()
00353 {
00354   return privateDefaultVerbLevel();
00355 }
00356 
00357 
00358 // Constructors/Initializers
00359 
00360 
00361 template<class ObjectType>
00362 VerboseObject<ObjectType>::VerboseObject(
00363   const EVerbosityLevel verbLevel,
00364   const RCP<FancyOStream> &oStream
00365   )
00366   : thisOverridingVerbLevel_(VERB_DEFAULT)
00367 {
00368   this->initializeVerboseObject(verbLevel,oStream);
00369 }
00370 
00371 
00372 template<class ObjectType>
00373 void VerboseObject<ObjectType>::initializeVerboseObject(
00374   const EVerbosityLevel verbLevel,
00375   const RCP<FancyOStream> &oStream
00376   )
00377 {
00378   thisVerbLevel_ = verbLevel;
00379   this->initializeVerboseObjectBase(oStream);
00380 }
00381 
00382 
00383 template<class ObjectType>
00384 const VerboseObject<ObjectType>&
00385 VerboseObject<ObjectType>::setVerbLevel(const EVerbosityLevel verbLevel) const
00386 {
00387   thisVerbLevel_ = verbLevel;
00388   informUpdatedVerbosityState();
00389   return *this;
00390 }
00391 
00392 
00393 template<class ObjectType>
00394 const VerboseObject<ObjectType>&
00395 VerboseObject<ObjectType>::setOverridingVerbLevel(
00396   const EVerbosityLevel verbLevel
00397   ) const
00398 {
00399   thisOverridingVerbLevel_ = verbLevel;
00400   informUpdatedVerbosityState();
00401   return *this;
00402 }
00403 
00404 
00405 // Query functions
00406 
00407 
00408 template<class ObjectType>
00409 EVerbosityLevel VerboseObject<ObjectType>::getVerbLevel() const
00410 {
00411   if (VERB_DEFAULT != thisOverridingVerbLevel_)
00412     return thisOverridingVerbLevel_;
00413   if (VERB_DEFAULT == thisVerbLevel_)
00414     return getDefaultVerbLevel();
00415   return thisVerbLevel_;
00416 }
00417 
00418 
00419 // Private static members
00420 
00421 
00422 template<class ObjectType>
00423 EVerbosityLevel& VerboseObject<ObjectType>::privateDefaultVerbLevel()
00424 {
00425   static EVerbosityLevel defaultVerbLevel = VERB_DEFAULT;
00426   return defaultVerbLevel;
00427 }
00428 
00429 
00430 } // namespace Teuchos
00431 
00432 
00433 #endif // TEUCHOS_VERBOSE_OBJECT_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines