|
Teuchos Package Browser (Single Doxygen Collection) Version of the Day
|
00001 // @HEADER 00002 // *********************************************************************** 00003 // 00004 // Teuchos: Common Tools Package 00005 // Copyright (2004) Sandia Corporation 00006 // 00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive 00008 // license for use of this work by or on behalf of the U.S. Government. 00009 // 00010 // This library is free software; you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as 00012 // published by the Free Software Foundation; either version 2.1 of the 00013 // License, or (at your option) any later version. 00014 // 00015 // This library is distributed in the hope that it will be useful, but 00016 // WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this library; if not, write to the Free Software 00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00023 // USA 00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00025 // 00026 // *********************************************************************** 00027 // @HEADER 00028 00029 #ifndef TEUCHOS_FANCY_O_STREAM_HPP 00030 #define TEUCHOS_FANCY_O_STREAM_HPP 00031 00032 #include "Teuchos_RCP.hpp" 00033 #include "Teuchos_GlobalMPISession.hpp" 00034 #include "Teuchos_oblackholestream.hpp" 00035 #include "Teuchos_as.hpp" 00036 00037 00038 namespace Teuchos { 00039 00040 00049 template<typename CharT, typename Traits> 00050 class basic_FancyOStream_buf : public std::basic_streambuf<CharT,Traits> 00051 { 00052 public: 00053 00055 typedef CharT char_type; 00057 typedef Traits traits_type; 00059 typedef typename traits_type::int_type int_type; 00061 typedef typename traits_type::pos_type pos_type; 00063 typedef typename traits_type::off_type off_type; 00064 00066 basic_FancyOStream_buf( 00067 const RCP<std::basic_ostream<char_type,traits_type> > &oStream 00068 ,const std::basic_string<char_type,traits_type> &tabIndentStr 00069 ,const int startingTab 00070 ,const bool showLinePrefix 00071 ,const int maxLenLinePrefix 00072 ,const bool showTabCount 00073 ,const bool showProcRank 00074 ); 00075 00077 void initialize( 00078 const RCP<std::basic_ostream<char_type,traits_type> > &oStream 00079 ,const std::basic_string<char_type,traits_type> &tabIndentStr 00080 ,const int startingTab 00081 ,const bool showLinePrefix 00082 ,const int maxLenLinePrefix 00083 ,const bool showTabCount 00084 ,const bool showProcRank 00085 ); 00086 00088 RCP<std::basic_ostream<char_type,traits_type> > getOStream(); 00089 00091 void setTabIndentStr(const std::basic_string<char_type,traits_type> &tabIndentStr); 00092 00094 const std::basic_string<char_type,traits_type>& getTabIndentStr() const; 00095 00097 void setShowLinePrefix(const bool showLinePrefix); 00098 00100 bool getShowLinePrefix() const; 00101 00103 void setMaxLenLinePrefix(const int maxLenLinePrefix); 00104 00106 int getMaxLenLinePrefix() const; 00107 00109 void setShowTabCount(const bool showTabCount); 00110 00112 bool getShowTabCount() const; 00113 00115 void setShowProcRank(const bool showProcRank); 00116 00118 bool getShowProcRank() const; 00119 00121 void setProcRankAndSize( const int procRank, const int numProcs ); 00122 00124 int getProcRank() const; 00125 00127 int getNumProcs() const; 00128 00130 void setOutputToRootOnly( const int rootRank ); 00131 00133 int getOutputToRootOnly() const; 00134 00136 void pushTab(const int tabs); 00137 00139 int getNumCurrTabs() const; 00140 00142 void popTab(); 00143 00145 void pushLinePrefix( 00146 const std::basic_string<char_type,traits_type> &linePrefix 00147 ); 00148 00150 void popLinePrefix(); 00151 00153 const std::basic_string<char_type,traits_type>& getTopLinePrefix() const; 00154 00156 void pushDisableTabbing(); 00157 00159 void popDisableTabbing(); 00160 00161 protected: 00162 00164 00165 00167 std::streamsize xsputn(const char_type* s, std::streamsize n); 00168 00170 int_type overflow(int_type c); 00171 00172 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS 00173 00174 void imbue(const locale& l) 00175 { 00176 std::cerr << "\ncalled imbue()\n"; 00177 std::basic_streambuf<CharT,Traits>::imbue(l); 00178 } 00179 00180 basic_streambuf<char_type,Traits>* 00181 setbuf(char_type* s, streamsize n) 00182 { 00183 std::cerr << "\ncalled setbuf()\n"; 00184 return std::basic_streambuf<CharT,Traits>::setbuf(s,n); 00185 } 00186 00187 pos_type 00188 seekoff(off_type a, ios_base::seekdir b,ios_base::openmode c) 00189 { 00190 std::cerr << "\ncalled seekoff()\n"; 00191 return std::basic_streambuf<CharT,Traits>::seekoff(a,b,c); 00192 } 00193 00194 pos_type 00195 seekpos(pos_type a, ios_base::openmode b) 00196 { 00197 std::cerr << "\ncalled seekpos()\n"; 00198 return std::basic_streambuf<CharT,Traits>::seekpos(a,b); 00199 } 00200 00201 int 00202 sync() 00203 { 00204 std::cerr << "\ncalled sync()\n"; 00205 return std::basic_streambuf<CharT,Traits>::sync(); 00206 } 00207 00208 streamsize 00209 showmanyc() 00210 { 00211 std::cerr << "\ncalled showmanyc()\n"; 00212 return std::basic_streambuf<CharT,Traits>::showmanyc(); 00213 } 00214 00215 streamsize 00216 xsgetn(char_type* s, streamsize n) 00217 { 00218 std::cerr << "\ncalled xsgetn()\n"; 00219 return std::basic_streambuf<CharT,Traits>::xsgetn(s,n); 00220 } 00221 00222 int_type 00223 underflow() 00224 { 00225 std::cerr << "\ncalled underflow()\n"; 00226 return std::basic_streambuf<CharT,Traits>::underflow(); 00227 } 00228 00229 int_type 00230 uflow() 00231 { 00232 std::cerr << "\ncalled uflow()\n"; 00233 return std::basic_streambuf<CharT,Traits>::uflow(); 00234 } 00235 00236 int_type 00237 pbackfail(int_type c = traits_type::eof()) 00238 { 00239 std::cerr << "\ncalled pbackfail()\n"; 00240 return std::basic_streambuf<CharT,Traits>::pbackfail(c); 00241 } 00242 00243 #endif // TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS 00244 00246 00247 private: 00248 00249 // //////////////////////// 00250 // Private types 00251 00252 typedef std::basic_string<char_type,traits_type> string_t; 00253 typedef std::deque<int> tabIndentStack_t; 00254 typedef std::deque<string_t> linePrefixStack_t; 00255 00256 // //////////////////////// 00257 // Private data members 00258 00259 RCP<std::basic_ostream<char_type,traits_type> > oStreamSet_; 00260 RCP<std::basic_ostream<char_type,traits_type> > oStream_; 00261 std::basic_string<char_type,traits_type> tabIndentStr_; 00262 bool showLinePrefix_; 00263 int maxLenLinePrefix_; 00264 bool showTabCount_; 00265 bool showProcRank_; 00266 int rootRank_; 00267 int procRank_; 00268 int numProcs_; 00269 int rankPrintWidth_; 00270 00271 RCP<std::ostringstream> lineOut_; 00272 00273 int tabIndent_; 00274 tabIndentStack_t tabIndentStack_; 00275 linePrefixStack_t linePrefixStack_; 00276 int enableTabbingStack_; 00277 00278 bool wroteNewline_; 00279 00280 // //////////////////////// 00281 // Private member functions 00282 00283 std::ostream& out(); 00284 00285 void writeChars( const char_type s[], std::streamsize n ); 00286 00287 void writeFrontMatter(); 00288 00289 // Not defined and not to be called 00290 basic_FancyOStream_buf(); 00291 basic_FancyOStream_buf(const basic_FancyOStream_buf<CharT,Traits>&); 00292 basic_FancyOStream_buf<CharT,Traits> operator=( 00293 const basic_FancyOStream_buf<CharT,Traits>& 00294 ); 00295 00296 }; 00297 00298 00320 template <typename CharT, typename Traits = std::char_traits<CharT> > 00321 class basic_FancyOStream : public std::basic_ostream<CharT, Traits> 00322 { 00323 public: 00324 00326 00327 00329 typedef CharT char_type; 00331 typedef Traits traits_type; 00333 typedef typename traits_type::int_type int_type; 00335 typedef typename traits_type::pos_type pos_type; 00337 typedef typename traits_type::off_type off_type; 00341 typedef basic_FancyOStream_buf<CharT,Traits> streambuf_t; 00343 typedef std::basic_ostream<char_type, traits_type> ostream_t; 00344 00346 00348 00349 00351 explicit 00352 basic_FancyOStream( 00353 const RCP< std::basic_ostream<char_type,traits_type> > &oStream 00354 ,const std::basic_string<char_type,traits_type> &tabIndentStr = " " 00355 ,const int startingTab = 0 00356 ,const bool showLinePrefix = false 00357 ,const int maxLenLinePrefix = 10 00358 ,const bool showTabCount = false 00359 ,const bool showProcRank = false 00360 ); 00361 00363 void initialize( 00364 const RCP< std::basic_ostream<char_type,traits_type> > &oStream 00365 ,const std::basic_string<char_type,traits_type> &tabIndentStr = " " 00366 ,const int startingTab = 0 00367 ,const bool showLinePrefix = false 00368 ,const int maxLenLinePrefix = 10 00369 ,const bool showTabCount = false 00370 ,const bool showProcRank = false 00371 ); 00372 00374 RCP<std::basic_ostream<char_type,traits_type> > getOStream(); 00375 00377 basic_FancyOStream& setTabIndentStr( 00378 const std::basic_string<char_type,traits_type> &tabIndentStr 00379 ); 00380 00382 const std::basic_string<char_type,traits_type>& getTabIndentStr() const; 00383 00385 basic_FancyOStream& setShowAllFrontMatter(const bool showAllFrontMatter); 00386 00388 basic_FancyOStream& setShowLinePrefix(const bool showLinePrefix); 00389 00391 basic_FancyOStream& setMaxLenLinePrefix(const int maxLenLinePrefix); 00392 00394 basic_FancyOStream& setShowTabCount(const bool showTabCount); 00395 00397 basic_FancyOStream& setShowProcRank(const bool showProcRank); 00398 00400 basic_FancyOStream& setProcRankAndSize( const int procRank, const int numProcs ); 00401 00403 basic_FancyOStream& setOutputToRootOnly( const int rootRank ); 00404 00406 int getOutputToRootOnly() const; 00407 00409 void copyAllOutputOptions(const basic_FancyOStream<CharT,Traits> &oStream); 00410 00412 00414 00415 00417 void pushTab(const int tabs = 1); 00418 00420 int getNumCurrTabs() const; 00421 00423 void popTab(); 00424 00426 void pushLinePrefix(const std::basic_string<char_type,traits_type> &linePrefix); 00427 00429 void popLinePrefix(); 00430 00432 const std::basic_string<char_type,traits_type>& getTopLinePrefix() const; 00433 00435 void pushDisableTabbing(); 00436 00438 void popDisableTabbing(); 00439 00441 00442 private: 00443 00444 streambuf_t streambuf_; 00445 00446 // Not defined and not to be called 00447 basic_FancyOStream(); 00448 basic_FancyOStream(const basic_FancyOStream<CharT,Traits>&); 00449 basic_FancyOStream<CharT,Traits> operator=(const basic_FancyOStream<CharT,Traits>&); 00450 00451 }; 00452 00453 00461 inline 00462 RCP<basic_FancyOStream<char> > 00463 fancyOStream( 00464 const RCP< std::basic_ostream<char> >& oStream, 00465 const std::basic_string<char>& tabIndentStr = " ", 00466 const int startingTab = 0, 00467 const bool showLinePrefix = false, 00468 const int maxLenLinePrefix = 10, 00469 const bool showTabCount = false, 00470 const bool showProcRank = false 00471 ) 00472 { 00473 if (nonnull(oStream)) { 00474 return rcp( 00475 new basic_FancyOStream<char>( 00476 oStream,tabIndentStr,startingTab,showLinePrefix, 00477 maxLenLinePrefix,showTabCount,showProcRank 00478 ) 00479 ); 00480 } 00481 return null; 00482 } 00483 00484 00493 inline 00494 RCP<basic_FancyOStream<char> > 00495 getFancyOStream( const RCP<std::basic_ostream<char> > &out ) 00496 { 00497 if (is_null(out)) 00498 return Teuchos::null; 00499 RCP<basic_FancyOStream<char> > 00500 fancyOut = rcp_dynamic_cast<basic_FancyOStream<char> >(out); 00501 if(nonnull(fancyOut)) 00502 return fancyOut; 00503 return rcp(new basic_FancyOStream<char>(out)); 00504 } 00505 00506 00518 template <typename CharT, typename Traits = std::char_traits<CharT> > 00519 class basic_OSTab 00520 { 00521 public: 00522 00524 static const int DISABLE_TABBING = -99999; // This magic number should be just fine! 00526 basic_OSTab( 00527 const RCP<basic_FancyOStream<CharT,Traits> > &fancyOStream 00528 ,const int tabs = 1 00529 ,const std::basic_string<CharT,Traits> linePrefix = "" 00530 ) 00531 :fancyOStream_(fancyOStream) 00532 ,tabs_(tabs) 00533 ,linePrefix_(linePrefix) 00534 { 00535 updateState(); 00536 } 00538 basic_OSTab( 00539 const RCP<std::basic_ostream<CharT,Traits> > &oStream 00540 ,const int tabs = 1 00541 ,const std::basic_string<CharT,Traits> linePrefix = "" 00542 ) 00543 :fancyOStream_(getFancyOStream(oStream)) 00544 ,tabs_(tabs) 00545 ,linePrefix_(linePrefix) 00546 { 00547 updateState(); 00548 } 00550 basic_OSTab( 00551 basic_FancyOStream<CharT,Traits> &fancyOStream 00552 ,const int tabs = 1 00553 ,const std::basic_string<CharT,Traits> linePrefix = "" 00554 ) 00555 :fancyOStream_(rcp(&fancyOStream,false)) 00556 ,tabs_(tabs) 00557 ,linePrefix_(linePrefix) 00558 { 00559 updateState(); 00560 } 00562 basic_OSTab( 00563 std::basic_ostream<CharT,Traits> &oStream 00564 ,const int tabs = 1 00565 ,const std::basic_string<CharT,Traits> linePrefix = "" 00566 ) 00567 :fancyOStream_(getFancyOStream(rcp(&oStream, false))) 00568 ,tabs_(tabs) 00569 ,linePrefix_(linePrefix) 00570 { 00571 updateState(); 00572 } 00574 basic_OSTab( const basic_OSTab &osTab ) 00575 :fancyOStream_(osTab.fancyOStream_) 00576 ,tabs_(osTab.tabs_) 00577 { 00578 updateState(); 00579 } 00581 ~basic_OSTab() 00582 { 00583 if(fancyOStream_.get()) { 00584 if(tabs_==DISABLE_TABBING) 00585 fancyOStream_->popDisableTabbing(); 00586 else 00587 fancyOStream_->popTab(); 00588 if(linePrefix_.length()) fancyOStream_->popLinePrefix(); 00589 } 00590 } 00592 basic_OSTab<CharT,Traits>& operator=( const basic_OSTab &osTab ) 00593 { 00594 fancyOStream_ = osTab.fancyOStream_; 00595 tabs_ = osTab.tabs_; 00596 updateState(); 00597 return *this; 00598 } 00600 basic_OSTab<CharT,Traits>& incrTab(const int tabs = 1) 00601 { 00602 tabs_ += tabs; 00603 if(fancyOStream_.get()) { 00604 fancyOStream_->popTab(); 00605 fancyOStream_->pushTab(tabs_); 00606 } 00607 return *this; 00608 } 00610 basic_FancyOStream<CharT,Traits>& o() const 00611 { 00612 return *fancyOStream_; 00613 } 00615 basic_FancyOStream<CharT,Traits>* get() const 00616 { 00617 return fancyOStream_.get(); 00618 } 00619 00620 private: 00621 00622 RCP<basic_FancyOStream<CharT,Traits> > fancyOStream_; 00623 int tabs_; 00624 std::basic_string<CharT,Traits> linePrefix_; 00625 00626 void updateState() 00627 { 00628 if(fancyOStream_.get()) { 00629 if(tabs_==DISABLE_TABBING) 00630 fancyOStream_->pushDisableTabbing(); 00631 else 00632 fancyOStream_->pushTab(tabs_); 00633 if(linePrefix_.length()) fancyOStream_->pushLinePrefix(linePrefix_); 00634 } 00635 } 00636 00637 }; 00638 00639 00655 template <typename CharT, typename Traits> 00656 RCP<basic_FancyOStream<CharT,Traits> > 00657 tab( 00658 const RCP<basic_FancyOStream<CharT,Traits> > &out, 00659 const int tabs = 1, 00660 const std::basic_string<CharT,Traits> linePrefix = "" 00661 ) 00662 { 00663 if(out.get()==NULL) 00664 return Teuchos::null; 00665 RCP<basic_FancyOStream<CharT,Traits> > fancyOut = out; 00666 set_extra_data( 00667 rcp(new basic_OSTab<CharT,Traits>(out,tabs,linePrefix)), 00668 "OSTab", 00669 inOutArg(fancyOut), 00670 PRE_DESTROY, 00671 false 00672 ); 00673 return fancyOut; 00674 } 00675 00676 00692 template <typename CharT, typename Traits> 00693 RCP<basic_FancyOStream<CharT,Traits> > 00694 tab( 00695 const RCP<std::basic_ostream<CharT,Traits> > &out 00696 ,const int tabs = 1 00697 ,const std::basic_string<CharT,Traits> linePrefix = "" 00698 ) 00699 { 00700 return tab(getFancyOStream(out),tabs,linePrefix); 00701 } 00702 00703 00704 // /////////////////////////////// 00705 // Typedefs 00706 00707 00711 typedef basic_FancyOStream<char> FancyOStream; 00712 00713 00717 typedef basic_OSTab<char> OSTab; 00718 00719 00723 #define TEUCHOS_OSTAB ::Teuchos::OSTab __localThisTab = this->getOSTab() 00724 00728 #define TEUCHOS_OSTAB_DIFF( DIFF ) ::Teuchos::OSTab DIFF ## __localThisTab = this->getOSTab() 00729 00730 00731 // //////////////////////////////// 00732 // Defintions 00733 00734 00735 // 00736 // basic_FancyOStream_buf 00737 // 00738 00739 00740 template<typename CharT, typename Traits> 00741 basic_FancyOStream_buf<CharT,Traits>::basic_FancyOStream_buf( 00742 const RCP<std::basic_ostream<char_type,traits_type> > &oStream 00743 ,const std::basic_string<char_type,traits_type> &tabIndentStr 00744 ,const int startingTab 00745 ,const bool showLinePrefix 00746 ,const int maxLenLinePrefix 00747 ,const bool showTabCount 00748 ,const bool showProcRank 00749 ) 00750 { 00751 this->initialize(oStream,tabIndentStr,startingTab,showLinePrefix, 00752 maxLenLinePrefix,showTabCount,showProcRank); 00753 } 00754 00755 00756 template<typename CharT, typename Traits> 00757 void basic_FancyOStream_buf<CharT,Traits>::initialize( 00758 const RCP<std::basic_ostream<char_type,traits_type> > &oStream 00759 ,const std::basic_string<char_type,traits_type> &tabIndentStr 00760 ,const int startingTab 00761 ,const bool showLinePrefix 00762 ,const int maxLenLinePrefix 00763 ,const bool showTabCount 00764 ,const bool showProcRank 00765 ) 00766 { 00767 oStreamSet_ = oStream; 00768 oStream_ = oStream; 00769 tabIndentStr_ = tabIndentStr; 00770 showLinePrefix_ = showLinePrefix; 00771 maxLenLinePrefix_ = maxLenLinePrefix; 00772 showTabCount_ = showTabCount; 00773 showProcRank_ = showProcRank; 00774 rootRank_ = -1; 00775 procRank_ = GlobalMPISession::getRank(); 00776 numProcs_ = GlobalMPISession::getNProc(); 00777 rankPrintWidth_ = int(std::log10(float(numProcs_)))+1; 00778 tabIndent_ = startingTab; 00779 tabIndentStack_.clear(); 00780 linePrefixStack_.clear(); 00781 wroteNewline_ = true; 00782 enableTabbingStack_ = 0; 00783 } 00784 00785 00786 template<typename CharT, typename Traits> 00787 RCP<std::basic_ostream<CharT,Traits> > 00788 basic_FancyOStream_buf<CharT,Traits>::getOStream() 00789 { 00790 return oStreamSet_; 00791 } 00792 00793 00794 template<typename CharT, typename Traits> 00795 void basic_FancyOStream_buf<CharT,Traits>::setTabIndentStr( 00796 const std::basic_string<char_type,traits_type> &tabIndentStr 00797 ) 00798 { 00799 tabIndentStr_ = tabIndentStr; 00800 } 00801 00802 00803 template<typename CharT, typename Traits> 00804 const std::basic_string<CharT,Traits>& 00805 basic_FancyOStream_buf<CharT,Traits>::getTabIndentStr() const 00806 { 00807 return tabIndentStr_; 00808 } 00809 00810 00811 template<typename CharT, typename Traits> 00812 void basic_FancyOStream_buf<CharT,Traits>::setShowLinePrefix(const bool showLinePrefix) 00813 { 00814 showLinePrefix_ = showLinePrefix; 00815 } 00816 00817 00818 template<typename CharT, typename Traits> 00819 bool basic_FancyOStream_buf<CharT,Traits>::getShowLinePrefix() const 00820 { 00821 return showLinePrefix_; 00822 } 00823 00824 00825 template<typename CharT, typename Traits> 00826 void basic_FancyOStream_buf<CharT,Traits>::setMaxLenLinePrefix(const int maxLenLinePrefix) 00827 { 00828 TEST_FOR_EXCEPT( !(maxLenLinePrefix>=5) ); 00829 maxLenLinePrefix_ = maxLenLinePrefix; 00830 } 00831 00832 00833 template<typename CharT, typename Traits> 00834 int basic_FancyOStream_buf<CharT,Traits>::getMaxLenLinePrefix() const 00835 { 00836 return maxLenLinePrefix_; 00837 } 00838 00839 00840 template<typename CharT, typename Traits> 00841 void basic_FancyOStream_buf<CharT,Traits>::setShowTabCount(const bool showTabCount) 00842 { 00843 showTabCount_ = showTabCount; 00844 } 00845 00846 00847 template<typename CharT, typename Traits> 00848 bool basic_FancyOStream_buf<CharT,Traits>::getShowTabCount() const 00849 { 00850 return showTabCount_; 00851 } 00852 00853 00854 template<typename CharT, typename Traits> 00855 void basic_FancyOStream_buf<CharT,Traits>::setShowProcRank(const bool showProcRank) 00856 { 00857 showProcRank_ = showProcRank; 00858 } 00859 00860 00861 template<typename CharT, typename Traits> 00862 bool basic_FancyOStream_buf<CharT,Traits>::getShowProcRank() const 00863 { 00864 return showProcRank_; 00865 } 00866 00867 00868 template<typename CharT, typename Traits> 00869 void basic_FancyOStream_buf<CharT,Traits>::setProcRankAndSize( 00870 const int procRank, const int numProcs 00871 ) 00872 { 00873 procRank_ = procRank; 00874 numProcs_ = numProcs; 00875 } 00876 00877 00878 template<typename CharT, typename Traits> 00879 int basic_FancyOStream_buf<CharT,Traits>::getProcRank() const 00880 { 00881 return procRank_; 00882 } 00883 00884 00885 template<typename CharT, typename Traits> 00886 int basic_FancyOStream_buf<CharT,Traits>::getNumProcs() const 00887 { 00888 return numProcs_; 00889 } 00890 00891 00892 template<typename CharT, typename Traits> 00893 void basic_FancyOStream_buf<CharT,Traits>::setOutputToRootOnly( 00894 const int rootRank 00895 ) 00896 { 00897 rootRank_ = rootRank; 00898 if(rootRank >= 0) { 00899 if(rootRank == procRank_) 00900 oStream_ = oStreamSet_; 00901 else 00902 oStream_ = rcp(new oblackholestream()); 00903 // Only processor is being output to so there is no need for line 00904 // batching! 00905 lineOut_ = null; 00906 } 00907 else { 00908 oStream_ = oStreamSet_; 00909 // Output is being sent to all processors so we need line batching to 00910 // insure that each line will be printed all together! 00911 lineOut_ = rcp(new std::ostringstream()); 00912 } 00913 } 00914 00915 00916 template<typename CharT, typename Traits> 00917 int basic_FancyOStream_buf<CharT,Traits>::getOutputToRootOnly() const 00918 { 00919 return rootRank_; 00920 } 00921 00922 00923 template<typename CharT, typename Traits> 00924 void basic_FancyOStream_buf<CharT,Traits>::pushTab(const int tabs) 00925 { 00926 if( tabIndent_ + tabs < 0 ) { 00927 tabIndentStack_.push_back(-tabIndent_); 00928 tabIndent_ = 0; 00929 } 00930 else { 00931 tabIndentStack_.push_back(tabs); 00932 tabIndent_ += tabs; 00933 } 00934 } 00935 00936 00937 template<typename CharT, typename Traits> 00938 int basic_FancyOStream_buf<CharT,Traits>::getNumCurrTabs() const 00939 { 00940 return tabIndent_; 00941 } 00942 00943 00944 template<typename CharT, typename Traits> 00945 void basic_FancyOStream_buf<CharT,Traits>::popTab() 00946 { 00947 tabIndent_ -= tabIndentStack_.back(); 00948 tabIndentStack_.pop_back(); 00949 } 00950 00951 00952 template<typename CharT, typename Traits> 00953 void basic_FancyOStream_buf<CharT,Traits>::pushLinePrefix( 00954 const std::basic_string<char_type,traits_type> &linePrefix 00955 ) 00956 { 00957 linePrefixStack_.push_back(linePrefix); 00958 } 00959 00960 00961 template<typename CharT, typename Traits> 00962 void basic_FancyOStream_buf<CharT,Traits>::popLinePrefix() 00963 { 00964 linePrefixStack_.pop_back(); 00965 } 00966 00967 00968 template<typename CharT, typename Traits> 00969 const std::basic_string<CharT,Traits>& 00970 basic_FancyOStream_buf<CharT,Traits>::getTopLinePrefix() const 00971 { 00972 return linePrefixStack_.back(); 00973 } 00974 00975 00976 template<typename CharT, typename Traits> 00977 void basic_FancyOStream_buf<CharT,Traits>::pushDisableTabbing() 00978 { 00979 ++enableTabbingStack_; 00980 } 00981 00982 00983 template<typename CharT, typename Traits> 00984 void basic_FancyOStream_buf<CharT,Traits>::popDisableTabbing() 00985 { 00986 --enableTabbingStack_; 00987 } 00988 00989 00990 // protected 00991 00992 00993 template<typename CharT, typename Traits> 00994 std::streamsize basic_FancyOStream_buf<CharT,Traits>::xsputn( 00995 const char_type* s, std::streamsize n 00996 ) 00997 { 00998 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS 00999 std::cerr << "\ncalled xsputn()\n"; 01000 #endif 01001 writeChars(s,n); 01002 return n; 01003 } 01004 01005 01006 template<typename CharT, typename Traits> 01007 typename basic_FancyOStream_buf<CharT,Traits>::int_type 01008 basic_FancyOStream_buf<CharT,Traits>::overflow(int_type c) 01009 { 01010 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS 01011 std::cerr << "\ncalled overflow()\n"; 01012 #endif 01013 if(c != traits_type::eof()) { 01014 const char_type cc[] = { traits_type::to_char_type(c) }; 01015 this->writeChars(cc,1); 01016 } 01017 return traits_type::not_eof(c); 01018 //return std::basic_streambuf<CharT,Traits>::overflow(c); 01019 } 01020 01021 01022 // private 01023 01024 01025 template<typename CharT, typename Traits> 01026 std::ostream& basic_FancyOStream_buf<CharT,Traits>::out() 01027 { 01028 if(lineOut_.get()) 01029 return *lineOut_; 01030 return *oStream_; 01031 } 01032 01033 01034 template<typename CharT, typename Traits> 01035 void basic_FancyOStream_buf<CharT,Traits>::writeChars( 01036 const char_type s[], std::streamsize n 01037 ) 01038 { 01039 if(n == 0) return; 01040 std::streamsize p = 0, first_p = 0; 01041 bool done_outputting = false; 01042 const char_type newline = '\n'; 01043 while( !done_outputting ) { 01044 // Find the next newline 01045 for( p = first_p; p < n; ++p ) { 01046 if(s[p] == newline) { 01047 break; 01048 } 01049 } 01050 if(p == n) { 01051 // We did not find a newline at the end! 01052 --p; 01053 done_outputting = true; 01054 } 01055 else if( p == n-1 && s[p] == newline ) { 01056 // The last character in the std::string is a newline 01057 done_outputting = true; 01058 } 01059 // Write the beginning of the line if we need to 01060 if(wroteNewline_) { 01061 writeFrontMatter(); 01062 wroteNewline_ = false; 01063 } 01064 // Write up to the newline or the end of the std::string 01065 out().write(s+first_p,p-first_p+1); 01066 if(s[p] == newline) { 01067 wroteNewline_ = true; 01068 if(lineOut_.get()) { 01069 *oStream_ << lineOut_->str() << std::flush; 01070 lineOut_->str(""); 01071 } 01072 } 01073 // Update for next search 01074 if(!done_outputting) 01075 first_p = p+1; 01076 } 01077 } 01078 01079 01080 template<typename CharT, typename Traits> 01081 void basic_FancyOStream_buf<CharT,Traits>::writeFrontMatter() 01082 { 01083 bool didOutput = false; 01084 std::ostream &o = this->out(); 01085 if(showProcRank_) { 01086 o << "p=" << std::right << std::setw(rankPrintWidth_) << procRank_; 01087 didOutput = true; 01088 } 01089 if(showLinePrefix_) { 01090 if(didOutput) 01091 o << ", "; 01092 std::string currLinePrefix = ""; 01093 if ( linePrefixStack_.size() ) 01094 currLinePrefix = this->getTopLinePrefix(); 01095 const int localMaxLenLinePrefix = 01096 TEUCHOS_MAX( as<int>(currLinePrefix.length()), maxLenLinePrefix_ ); 01097 o << std::left << std::setw(localMaxLenLinePrefix); 01098 o << currLinePrefix; 01099 didOutput = true; 01100 } 01101 if(showTabCount_) { 01102 if(didOutput) 01103 o << ", "; 01104 o << "tabs=" << std::right << std::setw(2) << tabIndent_; 01105 didOutput = true; 01106 } 01107 // ToDo: Add the Prefix name if asked 01108 // ToDo: Add the processor number if asked 01109 // ToDo: Add the number of indents if asked 01110 if(didOutput) { 01111 o << " |" << tabIndentStr_; 01112 } 01113 if(enableTabbingStack_==0) { 01114 for( int i = 0; i < tabIndent_; ++i ) 01115 o << tabIndentStr_; 01116 } 01117 } 01118 01119 01120 // 01121 // basic_FancyOStream 01122 // 01123 01124 01125 template<typename CharT, typename Traits> 01126 basic_FancyOStream<CharT,Traits>::basic_FancyOStream( 01127 const RCP< std::basic_ostream<char_type,traits_type> > &oStream 01128 ,const std::basic_string<char_type,traits_type> &tabIndentStr 01129 ,const int startingTab 01130 ,const bool showLinePrefix 01131 ,const int maxLenLinePrefix 01132 ,const bool showTabCount 01133 ,const bool showProcRank 01134 ) 01135 :ostream_t(NULL), 01136 streambuf_(oStream,tabIndentStr,startingTab,showLinePrefix, 01137 maxLenLinePrefix,showTabCount,showProcRank) 01138 { 01139 this->init(&streambuf_); 01140 } 01141 01142 01143 template<typename CharT, typename Traits> 01144 void basic_FancyOStream<CharT,Traits>::initialize( 01145 const RCP< std::basic_ostream<char_type,traits_type> > &oStream 01146 ,const std::basic_string<char_type,traits_type> &tabIndentStr 01147 ,const int startingTab 01148 ,const bool showLinePrefix 01149 ,const int maxLenLinePrefix 01150 ,const bool showTabCount 01151 ,const bool showProcRank 01152 ) 01153 { 01154 streambuf_.initialize(oStream,tabIndentStr,startingTab, 01155 showLinePrefix,maxLenLinePrefix,showTabCount,showProcRank); 01156 this->init(&streambuf_); 01157 } 01158 01159 01160 template<typename CharT, typename Traits> 01161 RCP<std::basic_ostream<CharT,Traits> > 01162 basic_FancyOStream<CharT,Traits>::getOStream() 01163 { 01164 return streambuf_.getOStream(); 01165 } 01166 01167 01168 template<typename CharT, typename Traits> 01169 basic_FancyOStream<CharT,Traits>& 01170 basic_FancyOStream<CharT,Traits>::setTabIndentStr( 01171 const std::basic_string<char_type,traits_type> &tabIndentStr 01172 ) 01173 { 01174 streambuf_.setTabIndentStr(tabIndentStr); 01175 return *this; 01176 } 01177 01178 01179 template<typename CharT, typename Traits> 01180 const std::basic_string<CharT,Traits>& 01181 basic_FancyOStream<CharT,Traits>::getTabIndentStr() const 01182 { 01183 return streambuf_.getTabIndentStr(); 01184 } 01185 01186 01187 template<typename CharT, typename Traits> 01188 basic_FancyOStream<CharT,Traits>& 01189 basic_FancyOStream<CharT,Traits>::setShowAllFrontMatter( 01190 const bool showAllFrontMatter 01191 ) 01192 { 01193 streambuf_.setShowLinePrefix(showAllFrontMatter); 01194 streambuf_.setShowTabCount(showAllFrontMatter); 01195 streambuf_.setShowProcRank(showAllFrontMatter); 01196 return *this; 01197 } 01198 01199 01200 template<typename CharT, typename Traits> 01201 basic_FancyOStream<CharT,Traits>& 01202 basic_FancyOStream<CharT,Traits>::setShowLinePrefix(const bool showLinePrefix) 01203 { 01204 streambuf_.setShowLinePrefix(showLinePrefix); 01205 return *this; 01206 } 01207 01208 01209 template<typename CharT, typename Traits> 01210 basic_FancyOStream<CharT,Traits>& 01211 basic_FancyOStream<CharT,Traits>::setMaxLenLinePrefix(const int maxLenLinePrefix) 01212 { 01213 streambuf_.setMaxLenLinePrefix(maxLenLinePrefix); 01214 return *this; 01215 } 01216 01217 01218 template<typename CharT, typename Traits> 01219 basic_FancyOStream<CharT,Traits>& 01220 basic_FancyOStream<CharT,Traits>::setShowTabCount(const bool showTabCount) 01221 { 01222 streambuf_.setShowTabCount(showTabCount); 01223 return *this; 01224 } 01225 01226 01227 template<typename CharT, typename Traits> 01228 basic_FancyOStream<CharT,Traits>& 01229 basic_FancyOStream<CharT,Traits>::setShowProcRank(const bool showProcRank) 01230 { 01231 streambuf_.setShowProcRank(showProcRank); 01232 return *this; 01233 } 01234 01235 01236 template<typename CharT, typename Traits> 01237 basic_FancyOStream<CharT,Traits>& 01238 basic_FancyOStream<CharT,Traits>::setProcRankAndSize( const int procRank, const int numProcs ) 01239 { 01240 streambuf_.setProcRankAndSize(procRank,numProcs); 01241 return *this; 01242 } 01243 01244 01245 template<typename CharT, typename Traits> 01246 basic_FancyOStream<CharT,Traits>& 01247 basic_FancyOStream<CharT,Traits>::setOutputToRootOnly( const int rootRank ) 01248 { 01249 streambuf_.setOutputToRootOnly(rootRank); 01250 return *this; 01251 } 01252 01253 01254 template<typename CharT, typename Traits> 01255 int basic_FancyOStream<CharT,Traits>::getOutputToRootOnly() const 01256 { 01257 return streambuf_.getOutputToRootOnly(); 01258 } 01259 01260 01261 template<typename CharT, typename Traits> 01262 void basic_FancyOStream<CharT,Traits>::copyAllOutputOptions( 01263 const basic_FancyOStream<CharT,Traits> &oStream ) 01264 { 01265 //streambuf_.setTabIndentStr(oStream.streambuf_.getTabIndentStr()); 01266 streambuf_.setShowLinePrefix(oStream.streambuf_.getShowLinePrefix()); 01267 streambuf_.setMaxLenLinePrefix(oStream.streambuf_.getMaxLenLinePrefix()); 01268 streambuf_.setShowTabCount(oStream.streambuf_.getShowTabCount()); 01269 streambuf_.setShowProcRank(oStream.streambuf_.getShowProcRank()); 01270 streambuf_.setProcRankAndSize(oStream.streambuf_.getProcRank(), 01271 oStream.streambuf_.getNumProcs()); 01272 streambuf_.setOutputToRootOnly(oStream.streambuf_.getOutputToRootOnly()); 01273 } 01274 01275 01276 template<typename CharT, typename Traits> 01277 void basic_FancyOStream<CharT,Traits>::pushTab(const int tabs) 01278 { 01279 streambuf_.pushTab(tabs); 01280 } 01281 01282 01283 template<typename CharT, typename Traits> 01284 int basic_FancyOStream<CharT,Traits>::getNumCurrTabs() const 01285 { 01286 return streambuf_.getNumCurrTabs(); 01287 } 01288 01289 01290 template<typename CharT, typename Traits> 01291 void basic_FancyOStream<CharT,Traits>::popTab() 01292 { 01293 streambuf_.popTab(); 01294 } 01295 01296 01297 template<typename CharT, typename Traits> 01298 void basic_FancyOStream<CharT,Traits>::pushLinePrefix( 01299 const std::basic_string<char_type,traits_type> &linePrefix 01300 ) 01301 { 01302 streambuf_.pushLinePrefix(linePrefix); 01303 } 01304 01305 01306 template<typename CharT, typename Traits> 01307 void basic_FancyOStream<CharT,Traits>::popLinePrefix() 01308 { 01309 streambuf_.popLinePrefix(); 01310 } 01311 01312 01313 template<typename CharT, typename Traits> 01314 const std::basic_string<CharT,Traits>& 01315 basic_FancyOStream<CharT,Traits>::getTopLinePrefix() const 01316 { 01317 return streambuf_.getTopLinePrefix(); 01318 } 01319 01320 01321 template<typename CharT, typename Traits> 01322 void basic_FancyOStream<CharT,Traits>::pushDisableTabbing() 01323 { 01324 streambuf_.pushDisableTabbing(); 01325 } 01326 01327 01328 template<typename CharT, typename Traits> 01329 void basic_FancyOStream<CharT,Traits>::popDisableTabbing() 01330 { 01331 return streambuf_.popDisableTabbing(); 01332 } 01333 01334 01335 } // namespace Teuchos 01336 01337 01338 #endif // TEUCHOS_FANCY_O_STREAM_HPP
1.7.4