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

Site Contact