|
Teuchos - Trilinos Tools Package Version of the Day
|
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_RCP.hpp" 00033 #include "Teuchos_FancyOStream.hpp" 00034 #include "Teuchos_VerbosityLevel.hpp" 00035 00036 00037 namespace Teuchos { 00038 00039 00053 class TEUCHOS_LIB_DLL_EXPORT VerboseObjectBase { 00054 public: 00055 00057 00058 00064 static void setDefaultOStream( const RCP<FancyOStream> &defaultOStream ); 00065 00067 static RCP<FancyOStream> getDefaultOStream(); 00068 00070 00072 00073 00075 virtual ~VerboseObjectBase() {} 00076 00079 explicit 00080 VerboseObjectBase( 00081 const RCP<FancyOStream> &oStream = Teuchos::null 00082 ); 00083 00086 virtual void initializeVerboseObjectBase( 00087 const RCP<FancyOStream> &oStream = Teuchos::null 00088 ); 00089 00095 virtual const VerboseObjectBase& setOStream( 00096 const RCP<FancyOStream> &oStream) const; 00097 00104 virtual const VerboseObjectBase& setOverridingOStream( 00105 const RCP<FancyOStream> &oStream) const; 00106 00108 virtual VerboseObjectBase& setLinePrefix(const std::string &linePrefix); 00109 00111 00113 00114 00118 virtual RCP<FancyOStream> getOStream() const; 00119 00126 virtual RCP<FancyOStream> getOverridingOStream() const; 00127 00129 virtual std::string getLinePrefix() const; 00130 00132 00134 00135 00149 virtual OSTab getOSTab(const int tabs = 1, const std::string &linePrefix = "") const; 00150 00152 00153 protected: 00154 00163 virtual void informUpdatedVerbosityState() const; 00164 00165 private: 00166 00167 std::string thisLinePrefix_; 00168 00169 //use pragmas to disable some false-positive warnings for windows sharedlibs export 00170 #ifdef _MSC_VER 00171 #pragma warning(push) 00172 #pragma warning(disable:4251) 00173 #endif 00174 mutable RCP<FancyOStream> thisOStream_; 00175 mutable RCP<FancyOStream> thisOverridingOStream_; 00176 #ifdef _MSC_VER 00177 #pragma warning(pop) 00178 #endif 00179 00180 static RCP<FancyOStream>& privateDefaultOStream(); 00181 00182 }; 00183 00184 00205 template<class ObjectType> 00206 class VerboseObject : virtual public VerboseObjectBase { 00207 public: 00208 00210 00211 00216 static void setDefaultVerbLevel( const EVerbosityLevel defaultVerbLevel); 00217 00219 static EVerbosityLevel getDefaultVerbLevel(); 00220 00222 00224 00225 00228 explicit 00229 VerboseObject( 00230 const EVerbosityLevel verbLevel = VERB_DEFAULT, // Note, this must be the same as the default value for defaultVerbLevel_ 00231 const RCP<FancyOStream> &oStream = Teuchos::null 00232 ); 00233 00236 virtual void initializeVerboseObject( 00237 const EVerbosityLevel verbLevel = VERB_DEFAULT, // Note, this must be the same as the default value for defaultVerbLevel_ 00238 const RCP<FancyOStream> &oStream = Teuchos::null 00239 ); 00240 00247 virtual const VerboseObject& setVerbLevel( 00248 const EVerbosityLevel verbLevel) const; 00249 00256 virtual const VerboseObject& setOverridingVerbLevel( 00257 const EVerbosityLevel verbLevel) const; 00258 00260 00262 00263 00265 virtual EVerbosityLevel getVerbLevel() const; 00266 00268 00269 private: 00270 00271 mutable EVerbosityLevel thisVerbLevel_; 00272 mutable EVerbosityLevel thisOverridingVerbLevel_; 00273 00274 static EVerbosityLevel& privateDefaultVerbLevel(); 00275 00276 }; 00277 00278 00282 template<class ObjectType> 00283 class VerboseObjectTempState { 00284 public: 00286 VerboseObjectTempState( 00287 const RCP<const VerboseObject<ObjectType> > &verboseObject, 00288 const RCP<FancyOStream> &newOStream, 00289 const EVerbosityLevel newVerbLevel 00290 ) 00291 :verboseObject_(verboseObject) 00292 { 00293 if(verboseObject_.get()) { 00294 oldOStream_ = verboseObject_->getOStream(); 00295 oldVerbLevel_ = verboseObject_->getVerbLevel(); 00296 verboseObject_->setOStream(newOStream); 00297 verboseObject_->setVerbLevel(newVerbLevel); 00298 } 00299 } 00301 ~VerboseObjectTempState() 00302 { 00303 if(verboseObject_.get()) { 00304 verboseObject_->setOStream(oldOStream_); 00305 verboseObject_->setVerbLevel(oldVerbLevel_); 00306 } 00307 } 00308 private: 00309 RCP<const VerboseObject<ObjectType> > verboseObject_; 00310 RCP<FancyOStream> oldOStream_; 00311 EVerbosityLevel oldVerbLevel_; 00312 // Not defined and not to be called 00313 VerboseObjectTempState(); 00314 VerboseObjectTempState(const VerboseObjectTempState&); 00315 VerboseObjectTempState& operator=(const VerboseObjectTempState&); 00316 }; 00317 00318 00319 // ////////////////////////////////// 00320 // Template defintions 00321 00322 00323 // 00324 // VerboseObject 00325 // 00326 00327 00328 // Public static member functions 00329 00330 00331 template<class ObjectType> 00332 void VerboseObject<ObjectType>::setDefaultVerbLevel( const EVerbosityLevel defaultVerbLevel) 00333 { 00334 privateDefaultVerbLevel() = defaultVerbLevel; 00335 } 00336 00337 00338 template<class ObjectType> 00339 EVerbosityLevel VerboseObject<ObjectType>::getDefaultVerbLevel() 00340 { 00341 return privateDefaultVerbLevel(); 00342 } 00343 00344 00345 // Constructors/Initializers 00346 00347 00348 template<class ObjectType> 00349 VerboseObject<ObjectType>::VerboseObject( 00350 const EVerbosityLevel verbLevel, 00351 const RCP<FancyOStream> &oStream 00352 ) 00353 : thisOverridingVerbLevel_(VERB_DEFAULT) 00354 { 00355 this->initializeVerboseObject(verbLevel,oStream); 00356 } 00357 00358 00359 template<class ObjectType> 00360 void VerboseObject<ObjectType>::initializeVerboseObject( 00361 const EVerbosityLevel verbLevel, 00362 const RCP<FancyOStream> &oStream 00363 ) 00364 { 00365 thisVerbLevel_ = verbLevel; 00366 this->initializeVerboseObjectBase(oStream); 00367 } 00368 00369 00370 template<class ObjectType> 00371 const VerboseObject<ObjectType>& 00372 VerboseObject<ObjectType>::setVerbLevel(const EVerbosityLevel verbLevel) const 00373 { 00374 thisVerbLevel_ = verbLevel; 00375 informUpdatedVerbosityState(); 00376 return *this; 00377 } 00378 00379 00380 template<class ObjectType> 00381 const VerboseObject<ObjectType>& 00382 VerboseObject<ObjectType>::setOverridingVerbLevel( 00383 const EVerbosityLevel verbLevel 00384 ) const 00385 { 00386 thisOverridingVerbLevel_ = verbLevel; 00387 informUpdatedVerbosityState(); 00388 return *this; 00389 } 00390 00391 00392 // Query functions 00393 00394 00395 template<class ObjectType> 00396 EVerbosityLevel VerboseObject<ObjectType>::getVerbLevel() const 00397 { 00398 if (VERB_DEFAULT != thisOverridingVerbLevel_) 00399 return thisOverridingVerbLevel_; 00400 if (VERB_DEFAULT == thisVerbLevel_) 00401 return getDefaultVerbLevel(); 00402 return thisVerbLevel_; 00403 } 00404 00405 00406 // Private static members 00407 00408 00409 template<class ObjectType> 00410 EVerbosityLevel& VerboseObject<ObjectType>::privateDefaultVerbLevel() 00411 { 00412 static EVerbosityLevel defaultVerbLevel = VERB_DEFAULT; 00413 return defaultVerbLevel; 00414 } 00415 00416 00417 } // namespace Teuchos 00418 00419 00420 #endif // TEUCHOS_VERBOSE_OBJECT_HPP
1.7.4