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 
00139   void setProcRankAndSize( const int procRank, const int numProcs );
00140 
00145   int getProcRank() const;
00146 
00151   int getNumProcs() const;
00152 
00167   void setOutputToRootOnly( const int rootRank );
00168 
00170   int getOutputToRootOnly() const;
00171 
00180   void pushTab(const int tabs);
00181 
00183   int getNumCurrTabs() const;
00184 
00192   void popTab();
00193 
00195   void pushLinePrefix(
00196     const std::basic_string<char_type,traits_type> &linePrefix
00197     );
00198 
00200   void popLinePrefix();
00201 
00203   const std::basic_string<char_type,traits_type>& getTopLinePrefix() const;
00204 
00206   void pushDisableTabbing();
00207 
00209   void popDisableTabbing();
00210 
00211 protected:
00212 
00214 
00215 
00217   std::streamsize xsputn(const char_type* s, std::streamsize n);
00218 
00220   int_type overflow(int_type c);
00221 
00222 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
00223 
00224   void imbue(const locale& l)
00225     {
00226       std::cerr << "\ncalled imbue()\n";
00227       std::basic_streambuf<CharT,Traits>::imbue(l);
00228     }
00229 
00230   basic_streambuf<char_type,Traits>*
00231   setbuf(char_type* s, streamsize n)
00232     {
00233       std::cerr << "\ncalled setbuf()\n";
00234       return std::basic_streambuf<CharT,Traits>::setbuf(s,n);
00235     }
00236 
00237   pos_type
00238   seekoff(off_type a, ios_base::seekdir b,ios_base::openmode c)
00239     {
00240       std::cerr << "\ncalled seekoff()\n";
00241       return std::basic_streambuf<CharT,Traits>::seekoff(a,b,c);
00242     }
00243 
00244   pos_type
00245   seekpos(pos_type a, ios_base::openmode b)
00246     {
00247       std::cerr << "\ncalled seekpos()\n";
00248       return std::basic_streambuf<CharT,Traits>::seekpos(a,b);
00249     }
00250 
00251   int
00252   sync()
00253     {
00254       std::cerr << "\ncalled sync()\n";
00255       return std::basic_streambuf<CharT,Traits>::sync();
00256     }
00257 
00258   streamsize
00259   showmanyc()
00260     {
00261       std::cerr << "\ncalled showmanyc()\n";
00262       return std::basic_streambuf<CharT,Traits>::showmanyc();
00263     }
00264 
00265   streamsize
00266   xsgetn(char_type* s, streamsize n)
00267     {
00268       std::cerr << "\ncalled xsgetn()\n";
00269       return std::basic_streambuf<CharT,Traits>::xsgetn(s,n);
00270     }
00271 
00272   int_type
00273   underflow()
00274     {
00275       std::cerr << "\ncalled underflow()\n";
00276       return std::basic_streambuf<CharT,Traits>::underflow();
00277     }
00278 
00279   int_type
00280   uflow()
00281     {
00282       std::cerr << "\ncalled uflow()\n";
00283       return std::basic_streambuf<CharT,Traits>::uflow();
00284     }
00285 
00286   int_type
00287   pbackfail(int_type c = traits_type::eof())
00288     {
00289       std::cerr << "\ncalled pbackfail()\n";
00290       return std::basic_streambuf<CharT,Traits>::pbackfail(c);
00291     }
00292 
00293 #endif // TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
00294 
00296 
00297 private:
00298 
00299   // ////////////////////////
00300   // Private types
00301 
00302   typedef std::basic_string<char_type,traits_type> string_t;
00303   typedef std::deque<int> tabIndentStack_t;
00304   typedef std::deque<string_t> linePrefixStack_t;
00305 
00306   // ////////////////////////
00307   // Private data members
00308 
00309   RCP<std::basic_ostream<char_type,traits_type> > oStreamSet_;
00310   RCP<std::basic_ostream<char_type,traits_type> > oStream_;
00311   std::basic_string<char_type,traits_type> tabIndentStr_;
00312   bool showLinePrefix_;
00313   int maxLenLinePrefix_;
00314   bool showTabCount_;
00315   bool showProcRank_;
00316   int rootRank_;
00317   int procRank_;
00318   int numProcs_;
00319   int rankPrintWidth_;
00320 
00321   RCP<std::ostringstream> lineOut_;
00322 
00323   int tabIndent_;
00324   tabIndentStack_t tabIndentStack_;
00325   linePrefixStack_t linePrefixStack_;
00326   int enableTabbingStack_;
00327 
00328   bool wroteNewline_;
00329 
00330   // ////////////////////////
00331   // Private member functions
00332 
00333   std::ostream& out();
00334 
00335   void writeChars( const char_type s[], std::streamsize n );
00336 
00337   void writeFrontMatter();
00338 
00339   // Not defined and not to be called
00340   basic_FancyOStream_buf();
00341   basic_FancyOStream_buf(const basic_FancyOStream_buf<CharT,Traits>&);
00342   basic_FancyOStream_buf<CharT,Traits> operator=(
00343     const basic_FancyOStream_buf<CharT,Traits>&
00344     );
00345 
00346 };
00347 
00348 
00370 template <typename CharT, typename Traits = std::char_traits<CharT> >
00371 class basic_FancyOStream : public std::basic_ostream<CharT, Traits>
00372 {
00373 public:
00374 
00376 
00377 
00379   typedef CharT char_type;
00381   typedef Traits traits_type;
00383   typedef typename traits_type::int_type int_type;
00385   typedef typename traits_type::pos_type pos_type;
00387   typedef typename traits_type::off_type off_type;
00391   typedef basic_FancyOStream_buf<CharT,Traits> streambuf_t;
00393   typedef std::basic_ostream<char_type, traits_type> ostream_t;
00394 
00396 
00398 
00399 
00429   explicit
00430   basic_FancyOStream(
00431     const RCP< std::basic_ostream<char_type,traits_type> > &oStream
00432     ,const std::basic_string<char_type,traits_type> &tabIndentStr = " "
00433     ,const int startingTab = 0
00434     ,const bool showLinePrefix = false
00435     ,const int maxLenLinePrefix = 10
00436     ,const bool showTabCount = false
00437     ,const bool showProcRank = false
00438     );
00439 
00443   void initialize(
00444     const RCP< std::basic_ostream<char_type,traits_type> > &oStream
00445     ,const std::basic_string<char_type,traits_type> &tabIndentStr = " "
00446     ,const int startingTab = 0
00447     ,const bool showLinePrefix = false
00448     ,const int maxLenLinePrefix = 10
00449     ,const bool showTabCount = false
00450     ,const bool showProcRank = false
00451     );
00452 
00454   RCP<std::basic_ostream<char_type,traits_type> > getOStream();
00455 
00457   basic_FancyOStream& setTabIndentStr(
00458     const std::basic_string<char_type,traits_type> &tabIndentStr
00459     );
00460 
00462   const std::basic_string<char_type,traits_type>& getTabIndentStr() const;
00463 
00478   basic_FancyOStream& setShowAllFrontMatter(const bool showAllFrontMatter);
00479 
00481   basic_FancyOStream& setShowLinePrefix(const bool showLinePrefix);
00482 
00484   basic_FancyOStream& setMaxLenLinePrefix(const int maxLenLinePrefix);
00485 
00487   basic_FancyOStream& setShowTabCount(const bool showTabCount);
00488 
00490   basic_FancyOStream& setShowProcRank(const bool showProcRank);
00491 
00498   basic_FancyOStream& setProcRankAndSize( const int procRank, const int numProcs );
00499 
00514   basic_FancyOStream& setOutputToRootOnly( const int rootRank );
00515 
00517   int getOutputToRootOnly() const;
00518 
00520   void copyAllOutputOptions(const basic_FancyOStream<CharT,Traits> &oStream);
00521 
00523 
00525 
00526 
00535   void pushTab(const int tabs = 1);
00536 
00538   int getNumCurrTabs() const;
00539 
00547   void popTab();
00548 
00550   void pushLinePrefix(const std::basic_string<char_type,traits_type> &linePrefix);
00551 
00553   void popLinePrefix();
00554 
00556   const std::basic_string<char_type,traits_type>& getTopLinePrefix() const;
00557 
00559   void pushDisableTabbing();
00560 
00562   void popDisableTabbing();
00563 
00565 
00566 private:
00567 
00568   streambuf_t   streambuf_;
00569 
00570   // Not defined and not to be called
00571   basic_FancyOStream();
00572   basic_FancyOStream(const basic_FancyOStream<CharT,Traits>&);
00573   basic_FancyOStream<CharT,Traits> operator=(const basic_FancyOStream<CharT,Traits>&);
00574 
00575 };
00576 
00577 
00585 inline
00586 RCP<basic_FancyOStream<char> >
00587 fancyOStream(
00588   const RCP< std::basic_ostream<char> >& oStream,
00589   const std::basic_string<char>& tabIndentStr = " ",
00590   const int startingTab = 0,
00591   const bool showLinePrefix = false,
00592   const int maxLenLinePrefix = 10,
00593   const bool showTabCount = false,
00594   const bool showProcRank = false
00595   )
00596 {
00597   if (nonnull(oStream)) {
00598     return rcp(
00599       new basic_FancyOStream<char>(
00600         oStream,tabIndentStr,startingTab,showLinePrefix,
00601         maxLenLinePrefix,showTabCount,showProcRank
00602         )
00603       );
00604   }
00605   return null;
00606 }
00607 
00608 
00617 inline
00618 RCP<basic_FancyOStream<char> >
00619 getFancyOStream( const RCP<std::basic_ostream<char> > &out )
00620 {
00621   if (is_null(out))
00622     return Teuchos::null;
00623   RCP<basic_FancyOStream<char> >
00624     fancyOut = rcp_dynamic_cast<basic_FancyOStream<char> >(out);
00625   if(nonnull(fancyOut))
00626     return fancyOut;
00627   return rcp(new basic_FancyOStream<char>(out));
00628 }
00629 
00630 
00642 template <typename CharT, typename Traits = std::char_traits<CharT> >
00643 class basic_OSTab
00644 {
00645 public:
00646 
00648   static const int DISABLE_TABBING = -99999; // This magic number should be just fine!
00650   basic_OSTab(
00651     const RCP<basic_FancyOStream<CharT,Traits> > &fancyOStream
00652     ,const int tabs = 1
00653     ,const std::basic_string<CharT,Traits> linePrefix = ""
00654     )
00655     :fancyOStream_(fancyOStream)
00656     ,tabs_(tabs)
00657     ,linePrefix_(linePrefix)
00658     {
00659       updateState();
00660     }
00662   basic_OSTab(
00663     const RCP<std::basic_ostream<CharT,Traits> > &oStream
00664     ,const int tabs = 1
00665     ,const std::basic_string<CharT,Traits> linePrefix = ""
00666     )
00667     :fancyOStream_(getFancyOStream(oStream))
00668     ,tabs_(tabs)
00669     ,linePrefix_(linePrefix)
00670     {
00671       updateState();
00672     }
00674   basic_OSTab(
00675     basic_FancyOStream<CharT,Traits> &fancyOStream
00676     ,const int tabs = 1
00677     ,const std::basic_string<CharT,Traits> linePrefix = ""
00678     )
00679     :fancyOStream_(rcp(&fancyOStream,false))
00680     ,tabs_(tabs)
00681     ,linePrefix_(linePrefix)
00682     {
00683       updateState();
00684     }
00686   basic_OSTab(
00687     std::basic_ostream<CharT,Traits> &oStream
00688     ,const int tabs = 1
00689     ,const std::basic_string<CharT,Traits> linePrefix = ""
00690     )
00691     :fancyOStream_(getFancyOStream(rcp(&oStream, false)))
00692     ,tabs_(tabs)
00693     ,linePrefix_(linePrefix)
00694     {
00695       updateState();
00696     }
00698   basic_OSTab( const basic_OSTab &osTab )
00699     :fancyOStream_(osTab.fancyOStream_)
00700     ,tabs_(osTab.tabs_)
00701     {
00702       updateState();
00703     }
00705   ~basic_OSTab()
00706     {
00707       if(fancyOStream_.get()) {
00708         if(tabs_==DISABLE_TABBING)
00709           fancyOStream_->popDisableTabbing();
00710         else
00711           fancyOStream_->popTab();
00712         if(linePrefix_.length()) fancyOStream_->popLinePrefix();
00713       }
00714     }
00716   basic_OSTab<CharT,Traits>& operator=( const basic_OSTab &osTab )
00717     {
00718       fancyOStream_ = osTab.fancyOStream_;
00719       tabs_ = osTab.tabs_;
00720       updateState();
00721       return *this;
00722     }
00724   basic_OSTab<CharT,Traits>& incrTab(const int tabs = 1)
00725     {
00726       tabs_ += tabs;
00727       if(fancyOStream_.get()) {
00728         fancyOStream_->popTab();
00729         fancyOStream_->pushTab(tabs_);
00730       }
00731       return *this;
00732     }
00734   basic_FancyOStream<CharT,Traits>& o() const
00735     {
00736       return *fancyOStream_;
00737     }
00739   basic_FancyOStream<CharT,Traits>* get() const
00740     {
00741       return fancyOStream_.get();
00742     }
00743 
00744 private:
00745 
00746   RCP<basic_FancyOStream<CharT,Traits> > fancyOStream_;
00747   int tabs_;
00748   std::basic_string<CharT,Traits> linePrefix_;
00749 
00750   void updateState()
00751     {
00752       if(fancyOStream_.get()) {
00753         if(tabs_==DISABLE_TABBING)
00754           fancyOStream_->pushDisableTabbing();
00755         else
00756           fancyOStream_->pushTab(tabs_);
00757         if(linePrefix_.length()) fancyOStream_->pushLinePrefix(linePrefix_);
00758       }
00759     }
00760 
00761 };
00762 
00763 
00779 template <typename CharT, typename Traits>
00780 RCP<basic_FancyOStream<CharT,Traits> >
00781 tab(
00782   const RCP<basic_FancyOStream<CharT,Traits> > &out,
00783   const int tabs = 1,
00784   const std::basic_string<CharT,Traits> linePrefix = ""
00785   )
00786 {
00787   if(out.get()==NULL)
00788     return Teuchos::null;
00789   RCP<basic_FancyOStream<CharT,Traits> > fancyOut = out;
00790   set_extra_data(
00791     rcp(new basic_OSTab<CharT,Traits>(out,tabs,linePrefix)),
00792     "OSTab",
00793     inOutArg(fancyOut),
00794     PRE_DESTROY,
00795     false
00796     );
00797   return fancyOut;
00798 }
00799 
00800 
00816 template <typename CharT, typename Traits>
00817 RCP<basic_FancyOStream<CharT,Traits> >
00818 tab(
00819   const RCP<std::basic_ostream<CharT,Traits> > &out
00820   ,const int tabs = 1
00821   ,const std::basic_string<CharT,Traits> linePrefix = ""
00822   )
00823 {
00824   return tab(getFancyOStream(out),tabs,linePrefix);
00825 }
00826 
00827 
00828 // ///////////////////////////////
00829 // Typedefs
00830 
00831 
00835 typedef basic_FancyOStream<char> FancyOStream;
00836 
00837 
00841 typedef basic_OSTab<char> OSTab;
00842 
00843 
00847 #define TEUCHOS_OSTAB ::Teuchos::OSTab __localThisTab = this->getOSTab()
00848 
00852 #define TEUCHOS_OSTAB_DIFF( DIFF ) ::Teuchos::OSTab DIFF ## __localThisTab = this->getOSTab()
00853 
00854 
00855 // ////////////////////////////////
00856 // Defintions
00857 
00858 
00859 //
00860 // basic_FancyOStream_buf
00861 //
00862 
00863 
00864 template<typename CharT, typename Traits>
00865 basic_FancyOStream_buf<CharT,Traits>::basic_FancyOStream_buf(
00866   const RCP<std::basic_ostream<char_type,traits_type> > &oStream
00867   ,const std::basic_string<char_type,traits_type> &tabIndentStr
00868   ,const int startingTab
00869   ,const bool showLinePrefix
00870   ,const int maxLenLinePrefix
00871   ,const bool showTabCount
00872   ,const bool showProcRank
00873   )
00874 {
00875   this->initialize(oStream,tabIndentStr,startingTab,showLinePrefix,
00876     maxLenLinePrefix,showTabCount,showProcRank);
00877 }
00878 
00879 
00880 template<typename CharT, typename Traits>
00881 void basic_FancyOStream_buf<CharT,Traits>::initialize(
00882   const RCP<std::basic_ostream<char_type,traits_type> > &oStream
00883   ,const std::basic_string<char_type,traits_type> &tabIndentStr
00884   ,const int startingTab
00885   ,const bool showLinePrefix
00886   ,const int maxLenLinePrefix
00887   ,const bool showTabCount
00888   ,const bool showProcRank
00889   )
00890 {
00891   oStreamSet_ = oStream;
00892   oStream_ = oStream;
00893   tabIndentStr_ = tabIndentStr;
00894   showLinePrefix_ = showLinePrefix;
00895   maxLenLinePrefix_ = maxLenLinePrefix;
00896   showTabCount_ = showTabCount;
00897   showProcRank_ = showProcRank;
00898   rootRank_ = -1;
00899   procRank_ = GlobalMPISession::getRank();
00900   numProcs_ = GlobalMPISession::getNProc();
00901   rankPrintWidth_ = int(std::log10(float(numProcs_)))+1;
00902   tabIndent_ = startingTab;
00903   tabIndentStack_.clear();
00904   linePrefixStack_.clear();
00905   wroteNewline_ = true;
00906   enableTabbingStack_ = 0;
00907 }
00908 
00909 
00910 template<typename CharT, typename Traits>
00911 RCP<std::basic_ostream<CharT,Traits> >
00912 basic_FancyOStream_buf<CharT,Traits>::getOStream()
00913 {
00914   return oStreamSet_;
00915 }
00916 
00917 
00918 template<typename CharT, typename Traits>
00919 void basic_FancyOStream_buf<CharT,Traits>::setTabIndentStr(
00920   const std::basic_string<char_type,traits_type> &tabIndentStr
00921   )
00922 {
00923   tabIndentStr_ = tabIndentStr;
00924 }
00925 
00926 
00927 template<typename CharT, typename Traits>
00928 const std::basic_string<CharT,Traits>&
00929 basic_FancyOStream_buf<CharT,Traits>::getTabIndentStr() const
00930 {
00931   return tabIndentStr_;
00932 }
00933 
00934 
00935 template<typename CharT, typename Traits>
00936 void basic_FancyOStream_buf<CharT,Traits>::setShowLinePrefix(const bool showLinePrefix)
00937 {
00938   showLinePrefix_ = showLinePrefix;
00939 }
00940 
00941 
00942 template<typename CharT, typename Traits>
00943 bool basic_FancyOStream_buf<CharT,Traits>::getShowLinePrefix() const
00944 {
00945   return showLinePrefix_;
00946 }
00947 
00948 
00949 template<typename CharT, typename Traits>
00950 void basic_FancyOStream_buf<CharT,Traits>::setMaxLenLinePrefix(const int maxLenLinePrefix)
00951 {
00952   TEUCHOS_TEST_FOR_EXCEPT( !(maxLenLinePrefix>=5) );
00953   maxLenLinePrefix_ = maxLenLinePrefix;
00954 }
00955 
00956 
00957 template<typename CharT, typename Traits>
00958 int basic_FancyOStream_buf<CharT,Traits>::getMaxLenLinePrefix() const
00959 {
00960   return maxLenLinePrefix_;
00961 }
00962 
00963 
00964 template<typename CharT, typename Traits>
00965 void basic_FancyOStream_buf<CharT,Traits>::setShowTabCount(const bool showTabCount)
00966 {
00967   showTabCount_ = showTabCount;
00968 }
00969 
00970 
00971 template<typename CharT, typename Traits>
00972 bool basic_FancyOStream_buf<CharT,Traits>::getShowTabCount() const
00973 {
00974   return showTabCount_;
00975 }
00976 
00977 
00978 template<typename CharT, typename Traits>
00979 void basic_FancyOStream_buf<CharT,Traits>::setShowProcRank(const bool showProcRank)
00980 {
00981   showProcRank_ = showProcRank;
00982 }
00983 
00984 
00985 template<typename CharT, typename Traits>
00986 bool basic_FancyOStream_buf<CharT,Traits>::getShowProcRank() const
00987 {
00988   return showProcRank_;
00989 }
00990 
00991 
00992 template<typename CharT, typename Traits>
00993 void basic_FancyOStream_buf<CharT,Traits>::setProcRankAndSize(
00994   const int procRank, const int numProcs
00995   )
00996 {
00997   procRank_ = procRank;
00998   numProcs_ = numProcs;
00999 }
01000 
01001 
01002 template<typename CharT, typename Traits>
01003 int basic_FancyOStream_buf<CharT,Traits>::getProcRank() const
01004 {
01005   return procRank_;
01006 }
01007 
01008 
01009 template<typename CharT, typename Traits>
01010 int basic_FancyOStream_buf<CharT,Traits>::getNumProcs() const
01011 {
01012   return numProcs_;
01013 }
01014 
01015 
01016 template<typename CharT, typename Traits>
01017 void basic_FancyOStream_buf<CharT,Traits>::setOutputToRootOnly(
01018   const int rootRank
01019   )
01020 {
01021   rootRank_ = rootRank;
01022   if(rootRank >= 0) {
01023     if(rootRank == procRank_)
01024       oStream_ = oStreamSet_;
01025     else
01026       oStream_ = rcp(new oblackholestream());
01027     // Only processor is being output to so there is no need for line
01028     // batching!
01029     lineOut_ = null;
01030   }
01031   else {
01032     oStream_ = oStreamSet_;
01033     // Output is being sent to all processors so we need line batching to
01034     // insure that each line will be printed all together!
01035     lineOut_ = rcp(new std::ostringstream());
01036   }
01037 }
01038 
01039 
01040 template<typename CharT, typename Traits>
01041 int basic_FancyOStream_buf<CharT,Traits>::getOutputToRootOnly() const
01042 {
01043   return rootRank_;
01044 }
01045 
01046 
01047 template<typename CharT, typename Traits>
01048 void basic_FancyOStream_buf<CharT,Traits>::pushTab(const int tabs)
01049 {
01050   if( tabIndent_ + tabs < 0 ) {
01051     tabIndentStack_.push_back(-tabIndent_);
01052     tabIndent_ = 0;
01053   }
01054   else {
01055     tabIndentStack_.push_back(tabs);
01056     tabIndent_ += tabs;
01057   }
01058 }
01059 
01060 
01061 template<typename CharT, typename Traits>
01062 int basic_FancyOStream_buf<CharT,Traits>::getNumCurrTabs() const
01063 {
01064   return tabIndent_;
01065 }
01066 
01067 
01068 template<typename CharT, typename Traits>
01069 void basic_FancyOStream_buf<CharT,Traits>::popTab()
01070 {
01071   tabIndent_ -= tabIndentStack_.back();
01072   tabIndentStack_.pop_back();
01073 }
01074 
01075 
01076 template<typename CharT, typename Traits>
01077 void basic_FancyOStream_buf<CharT,Traits>::pushLinePrefix(
01078   const std::basic_string<char_type,traits_type> &linePrefix
01079   )
01080 {
01081   linePrefixStack_.push_back(linePrefix);
01082 }
01083 
01084 
01085 template<typename CharT, typename Traits>
01086 void basic_FancyOStream_buf<CharT,Traits>::popLinePrefix()
01087 {
01088   linePrefixStack_.pop_back();
01089 }
01090 
01091 
01092 template<typename CharT, typename Traits>
01093 const std::basic_string<CharT,Traits>&
01094 basic_FancyOStream_buf<CharT,Traits>::getTopLinePrefix() const
01095 {
01096   return linePrefixStack_.back();
01097 }
01098 
01099 
01100 template<typename CharT, typename Traits>
01101 void basic_FancyOStream_buf<CharT,Traits>::pushDisableTabbing()
01102 {
01103   ++enableTabbingStack_;
01104 }
01105 
01106 
01107 template<typename CharT, typename Traits>
01108 void basic_FancyOStream_buf<CharT,Traits>::popDisableTabbing()
01109 {
01110   --enableTabbingStack_;
01111 }
01112 
01113 
01114 // protected
01115 
01116 
01117 template<typename CharT, typename Traits>
01118 std::streamsize basic_FancyOStream_buf<CharT,Traits>::xsputn(
01119   const char_type* s, std::streamsize n
01120   )
01121 {
01122 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
01123   std::cerr << "\ncalled xsputn()\n";
01124 #endif
01125   writeChars(s,n);
01126   return n;
01127 }
01128 
01129 
01130 template<typename CharT, typename Traits>
01131 typename basic_FancyOStream_buf<CharT,Traits>::int_type
01132 basic_FancyOStream_buf<CharT,Traits>::overflow(int_type c)
01133 {
01134 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
01135   std::cerr << "\ncalled overflow()\n";
01136 #endif
01137   if(c != traits_type::eof()) {
01138     const char_type cc[] = { traits_type::to_char_type(c) };
01139     this->writeChars(cc,1);
01140   }
01141   return traits_type::not_eof(c);
01142   //return std::basic_streambuf<CharT,Traits>::overflow(c);
01143 }
01144 
01145 
01146 // private
01147 
01148 
01149 template<typename CharT, typename Traits>
01150 std::ostream& basic_FancyOStream_buf<CharT,Traits>::out()
01151 {
01152   if(lineOut_.get())
01153     return *lineOut_;
01154   return *oStream_;
01155 }
01156 
01157 
01158 template<typename CharT, typename Traits>
01159 void basic_FancyOStream_buf<CharT,Traits>::writeChars(
01160   const char_type s[], std::streamsize n
01161   )
01162 {
01163   if(n == 0) return;
01164   std::streamsize p = 0, first_p = 0;
01165   bool done_outputting = false;
01166   const char_type newline = '\n';
01167   while( !done_outputting ) {
01168     // Find the next newline
01169     for( p = first_p; p < n; ++p ) {
01170       if(s[p] == newline) {
01171         break;
01172       }
01173     }
01174     if(p == n) {
01175       // We did not find a newline at the end!
01176       --p;
01177       done_outputting = true;
01178     }
01179     else if( p == n-1 && s[p] == newline ) {
01180       // The last character in the std::string is a newline
01181       done_outputting = true;
01182     }
01183     // Write the beginning of the line if we need to
01184     if(wroteNewline_) {
01185       writeFrontMatter();
01186       wroteNewline_ = false;
01187     }
01188     // Write up to the newline or the end of the std::string
01189     out().write(s+first_p,p-first_p+1);
01190     if(s[p] == newline) {
01191       wroteNewline_ = true;
01192       if(lineOut_.get()) {
01193         *oStream_ << lineOut_->str() << std::flush;
01194         lineOut_->str("");
01195       }
01196     }
01197     // Update for next search
01198     if(!done_outputting)
01199       first_p = p+1;
01200   }
01201 }
01202 
01203 
01204 template<typename CharT, typename Traits>
01205 void basic_FancyOStream_buf<CharT,Traits>::writeFrontMatter()
01206 {
01207   bool didOutput = false;
01208   std::ostream &o = this->out();
01209   if(showProcRank_) {
01210     o << "p=" << std::right << std::setw(rankPrintWidth_) << procRank_;
01211     didOutput = true;
01212   }
01213   if(showLinePrefix_) {
01214     if(didOutput)
01215       o << ", ";
01216     std::string currLinePrefix = "";
01217     if ( linePrefixStack_.size() )
01218       currLinePrefix = this->getTopLinePrefix();
01219     const int localMaxLenLinePrefix =
01220       TEUCHOS_MAX( as<int>(currLinePrefix.length()), maxLenLinePrefix_ );
01221     o << std::left << std::setw(localMaxLenLinePrefix);
01222     o << currLinePrefix;
01223     didOutput = true;
01224   }
01225   if(showTabCount_) {
01226     if(didOutput)
01227       o << ", ";
01228     o << "tabs=" << std::right << std::setw(2) << tabIndent_;
01229     didOutput = true;
01230   }
01231   // ToDo: Add the Prefix name if asked
01232   // ToDo: Add the processor number if asked
01233   // ToDo: Add the number of indents if asked
01234   if(didOutput) {
01235     o << " |" << tabIndentStr_;
01236   }
01237   if(enableTabbingStack_==0) {
01238     for( int i = 0; i < tabIndent_; ++i )
01239       o << tabIndentStr_;
01240   }
01241 }
01242 
01243 
01244 //
01245 // basic_FancyOStream
01246 //
01247 
01248 
01249 template<typename CharT, typename Traits>
01250 basic_FancyOStream<CharT,Traits>::basic_FancyOStream(
01251   const RCP< std::basic_ostream<char_type,traits_type> > &oStream
01252   ,const std::basic_string<char_type,traits_type> &tabIndentStr
01253   ,const int startingTab
01254   ,const bool showLinePrefix
01255   ,const int maxLenLinePrefix
01256   ,const bool showTabCount
01257   ,const bool showProcRank
01258   )
01259   :ostream_t(NULL),
01260    streambuf_(oStream,tabIndentStr,startingTab,showLinePrefix,
01261      maxLenLinePrefix,showTabCount,showProcRank)
01262 {
01263   this->init(&streambuf_);
01264 }
01265 
01266 
01267 template<typename CharT, typename Traits>
01268 void basic_FancyOStream<CharT,Traits>::initialize(
01269   const RCP< std::basic_ostream<char_type,traits_type> > &oStream
01270   ,const std::basic_string<char_type,traits_type> &tabIndentStr
01271   ,const int startingTab
01272   ,const bool showLinePrefix
01273   ,const int maxLenLinePrefix
01274   ,const bool showTabCount
01275   ,const bool showProcRank
01276   )
01277 {
01278   streambuf_.initialize(oStream,tabIndentStr,startingTab,
01279     showLinePrefix,maxLenLinePrefix,showTabCount,showProcRank);
01280   this->init(&streambuf_);
01281 }
01282 
01283 
01284 template<typename CharT, typename Traits>
01285 RCP<std::basic_ostream<CharT,Traits> >
01286 basic_FancyOStream<CharT,Traits>::getOStream()
01287 {
01288   return streambuf_.getOStream();
01289 }
01290 
01291 
01292 template<typename CharT, typename Traits>
01293 basic_FancyOStream<CharT,Traits>&
01294 basic_FancyOStream<CharT,Traits>::setTabIndentStr(
01295   const std::basic_string<char_type,traits_type> &tabIndentStr
01296   )
01297 {
01298   streambuf_.setTabIndentStr(tabIndentStr);
01299   return *this;
01300 }
01301 
01302 
01303 template<typename CharT, typename Traits>
01304 const std::basic_string<CharT,Traits>&
01305 basic_FancyOStream<CharT,Traits>::getTabIndentStr() const
01306 {
01307   return streambuf_.getTabIndentStr();
01308 }
01309 
01310 
01311 template<typename CharT, typename Traits>
01312 basic_FancyOStream<CharT,Traits>&
01313 basic_FancyOStream<CharT,Traits>::setShowAllFrontMatter(
01314   const bool showAllFrontMatter
01315   )
01316 {
01317   streambuf_.setShowLinePrefix(showAllFrontMatter);
01318   streambuf_.setShowTabCount(showAllFrontMatter);
01319   streambuf_.setShowProcRank(showAllFrontMatter);
01320   return *this;
01321 }
01322 
01323 
01324 template<typename CharT, typename Traits>
01325 basic_FancyOStream<CharT,Traits>&
01326 basic_FancyOStream<CharT,Traits>::setShowLinePrefix(const bool showLinePrefix)
01327 {
01328   streambuf_.setShowLinePrefix(showLinePrefix);
01329   return *this;
01330 }
01331 
01332 
01333 template<typename CharT, typename Traits>
01334 basic_FancyOStream<CharT,Traits>&
01335 basic_FancyOStream<CharT,Traits>::setMaxLenLinePrefix(const int maxLenLinePrefix)
01336 {
01337   streambuf_.setMaxLenLinePrefix(maxLenLinePrefix);
01338   return *this;
01339 }
01340 
01341 
01342 template<typename CharT, typename Traits>
01343 basic_FancyOStream<CharT,Traits>&
01344 basic_FancyOStream<CharT,Traits>::setShowTabCount(const bool showTabCount)
01345 {
01346   streambuf_.setShowTabCount(showTabCount);
01347   return *this;
01348 }
01349 
01350 
01351 template<typename CharT, typename Traits>
01352 basic_FancyOStream<CharT,Traits>&
01353 basic_FancyOStream<CharT,Traits>::setShowProcRank(const bool showProcRank)
01354 {
01355   streambuf_.setShowProcRank(showProcRank);
01356   return *this;
01357 }
01358 
01359 
01360 template<typename CharT, typename Traits>
01361 basic_FancyOStream<CharT,Traits>&
01362 basic_FancyOStream<CharT,Traits>::setProcRankAndSize( const int procRank, const int numProcs )
01363 {
01364   streambuf_.setProcRankAndSize(procRank,numProcs);
01365   return *this;
01366 }
01367 
01368 
01369 template<typename CharT, typename Traits>
01370 basic_FancyOStream<CharT,Traits>&
01371 basic_FancyOStream<CharT,Traits>::setOutputToRootOnly( const int rootRank )
01372 {
01373   streambuf_.setOutputToRootOnly(rootRank);
01374   return *this;
01375 }
01376 
01377 
01378 template<typename CharT, typename Traits>
01379 int basic_FancyOStream<CharT,Traits>::getOutputToRootOnly() const
01380 {
01381   return streambuf_.getOutputToRootOnly();
01382 }
01383 
01384 
01385 template<typename CharT, typename Traits>
01386 void basic_FancyOStream<CharT,Traits>::copyAllOutputOptions(
01387   const basic_FancyOStream<CharT,Traits> &oStream )
01388 {
01389   //streambuf_.setTabIndentStr(oStream.streambuf_.getTabIndentStr());
01390   streambuf_.setShowLinePrefix(oStream.streambuf_.getShowLinePrefix());
01391   streambuf_.setMaxLenLinePrefix(oStream.streambuf_.getMaxLenLinePrefix());
01392   streambuf_.setShowTabCount(oStream.streambuf_.getShowTabCount());
01393   streambuf_.setShowProcRank(oStream.streambuf_.getShowProcRank());
01394   streambuf_.setProcRankAndSize(oStream.streambuf_.getProcRank(),
01395     oStream.streambuf_.getNumProcs());
01396   streambuf_.setOutputToRootOnly(oStream.streambuf_.getOutputToRootOnly());
01397 }
01398 
01399 
01400 template<typename CharT, typename Traits>
01401 void basic_FancyOStream<CharT,Traits>::pushTab(const int tabs)
01402 {
01403   streambuf_.pushTab(tabs);
01404 }
01405 
01406 
01407 template<typename CharT, typename Traits>
01408 int basic_FancyOStream<CharT,Traits>::getNumCurrTabs() const
01409 {
01410   return streambuf_.getNumCurrTabs();
01411 }
01412 
01413 
01414 template<typename CharT, typename Traits>
01415 void basic_FancyOStream<CharT,Traits>::popTab()
01416 {
01417   streambuf_.popTab();
01418 }
01419 
01420 
01421 template<typename CharT, typename Traits>
01422 void basic_FancyOStream<CharT,Traits>::pushLinePrefix(
01423   const std::basic_string<char_type,traits_type> &linePrefix
01424   )
01425 {
01426   streambuf_.pushLinePrefix(linePrefix);
01427 }
01428 
01429 
01430 template<typename CharT, typename Traits>
01431 void basic_FancyOStream<CharT,Traits>::popLinePrefix()
01432 {
01433   streambuf_.popLinePrefix();
01434 }
01435 
01436 
01437 template<typename CharT, typename Traits>
01438 const std::basic_string<CharT,Traits>&
01439 basic_FancyOStream<CharT,Traits>::getTopLinePrefix() const
01440 {
01441   return streambuf_.getTopLinePrefix();
01442 }
01443 
01444 
01445 template<typename CharT, typename Traits>
01446 void basic_FancyOStream<CharT,Traits>::pushDisableTabbing()
01447 {
01448   streambuf_.pushDisableTabbing();
01449 }
01450 
01451 
01452 template<typename CharT, typename Traits>
01453 void basic_FancyOStream<CharT,Traits>::popDisableTabbing()
01454 {
01455   return streambuf_.popDisableTabbing();
01456 }
01457 
01458 
01459 } // namespace Teuchos
01460 
01461 
01462 #endif // TEUCHOS_FANCY_O_STREAM_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines