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_RefCountPtr.hpp"
00033 #include "Teuchos_GlobalMPISession.hpp"
00034 #include "Teuchos_oblackholestream.hpp"
00035 
00036 namespace Teuchos {
00037 
00038 
00047 template<typename CharT, typename Traits>
00048 class basic_FancyOStream_buf : public std::basic_streambuf<CharT,Traits>
00049 {
00050 public:
00051   
00053   typedef CharT                             char_type;
00055   typedef Traits                            traits_type;
00057   typedef typename traits_type::int_type    int_type;
00059   typedef typename traits_type::pos_type    pos_type;
00061   typedef typename traits_type::off_type    off_type;
00062 
00064   basic_FancyOStream_buf(
00065     const RefCountPtr<std::basic_ostream<char_type,traits_type> >  &oStream
00066     ,const std::basic_string<char_type,traits_type>                &tabIndentStr
00067     ,const int                                                     startingTab
00068     ,const bool                                                    showLinePrefix
00069     ,const int                                                     maxLenLinePrefix
00070     ,const bool                                                    showTabCount
00071     ,const bool                                                    showProcRank
00072     );
00073 
00075   void initialize(
00076     const RefCountPtr<std::basic_ostream<char_type,traits_type> >  &oStream
00077     ,const std::basic_string<char_type,traits_type>                &tabIndentStr
00078     ,const int                                                     startingTab
00079     ,const bool                                                    showLinePrefix
00080     ,const int                                                     maxLenLinePrefix
00081     ,const bool                                                    showTabCount
00082     ,const bool                                                    showProcRank
00083     );
00084 
00086   RefCountPtr<std::basic_ostream<char_type,traits_type> > getOStream();
00087 
00089   void setTabIndentStr(const std::basic_string<char_type,traits_type> &tabIndentStr);
00090 
00092   const std::basic_string<char_type,traits_type>& getTabIndentStr() const;
00093 
00095   void setShowLinePrefix(const bool showLinePrefix);
00096 
00098   bool getShowLinePrefix() const;
00099 
00101   void setMaxLenLinePrefix(const int maxLenLinePrefix);
00102 
00104   int getMaxLenLinePrefix() const;
00105 
00107   void setShowTabCount(const bool showTabCount);
00108 
00110   bool getShowTabCount() const;
00111 
00113   void setShowProcRank(const bool showProcRank);
00114 
00116   bool getShowProcRank() const;
00117 
00119   void setProcRankAndSize( const int procRank, const int numProcs );
00120 
00122   int getProcRank() const;
00123 
00125   int getNumProcs() const;
00126 
00128   void setOutputToRootOnly( const int rootRank );
00129 
00131   int getOutputToRootOnly() const;
00132 
00134   void pushTab(const int tabs);
00135 
00137   void popTab();
00138 
00140   void pushLinePrefix(const std::basic_string<char_type,traits_type> &linePrefix);
00141 
00143   void popLinePrefix();
00144 
00146   const std::basic_string<char_type,traits_type>& getTopLinePrefix() const;
00147 
00149   void pushDisableTabbing();
00150 
00152   void popDisableTabbing();
00153   
00154 protected:
00155   
00157 
00158 
00160   std::streamsize xsputn(const char_type* s, std::streamsize n);
00161 
00163   int_type overflow(int_type c);
00164 
00165 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
00166 
00167   void imbue(const locale& l) 
00168     {
00169       std::cerr << "\ncalled imbue()\n";
00170       std::basic_streambuf<CharT,Traits>::imbue(l);
00171     }
00172   
00173   basic_streambuf<char_type,Traits>* 
00174   setbuf(char_type* s, streamsize n)
00175     {
00176       std::cerr << "\ncalled setbuf()\n";
00177       return std::basic_streambuf<CharT,Traits>::setbuf(s,n);
00178     }
00179       
00180   pos_type 
00181   seekoff(off_type a, ios_base::seekdir b,ios_base::openmode c)
00182     {
00183       std::cerr << "\ncalled seekoff()\n";
00184       return std::basic_streambuf<CharT,Traits>::seekoff(a,b,c);
00185     }
00186 
00187   pos_type 
00188   seekpos(pos_type a, ios_base::openmode b)
00189     {
00190       std::cerr << "\ncalled seekpos()\n";
00191       return std::basic_streambuf<CharT,Traits>::seekpos(a,b);
00192     }
00193   
00194   int 
00195   sync()
00196     {
00197       std::cerr << "\ncalled sync()\n";
00198       return std::basic_streambuf<CharT,Traits>::sync();
00199     }
00200   
00201   streamsize 
00202   showmanyc()
00203     {
00204       std::cerr << "\ncalled showmanyc()\n";
00205       return std::basic_streambuf<CharT,Traits>::showmanyc();
00206     }
00207   
00208   streamsize 
00209   xsgetn(char_type* s, streamsize n)
00210     {
00211       std::cerr << "\ncalled xsgetn()\n";
00212       return std::basic_streambuf<CharT,Traits>::xsgetn(s,n);
00213     }
00214   
00215   int_type 
00216   underflow()
00217     {
00218       std::cerr << "\ncalled underflow()\n";
00219       return std::basic_streambuf<CharT,Traits>::underflow();
00220     }
00221 
00222   int_type 
00223   uflow() 
00224     {
00225       std::cerr << "\ncalled uflow()\n";
00226       return std::basic_streambuf<CharT,Traits>::uflow();
00227     }
00228 
00229   int_type 
00230   pbackfail(int_type c = traits_type::eof())
00231     {
00232       std::cerr << "\ncalled pbackfail()\n";
00233       return std::basic_streambuf<CharT,Traits>::pbackfail(c);
00234     }
00235 
00236 #endif // TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
00237 
00239 
00240 private:
00241 
00242   // ////////////////////////
00243   // Private types
00244 
00245   typedef std::basic_string<char_type,traits_type>   string_t;
00246   typedef std::deque<int>                            tabIndentStack_t;
00247   typedef std::deque<string_t>                       linePrefixStack_t;
00248 
00249   // ////////////////////////
00250   // Private data members
00251 
00252   RefCountPtr<std::basic_ostream<char_type,traits_type> >  oStreamSet_;
00253   RefCountPtr<std::basic_ostream<char_type,traits_type> >  oStream_;
00254   std::basic_string<char_type,traits_type>                 tabIndentStr_;
00255   bool                                                     showLinePrefix_;
00256   int                                                      maxLenLinePrefix_;
00257   bool                                                     showTabCount_;
00258   bool                                                     showProcRank_;
00259   int                                                      rootRank_;
00260   int                                                      procRank_;
00261   int                                                      numProcs_;
00262   int                                                      rankPrintWidth_;
00263 
00264   RefCountPtr<std::ostringstream>                          lineOut_;
00265   
00266   int                      tabIndent_;
00267   tabIndentStack_t         tabIndentStack_;
00268   linePrefixStack_t        linePrefixStack_;
00269   int                      enableTabbingStack_;
00270   
00271   bool                     wroteNewline_;
00272 
00273   // ////////////////////////
00274   // Private member functions
00275 
00276   std::ostream& out();
00277 
00278   void writeChars( const char_type s[], std::streamsize n );
00279 
00280   void writeFrontMatter();
00281 
00282   // Not defined and not to be called
00283   basic_FancyOStream_buf();
00284   basic_FancyOStream_buf(const basic_FancyOStream_buf<CharT,Traits>&);
00285   basic_FancyOStream_buf<CharT,Traits> operator=(const basic_FancyOStream_buf<CharT,Traits>&);
00286 
00287 };
00288 
00310 template <typename CharT, typename Traits = std::char_traits<CharT> >
00311 class basic_FancyOStream : public basic_ostream<CharT, Traits>
00312 {
00313 public:
00314 
00316 
00317 
00319   typedef CharT                             char_type;
00321   typedef Traits                            traits_type;
00323   typedef typename traits_type::int_type    int_type;
00325   typedef typename traits_type::pos_type    pos_type;
00327   typedef typename traits_type::off_type    off_type;
00331   typedef basic_FancyOStream_buf<CharT,Traits>  streambuf_t;
00333   typedef basic_ostream<char_type, traits_type> ostream_t;
00334 
00336 
00338 
00339 
00341   explicit
00342   basic_FancyOStream(
00343     const RefCountPtr< std::basic_ostream<char_type,traits_type> > &oStream
00344     ,const std::basic_string<char_type,traits_type>                &tabIndentStr     = "  "
00345     ,const int                                                     startingTab       = 0
00346     ,const bool                                                    showLinePrefix    = false
00347     ,const int                                                     maxLenLinePrefix  = 10
00348     ,const bool                                                    showTabCount      = false
00349     ,const bool                                                    showProcRank      = false
00350     );
00351 
00353   void initialize(
00354     const RefCountPtr< std::basic_ostream<char_type,traits_type> > &oStream
00355     ,const std::basic_string<char_type,traits_type>                &tabIndentStr     = "  "
00356     ,const int                                                     startingTab       = 0
00357     ,const bool                                                    showLinePrefix    = false
00358     ,const int                                                     maxLenLinePrefix  = 10
00359     ,const bool                                                    showTabCount      = false
00360     ,const bool                                                    showProcRank      = false
00361     );
00362 
00364   RefCountPtr<std::basic_ostream<char_type,traits_type> > getOStream();
00365 
00367   basic_FancyOStream& setTabIndentStr(const std::basic_string<char_type,traits_type> &tabIndentStr);
00368 
00370   const std::basic_string<char_type,traits_type>& getTabIndentStr() const;
00371 
00373   basic_FancyOStream& setShowAllFrontMatter(const bool showAllFrontMatter);
00374 
00376   basic_FancyOStream& setShowLinePrefix(const bool showLinePrefix);
00377 
00379   basic_FancyOStream& setMaxLenLinePrefix(const int maxLenLinePrefix);
00380 
00382   basic_FancyOStream& setShowTabCount(const bool showTabCount);
00383 
00385   basic_FancyOStream& setShowProcRank(const bool showProcRank);
00386 
00388   basic_FancyOStream& setProcRankAndSize( const int procRank, const int numProcs );
00389 
00391   basic_FancyOStream& setOutputToRootOnly( const int rootRank );
00392 
00394   int getOutputToRootOnly() const;
00395 
00397   void copyAllOutputOptions(const basic_FancyOStream<CharT,Traits> &oStream);
00398 
00400 
00402 
00403 
00405   void pushTab(const int tabs = 1);
00406 
00408   void popTab();
00409 
00411   void pushLinePrefix(const std::basic_string<char_type,traits_type> &linePrefix);
00412 
00414   void popLinePrefix();
00415 
00417   const std::basic_string<char_type,traits_type>& getTopLinePrefix() const;
00418 
00420   void pushDisableTabbing();
00421 
00423   void popDisableTabbing();
00424 
00426   
00427 private:
00428 
00429   streambuf_t streambuf_;
00430 
00431   // Not defined and not to be called
00432   basic_FancyOStream();
00433   basic_FancyOStream(const basic_FancyOStream<CharT,Traits>&);
00434   basic_FancyOStream<CharT,Traits> operator=(const basic_FancyOStream<CharT,Traits>&);
00435 
00436 };
00437 
00446 template <typename CharT, typename Traits>
00447 RefCountPtr<basic_FancyOStream<CharT,Traits> >
00448 getFancyOStream( const RefCountPtr<std::basic_ostream<CharT,Traits> > &out )
00449 {
00450   if(out.get()==NULL)
00451     return Teuchos::null;
00452   RefCountPtr<basic_FancyOStream<CharT,Traits> >
00453     fancyOut = rcp_dynamic_cast<basic_FancyOStream<CharT,Traits> >(out);
00454   if(fancyOut.get())
00455     return fancyOut;
00456   return rcp(new basic_FancyOStream<CharT,Traits>(out));
00457 }
00458 
00470 template <typename CharT, typename Traits = std::char_traits<CharT> >
00471 class basic_OSTab
00472 {
00473 public:
00474 
00476   static const int DISABLE_TABBING = -99999; // This magic number should be just fine!
00478   basic_OSTab(
00479     const RefCountPtr<basic_FancyOStream<CharT,Traits> >   &fancyOStream
00480     ,const int                                             tabs = 1
00481     ,const std::basic_string<CharT,Traits>                 linePrefix = ""
00482     )
00483     :fancyOStream_(fancyOStream)
00484     ,tabs_(tabs)
00485     ,linePrefix_(linePrefix)
00486     {
00487       updateState();
00488     }
00490   basic_OSTab(
00491     basic_FancyOStream<CharT,Traits>                       &fancyOStream
00492     ,const int                                             tabs = 1
00493     ,const std::basic_string<CharT,Traits>                 linePrefix = ""
00494     )
00495     :fancyOStream_(rcp(&fancyOStream,false))
00496     ,tabs_(tabs)
00497     ,linePrefix_(linePrefix)
00498     {
00499       updateState();
00500     }
00502   basic_OSTab( const basic_OSTab &osTab )
00503     :fancyOStream_(osTab.fancyOStream_)
00504     ,tabs_(osTab.tabs_)
00505     {
00506       updateState();
00507     }
00509   ~basic_OSTab()
00510     {
00511       if(fancyOStream_.get()) {
00512         if(tabs_==DISABLE_TABBING)
00513           fancyOStream_->popDisableTabbing();
00514         else
00515           fancyOStream_->popTab();
00516         if(linePrefix_.length()) fancyOStream_->popLinePrefix();
00517       }
00518     }
00520   basic_OSTab<CharT,Traits>& operator=( const basic_OSTab &osTab )
00521     {
00522       fancyOStream_ = osTab.fancyOStream_;
00523       tabs_ = osTab.tabs_;
00524       updateState();
00525       return *this;
00526     }
00528   basic_OSTab<CharT,Traits>& incrTab(const int tabs = 1)
00529     {
00530       tabs_ += tabs;
00531       if(fancyOStream_.get()) {
00532         fancyOStream_->popTab();
00533         fancyOStream_->pushTab(tabs_);
00534       }
00535       return *this;
00536     }
00538   RefCountPtr<basic_FancyOStream<CharT,Traits> > getOStream() const
00539     {
00540       return fancyOStream_;
00541     }
00543   basic_FancyOStream<CharT,Traits>& operator()() const
00544     {
00545       return *fancyOStream_;
00546     }
00547   
00548 private:
00549   
00550   RefCountPtr<basic_FancyOStream<CharT,Traits> >  fancyOStream_;
00551   int                                             tabs_;
00552   std::basic_string<CharT,Traits>                 linePrefix_;
00553 
00554   void updateState()
00555     {
00556       if(fancyOStream_.get()) {
00557         if(tabs_==DISABLE_TABBING)
00558           fancyOStream_->pushDisableTabbing();
00559         else
00560           fancyOStream_->pushTab(tabs_);
00561         if(linePrefix_.length()) fancyOStream_->pushLinePrefix(linePrefix_);
00562       }
00563     }
00564   
00565 };
00566 
00567 // ///////////////////////////////
00568 // Typedefs
00569 
00573 typedef basic_FancyOStream<char> FancyOStream;
00574 
00578 typedef basic_OSTab<char> OSTab;
00579 
00583 #define TEUCHOS_OSTAB ::Teuchos::OSTab __localThisTab = this->getOSTab()
00584 
00585 // ////////////////////////////////
00586 // Defintions
00587 
00588 //
00589 // basic_FancyOStream_buf
00590 //
00591 
00592 template<typename CharT, typename Traits>
00593 basic_FancyOStream_buf<CharT,Traits>::basic_FancyOStream_buf(
00594   const RefCountPtr<std::basic_ostream<char_type,traits_type> >  &oStream
00595   ,const std::basic_string<char_type,traits_type>                &tabIndentStr
00596   ,const int                                                     startingTab
00597   ,const bool                                                    showLinePrefix
00598   ,const int                                                     maxLenLinePrefix
00599   ,const bool                                                    showTabCount
00600   ,const bool                                                    showProcRank
00601   )
00602 {
00603   this->initialize(oStream,tabIndentStr,startingTab,showLinePrefix,maxLenLinePrefix,showTabCount,showProcRank);
00604 }
00605 
00606 template<typename CharT, typename Traits>
00607 void basic_FancyOStream_buf<CharT,Traits>::initialize(
00608   const RefCountPtr<std::basic_ostream<char_type,traits_type> >  &oStream
00609   ,const std::basic_string<char_type,traits_type>                &tabIndentStr
00610   ,const int                                                     startingTab
00611   ,const bool                                                    showLinePrefix
00612   ,const int                                                     maxLenLinePrefix
00613   ,const bool                                                    showTabCount
00614   ,const bool                                                    showProcRank
00615   )
00616 {
00617   oStreamSet_ = oStream;
00618   oStream_ = oStream;
00619   tabIndentStr_ = tabIndentStr;
00620   showLinePrefix_ = showLinePrefix;
00621   maxLenLinePrefix_ = maxLenLinePrefix;
00622   showTabCount_ = showTabCount;
00623   showProcRank_ = showProcRank;
00624   rootRank_ = -1;
00625   procRank_ = GlobalMPISession::getRank();
00626   numProcs_ = GlobalMPISession::getNProc();
00627   rankPrintWidth_ = int(std::log10(float(numProcs_)))+1;
00628   tabIndent_ = startingTab;
00629   tabIndentStack_.resize(0);
00630   linePrefixStack_.resize(0);
00631   wroteNewline_ = true;
00632   enableTabbingStack_ = 0;
00633 }
00634 
00635 template<typename CharT, typename Traits>
00636 RefCountPtr<std::basic_ostream<CharT,Traits> >
00637 basic_FancyOStream_buf<CharT,Traits>::getOStream()
00638 {
00639   return oStreamSet_;
00640 }
00641 
00642 template<typename CharT, typename Traits>
00643 void basic_FancyOStream_buf<CharT,Traits>::setTabIndentStr(const std::basic_string<char_type,traits_type> &tabIndentStr)
00644 {
00645   tabIndentStr_ = tabIndentStr;
00646 }
00647 
00648 template<typename CharT, typename Traits>
00649 const std::basic_string<CharT,Traits>&
00650 basic_FancyOStream_buf<CharT,Traits>::getTabIndentStr() const
00651 {
00652   return tabIndentStr_;
00653 }
00654 
00655 template<typename CharT, typename Traits>
00656 void basic_FancyOStream_buf<CharT,Traits>::setShowLinePrefix(const bool showLinePrefix)
00657 {
00658   showLinePrefix_ = showLinePrefix;
00659 }
00660 
00661 template<typename CharT, typename Traits>
00662 bool basic_FancyOStream_buf<CharT,Traits>::getShowLinePrefix() const
00663 {
00664   return showLinePrefix_;
00665 }
00666 
00667 template<typename CharT, typename Traits>
00668 void basic_FancyOStream_buf<CharT,Traits>::setMaxLenLinePrefix(const int maxLenLinePrefix)
00669 {
00670   TEST_FOR_EXCEPT( !(maxLenLinePrefix>=5) );
00671   maxLenLinePrefix_ = maxLenLinePrefix;
00672 }
00673 
00674 template<typename CharT, typename Traits>
00675 int basic_FancyOStream_buf<CharT,Traits>::getMaxLenLinePrefix() const
00676 {
00677   return maxLenLinePrefix_;
00678 }
00679 
00680 template<typename CharT, typename Traits>
00681 void basic_FancyOStream_buf<CharT,Traits>::setShowTabCount(const bool showTabCount)
00682 {
00683   showTabCount_ = showTabCount;
00684 }
00685 
00686 template<typename CharT, typename Traits>
00687 bool basic_FancyOStream_buf<CharT,Traits>::getShowTabCount() const
00688 {
00689   return showTabCount_;
00690 }
00691 
00692 template<typename CharT, typename Traits>
00693 void basic_FancyOStream_buf<CharT,Traits>::setShowProcRank(const bool showProcRank)
00694 {
00695   showProcRank_ = showProcRank;
00696 }
00697 
00698 template<typename CharT, typename Traits>
00699 bool basic_FancyOStream_buf<CharT,Traits>::getShowProcRank() const
00700 {
00701   return showProcRank_;
00702 }
00703 
00704 template<typename CharT, typename Traits>
00705 void basic_FancyOStream_buf<CharT,Traits>::setProcRankAndSize( const int procRank, const int numProcs )
00706 {
00707   procRank_ = procRank;
00708   numProcs_ = numProcs;
00709 }
00710 
00711 template<typename CharT, typename Traits>
00712 int basic_FancyOStream_buf<CharT,Traits>::getProcRank() const
00713 {
00714   return procRank_;
00715 }
00716 
00717 template<typename CharT, typename Traits>
00718 int basic_FancyOStream_buf<CharT,Traits>::getNumProcs() const
00719 {
00720   return numProcs_;
00721 }
00722 
00723 template<typename CharT, typename Traits>
00724 void basic_FancyOStream_buf<CharT,Traits>::setOutputToRootOnly( const int rootRank  )
00725 {
00726   rootRank_ = rootRank;
00727   if(rootRank >= 0) {
00728     if(rootRank == procRank_)
00729       oStream_ = oStreamSet_;
00730     else
00731       oStream_ = rcp(new oblackholestream());
00732     // Only processor is being output to so there is no need for line
00733     // batching!
00734     lineOut_ = null;
00735   }
00736   else {
00737     oStream_ = oStreamSet_;
00738     // Output is being sent to all processors so we need line batching to
00739     // insure that each line will be printed all together!
00740     lineOut_ = rcp(new std::ostringstream());
00741   }
00742 }
00743 
00744 template<typename CharT, typename Traits>
00745 int basic_FancyOStream_buf<CharT,Traits>::getOutputToRootOnly() const
00746 {
00747   return rootRank_;
00748 }
00749 
00750 template<typename CharT, typename Traits>
00751 void basic_FancyOStream_buf<CharT,Traits>::pushTab(const int tabs)
00752 {
00753   tabIndentStack_.push_back(tabs);
00754   tabIndent_ += tabs;
00755 }
00756 
00757 template<typename CharT, typename Traits>
00758 void basic_FancyOStream_buf<CharT,Traits>::popTab()
00759 {
00760   tabIndent_ -= tabIndentStack_.back();
00761   tabIndentStack_.pop_back();
00762 }
00763 
00764 template<typename CharT, typename Traits>
00765 void basic_FancyOStream_buf<CharT,Traits>::pushLinePrefix(const std::basic_string<char_type,traits_type> &linePrefix)
00766 {
00767   TEST_FOR_EXCEPT(static_cast<int>(linePrefix.length()) > maxLenLinePrefix_);
00768   linePrefixStack_.push_back(linePrefix);
00769 }
00770 
00771 template<typename CharT, typename Traits>
00772 void basic_FancyOStream_buf<CharT,Traits>::popLinePrefix()
00773 {
00774   linePrefixStack_.pop_back();
00775 }
00776 
00777 template<typename CharT, typename Traits>
00778 const std::basic_string<CharT,Traits>&
00779 basic_FancyOStream_buf<CharT,Traits>::getTopLinePrefix() const
00780 {
00781   return linePrefixStack_.back();
00782 }
00783 
00784 template<typename CharT, typename Traits>
00785 void basic_FancyOStream_buf<CharT,Traits>::pushDisableTabbing()
00786 {
00787   ++enableTabbingStack_;
00788 }
00789 
00790 template<typename CharT, typename Traits>
00791 void basic_FancyOStream_buf<CharT,Traits>::popDisableTabbing()
00792 {
00793   --enableTabbingStack_;
00794 }
00795 
00796 // protected
00797 
00798 template<typename CharT, typename Traits>
00799 std::streamsize basic_FancyOStream_buf<CharT,Traits>::xsputn(const char_type* s, std::streamsize n)
00800 {
00801 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
00802   std::cerr << "\ncalled xsputn()\n";
00803 #endif
00804   writeChars(s,n);
00805   return n;
00806 }
00807 
00808 template<typename CharT, typename Traits>
00809 typename basic_FancyOStream_buf<CharT,Traits>::int_type 
00810 basic_FancyOStream_buf<CharT,Traits>::overflow(int_type c)
00811 {
00812 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
00813   std::cerr << "\ncalled overflow()\n";
00814 #endif
00815   if(c != traits_type::eof()) {
00816     const char_type cc[] = { traits_type::to_char_type(c) };
00817     this->writeChars(cc,1);
00818   }
00819   return traits_type::not_eof(c);
00820   //return std::basic_streambuf<CharT,Traits>::overflow(c);
00821 }
00822 
00823 // private
00824 
00825 template<typename CharT, typename Traits>
00826 std::ostream& basic_FancyOStream_buf<CharT,Traits>::out()
00827 {
00828   if(lineOut_.get())
00829     return *lineOut_;
00830   return *oStream_;
00831 }
00832 
00833 template<typename CharT, typename Traits>
00834 void basic_FancyOStream_buf<CharT,Traits>::writeChars( const char_type s[], std::streamsize n )
00835 {
00836   if(n == 0) return;
00837   std::streamsize p = 0, first_p = 0;
00838   bool done_outputting = false;
00839   const char_type newline = '\n';
00840   while( !done_outputting ) {
00841     // Find the next newline
00842     for( p = first_p; p < n; ++p ) {
00843       if(s[p] == newline) {
00844         break;
00845       }
00846     }
00847     if(p == n) {
00848       // We did not find a newline at the end!
00849       --p;
00850       done_outputting = true;
00851     }
00852     else if( p == n-1 && s[p] == newline ) {
00853       // The last character in the string is a newline
00854       done_outputting = true;
00855     }
00856     // Write the beginning of the line if we need to
00857     if(wroteNewline_) {
00858       writeFrontMatter();
00859       wroteNewline_ = false;
00860     }
00861     // Write up to the newline or the end of the string
00862     out().write(s+first_p,p-first_p+1);
00863     if(s[p] == newline) {
00864       wroteNewline_ = true;
00865       if(lineOut_.get()) {
00866         *oStream_ << lineOut_->str() << std::flush;
00867         lineOut_->str("");
00868       }
00869     }
00870     // Update for next search
00871     if(!done_outputting)
00872       first_p = p+1; 
00873   }
00874 }
00875 
00876 template<typename CharT, typename Traits>
00877 void basic_FancyOStream_buf<CharT,Traits>::writeFrontMatter()
00878 {
00879   bool didOutput = false;
00880   std::ostream &out = this->out();
00881   if(showProcRank_) {
00882     out << "p=" << std::right << std::setw(rankPrintWidth_) << procRank_;
00883     didOutput = true;
00884   }
00885   if(showLinePrefix_) {
00886     if(didOutput)
00887       out << ", ";
00888     out << std::left << std::setw(maxLenLinePrefix_);
00889     if(linePrefixStack_.size()) {
00890       out << this->getTopLinePrefix();
00891     }
00892     else {
00893       out << "";
00894     }
00895     didOutput = true;
00896   }
00897   if(showTabCount_) {
00898     if(didOutput)
00899       out << ", ";
00900     out << "tabs=" << std::right << std::setw(2) << tabIndent_;
00901     didOutput = true;
00902   }
00903   // ToDo: Add the Prefix name if asked
00904   // ToDo: Add the processor number if asked
00905   // ToDo: Add the number of indents if asked
00906   if(didOutput) {
00907     out << " |" << tabIndentStr_;
00908   }
00909   if(enableTabbingStack_==0) {
00910     for( int i = 0; i < tabIndent_; ++i )
00911       out << tabIndentStr_;
00912   }
00913 }
00914 
00915 //
00916 // basic_FancyOStream
00917 //
00918 
00919 template<typename CharT, typename Traits>
00920 basic_FancyOStream<CharT,Traits>::basic_FancyOStream(
00921   const RefCountPtr< std::basic_ostream<char_type,traits_type> > &oStream
00922   ,const std::basic_string<char_type,traits_type>                &tabIndentStr
00923   ,const int                                                     startingTab
00924   ,const bool                                                    showLinePrefix
00925   ,const int                                                     maxLenLinePrefix
00926   ,const bool                                                    showTabCount
00927   ,const bool                                                    showProcRank
00928  )
00929   : ostream_t(NULL), streambuf_(oStream,tabIndentStr,startingTab,showLinePrefix,maxLenLinePrefix,showTabCount,showProcRank)
00930 {
00931   this->init(&streambuf_);
00932 }
00933 
00934 template<typename CharT, typename Traits>
00935 void basic_FancyOStream<CharT,Traits>::initialize(
00936   const RefCountPtr< std::basic_ostream<char_type,traits_type> > &oStream
00937   ,const std::basic_string<char_type,traits_type>                &tabIndentStr
00938   ,const int                                                     startingTab
00939   ,const bool                                                    showLinePrefix
00940   ,const int                                                     maxLenLinePrefix
00941   ,const bool                                                    showTabCount
00942   ,const bool                                                    showProcRank
00943   )
00944 {
00945   streambuf_.initialize(oStream,tabIndentStr,startingTab,showLinePrefix,maxLenLinePrefix,showTabCount,showProcRank);
00946   this->init(&streambuf_);
00947 }
00948 
00949 template<typename CharT, typename Traits>
00950 RefCountPtr<std::basic_ostream<CharT,Traits> >
00951 basic_FancyOStream<CharT,Traits>::getOStream()
00952 {
00953   return streambuf_.getOStream();
00954 }
00955 
00956 template<typename CharT, typename Traits>
00957 basic_FancyOStream<CharT,Traits>&
00958 basic_FancyOStream<CharT,Traits>::setTabIndentStr(const std::basic_string<char_type,traits_type> &tabIndentStr)
00959 {
00960   streambuf_.setTabIndentStr(tabIndentStr);
00961   return *this;
00962 }
00963 
00964 template<typename CharT, typename Traits>
00965 const std::basic_string<CharT,Traits>&
00966 basic_FancyOStream<CharT,Traits>::getTabIndentStr() const
00967 {
00968   return streambuf_.getTabIndentStr();
00969 }
00970 
00971 template<typename CharT, typename Traits>
00972 basic_FancyOStream<CharT,Traits>&
00973 basic_FancyOStream<CharT,Traits>::setShowAllFrontMatter(const bool showAllFrontMatter)
00974 {
00975   streambuf_.setShowLinePrefix(showAllFrontMatter);
00976   streambuf_.setShowTabCount(showAllFrontMatter);
00977   streambuf_.setShowProcRank(showAllFrontMatter);
00978   return *this;
00979 }
00980 
00981 template<typename CharT, typename Traits>
00982 basic_FancyOStream<CharT,Traits>&
00983 basic_FancyOStream<CharT,Traits>::setShowLinePrefix(const bool showLinePrefix)
00984 {
00985   streambuf_.setShowLinePrefix(showLinePrefix);
00986   return *this;
00987 }
00988 
00989 template<typename CharT, typename Traits>
00990 basic_FancyOStream<CharT,Traits>&
00991  basic_FancyOStream<CharT,Traits>::setMaxLenLinePrefix(const int maxLenLinePrefix)
00992 {
00993   streambuf_.setMaxLenLinePrefix(maxLenLinePrefix);
00994   return *this;
00995 }
00996 
00997 template<typename CharT, typename Traits>
00998 basic_FancyOStream<CharT,Traits>&
00999 basic_FancyOStream<CharT,Traits>::setShowTabCount(const bool showTabCount)
01000 {
01001   streambuf_.setShowTabCount(showTabCount);
01002   return *this;
01003 }
01004 
01005 template<typename CharT, typename Traits>
01006 basic_FancyOStream<CharT,Traits>&
01007 basic_FancyOStream<CharT,Traits>::setShowProcRank(const bool showProcRank)
01008 {
01009   streambuf_.setShowProcRank(showProcRank);
01010   return *this;
01011 }
01012 
01013 template<typename CharT, typename Traits>
01014 basic_FancyOStream<CharT,Traits>&
01015 basic_FancyOStream<CharT,Traits>::setProcRankAndSize( const int procRank, const int numProcs )
01016 {
01017   streambuf_.setProcRankAndSize(procRank,numProcs);
01018   return *this;
01019 }
01020 
01021 template<typename CharT, typename Traits>
01022 basic_FancyOStream<CharT,Traits>&
01023 basic_FancyOStream<CharT,Traits>::setOutputToRootOnly( const int rootRank )
01024 {
01025   streambuf_.setOutputToRootOnly(rootRank);
01026   return *this;
01027 }
01028 
01029 template<typename CharT, typename Traits>
01030 int basic_FancyOStream<CharT,Traits>::getOutputToRootOnly() const
01031 {
01032   return streambuf_.getOutputToRootOnly();
01033 }
01034 
01035 template<typename CharT, typename Traits>
01036 void basic_FancyOStream<CharT,Traits>::copyAllOutputOptions( const basic_FancyOStream<CharT,Traits> &oStream )
01037 {
01038   //streambuf_.setTabIndentStr(oStream.streambuf_.getTabIndentStr());
01039   streambuf_.setShowLinePrefix(oStream.streambuf_.getShowLinePrefix());
01040   streambuf_.setMaxLenLinePrefix(oStream.streambuf_.getMaxLenLinePrefix());
01041   streambuf_.setShowTabCount(oStream.streambuf_.getShowTabCount());
01042   streambuf_.setShowProcRank(oStream.streambuf_.getShowProcRank());
01043   streambuf_.setProcRankAndSize(oStream.streambuf_.getProcRank(),oStream.streambuf_.getNumProcs());
01044   streambuf_.setOutputToRootOnly(oStream.streambuf_.getOutputToRootOnly());
01045 }
01046 
01047 template<typename CharT, typename Traits>
01048 void basic_FancyOStream<CharT,Traits>::pushTab(const int tabs)
01049 {
01050   streambuf_.pushTab(tabs);
01051 }
01052 
01053 template<typename CharT, typename Traits>
01054 void basic_FancyOStream<CharT,Traits>::popTab()
01055 {
01056   streambuf_.popTab();
01057 }
01058 
01059 template<typename CharT, typename Traits>
01060 void basic_FancyOStream<CharT,Traits>::pushLinePrefix(const std::basic_string<char_type,traits_type> &linePrefix)
01061 {
01062   streambuf_.pushLinePrefix(linePrefix);
01063 }
01064 
01065 template<typename CharT, typename Traits>
01066 void basic_FancyOStream<CharT,Traits>::popLinePrefix()
01067 {
01068   streambuf_.popLinePrefix();
01069 }
01070 
01071 template<typename CharT, typename Traits>
01072 const std::basic_string<CharT,Traits>&
01073 basic_FancyOStream<CharT,Traits>::getTopLinePrefix() const
01074 {
01075   return streambuf_.getTopLinePrefix();
01076 }
01077 
01078 template<typename CharT, typename Traits>
01079 void basic_FancyOStream<CharT,Traits>::pushDisableTabbing()
01080 {
01081   streambuf_.pushDisableTabbing();
01082 }
01083 
01084 template<typename CharT, typename Traits>
01085 void basic_FancyOStream<CharT,Traits>::popDisableTabbing()
01086 {
01087   return streambuf_.popDisableTabbing();
01088 }
01089 
01090 } // namespace Teuchos
01091 
01092 #endif // TEUCHOS_FANCY_O_STREAM_HPP

Generated on Thu Sep 18 12:30:29 2008 for Teuchos - Trilinos Tools Package by doxygen 1.3.9.1