00001 /* @HEADER@ */ 00002 // ************************************************************************ 00003 // 00004 // Sundance 00005 // Copyright (2005) Sandia Corporation 00006 // 00007 // Copyright (year first published) Sandia Corporation. Under the terms 00008 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government 00009 // retains certain rights in this software. 00010 // 00011 // This library is free software; you can redistribute it and/or modify 00012 // it under the terms of the GNU Lesser General Public License as 00013 // published by the Free Software Foundation; either version 2.1 of the 00014 // License, or (at your option) any later version. 00015 // 00016 // This library is distributed in the hope that it will be useful, but 00017 // WITHOUT ANY WARRANTY; without even the implied warranty of 00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 // Lesser General Public License for more details. 00020 // 00021 // You should have received a copy of the GNU Lesser General Public 00022 // License along with this library; if not, write to the Free Software 00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00024 // USA 00025 // Questions? Contact Kevin Long (krlong@sandia.gov), 00026 // Sandia National Laboratories, Livermore, California, USA 00027 // 00028 // ************************************************************************ 00029 /* @HEADER@ */ 00030 00031 #ifndef SUNDANCE_TABS_H 00032 #define SUNDANCE_TABS_H 00033 00034 #include "SundanceDefs.hpp" 00035 00036 namespace Sundance 00037 { 00038 /** 00039 * Tabbing utility for output. Constructing a new Tabs object automatically 00040 * increments the number of tabs to be written. When the Tabs object goes out 00041 * of scope, the original tabs level is restored. 00042 * 00043 * The tab size and character can be specified through the setTabSize() and 00044 * setTabChar() methods, for example, 00045 * \code 00046 * Tabs::setTabChar('*'); 00047 * Tabs::setTabSize(4); 00048 * \endcode 00049 * The tab character can be set on an object-by-object basis 00050 * through a constructor argument. 00051 * 00052 * By default, a header giving the depth of tabs is written to each line; this 00053 * can simplify scanning by eye for when a given tab level is reached. 00054 * This header can be turned off by calling 00055 * \code 00056 * Tabs::showDepth() = false; 00057 * \endcode 00058 * 00059 * Example: the code 00060 * \code 00061 * void f() 00062 * { 00063 * Tabs tab; 00064 * cout << tab << "in f()" << std::endl; 00065 * g(); 00066 * cout << tab << "leaving f()" << std::endl; 00067 * } 00068 * 00069 * void g() 00070 * { 00071 * Tabs tab0; 00072 * cout << tab0 << "in g()" << std::endl; 00073 * for (int i=0; i<3; i++) 00074 * { 00075 * Tabs tab1(); 00076 * cout << tab1 << "i=" << i << std::endl; 00077 * } 00078 * cout << tab0 << "leaving g()" << std::endl; 00079 * } 00080 * \endcode 00081 * writes the following output 00082 * \code 00083 * [0] in f() 00084 * [1] in g() 00085 * [2]------i=0 00086 * [2]------i=1 00087 * [2]------i=2 00088 * [1] leaving g() 00089 * [0] leaving f() 00090 * \endcode 00091 */ 00092 class Tabs 00093 { 00094 public: 00095 /** Constructor increments tab level */ 00096 Tabs(bool jump=true); 00097 00098 /** Destructor decrements tab level */ 00099 ~Tabs(); 00100 00101 /** 00102 * Print to stream. This method is usually not called directly, as 00103 * tabs will usually be written with the insertion operator 00104 */ 00105 void print(std::ostream& os) const ; 00106 00107 /** Change the tab size. Default is 2. */ 00108 static void setTabSize(int ts) {tabSize() = ts;} 00109 00110 /** Indicate whether to print the tab depth as a header for each line. */ 00111 static bool& showDepth() {static bool rtn = true; return rtn;} 00112 00113 private: 00114 /** */ 00115 static int& tabLevel() {static int rtn = 0; return rtn;} 00116 00117 /** */ 00118 static int& tabSize() {static int rtn = 2; return rtn;} 00119 00120 bool jump_; 00121 00122 int myLevel_; 00123 }; 00124 } 00125 00126 namespace Sundance 00127 { 00128 /** \relates Tabs stream insertion operator for tab */ 00129 inline std::ostream& operator<<(std::ostream& os, const Tabs& t) 00130 { 00131 t.print(os); 00132 return os; 00133 } 00134 } 00135 00136 #endif