Teuchos_FancyOStream.hpp

Go to the documentation of this file.
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(fancyOut.get())
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> >
00666     fancyOut = rcp(&*out,false);
00667   set_extra_data( out, "out", inOutArg(fancyOut) );
00668   set_extra_data(
00669     rcp(new basic_OSTab<CharT,Traits>(out,tabs,linePrefix)),
00670     "OSTab",
00671     inOutArg(fancyOut),
00672     PRE_DESTROY
00673     );
00674   return fancyOut;
00675 }
00676 
00677 
00693 template <typename CharT, typename Traits>
00694 RCP<basic_FancyOStream<CharT,Traits> >
00695 tab(
00696   const RCP<std::basic_ostream<CharT,Traits> > &out
00697   ,const int tabs = 1
00698   ,const std::basic_string<CharT,Traits> linePrefix = ""
00699   )
00700 {
00701   return tab(getFancyOStream(out),tabs,linePrefix);
00702 }
00703 
00704 
00705 // ///////////////////////////////
00706 // Typedefs
00707 
00708 
00712 typedef basic_FancyOStream<char> FancyOStream;
00713 
00714 
00718 typedef basic_OSTab<char> OSTab;
00719 
00720 
00724 #define TEUCHOS_OSTAB ::Teuchos::OSTab __localThisTab = this->getOSTab()
00725 
00726 
00727 // ////////////////////////////////
00728 // Defintions
00729 
00730 
00731 //
00732 // basic_FancyOStream_buf
00733 //
00734 
00735 
00736 template<typename CharT, typename Traits>
00737 basic_FancyOStream_buf<CharT,Traits>::basic_FancyOStream_buf(
00738   const RCP<std::basic_ostream<char_type,traits_type> > &oStream
00739   ,const std::basic_string<char_type,traits_type> &tabIndentStr
00740   ,const int startingTab
00741   ,const bool showLinePrefix
00742   ,const int maxLenLinePrefix
00743   ,const bool showTabCount
00744   ,const bool showProcRank
00745   )
00746 {
00747   this->initialize(oStream,tabIndentStr,startingTab,showLinePrefix,
00748     maxLenLinePrefix,showTabCount,showProcRank);
00749 }
00750 
00751 
00752 template<typename CharT, typename Traits>
00753 void basic_FancyOStream_buf<CharT,Traits>::initialize(
00754   const RCP<std::basic_ostream<char_type,traits_type> > &oStream
00755   ,const std::basic_string<char_type,traits_type> &tabIndentStr
00756   ,const int startingTab
00757   ,const bool showLinePrefix
00758   ,const int maxLenLinePrefix
00759   ,const bool showTabCount
00760   ,const bool showProcRank
00761   )
00762 {
00763   oStreamSet_ = oStream;
00764   oStream_ = oStream;
00765   tabIndentStr_ = tabIndentStr;
00766   showLinePrefix_ = showLinePrefix;
00767   maxLenLinePrefix_ = maxLenLinePrefix;
00768   showTabCount_ = showTabCount;
00769   showProcRank_ = showProcRank;
00770   rootRank_ = -1;
00771   procRank_ = GlobalMPISession::getRank();
00772   numProcs_ = GlobalMPISession::getNProc();
00773   rankPrintWidth_ = int(std::log10(float(numProcs_)))+1;
00774   tabIndent_ = startingTab;
00775   tabIndentStack_.clear();
00776   linePrefixStack_.clear();
00777   wroteNewline_ = true;
00778   enableTabbingStack_ = 0;
00779 }
00780 
00781 
00782 template<typename CharT, typename Traits>
00783 RCP<std::basic_ostream<CharT,Traits> >
00784 basic_FancyOStream_buf<CharT,Traits>::getOStream()
00785 {
00786   return oStreamSet_;
00787 }
00788 
00789 
00790 template<typename CharT, typename Traits>
00791 void basic_FancyOStream_buf<CharT,Traits>::setTabIndentStr(
00792   const std::basic_string<char_type,traits_type> &tabIndentStr
00793   )
00794 {
00795   tabIndentStr_ = tabIndentStr;
00796 }
00797 
00798 
00799 template<typename CharT, typename Traits>
00800 const std::basic_string<CharT,Traits>&
00801 basic_FancyOStream_buf<CharT,Traits>::getTabIndentStr() const
00802 {
00803   return tabIndentStr_;
00804 }
00805 
00806 
00807 template<typename CharT, typename Traits>
00808 void basic_FancyOStream_buf<CharT,Traits>::setShowLinePrefix(const bool showLinePrefix)
00809 {
00810   showLinePrefix_ = showLinePrefix;
00811 }
00812 
00813 
00814 template<typename CharT, typename Traits>
00815 bool basic_FancyOStream_buf<CharT,Traits>::getShowLinePrefix() const
00816 {
00817   return showLinePrefix_;
00818 }
00819 
00820 
00821 template<typename CharT, typename Traits>
00822 void basic_FancyOStream_buf<CharT,Traits>::setMaxLenLinePrefix(const int maxLenLinePrefix)
00823 {
00824   TEST_FOR_EXCEPT( !(maxLenLinePrefix>=5) );
00825   maxLenLinePrefix_ = maxLenLinePrefix;
00826 }
00827 
00828 
00829 template<typename CharT, typename Traits>
00830 int basic_FancyOStream_buf<CharT,Traits>::getMaxLenLinePrefix() const
00831 {
00832   return maxLenLinePrefix_;
00833 }
00834 
00835 
00836 template<typename CharT, typename Traits>
00837 void basic_FancyOStream_buf<CharT,Traits>::setShowTabCount(const bool showTabCount)
00838 {
00839   showTabCount_ = showTabCount;
00840 }
00841 
00842 
00843 template<typename CharT, typename Traits>
00844 bool basic_FancyOStream_buf<CharT,Traits>::getShowTabCount() const
00845 {
00846   return showTabCount_;
00847 }
00848 
00849 
00850 template<typename CharT, typename Traits>
00851 void basic_FancyOStream_buf<CharT,Traits>::setShowProcRank(const bool showProcRank)
00852 {
00853   showProcRank_ = showProcRank;
00854 }
00855 
00856 
00857 template<typename CharT, typename Traits>
00858 bool basic_FancyOStream_buf<CharT,Traits>::getShowProcRank() const
00859 {
00860   return showProcRank_;
00861 }
00862 
00863 
00864 template<typename CharT, typename Traits>
00865 void basic_FancyOStream_buf<CharT,Traits>::setProcRankAndSize(
00866   const int procRank, const int numProcs
00867   )
00868 {
00869   procRank_ = procRank;
00870   numProcs_ = numProcs;
00871 }
00872 
00873 
00874 template<typename CharT, typename Traits>
00875 int basic_FancyOStream_buf<CharT,Traits>::getProcRank() const
00876 {
00877   return procRank_;
00878 }
00879 
00880 
00881 template<typename CharT, typename Traits>
00882 int basic_FancyOStream_buf<CharT,Traits>::getNumProcs() const
00883 {
00884   return numProcs_;
00885 }
00886 
00887 
00888 template<typename CharT, typename Traits>
00889 void basic_FancyOStream_buf<CharT,Traits>::setOutputToRootOnly(
00890   const int rootRank
00891   )
00892 {
00893   rootRank_ = rootRank;
00894   if(rootRank >= 0) {
00895     if(rootRank == procRank_)
00896       oStream_ = oStreamSet_;
00897     else
00898       oStream_ = rcp(new oblackholestream());
00899     // Only processor is being output to so there is no need for line
00900     // batching!
00901     lineOut_ = null;
00902   }
00903   else {
00904     oStream_ = oStreamSet_;
00905     // Output is being sent to all processors so we need line batching to
00906     // insure that each line will be printed all together!
00907     lineOut_ = rcp(new std::ostringstream());
00908   }
00909 }
00910 
00911 
00912 template<typename CharT, typename Traits>
00913 int basic_FancyOStream_buf<CharT,Traits>::getOutputToRootOnly() const
00914 {
00915   return rootRank_;
00916 }
00917 
00918 
00919 template<typename CharT, typename Traits>
00920 void basic_FancyOStream_buf<CharT,Traits>::pushTab(const int tabs)
00921 {
00922   if( tabIndent_ + tabs < 0 ) {
00923     tabIndentStack_.push_back(-tabIndent_);
00924     tabIndent_ = 0;
00925   }
00926   else {
00927     tabIndentStack_.push_back(tabs);
00928     tabIndent_ += tabs;
00929   }
00930 }
00931 
00932 
00933 template<typename CharT, typename Traits>
00934 int basic_FancyOStream_buf<CharT,Traits>::getNumCurrTabs() const
00935 {
00936   return tabIndent_;
00937 }
00938 
00939 
00940 template<typename CharT, typename Traits>
00941 void basic_FancyOStream_buf<CharT,Traits>::popTab()
00942 {
00943   tabIndent_ -= tabIndentStack_.back();
00944   tabIndentStack_.pop_back();
00945 }
00946 
00947 
00948 template<typename CharT, typename Traits>
00949 void basic_FancyOStream_buf<CharT,Traits>::pushLinePrefix(
00950   const std::basic_string<char_type,traits_type> &linePrefix
00951   )
00952 {
00953   linePrefixStack_.push_back(linePrefix);
00954 }
00955 
00956 
00957 template<typename CharT, typename Traits>
00958 void basic_FancyOStream_buf<CharT,Traits>::popLinePrefix()
00959 {
00960   linePrefixStack_.pop_back();
00961 }
00962 
00963 
00964 template<typename CharT, typename Traits>
00965 const std::basic_string<CharT,Traits>&
00966 basic_FancyOStream_buf<CharT,Traits>::getTopLinePrefix() const
00967 {
00968   return linePrefixStack_.back();
00969 }
00970 
00971 
00972 template<typename CharT, typename Traits>
00973 void basic_FancyOStream_buf<CharT,Traits>::pushDisableTabbing()
00974 {
00975   ++enableTabbingStack_;
00976 }
00977 
00978 
00979 template<typename CharT, typename Traits>
00980 void basic_FancyOStream_buf<CharT,Traits>::popDisableTabbing()
00981 {
00982   --enableTabbingStack_;
00983 }
00984 
00985 
00986 // protected
00987 
00988 
00989 template<typename CharT, typename Traits>
00990 std::streamsize basic_FancyOStream_buf<CharT,Traits>::xsputn(
00991   const char_type* s, std::streamsize n
00992   )
00993 {
00994 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
00995   std::cerr << "\ncalled xsputn()\n";
00996 #endif
00997   writeChars(s,n);
00998   return n;
00999 }
01000 
01001 
01002 template<typename CharT, typename Traits>
01003 typename basic_FancyOStream_buf<CharT,Traits>::int_type 
01004 basic_FancyOStream_buf<CharT,Traits>::overflow(int_type c)
01005 {
01006 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
01007   std::cerr << "\ncalled overflow()\n";
01008 #endif
01009   if(c != traits_type::eof()) {
01010     const char_type cc[] = { traits_type::to_char_type(c) };
01011     this->writeChars(cc,1);
01012   }
01013   return traits_type::not_eof(c);
01014   //return std::basic_streambuf<CharT,Traits>::overflow(c);
01015 }
01016 
01017 
01018 // private
01019 
01020 
01021 template<typename CharT, typename Traits>
01022 std::ostream& basic_FancyOStream_buf<CharT,Traits>::out()
01023 {
01024   if(lineOut_.get())
01025     return *lineOut_;
01026   return *oStream_;
01027 }
01028 
01029 
01030 template<typename CharT, typename Traits>
01031 void basic_FancyOStream_buf<CharT,Traits>::writeChars(
01032   const char_type s[], std::streamsize n
01033   )
01034 {
01035   if(n == 0) return;
01036   std::streamsize p = 0, first_p = 0;
01037   bool done_outputting = false;
01038   const char_type newline = '\n';
01039   while( !done_outputting ) {
01040     // Find the next newline
01041     for( p = first_p; p < n; ++p ) {
01042       if(s[p] == newline) {
01043         break;
01044       }
01045     }
01046     if(p == n) {
01047       // We did not find a newline at the end!
01048       --p;
01049       done_outputting = true;
01050     }
01051     else if( p == n-1 && s[p] == newline ) {
01052       // The last character in the std::string is a newline
01053       done_outputting = true;
01054     }
01055     // Write the beginning of the line if we need to
01056     if(wroteNewline_) {
01057       writeFrontMatter();
01058       wroteNewline_ = false;
01059     }
01060     // Write up to the newline or the end of the std::string
01061     out().write(s+first_p,p-first_p+1);
01062     if(s[p] == newline) {
01063       wroteNewline_ = true;
01064       if(lineOut_.get()) {
01065         *oStream_ << lineOut_->str() << std::flush;
01066         lineOut_->str("");
01067       }
01068     }
01069     // Update for next search
01070     if(!done_outputting)
01071       first_p = p+1; 
01072   }
01073 }
01074 
01075 
01076 template<typename CharT, typename Traits>
01077 void basic_FancyOStream_buf<CharT,Traits>::writeFrontMatter()
01078 {
01079   bool didOutput = false;
01080   std::ostream &o = this->out();
01081   if(showProcRank_) {
01082     o << "p=" << std::right << std::setw(rankPrintWidth_) << procRank_;
01083     didOutput = true;
01084   }
01085   if(showLinePrefix_) {
01086     if(didOutput)
01087       o << ", ";
01088     std::string currLinePrefix = "";
01089     if ( linePrefixStack_.size() )
01090       currLinePrefix = this->getTopLinePrefix();
01091     const int localMaxLenLinePrefix =
01092       TEUCHOS_MAX( as<int>(currLinePrefix.length()), maxLenLinePrefix_ );
01093     o << std::left << std::setw(localMaxLenLinePrefix);
01094     o << currLinePrefix;
01095     didOutput = true;
01096   }
01097   if(showTabCount_) {
01098     if(didOutput)
01099       o << ", ";
01100     o << "tabs=" << std::right << std::setw(2) << tabIndent_;
01101     didOutput = true;
01102   }
01103   // ToDo: Add the Prefix name if asked
01104   // ToDo: Add the processor number if asked
01105   // ToDo: Add the number of indents if asked
01106   if(didOutput) {
01107     o << " |" << tabIndentStr_;
01108   }
01109   if(enableTabbingStack_==0) {
01110     for( int i = 0; i < tabIndent_; ++i )
01111       o << tabIndentStr_;
01112   }
01113 }
01114 
01115 
01116 //
01117 // basic_FancyOStream
01118 //
01119 
01120 
01121 template<typename CharT, typename Traits>
01122 basic_FancyOStream<CharT,Traits>::basic_FancyOStream(
01123   const RCP< std::basic_ostream<char_type,traits_type> > &oStream
01124   ,const std::basic_string<char_type,traits_type> &tabIndentStr
01125   ,const int startingTab
01126   ,const bool showLinePrefix
01127   ,const int maxLenLinePrefix
01128   ,const bool showTabCount
01129   ,const bool showProcRank
01130   )
01131   :ostream_t(NULL),
01132    streambuf_(oStream,tabIndentStr,startingTab,showLinePrefix,
01133      maxLenLinePrefix,showTabCount,showProcRank)
01134 {
01135   this->init(&streambuf_);
01136 }
01137 
01138 
01139 template<typename CharT, typename Traits>
01140 void basic_FancyOStream<CharT,Traits>::initialize(
01141   const RCP< std::basic_ostream<char_type,traits_type> > &oStream
01142   ,const std::basic_string<char_type,traits_type> &tabIndentStr
01143   ,const int startingTab
01144   ,const bool showLinePrefix
01145   ,const int maxLenLinePrefix
01146   ,const bool showTabCount
01147   ,const bool showProcRank
01148   )
01149 {
01150   streambuf_.initialize(oStream,tabIndentStr,startingTab,
01151     showLinePrefix,maxLenLinePrefix,showTabCount,showProcRank);
01152   this->init(&streambuf_);
01153 }
01154 
01155 
01156 template<typename CharT, typename Traits>
01157 RCP<std::basic_ostream<CharT,Traits> >
01158 basic_FancyOStream<CharT,Traits>::getOStream()
01159 {
01160   return streambuf_.getOStream();
01161 }
01162 
01163 
01164 template<typename CharT, typename Traits>
01165 basic_FancyOStream<CharT,Traits>&
01166 basic_FancyOStream<CharT,Traits>::setTabIndentStr(
01167   const std::basic_string<char_type,traits_type> &tabIndentStr
01168   )
01169 {
01170   streambuf_.setTabIndentStr(tabIndentStr);
01171   return *this;
01172 }
01173 
01174 
01175 template<typename CharT, typename Traits>
01176 const std::basic_string<CharT,Traits>&
01177 basic_FancyOStream<CharT,Traits>::getTabIndentStr() const
01178 {
01179   return streambuf_.getTabIndentStr();
01180 }
01181 
01182 
01183 template<typename CharT, typename Traits>
01184 basic_FancyOStream<CharT,Traits>&
01185 basic_FancyOStream<CharT,Traits>::setShowAllFrontMatter(
01186   const bool showAllFrontMatter
01187   )
01188 {
01189   streambuf_.setShowLinePrefix(showAllFrontMatter);
01190   streambuf_.setShowTabCount(showAllFrontMatter);
01191   streambuf_.setShowProcRank(showAllFrontMatter);
01192   return *this;
01193 }
01194 
01195 
01196 template<typename CharT, typename Traits>
01197 basic_FancyOStream<CharT,Traits>&
01198 basic_FancyOStream<CharT,Traits>::setShowLinePrefix(const bool showLinePrefix)
01199 {
01200   streambuf_.setShowLinePrefix(showLinePrefix);
01201   return *this;
01202 }
01203 
01204 
01205 template<typename CharT, typename Traits>
01206 basic_FancyOStream<CharT,Traits>&
01207 basic_FancyOStream<CharT,Traits>::setMaxLenLinePrefix(const int maxLenLinePrefix)
01208 {
01209   streambuf_.setMaxLenLinePrefix(maxLenLinePrefix);
01210   return *this;
01211 }
01212 
01213 
01214 template<typename CharT, typename Traits>
01215 basic_FancyOStream<CharT,Traits>&
01216 basic_FancyOStream<CharT,Traits>::setShowTabCount(const bool showTabCount)
01217 {
01218   streambuf_.setShowTabCount(showTabCount);
01219   return *this;
01220 }
01221 
01222 
01223 template<typename CharT, typename Traits>
01224 basic_FancyOStream<CharT,Traits>&
01225 basic_FancyOStream<CharT,Traits>::setShowProcRank(const bool showProcRank)
01226 {
01227   streambuf_.setShowProcRank(showProcRank);
01228   return *this;
01229 }
01230 
01231 
01232 template<typename CharT, typename Traits>
01233 basic_FancyOStream<CharT,Traits>&
01234 basic_FancyOStream<CharT,Traits>::setProcRankAndSize( const int procRank, const int numProcs )
01235 {
01236   streambuf_.setProcRankAndSize(procRank,numProcs);
01237   return *this;
01238 }
01239 
01240 
01241 template<typename CharT, typename Traits>
01242 basic_FancyOStream<CharT,Traits>&
01243 basic_FancyOStream<CharT,Traits>::setOutputToRootOnly( const int rootRank )
01244 {
01245   streambuf_.setOutputToRootOnly(rootRank);
01246   return *this;
01247 }
01248 
01249 
01250 template<typename CharT, typename Traits>
01251 int basic_FancyOStream<CharT,Traits>::getOutputToRootOnly() const
01252 {
01253   return streambuf_.getOutputToRootOnly();
01254 }
01255 
01256 
01257 template<typename CharT, typename Traits>
01258 void basic_FancyOStream<CharT,Traits>::copyAllOutputOptions(
01259   const basic_FancyOStream<CharT,Traits> &oStream )
01260 {
01261   //streambuf_.setTabIndentStr(oStream.streambuf_.getTabIndentStr());
01262   streambuf_.setShowLinePrefix(oStream.streambuf_.getShowLinePrefix());
01263   streambuf_.setMaxLenLinePrefix(oStream.streambuf_.getMaxLenLinePrefix());
01264   streambuf_.setShowTabCount(oStream.streambuf_.getShowTabCount());
01265   streambuf_.setShowProcRank(oStream.streambuf_.getShowProcRank());
01266   streambuf_.setProcRankAndSize(oStream.streambuf_.getProcRank(), 
01267     oStream.streambuf_.getNumProcs());
01268   streambuf_.setOutputToRootOnly(oStream.streambuf_.getOutputToRootOnly());
01269 }
01270 
01271 
01272 template<typename CharT, typename Traits>
01273 void basic_FancyOStream<CharT,Traits>::pushTab(const int tabs)
01274 {
01275   streambuf_.pushTab(tabs);
01276 }
01277 
01278 
01279 template<typename CharT, typename Traits>
01280 int basic_FancyOStream<CharT,Traits>::getNumCurrTabs() const
01281 {
01282   return streambuf_.getNumCurrTabs();
01283 }
01284 
01285 
01286 template<typename CharT, typename Traits>
01287 void basic_FancyOStream<CharT,Traits>::popTab()
01288 {
01289   streambuf_.popTab();
01290 }
01291 
01292 
01293 template<typename CharT, typename Traits>
01294 void basic_FancyOStream<CharT,Traits>::pushLinePrefix(
01295   const std::basic_string<char_type,traits_type> &linePrefix
01296   )
01297 {
01298   streambuf_.pushLinePrefix(linePrefix);
01299 }
01300 
01301 
01302 template<typename CharT, typename Traits>
01303 void basic_FancyOStream<CharT,Traits>::popLinePrefix()
01304 {
01305   streambuf_.popLinePrefix();
01306 }
01307 
01308 
01309 template<typename CharT, typename Traits>
01310 const std::basic_string<CharT,Traits>&
01311 basic_FancyOStream<CharT,Traits>::getTopLinePrefix() const
01312 {
01313   return streambuf_.getTopLinePrefix();
01314 }
01315 
01316 
01317 template<typename CharT, typename Traits>
01318 void basic_FancyOStream<CharT,Traits>::pushDisableTabbing()
01319 {
01320   streambuf_.pushDisableTabbing();
01321 }
01322 
01323 
01324 template<typename CharT, typename Traits>
01325 void basic_FancyOStream<CharT,Traits>::popDisableTabbing()
01326 {
01327   return streambuf_.popDisableTabbing();
01328 }
01329 
01330 
01331 } // namespace Teuchos
01332 
01333 
01334 #endif // TEUCHOS_FANCY_O_STREAM_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Tue Oct 20 10:13:59 2009 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.1