SundanceObjectWithVerbosity.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 /* @HEADER@ */
00003 
00004 #ifndef SUNDANCE_OBJECTWITHVERBOSITY_H
00005 #define SUNDANCE_OBJECTWITHVERBOSITY_H
00006 
00007 #include "SundanceDefs.hpp"
00008 #include "Teuchos_ParameterList.hpp"
00009 #include "Teuchos_RefCountPtr.hpp"
00010 #include "SundanceParamUtils.hpp"
00011 #include "Teuchos_RefCountPtr.hpp"
00012 #include "Teuchos_FancyOStream.hpp"
00013 
00014 
00015 namespace Sundance
00016 {
00017 using Teuchos::RefCountPtr;
00018 using Teuchos::rcp;
00019 using Teuchos::FancyOStream;
00020 using Teuchos::ParameterList;
00021 using Teuchos::ParameterEntry;
00022 
00023 
00024 /** 
00025  * Defines traits for getting default verbosity parameters from a class
00026  */
00027 template <class X>
00028 class VerbosityTraits
00029 {
00030 public:
00031   static RCP<ParameterList> defaultVerbParams()
00032     {return X::defaultVerbParams();}
00033 };
00034 
00035 
00036 /** 
00037  * Abstract interface for getting/setting verbosity levels.
00038  */
00039 class ObjectWithVerbosityBase
00040 {
00041 public:
00042   /** */
00043   virtual ~ObjectWithVerbosityBase(){}
00044 
00045   /** */
00046   virtual int verb() const = 0 ;
00047 
00048   /** */
00049   virtual void setVerbosity(int verb) = 0 ;
00050 };
00051 
00052 
00053 /** 
00054  * Simple implementation of ObjectWithVerbosityBase interface
00055  */
00056 class DefaultObjectWithVerbosity : public ObjectWithVerbosityBase
00057 {
00058 public:
00059   /** */
00060   DefaultObjectWithVerbosity(int verb=0) : verb_(verb) {}
00061 
00062   /** */
00063   virtual ~DefaultObjectWithVerbosity(){}
00064 
00065   /** */
00066   virtual int verb() const {return verb_;}
00067 
00068   /** */
00069   virtual void setVerbosity(int verb) {verb_ = verb;}
00070 private:
00071   int verb_;
00072 };
00073 
00074 
00075 /**
00076  * ObjectWithClassVerbosity and the related verbosity() method
00077  * provide a method for getting/setting
00078  * verbosity flags for entire classes.
00079  *
00080  * You can set verbosity for a single instance of a class, or for
00081  * the whole class. To set for an instance, use the verbosity()
00082  * member function, for example,
00083  * \code
00084  * Mesh mesh1 = reader1.getMesh();
00085  * Mesh mesh2 = reader2.getMesh();
00086  * Mesh mesh3 = reader3.getMesh();
00087  * mesh1.verbosity() = 3;
00088  * \endcode
00089  * which sets the verbosity of <tt>mesh1</tt> to 3 and leaves
00090  * those of <tt>mesh2</tt> and <tt>mesh3</tt> unchanged.
00091  *
00092  * Alternatively, you can set a default verbosity for an entire
00093  * class, for example,
00094  * \code
00095  * Mesh mesh1 = reader1.getMesh();
00096  * Mesh mesh2 = reader2.getMesh();
00097  * Mesh mesh3 = reader3.getMesh();
00098  * mesh1.verbosity() = 3;
00099  * verbosity<Mesh>() = 2;
00100  * \endcode
00101  * which sets the default verbosity to 2. Since <tt>mesh1</tt>
00102  * has its own verbosity setting of 3, 
00103  * it will use it rather than the
00104  * default, but <tt>mesh2</tt> and <tt>mesh3</tt> will use 2.
00105  * 
00106  */
00107 template <class X>
00108 class ObjectWithClassVerbosity : public DefaultObjectWithVerbosity
00109 {
00110 public:
00111   /** \deprecated Construct, starting silent */
00112   ObjectWithClassVerbosity(int verb=classVerbosity())
00113     : DefaultObjectWithVerbosity(verb) {;}
00114 
00115   /** \deprecated Writeable access to the default verbosity for the class */
00116   static int& classVerbosity() 
00117     {
00118       static int rtn = 0;
00119       return rtn;
00120     }
00121 
00122 };
00123 
00124 
00125 
00126 /** 
00127  * \relates ObjectWithClassVerbosity
00128  * Global method for setting verbosity of a class
00129  */
00130 template <class X> int& verbosity() 
00131 {
00132   return X::classVerbosity();
00133 }
00134 
00135 template <class X>
00136 class ParameterControlledObjectWithVerbosity 
00137   : public ObjectWithClassVerbosity<X>
00138 {
00139 public:
00140   /** \deprecated Construct, starting silent */
00141   ParameterControlledObjectWithVerbosity() : ObjectWithClassVerbosity<X>() {;}
00142 
00143   /** Construct with a parameter list controlling the verbosity settings */
00144   ParameterControlledObjectWithVerbosity(const std::string& objName, const ParameterList& p)
00145     : ObjectWithClassVerbosity<X>(),
00146     verbControlParams_() 
00147     {
00148       RCP<ParameterList> defaults = VerbosityTraits<X>::defaultVerbParams();
00149       TEST_FOR_EXCEPTION(defaults->name() != objName, std::runtime_error,
00150         "mismatched ParameterList names for verbosity control: expected "
00151         << defaults->name() << ", got " << objName);
00152       TEST_FOR_EXCEPTION(defaults->name() != p.name(), std::runtime_error,
00153         "mismatched ParameterList names for verbosity control: expected "
00154         << defaults->name() << ", got " << p.name());
00155       verbControlParams_ = rcp(new ParameterList(mergeParams(*defaults, p)));
00156     }
00157 
00158   /** */
00159   int verbLevel(const std::string& context) const 
00160     {
00161       const ParameterEntry* pe = verbControlParams_->getEntryPtr(context);
00162       TEST_FOR_EXCEPTION(pe==0, std::runtime_error,
00163         "parameter with name \"" << context << "\" not found in verbosity "
00164         "control parameter list " << verbControlParams_);
00165       TEST_FOR_EXCEPTION(pe->getAny().type() != typeid(int),
00166         std::runtime_error,
00167         "context parameter name \"" 
00168         << context << "\" does not have an integer value in verbosity "
00169         "control parameter list " << verbControlParams_);
00170 
00171       return Teuchos::any_cast<int>(pe->getAny());
00172     }
00173 
00174   /** */
00175   const ParameterList& verbSublist(const std::string& name) const 
00176     {
00177       TEST_FOR_EXCEPTION(!verbControlParams_->isSublist(name),
00178         std::runtime_error,
00179         "context parameter name \"" 
00180         << name << "\" is not a sublist in verbosity "
00181         "control parameter list " << *verbControlParams_);
00182 
00183       return verbControlParams_->sublist(name);
00184     }
00185 
00186   /** */
00187   ParameterList mergeParams(const ParameterList& pDef, const ParameterList& pIn) const
00188     {
00189       return mergeParamLists(pDef, pIn);
00190     }
00191        
00192   /** */
00193   const ParameterList params() const {return *verbControlParams_;}
00194 
00195   /** */
00196   RCP<ParameterList> modifiableParams() const {return verbControlParams_;}
00197 private:
00198   RCP<ParameterList> verbControlParams_;
00199 };
00200 
00201 
00202 
00203 }
00204 
00205 
00206 
00207 
00208 
00209 #endif

Site Contact