Teuchos Package Browser (Single Doxygen Collection) Version of the Day
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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #ifndef TEUCHOS_FANCY_O_STREAM_HPP
00043 #define TEUCHOS_FANCY_O_STREAM_HPP
00044 
00045 #include "Teuchos_RCP.hpp"
00046 #include "Teuchos_GlobalMPISession.hpp"
00047 #include "Teuchos_oblackholestream.hpp"
00048 #include "Teuchos_as.hpp"
00049 
00050 
00051 namespace Teuchos {
00052 
00053 
00062 template<typename CharT, typename Traits>
00063 class basic_FancyOStream_buf : public std::basic_streambuf<CharT,Traits>
00064 {
00065 public:
00066   
00068   typedef CharT char_type;
00070   typedef Traits           traits_type;
00072   typedef typename traits_type::int_type    int_type;
00074   typedef typename traits_type::pos_type    pos_type;
00076   typedef typename traits_type::off_type    off_type;
00077 
00079   basic_FancyOStream_buf(
00080     const RCP<std::basic_ostream<char_type,traits_type> > &oStream
00081     ,const std::basic_string<char_type,traits_type> &tabIndentStr
00082     ,const int startingTab
00083     ,const bool showLinePrefix
00084     ,const int maxLenLinePrefix
00085     ,const bool showTabCount
00086     ,const bool showProcRank
00087     );
00088 
00090   void initialize(
00091     const RCP<std::basic_ostream<char_type,traits_type> > &oStream
00092     ,const std::basic_string<char_type,traits_type> &tabIndentStr
00093     ,const int startingTab
00094     ,const bool showLinePrefix
00095     ,const int maxLenLinePrefix
00096     ,const bool showTabCount
00097     ,const bool showProcRank
00098     );
00099 
00101   RCP<std::basic_ostream<char_type,traits_type> > getOStream();
00102 
00104   void setTabIndentStr(const std::basic_string<char_type,traits_type> &tabIndentStr);
00105 
00107   const std::basic_string<char_type,traits_type>& getTabIndentStr() const;
00108 
00110   void setShowLinePrefix(const bool showLinePrefix);
00111 
00113   bool getShowLinePrefix() const;
00114 
00116   void setMaxLenLinePrefix(const int maxLenLinePrefix);
00117 
00119   int getMaxLenLinePrefix() const;
00120 
00122   void setShowTabCount(const bool showTabCount);
00123 
00125   bool getShowTabCount() const;
00126 
00128   void setShowProcRank(const bool showProcRank);
00129 
00131   bool getShowProcRank() const;
00132 
00134   void setProcRankAndSize( const int procRank, const int numProcs );
00135 
00137   int getProcRank() const;
00138 
00140   int getNumProcs() const;
00141 
00143   void setOutputToRootOnly( const int rootRank );
00144 
00146   int getOutputToRootOnly() const;
00147 
00149   void pushTab(const int tabs);
00150 
00152   int getNumCurrTabs() const;
00153 
00155   void popTab();
00156 
00158   void pushLinePrefix(
00159     const std::basic_string<char_type,traits_type> &linePrefix
00160     );
00161 
00163   void popLinePrefix();
00164 
00166   const std::basic_string<char_type,traits_type>& getTopLinePrefix() const;
00167 
00169   void pushDisableTabbing();
00170 
00172   void popDisableTabbing();
00173  
00174 protected:
00175  
00177 
00178 
00180   std::streamsize xsputn(const char_type* s, std::streamsize n);
00181 
00183   int_type overflow(int_type c);
00184 
00185 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
00186 
00187   void imbue(const locale& l) 
00188     {
00189       std::cerr << "\ncalled imbue()\n";
00190       std::basic_streambuf<CharT,Traits>::imbue(l);
00191     }
00192  
00193   basic_streambuf<char_type,Traits>* 
00194   setbuf(char_type* s, streamsize n)
00195     {
00196       std::cerr << "\ncalled setbuf()\n";
00197       return std::basic_streambuf<CharT,Traits>::setbuf(s,n);
00198     }
00199  
00200   pos_type 
00201   seekoff(off_type a, ios_base::seekdir b,ios_base::openmode c)
00202     {
00203       std::cerr << "\ncalled seekoff()\n";
00204       return std::basic_streambuf<CharT,Traits>::seekoff(a,b,c);
00205     }
00206 
00207   pos_type 
00208   seekpos(pos_type a, ios_base::openmode b)
00209     {
00210       std::cerr << "\ncalled seekpos()\n";
00211       return std::basic_streambuf<CharT,Traits>::seekpos(a,b);
00212     }
00213  
00214   int 
00215   sync()
00216     {
00217       std::cerr << "\ncalled sync()\n";
00218       return std::basic_streambuf<CharT,Traits>::sync();
00219     }
00220  
00221   streamsize 
00222   showmanyc()
00223     {
00224       std::cerr << "\ncalled showmanyc()\n";
00225       return std::basic_streambuf<CharT,Traits>::showmanyc();
00226     }
00227  
00228   streamsize 
00229   xsgetn(char_type* s, streamsize n)
00230     {
00231       std::cerr << "\ncalled xsgetn()\n";
00232       return std::basic_streambuf<CharT,Traits>::xsgetn(s,n);
00233     }
00234  
00235   int_type 
00236   underflow()
00237     {
00238       std::cerr << "\ncalled underflow()\n";
00239       return std::basic_streambuf<CharT,Traits>::underflow();
00240     }
00241 
00242   int_type 
00243   uflow() 
00244     {
00245       std::cerr << "\ncalled uflow()\n";
00246       return std::basic_streambuf<CharT,Traits>::uflow();
00247     }
00248 
00249   int_type 
00250   pbackfail(int_type c = traits_type::eof())
00251     {
00252       std::cerr << "\ncalled pbackfail()\n";
00253       return std::basic_streambuf<CharT,Traits>::pbackfail(c);
00254     }
00255 
00256 #endif // TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
00257 
00259 
00260 private:
00261 
00262   // ////////////////////////
00263   // Private types
00264 
00265   typedef std::basic_string<char_type,traits_type> string_t;
00266   typedef std::deque<int> tabIndentStack_t;
00267   typedef std::deque<string_t> linePrefixStack_t;
00268 
00269   // ////////////////////////
00270   // Private data members
00271 
00272   RCP<std::basic_ostream<char_type,traits_type> > oStreamSet_;
00273   RCP<std::basic_ostream<char_type,traits_type> > oStream_;
00274   std::basic_string<char_type,traits_type> tabIndentStr_;
00275   bool showLinePrefix_;
00276   int maxLenLinePrefix_;
00277   bool showTabCount_;
00278   bool showProcRank_;
00279   int rootRank_;
00280   int procRank_;
00281   int numProcs_;
00282   int rankPrintWidth_;
00283 
00284   RCP<std::ostringstream> lineOut_;
00285  
00286   int tabIndent_;
00287   tabIndentStack_t tabIndentStack_;
00288   linePrefixStack_t linePrefixStack_;
00289   int enableTabbingStack_;
00290  
00291   bool wroteNewline_;
00292 
00293   // ////////////////////////
00294   // Private member functions
00295 
00296   std::ostream& out();
00297 
00298   void writeChars( const char_type s[], std::streamsize n );
00299 
00300   void writeFrontMatter();
00301 
00302   // Not defined and not to be called
00303   basic_FancyOStream_buf();
00304   basic_FancyOStream_buf(const basic_FancyOStream_buf<CharT,Traits>&);
00305   basic_FancyOStream_buf<CharT,Traits> operator=(
00306     const basic_FancyOStream_buf<CharT,Traits>&
00307     );
00308 
00309 };
00310 
00311 
00333 template <typename CharT, typename Traits = std::char_traits<CharT> >
00334 class basic_FancyOStream : public std::basic_ostream<CharT, Traits>
00335 {
00336 public:
00337 
00339 
00340 
00342   typedef CharT char_type;
00344   typedef Traits traits_type;
00346   typedef typename traits_type::int_type int_type;
00348   typedef typename traits_type::pos_type pos_type;
00350   typedef typename traits_type::off_type off_type;
00354   typedef basic_FancyOStream_buf<CharT,Traits> streambuf_t;
00356   typedef std::basic_ostream<char_type, traits_type> ostream_t;
00357 
00359 
00361 
00362 
00364   explicit
00365   basic_FancyOStream(
00366     const RCP< std::basic_ostream<char_type,traits_type> > &oStream
00367     ,const std::basic_string<char_type,traits_type> &tabIndentStr = " "
00368     ,const int startingTab = 0
00369     ,const bool showLinePrefix = false
00370     ,const int maxLenLinePrefix = 10
00371     ,const bool showTabCount = false
00372     ,const bool showProcRank = false
00373     );
00374 
00376   void initialize(
00377     const RCP< std::basic_ostream<char_type,traits_type> > &oStream
00378     ,const std::basic_string<char_type,traits_type> &tabIndentStr = " "
00379     ,const int startingTab = 0
00380     ,const bool showLinePrefix = false
00381     ,const int maxLenLinePrefix = 10
00382     ,const bool showTabCount = false
00383     ,const bool showProcRank = false
00384     );
00385 
00387   RCP<std::basic_ostream<char_type,traits_type> > getOStream();
00388 
00390   basic_FancyOStream& setTabIndentStr(
00391     const std::basic_string<char_type,traits_type> &tabIndentStr
00392     );
00393 
00395   const std::basic_string<char_type,traits_type>& getTabIndentStr() const;
00396 
00398   basic_FancyOStream& setShowAllFrontMatter(const bool showAllFrontMatter);
00399 
00401   basic_FancyOStream& setShowLinePrefix(const bool showLinePrefix);
00402 
00404   basic_FancyOStream& setMaxLenLinePrefix(const int maxLenLinePrefix);
00405 
00407   basic_FancyOStream& setShowTabCount(const bool showTabCount);
00408 
00410   basic_FancyOStream& setShowProcRank(const bool showProcRank);
00411 
00413   basic_FancyOStream& setProcRankAndSize( const int procRank, const int numProcs );
00414 
00416   basic_FancyOStream& setOutputToRootOnly( const int rootRank );
00417 
00419   int getOutputToRootOnly() const;
00420 
00422   void copyAllOutputOptions(const basic_FancyOStream<CharT,Traits> &oStream);
00423 
00425 
00427 
00428 
00430   void pushTab(const int tabs = 1);
00431 
00433   int getNumCurrTabs() const;
00434 
00436   void popTab();
00437 
00439   void pushLinePrefix(const std::basic_string<char_type,traits_type> &linePrefix);
00440 
00442   void popLinePrefix();
00443 
00445   const std::basic_string<char_type,traits_type>& getTopLinePrefix() const;
00446 
00448   void pushDisableTabbing();
00449 
00451   void popDisableTabbing();
00452 
00454  
00455 private:
00456 
00457   streambuf_t streambuf_;
00458 
00459   // Not defined and not to be called
00460   basic_FancyOStream();
00461   basic_FancyOStream(const basic_FancyOStream<CharT,Traits>&);
00462   basic_FancyOStream<CharT,Traits> operator=(const basic_FancyOStream<CharT,Traits>&);
00463 
00464 };
00465 
00466 
00474 inline
00475 RCP<basic_FancyOStream<char> >
00476 fancyOStream(
00477   const RCP< std::basic_ostream<char> >& oStream,
00478   const std::basic_string<char>& tabIndentStr = " ",
00479   const int startingTab = 0,
00480   const bool showLinePrefix = false,
00481   const int maxLenLinePrefix = 10,
00482   const bool showTabCount = false,
00483   const bool showProcRank = false
00484   )
00485 {
00486   if (nonnull(oStream)) {
00487     return rcp(
00488       new basic_FancyOStream<char>(
00489         oStream,tabIndentStr,startingTab,showLinePrefix,
00490         maxLenLinePrefix,showTabCount,showProcRank
00491         )
00492       );
00493   }
00494   return null;
00495 }
00496 
00497 
00506 inline
00507 RCP<basic_FancyOStream<char> >
00508 getFancyOStream( const RCP<std::basic_ostream<char> > &out )
00509 {
00510   if (is_null(out))
00511     return Teuchos::null;
00512   RCP<basic_FancyOStream<char> >
00513     fancyOut = rcp_dynamic_cast<basic_FancyOStream<char> >(out);
00514   if(nonnull(fancyOut))
00515     return fancyOut;
00516   return rcp(new basic_FancyOStream<char>(out));
00517 }
00518 
00519 
00531 template <typename CharT, typename Traits = std::char_traits<CharT> >
00532 class basic_OSTab
00533 {
00534 public:
00535 
00537   static const int DISABLE_TABBING = -99999; // This magic number should be just fine!
00539   basic_OSTab(
00540     const RCP<basic_FancyOStream<CharT,Traits> > &fancyOStream
00541     ,const int tabs = 1
00542     ,const std::basic_string<CharT,Traits> linePrefix = ""
00543     )
00544     :fancyOStream_(fancyOStream)
00545     ,tabs_(tabs)
00546     ,linePrefix_(linePrefix)
00547     {
00548       updateState();
00549     }
00551   basic_OSTab(
00552     const RCP<std::basic_ostream<CharT,Traits> > &oStream
00553     ,const int tabs = 1
00554     ,const std::basic_string<CharT,Traits> linePrefix = ""
00555     )
00556     :fancyOStream_(getFancyOStream(oStream))
00557     ,tabs_(tabs)
00558     ,linePrefix_(linePrefix)
00559     {
00560       updateState();
00561     }
00563   basic_OSTab(
00564     basic_FancyOStream<CharT,Traits> &fancyOStream
00565     ,const int tabs = 1
00566     ,const std::basic_string<CharT,Traits> linePrefix = ""
00567     )
00568     :fancyOStream_(rcp(&fancyOStream,false))
00569     ,tabs_(tabs)
00570     ,linePrefix_(linePrefix)
00571     {
00572       updateState();
00573     }
00575   basic_OSTab(
00576     std::basic_ostream<CharT,Traits> &oStream
00577     ,const int tabs = 1
00578     ,const std::basic_string<CharT,Traits> linePrefix = ""
00579     )
00580     :fancyOStream_(getFancyOStream(rcp(&oStream, false)))
00581     ,tabs_(tabs)
00582     ,linePrefix_(linePrefix)
00583     {
00584       updateState();
00585     }
00587   basic_OSTab( const basic_OSTab &osTab )
00588     :fancyOStream_(osTab.fancyOStream_)
00589     ,tabs_(osTab.tabs_)
00590     {
00591       updateState();
00592     }
00594   ~basic_OSTab()
00595     {
00596       if(fancyOStream_.get()) {
00597         if(tabs_==DISABLE_TABBING)
00598           fancyOStream_->popDisableTabbing();
00599         else
00600           fancyOStream_->popTab();
00601         if(linePrefix_.length()) fancyOStream_->popLinePrefix();
00602       }
00603     }
00605   basic_OSTab<CharT,Traits>& operator=( const basic_OSTab &osTab )
00606     {
00607       fancyOStream_ = osTab.fancyOStream_;
00608       tabs_ = osTab.tabs_;
00609       updateState();
00610       return *this;
00611     }
00613   basic_OSTab<CharT,Traits>& incrTab(const int tabs = 1)
00614     {
00615       tabs_ += tabs;
00616       if(fancyOStream_.get()) {
00617         fancyOStream_->popTab();
00618         fancyOStream_->pushTab(tabs_);
00619       }
00620       return *this;
00621     }
00623   basic_FancyOStream<CharT,Traits>& o() const
00624     {
00625       return *fancyOStream_;
00626     }
00628   basic_FancyOStream<CharT,Traits>* get() const
00629     {
00630       return fancyOStream_.get();
00631     }
00632  
00633 private:
00634  
00635   RCP<basic_FancyOStream<CharT,Traits> > fancyOStream_;
00636   int tabs_;
00637   std::basic_string<CharT,Traits> linePrefix_;
00638 
00639   void updateState()
00640     {
00641       if(fancyOStream_.get()) {
00642         if(tabs_==DISABLE_TABBING)
00643           fancyOStream_->pushDisableTabbing();
00644         else
00645           fancyOStream_->pushTab(tabs_);
00646         if(linePrefix_.length()) fancyOStream_->pushLinePrefix(linePrefix_);
00647       }
00648     }
00649  
00650 };
00651 
00652 
00668 template <typename CharT, typename Traits>
00669 RCP<basic_FancyOStream<CharT,Traits> >
00670 tab(
00671   const RCP<basic_FancyOStream<CharT,Traits> > &out,
00672   const int tabs = 1,
00673   const std::basic_string<CharT,Traits> linePrefix = ""
00674   )
00675 {
00676   if(out.get()==NULL)
00677     return Teuchos::null;
00678   RCP<basic_FancyOStream<CharT,Traits> > fancyOut = out;
00679   set_extra_data(
00680     rcp(new basic_OSTab<CharT,Traits>(out,tabs,linePrefix)),
00681     "OSTab",
00682     inOutArg(fancyOut),
00683     PRE_DESTROY,
00684     false
00685     );
00686   return fancyOut;
00687 }
00688 
00689 
00705 template <typename CharT, typename Traits>
00706 RCP<basic_FancyOStream<CharT,Traits> >
00707 tab(
00708   const RCP<std::basic_ostream<CharT,Traits> > &out
00709   ,const int tabs = 1
00710   ,const std::basic_string<CharT,Traits> linePrefix = ""
00711   )
00712 {
00713   return tab(getFancyOStream(out),tabs,linePrefix);
00714 }
00715 
00716 
00717 // ///////////////////////////////
00718 // Typedefs
00719 
00720 
00724 typedef basic_FancyOStream<char> FancyOStream;
00725 
00726 
00730 typedef basic_OSTab<char> OSTab;
00731 
00732 
00736 #define TEUCHOS_OSTAB ::Teuchos::OSTab __localThisTab = this->getOSTab()
00737 
00741 #define TEUCHOS_OSTAB_DIFF( DIFF ) ::Teuchos::OSTab DIFF ## __localThisTab = this->getOSTab()
00742 
00743 
00744 // ////////////////////////////////
00745 // Defintions
00746 
00747 
00748 //
00749 // basic_FancyOStream_buf
00750 //
00751 
00752 
00753 template<typename CharT, typename Traits>
00754 basic_FancyOStream_buf<CharT,Traits>::basic_FancyOStream_buf(
00755   const RCP<std::basic_ostream<char_type,traits_type> > &oStream
00756   ,const std::basic_string<char_type,traits_type> &tabIndentStr
00757   ,const int startingTab
00758   ,const bool showLinePrefix
00759   ,const int maxLenLinePrefix
00760   ,const bool showTabCount
00761   ,const bool showProcRank
00762   )
00763 {
00764   this->initialize(oStream,tabIndentStr,startingTab,showLinePrefix,
00765     maxLenLinePrefix,showTabCount,showProcRank);
00766 }
00767 
00768 
00769 template<typename CharT, typename Traits>
00770 void basic_FancyOStream_buf<CharT,Traits>::initialize(
00771   const RCP<std::basic_ostream<char_type,traits_type> > &oStream
00772   ,const std::basic_string<char_type,traits_type> &tabIndentStr
00773   ,const int startingTab
00774   ,const bool showLinePrefix
00775   ,const int maxLenLinePrefix
00776   ,const bool showTabCount
00777   ,const bool showProcRank
00778   )
00779 {
00780   oStreamSet_ = oStream;
00781   oStream_ = oStream;
00782   tabIndentStr_ = tabIndentStr;
00783   showLinePrefix_ = showLinePrefix;
00784   maxLenLinePrefix_ = maxLenLinePrefix;
00785   showTabCount_ = showTabCount;
00786   showProcRank_ = showProcRank;
00787   rootRank_ = -1;
00788   procRank_ = GlobalMPISession::getRank();
00789   numProcs_ = GlobalMPISession::getNProc();
00790   rankPrintWidth_ = int(std::log10(float(numProcs_)))+1;
00791   tabIndent_ = startingTab;
00792   tabIndentStack_.clear();
00793   linePrefixStack_.clear();
00794   wroteNewline_ = true;
00795   enableTabbingStack_ = 0;
00796 }
00797 
00798 
00799 template<typename CharT, typename Traits>
00800 RCP<std::basic_ostream<CharT,Traits> >
00801 basic_FancyOStream_buf<CharT,Traits>::getOStream()
00802 {
00803   return oStreamSet_;
00804 }
00805 
00806 
00807 template<typename CharT, typename Traits>
00808 void basic_FancyOStream_buf<CharT,Traits>::setTabIndentStr(
00809   const std::basic_string<char_type,traits_type> &tabIndentStr
00810   )
00811 {
00812   tabIndentStr_ = tabIndentStr;
00813 }
00814 
00815 
00816 template<typename CharT, typename Traits>
00817 const std::basic_string<CharT,Traits>&
00818 basic_FancyOStream_buf<CharT,Traits>::getTabIndentStr() const
00819 {
00820   return tabIndentStr_;
00821 }
00822 
00823 
00824 template<typename CharT, typename Traits>
00825 void basic_FancyOStream_buf<CharT,Traits>::setShowLinePrefix(const bool showLinePrefix)
00826 {
00827   showLinePrefix_ = showLinePrefix;
00828 }
00829 
00830 
00831 template<typename CharT, typename Traits>
00832 bool basic_FancyOStream_buf<CharT,Traits>::getShowLinePrefix() const
00833 {
00834   return showLinePrefix_;
00835 }
00836 
00837 
00838 template<typename CharT, typename Traits>
00839 void basic_FancyOStream_buf<CharT,Traits>::setMaxLenLinePrefix(const int maxLenLinePrefix)
00840 {
00841   TEUCHOS_TEST_FOR_EXCEPT( !(maxLenLinePrefix>=5) );
00842   maxLenLinePrefix_ = maxLenLinePrefix;
00843 }
00844 
00845 
00846 template<typename CharT, typename Traits>
00847 int basic_FancyOStream_buf<CharT,Traits>::getMaxLenLinePrefix() const
00848 {
00849   return maxLenLinePrefix_;
00850 }
00851 
00852 
00853 template<typename CharT, typename Traits>
00854 void basic_FancyOStream_buf<CharT,Traits>::setShowTabCount(const bool showTabCount)
00855 {
00856   showTabCount_ = showTabCount;
00857 }
00858 
00859 
00860 template<typename CharT, typename Traits>
00861 bool basic_FancyOStream_buf<CharT,Traits>::getShowTabCount() const
00862 {
00863   return showTabCount_;
00864 }
00865 
00866 
00867 template<typename CharT, typename Traits>
00868 void basic_FancyOStream_buf<CharT,Traits>::setShowProcRank(const bool showProcRank)
00869 {
00870   showProcRank_ = showProcRank;
00871 }
00872 
00873 
00874 template<typename CharT, typename Traits>
00875 bool basic_FancyOStream_buf<CharT,Traits>::getShowProcRank() const
00876 {
00877   return showProcRank_;
00878 }
00879 
00880 
00881 template<typename CharT, typename Traits>
00882 void basic_FancyOStream_buf<CharT,Traits>::setProcRankAndSize(
00883   const int procRank, const int numProcs
00884   )
00885 {
00886   procRank_ = procRank;
00887   numProcs_ = numProcs;
00888 }
00889 
00890 
00891 template<typename CharT, typename Traits>
00892 int basic_FancyOStream_buf<CharT,Traits>::getProcRank() const
00893 {
00894   return procRank_;
00895 }
00896 
00897 
00898 template<typename CharT, typename Traits>
00899 int basic_FancyOStream_buf<CharT,Traits>::getNumProcs() const
00900 {
00901   return numProcs_;
00902 }
00903 
00904 
00905 template<typename CharT, typename Traits>
00906 void basic_FancyOStream_buf<CharT,Traits>::setOutputToRootOnly(
00907   const int rootRank
00908   )
00909 {
00910   rootRank_ = rootRank;
00911   if(rootRank >= 0) {
00912     if(rootRank == procRank_)
00913       oStream_ = oStreamSet_;
00914     else
00915       oStream_ = rcp(new oblackholestream());
00916     // Only processor is being output to so there is no need for line
00917     // batching!
00918     lineOut_ = null;
00919   }
00920   else {
00921     oStream_ = oStreamSet_;
00922     // Output is being sent to all processors so we need line batching to
00923     // insure that each line will be printed all together!
00924     lineOut_ = rcp(new std::ostringstream());
00925   }
00926 }
00927 
00928 
00929 template<typename CharT, typename Traits>
00930 int basic_FancyOStream_buf<CharT,Traits>::getOutputToRootOnly() const
00931 {
00932   return rootRank_;
00933 }
00934 
00935 
00936 template<typename CharT, typename Traits>
00937 void basic_FancyOStream_buf<CharT,Traits>::pushTab(const int tabs)
00938 {
00939   if( tabIndent_ + tabs < 0 ) {
00940     tabIndentStack_.push_back(-tabIndent_);
00941     tabIndent_ = 0;
00942   }
00943   else {
00944     tabIndentStack_.push_back(tabs);
00945     tabIndent_ += tabs;
00946   }
00947 }
00948 
00949 
00950 template<typename CharT, typename Traits>
00951 int basic_FancyOStream_buf<CharT,Traits>::getNumCurrTabs() const
00952 {
00953   return tabIndent_;
00954 }
00955 
00956 
00957 template<typename CharT, typename Traits>
00958 void basic_FancyOStream_buf<CharT,Traits>::popTab()
00959 {
00960   tabIndent_ -= tabIndentStack_.back();
00961   tabIndentStack_.pop_back();
00962 }
00963 
00964 
00965 template<typename CharT, typename Traits>
00966 void basic_FancyOStream_buf<CharT,Traits>::pushLinePrefix(
00967   const std::basic_string<char_type,traits_type> &linePrefix
00968   )
00969 {
00970   linePrefixStack_.push_back(linePrefix);
00971 }
00972 
00973 
00974 template<typename CharT, typename Traits>
00975 void basic_FancyOStream_buf<CharT,Traits>::popLinePrefix()
00976 {
00977   linePrefixStack_.pop_back();
00978 }
00979 
00980 
00981 template<typename CharT, typename Traits>
00982 const std::basic_string<CharT,Traits>&
00983 basic_FancyOStream_buf<CharT,Traits>::getTopLinePrefix() const
00984 {
00985   return linePrefixStack_.back();
00986 }
00987 
00988 
00989 template<typename CharT, typename Traits>
00990 void basic_FancyOStream_buf<CharT,Traits>::pushDisableTabbing()
00991 {
00992   ++enableTabbingStack_;
00993 }
00994 
00995 
00996 template<typename CharT, typename Traits>
00997 void basic_FancyOStream_buf<CharT,Traits>::popDisableTabbing()
00998 {
00999   --enableTabbingStack_;
01000 }
01001 
01002 
01003 // protected
01004 
01005 
01006 template<typename CharT, typename Traits>
01007 std::streamsize basic_FancyOStream_buf<CharT,Traits>::xsputn(
01008   const char_type* s, std::streamsize n
01009   )
01010 {
01011 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
01012   std::cerr << "\ncalled xsputn()\n";
01013 #endif
01014   writeChars(s,n);
01015   return n;
01016 }
01017 
01018 
01019 template<typename CharT, typename Traits>
01020 typename basic_FancyOStream_buf<CharT,Traits>::int_type 
01021 basic_FancyOStream_buf<CharT,Traits>::overflow(int_type c)
01022 {
01023 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
01024   std::cerr << "\ncalled overflow()\n";
01025 #endif
01026   if(c != traits_type::eof()) {
01027     const char_type cc[] = { traits_type::to_char_type(c) };
01028     this->writeChars(cc,1);
01029   }
01030   return traits_type::not_eof(c);
01031   //return std::basic_streambuf<CharT,Traits>::overflow(c);
01032 }
01033 
01034 
01035 // private
01036 
01037 
01038 template<typename CharT, typename Traits>
01039 std::ostream& basic_FancyOStream_buf<CharT,Traits>::out()
01040 {
01041   if(lineOut_.get())
01042     return *lineOut_;
01043   return *oStream_;
01044 }
01045 
01046 
01047 template<typename CharT, typename Traits>
01048 void basic_FancyOStream_buf<CharT,Traits>::writeChars(
01049   const char_type s[], std::streamsize n
01050   )
01051 {
01052   if(n == 0) return;
01053   std::streamsize p = 0, first_p = 0;
01054   bool done_outputting = false;
01055   const char_type newline = '\n';
01056   while( !done_outputting ) {
01057     // Find the next newline
01058     for( p = first_p; p < n; ++p ) {
01059       if(s[p] == newline) {
01060         break;
01061       }
01062     }
01063     if(p == n) {
01064       // We did not find a newline at the end!
01065       --p;
01066       done_outputting = true;
01067     }
01068     else if( p == n-1 && s[p] == newline ) {
01069       // The last character in the std::string is a newline
01070       done_outputting = true;
01071     }
01072     // Write the beginning of the line if we need to
01073     if(wroteNewline_) {
01074       writeFrontMatter();
01075       wroteNewline_ = false;
01076     }
01077     // Write up to the newline or the end of the std::string
01078     out().write(s+first_p,p-first_p+1);
01079     if(s[p] == newline) {
01080       wroteNewline_ = true;
01081       if(lineOut_.get()) {
01082         *oStream_ << lineOut_->str() << std::flush;
01083         lineOut_->str("");
01084       }
01085     }
01086     // Update for next search
01087     if(!done_outputting)
01088       first_p = p+1; 
01089   }
01090 }
01091 
01092 
01093 template<typename CharT, typename Traits>
01094 void basic_FancyOStream_buf<CharT,Traits>::writeFrontMatter()
01095 {
01096   bool didOutput = false;
01097   std::ostream &o = this->out();
01098   if(showProcRank_) {
01099     o << "p=" << std::right << std::setw(rankPrintWidth_) << procRank_;
01100     didOutput = true;
01101   }
01102   if(showLinePrefix_) {
01103     if(didOutput)
01104       o << ", ";
01105     std::string currLinePrefix = "";
01106     if ( linePrefixStack_.size() )
01107       currLinePrefix = this->getTopLinePrefix();
01108     const int localMaxLenLinePrefix =
01109       TEUCHOS_MAX( as<int>(currLinePrefix.length()), maxLenLinePrefix_ );
01110     o << std::left << std::setw(localMaxLenLinePrefix);
01111     o << currLinePrefix;
01112     didOutput = true;
01113   }
01114   if(showTabCount_) {
01115     if(didOutput)
01116       o << ", ";
01117     o << "tabs=" << std::right << std::setw(2) << tabIndent_;
01118     didOutput = true;
01119   }
01120   // ToDo: Add the Prefix name if asked
01121   // ToDo: Add the processor number if asked
01122   // ToDo: Add the number of indents if asked
01123   if(didOutput) {
01124     o << " |" << tabIndentStr_;
01125   }
01126   if(enableTabbingStack_==0) {
01127     for( int i = 0; i < tabIndent_; ++i )
01128       o << tabIndentStr_;
01129   }
01130 }
01131 
01132 
01133 //
01134 // basic_FancyOStream
01135 //
01136 
01137 
01138 template<typename CharT, typename Traits>
01139 basic_FancyOStream<CharT,Traits>::basic_FancyOStream(
01140   const RCP< std::basic_ostream<char_type,traits_type> > &oStream
01141   ,const std::basic_string<char_type,traits_type> &tabIndentStr
01142   ,const int startingTab
01143   ,const bool showLinePrefix
01144   ,const int maxLenLinePrefix
01145   ,const bool showTabCount
01146   ,const bool showProcRank
01147   )
01148   :ostream_t(NULL),
01149    streambuf_(oStream,tabIndentStr,startingTab,showLinePrefix,
01150      maxLenLinePrefix,showTabCount,showProcRank)
01151 {
01152   this->init(&streambuf_);
01153 }
01154 
01155 
01156 template<typename CharT, typename Traits>
01157 void basic_FancyOStream<CharT,Traits>::initialize(
01158   const RCP< std::basic_ostream<char_type,traits_type> > &oStream
01159   ,const std::basic_string<char_type,traits_type> &tabIndentStr
01160   ,const int startingTab
01161   ,const bool showLinePrefix
01162   ,const int maxLenLinePrefix
01163   ,const bool showTabCount
01164   ,const bool showProcRank
01165   )
01166 {
01167   streambuf_.initialize(oStream,tabIndentStr,startingTab,
01168     showLinePrefix,maxLenLinePrefix,showTabCount,showProcRank);
01169   this->init(&streambuf_);
01170 }
01171 
01172 
01173 template<typename CharT, typename Traits>
01174 RCP<std::basic_ostream<CharT,Traits> >
01175 basic_FancyOStream<CharT,Traits>::getOStream()
01176 {
01177   return streambuf_.getOStream();
01178 }
01179 
01180 
01181 template<typename CharT, typename Traits>
01182 basic_FancyOStream<CharT,Traits>&
01183 basic_FancyOStream<CharT,Traits>::setTabIndentStr(
01184   const std::basic_string<char_type,traits_type> &tabIndentStr
01185   )
01186 {
01187   streambuf_.setTabIndentStr(tabIndentStr);
01188   return *this;
01189 }
01190 
01191 
01192 template<typename CharT, typename Traits>
01193 const std::basic_string<CharT,Traits>&
01194 basic_FancyOStream<CharT,Traits>::getTabIndentStr() const
01195 {
01196   return streambuf_.getTabIndentStr();
01197 }
01198 
01199 
01200 template<typename CharT, typename Traits>
01201 basic_FancyOStream<CharT,Traits>&
01202 basic_FancyOStream<CharT,Traits>::setShowAllFrontMatter(
01203   const bool showAllFrontMatter
01204   )
01205 {
01206   streambuf_.setShowLinePrefix(showAllFrontMatter);
01207   streambuf_.setShowTabCount(showAllFrontMatter);
01208   streambuf_.setShowProcRank(showAllFrontMatter);
01209   return *this;
01210 }
01211 
01212 
01213 template<typename CharT, typename Traits>
01214 basic_FancyOStream<CharT,Traits>&
01215 basic_FancyOStream<CharT,Traits>::setShowLinePrefix(const bool showLinePrefix)
01216 {
01217   streambuf_.setShowLinePrefix(showLinePrefix);
01218   return *this;
01219 }
01220 
01221 
01222 template<typename CharT, typename Traits>
01223 basic_FancyOStream<CharT,Traits>&
01224 basic_FancyOStream<CharT,Traits>::setMaxLenLinePrefix(const int maxLenLinePrefix)
01225 {
01226   streambuf_.setMaxLenLinePrefix(maxLenLinePrefix);
01227   return *this;
01228 }
01229 
01230 
01231 template<typename CharT, typename Traits>
01232 basic_FancyOStream<CharT,Traits>&
01233 basic_FancyOStream<CharT,Traits>::setShowTabCount(const bool showTabCount)
01234 {
01235   streambuf_.setShowTabCount(showTabCount);
01236   return *this;
01237 }
01238 
01239 
01240 template<typename CharT, typename Traits>
01241 basic_FancyOStream<CharT,Traits>&
01242 basic_FancyOStream<CharT,Traits>::setShowProcRank(const bool showProcRank)
01243 {
01244   streambuf_.setShowProcRank(showProcRank);
01245   return *this;
01246 }
01247 
01248 
01249 template<typename CharT, typename Traits>
01250 basic_FancyOStream<CharT,Traits>&
01251 basic_FancyOStream<CharT,Traits>::setProcRankAndSize( const int procRank, const int numProcs )
01252 {
01253   streambuf_.setProcRankAndSize(procRank,numProcs);
01254   return *this;
01255 }
01256 
01257 
01258 template<typename CharT, typename Traits>
01259 basic_FancyOStream<CharT,Traits>&
01260 basic_FancyOStream<CharT,Traits>::setOutputToRootOnly( const int rootRank )
01261 {
01262   streambuf_.setOutputToRootOnly(rootRank);
01263   return *this;
01264 }
01265 
01266 
01267 template<typename CharT, typename Traits>
01268 int basic_FancyOStream<CharT,Traits>::getOutputToRootOnly() const
01269 {
01270   return streambuf_.getOutputToRootOnly();
01271 }
01272 
01273 
01274 template<typename CharT, typename Traits>
01275 void basic_FancyOStream<CharT,Traits>::copyAllOutputOptions(
01276   const basic_FancyOStream<CharT,Traits> &oStream )
01277 {
01278   //streambuf_.setTabIndentStr(oStream.streambuf_.getTabIndentStr());
01279   streambuf_.setShowLinePrefix(oStream.streambuf_.getShowLinePrefix());
01280   streambuf_.setMaxLenLinePrefix(oStream.streambuf_.getMaxLenLinePrefix());
01281   streambuf_.setShowTabCount(oStream.streambuf_.getShowTabCount());
01282   streambuf_.setShowProcRank(oStream.streambuf_.getShowProcRank());
01283   streambuf_.setProcRankAndSize(oStream.streambuf_.getProcRank(), 
01284     oStream.streambuf_.getNumProcs());
01285   streambuf_.setOutputToRootOnly(oStream.streambuf_.getOutputToRootOnly());
01286 }
01287 
01288 
01289 template<typename CharT, typename Traits>
01290 void basic_FancyOStream<CharT,Traits>::pushTab(const int tabs)
01291 {
01292   streambuf_.pushTab(tabs);
01293 }
01294 
01295 
01296 template<typename CharT, typename Traits>
01297 int basic_FancyOStream<CharT,Traits>::getNumCurrTabs() const
01298 {
01299   return streambuf_.getNumCurrTabs();
01300 }
01301 
01302 
01303 template<typename CharT, typename Traits>
01304 void basic_FancyOStream<CharT,Traits>::popTab()
01305 {
01306   streambuf_.popTab();
01307 }
01308 
01309 
01310 template<typename CharT, typename Traits>
01311 void basic_FancyOStream<CharT,Traits>::pushLinePrefix(
01312   const std::basic_string<char_type,traits_type> &linePrefix
01313   )
01314 {
01315   streambuf_.pushLinePrefix(linePrefix);
01316 }
01317 
01318 
01319 template<typename CharT, typename Traits>
01320 void basic_FancyOStream<CharT,Traits>::popLinePrefix()
01321 {
01322   streambuf_.popLinePrefix();
01323 }
01324 
01325 
01326 template<typename CharT, typename Traits>
01327 const std::basic_string<CharT,Traits>&
01328 basic_FancyOStream<CharT,Traits>::getTopLinePrefix() const
01329 {
01330   return streambuf_.getTopLinePrefix();
01331 }
01332 
01333 
01334 template<typename CharT, typename Traits>
01335 void basic_FancyOStream<CharT,Traits>::pushDisableTabbing()
01336 {
01337   streambuf_.pushDisableTabbing();
01338 }
01339 
01340 
01341 template<typename CharT, typename Traits>
01342 void basic_FancyOStream<CharT,Traits>::popDisableTabbing()
01343 {
01344   return streambuf_.popDisableTabbing();
01345 }
01346 
01347 
01348 } // namespace Teuchos
01349 
01350 
01351 #endif // TEUCHOS_FANCY_O_STREAM_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines