Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_FancyOStream.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef TEUCHOS_FANCY_O_STREAM_HPP
43 #define TEUCHOS_FANCY_O_STREAM_HPP
44 
45 #include "Teuchos_RCP.hpp"
47 #include "Teuchos_oblackholestream.hpp"
48 #include "Teuchos_as.hpp"
49 
50 
51 namespace Teuchos {
52 
53 
62 template<typename CharT, typename Traits>
63 class basic_FancyOStream_buf : public std::basic_streambuf<CharT,Traits>
64 {
65 public:
66 
68  typedef CharT char_type;
70  typedef Traits traits_type;
72  typedef typename traits_type::int_type int_type;
74  typedef typename traits_type::pos_type pos_type;
76  typedef typename traits_type::off_type off_type;
77 
80  const RCP<std::basic_ostream<char_type,traits_type> > &oStream
81  ,const std::basic_string<char_type,traits_type> &tabIndentStr
82  ,const int startingTab
83  ,const bool showLinePrefix
84  ,const int maxLenLinePrefix
85  ,const bool showTabCount
86  ,const bool showProcRank
87  );
88 
90  void initialize(
91  const RCP<std::basic_ostream<char_type,traits_type> > &oStream
92  ,const std::basic_string<char_type,traits_type> &tabIndentStr
93  ,const int startingTab
94  ,const bool showLinePrefix
95  ,const int maxLenLinePrefix
96  ,const bool showTabCount
97  ,const bool showProcRank
98  );
99 
102 
104  void setTabIndentStr(const std::basic_string<char_type,traits_type> &tabIndentStr);
105 
107  const std::basic_string<char_type,traits_type>& getTabIndentStr() const;
108 
110  void setShowLinePrefix(const bool showLinePrefix);
111 
113  bool getShowLinePrefix() const;
114 
116  void setMaxLenLinePrefix(const int maxLenLinePrefix);
117 
119  int getMaxLenLinePrefix() const;
120 
122  void setShowTabCount(const bool showTabCount);
123 
125  bool getShowTabCount() const;
126 
128  void setShowProcRank(const bool showProcRank);
129 
131  bool getShowProcRank() const;
132 
134  void setProcRankAndSize( const int procRank, const int numProcs );
135 
137  int getProcRank() const;
138 
140  int getNumProcs() const;
141 
143  void setOutputToRootOnly( const int rootRank );
144 
146  int getOutputToRootOnly() const;
147 
149  void pushTab(const int tabs);
150 
152  int getNumCurrTabs() const;
153 
155  void popTab();
156 
158  void pushLinePrefix(
159  const std::basic_string<char_type,traits_type> &linePrefix
160  );
161 
163  void popLinePrefix();
164 
166  const std::basic_string<char_type,traits_type>& getTopLinePrefix() const;
167 
169  void pushDisableTabbing();
170 
172  void popDisableTabbing();
173 
174 protected:
175 
177 
178 
180  std::streamsize xsputn(const char_type* s, std::streamsize n);
181 
184 
185 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
186 
187  void imbue(const locale& l)
188  {
189  std::cerr << "\ncalled imbue()\n";
190  std::basic_streambuf<CharT,Traits>::imbue(l);
191  }
192 
193  basic_streambuf<char_type,Traits>*
194  setbuf(char_type* s, streamsize n)
195  {
196  std::cerr << "\ncalled setbuf()\n";
197  return std::basic_streambuf<CharT,Traits>::setbuf(s,n);
198  }
199 
200  pos_type
201  seekoff(off_type a, ios_base::seekdir b,ios_base::openmode c)
202  {
203  std::cerr << "\ncalled seekoff()\n";
204  return std::basic_streambuf<CharT,Traits>::seekoff(a,b,c);
205  }
206 
207  pos_type
208  seekpos(pos_type a, ios_base::openmode b)
209  {
210  std::cerr << "\ncalled seekpos()\n";
211  return std::basic_streambuf<CharT,Traits>::seekpos(a,b);
212  }
213 
214  int
215  sync()
216  {
217  std::cerr << "\ncalled sync()\n";
218  return std::basic_streambuf<CharT,Traits>::sync();
219  }
220 
221  streamsize
222  showmanyc()
223  {
224  std::cerr << "\ncalled showmanyc()\n";
225  return std::basic_streambuf<CharT,Traits>::showmanyc();
226  }
227 
228  streamsize
229  xsgetn(char_type* s, streamsize n)
230  {
231  std::cerr << "\ncalled xsgetn()\n";
232  return std::basic_streambuf<CharT,Traits>::xsgetn(s,n);
233  }
234 
235  int_type
236  underflow()
237  {
238  std::cerr << "\ncalled underflow()\n";
239  return std::basic_streambuf<CharT,Traits>::underflow();
240  }
241 
242  int_type
243  uflow()
244  {
245  std::cerr << "\ncalled uflow()\n";
246  return std::basic_streambuf<CharT,Traits>::uflow();
247  }
248 
249  int_type
250  pbackfail(int_type c = traits_type::eof())
251  {
252  std::cerr << "\ncalled pbackfail()\n";
253  return std::basic_streambuf<CharT,Traits>::pbackfail(c);
254  }
255 
256 #endif // TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
257 
259 
260 private:
261 
262  // ////////////////////////
263  // Private types
264 
265  typedef std::basic_string<char_type,traits_type> string_t;
266  typedef std::deque<int> tabIndentStack_t;
267  typedef std::deque<string_t> linePrefixStack_t;
268 
269  // ////////////////////////
270  // Private data members
271 
272  RCP<std::basic_ostream<char_type,traits_type> > oStreamSet_;
273  RCP<std::basic_ostream<char_type,traits_type> > oStream_;
274  std::basic_string<char_type,traits_type> tabIndentStr_;
275  bool showLinePrefix_;
276  int maxLenLinePrefix_;
277  bool showTabCount_;
278  bool showProcRank_;
279  int rootRank_;
280  int procRank_;
281  int numProcs_;
282  int rankPrintWidth_;
283 
284  RCP<std::ostringstream> lineOut_;
285 
286  int tabIndent_;
287  tabIndentStack_t tabIndentStack_;
288  linePrefixStack_t linePrefixStack_;
289  int enableTabbingStack_;
290 
291  bool wroteNewline_;
292 
293  // ////////////////////////
294  // Private member functions
295 
296  std::ostream& out();
297 
298  void writeChars( const char_type s[], std::streamsize n );
299 
300  void writeFrontMatter();
301 
302  // Not defined and not to be called
304  basic_FancyOStream_buf(const basic_FancyOStream_buf<CharT,Traits>&);
305  basic_FancyOStream_buf<CharT,Traits> operator=(
306  const basic_FancyOStream_buf<CharT,Traits>&
307  );
308 
309 };
310 
311 
333 template <typename CharT, typename Traits = std::char_traits<CharT> >
334 class basic_FancyOStream : public std::basic_ostream<CharT, Traits>
335 {
336 public:
337 
339 
340 
342  typedef CharT char_type;
344  typedef Traits traits_type;
346  typedef typename traits_type::int_type int_type;
348  typedef typename traits_type::pos_type pos_type;
350  typedef typename traits_type::off_type off_type;
356  typedef std::basic_ostream<char_type, traits_type> ostream_t;
357 
359 
361 
362 
364  explicit
366  const RCP< std::basic_ostream<char_type,traits_type> > &oStream
367  ,const std::basic_string<char_type,traits_type> &tabIndentStr = " "
368  ,const int startingTab = 0
369  ,const bool showLinePrefix = false
370  ,const int maxLenLinePrefix = 10
371  ,const bool showTabCount = false
372  ,const bool showProcRank = false
373  );
374 
376  void initialize(
377  const RCP< std::basic_ostream<char_type,traits_type> > &oStream
378  ,const std::basic_string<char_type,traits_type> &tabIndentStr = " "
379  ,const int startingTab = 0
380  ,const bool showLinePrefix = false
381  ,const int maxLenLinePrefix = 10
382  ,const bool showTabCount = false
383  ,const bool showProcRank = false
384  );
385 
388 
391  const std::basic_string<char_type,traits_type> &tabIndentStr
392  );
393 
395  const std::basic_string<char_type,traits_type>& getTabIndentStr() const;
396 
398  basic_FancyOStream& setShowAllFrontMatter(const bool showAllFrontMatter);
399 
401  basic_FancyOStream& setShowLinePrefix(const bool showLinePrefix);
402 
404  basic_FancyOStream& setMaxLenLinePrefix(const int maxLenLinePrefix);
405 
407  basic_FancyOStream& setShowTabCount(const bool showTabCount);
408 
410  basic_FancyOStream& setShowProcRank(const bool showProcRank);
411 
413  basic_FancyOStream& setProcRankAndSize( const int procRank, const int numProcs );
414 
416  basic_FancyOStream& setOutputToRootOnly( const int rootRank );
417 
419  int getOutputToRootOnly() const;
420 
423 
425 
427 
428 
430  void pushTab(const int tabs = 1);
431 
433  int getNumCurrTabs() const;
434 
436  void popTab();
437 
439  void pushLinePrefix(const std::basic_string<char_type,traits_type> &linePrefix);
440 
442  void popLinePrefix();
443 
445  const std::basic_string<char_type,traits_type>& getTopLinePrefix() const;
446 
448  void pushDisableTabbing();
449 
451  void popDisableTabbing();
452 
454 
455 private:
456 
457  streambuf_t streambuf_;
458 
459  // Not defined and not to be called
463 
464 };
465 
466 
474 inline
477  const RCP< std::basic_ostream<char> >& oStream,
478  const std::basic_string<char>& tabIndentStr = " ",
479  const int startingTab = 0,
480  const bool showLinePrefix = false,
481  const int maxLenLinePrefix = 10,
482  const bool showTabCount = false,
483  const bool showProcRank = false
484  )
485 {
486  if (nonnull(oStream)) {
487  return rcp(
489  oStream,tabIndentStr,startingTab,showLinePrefix,
490  maxLenLinePrefix,showTabCount,showProcRank
491  )
492  );
493  }
494  return null;
495 }
496 
497 
506 inline
508 getFancyOStream( const RCP<std::basic_ostream<char> > &out )
509 {
510  if (is_null(out))
511  return Teuchos::null;
513  fancyOut = rcp_dynamic_cast<basic_FancyOStream<char> >(out);
514  if(nonnull(fancyOut))
515  return fancyOut;
516  return rcp(new basic_FancyOStream<char>(out));
517 }
518 
519 
531 template <typename CharT, typename Traits = std::char_traits<CharT> >
533 {
534 public:
535 
537  static const int DISABLE_TABBING = -99999; // This magic number should be just fine!
540  const RCP<basic_FancyOStream<CharT,Traits> > &fancyOStream
541  ,const int tabs = 1
542  ,const std::basic_string<CharT,Traits> linePrefix = ""
543  )
544  :fancyOStream_(fancyOStream)
545  ,tabs_(tabs)
546  ,linePrefix_(linePrefix)
547  {
548  updateState();
549  }
552  const RCP<std::basic_ostream<CharT,Traits> > &oStream
553  ,const int tabs = 1
554  ,const std::basic_string<CharT,Traits> linePrefix = ""
555  )
556  :fancyOStream_(getFancyOStream(oStream))
557  ,tabs_(tabs)
558  ,linePrefix_(linePrefix)
559  {
560  updateState();
561  }
565  ,const int tabs = 1
566  ,const std::basic_string<CharT,Traits> linePrefix = ""
567  )
568  :fancyOStream_(rcp(&fancyOStream,false))
569  ,tabs_(tabs)
570  ,linePrefix_(linePrefix)
571  {
572  updateState();
573  }
576  std::basic_ostream<CharT,Traits> &oStream
577  ,const int tabs = 1
578  ,const std::basic_string<CharT,Traits> linePrefix = ""
579  )
580  :fancyOStream_(getFancyOStream(rcp(&oStream, false)))
581  ,tabs_(tabs)
582  ,linePrefix_(linePrefix)
583  {
584  updateState();
585  }
587  basic_OSTab( const basic_OSTab &osTab )
588  :fancyOStream_(osTab.fancyOStream_)
589  ,tabs_(osTab.tabs_)
590  {
591  updateState();
592  }
595  {
596  if(fancyOStream_.get()) {
597  if(tabs_==DISABLE_TABBING)
598  fancyOStream_->popDisableTabbing();
599  else
600  fancyOStream_->popTab();
601  if(linePrefix_.length()) fancyOStream_->popLinePrefix();
602  }
603  }
606  {
607  fancyOStream_ = osTab.fancyOStream_;
608  tabs_ = osTab.tabs_;
609  updateState();
610  return *this;
611  }
613  basic_OSTab<CharT,Traits>& incrTab(const int tabs = 1)
614  {
615  tabs_ += tabs;
616  if(fancyOStream_.get()) {
617  fancyOStream_->popTab();
618  fancyOStream_->pushTab(tabs_);
619  }
620  return *this;
621  }
624  {
625  return *fancyOStream_;
626  }
629  {
630  return fancyOStream_.get();
631  }
632 
633 private:
634 
635  RCP<basic_FancyOStream<CharT,Traits> > fancyOStream_;
636  int tabs_;
637  std::basic_string<CharT,Traits> linePrefix_;
638 
639  void updateState()
640  {
641  if(fancyOStream_.get()) {
642  if(tabs_==DISABLE_TABBING)
643  fancyOStream_->pushDisableTabbing();
644  else
645  fancyOStream_->pushTab(tabs_);
646  if(linePrefix_.length()) fancyOStream_->pushLinePrefix(linePrefix_);
647  }
648  }
649 
650 };
651 
652 
668 template <typename CharT, typename Traits>
669 RCP<basic_FancyOStream<CharT,Traits> >
672  const int tabs = 1,
673  const std::basic_string<CharT,Traits> linePrefix = ""
674  )
675 {
676  if(out.get()==NULL)
677  return Teuchos::null;
678  RCP<basic_FancyOStream<CharT,Traits> > fancyOut = out;
679  set_extra_data(
680  rcp(new basic_OSTab<CharT,Traits>(out,tabs,linePrefix)),
681  "OSTab",
682  inOutArg(fancyOut),
683  PRE_DESTROY,
684  false
685  );
686  return fancyOut;
687 }
688 
689 
705 template <typename CharT, typename Traits>
708  const RCP<std::basic_ostream<CharT,Traits> > &out
709  ,const int tabs = 1
710  ,const std::basic_string<CharT,Traits> linePrefix = ""
711  )
712 {
713  return tab(getFancyOStream(out),tabs,linePrefix);
714 }
715 
716 
717 // ///////////////////////////////
718 // Typedefs
719 
720 
725 
726 
731 
732 
736 #define TEUCHOS_OSTAB ::Teuchos::OSTab __localThisTab = this->getOSTab()
737 
741 #define TEUCHOS_OSTAB_DIFF( DIFF ) ::Teuchos::OSTab DIFF ## __localThisTab = this->getOSTab()
742 
743 
744 // ////////////////////////////////
745 // Defintions
746 
747 
748 //
749 // basic_FancyOStream_buf
750 //
751 
752 
753 template<typename CharT, typename Traits>
755  const RCP<std::basic_ostream<char_type,traits_type> > &oStream
756  ,const std::basic_string<char_type,traits_type> &tabIndentStr
757  ,const int startingTab
758  ,const bool showLinePrefix
759  ,const int maxLenLinePrefix
760  ,const bool showTabCount
761  ,const bool showProcRank
762  )
763 {
764  this->initialize(oStream,tabIndentStr,startingTab,showLinePrefix,
765  maxLenLinePrefix,showTabCount,showProcRank);
766 }
767 
768 
769 template<typename CharT, typename Traits>
771  const RCP<std::basic_ostream<char_type,traits_type> > &oStream
772  ,const std::basic_string<char_type,traits_type> &tabIndentStr
773  ,const int startingTab
774  ,const bool showLinePrefix
775  ,const int maxLenLinePrefix
776  ,const bool showTabCount
777  ,const bool showProcRank
778  )
779 {
780  oStreamSet_ = oStream;
781  oStream_ = oStream;
782  tabIndentStr_ = tabIndentStr;
783  showLinePrefix_ = showLinePrefix;
784  maxLenLinePrefix_ = maxLenLinePrefix;
785  showTabCount_ = showTabCount;
786  showProcRank_ = showProcRank;
787  rootRank_ = -1;
788  procRank_ = GlobalMPISession::getRank();
789  numProcs_ = GlobalMPISession::getNProc();
790  rankPrintWidth_ = int(std::log10(float(numProcs_)))+1;
791  tabIndent_ = startingTab;
792  tabIndentStack_.clear();
793  linePrefixStack_.clear();
794  wroteNewline_ = true;
795  enableTabbingStack_ = 0;
796 }
797 
798 
799 template<typename CharT, typename Traits>
802 {
803  return oStreamSet_;
804 }
805 
806 
807 template<typename CharT, typename Traits>
809  const std::basic_string<char_type,traits_type> &tabIndentStr
810  )
811 {
812  tabIndentStr_ = tabIndentStr;
813 }
814 
815 
816 template<typename CharT, typename Traits>
817 const std::basic_string<CharT,Traits>&
819 {
820  return tabIndentStr_;
821 }
822 
823 
824 template<typename CharT, typename Traits>
826 {
827  showLinePrefix_ = showLinePrefix;
828 }
829 
830 
831 template<typename CharT, typename Traits>
833 {
834  return showLinePrefix_;
835 }
836 
837 
838 template<typename CharT, typename Traits>
840 {
841  TEUCHOS_TEST_FOR_EXCEPT( !(maxLenLinePrefix>=5) );
842  maxLenLinePrefix_ = maxLenLinePrefix;
843 }
844 
845 
846 template<typename CharT, typename Traits>
848 {
849  return maxLenLinePrefix_;
850 }
851 
852 
853 template<typename CharT, typename Traits>
855 {
856  showTabCount_ = showTabCount;
857 }
858 
859 
860 template<typename CharT, typename Traits>
862 {
863  return showTabCount_;
864 }
865 
866 
867 template<typename CharT, typename Traits>
869 {
870  showProcRank_ = showProcRank;
871 }
872 
873 
874 template<typename CharT, typename Traits>
876 {
877  return showProcRank_;
878 }
879 
880 
881 template<typename CharT, typename Traits>
883  const int procRank, const int numProcs
884  )
885 {
886  procRank_ = procRank;
887  numProcs_ = numProcs;
888 }
889 
890 
891 template<typename CharT, typename Traits>
893 {
894  return procRank_;
895 }
896 
897 
898 template<typename CharT, typename Traits>
900 {
901  return numProcs_;
902 }
903 
904 
905 template<typename CharT, typename Traits>
907  const int rootRank
908  )
909 {
910  rootRank_ = rootRank;
911  if(rootRank >= 0) {
912  if(rootRank == procRank_)
913  oStream_ = oStreamSet_;
914  else
915  oStream_ = rcp(new oblackholestream());
916  // Only processor is being output to so there is no need for line
917  // batching!
918  lineOut_ = null;
919  }
920  else {
921  oStream_ = oStreamSet_;
922  // Output is being sent to all processors so we need line batching to
923  // insure that each line will be printed all together!
924  lineOut_ = rcp(new std::ostringstream());
925  }
926 }
927 
928 
929 template<typename CharT, typename Traits>
931 {
932  return rootRank_;
933 }
934 
935 
936 template<typename CharT, typename Traits>
938 {
939  if( tabIndent_ + tabs < 0 ) {
940  tabIndentStack_.push_back(-tabIndent_);
941  tabIndent_ = 0;
942  }
943  else {
944  tabIndentStack_.push_back(tabs);
945  tabIndent_ += tabs;
946  }
947 }
948 
949 
950 template<typename CharT, typename Traits>
952 {
953  return tabIndent_;
954 }
955 
956 
957 template<typename CharT, typename Traits>
959 {
960  tabIndent_ -= tabIndentStack_.back();
961  tabIndentStack_.pop_back();
962 }
963 
964 
965 template<typename CharT, typename Traits>
967  const std::basic_string<char_type,traits_type> &linePrefix
968  )
969 {
970  linePrefixStack_.push_back(linePrefix);
971 }
972 
973 
974 template<typename CharT, typename Traits>
976 {
977  linePrefixStack_.pop_back();
978 }
979 
980 
981 template<typename CharT, typename Traits>
982 const std::basic_string<CharT,Traits>&
984 {
985  return linePrefixStack_.back();
986 }
987 
988 
989 template<typename CharT, typename Traits>
991 {
992  ++enableTabbingStack_;
993 }
994 
995 
996 template<typename CharT, typename Traits>
998 {
999  --enableTabbingStack_;
1000 }
1001 
1002 
1003 // protected
1004 
1005 
1006 template<typename CharT, typename Traits>
1008  const char_type* s, std::streamsize n
1009  )
1010 {
1011 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
1012  std::cerr << "\ncalled xsputn()\n";
1013 #endif
1014  writeChars(s,n);
1015  return n;
1016 }
1017 
1018 
1019 template<typename CharT, typename Traits>
1022 {
1023 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
1024  std::cerr << "\ncalled overflow()\n";
1025 #endif
1026  if(c != traits_type::eof()) {
1027  const char_type cc[] = { traits_type::to_char_type(c) };
1028  this->writeChars(cc,1);
1029  }
1030  return traits_type::not_eof(c);
1031  //return std::basic_streambuf<CharT,Traits>::overflow(c);
1032 }
1033 
1034 
1035 // private
1036 
1037 
1038 template<typename CharT, typename Traits>
1040 {
1041  if(lineOut_.get())
1042  return *lineOut_;
1043  return *oStream_;
1044 }
1045 
1046 
1047 template<typename CharT, typename Traits>
1048 void basic_FancyOStream_buf<CharT,Traits>::writeChars(
1049  const char_type s[], std::streamsize n
1050  )
1051 {
1052  if(n == 0) return;
1053  std::streamsize p = 0, first_p = 0;
1054  bool done_outputting = false;
1055  const char_type newline = '\n';
1056  while( !done_outputting ) {
1057  // Find the next newline
1058  for( p = first_p; p < n; ++p ) {
1059  if(s[p] == newline) {
1060  break;
1061  }
1062  }
1063  if(p == n) {
1064  // We did not find a newline at the end!
1065  --p;
1066  done_outputting = true;
1067  }
1068  else if( p == n-1 && s[p] == newline ) {
1069  // The last character in the std::string is a newline
1070  done_outputting = true;
1071  }
1072  // Write the beginning of the line if we need to
1073  if(wroteNewline_) {
1074  writeFrontMatter();
1075  wroteNewline_ = false;
1076  }
1077  // Write up to the newline or the end of the std::string
1078  out().write(s+first_p,p-first_p+1);
1079  if(s[p] == newline) {
1080  wroteNewline_ = true;
1081  if(lineOut_.get()) {
1082  *oStream_ << lineOut_->str() << std::flush;
1083  lineOut_->str("");
1084  }
1085  }
1086  // Update for next search
1087  if(!done_outputting)
1088  first_p = p+1;
1089  }
1090 }
1091 
1092 
1093 template<typename CharT, typename Traits>
1094 void basic_FancyOStream_buf<CharT,Traits>::writeFrontMatter()
1095 {
1096  bool didOutput = false;
1097  std::ostream &o = this->out();
1098  if(showProcRank_) {
1099  o << "p=" << std::right << std::setw(rankPrintWidth_) << procRank_;
1100  didOutput = true;
1101  }
1102  if(showLinePrefix_) {
1103  if(didOutput)
1104  o << ", ";
1105  std::string currLinePrefix = "";
1106  if ( linePrefixStack_.size() )
1107  currLinePrefix = this->getTopLinePrefix();
1108  const int localMaxLenLinePrefix =
1109  TEUCHOS_MAX( as<int>(currLinePrefix.length()), maxLenLinePrefix_ );
1110  o << std::left << std::setw(localMaxLenLinePrefix);
1111  o << currLinePrefix;
1112  didOutput = true;
1113  }
1114  if(showTabCount_) {
1115  if(didOutput)
1116  o << ", ";
1117  o << "tabs=" << std::right << std::setw(2) << tabIndent_;
1118  didOutput = true;
1119  }
1120  // ToDo: Add the Prefix name if asked
1121  // ToDo: Add the processor number if asked
1122  // ToDo: Add the number of indents if asked
1123  if(didOutput) {
1124  o << " |" << tabIndentStr_;
1125  }
1126  if(enableTabbingStack_==0) {
1127  for( int i = 0; i < tabIndent_; ++i )
1128  o << tabIndentStr_;
1129  }
1130 }
1131 
1132 
1133 //
1134 // basic_FancyOStream
1135 //
1136 
1137 
1138 template<typename CharT, typename Traits>
1140  const RCP< std::basic_ostream<char_type,traits_type> > &oStream
1141  ,const std::basic_string<char_type,traits_type> &tabIndentStr
1142  ,const int startingTab
1143  ,const bool showLinePrefix
1144  ,const int maxLenLinePrefix
1145  ,const bool showTabCount
1146  ,const bool showProcRank
1147  )
1148  :ostream_t(NULL),
1149  streambuf_(oStream,tabIndentStr,startingTab,showLinePrefix,
1150  maxLenLinePrefix,showTabCount,showProcRank)
1151 {
1152  this->init(&streambuf_);
1153 }
1154 
1155 
1156 template<typename CharT, typename Traits>
1158  const RCP< std::basic_ostream<char_type,traits_type> > &oStream
1159  ,const std::basic_string<char_type,traits_type> &tabIndentStr
1160  ,const int startingTab
1161  ,const bool showLinePrefix
1162  ,const int maxLenLinePrefix
1163  ,const bool showTabCount
1164  ,const bool showProcRank
1165  )
1166 {
1167  streambuf_.initialize(oStream,tabIndentStr,startingTab,
1168  showLinePrefix,maxLenLinePrefix,showTabCount,showProcRank);
1169  this->init(&streambuf_);
1170 }
1171 
1172 
1173 template<typename CharT, typename Traits>
1176 {
1177  return streambuf_.getOStream();
1178 }
1179 
1180 
1181 template<typename CharT, typename Traits>
1184  const std::basic_string<char_type,traits_type> &tabIndentStr
1185  )
1186 {
1187  streambuf_.setTabIndentStr(tabIndentStr);
1188  return *this;
1189 }
1190 
1191 
1192 template<typename CharT, typename Traits>
1193 const std::basic_string<CharT,Traits>&
1195 {
1196  return streambuf_.getTabIndentStr();
1197 }
1198 
1199 
1200 template<typename CharT, typename Traits>
1203  const bool showAllFrontMatter
1204  )
1205 {
1206  streambuf_.setShowLinePrefix(showAllFrontMatter);
1207  streambuf_.setShowTabCount(showAllFrontMatter);
1208  streambuf_.setShowProcRank(showAllFrontMatter);
1209  return *this;
1210 }
1211 
1212 
1213 template<typename CharT, typename Traits>
1216 {
1217  streambuf_.setShowLinePrefix(showLinePrefix);
1218  return *this;
1219 }
1220 
1221 
1222 template<typename CharT, typename Traits>
1225 {
1226  streambuf_.setMaxLenLinePrefix(maxLenLinePrefix);
1227  return *this;
1228 }
1229 
1230 
1231 template<typename CharT, typename Traits>
1234 {
1235  streambuf_.setShowTabCount(showTabCount);
1236  return *this;
1237 }
1238 
1239 
1240 template<typename CharT, typename Traits>
1243 {
1244  streambuf_.setShowProcRank(showProcRank);
1245  return *this;
1246 }
1247 
1248 
1249 template<typename CharT, typename Traits>
1251 basic_FancyOStream<CharT,Traits>::setProcRankAndSize( const int procRank, const int numProcs )
1252 {
1253  streambuf_.setProcRankAndSize(procRank,numProcs);
1254  return *this;
1255 }
1256 
1257 
1258 template<typename CharT, typename Traits>
1261 {
1262  streambuf_.setOutputToRootOnly(rootRank);
1263  return *this;
1264 }
1265 
1266 
1267 template<typename CharT, typename Traits>
1269 {
1270  return streambuf_.getOutputToRootOnly();
1271 }
1272 
1273 
1274 template<typename CharT, typename Traits>
1276  const basic_FancyOStream<CharT,Traits> &oStream )
1277 {
1278  //streambuf_.setTabIndentStr(oStream.streambuf_.getTabIndentStr());
1279  streambuf_.setShowLinePrefix(oStream.streambuf_.getShowLinePrefix());
1280  streambuf_.setMaxLenLinePrefix(oStream.streambuf_.getMaxLenLinePrefix());
1281  streambuf_.setShowTabCount(oStream.streambuf_.getShowTabCount());
1282  streambuf_.setShowProcRank(oStream.streambuf_.getShowProcRank());
1283  streambuf_.setProcRankAndSize(oStream.streambuf_.getProcRank(),
1284  oStream.streambuf_.getNumProcs());
1285  streambuf_.setOutputToRootOnly(oStream.streambuf_.getOutputToRootOnly());
1286 }
1287 
1288 
1289 template<typename CharT, typename Traits>
1291 {
1292  streambuf_.pushTab(tabs);
1293 }
1294 
1295 
1296 template<typename CharT, typename Traits>
1298 {
1299  return streambuf_.getNumCurrTabs();
1300 }
1301 
1302 
1303 template<typename CharT, typename Traits>
1305 {
1306  streambuf_.popTab();
1307 }
1308 
1309 
1310 template<typename CharT, typename Traits>
1312  const std::basic_string<char_type,traits_type> &linePrefix
1313  )
1314 {
1315  streambuf_.pushLinePrefix(linePrefix);
1316 }
1317 
1318 
1319 template<typename CharT, typename Traits>
1321 {
1322  streambuf_.popLinePrefix();
1323 }
1324 
1325 
1326 template<typename CharT, typename Traits>
1327 const std::basic_string<CharT,Traits>&
1329 {
1330  return streambuf_.getTopLinePrefix();
1331 }
1332 
1333 
1334 template<typename CharT, typename Traits>
1336 {
1337  streambuf_.pushDisableTabbing();
1338 }
1339 
1340 
1341 template<typename CharT, typename Traits>
1343 {
1344  return streambuf_.popDisableTabbing();
1345 }
1346 
1347 
1348 } // namespace Teuchos
1349 
1350 
1351 #endif // TEUCHOS_FANCY_O_STREAM_HPP
basic_FancyOStream & setProcRankAndSize(const int procRank, const int numProcs)
static int getRank()
Returns the process rank relative to MPI_COMM_WORLD
basic_FancyOStream & setShowLinePrefix(const bool showLinePrefix)
bool is_null(const boost::shared_ptr< T > &p)
Returns true if p.get()==NULL.
basic_OSTab< char > OSTab
static int getNProc()
Returns the number of processors relative to MPI_COMM_WORLD
const std::basic_string< char_type, traits_type > & getTabIndentStr() const
RCP< basic_FancyOStream< char > > fancyOStream(const RCP< std::basic_ostream< char > > &oStream, const std::basic_string< char > &tabIndentStr=" ", const int startingTab=0, const bool showLinePrefix=false, const int maxLenLinePrefix=10, const bool showTabCount=false, const bool showProcRank=false)
Dynamically allocate a FancyOStream and return it wrapped in an RCP object.
basic_FancyOStream & setMaxLenLinePrefix(const int maxLenLinePrefix)
basic_OSTab(const RCP< std::basic_ostream< CharT, Traits > > &oStream, const int tabs=1, const std::basic_string< CharT, Traits > linePrefix="")
basic_FancyOStream & setShowProcRank(const bool showProcRank)
Stream buffering class that performs the magic of indenting data sent to an std::ostream object...
const std::basic_string< char_type, traits_type > & getTopLinePrefix() const
basic_FancyOStream< CharT, Traits > & o() const
basic_FancyOStream< char > FancyOStream
std::basic_ostream< char_type, traits_type > ostream_t
void initialize(const RCP< std::basic_ostream< char_type, traits_type > > &oStream, const std::basic_string< char_type, traits_type > &tabIndentStr, const int startingTab, const bool showLinePrefix, const int maxLenLinePrefix, const bool showTabCount, const bool showProcRank)
void setOutputToRootOnly(const int rootRank)
void setShowProcRank(const bool showProcRank)
static const int DISABLE_TABBING
basic_OSTab< CharT, Traits > & incrTab(const int tabs=1)
void setTabIndentStr(const std::basic_string< char_type, traits_type > &tabIndentStr)
void initialize(const RCP< std::basic_ostream< char_type, traits_type > > &oStream, const std::basic_string< char_type, traits_type > &tabIndentStr=" ", const int startingTab=0, const bool showLinePrefix=false, const int maxLenLinePrefix=10, const bool showTabCount=false, const bool showProcRank=false)
Tabbing class for helping to create formated, indented output for a basic_FancyOStream object...
basic_OSTab(basic_FancyOStream< CharT, Traits > &fancyOStream, const int tabs=1, const std::basic_string< CharT, Traits > linePrefix="")
Warning: Only call this constructor for stack-based object.
void pushLinePrefix(const std::basic_string< char_type, traits_type > &linePrefix)
basic_FancyOStream & setShowTabCount(const bool showTabCount)
const std::basic_string< char_type, traits_type > & getTabIndentStr() const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
std::ostream subclass that performs the magic of indenting data sent to an std::ostream object among ...
basic_OSTab< CharT, Traits > & operator=(const basic_OSTab &osTab)
void setShowTabCount(const bool showTabCount)
void setShowLinePrefix(const bool showLinePrefix)
basic_FancyOStream & setShowAllFrontMatter(const bool showAllFrontMatter)
Set if processor rank, line prefixes, and tab counts are shown or not .
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
basic_FancyOStream_buf< CharT, Traits > streambuf_t
basic_FancyOStream & setTabIndentStr(const std::basic_string< char_type, traits_type > &tabIndentStr)
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
bool nonnull(const boost::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
std::streamsize xsputn(const char_type *s, std::streamsize n)
basic_OSTab(std::basic_ostream< CharT, Traits > &oStream, const int tabs=1, const std::basic_string< CharT, Traits > linePrefix="")
Warning: Only call this constructor for stack-based object.
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
void setMaxLenLinePrefix(const int maxLenLinePrefix)
RCP< basic_FancyOStream< char > > getFancyOStream(const RCP< std::basic_ostream< char > > &out)
Get a FancyOStream from an std::ostream object.
RCP< basic_FancyOStream< CharT, Traits > > tab(const RCP< std::basic_ostream< CharT, Traits > > &out, const int tabs=1, const std::basic_string< CharT, Traits > linePrefix="")
Create a tab for an RCP-wrapped std:: std::ostream object to cause the indentation of all output auto...
basic_OSTab(const RCP< basic_FancyOStream< CharT, Traits > > &fancyOStream, const int tabs=1, const std::basic_string< CharT, Traits > linePrefix="")
Smart reference counting pointer class for automatic garbage collection.
void copyAllOutputOptions(const basic_FancyOStream< CharT, Traits > &oStream)
void pushLinePrefix(const std::basic_string< char_type, traits_type > &linePrefix)
RCP< basic_FancyOStream< CharT, Traits > > tab(const RCP< basic_FancyOStream< CharT, Traits > > &out, const int tabs=1, const std::basic_string< CharT, Traits > linePrefix="")
Create a tab for an RCP-wrapped basic_FancyOStream object to cause the indentation of all output auto...
const std::basic_string< char_type, traits_type > & getTopLinePrefix() const
Reference-counted pointer class and non-member templated function implementations.
Definition of Teuchos::as, for conversions between types.
basic_FancyOStream_buf(const RCP< std::basic_ostream< char_type, traits_type > > &oStream, const std::basic_string< char_type, traits_type > &tabIndentStr, const int startingTab, const bool showLinePrefix, const int maxLenLinePrefix, const bool showTabCount, const bool showProcRank)
void setProcRankAndSize(const int procRank, const int numProcs)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call...
basic_oblackholestream< char, std::char_traits< char > > oblackholestream
basic_OSTab(const basic_OSTab &osTab)