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 #include <deque>
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 
139  void setProcRankAndSize( const int procRank, const int numProcs );
140 
145  int getProcRank() const;
146 
151  int getNumProcs() const;
152 
167  void setOutputToRootOnly( const int rootRank );
168 
170  int getOutputToRootOnly() const;
171 
180  void pushTab(const int tabs);
181 
183  int getNumCurrTabs() const;
184 
192  void popTab();
193 
195  void pushLinePrefix(
196  const std::basic_string<char_type,traits_type> &linePrefix
197  );
198 
200  void popLinePrefix();
201 
203  const std::basic_string<char_type,traits_type>& getTopLinePrefix() const;
204 
206  void pushDisableTabbing();
207 
209  void popDisableTabbing();
210 
211 protected:
212 
214 
215 
217  std::streamsize xsputn(const char_type* s, std::streamsize n);
218 
221 
222 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
223 
224  void imbue(const locale& l)
225  {
226  std::cerr << "\ncalled imbue()\n";
227  std::basic_streambuf<CharT,Traits>::imbue(l);
228  }
229 
230  basic_streambuf<char_type,Traits>*
231  setbuf(char_type* s, streamsize n)
232  {
233  std::cerr << "\ncalled setbuf()\n";
234  return std::basic_streambuf<CharT,Traits>::setbuf(s,n);
235  }
236 
237  pos_type
238  seekoff(off_type a, ios_base::seekdir b,ios_base::openmode c)
239  {
240  std::cerr << "\ncalled seekoff()\n";
241  return std::basic_streambuf<CharT,Traits>::seekoff(a,b,c);
242  }
243 
244  pos_type
245  seekpos(pos_type a, ios_base::openmode b)
246  {
247  std::cerr << "\ncalled seekpos()\n";
248  return std::basic_streambuf<CharT,Traits>::seekpos(a,b);
249  }
250 
251  int
252  sync()
253  {
254  std::cerr << "\ncalled sync()\n";
255  return std::basic_streambuf<CharT,Traits>::sync();
256  }
257 
258  streamsize
259  showmanyc()
260  {
261  std::cerr << "\ncalled showmanyc()\n";
262  return std::basic_streambuf<CharT,Traits>::showmanyc();
263  }
264 
265  streamsize
266  xsgetn(char_type* s, streamsize n)
267  {
268  std::cerr << "\ncalled xsgetn()\n";
269  return std::basic_streambuf<CharT,Traits>::xsgetn(s,n);
270  }
271 
272  int_type
273  underflow()
274  {
275  std::cerr << "\ncalled underflow()\n";
276  return std::basic_streambuf<CharT,Traits>::underflow();
277  }
278 
279  int_type
280  uflow()
281  {
282  std::cerr << "\ncalled uflow()\n";
283  return std::basic_streambuf<CharT,Traits>::uflow();
284  }
285 
286  int_type
287  pbackfail(int_type c = traits_type::eof())
288  {
289  std::cerr << "\ncalled pbackfail()\n";
290  return std::basic_streambuf<CharT,Traits>::pbackfail(c);
291  }
292 
293 #endif // TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
294 
296 
297 private:
298 
299  // ////////////////////////
300  // Private types
301 
302  typedef std::basic_string<char_type,traits_type> string_t;
303  typedef std::deque<int> tabIndentStack_t;
304  typedef std::deque<string_t> linePrefixStack_t;
305 
306  // ////////////////////////
307  // Private data members
308 
309  RCP<std::basic_ostream<char_type,traits_type> > oStreamSet_;
310  RCP<std::basic_ostream<char_type,traits_type> > oStream_;
311  std::basic_string<char_type,traits_type> tabIndentStr_;
312  bool showLinePrefix_;
313  int maxLenLinePrefix_;
314  bool showTabCount_;
315  bool showProcRank_;
316  int rootRank_;
317  int procRank_;
318  int numProcs_;
319  int rankPrintWidth_;
320 
321  RCP<std::ostringstream> lineOut_;
322 
323  int tabIndent_;
324  tabIndentStack_t tabIndentStack_;
325  linePrefixStack_t linePrefixStack_;
326  int enableTabbingStack_;
327 
328  bool wroteNewline_;
329 
330  // ////////////////////////
331  // Private member functions
332 
333  std::ostream& out();
334 
335  void writeChars( const char_type s[], std::streamsize n );
336 
337  void writeFrontMatter();
338 
339  // Not defined and not to be called
341  basic_FancyOStream_buf(const basic_FancyOStream_buf<CharT,Traits>&);
342  basic_FancyOStream_buf<CharT,Traits> operator=(
343  const basic_FancyOStream_buf<CharT,Traits>&
344  );
345 
346 };
347 
348 
380 template <typename CharT, typename Traits = std::char_traits<CharT> >
381 class basic_FancyOStream : public std::basic_ostream<CharT, Traits>
382 {
383 public:
384 
386 
387 
389  typedef CharT char_type;
391  typedef Traits traits_type;
393  typedef typename traits_type::int_type int_type;
395  typedef typename traits_type::pos_type pos_type;
397  typedef typename traits_type::off_type off_type;
403  typedef std::basic_ostream<char_type, traits_type> ostream_t;
404 
406 
408 
409 
439  explicit
441  const RCP< std::basic_ostream<char_type,traits_type> > &oStream
442  ,const std::basic_string<char_type,traits_type> &tabIndentStr = " "
443  ,const int startingTab = 0
444  ,const bool showLinePrefix = false
445  ,const int maxLenLinePrefix = 10
446  ,const bool showTabCount = false
447  ,const bool showProcRank = false
448  );
449 
453  void initialize(
454  const RCP< std::basic_ostream<char_type,traits_type> > &oStream
455  ,const std::basic_string<char_type,traits_type> &tabIndentStr = " "
456  ,const int startingTab = 0
457  ,const bool showLinePrefix = false
458  ,const int maxLenLinePrefix = 10
459  ,const bool showTabCount = false
460  ,const bool showProcRank = false
461  );
462 
465 
468  const std::basic_string<char_type,traits_type> &tabIndentStr
469  );
470 
472  const std::basic_string<char_type,traits_type>& getTabIndentStr() const;
473 
488  basic_FancyOStream& setShowAllFrontMatter(const bool showAllFrontMatter);
489 
491  basic_FancyOStream& setShowLinePrefix(const bool showLinePrefix);
492 
494  basic_FancyOStream& setMaxLenLinePrefix(const int maxLenLinePrefix);
495 
497  basic_FancyOStream& setShowTabCount(const bool showTabCount);
498 
500  basic_FancyOStream& setShowProcRank(const bool showProcRank);
501 
508  basic_FancyOStream& setProcRankAndSize( const int procRank, const int numProcs );
509 
524  basic_FancyOStream& setOutputToRootOnly( const int rootRank );
525 
527  int getOutputToRootOnly() const;
528 
531 
533 
535 
536 
545  void pushTab(const int tabs = 1);
546 
548  int getNumCurrTabs() const;
549 
557  void popTab();
558 
560  void pushLinePrefix(const std::basic_string<char_type,traits_type> &linePrefix);
561 
563  void popLinePrefix();
564 
566  const std::basic_string<char_type,traits_type>& getTopLinePrefix() const;
567 
569  void pushDisableTabbing();
570 
572  void popDisableTabbing();
573 
575 
576 private:
577 
578  streambuf_t streambuf_;
579 
580  // Not defined and not to be called
584 
585 };
586 
587 
595 inline
598  const RCP< std::basic_ostream<char> >& oStream,
599  const std::basic_string<char>& tabIndentStr = " ",
600  const int startingTab = 0,
601  const bool showLinePrefix = false,
602  const int maxLenLinePrefix = 10,
603  const bool showTabCount = false,
604  const bool showProcRank = false
605  )
606 {
607  if (nonnull(oStream)) {
608  return rcp(
610  oStream,tabIndentStr,startingTab,showLinePrefix,
611  maxLenLinePrefix,showTabCount,showProcRank
612  )
613  );
614  }
615  return null;
616 }
617 
618 
627 inline
629 getFancyOStream( const RCP<std::basic_ostream<char> > &out )
630 {
631  if (is_null(out))
632  return Teuchos::null;
634  fancyOut = rcp_dynamic_cast<basic_FancyOStream<char> >(out);
635  if(nonnull(fancyOut))
636  return fancyOut;
637  return rcp(new basic_FancyOStream<char>(out));
638 }
639 
640 
652 template <typename CharT, typename Traits = std::char_traits<CharT> >
654 {
655 public:
656 
658  static const int DISABLE_TABBING = -99999; // This magic number should be just fine!
661  const RCP<basic_FancyOStream<CharT,Traits> > &fancyOStream
662  ,const int tabs = 1
663  ,const std::basic_string<CharT,Traits> linePrefix = ""
664  )
665  :fancyOStream_(fancyOStream)
666  ,tabs_(tabs)
667  ,linePrefix_(linePrefix)
668  {
669  updateState();
670  }
673  const RCP<std::basic_ostream<CharT,Traits> > &oStream
674  ,const int tabs = 1
675  ,const std::basic_string<CharT,Traits> linePrefix = ""
676  )
677  :fancyOStream_(getFancyOStream(oStream))
678  ,tabs_(tabs)
679  ,linePrefix_(linePrefix)
680  {
681  updateState();
682  }
686  ,const int tabs = 1
687  ,const std::basic_string<CharT,Traits> linePrefix = ""
688  )
689  :fancyOStream_(rcp(&fancyOStream,false))
690  ,tabs_(tabs)
691  ,linePrefix_(linePrefix)
692  {
693  updateState();
694  }
697  std::basic_ostream<CharT,Traits> &oStream
698  ,const int tabs = 1
699  ,const std::basic_string<CharT,Traits> linePrefix = ""
700  )
701  :fancyOStream_(getFancyOStream(rcp(&oStream, false)))
702  ,tabs_(tabs)
703  ,linePrefix_(linePrefix)
704  {
705  updateState();
706  }
708  basic_OSTab( const basic_OSTab &osTab )
709  :fancyOStream_(osTab.fancyOStream_)
710  ,tabs_(osTab.tabs_)
711  {
712  updateState();
713  }
716  {
717  if(fancyOStream_.get()) {
718  if(tabs_==DISABLE_TABBING)
719  fancyOStream_->popDisableTabbing();
720  else
721  fancyOStream_->popTab();
722  if(linePrefix_.length()) fancyOStream_->popLinePrefix();
723  }
724  }
727  {
728  fancyOStream_ = osTab.fancyOStream_;
729  tabs_ = osTab.tabs_;
730  updateState();
731  return *this;
732  }
734  basic_OSTab<CharT,Traits>& incrTab(const int tabs = 1)
735  {
736  tabs_ += tabs;
737  if(fancyOStream_.get()) {
738  fancyOStream_->popTab();
739  fancyOStream_->pushTab(tabs_);
740  }
741  return *this;
742  }
745  {
746  return *fancyOStream_;
747  }
750  {
751  return fancyOStream_.get();
752  }
753 
754 private:
755 
756  RCP<basic_FancyOStream<CharT,Traits> > fancyOStream_;
757  int tabs_;
758  std::basic_string<CharT,Traits> linePrefix_;
759 
760  void updateState()
761  {
762  if(fancyOStream_.get()) {
763  if(tabs_==DISABLE_TABBING)
764  fancyOStream_->pushDisableTabbing();
765  else
766  fancyOStream_->pushTab(tabs_);
767  if(linePrefix_.length()) fancyOStream_->pushLinePrefix(linePrefix_);
768  }
769  }
770 
771 };
772 
773 
789 template <typename CharT, typename Traits>
790 RCP<basic_FancyOStream<CharT,Traits> >
793  const int tabs = 1,
794  const std::basic_string<CharT,Traits> linePrefix = ""
795  )
796 {
797  if(out.get()==NULL)
798  return Teuchos::null;
799  RCP<basic_FancyOStream<CharT,Traits> > fancyOut = out;
800  set_extra_data(
801  rcp(new basic_OSTab<CharT,Traits>(out,tabs,linePrefix)),
802  "OSTab",
803  inOutArg(fancyOut),
804  PRE_DESTROY,
805  false
806  );
807  return fancyOut;
808 }
809 
810 
826 template <typename CharT, typename Traits>
829  const RCP<std::basic_ostream<CharT,Traits> > &out
830  ,const int tabs = 1
831  ,const std::basic_string<CharT,Traits> linePrefix = ""
832  )
833 {
834  return tab(getFancyOStream(out),tabs,linePrefix);
835 }
836 
837 
838 // ///////////////////////////////
839 // Typedefs
840 
841 
846 
847 
852 
853 
857 #define TEUCHOS_OSTAB ::Teuchos::OSTab __localThisTab = this->getOSTab()
858 
862 #define TEUCHOS_OSTAB_DIFF( DIFF ) ::Teuchos::OSTab DIFF ## __localThisTab = this->getOSTab()
863 
864 
865 // ////////////////////////////////
866 // Defintions
867 
868 
869 //
870 // basic_FancyOStream_buf
871 //
872 
873 
874 template<typename CharT, typename Traits>
876  const RCP<std::basic_ostream<char_type,traits_type> > &oStream
877  ,const std::basic_string<char_type,traits_type> &tabIndentStr
878  ,const int startingTab
879  ,const bool showLinePrefix
880  ,const int maxLenLinePrefix
881  ,const bool showTabCount
882  ,const bool showProcRank
883  )
884 {
885  this->initialize(oStream,tabIndentStr,startingTab,showLinePrefix,
886  maxLenLinePrefix,showTabCount,showProcRank);
887 }
888 
889 
890 template<typename CharT, typename Traits>
892  const RCP<std::basic_ostream<char_type,traits_type> > &oStream
893  ,const std::basic_string<char_type,traits_type> &tabIndentStr
894  ,const int startingTab
895  ,const bool showLinePrefix
896  ,const int maxLenLinePrefix
897  ,const bool showTabCount
898  ,const bool showProcRank
899  )
900 {
901  oStreamSet_ = oStream;
902  oStream_ = oStream;
903  tabIndentStr_ = tabIndentStr;
904  showLinePrefix_ = showLinePrefix;
905  maxLenLinePrefix_ = maxLenLinePrefix;
906  showTabCount_ = showTabCount;
907  showProcRank_ = showProcRank;
908  rootRank_ = -1;
909  procRank_ = GlobalMPISession::getRank();
910  numProcs_ = GlobalMPISession::getNProc();
911  rankPrintWidth_ = int(std::log10(float(numProcs_)))+1;
912  tabIndent_ = startingTab;
913  tabIndentStack_.clear();
914  linePrefixStack_.clear();
915  wroteNewline_ = true;
916  enableTabbingStack_ = 0;
917 }
918 
919 
920 template<typename CharT, typename Traits>
923 {
924  return oStreamSet_;
925 }
926 
927 
928 template<typename CharT, typename Traits>
930  const std::basic_string<char_type,traits_type> &tabIndentStr
931  )
932 {
933  tabIndentStr_ = tabIndentStr;
934 }
935 
936 
937 template<typename CharT, typename Traits>
938 const std::basic_string<CharT,Traits>&
940 {
941  return tabIndentStr_;
942 }
943 
944 
945 template<typename CharT, typename Traits>
947 {
948  showLinePrefix_ = showLinePrefix;
949 }
950 
951 
952 template<typename CharT, typename Traits>
954 {
955  return showLinePrefix_;
956 }
957 
958 
959 template<typename CharT, typename Traits>
961 {
962  TEUCHOS_TEST_FOR_EXCEPT( !(maxLenLinePrefix>=5) );
963  maxLenLinePrefix_ = maxLenLinePrefix;
964 }
965 
966 
967 template<typename CharT, typename Traits>
969 {
970  return maxLenLinePrefix_;
971 }
972 
973 
974 template<typename CharT, typename Traits>
976 {
977  showTabCount_ = showTabCount;
978 }
979 
980 
981 template<typename CharT, typename Traits>
983 {
984  return showTabCount_;
985 }
986 
987 
988 template<typename CharT, typename Traits>
990 {
991  showProcRank_ = showProcRank;
992 }
993 
994 
995 template<typename CharT, typename Traits>
997 {
998  return showProcRank_;
999 }
1000 
1001 
1002 template<typename CharT, typename Traits>
1004  const int procRank, const int numProcs
1005  )
1006 {
1007  procRank_ = procRank;
1008  numProcs_ = numProcs;
1009 }
1010 
1011 
1012 template<typename CharT, typename Traits>
1014 {
1015  return procRank_;
1016 }
1017 
1018 
1019 template<typename CharT, typename Traits>
1021 {
1022  return numProcs_;
1023 }
1024 
1025 
1026 template<typename CharT, typename Traits>
1028  const int rootRank
1029  )
1030 {
1031  rootRank_ = rootRank;
1032  if(rootRank >= 0) {
1033  if(rootRank == procRank_)
1034  oStream_ = oStreamSet_;
1035  else
1036  oStream_ = rcp(new oblackholestream());
1037  // Only processor is being output to so there is no need for line
1038  // batching!
1039  lineOut_ = null;
1040  }
1041  else {
1042  oStream_ = oStreamSet_;
1043  // Output is being sent to all processors so we need line batching to
1044  // insure that each line will be printed all together!
1045  lineOut_ = rcp(new std::ostringstream());
1046  }
1047 }
1048 
1049 
1050 template<typename CharT, typename Traits>
1052 {
1053  return rootRank_;
1054 }
1055 
1056 
1057 template<typename CharT, typename Traits>
1059 {
1060  if( tabIndent_ + tabs < 0 ) {
1061  tabIndentStack_.push_back(-tabIndent_);
1062  tabIndent_ = 0;
1063  }
1064  else {
1065  tabIndentStack_.push_back(tabs);
1066  tabIndent_ += tabs;
1067  }
1068 }
1069 
1070 
1071 template<typename CharT, typename Traits>
1073 {
1074  return tabIndent_;
1075 }
1076 
1077 
1078 template<typename CharT, typename Traits>
1080 {
1081  tabIndent_ -= tabIndentStack_.back();
1082  tabIndentStack_.pop_back();
1083 }
1084 
1085 
1086 template<typename CharT, typename Traits>
1088  const std::basic_string<char_type,traits_type> &linePrefix
1089  )
1090 {
1091  linePrefixStack_.push_back(linePrefix);
1092 }
1093 
1094 
1095 template<typename CharT, typename Traits>
1097 {
1098  linePrefixStack_.pop_back();
1099 }
1100 
1101 
1102 template<typename CharT, typename Traits>
1103 const std::basic_string<CharT,Traits>&
1105 {
1106  return linePrefixStack_.back();
1107 }
1108 
1109 
1110 template<typename CharT, typename Traits>
1112 {
1113  ++enableTabbingStack_;
1114 }
1115 
1116 
1117 template<typename CharT, typename Traits>
1119 {
1120  --enableTabbingStack_;
1121 }
1122 
1123 
1124 // protected
1125 
1126 
1127 template<typename CharT, typename Traits>
1129  const char_type* s, std::streamsize n
1130  )
1131 {
1132 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
1133  std::cerr << "\ncalled xsputn()\n";
1134 #endif
1135  writeChars(s,n);
1136  return n;
1137 }
1138 
1139 
1140 template<typename CharT, typename Traits>
1143 {
1144 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
1145  std::cerr << "\ncalled overflow()\n";
1146 #endif
1147  if(c != traits_type::eof()) {
1148  const char_type cc[] = { traits_type::to_char_type(c) };
1149  this->writeChars(cc,1);
1150  }
1151  return traits_type::not_eof(c);
1152  //return std::basic_streambuf<CharT,Traits>::overflow(c);
1153 }
1154 
1155 
1156 // private
1157 
1158 
1159 template<typename CharT, typename Traits>
1161 {
1162  if(lineOut_.get())
1163  return *lineOut_;
1164  return *oStream_;
1165 }
1166 
1167 
1168 template<typename CharT, typename Traits>
1169 void basic_FancyOStream_buf<CharT,Traits>::writeChars(
1170  const char_type s[], std::streamsize n
1171  )
1172 {
1173  if(n == 0) return;
1174  std::streamsize p = 0, first_p = 0;
1175  bool done_outputting = false;
1176  const char_type newline = '\n';
1177  while( !done_outputting ) {
1178  // Find the next newline
1179  for( p = first_p; p < n; ++p ) {
1180  if(s[p] == newline) {
1181  break;
1182  }
1183  }
1184  if(p == n) {
1185  // We did not find a newline at the end!
1186  --p;
1187  done_outputting = true;
1188  }
1189  else if( p == n-1 && s[p] == newline ) {
1190  // The last character in the std::string is a newline
1191  done_outputting = true;
1192  }
1193  // Write the beginning of the line if we need to
1194  if(wroteNewline_) {
1195  writeFrontMatter();
1196  wroteNewline_ = false;
1197  }
1198  // Write up to the newline or the end of the std::string
1199  out().write(s+first_p,p-first_p+1);
1200  if(s[p] == newline) {
1201  wroteNewline_ = true;
1202  if(lineOut_.get()) {
1203  *oStream_ << lineOut_->str() << std::flush;
1204  lineOut_->str("");
1205  }
1206  }
1207  // Update for next search
1208  if(!done_outputting)
1209  first_p = p+1;
1210  }
1211 }
1212 
1213 
1214 template<typename CharT, typename Traits>
1215 void basic_FancyOStream_buf<CharT,Traits>::writeFrontMatter()
1216 {
1217  bool didOutput = false;
1218  std::ostream &o = this->out();
1219  if(showProcRank_) {
1220  o << "p=" << std::right << std::setw(rankPrintWidth_) << procRank_;
1221  didOutput = true;
1222  }
1223  if(showLinePrefix_) {
1224  if(didOutput)
1225  o << ", ";
1226  std::string currLinePrefix = "";
1227  if ( linePrefixStack_.size() )
1228  currLinePrefix = this->getTopLinePrefix();
1229  const int localMaxLenLinePrefix =
1230  TEUCHOS_MAX( as<int>(currLinePrefix.length()), maxLenLinePrefix_ );
1231  o << std::left << std::setw(localMaxLenLinePrefix);
1232  o << currLinePrefix;
1233  didOutput = true;
1234  }
1235  if(showTabCount_) {
1236  if(didOutput)
1237  o << ", ";
1238  o << "tabs=" << std::right << std::setw(2) << tabIndent_;
1239  didOutput = true;
1240  }
1241  // ToDo: Add the Prefix name if asked
1242  // ToDo: Add the processor number if asked
1243  // ToDo: Add the number of indents if asked
1244  if(didOutput) {
1245  o << " |" << tabIndentStr_;
1246  }
1247  if(enableTabbingStack_==0) {
1248  for( int i = 0; i < tabIndent_; ++i )
1249  o << tabIndentStr_;
1250  }
1251 }
1252 
1253 
1254 //
1255 // basic_FancyOStream
1256 //
1257 
1258 
1259 template<typename CharT, typename Traits>
1261  const RCP< std::basic_ostream<char_type,traits_type> > &oStream
1262  ,const std::basic_string<char_type,traits_type> &tabIndentStr
1263  ,const int startingTab
1264  ,const bool showLinePrefix
1265  ,const int maxLenLinePrefix
1266  ,const bool showTabCount
1267  ,const bool showProcRank
1268  )
1269  :ostream_t(NULL),
1270  streambuf_(oStream,tabIndentStr,startingTab,showLinePrefix,
1271  maxLenLinePrefix,showTabCount,showProcRank)
1272 {
1273  this->init(&streambuf_);
1274 }
1275 
1276 
1277 template<typename CharT, typename Traits>
1279  const RCP< std::basic_ostream<char_type,traits_type> > &oStream
1280  ,const std::basic_string<char_type,traits_type> &tabIndentStr
1281  ,const int startingTab
1282  ,const bool showLinePrefix
1283  ,const int maxLenLinePrefix
1284  ,const bool showTabCount
1285  ,const bool showProcRank
1286  )
1287 {
1288  streambuf_.initialize(oStream,tabIndentStr,startingTab,
1289  showLinePrefix,maxLenLinePrefix,showTabCount,showProcRank);
1290  this->init(&streambuf_);
1291 }
1292 
1293 
1294 template<typename CharT, typename Traits>
1297 {
1298  return streambuf_.getOStream();
1299 }
1300 
1301 
1302 template<typename CharT, typename Traits>
1305  const std::basic_string<char_type,traits_type> &tabIndentStr
1306  )
1307 {
1308  streambuf_.setTabIndentStr(tabIndentStr);
1309  return *this;
1310 }
1311 
1312 
1313 template<typename CharT, typename Traits>
1314 const std::basic_string<CharT,Traits>&
1316 {
1317  return streambuf_.getTabIndentStr();
1318 }
1319 
1320 
1321 template<typename CharT, typename Traits>
1324  const bool showAllFrontMatter
1325  )
1326 {
1327  streambuf_.setShowLinePrefix(showAllFrontMatter);
1328  streambuf_.setShowTabCount(showAllFrontMatter);
1329  streambuf_.setShowProcRank(showAllFrontMatter);
1330  return *this;
1331 }
1332 
1333 
1334 template<typename CharT, typename Traits>
1337 {
1338  streambuf_.setShowLinePrefix(showLinePrefix);
1339  return *this;
1340 }
1341 
1342 
1343 template<typename CharT, typename Traits>
1346 {
1347  streambuf_.setMaxLenLinePrefix(maxLenLinePrefix);
1348  return *this;
1349 }
1350 
1351 
1352 template<typename CharT, typename Traits>
1355 {
1356  streambuf_.setShowTabCount(showTabCount);
1357  return *this;
1358 }
1359 
1360 
1361 template<typename CharT, typename Traits>
1364 {
1365  streambuf_.setShowProcRank(showProcRank);
1366  return *this;
1367 }
1368 
1369 
1370 template<typename CharT, typename Traits>
1372 basic_FancyOStream<CharT,Traits>::setProcRankAndSize( const int procRank, const int numProcs )
1373 {
1374  streambuf_.setProcRankAndSize(procRank,numProcs);
1375  return *this;
1376 }
1377 
1378 
1379 template<typename CharT, typename Traits>
1382 {
1383  streambuf_.setOutputToRootOnly(rootRank);
1384  return *this;
1385 }
1386 
1387 
1388 template<typename CharT, typename Traits>
1390 {
1391  return streambuf_.getOutputToRootOnly();
1392 }
1393 
1394 
1395 template<typename CharT, typename Traits>
1397  const basic_FancyOStream<CharT,Traits> &oStream )
1398 {
1399  //streambuf_.setTabIndentStr(oStream.streambuf_.getTabIndentStr());
1400  streambuf_.setShowLinePrefix(oStream.streambuf_.getShowLinePrefix());
1401  streambuf_.setMaxLenLinePrefix(oStream.streambuf_.getMaxLenLinePrefix());
1402  streambuf_.setShowTabCount(oStream.streambuf_.getShowTabCount());
1403  streambuf_.setShowProcRank(oStream.streambuf_.getShowProcRank());
1404  streambuf_.setProcRankAndSize(oStream.streambuf_.getProcRank(),
1405  oStream.streambuf_.getNumProcs());
1406  streambuf_.setOutputToRootOnly(oStream.streambuf_.getOutputToRootOnly());
1407 }
1408 
1409 
1410 template<typename CharT, typename Traits>
1412 {
1413  streambuf_.pushTab(tabs);
1414 }
1415 
1416 
1417 template<typename CharT, typename Traits>
1419 {
1420  return streambuf_.getNumCurrTabs();
1421 }
1422 
1423 
1424 template<typename CharT, typename Traits>
1426 {
1427  streambuf_.popTab();
1428 }
1429 
1430 
1431 template<typename CharT, typename Traits>
1433  const std::basic_string<char_type,traits_type> &linePrefix
1434  )
1435 {
1436  streambuf_.pushLinePrefix(linePrefix);
1437 }
1438 
1439 
1440 template<typename CharT, typename Traits>
1442 {
1443  streambuf_.popLinePrefix();
1444 }
1445 
1446 
1447 template<typename CharT, typename Traits>
1448 const std::basic_string<CharT,Traits>&
1450 {
1451  return streambuf_.getTopLinePrefix();
1452 }
1453 
1454 
1455 template<typename CharT, typename Traits>
1457 {
1458  streambuf_.pushDisableTabbing();
1459 }
1460 
1461 
1462 template<typename CharT, typename Traits>
1464 {
1465  return streambuf_.popDisableTabbing();
1466 }
1467 
1468 
1469 } // namespace Teuchos
1470 
1471 
1472 #endif // TEUCHOS_FANCY_O_STREAM_HPP
int getNumProcs() const
Get the number of processes in the communicator.
basic_FancyOStream & setProcRankAndSize(const int procRank, const int numProcs)
Set the (MPI) process rank and the number of processes in the communicator.
static int getRank()
The rank of the calling process in MPI_COMM_WORLD.
basic_FancyOStream & setShowLinePrefix(const bool showLinePrefix)
int getProcRank() const
Get the rank of the calling (MPI) process.
basic_OSTab< char > OSTab
static int getNProc()
The number of processes in 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
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
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)
Set the stream to print only on the (MPI) process with the given rank.
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 pushTab(const int tabs=1)
Push one or more tabs.
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)
Initialize the output stream.
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
Get the tab indent string.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
void pushTab(const int tabs)
Push one or more tabs.
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 popTab()
Pop the current tab.
void setShowTabCount(const bool showTabCount)
void popTab()
Pop the current tab.
void setShowLinePrefix(const bool showLinePrefix)
basic_FancyOStream & setShowAllFrontMatter(const bool showAllFrontMatter)
Control whether this stream prints &quot;front matter.&quot;.
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
Set the stream to print only on the (MPI) process with the given rank.
basic_FancyOStream_buf< CharT, Traits > streambuf_t
basic_FancyOStream & setTabIndentStr(const std::basic_string< char_type, traits_type > &tabIndentStr)
Set the tab indent string.
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
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()
Get the output stream this object wraps.
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)
Set the (MPI) process rank and the number of processes in the communicator.
#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)