Teuchos_FancyOStream.hpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #ifndef TEUCHOS_FANCY_O_STREAM_HPP
00030 #define TEUCHOS_FANCY_O_STREAM_HPP
00031 
00032 #include "Teuchos_RCP.hpp"
00033 #include "Teuchos_GlobalMPISession.hpp"
00034 #include "Teuchos_oblackholestream.hpp"
00035 #include "Teuchos_as.hpp"
00036 
00037 
00038 namespace Teuchos {
00039 
00040 
00049 template<typename CharT, typename Traits>
00050 class basic_FancyOStream_buf : public std::basic_streambuf<CharT,Traits>
00051 {
00052 public:
00053   
00055   typedef CharT char_type;
00057   typedef Traits           traits_type;
00059   typedef typename traits_type::int_type    int_type;
00061   typedef typename traits_type::pos_type    pos_type;
00063   typedef typename traits_type::off_type    off_type;
00064 
00066   basic_FancyOStream_buf(
00067     const RCP<std::basic_ostream<char_type,traits_type> > &oStream
00068     ,const std::basic_string<char_type,traits_type> &tabIndentStr
00069     ,const int startingTab
00070     ,const bool showLinePrefix
00071     ,const int maxLenLinePrefix
00072     ,const bool showTabCount
00073     ,const bool showProcRank
00074     );
00075 
00077   void initialize(
00078     const RCP<std::basic_ostream<char_type,traits_type> > &oStream
00079     ,const std::basic_string<char_type,traits_type> &tabIndentStr
00080     ,const int startingTab
00081     ,const bool showLinePrefix
00082     ,const int maxLenLinePrefix
00083     ,const bool showTabCount
00084     ,const bool showProcRank
00085     );
00086 
00088   RCP<std::basic_ostream<char_type,traits_type> > getOStream();
00089 
00091   void setTabIndentStr(const std::basic_string<char_type,traits_type> &tabIndentStr);
00092 
00094   const std::basic_string<char_type,traits_type>& getTabIndentStr() const;
00095 
00097   void setShowLinePrefix(const bool showLinePrefix);
00098 
00100   bool getShowLinePrefix() const;
00101 
00103   void setMaxLenLinePrefix(const int maxLenLinePrefix);
00104 
00106   int getMaxLenLinePrefix() const;
00107 
00109   void setShowTabCount(const bool showTabCount);
00110 
00112   bool getShowTabCount() const;
00113 
00115   void setShowProcRank(const bool showProcRank);
00116 
00118   bool getShowProcRank() const;
00119 
00121   void setProcRankAndSize( const int procRank, const int numProcs );
00122 
00124   int getProcRank() const;
00125 
00127   int getNumProcs() const;
00128 
00130   void setOutputToRootOnly( const int rootRank );
00131 
00133   int getOutputToRootOnly() const;
00134 
00136   void pushTab(const int tabs);
00137 
00139   int getNumCurrTabs() const;
00140 
00142   void popTab();
00143 
00145   void pushLinePrefix(
00146     const std::basic_string<char_type,traits_type> &linePrefix
00147     );
00148 
00150   void popLinePrefix();
00151 
00153   const std::basic_string<char_type,traits_type>& getTopLinePrefix() const;
00154 
00156   void pushDisableTabbing();
00157 
00159   void popDisableTabbing();
00160  
00161 protected:
00162  
00164 
00165 
00167   std::streamsize xsputn(const char_type* s, std::streamsize n);
00168 
00170   int_type overflow(int_type c);
00171 
00172 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
00173 
00174   void imbue(const locale& l) 
00175     {
00176       std::cerr << "\ncalled imbue()\n";
00177       std::basic_streambuf<CharT,Traits>::imbue(l);
00178     }
00179  
00180   basic_streambuf<char_type,Traits>* 
00181   setbuf(char_type* s, streamsize n)
00182     {
00183       std::cerr << "\ncalled setbuf()\n";
00184       return std::basic_streambuf<CharT,Traits>::setbuf(s,n);
00185     }
00186  
00187   pos_type 
00188   seekoff(off_type a, ios_base::seekdir b,ios_base::openmode c)
00189     {
00190       std::cerr << "\ncalled seekoff()\n";
00191       return std::basic_streambuf<CharT,Traits>::seekoff(a,b,c);
00192     }
00193 
00194   pos_type 
00195   seekpos(pos_type a, ios_base::openmode b)
00196     {
00197       std::cerr << "\ncalled seekpos()\n";
00198       return std::basic_streambuf<CharT,Traits>::seekpos(a,b);
00199     }
00200  
00201   int 
00202   sync()
00203     {
00204       std::cerr << "\ncalled sync()\n";
00205       return std::basic_streambuf<CharT,Traits>::sync();
00206     }
00207  
00208   streamsize 
00209   showmanyc()
00210     {
00211       std::cerr << "\ncalled showmanyc()\n";
00212       return std::basic_streambuf<CharT,Traits>::showmanyc();
00213     }
00214  
00215   streamsize 
00216   xsgetn(char_type* s, streamsize n)
00217     {
00218       std::cerr << "\ncalled xsgetn()\n";
00219       return std::basic_streambuf<CharT,Traits>::xsgetn(s,n);
00220     }
00221  
00222   int_type 
00223   underflow()
00224     {
00225       std::cerr << "\ncalled underflow()\n";
00226       return std::basic_streambuf<CharT,Traits>::underflow();
00227     }
00228 
00229   int_type 
00230   uflow() 
00231     {
00232       std::cerr << "\ncalled uflow()\n";
00233       return std::basic_streambuf<CharT,Traits>::uflow();
00234     }
00235 
00236   int_type 
00237   pbackfail(int_type c = traits_type::eof())
00238     {
00239       std::cerr << "\ncalled pbackfail()\n";
00240       return std::basic_streambuf<CharT,Traits>::pbackfail(c);
00241     }
00242 
00243 #endif // TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
00244 
00246 
00247 private:
00248 
00249   // ////////////////////////
00250   // Private types
00251 
00252   typedef std::basic_string<char_type,traits_type> string_t;
00253   typedef std::deque<int> tabIndentStack_t;
00254   typedef std::deque<string_t> linePrefixStack_t;
00255 
00256   // ////////////////////////
00257   // Private data members
00258 
00259   RCP<std::basic_ostream<char_type,traits_type> > oStreamSet_;
00260   RCP<std::basic_ostream<char_type,traits_type> > oStream_;
00261   std::basic_string<char_type,traits_type> tabIndentStr_;
00262   bool showLinePrefix_;
00263   int maxLenLinePrefix_;
00264   bool showTabCount_;
00265   bool showProcRank_;
00266   int rootRank_;
00267   int procRank_;
00268   int numProcs_;
00269   int rankPrintWidth_;
00270 
00271   RCP<std::ostringstream> lineOut_;
00272  
00273   int tabIndent_;
00274   tabIndentStack_t tabIndentStack_;
00275   linePrefixStack_t linePrefixStack_;
00276   int enableTabbingStack_;
00277  
00278   bool wroteNewline_;
00279 
00280   // ////////////////////////
00281   // Private member functions
00282 
00283   std::ostream& out();
00284 
00285   void writeChars( const char_type s[], std::streamsize n );
00286 
00287   void writeFrontMatter();
00288 
00289   // Not defined and not to be called
00290   basic_FancyOStream_buf();
00291   basic_FancyOStream_buf(const basic_FancyOStream_buf<CharT,Traits>&);
00292   basic_FancyOStream_buf<CharT,Traits> operator=(
00293     const basic_FancyOStream_buf<CharT,Traits>&
00294     );
00295 
00296 };
00297 
00298 
00320 template <typename CharT, typename Traits = std::char_traits<CharT> >
00321 class basic_FancyOStream : public std::basic_ostream<CharT, Traits>
00322 {
00323 public:
00324 
00326 
00327 
00329   typedef CharT char_type;
00331   typedef Traits traits_type;
00333   typedef typename traits_type::int_type int_type;
00335   typedef typename traits_type::pos_type pos_type;
00337   typedef typename traits_type::off_type off_type;
00341   typedef basic_FancyOStream_buf<CharT,Traits> streambuf_t;
00343   typedef std::basic_ostream<char_type, traits_type> ostream_t;
00344 
00346 
00348 
00349 
00351   explicit
00352   basic_FancyOStream(
00353     const RCP< std::basic_ostream<char_type,traits_type> > &oStream
00354     ,const std::basic_string<char_type,traits_type> &tabIndentStr = " "
00355     ,const int startingTab = 0
00356     ,const bool showLinePrefix = false
00357     ,const int maxLenLinePrefix = 10
00358     ,const bool showTabCount = false
00359     ,const bool showProcRank = false
00360     );
00361 
00363   void initialize(
00364     const RCP< std::basic_ostream<char_type,traits_type> > &oStream
00365     ,const std::basic_string<char_type,traits_type> &tabIndentStr = " "
00366     ,const int startingTab = 0
00367     ,const bool showLinePrefix = false
00368     ,const int maxLenLinePrefix = 10
00369     ,const bool showTabCount = false
00370     ,const bool showProcRank = false
00371     );
00372 
00374   RCP<std::basic_ostream<char_type,traits_type> > getOStream();
00375 
00377   basic_FancyOStream& setTabIndentStr(
00378     const std::basic_string<char_type,traits_type> &tabIndentStr
00379     );
00380 
00382   const std::basic_string<char_type,traits_type>& getTabIndentStr() const;
00383 
00385   basic_FancyOStream& setShowAllFrontMatter(const bool showAllFrontMatter);
00386 
00388   basic_FancyOStream& setShowLinePrefix(const bool showLinePrefix);
00389 
00391   basic_FancyOStream& setMaxLenLinePrefix(const int maxLenLinePrefix);
00392 
00394   basic_FancyOStream& setShowTabCount(const bool showTabCount);
00395 
00397   basic_FancyOStream& setShowProcRank(const bool showProcRank);
00398 
00400   basic_FancyOStream& setProcRankAndSize( const int procRank, const int numProcs );
00401 
00403   basic_FancyOStream& setOutputToRootOnly( const int rootRank );
00404 
00406   int getOutputToRootOnly() const;
00407 
00409   void copyAllOutputOptions(const basic_FancyOStream<CharT,Traits> &oStream);
00410 
00412 
00414 
00415 
00417   void pushTab(const int tabs = 1);
00418 
00420   int getNumCurrTabs() const;
00421 
00423   void popTab();
00424 
00426   void pushLinePrefix(const std::basic_string<char_type,traits_type> &linePrefix);
00427 
00429   void popLinePrefix();
00430 
00432   const std::basic_string<char_type,traits_type>& getTopLinePrefix() const;
00433 
00435   void pushDisableTabbing();
00436 
00438   void popDisableTabbing();
00439 
00441  
00442 private:
00443 
00444   streambuf_t streambuf_;
00445 
00446   // Not defined and not to be called
00447   basic_FancyOStream();
00448   basic_FancyOStream(const basic_FancyOStream<CharT,Traits>&);
00449   basic_FancyOStream<CharT,Traits> operator=(const basic_FancyOStream<CharT,Traits>&);
00450 
00451 };
00452 
00453 
00461 inline
00462 RCP<basic_FancyOStream<char> >
00463 fancyOStream(
00464   const RCP< std::basic_ostream<char> >& oStream,
00465   const std::basic_string<char>& tabIndentStr = " ",
00466   const int startingTab = 0,
00467   const bool showLinePrefix = false,
00468   const int maxLenLinePrefix = 10,
00469   const bool showTabCount = false,
00470   const bool showProcRank = false
00471   )
00472 {
00473   if (nonnull(oStream)) {
00474     return rcp(
00475       new basic_FancyOStream<char>(
00476         oStream,tabIndentStr,startingTab,showLinePrefix,
00477         maxLenLinePrefix,showTabCount,showProcRank
00478         )
00479       );
00480   }
00481   return null;
00482 }
00483 
00484 
00493 inline
00494 RCP<basic_FancyOStream<char> >
00495 getFancyOStream( const RCP<std::basic_ostream<char> > &out )
00496 {
00497   if (is_null(out))
00498     return Teuchos::null;
00499   RCP<basic_FancyOStream<char> >
00500     fancyOut = rcp_dynamic_cast<basic_FancyOStream<char> >(out);
00501   if(nonnull(fancyOut))
00502     return fancyOut;
00503   return rcp(new basic_FancyOStream<char>(out));
00504 }
00505 
00506 
00518 template <typename CharT, typename Traits = std::char_traits<CharT> >
00519 class basic_OSTab
00520 {
00521 public:
00522 
00524   static const int DISABLE_TABBING = -99999; // This magic number should be just fine!
00526   basic_OSTab(
00527     const RCP<basic_FancyOStream<CharT,Traits> > &fancyOStream
00528     ,const int tabs = 1
00529     ,const std::basic_string<CharT,Traits> linePrefix = ""
00530     )
00531     :fancyOStream_(fancyOStream)
00532     ,tabs_(tabs)
00533     ,linePrefix_(linePrefix)
00534     {
00535       updateState();
00536     }
00538   basic_OSTab(
00539     const RCP<std::basic_ostream<CharT,Traits> > &oStream
00540     ,const int tabs = 1
00541     ,const std::basic_string<CharT,Traits> linePrefix = ""
00542     )
00543     :fancyOStream_(getFancyOStream(oStream))
00544     ,tabs_(tabs)
00545     ,linePrefix_(linePrefix)
00546     {
00547       updateState();
00548     }
00550   basic_OSTab(
00551     basic_FancyOStream<CharT,Traits> &fancyOStream
00552     ,const int tabs = 1
00553     ,const std::basic_string<CharT,Traits> linePrefix = ""
00554     )
00555     :fancyOStream_(rcp(&fancyOStream,false))
00556     ,tabs_(tabs)
00557     ,linePrefix_(linePrefix)
00558     {
00559       updateState();
00560     }
00562   basic_OSTab(
00563     std::basic_ostream<CharT,Traits> &oStream
00564     ,const int tabs = 1
00565     ,const std::basic_string<CharT,Traits> linePrefix = ""
00566     )
00567     :fancyOStream_(getFancyOStream(rcp(&oStream, false)))
00568     ,tabs_(tabs)
00569     ,linePrefix_(linePrefix)
00570     {
00571       updateState();
00572     }
00574   basic_OSTab( const basic_OSTab &osTab )
00575     :fancyOStream_(osTab.fancyOStream_)
00576     ,tabs_(osTab.tabs_)
00577     {
00578       updateState();
00579     }
00581   ~basic_OSTab()
00582     {
00583       if(fancyOStream_.get()) {
00584         if(tabs_==DISABLE_TABBING)
00585           fancyOStream_->popDisableTabbing();
00586         else
00587           fancyOStream_->popTab();
00588         if(linePrefix_.length()) fancyOStream_->popLinePrefix();
00589       }
00590     }
00592   basic_OSTab<CharT,Traits>& operator=( const basic_OSTab &osTab )
00593     {
00594       fancyOStream_ = osTab.fancyOStream_;
00595       tabs_ = osTab.tabs_;
00596       updateState();
00597       return *this;
00598     }
00600   basic_OSTab<CharT,Traits>& incrTab(const int tabs = 1)
00601     {
00602       tabs_ += tabs;
00603       if(fancyOStream_.get()) {
00604         fancyOStream_->popTab();
00605         fancyOStream_->pushTab(tabs_);
00606       }
00607       return *this;
00608     }
00610   basic_FancyOStream<CharT,Traits>& o() const
00611     {
00612       return *fancyOStream_;
00613     }
00615   basic_FancyOStream<CharT,Traits>* get() const
00616     {
00617       return fancyOStream_.get();
00618     }
00619  
00620 private:
00621  
00622   RCP<basic_FancyOStream<CharT,Traits> > fancyOStream_;
00623   int tabs_;
00624   std::basic_string<CharT,Traits> linePrefix_;
00625 
00626   void updateState()
00627     {
00628       if(fancyOStream_.get()) {
00629         if(tabs_==DISABLE_TABBING)
00630           fancyOStream_->pushDisableTabbing();
00631         else
00632           fancyOStream_->pushTab(tabs_);
00633         if(linePrefix_.length()) fancyOStream_->pushLinePrefix(linePrefix_);
00634       }
00635     }
00636  
00637 };
00638 
00639 
00655 template <typename CharT, typename Traits>
00656 RCP<basic_FancyOStream<CharT,Traits> >
00657 tab(
00658   const RCP<basic_FancyOStream<CharT,Traits> > &out,
00659   const int tabs = 1,
00660   const std::basic_string<CharT,Traits> linePrefix = ""
00661   )
00662 {
00663   if(out.get()==NULL)
00664     return Teuchos::null;
00665   RCP<basic_FancyOStream<CharT,Traits> > fancyOut = out;
00666   set_extra_data(
00667     rcp(new basic_OSTab<CharT,Traits>(out,tabs,linePrefix)),
00668     "OSTab",
00669     inOutArg(fancyOut),
00670     PRE_DESTROY,
00671     false
00672     );
00673   return fancyOut;
00674 }
00675 
00676 
00692 template <typename CharT, typename Traits>
00693 RCP<basic_FancyOStream<CharT,Traits> >
00694 tab(
00695   const RCP<std::basic_ostream<CharT,Traits> > &out
00696   ,const int tabs = 1
00697   ,const std::basic_string<CharT,Traits> linePrefix = ""
00698   )
00699 {
00700   return tab(getFancyOStream(out),tabs,linePrefix);
00701 }
00702 
00703 
00704 // ///////////////////////////////
00705 // Typedefs
00706 
00707 
00711 typedef basic_FancyOStream<char> FancyOStream;
00712 
00713 
00717 typedef basic_OSTab<char> OSTab;
00718 
00719 
00723 #define TEUCHOS_OSTAB ::Teuchos::OSTab __localThisTab = this->getOSTab()
00724 
00725 
00726 // ////////////////////////////////
00727 // Defintions
00728 
00729 
00730 //
00731 // basic_FancyOStream_buf
00732 //
00733 
00734 
00735 template<typename CharT, typename Traits>
00736 basic_FancyOStream_buf<CharT,Traits>::basic_FancyOStream_buf(
00737   const RCP<std::basic_ostream<char_type,traits_type> > &oStream
00738   ,const std::basic_string<char_type,traits_type> &tabIndentStr
00739   ,const int startingTab
00740   ,const bool showLinePrefix
00741   ,const int maxLenLinePrefix
00742   ,const bool showTabCount
00743   ,const bool showProcRank
00744   )
00745 {
00746   this->initialize(oStream,tabIndentStr,startingTab,showLinePrefix,
00747     maxLenLinePrefix,showTabCount,showProcRank);
00748 }
00749 
00750 
00751 template<typename CharT, typename Traits>
00752 void basic_FancyOStream_buf<CharT,Traits>::initialize(
00753   const RCP<std::basic_ostream<char_type,traits_type> > &oStream
00754   ,const std::basic_string<char_type,traits_type> &tabIndentStr
00755   ,const int startingTab
00756   ,const bool showLinePrefix
00757   ,const int maxLenLinePrefix
00758   ,const bool showTabCount
00759   ,const bool showProcRank
00760   )
00761 {
00762   oStreamSet_ = oStream;
00763   oStream_ = oStream;
00764   tabIndentStr_ = tabIndentStr;
00765   showLinePrefix_ = showLinePrefix;
00766   maxLenLinePrefix_ = maxLenLinePrefix;
00767   showTabCount_ = showTabCount;
00768   showProcRank_ = showProcRank;
00769   rootRank_ = -1;
00770   procRank_ = GlobalMPISession::getRank();
00771   numProcs_ = GlobalMPISession::getNProc();
00772   rankPrintWidth_ = int(std::log10(float(numProcs_)))+1;
00773   tabIndent_ = startingTab;
00774   tabIndentStack_.clear();
00775   linePrefixStack_.clear();
00776   wroteNewline_ = true;
00777   enableTabbingStack_ = 0;
00778 }
00779 
00780 
00781 template<typename CharT, typename Traits>
00782 RCP<std::basic_ostream<CharT,Traits> >
00783 basic_FancyOStream_buf<CharT,Traits>::getOStream()
00784 {
00785   return oStreamSet_;
00786 }
00787 
00788 
00789 template<typename CharT, typename Traits>
00790 void basic_FancyOStream_buf<CharT,Traits>::setTabIndentStr(
00791   const std::basic_string<char_type,traits_type> &tabIndentStr
00792   )
00793 {
00794   tabIndentStr_ = tabIndentStr;
00795 }
00796 
00797 
00798 template<typename CharT, typename Traits>
00799 const std::basic_string<CharT,Traits>&
00800 basic_FancyOStream_buf<CharT,Traits>::getTabIndentStr() const
00801 {
00802   return tabIndentStr_;
00803 }
00804 
00805 
00806 template<typename CharT, typename Traits>
00807 void basic_FancyOStream_buf<CharT,Traits>::setShowLinePrefix(const bool showLinePrefix)
00808 {
00809   showLinePrefix_ = showLinePrefix;
00810 }
00811 
00812 
00813 template<typename CharT, typename Traits>
00814 bool basic_FancyOStream_buf<CharT,Traits>::getShowLinePrefix() const
00815 {
00816   return showLinePrefix_;
00817 }
00818 
00819 
00820 template<typename CharT, typename Traits>
00821 void basic_FancyOStream_buf<CharT,Traits>::setMaxLenLinePrefix(const int maxLenLinePrefix)
00822 {
00823   TEST_FOR_EXCEPT( !(maxLenLinePrefix>=5) );
00824   maxLenLinePrefix_ = maxLenLinePrefix;
00825 }
00826 
00827 
00828 template<typename CharT, typename Traits>
00829 int basic_FancyOStream_buf<CharT,Traits>::getMaxLenLinePrefix() const
00830 {
00831   return maxLenLinePrefix_;
00832 }
00833 
00834 
00835 template<typename CharT, typename Traits>
00836 void basic_FancyOStream_buf<CharT,Traits>::setShowTabCount(const bool showTabCount)
00837 {
00838   showTabCount_ = showTabCount;
00839 }
00840 
00841 
00842 template<typename CharT, typename Traits>
00843 bool basic_FancyOStream_buf<CharT,Traits>::getShowTabCount() const
00844 {
00845   return showTabCount_;
00846 }
00847 
00848 
00849 template<typename CharT, typename Traits>
00850 void basic_FancyOStream_buf<CharT,Traits>::setShowProcRank(const bool showProcRank)
00851 {
00852   showProcRank_ = showProcRank;
00853 }
00854 
00855 
00856 template<typename CharT, typename Traits>
00857 bool basic_FancyOStream_buf<CharT,Traits>::getShowProcRank() const
00858 {
00859   return showProcRank_;
00860 }
00861 
00862 
00863 template<typename CharT, typename Traits>
00864 void basic_FancyOStream_buf<CharT,Traits>::setProcRankAndSize(
00865   const int procRank, const int numProcs
00866   )
00867 {
00868   procRank_ = procRank;
00869   numProcs_ = numProcs;
00870 }
00871 
00872 
00873 template<typename CharT, typename Traits>
00874 int basic_FancyOStream_buf<CharT,Traits>::getProcRank() const
00875 {
00876   return procRank_;
00877 }
00878 
00879 
00880 template<typename CharT, typename Traits>
00881 int basic_FancyOStream_buf<CharT,Traits>::getNumProcs() const
00882 {
00883   return numProcs_;
00884 }
00885 
00886 
00887 template<typename CharT, typename Traits>
00888 void basic_FancyOStream_buf<CharT,Traits>::setOutputToRootOnly(
00889   const int rootRank
00890   )
00891 {
00892   rootRank_ = rootRank;
00893   if(rootRank >= 0) {
00894     if(rootRank == procRank_)
00895       oStream_ = oStreamSet_;
00896     else
00897       oStream_ = rcp(new oblackholestream());
00898     // Only processor is being output to so there is no need for line
00899     // batching!
00900     lineOut_ = null;
00901   }
00902   else {
00903     oStream_ = oStreamSet_;
00904     // Output is being sent to all processors so we need line batching to
00905     // insure that each line will be printed all together!
00906     lineOut_ = rcp(new std::ostringstream());
00907   }
00908 }
00909 
00910 
00911 template<typename CharT, typename Traits>
00912 int basic_FancyOStream_buf<CharT,Traits>::getOutputToRootOnly() const
00913 {
00914   return rootRank_;
00915 }
00916 
00917 
00918 template<typename CharT, typename Traits>
00919 void basic_FancyOStream_buf<CharT,Traits>::pushTab(const int tabs)
00920 {
00921   if( tabIndent_ + tabs < 0 ) {
00922     tabIndentStack_.push_back(-tabIndent_);
00923     tabIndent_ = 0;
00924   }
00925   else {
00926     tabIndentStack_.push_back(tabs);
00927     tabIndent_ += tabs;
00928   }
00929 }
00930 
00931 
00932 template<typename CharT, typename Traits>
00933 int basic_FancyOStream_buf<CharT,Traits>::getNumCurrTabs() const
00934 {
00935   return tabIndent_;
00936 }
00937 
00938 
00939 template<typename CharT, typename Traits>
00940 void basic_FancyOStream_buf<CharT,Traits>::popTab()
00941 {
00942   tabIndent_ -= tabIndentStack_.back();
00943   tabIndentStack_.pop_back();
00944 }
00945 
00946 
00947 template<typename CharT, typename Traits>
00948 void basic_FancyOStream_buf<CharT,Traits>::pushLinePrefix(
00949   const std::basic_string<char_type,traits_type> &linePrefix
00950   )
00951 {
00952   linePrefixStack_.push_back(linePrefix);
00953 }
00954 
00955 
00956 template<typename CharT, typename Traits>
00957 void basic_FancyOStream_buf<CharT,Traits>::popLinePrefix()
00958 {
00959   linePrefixStack_.pop_back();
00960 }
00961 
00962 
00963 template<typename CharT, typename Traits>
00964 const std::basic_string<CharT,Traits>&
00965 basic_FancyOStream_buf<CharT,Traits>::getTopLinePrefix() const
00966 {
00967   return linePrefixStack_.back();
00968 }
00969 
00970 
00971 template<typename CharT, typename Traits>
00972 void basic_FancyOStream_buf<CharT,Traits>::pushDisableTabbing()
00973 {
00974   ++enableTabbingStack_;
00975 }
00976 
00977 
00978 template<typename CharT, typename Traits>
00979 void basic_FancyOStream_buf<CharT,Traits>::popDisableTabbing()
00980 {
00981   --enableTabbingStack_;
00982 }
00983 
00984 
00985 // protected
00986 
00987 
00988 template<typename CharT, typename Traits>
00989 std::streamsize basic_FancyOStream_buf<CharT,Traits>::xsputn(
00990   const char_type* s, std::streamsize n
00991   )
00992 {
00993 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
00994   std::cerr << "\ncalled xsputn()\n";
00995 #endif
00996   writeChars(s,n);
00997   return n;
00998 }
00999 
01000 
01001 template<typename CharT, typename Traits>
01002 typename basic_FancyOStream_buf<CharT,Traits>::int_type 
01003 basic_FancyOStream_buf<CharT,Traits>::overflow(int_type c)
01004 {
01005 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
01006   std::cerr << "\ncalled overflow()\n";
01007 #endif
01008   if(c != traits_type::eof()) {
01009     const char_type cc[] = { traits_type::to_char_type(c) };
01010     this->writeChars(cc,1);
01011   }
01012   return traits_type::not_eof(c);
01013   //return std::basic_streambuf<CharT,Traits>::overflow(c);
01014 }
01015 
01016 
01017 // private
01018 
01019 
01020 template<typename CharT, typename Traits>
01021 std::ostream& basic_FancyOStream_buf<CharT,Traits>::out()
01022 {
01023   if(lineOut_.get())
01024     return *lineOut_;
01025   return *oStream_;
01026 }
01027 
01028 
01029 template<typename CharT, typename Traits>
01030 void basic_FancyOStream_buf<CharT,Traits>::writeChars(
01031   const char_type s[], std::streamsize n
01032   )
01033 {
01034   if(n == 0) return;
01035   std::streamsize p = 0, first_p = 0;
01036   bool done_outputting = false;
01037   const char_type newline = '\n';
01038   while( !done_outputting ) {
01039     // Find the next newline
01040     for( p = first_p; p < n; ++p ) {
01041       if(s[p] == newline) {
01042         break;
01043       }
01044     }
01045     if(p == n) {
01046       // We did not find a newline at the end!
01047       --p;
01048       done_outputting = true;
01049     }
01050     else if( p == n-1 && s[p] == newline ) {
01051       // The last character in the std::string is a newline
01052       done_outputting = true;
01053     }
01054     // Write the beginning of the line if we need to
01055     if(wroteNewline_) {
01056       writeFrontMatter();
01057       wroteNewline_ = false;
01058     }
01059     // Write up to the newline or the end of the std::string
01060     out().write(s+first_p,p-first_p+1);
01061     if(s[p] == newline) {
01062       wroteNewline_ = true;
01063       if(lineOut_.get()) {
01064         *oStream_ << lineOut_->str() << std::flush;
01065         lineOut_->str("");
01066       }
01067     }
01068     // Update for next search
01069     if(!done_outputting)
01070       first_p = p+1; 
01071   }
01072 }
01073 
01074 
01075 template<typename CharT, typename Traits>
01076 void basic_FancyOStream_buf<CharT,Traits>::writeFrontMatter()
01077 {
01078   bool didOutput = false;
01079   std::ostream &o = this->out();
01080   if(showProcRank_) {
01081     o << "p=" << std::right << std::setw(rankPrintWidth_) << procRank_;
01082     didOutput = true;
01083   }
01084   if(showLinePrefix_) {
01085     if(didOutput)
01086       o << ", ";
01087     std::string currLinePrefix = "";
01088     if ( linePrefixStack_.size() )
01089       currLinePrefix = this->getTopLinePrefix();
01090     const int localMaxLenLinePrefix =
01091       TEUCHOS_MAX( as<int>(currLinePrefix.length()), maxLenLinePrefix_ );
01092     o << std::left << std::setw(localMaxLenLinePrefix);
01093     o << currLinePrefix;
01094     didOutput = true;
01095   }
01096   if(showTabCount_) {
01097     if(didOutput)
01098       o << ", ";
01099     o << "tabs=" << std::right << std::setw(2) << tabIndent_;
01100     didOutput = true;
01101   }
01102   // ToDo: Add the Prefix name if asked
01103   // ToDo: Add the processor number if asked
01104   // ToDo: Add the number of indents if asked
01105   if(didOutput) {
01106     o << " |" << tabIndentStr_;
01107   }
01108   if(enableTabbingStack_==0) {
01109     for( int i = 0; i < tabIndent_; ++i )
01110       o << tabIndentStr_;
01111   }
01112 }
01113 
01114 
01115 //
01116 // basic_FancyOStream
01117 //
01118 
01119 
01120 template<typename CharT, typename Traits>
01121 basic_FancyOStream<CharT,Traits>::basic_FancyOStream(
01122   const RCP< std::basic_ostream<char_type,traits_type> > &oStream
01123   ,const std::basic_string<char_type,traits_type> &tabIndentStr
01124   ,const int startingTab
01125   ,const bool showLinePrefix
01126   ,const int maxLenLinePrefix
01127   ,const bool showTabCount
01128   ,const bool showProcRank
01129   )
01130   :ostream_t(NULL),
01131    streambuf_(oStream,tabIndentStr,startingTab,showLinePrefix,
01132      maxLenLinePrefix,showTabCount,showProcRank)
01133 {
01134   this->init(&streambuf_);
01135 }
01136 
01137 
01138 template<typename CharT, typename Traits>
01139 void basic_FancyOStream<CharT,Traits>::initialize(
01140   const RCP< std::basic_ostream<char_type,traits_type> > &oStream
01141   ,const std::basic_string<char_type,traits_type> &tabIndentStr
01142   ,const int startingTab
01143   ,const bool showLinePrefix
01144   ,const int maxLenLinePrefix
01145   ,const bool showTabCount
01146   ,const bool showProcRank
01147   )
01148 {
01149   streambuf_.initialize(oStream,tabIndentStr,startingTab,
01150     showLinePrefix,maxLenLinePrefix,showTabCount,showProcRank);
01151   this->init(&streambuf_);
01152 }
01153 
01154 
01155 template<typename CharT, typename Traits>
01156 RCP<std::basic_ostream<CharT,Traits> >
01157 basic_FancyOStream<CharT,Traits>::getOStream()
01158 {
01159   return streambuf_.getOStream();
01160 }
01161 
01162 
01163 template<typename CharT, typename Traits>
01164 basic_FancyOStream<CharT,Traits>&
01165 basic_FancyOStream<CharT,Traits>::setTabIndentStr(
01166   const std::basic_string<char_type,traits_type> &tabIndentStr
01167   )
01168 {
01169   streambuf_.setTabIndentStr(tabIndentStr);
01170   return *this;
01171 }
01172 
01173 
01174 template<typename CharT, typename Traits>
01175 const std::basic_string<CharT,Traits>&
01176 basic_FancyOStream<CharT,Traits>::getTabIndentStr() const
01177 {
01178   return streambuf_.getTabIndentStr();
01179 }
01180 
01181 
01182 template<typename CharT, typename Traits>
01183 basic_FancyOStream<CharT,Traits>&
01184 basic_FancyOStream<CharT,Traits>::setShowAllFrontMatter(
01185   const bool showAllFrontMatter
01186   )
01187 {
01188   streambuf_.setShowLinePrefix(showAllFrontMatter);
01189   streambuf_.setShowTabCount(showAllFrontMatter);
01190   streambuf_.setShowProcRank(showAllFrontMatter);
01191   return *this;
01192 }
01193 
01194 
01195 template<typename CharT, typename Traits>
01196 basic_FancyOStream<CharT,Traits>&
01197 basic_FancyOStream<CharT,Traits>::setShowLinePrefix(const bool showLinePrefix)
01198 {
01199   streambuf_.setShowLinePrefix(showLinePrefix);
01200   return *this;
01201 }
01202 
01203 
01204 template<typename CharT, typename Traits>
01205 basic_FancyOStream<CharT,Traits>&
01206 basic_FancyOStream<CharT,Traits>::setMaxLenLinePrefix(const int maxLenLinePrefix)
01207 {
01208   streambuf_.setMaxLenLinePrefix(maxLenLinePrefix);
01209   return *this;
01210 }
01211 
01212 
01213 template<typename CharT, typename Traits>
01214 basic_FancyOStream<CharT,Traits>&
01215 basic_FancyOStream<CharT,Traits>::setShowTabCount(const bool showTabCount)
01216 {
01217   streambuf_.setShowTabCount(showTabCount);
01218   return *this;
01219 }
01220 
01221 
01222 template<typename CharT, typename Traits>
01223 basic_FancyOStream<CharT,Traits>&
01224 basic_FancyOStream<CharT,Traits>::setShowProcRank(const bool showProcRank)
01225 {
01226   streambuf_.setShowProcRank(showProcRank);
01227   return *this;
01228 }
01229 
01230 
01231 template<typename CharT, typename Traits>
01232 basic_FancyOStream<CharT,Traits>&
01233 basic_FancyOStream<CharT,Traits>::setProcRankAndSize( const int procRank, const int numProcs )
01234 {
01235   streambuf_.setProcRankAndSize(procRank,numProcs);
01236   return *this;
01237 }
01238 
01239 
01240 template<typename CharT, typename Traits>
01241 basic_FancyOStream<CharT,Traits>&
01242 basic_FancyOStream<CharT,Traits>::setOutputToRootOnly( const int rootRank )
01243 {
01244   streambuf_.setOutputToRootOnly(rootRank);
01245   return *this;
01246 }
01247 
01248 
01249 template<typename CharT, typename Traits>
01250 int basic_FancyOStream<CharT,Traits>::getOutputToRootOnly() const
01251 {
01252   return streambuf_.getOutputToRootOnly();
01253 }
01254 
01255 
01256 template<typename CharT, typename Traits>
01257 void basic_FancyOStream<CharT,Traits>::copyAllOutputOptions(
01258   const basic_FancyOStream<CharT,Traits> &oStream )
01259 {
01260   //streambuf_.setTabIndentStr(oStream.streambuf_.getTabIndentStr());
01261   streambuf_.setShowLinePrefix(oStream.streambuf_.getShowLinePrefix());
01262   streambuf_.setMaxLenLinePrefix(oStream.streambuf_.getMaxLenLinePrefix());
01263   streambuf_.setShowTabCount(oStream.streambuf_.getShowTabCount());
01264   streambuf_.setShowProcRank(oStream.streambuf_.getShowProcRank());
01265   streambuf_.setProcRankAndSize(oStream.streambuf_.getProcRank(), 
01266     oStream.streambuf_.getNumProcs());
01267   streambuf_.setOutputToRootOnly(oStream.streambuf_.getOutputToRootOnly());
01268 }
01269 
01270 
01271 template<typename CharT, typename Traits>
01272 void basic_FancyOStream<CharT,Traits>::pushTab(const int tabs)
01273 {
01274   streambuf_.pushTab(tabs);
01275 }
01276 
01277 
01278 template<typename CharT, typename Traits>
01279 int basic_FancyOStream<CharT,Traits>::getNumCurrTabs() const
01280 {
01281   return streambuf_.getNumCurrTabs();
01282 }
01283 
01284 
01285 template<typename CharT, typename Traits>
01286 void basic_FancyOStream<CharT,Traits>::popTab()
01287 {
01288   streambuf_.popTab();
01289 }
01290 
01291 
01292 template<typename CharT, typename Traits>
01293 void basic_FancyOStream<CharT,Traits>::pushLinePrefix(
01294   const std::basic_string<char_type,traits_type> &linePrefix
01295   )
01296 {
01297   streambuf_.pushLinePrefix(linePrefix);
01298 }
01299 
01300 
01301 template<typename CharT, typename Traits>
01302 void basic_FancyOStream<CharT,Traits>::popLinePrefix()
01303 {
01304   streambuf_.popLinePrefix();
01305 }
01306 
01307 
01308 template<typename CharT, typename Traits>
01309 const std::basic_string<CharT,Traits>&
01310 basic_FancyOStream<CharT,Traits>::getTopLinePrefix() const
01311 {
01312   return streambuf_.getTopLinePrefix();
01313 }
01314 
01315 
01316 template<typename CharT, typename Traits>
01317 void basic_FancyOStream<CharT,Traits>::pushDisableTabbing()
01318 {
01319   streambuf_.pushDisableTabbing();
01320 }
01321 
01322 
01323 template<typename CharT, typename Traits>
01324 void basic_FancyOStream<CharT,Traits>::popDisableTabbing()
01325 {
01326   return streambuf_.popDisableTabbing();
01327 }
01328 
01329 
01330 } // namespace Teuchos
01331 
01332 
01333 #endif // TEUCHOS_FANCY_O_STREAM_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 09:57:43 2011 for Teuchos - Trilinos Tools Package by  doxygen 1.6.3