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 namespace Teuchos {
00038 
00039 
00048 template<typename CharT, typename Traits>
00049 class basic_FancyOStream_buf : public std::basic_streambuf<CharT,Traits>
00050 {
00051 public:
00052   
00054   typedef CharT char_type;
00056   typedef Traits           traits_type;
00058   typedef typename traits_type::int_type    int_type;
00060   typedef typename traits_type::pos_type    pos_type;
00062   typedef typename traits_type::off_type    off_type;
00063 
00065   basic_FancyOStream_buf(
00066     const RCP<std::basic_ostream<char_type,traits_type> > &oStream
00067     ,const std::basic_string<char_type,traits_type> &tabIndentStr
00068     ,const int startingTab
00069     ,const bool showLinePrefix
00070     ,const int maxLenLinePrefix
00071     ,const bool showTabCount
00072     ,const bool showProcRank
00073     );
00074 
00076   void initialize(
00077     const RCP<std::basic_ostream<char_type,traits_type> > &oStream
00078     ,const std::basic_string<char_type,traits_type> &tabIndentStr
00079     ,const int startingTab
00080     ,const bool showLinePrefix
00081     ,const int maxLenLinePrefix
00082     ,const bool showTabCount
00083     ,const bool showProcRank
00084     );
00085 
00087   RCP<std::basic_ostream<char_type,traits_type> > getOStream();
00088 
00090   void setTabIndentStr(const std::basic_string<char_type,traits_type> &tabIndentStr);
00091 
00093   const std::basic_string<char_type,traits_type>& getTabIndentStr() const;
00094 
00096   void setShowLinePrefix(const bool showLinePrefix);
00097 
00099   bool getShowLinePrefix() const;
00100 
00102   void setMaxLenLinePrefix(const int maxLenLinePrefix);
00103 
00105   int getMaxLenLinePrefix() const;
00106 
00108   void setShowTabCount(const bool showTabCount);
00109 
00111   bool getShowTabCount() const;
00112 
00114   void setShowProcRank(const bool showProcRank);
00115 
00117   bool getShowProcRank() const;
00118 
00120   void setProcRankAndSize( const int procRank, const int numProcs );
00121 
00123   int getProcRank() const;
00124 
00126   int getNumProcs() const;
00127 
00129   void setOutputToRootOnly( const int rootRank );
00130 
00132   int getOutputToRootOnly() const;
00133 
00135   void pushTab(const int tabs);
00136 
00138   int getNumCurrTabs() const;
00139 
00141   void popTab();
00142 
00144   void pushLinePrefix(
00145     const std::basic_string<char_type,traits_type> &linePrefix
00146     );
00147 
00149   void popLinePrefix();
00150 
00152   const std::basic_string<char_type,traits_type>& getTopLinePrefix() const;
00153 
00155   void pushDisableTabbing();
00156 
00158   void popDisableTabbing();
00159  
00160 protected:
00161  
00163 
00164 
00166   std::streamsize xsputn(const char_type* s, std::streamsize n);
00167 
00169   int_type overflow(int_type c);
00170 
00171 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
00172 
00173   void imbue(const locale& l) 
00174     {
00175       std::cerr << "\ncalled imbue()\n";
00176       std::basic_streambuf<CharT,Traits>::imbue(l);
00177     }
00178  
00179   basic_streambuf<char_type,Traits>* 
00180   setbuf(char_type* s, streamsize n)
00181     {
00182       std::cerr << "\ncalled setbuf()\n";
00183       return std::basic_streambuf<CharT,Traits>::setbuf(s,n);
00184     }
00185  
00186   pos_type 
00187   seekoff(off_type a, ios_base::seekdir b,ios_base::openmode c)
00188     {
00189       std::cerr << "\ncalled seekoff()\n";
00190       return std::basic_streambuf<CharT,Traits>::seekoff(a,b,c);
00191     }
00192 
00193   pos_type 
00194   seekpos(pos_type a, ios_base::openmode b)
00195     {
00196       std::cerr << "\ncalled seekpos()\n";
00197       return std::basic_streambuf<CharT,Traits>::seekpos(a,b);
00198     }
00199  
00200   int 
00201   sync()
00202     {
00203       std::cerr << "\ncalled sync()\n";
00204       return std::basic_streambuf<CharT,Traits>::sync();
00205     }
00206  
00207   streamsize 
00208   showmanyc()
00209     {
00210       std::cerr << "\ncalled showmanyc()\n";
00211       return std::basic_streambuf<CharT,Traits>::showmanyc();
00212     }
00213  
00214   streamsize 
00215   xsgetn(char_type* s, streamsize n)
00216     {
00217       std::cerr << "\ncalled xsgetn()\n";
00218       return std::basic_streambuf<CharT,Traits>::xsgetn(s,n);
00219     }
00220  
00221   int_type 
00222   underflow()
00223     {
00224       std::cerr << "\ncalled underflow()\n";
00225       return std::basic_streambuf<CharT,Traits>::underflow();
00226     }
00227 
00228   int_type 
00229   uflow() 
00230     {
00231       std::cerr << "\ncalled uflow()\n";
00232       return std::basic_streambuf<CharT,Traits>::uflow();
00233     }
00234 
00235   int_type 
00236   pbackfail(int_type c = traits_type::eof())
00237     {
00238       std::cerr << "\ncalled pbackfail()\n";
00239       return std::basic_streambuf<CharT,Traits>::pbackfail(c);
00240     }
00241 
00242 #endif // TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
00243 
00245 
00246 private:
00247 
00248   // ////////////////////////
00249   // Private types
00250 
00251   typedef std::basic_string<char_type,traits_type> string_t;
00252   typedef std::deque<int> tabIndentStack_t;
00253   typedef std::deque<string_t> linePrefixStack_t;
00254 
00255   // ////////////////////////
00256   // Private data members
00257 
00258   RCP<std::basic_ostream<char_type,traits_type> > oStreamSet_;
00259   RCP<std::basic_ostream<char_type,traits_type> > oStream_;
00260   std::basic_string<char_type,traits_type> tabIndentStr_;
00261   bool showLinePrefix_;
00262   int maxLenLinePrefix_;
00263   bool showTabCount_;
00264   bool showProcRank_;
00265   int rootRank_;
00266   int procRank_;
00267   int numProcs_;
00268   int rankPrintWidth_;
00269 
00270   RCP<std::ostringstream> lineOut_;
00271  
00272   int tabIndent_;
00273   tabIndentStack_t tabIndentStack_;
00274   linePrefixStack_t linePrefixStack_;
00275   int enableTabbingStack_;
00276  
00277   bool wroteNewline_;
00278 
00279   // ////////////////////////
00280   // Private member functions
00281 
00282   std::ostream& out();
00283 
00284   void writeChars( const char_type s[], std::streamsize n );
00285 
00286   void writeFrontMatter();
00287 
00288   // Not defined and not to be called
00289   basic_FancyOStream_buf();
00290   basic_FancyOStream_buf(const basic_FancyOStream_buf<CharT,Traits>&);
00291   basic_FancyOStream_buf<CharT,Traits> operator=(
00292     const basic_FancyOStream_buf<CharT,Traits>&
00293     );
00294 
00295 };
00296 
00318 template <typename CharT, typename Traits = std::char_traits<CharT> >
00319 class basic_FancyOStream : public std::basic_ostream<CharT, Traits>
00320 {
00321 public:
00322 
00324 
00325 
00327   typedef CharT char_type;
00329   typedef Traits traits_type;
00331   typedef typename traits_type::int_type int_type;
00333   typedef typename traits_type::pos_type pos_type;
00335   typedef typename traits_type::off_type off_type;
00339   typedef basic_FancyOStream_buf<CharT,Traits> streambuf_t;
00341   typedef std::basic_ostream<char_type, traits_type> ostream_t;
00342 
00344 
00346 
00347 
00349   explicit
00350   basic_FancyOStream(
00351     const RCP< std::basic_ostream<char_type,traits_type> > &oStream
00352     ,const std::basic_string<char_type,traits_type> &tabIndentStr = " "
00353     ,const int startingTab = 0
00354     ,const bool showLinePrefix = false
00355     ,const int maxLenLinePrefix = 10
00356     ,const bool showTabCount = false
00357     ,const bool showProcRank = false
00358     );
00359 
00361   void initialize(
00362     const RCP< std::basic_ostream<char_type,traits_type> > &oStream
00363     ,const std::basic_string<char_type,traits_type> &tabIndentStr = " "
00364     ,const int startingTab = 0
00365     ,const bool showLinePrefix = false
00366     ,const int maxLenLinePrefix = 10
00367     ,const bool showTabCount = false
00368     ,const bool showProcRank = false
00369     );
00370 
00372   RCP<std::basic_ostream<char_type,traits_type> > getOStream();
00373 
00375   basic_FancyOStream& setTabIndentStr(
00376     const std::basic_string<char_type,traits_type> &tabIndentStr
00377     );
00378 
00380   const std::basic_string<char_type,traits_type>& getTabIndentStr() const;
00381 
00383   basic_FancyOStream& setShowAllFrontMatter(const bool showAllFrontMatter);
00384 
00386   basic_FancyOStream& setShowLinePrefix(const bool showLinePrefix);
00387 
00389   basic_FancyOStream& setMaxLenLinePrefix(const int maxLenLinePrefix);
00390 
00392   basic_FancyOStream& setShowTabCount(const bool showTabCount);
00393 
00395   basic_FancyOStream& setShowProcRank(const bool showProcRank);
00396 
00398   basic_FancyOStream& setProcRankAndSize( const int procRank, const int numProcs );
00399 
00401   basic_FancyOStream& setOutputToRootOnly( const int rootRank );
00402 
00404   int getOutputToRootOnly() const;
00405 
00407   void copyAllOutputOptions(const basic_FancyOStream<CharT,Traits> &oStream);
00408 
00410 
00412 
00413 
00415   void pushTab(const int tabs = 1);
00416 
00418   int getNumCurrTabs() const;
00419 
00421   void popTab();
00422 
00424   void pushLinePrefix(const std::basic_string<char_type,traits_type> &linePrefix);
00425 
00427   void popLinePrefix();
00428 
00430   const std::basic_string<char_type,traits_type>& getTopLinePrefix() const;
00431 
00433   void pushDisableTabbing();
00434 
00436   void popDisableTabbing();
00437 
00439  
00440 private:
00441 
00442   streambuf_t streambuf_;
00443 
00444   // Not defined and not to be called
00445   basic_FancyOStream();
00446   basic_FancyOStream(const basic_FancyOStream<CharT,Traits>&);
00447   basic_FancyOStream<CharT,Traits> operator=(const basic_FancyOStream<CharT,Traits>&);
00448 
00449 };
00455 template <typename CharT, typename Traits>
00456 RCP<basic_FancyOStream<CharT,Traits> >
00457 fancyOStream(
00458   const RCP< std::basic_ostream<CharT,Traits> >& oStream,
00459   const std::basic_string<CharT,Traits>& tabIndentStr = " ",
00460   const int startingTab = 0,
00461   const bool showLinePrefix = false,
00462   const int maxLenLinePrefix = 10,
00463   const bool showTabCount = false,
00464   const bool showProcRank = false
00465   )
00466 {
00467   return rcp(
00468     new basic_FancyOStream<CharT,Traits>(
00469       oStream,tabIndentStr,startingTab,showLinePrefix,
00470       maxLenLinePrefix,showTabCount,showProcRank
00471       )
00472     );
00473 }
00474 
00483 template <typename CharT, typename Traits>
00484 RCP<basic_FancyOStream<CharT,Traits> >
00485 getFancyOStream( const RCP<std::basic_ostream<CharT,Traits> > &out )
00486 {
00487   if(out.get()==NULL)
00488     return Teuchos::null;
00489   RCP<basic_FancyOStream<CharT,Traits> >
00490     fancyOut = rcp_dynamic_cast<basic_FancyOStream<CharT,Traits> >(out);
00491   if(fancyOut.get())
00492     return fancyOut;
00493   return rcp(new basic_FancyOStream<CharT,Traits>(out));
00494 }
00495 
00507 template <typename CharT, typename Traits = std::char_traits<CharT> >
00508 class basic_OSTab
00509 {
00510 public:
00511 
00513   static const int DISABLE_TABBING = -99999; // This magic number should be just fine!
00515   basic_OSTab(
00516     const RCP<basic_FancyOStream<CharT,Traits> > &fancyOStream
00517     ,const int tabs = 1
00518     ,const std::basic_string<CharT,Traits> linePrefix = ""
00519     )
00520     :fancyOStream_(fancyOStream)
00521     ,tabs_(tabs)
00522     ,linePrefix_(linePrefix)
00523     {
00524       updateState();
00525     }
00527   basic_OSTab(
00528     const RCP<std::basic_ostream<CharT,Traits> > &oStream
00529     ,const int tabs = 1
00530     ,const std::basic_string<CharT,Traits> linePrefix = ""
00531     )
00532     :fancyOStream_(getFancyOStream(oStream))
00533     ,tabs_(tabs)
00534     ,linePrefix_(linePrefix)
00535     {
00536       updateState();
00537     }
00539   basic_OSTab(
00540     basic_FancyOStream<CharT,Traits> &fancyOStream
00541     ,const int tabs = 1
00542     ,const std::basic_string<CharT,Traits> linePrefix = ""
00543     )
00544     :fancyOStream_(rcp(&fancyOStream,false))
00545     ,tabs_(tabs)
00546     ,linePrefix_(linePrefix)
00547     {
00548       updateState();
00549     }
00551   basic_OSTab(
00552     std::basic_ostream<CharT,Traits> &oStream
00553     ,const int tabs = 1
00554     ,const std::basic_string<CharT,Traits> linePrefix = ""
00555     )
00556     :fancyOStream_(getFancyOStream(rcp(&oStream,false)))
00557     ,tabs_(tabs)
00558     ,linePrefix_(linePrefix)
00559     {
00560       updateState();
00561     }
00563   basic_OSTab( const basic_OSTab &osTab )
00564     :fancyOStream_(osTab.fancyOStream_)
00565     ,tabs_(osTab.tabs_)
00566     {
00567       updateState();
00568     }
00570   ~basic_OSTab()
00571     {
00572       if(fancyOStream_.get()) {
00573         if(tabs_==DISABLE_TABBING)
00574           fancyOStream_->popDisableTabbing();
00575         else
00576           fancyOStream_->popTab();
00577         if(linePrefix_.length()) fancyOStream_->popLinePrefix();
00578       }
00579     }
00581   basic_OSTab<CharT,Traits>& operator=( const basic_OSTab &osTab )
00582     {
00583       fancyOStream_ = osTab.fancyOStream_;
00584       tabs_ = osTab.tabs_;
00585       updateState();
00586       return *this;
00587     }
00589   basic_OSTab<CharT,Traits>& incrTab(const int tabs = 1)
00590     {
00591       tabs_ += tabs;
00592       if(fancyOStream_.get()) {
00593         fancyOStream_->popTab();
00594         fancyOStream_->pushTab(tabs_);
00595       }
00596       return *this;
00597     }
00599   basic_FancyOStream<CharT,Traits>& o() const
00600     {
00601       return *fancyOStream_;
00602     }
00604   basic_FancyOStream<CharT,Traits>* get() const
00605     {
00606       return fancyOStream_.get();
00607     }
00608  
00609 private:
00610  
00611   RCP<basic_FancyOStream<CharT,Traits> > fancyOStream_;
00612   int tabs_;
00613   std::basic_string<CharT,Traits> linePrefix_;
00614 
00615   void updateState()
00616     {
00617       if(fancyOStream_.get()) {
00618         if(tabs_==DISABLE_TABBING)
00619           fancyOStream_->pushDisableTabbing();
00620         else
00621           fancyOStream_->pushTab(tabs_);
00622         if(linePrefix_.length()) fancyOStream_->pushLinePrefix(linePrefix_);
00623       }
00624     }
00625  
00626 };
00627 
00643 template <typename CharT, typename Traits>
00644 RCP<basic_FancyOStream<CharT,Traits> >
00645 tab(
00646   const RCP<basic_FancyOStream<CharT,Traits> > &out
00647   ,const int tabs = 1
00648   ,const std::basic_string<CharT,Traits> linePrefix = ""
00649   )
00650 {
00651   if(out.get()==NULL)
00652     return Teuchos::null;
00653   RCP<basic_FancyOStream<CharT,Traits> >
00654     fancyOut = rcp(&*out,false);
00655   set_extra_data( out, "out", &fancyOut );
00656   set_extra_data(
00657     rcp(new basic_OSTab<CharT,Traits>(out,tabs,linePrefix))
00658     ,"OSTab"
00659     ,&fancyOut
00660     ,PRE_DESTROY
00661     );
00662   return fancyOut;
00663 }
00664 
00665 
00681 template <typename CharT, typename Traits>
00682 RCP<basic_FancyOStream<CharT,Traits> >
00683 tab(
00684   const RCP<std::basic_ostream<CharT,Traits> > &out
00685   ,const int tabs = 1
00686   ,const std::basic_string<CharT,Traits> linePrefix = ""
00687   )
00688 {
00689   return tab(getFancyOStream(out),tabs,linePrefix);
00690 }
00691 
00692 // ///////////////////////////////
00693 // Typedefs
00694 
00698 typedef basic_FancyOStream<char> FancyOStream;
00699 
00703 typedef basic_OSTab<char> OSTab;
00704 
00708 #define TEUCHOS_OSTAB ::Teuchos::OSTab __localThisTab = this->getOSTab()
00709 
00710 // ////////////////////////////////
00711 // Defintions
00712 
00713 //
00714 // basic_FancyOStream_buf
00715 //
00716 
00717 template<typename CharT, typename Traits>
00718 basic_FancyOStream_buf<CharT,Traits>::basic_FancyOStream_buf(
00719   const RCP<std::basic_ostream<char_type,traits_type> > &oStream
00720   ,const std::basic_string<char_type,traits_type> &tabIndentStr
00721   ,const int startingTab
00722   ,const bool showLinePrefix
00723   ,const int maxLenLinePrefix
00724   ,const bool showTabCount
00725   ,const bool showProcRank
00726   )
00727 {
00728   this->initialize(oStream,tabIndentStr,startingTab,showLinePrefix,
00729     maxLenLinePrefix,showTabCount,showProcRank);
00730 }
00731 
00732 template<typename CharT, typename Traits>
00733 void basic_FancyOStream_buf<CharT,Traits>::initialize(
00734   const RCP<std::basic_ostream<char_type,traits_type> > &oStream
00735   ,const std::basic_string<char_type,traits_type> &tabIndentStr
00736   ,const int startingTab
00737   ,const bool showLinePrefix
00738   ,const int maxLenLinePrefix
00739   ,const bool showTabCount
00740   ,const bool showProcRank
00741   )
00742 {
00743   oStreamSet_ = oStream;
00744   oStream_ = oStream;
00745   tabIndentStr_ = tabIndentStr;
00746   showLinePrefix_ = showLinePrefix;
00747   maxLenLinePrefix_ = maxLenLinePrefix;
00748   showTabCount_ = showTabCount;
00749   showProcRank_ = showProcRank;
00750   rootRank_ = -1;
00751   procRank_ = GlobalMPISession::getRank();
00752   numProcs_ = GlobalMPISession::getNProc();
00753   rankPrintWidth_ = int(std::log10(float(numProcs_)))+1;
00754   tabIndent_ = startingTab;
00755   tabIndentStack_.clear();
00756   linePrefixStack_.clear();
00757   wroteNewline_ = true;
00758   enableTabbingStack_ = 0;
00759 }
00760 
00761 template<typename CharT, typename Traits>
00762 RCP<std::basic_ostream<CharT,Traits> >
00763 basic_FancyOStream_buf<CharT,Traits>::getOStream()
00764 {
00765   return oStreamSet_;
00766 }
00767 
00768 template<typename CharT, typename Traits>
00769 void basic_FancyOStream_buf<CharT,Traits>::setTabIndentStr(
00770   const std::basic_string<char_type,traits_type> &tabIndentStr
00771   )
00772 {
00773   tabIndentStr_ = tabIndentStr;
00774 }
00775 
00776 template<typename CharT, typename Traits>
00777 const std::basic_string<CharT,Traits>&
00778 basic_FancyOStream_buf<CharT,Traits>::getTabIndentStr() const
00779 {
00780   return tabIndentStr_;
00781 }
00782 
00783 template<typename CharT, typename Traits>
00784 void basic_FancyOStream_buf<CharT,Traits>::setShowLinePrefix(const bool showLinePrefix)
00785 {
00786   showLinePrefix_ = showLinePrefix;
00787 }
00788 
00789 template<typename CharT, typename Traits>
00790 bool basic_FancyOStream_buf<CharT,Traits>::getShowLinePrefix() const
00791 {
00792   return showLinePrefix_;
00793 }
00794 
00795 template<typename CharT, typename Traits>
00796 void basic_FancyOStream_buf<CharT,Traits>::setMaxLenLinePrefix(const int maxLenLinePrefix)
00797 {
00798   TEST_FOR_EXCEPT( !(maxLenLinePrefix>=5) );
00799   maxLenLinePrefix_ = maxLenLinePrefix;
00800 }
00801 
00802 template<typename CharT, typename Traits>
00803 int basic_FancyOStream_buf<CharT,Traits>::getMaxLenLinePrefix() const
00804 {
00805   return maxLenLinePrefix_;
00806 }
00807 
00808 template<typename CharT, typename Traits>
00809 void basic_FancyOStream_buf<CharT,Traits>::setShowTabCount(const bool showTabCount)
00810 {
00811   showTabCount_ = showTabCount;
00812 }
00813 
00814 template<typename CharT, typename Traits>
00815 bool basic_FancyOStream_buf<CharT,Traits>::getShowTabCount() const
00816 {
00817   return showTabCount_;
00818 }
00819 
00820 template<typename CharT, typename Traits>
00821 void basic_FancyOStream_buf<CharT,Traits>::setShowProcRank(const bool showProcRank)
00822 {
00823   showProcRank_ = showProcRank;
00824 }
00825 
00826 template<typename CharT, typename Traits>
00827 bool basic_FancyOStream_buf<CharT,Traits>::getShowProcRank() const
00828 {
00829   return showProcRank_;
00830 }
00831 
00832 template<typename CharT, typename Traits>
00833 void basic_FancyOStream_buf<CharT,Traits>::setProcRankAndSize(
00834   const int procRank, const int numProcs
00835   )
00836 {
00837   procRank_ = procRank;
00838   numProcs_ = numProcs;
00839 }
00840 
00841 template<typename CharT, typename Traits>
00842 int basic_FancyOStream_buf<CharT,Traits>::getProcRank() const
00843 {
00844   return procRank_;
00845 }
00846 
00847 template<typename CharT, typename Traits>
00848 int basic_FancyOStream_buf<CharT,Traits>::getNumProcs() const
00849 {
00850   return numProcs_;
00851 }
00852 
00853 template<typename CharT, typename Traits>
00854 void basic_FancyOStream_buf<CharT,Traits>::setOutputToRootOnly(
00855   const int rootRank
00856   )
00857 {
00858   rootRank_ = rootRank;
00859   if(rootRank >= 0) {
00860     if(rootRank == procRank_)
00861       oStream_ = oStreamSet_;
00862     else
00863       oStream_ = rcp(new oblackholestream());
00864     // Only processor is being output to so there is no need for line
00865     // batching!
00866     lineOut_ = null;
00867   }
00868   else {
00869     oStream_ = oStreamSet_;
00870     // Output is being sent to all processors so we need line batching to
00871     // insure that each line will be printed all together!
00872     lineOut_ = rcp(new std::ostringstream());
00873   }
00874 }
00875 
00876 template<typename CharT, typename Traits>
00877 int basic_FancyOStream_buf<CharT,Traits>::getOutputToRootOnly() const
00878 {
00879   return rootRank_;
00880 }
00881 
00882 template<typename CharT, typename Traits>
00883 void basic_FancyOStream_buf<CharT,Traits>::pushTab(const int tabs)
00884 {
00885   if( tabIndent_ + tabs < 0 ) {
00886     tabIndentStack_.push_back(-tabIndent_);
00887     tabIndent_ = 0;
00888   }
00889   else {
00890     tabIndentStack_.push_back(tabs);
00891     tabIndent_ += tabs;
00892   }
00893 }
00894 
00895 template<typename CharT, typename Traits>
00896 int basic_FancyOStream_buf<CharT,Traits>::getNumCurrTabs() const
00897 {
00898   return tabIndent_;
00899 }
00900 
00901 template<typename CharT, typename Traits>
00902 void basic_FancyOStream_buf<CharT,Traits>::popTab()
00903 {
00904   tabIndent_ -= tabIndentStack_.back();
00905   tabIndentStack_.pop_back();
00906 }
00907 
00908 template<typename CharT, typename Traits>
00909 void basic_FancyOStream_buf<CharT,Traits>::pushLinePrefix(
00910   const std::basic_string<char_type,traits_type> &linePrefix
00911   )
00912 {
00913   linePrefixStack_.push_back(linePrefix);
00914 }
00915 
00916 template<typename CharT, typename Traits>
00917 void basic_FancyOStream_buf<CharT,Traits>::popLinePrefix()
00918 {
00919   linePrefixStack_.pop_back();
00920 }
00921 
00922 template<typename CharT, typename Traits>
00923 const std::basic_string<CharT,Traits>&
00924 basic_FancyOStream_buf<CharT,Traits>::getTopLinePrefix() const
00925 {
00926   return linePrefixStack_.back();
00927 }
00928 
00929 template<typename CharT, typename Traits>
00930 void basic_FancyOStream_buf<CharT,Traits>::pushDisableTabbing()
00931 {
00932   ++enableTabbingStack_;
00933 }
00934 
00935 template<typename CharT, typename Traits>
00936 void basic_FancyOStream_buf<CharT,Traits>::popDisableTabbing()
00937 {
00938   --enableTabbingStack_;
00939 }
00940 
00941 // protected
00942 
00943 template<typename CharT, typename Traits>
00944 std::streamsize basic_FancyOStream_buf<CharT,Traits>::xsputn(
00945   const char_type* s, std::streamsize n
00946   )
00947 {
00948 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
00949   std::cerr << "\ncalled xsputn()\n";
00950 #endif
00951   writeChars(s,n);
00952   return n;
00953 }
00954 
00955 template<typename CharT, typename Traits>
00956 typename basic_FancyOStream_buf<CharT,Traits>::int_type 
00957 basic_FancyOStream_buf<CharT,Traits>::overflow(int_type c)
00958 {
00959 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
00960   std::cerr << "\ncalled overflow()\n";
00961 #endif
00962   if(c != traits_type::eof()) {
00963     const char_type cc[] = { traits_type::to_char_type(c) };
00964     this->writeChars(cc,1);
00965   }
00966   return traits_type::not_eof(c);
00967   //return std::basic_streambuf<CharT,Traits>::overflow(c);
00968 }
00969 
00970 // private
00971 
00972 template<typename CharT, typename Traits>
00973 std::ostream& basic_FancyOStream_buf<CharT,Traits>::out()
00974 {
00975   if(lineOut_.get())
00976     return *lineOut_;
00977   return *oStream_;
00978 }
00979 
00980 template<typename CharT, typename Traits>
00981 void basic_FancyOStream_buf<CharT,Traits>::writeChars(
00982   const char_type s[], std::streamsize n
00983   )
00984 {
00985   if(n == 0) return;
00986   std::streamsize p = 0, first_p = 0;
00987   bool done_outputting = false;
00988   const char_type newline = '\n';
00989   while( !done_outputting ) {
00990     // Find the next newline
00991     for( p = first_p; p < n; ++p ) {
00992       if(s[p] == newline) {
00993         break;
00994       }
00995     }
00996     if(p == n) {
00997       // We did not find a newline at the end!
00998       --p;
00999       done_outputting = true;
01000     }
01001     else if( p == n-1 && s[p] == newline ) {
01002       // The last character in the std::string is a newline
01003       done_outputting = true;
01004     }
01005     // Write the beginning of the line if we need to
01006     if(wroteNewline_) {
01007       writeFrontMatter();
01008       wroteNewline_ = false;
01009     }
01010     // Write up to the newline or the end of the std::string
01011     out().write(s+first_p,p-first_p+1);
01012     if(s[p] == newline) {
01013       wroteNewline_ = true;
01014       if(lineOut_.get()) {
01015         *oStream_ << lineOut_->str() << std::flush;
01016         lineOut_->str("");
01017       }
01018     }
01019     // Update for next search
01020     if(!done_outputting)
01021       first_p = p+1; 
01022   }
01023 }
01024 
01025 template<typename CharT, typename Traits>
01026 void basic_FancyOStream_buf<CharT,Traits>::writeFrontMatter()
01027 {
01028   bool didOutput = false;
01029   std::ostream &o = this->out();
01030   if(showProcRank_) {
01031     o << "p=" << std::right << std::setw(rankPrintWidth_) << procRank_;
01032     didOutput = true;
01033   }
01034   if(showLinePrefix_) {
01035     if(didOutput)
01036       o << ", ";
01037     std::string currLinePrefix = "";
01038     if ( linePrefixStack_.size() )
01039       currLinePrefix = this->getTopLinePrefix();
01040     const int localMaxLenLinePrefix =
01041       TEUCHOS_MAX( as<int>(currLinePrefix.length()), maxLenLinePrefix_ );
01042     o << std::left << std::setw(localMaxLenLinePrefix);
01043     o << currLinePrefix;
01044     didOutput = true;
01045   }
01046   if(showTabCount_) {
01047     if(didOutput)
01048       o << ", ";
01049     o << "tabs=" << std::right << std::setw(2) << tabIndent_;
01050     didOutput = true;
01051   }
01052   // ToDo: Add the Prefix name if asked
01053   // ToDo: Add the processor number if asked
01054   // ToDo: Add the number of indents if asked
01055   if(didOutput) {
01056     o << " |" << tabIndentStr_;
01057   }
01058   if(enableTabbingStack_==0) {
01059     for( int i = 0; i < tabIndent_; ++i )
01060       o << tabIndentStr_;
01061   }
01062 }
01063 
01064 //
01065 // basic_FancyOStream
01066 //
01067 
01068 template<typename CharT, typename Traits>
01069 basic_FancyOStream<CharT,Traits>::basic_FancyOStream(
01070   const RCP< std::basic_ostream<char_type,traits_type> > &oStream
01071   ,const std::basic_string<char_type,traits_type> &tabIndentStr
01072   ,const int startingTab
01073   ,const bool showLinePrefix
01074   ,const int maxLenLinePrefix
01075   ,const bool showTabCount
01076   ,const bool showProcRank
01077   )
01078   :ostream_t(NULL),
01079    streambuf_(oStream,tabIndentStr,startingTab,showLinePrefix,
01080      maxLenLinePrefix,showTabCount,showProcRank)
01081 {
01082   this->init(&streambuf_);
01083 }
01084 
01085 template<typename CharT, typename Traits>
01086 void basic_FancyOStream<CharT,Traits>::initialize(
01087   const RCP< std::basic_ostream<char_type,traits_type> > &oStream
01088   ,const std::basic_string<char_type,traits_type> &tabIndentStr
01089   ,const int startingTab
01090   ,const bool showLinePrefix
01091   ,const int maxLenLinePrefix
01092   ,const bool showTabCount
01093   ,const bool showProcRank
01094   )
01095 {
01096   streambuf_.initialize(oStream,tabIndentStr,startingTab,
01097     showLinePrefix,maxLenLinePrefix,showTabCount,showProcRank);
01098   this->init(&streambuf_);
01099 }
01100 
01101 template<typename CharT, typename Traits>
01102 RCP<std::basic_ostream<CharT,Traits> >
01103 basic_FancyOStream<CharT,Traits>::getOStream()
01104 {
01105   return streambuf_.getOStream();
01106 }
01107 
01108 template<typename CharT, typename Traits>
01109 basic_FancyOStream<CharT,Traits>&
01110 basic_FancyOStream<CharT,Traits>::setTabIndentStr(
01111   const std::basic_string<char_type,traits_type> &tabIndentStr
01112   )
01113 {
01114   streambuf_.setTabIndentStr(tabIndentStr);
01115   return *this;
01116 }
01117 
01118 template<typename CharT, typename Traits>
01119 const std::basic_string<CharT,Traits>&
01120 basic_FancyOStream<CharT,Traits>::getTabIndentStr() const
01121 {
01122   return streambuf_.getTabIndentStr();
01123 }
01124 
01125 template<typename CharT, typename Traits>
01126 basic_FancyOStream<CharT,Traits>&
01127 basic_FancyOStream<CharT,Traits>::setShowAllFrontMatter(
01128   const bool showAllFrontMatter
01129   )
01130 {
01131   streambuf_.setShowLinePrefix(showAllFrontMatter);
01132   streambuf_.setShowTabCount(showAllFrontMatter);
01133   streambuf_.setShowProcRank(showAllFrontMatter);
01134   return *this;
01135 }
01136 
01137 template<typename CharT, typename Traits>
01138 basic_FancyOStream<CharT,Traits>&
01139 basic_FancyOStream<CharT,Traits>::setShowLinePrefix(const bool showLinePrefix)
01140 {
01141   streambuf_.setShowLinePrefix(showLinePrefix);
01142   return *this;
01143 }
01144 
01145 template<typename CharT, typename Traits>
01146 basic_FancyOStream<CharT,Traits>&
01147 basic_FancyOStream<CharT,Traits>::setMaxLenLinePrefix(const int maxLenLinePrefix)
01148 {
01149   streambuf_.setMaxLenLinePrefix(maxLenLinePrefix);
01150   return *this;
01151 }
01152 
01153 template<typename CharT, typename Traits>
01154 basic_FancyOStream<CharT,Traits>&
01155 basic_FancyOStream<CharT,Traits>::setShowTabCount(const bool showTabCount)
01156 {
01157   streambuf_.setShowTabCount(showTabCount);
01158   return *this;
01159 }
01160 
01161 template<typename CharT, typename Traits>
01162 basic_FancyOStream<CharT,Traits>&
01163 basic_FancyOStream<CharT,Traits>::setShowProcRank(const bool showProcRank)
01164 {
01165   streambuf_.setShowProcRank(showProcRank);
01166   return *this;
01167 }
01168 
01169 template<typename CharT, typename Traits>
01170 basic_FancyOStream<CharT,Traits>&
01171 basic_FancyOStream<CharT,Traits>::setProcRankAndSize( const int procRank, const int numProcs )
01172 {
01173   streambuf_.setProcRankAndSize(procRank,numProcs);
01174   return *this;
01175 }
01176 
01177 template<typename CharT, typename Traits>
01178 basic_FancyOStream<CharT,Traits>&
01179 basic_FancyOStream<CharT,Traits>::setOutputToRootOnly( const int rootRank )
01180 {
01181   streambuf_.setOutputToRootOnly(rootRank);
01182   return *this;
01183 }
01184 
01185 template<typename CharT, typename Traits>
01186 int basic_FancyOStream<CharT,Traits>::getOutputToRootOnly() const
01187 {
01188   return streambuf_.getOutputToRootOnly();
01189 }
01190 
01191 template<typename CharT, typename Traits>
01192 void basic_FancyOStream<CharT,Traits>::copyAllOutputOptions( const basic_FancyOStream<CharT,Traits> &oStream )
01193 {
01194   //streambuf_.setTabIndentStr(oStream.streambuf_.getTabIndentStr());
01195   streambuf_.setShowLinePrefix(oStream.streambuf_.getShowLinePrefix());
01196   streambuf_.setMaxLenLinePrefix(oStream.streambuf_.getMaxLenLinePrefix());
01197   streambuf_.setShowTabCount(oStream.streambuf_.getShowTabCount());
01198   streambuf_.setShowProcRank(oStream.streambuf_.getShowProcRank());
01199   streambuf_.setProcRankAndSize(oStream.streambuf_.getProcRank(),oStream.streambuf_.getNumProcs());
01200   streambuf_.setOutputToRootOnly(oStream.streambuf_.getOutputToRootOnly());
01201 }
01202 
01203 template<typename CharT, typename Traits>
01204 void basic_FancyOStream<CharT,Traits>::pushTab(const int tabs)
01205 {
01206   streambuf_.pushTab(tabs);
01207 }
01208 
01209 template<typename CharT, typename Traits>
01210 int basic_FancyOStream<CharT,Traits>::getNumCurrTabs() const
01211 {
01212   return streambuf_.getNumCurrTabs();
01213 }
01214 
01215 template<typename CharT, typename Traits>
01216 void basic_FancyOStream<CharT,Traits>::popTab()
01217 {
01218   streambuf_.popTab();
01219 }
01220 
01221 template<typename CharT, typename Traits>
01222 void basic_FancyOStream<CharT,Traits>::pushLinePrefix(
01223   const std::basic_string<char_type,traits_type> &linePrefix
01224   )
01225 {
01226   streambuf_.pushLinePrefix(linePrefix);
01227 }
01228 
01229 template<typename CharT, typename Traits>
01230 void basic_FancyOStream<CharT,Traits>::popLinePrefix()
01231 {
01232   streambuf_.popLinePrefix();
01233 }
01234 
01235 template<typename CharT, typename Traits>
01236 const std::basic_string<CharT,Traits>&
01237 basic_FancyOStream<CharT,Traits>::getTopLinePrefix() const
01238 {
01239   return streambuf_.getTopLinePrefix();
01240 }
01241 
01242 template<typename CharT, typename Traits>
01243 void basic_FancyOStream<CharT,Traits>::pushDisableTabbing()
01244 {
01245   streambuf_.pushDisableTabbing();
01246 }
01247 
01248 template<typename CharT, typename Traits>
01249 void basic_FancyOStream<CharT,Traits>::popDisableTabbing()
01250 {
01251   return streambuf_.popDisableTabbing();
01252 }
01253 
01254 } // namespace Teuchos
01255 
01256 #endif // TEUCHOS_FANCY_O_STREAM_HPP

Generated on Wed May 12 21:40:32 2010 for Teuchos - Trilinos Tools Package by  doxygen 1.4.7