Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AlgorithmA.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "AlgorithmA.hpp"
13 
14 
15 // This is a typical function that would be present in Trilinos right now what
16 // does not know about FancyOStream and does not derive from VerboseObject.
17 // However, because of the magic of FancyOStream, this output will be indented
18 // correctly!
19 void someDumbFunction( std::ostream &out, const std::string &indentSpacer )
20 {
21  out << "\nEntering someDumbFunction(...)\n";
22  {
23  out << std::endl << indentSpacer << "I am \"dumb\" code that knows nothing of FancyOStream and does indenting manually! ...\n";
24  }
25  out << "\nLeaving someDumbFunction(...)\n";
26  // Note that this output will be indented correctly even through it knows nothing of FancyOStream
27 }
28 
29 // This is a function who's interface was written before there was a
30 // FancyOStream and therefore is written in terms of std::ostream. However,
31 // in this case the implementation has been modifed to use FancyOStream as
32 // shown.
33 void someLessDumbFunction( std::ostream &out_arg )
34 {
35  using Teuchos::OSTab;
36  // Get a FancyOStream from out_arg or create a new one ...
38  out = Teuchos::getFancyOStream(Teuchos::rcp(&out_arg,false));
39  // Do our tab indent and our name.
40  OSTab tab(out,1,"LDUMBALGO");
41  *out << "\nEntering someLessDumbFunction(...)\n";
42  {
43  Teuchos::OSTab(out_arg).o()
44  << std::endl << "I am less \"dumb\" code that knows about FancyOStream but my interface does not support it directly! ...\n";
45  *Teuchos::tab(out)
46  << std::endl << "Another print from this less \"dumb\" code ...\n";
47  }
48  *out << "\nLeaving someLessDumbFunction(...)\n";
49 }
50 
51 
52 namespace {
53 
54 const std::string AlgoType_name = "Algo Type";
55 const std::string AlgoType_default = "Bob";
56 
57 const std::string AlgoTol_name = "Algo Tol";
58 const double AlgoTol_default = 1e-5;
59 
60 } // namespace
61 
62 
63 const std::string AlgorithmA::toString( AlgorithmA::EAlgoType algoType )
64 {
65  switch(algoType) {
66  case ALGO_BOB: return "Bob";
67  case ALGO_JOHN: return "John";
68  case ALGO_HARRY: return "Harry";
69  default: TEUCHOS_TEST_FOR_EXCEPT("Should never get here!");
70  }
71  return ""; // never be called!
72 }
73 
74 
76  : algoType_(ALGO_BOB), algoTol_(AlgoTol_default)
77 {
78  this->setLinePrefix("ALGO_A"); // I tell me who I am for line prefix outputting
79 }
80 
81 
84  )
85 {
86  TEUCHOS_TEST_FOR_EXCEPT(is_null(paramList));
87  // Validate and set the parameter defaults. Here, the parameters are
88  // validated and the state of *this is not changed unless the parameter
89  // validation succeeds. Also, any validators that are defined for various
90  // parameters are passed along so that they can be used in extracting
91  // values!
93  paramList_ = paramList;
94  // Get the enum value for the algorithm type. Here, the actual type stored
95  // for the algorithm type in the parameter list is an std::string but this
96  // helper function does all the work of extracting the validator set in
97  // getValidParameters() and set on *paramList_ through the
98  // validateParametersAndSetDefaults(...) function above!
99  algoType_ = Teuchos::getIntegralValue<EAlgoType>(*paramList_,AlgoType_name);
100  // Get the tolerance for the algorithm. Here, the actual type of the
101  // parameter stored on input could be many different types. Here, I can
102  // just assume that it is a double since it would have been converted to a
103  // double above in validateParametersAndSetDefaults(...).
104  algoTol_ = Teuchos::getParameter<double>(*paramList_,AlgoTol_name);
105  // Read the sublist for verbosity settings.
106  Teuchos::readVerboseObjectSublist(&*paramList_,this);
107 #ifdef TEUCHOS_DEBUG
108  paramList_->validateParameters(*this->getValidParameters());
109 #endif
110 }
111 
112 
115 {
116  return paramList_;
117 }
118 
119 
122 {
125  return paramList;
126 }
127 
128 
131 {
132  return paramList_;
133 }
134 
135 
138 {
140  using Teuchos::setStringToIntegralParameter;
141  using Teuchos::tuple;
142  static RCP<const ParameterList> validParams;
143  if (is_null(validParams)) {
144  RCP<ParameterList>
145  pl = Teuchos::rcp(new ParameterList("AlgorithmA"));
146  setStringToIntegralParameter<EAlgoType>(
147  AlgoType_name, AlgoType_default,
148  "The algorithm type to use",
149  tuple<std::string>("Bob", "John", "Harry"),
150  tuple<EAlgoType>(ALGO_BOB, ALGO_JOHN, ALGO_HARRY),
151  &*pl
152  );
153  Teuchos::setDoubleParameter(
154  AlgoTol_name, AlgoTol_default,
155  "The tolerance for the algorithm.",
156  &*pl
157  );
158  Teuchos::setupVerboseObjectSublist(&*pl);
159  validParams = pl;
160  }
161  return validParams;
162 }
163 
164 
166 {
167  using Teuchos::OSTab;
168  // Get the verbosity that we are going to use
169  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
170  // Here I grab the stream that I will use for outputting. It is a good
171  // idea to grab the RCP to this object just to be safe.
173  // Here I set my line prefix and a single indent. The convention will
174  // be that a called function will set its own indent. This convention makes
175  // the most sense.
176  OSTab tab = this->getOSTab(); // This sets the line prefix and adds one tab
177  if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW,true))
178  *out << "\nEntering AlgorithmA::doAlgorithm() with verbLevel="<<Teuchos::toString(verbLevel)<<"\n";
179  {
180  // Here I use a simple macro for the typical case of one tab indent to
181  // save typing. The idea is that this should be as easy to write as
182  // OSTab tab; but is more general.
184  if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW,true))
185  *out
186  << "\nI am \"smart\" code that knows about FancyOStream and OSTab ...\n"
187  << "\nDoing algorithm of type \""<<toString(algoType_)<<"\""
188  << "\nUsing tolerance of " << algoTol_ << "\n";
189  {
190  // Here I temporaraly turn off tabbing so that I can print an imporant warning message.
191  OSTab tab2 = this->getOSTab(OSTab::DISABLE_TABBING);
192  if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW,true))
193  *out << "\n***\n*** Warning, I am doing something very dangerous so watch out!!!\n***\n";
194  }
195  if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW,true))
196  *out << "\nHere I am doing some more stuff and printing with indenting turned back on!\n";
197  {
198  // Here I am going to be calling a dumb piece of code that does not
199  // know about the FancyOStream system and will not use tabs or
200  // anything like that. There is a lot of code in Trilinos that
201  // falls in this category. The first thing I do is manually indent
202  // the stream one tab and set a line prefix for the dumb code since
203  // it may not do this itself.
204  OSTab tab2 = this->getOSTab(1,"DUMBALGO");
205  // Now a Pass in the updated FancyOStream object, which is properly
206  // indented now, through the std::ostream interface. I also pass in
207  // the std::string that is being used for creating tabs. The output from
208  // this function will be indented correctly without the dumb code
209  // knowing it!
210  someDumbFunction(*out,out->getTabIndentStr());
211  }
212  // Here I am calling a less dumb piece of code who's interface does
213  // not support FancyOStream but the implementation does. Note that
214  // this function also follows the convention of doing an initial
215  // indent.
216  someLessDumbFunction(*out);
217  }
218  if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW,true))
219  *out << "\nLeaving AlgorithmA::doAlgorithm()\n";
220 }
EVerbosityLevel
Verbosity level.
bool is_null(const boost::shared_ptr< T > &p)
Returns true if p.get()==NULL.
Generate only a minimal amount of output.
void setParameterList(Teuchos::RCP< Teuchos::ParameterList > const &paramList)
Set parameters from a parameter list and return with default values.
Definition: AlgorithmA.cpp:82
EAlgoType algoType_
Definition: AlgorithmA.hpp:63
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList()
Unset the parameter list that was set using setParameterList().
Definition: AlgorithmA.cpp:121
#define TEUCHOS_OSTAB
T * get() const
Get the raw C++ pointer to the underlying object.
basic_OSTab< char > OSTab
const std::basic_string< char_type, traits_type > & getTabIndentStr() const
Get the tab indent string.
virtual VerboseObjectBase & setLinePrefix(const std::string &linePrefix)
Set line prefix name for this object.
void someDumbFunction(std::ostream &out, const std::string &indentSpacer)
Definition: AlgorithmA.cpp:19
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Return a ParameterList containing all of the valid parameters that this-&gt;setParameterList(...) will accept, along with any validators.
Definition: AlgorithmA.cpp:137
std::string toString(const HashSet< Key > &h)
virtual OSTab getOSTab(const int tabs=1, const std::string &linePrefix="") const
Create a tab object which sets the number of tabs and optionally the line prefix. ...
Teuchos::RCP< const Teuchos::ParameterList > getParameterList() const
Get const version of the parameter list that was set using setParameterList().
Definition: AlgorithmA.cpp:130
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
Validate the parameters in this list given valid selections in the input list and set defaults for th...
A list of parameters of arbitrary type.
TEUCHOSCORE_LIB_DLL_EXPORT bool includesVerbLevel(const EVerbosityLevel verbLevel, const EVerbosityLevel requestedVerbLevel, const bool isDefaultLevel=false)
Return true if the verbosity level includes the given level.
static const std::string toString(AlgorithmA::EAlgoType algoType)
Definition: AlgorithmA.cpp:63
double algoTol_
Definition: AlgorithmA.hpp:64
void someLessDumbFunction(std::ostream &out_arg)
Definition: AlgorithmA.cpp:33
Smart reference counting pointer class for automatic garbage collection.
RCP< basic_FancyOStream< CharT, Traits > > tab(const RCP< basic_FancyOStream< CharT, Traits > > &out, const int tabs=1, const std::basic_string< CharT, Traits > linePrefix="")
Create a tab for an RCP-wrapped basic_FancyOStream object to cause the indentation of all output auto...
Teuchos::RCP< Teuchos::ParameterList > paramList_
Definition: AlgorithmA.hpp:62
virtual EVerbosityLevel getVerbLevel() const
Get the verbosity level.
void doAlgorithm()
Definition: AlgorithmA.cpp:165
virtual RCP< FancyOStream > getOStream() const
Return the output stream to be used for out for *this object.
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
Get a nonconst version of the parameter list that was set using setParameterList().
Definition: AlgorithmA.cpp:114
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call...