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 "PlayaObjectWithVerbosity.hpp"
00009 #include "SundanceParamUtils.hpp"
00010 #include "Teuchos_ParameterList.hpp"
00011 
00012 namespace Sundance
00013 {
00014 
00015 using Teuchos::ParameterList;
00016 using Teuchos::RCP;
00017 using Teuchos::rcp;
00018 using Teuchos::FancyOStream;
00019 using Teuchos::ParameterEntry;
00020 
00021 
00022 /** 
00023  * Defines traits for getting default verbosity parameters from a class
00024  */
00025 template <class X>
00026 class VerbosityTraits
00027 {
00028 public:
00029   static RCP<ParameterList> defaultVerbParams()
00030     {return X::defaultVerbParams();}
00031 };
00032 
00033 
00034 
00035 /**
00036  * ObjectWithClassVerbosity and the related verbosity() method
00037  * provide a method for getting/setting
00038  * verbosity flags for entire classes.
00039  *
00040  * You can set verbosity for a single instance of a class, or for
00041  * the whole class. To set for an instance, use the verbosity()
00042  * member function, for example,
00043  * \code
00044  * Mesh mesh1 = reader1.getMesh();
00045  * Mesh mesh2 = reader2.getMesh();
00046  * Mesh mesh3 = reader3.getMesh();
00047  * mesh1.verbosity() = 3;
00048  * \endcode
00049  * which sets the verbosity of <tt>mesh1</tt> to 3 and leaves
00050  * those of <tt>mesh2</tt> and <tt>mesh3</tt> unchanged.
00051  *
00052  * Alternatively, you can set a default verbosity for an entire
00053  * class, for example,
00054  * \code
00055  * Mesh mesh1 = reader1.getMesh();
00056  * Mesh mesh2 = reader2.getMesh();
00057  * Mesh mesh3 = reader3.getMesh();
00058  * mesh1.verbosity() = 3;
00059  * verbosity<Mesh>() = 2;
00060  * \endcode
00061  * which sets the default verbosity to 2. Since <tt>mesh1</tt>
00062  * has its own verbosity setting of 3, 
00063  * it will use it rather than the
00064  * default, but <tt>mesh2</tt> and <tt>mesh3</tt> will use 2.
00065  * 
00066  */
00067 template <class X>
00068 class ObjectWithClassVerbosity : public Playa::ObjectWithVerbosity
00069 {
00070 public:
00071   /** \deprecated Construct, starting silent */
00072   ObjectWithClassVerbosity(int verb=classVerbosity())
00073     : Playa::ObjectWithVerbosity(verb) {;}
00074 
00075   /** \deprecated Writeable access to the default verbosity for the class */
00076   static int& classVerbosity() 
00077     {
00078       static int rtn = 0;
00079       return rtn;
00080     }
00081 
00082 };
00083 
00084 
00085 
00086 /** 
00087  * \relates ObjectWithClassVerbosity
00088  * Global method for setting verbosity of a class
00089  */
00090 template <class X> int& verbosity() 
00091 {
00092   return X::classVerbosity();
00093 }
00094 
00095 template <class X>
00096 class ParameterControlledObjectWithVerbosity 
00097   : public ObjectWithClassVerbosity<X>
00098 {
00099 public:
00100   /** \deprecated Construct, starting silent */
00101   ParameterControlledObjectWithVerbosity() : ObjectWithClassVerbosity<X>() {;}
00102 
00103   /** Construct with a parameter list controlling the verbosity settings */
00104   ParameterControlledObjectWithVerbosity(const std::string& objName, const ParameterList& p)
00105     : ObjectWithClassVerbosity<X>(),
00106       verbControlParams_() 
00107     {
00108       RCP<ParameterList> defaults = VerbosityTraits<X>::defaultVerbParams();
00109       TEUCHOS_TEST_FOR_EXCEPTION(defaults->name() != objName, std::runtime_error,
00110         "mismatched ParameterList names for verbosity control: expected "
00111         << defaults->name() << ", got " << objName);
00112       TEUCHOS_TEST_FOR_EXCEPTION(defaults->name() != p.name(), std::runtime_error,
00113         "mismatched ParameterList names for verbosity control: expected "
00114         << defaults->name() << ", got " << p.name());
00115       verbControlParams_ = rcp(new ParameterList(mergeParams(*defaults, p)));
00116     }
00117 
00118   /** */
00119   int verbLevel(const std::string& context) const 
00120     {
00121       const ParameterEntry* pe = verbControlParams_->getEntryPtr(context);
00122       TEUCHOS_TEST_FOR_EXCEPTION(pe==0, std::runtime_error,
00123         "parameter with name \"" << context << "\" not found in verbosity "
00124         "control parameter list " << verbControlParams_);
00125       TEUCHOS_TEST_FOR_EXCEPTION(pe->getAny().type() != typeid(int),
00126         std::runtime_error,
00127         "context parameter name \"" 
00128         << context << "\" does not have an integer value in verbosity "
00129         "control parameter list " << verbControlParams_);
00130 
00131       return Teuchos::any_cast<int>(pe->getAny());
00132     }
00133 
00134   /** */
00135   const ParameterList& verbSublist(const std::string& name) const 
00136     {
00137       TEUCHOS_TEST_FOR_EXCEPTION(!verbControlParams_->isSublist(name),
00138         std::runtime_error,
00139         "context parameter name \"" 
00140         << name << "\" is not a sublist in verbosity "
00141         "control parameter list " << *verbControlParams_);
00142 
00143       return verbControlParams_->sublist(name);
00144     }
00145 
00146   /** */
00147   ParameterList mergeParams(const ParameterList& pDef, const ParameterList& pIn) const
00148     {
00149       return mergeParamLists(pDef, pIn);
00150     }
00151        
00152   /** */
00153   const ParameterList params() const {return *verbControlParams_;}
00154 
00155   /** */
00156   RCP<ParameterList> modifiableParams() const {return verbControlParams_;}
00157 private:
00158   RCP<ParameterList> verbControlParams_;
00159 };
00160 
00161 
00162 
00163 }
00164 
00165 
00166 
00167 
00168 
00169 #endif

Site Contact