Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_Describable.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef TEUCHOS_DESCRIBABLE_HPP
43 #define TEUCHOS_DESCRIBABLE_HPP
44 
46 #include "Teuchos_FancyOStream.hpp"
47 #include "Teuchos_LabeledObject.hpp"
48 
49 
50 namespace Teuchos {
51 
52 
76 class TEUCHOSCORE_LIB_DLL_EXPORT Describable : virtual public LabeledObject {
77 public:
80 
82 
83 
95  virtual std::string description() const;
96 
148  virtual void
149  describe (FancyOStream &out,
150  const EVerbosityLevel verbLevel = verbLevel_default) const;
151 
162  void
163  describe (std::ostream &out,
164  const EVerbosityLevel verbLevel = verbLevel_default) const;
165 
167  virtual ~Describable ();
168 };
169 
170 
171 // Describable stream manipulator state class
172 //
173 // This is not a class that a user needs to see and that is why it is not
174 // being given doxygen documentation!
175 struct DescribableStreamManipulatorState {
176  const Describable &describable;
177  const EVerbosityLevel verbLevel;
178  DescribableStreamManipulatorState(
179  const Describable &_describable,
180  const EVerbosityLevel _verbLevel = VERB_MEDIUM
181  )
182  :describable(_describable)
183  ,verbLevel(_verbLevel)
184  {}
185 };
186 
187 
209 inline DescribableStreamManipulatorState describe(
210  const Describable &describable,
212  )
213 {
214  return DescribableStreamManipulatorState(describable,verbLevel);
215 }
216 
217 
244 inline
245 std::ostream& operator<<(
246  std::ostream& os, const DescribableStreamManipulatorState& d
247  )
248 {
249  d.describable.describe(*getFancyOStream(Teuchos::rcp(&os,false)),d.verbLevel);
250  return os;
251 }
252 
253 //
254 // RAB: Note: The above function works with an std::ostream object even
255 // through Describable::describe(...) requires a FancyOStream object. We must
256 // write the stream manipulator in terms of std::ostream, or compound output
257 // statements like:
258 //
259 // void foo( FancyOStream &out, Describable &d, EVerbLevel verbLevel )
260 // {
261 // out << "\nThis is the describable object d:" << describe(d,verbLevel);
262 // }
263 //
264 // will not work correctly. The problem is that the first output
265 //
266 // out << "\nThis is the describable object d:"
267 //
268 // must return a reference to an std::ostream object. This should mean that
269 // the next statement, which is basically:
270 //
271 // static_cast<std::ostream&>(out) << DescribableStreamManipulatorState
272 //
273 // should not even compile. However, under gcc 3.4.3, the code did compile
274 // but did not call the above function. Instead, it set up some type of
275 // infinite recursion that resulted in a segfault due to the presence of the
276 // Teuchos::any class!
277 //
278 
279 
280 } // namespace Teuchos
281 
282 #endif // TEUCHOS_DESCRIBABLE_HPP
Base class for objects that contain a std::string label.
EVerbosityLevel
Verbosity level.
Generate more output.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
std::ostream subclass that performs the magic of indenting data sent to an std::ostream object among ...
std::ostream & operator<<(std::ostream &os, const DescribableStreamManipulatorState &d)
Output stream operator for Describable manipulator.
static const EVerbosityLevel verbLevel_default
Default value for the verbLevel argument of describe().
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
Base class for all objects that can describe themselves.
DescribableStreamManipulatorState describe(const Describable &describable, const EVerbosityLevel verbLevel=Describable::verbLevel_default)
Describable output stream manipulator.