PlayaTabs.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 //   
00003 /* @HEADER@ */
00004 
00005 #ifndef PLAYA_TABS_H
00006 #define PLAYA_TABS_H
00007 
00008 #include "PlayaDefs.hpp"
00009 
00010 namespace Playa
00011 {
00012 /**
00013  * Tabbing utility for output. Constructing a new Tabs object automatically
00014  * increments the number of tabs to be written. When the Tabs object goes out
00015  * of scope, the original tabs level is restored. 
00016  *
00017  * The tab size and character can be specified through the setTabSize() and
00018  * setTabChar() methods, for example,
00019  * \code
00020  * Tabs::setTabChar('*');
00021  * Tabs::setTabSize(4);
00022  * \endcode
00023  * The tab character can be set on an object-by-object basis
00024  * through a constructor argument.
00025  *
00026  * By default, a header giving the depth of tabs is written to each line; this
00027  * can simplify scanning by eye for when a given tab level is reached. 
00028  * This header can be turned off by calling
00029  * \code
00030  * Tabs::showDepth() = false;
00031  * \endcode
00032  * 
00033  * Example: the code
00034  * \code
00035  * void f()
00036  * {
00037  *   Tabs tab;
00038  *   cout << tab << "in f()" << std::endl;
00039  *   g();
00040  *   cout << tab << "leaving f()" << std::endl;
00041  * }
00042  *
00043  * void g()
00044  * {
00045  *   Tabs tab0;
00046  *   cout << tab0 << "in g()" << std::endl;
00047  *   for (int i=0; i<3; i++)
00048  *     {
00049  *       Tabs tab1();
00050  *       cout << tab1 << "i=" << i << std::endl;
00051  *     }
00052  *   cout << tab0 << "leaving g()" << std::endl;
00053  * }
00054  * \endcode
00055  * writes the following output 
00056  * \code
00057  * [0]  in f()
00058  * [1]    in g()
00059  * [2]------i=0
00060  * [2]------i=1
00061  * [2]------i=2
00062  * [1]    leaving g()
00063  * [0]  leaving f()
00064  * \endcode 
00065  */
00066 class Tabs
00067 {
00068 public:
00069   /** Constructor increments tab level */
00070   Tabs(bool jump=true);
00071 
00072   /** Destructor decrements tab level */
00073   ~Tabs();
00074 
00075   /** 
00076    * Print to stream. This method is usually not called directly, as
00077    * tabs will usually be written with the insertion operator
00078    */
00079   void print(std::ostream& os) const ;
00080 
00081   /** Change the tab size. Default is 2.  */
00082   static void setTabSize(int ts) {tabSize() = ts;}
00083 
00084   /** Indicate whether to print the tab depth as a header for each line. */
00085   static bool& showDepth() {static bool rtn = true; return rtn;}
00086 
00087 private:
00088   /** */
00089   static int& tabLevel() {static int rtn = 0; return rtn;}
00090 
00091   /** */
00092   static int& tabSize() {static int rtn = 2; return rtn;}
00093 
00094   bool jump_;
00095 
00096   int myLevel_;
00097 };
00098 }
00099 
00100 namespace Playa
00101 {
00102 /** \relates Tabs stream insertion operator for tab */
00103 inline std::ostream& operator<<(std::ostream& os, const Tabs& t)
00104 {
00105   t.print(os);
00106   return os;
00107 }
00108 }
00109 
00110 #endif

Site Contact