Teuchos - Trilinos Tools Package  Version of the Day
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 
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 
220  int_type overflow(int_type c);
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 
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
344  );
345 
346 };
347 
348 
370 template <typename CharT, typename Traits = std::char_traits<CharT> >
371 class basic_FancyOStream : public std::basic_ostream<CharT, Traits>
372 {
373 public:
374 
376 
377 
379  typedef CharT char_type;
381  typedef Traits traits_type;
383  typedef typename traits_type::int_type int_type;
385  typedef typename traits_type::pos_type pos_type;
387  typedef typename traits_type::off_type off_type;
393  typedef std::basic_ostream<char_type, traits_type> ostream_t;
394 
396 
398 
399 
429  explicit
431  const RCP< std::basic_ostream<char_type,traits_type> > &oStream
432  ,const std::basic_string<char_type,traits_type> &tabIndentStr = " "
433  ,const int startingTab = 0
434  ,const bool showLinePrefix = false
435  ,const int maxLenLinePrefix = 10
436  ,const bool showTabCount = false
437  ,const bool showProcRank = false
438  );
439 
443  void initialize(
444  const RCP< std::basic_ostream<char_type,traits_type> > &oStream
445  ,const std::basic_string<char_type,traits_type> &tabIndentStr = " "
446  ,const int startingTab = 0
447  ,const bool showLinePrefix = false
448  ,const int maxLenLinePrefix = 10
449  ,const bool showTabCount = false
450  ,const bool showProcRank = false
451  );
452 
455 
458  const std::basic_string<char_type,traits_type> &tabIndentStr
459  );
460 
462  const std::basic_string<char_type,traits_type>& getTabIndentStr() const;
463 
478  basic_FancyOStream& setShowAllFrontMatter(const bool showAllFrontMatter);
479 
481  basic_FancyOStream& setShowLinePrefix(const bool showLinePrefix);
482 
484  basic_FancyOStream& setMaxLenLinePrefix(const int maxLenLinePrefix);
485 
487  basic_FancyOStream& setShowTabCount(const bool showTabCount);
488 
490  basic_FancyOStream& setShowProcRank(const bool showProcRank);
491 
498  basic_FancyOStream& setProcRankAndSize( const int procRank, const int numProcs );
499 
514  basic_FancyOStream& setOutputToRootOnly( const int rootRank );
515 
517  int getOutputToRootOnly() const;
518 
520  void copyAllOutputOptions(const basic_FancyOStream<CharT,Traits> &oStream);
521 
523 
525 
526 
535  void pushTab(const int tabs = 1);
536 
538  int getNumCurrTabs() const;
539 
547  void popTab();
548 
550  void pushLinePrefix(const std::basic_string<char_type,traits_type> &linePrefix);
551 
553  void popLinePrefix();
554 
556  const std::basic_string<char_type,traits_type>& getTopLinePrefix() const;
557 
559  void pushDisableTabbing();
560 
562  void popDisableTabbing();
563 
565 
566 private:
567 
568  streambuf_t streambuf_;
569 
570  // Not defined and not to be called
574 
575 };
576 
577 
585 inline
588  const RCP< std::basic_ostream<char> >& oStream,
589  const std::basic_string<char>& tabIndentStr = " ",
590  const int startingTab = 0,
591  const bool showLinePrefix = false,
592  const int maxLenLinePrefix = 10,
593  const bool showTabCount = false,
594  const bool showProcRank = false
595  )
596 {
597  if (nonnull(oStream)) {
598  return rcp(
600  oStream,tabIndentStr,startingTab,showLinePrefix,
601  maxLenLinePrefix,showTabCount,showProcRank
602  )
603  );
604  }
605  return null;
606 }
607 
608 
617 inline
619 getFancyOStream( const RCP<std::basic_ostream<char> > &out )
620 {
621  if (is_null(out))
622  return Teuchos::null;
624  fancyOut = rcp_dynamic_cast<basic_FancyOStream<char> >(out);
625  if(nonnull(fancyOut))
626  return fancyOut;
627  return rcp(new basic_FancyOStream<char>(out));
628 }
629 
630 
642 template <typename CharT, typename Traits = std::char_traits<CharT> >
644 {
645 public:
646 
648  static const int DISABLE_TABBING = -99999; // This magic number should be just fine!
651  const RCP<basic_FancyOStream<CharT,Traits> > &fancyOStream
652  ,const int tabs = 1
653  ,const std::basic_string<CharT,Traits> linePrefix = ""
654  )
655  :fancyOStream_(fancyOStream)
656  ,tabs_(tabs)
657  ,linePrefix_(linePrefix)
658  {
659  updateState();
660  }
663  const RCP<std::basic_ostream<CharT,Traits> > &oStream
664  ,const int tabs = 1
665  ,const std::basic_string<CharT,Traits> linePrefix = ""
666  )
667  :fancyOStream_(getFancyOStream(oStream))
668  ,tabs_(tabs)
669  ,linePrefix_(linePrefix)
670  {
671  updateState();
672  }
676  ,const int tabs = 1
677  ,const std::basic_string<CharT,Traits> linePrefix = ""
678  )
679  :fancyOStream_(rcp(&fancyOStream,false))
680  ,tabs_(tabs)
681  ,linePrefix_(linePrefix)
682  {
683  updateState();
684  }
687  std::basic_ostream<CharT,Traits> &oStream
688  ,const int tabs = 1
689  ,const std::basic_string<CharT,Traits> linePrefix = ""
690  )
691  :fancyOStream_(getFancyOStream(rcp(&oStream, false)))
692  ,tabs_(tabs)
693  ,linePrefix_(linePrefix)
694  {
695  updateState();
696  }
698  basic_OSTab( const basic_OSTab &osTab )
699  :fancyOStream_(osTab.fancyOStream_)
700  ,tabs_(osTab.tabs_)
701  {
702  updateState();
703  }
706  {
707  if(fancyOStream_.get()) {
708  if(tabs_==DISABLE_TABBING)
709  fancyOStream_->popDisableTabbing();
710  else
711  fancyOStream_->popTab();
712  if(linePrefix_.length()) fancyOStream_->popLinePrefix();
713  }
714  }
717  {
718  fancyOStream_ = osTab.fancyOStream_;
719  tabs_ = osTab.tabs_;
720  updateState();
721  return *this;
722  }
724  basic_OSTab<CharT,Traits>& incrTab(const int tabs = 1)
725  {
726  tabs_ += tabs;
727  if(fancyOStream_.get()) {
728  fancyOStream_->popTab();
729  fancyOStream_->pushTab(tabs_);
730  }
731  return *this;
732  }
735  {
736  return *fancyOStream_;
737  }
740  {
741  return fancyOStream_.get();
742  }
743 
744 private:
745 
746  RCP<basic_FancyOStream<CharT,Traits> > fancyOStream_;
747  int tabs_;
748  std::basic_string<CharT,Traits> linePrefix_;
749 
750  void updateState()
751  {
752  if(fancyOStream_.get()) {
753  if(tabs_==DISABLE_TABBING)
754  fancyOStream_->pushDisableTabbing();
755  else
756  fancyOStream_->pushTab(tabs_);
757  if(linePrefix_.length()) fancyOStream_->pushLinePrefix(linePrefix_);
758  }
759  }
760 
761 };
762 
763 
779 template <typename CharT, typename Traits>
783  const int tabs = 1,
784  const std::basic_string<CharT,Traits> linePrefix = ""
785  )
786 {
787  if(out.get()==NULL)
788  return Teuchos::null;
789  RCP<basic_FancyOStream<CharT,Traits> > fancyOut = out;
790  set_extra_data(
791  rcp(new basic_OSTab<CharT,Traits>(out,tabs,linePrefix)),
792  "OSTab",
793  inOutArg(fancyOut),
794  PRE_DESTROY,
795  false
796  );
797  return fancyOut;
798 }
799 
800 
816 template <typename CharT, typename Traits>
819  const RCP<std::basic_ostream<CharT,Traits> > &out
820  ,const int tabs = 1
821  ,const std::basic_string<CharT,Traits> linePrefix = ""
822  )
823 {
824  return tab(getFancyOStream(out),tabs,linePrefix);
825 }
826 
827 
828 // ///////////////////////////////
829 // Typedefs
830 
831 
836 
837 
842 
843 
847 #define TEUCHOS_OSTAB ::Teuchos::OSTab __localThisTab = this->getOSTab()
848 
852 #define TEUCHOS_OSTAB_DIFF( DIFF ) ::Teuchos::OSTab DIFF ## __localThisTab = this->getOSTab()
853 
854 
855 // ////////////////////////////////
856 // Defintions
857 
858 
859 //
860 // basic_FancyOStream_buf
861 //
862 
863 
864 template<typename CharT, typename Traits>
866  const RCP<std::basic_ostream<char_type,traits_type> > &oStream
867  ,const std::basic_string<char_type,traits_type> &tabIndentStr
868  ,const int startingTab
869  ,const bool showLinePrefix
870  ,const int maxLenLinePrefix
871  ,const bool showTabCount
872  ,const bool showProcRank
873  )
874 {
875  this->initialize(oStream,tabIndentStr,startingTab,showLinePrefix,
876  maxLenLinePrefix,showTabCount,showProcRank);
877 }
878 
879 
880 template<typename CharT, typename Traits>
882  const RCP<std::basic_ostream<char_type,traits_type> > &oStream
883  ,const std::basic_string<char_type,traits_type> &tabIndentStr
884  ,const int startingTab
885  ,const bool showLinePrefix
886  ,const int maxLenLinePrefix
887  ,const bool showTabCount
888  ,const bool showProcRank
889  )
890 {
891  oStreamSet_ = oStream;
892  oStream_ = oStream;
893  tabIndentStr_ = tabIndentStr;
894  showLinePrefix_ = showLinePrefix;
895  maxLenLinePrefix_ = maxLenLinePrefix;
896  showTabCount_ = showTabCount;
897  showProcRank_ = showProcRank;
898  rootRank_ = -1;
899  procRank_ = GlobalMPISession::getRank();
900  numProcs_ = GlobalMPISession::getNProc();
901  rankPrintWidth_ = int(std::log10(float(numProcs_)))+1;
902  tabIndent_ = startingTab;
903  tabIndentStack_.clear();
904  linePrefixStack_.clear();
905  wroteNewline_ = true;
906  enableTabbingStack_ = 0;
907 }
908 
909 
910 template<typename CharT, typename Traits>
913 {
914  return oStreamSet_;
915 }
916 
917 
918 template<typename CharT, typename Traits>
920  const std::basic_string<char_type,traits_type> &tabIndentStr
921  )
922 {
923  tabIndentStr_ = tabIndentStr;
924 }
925 
926 
927 template<typename CharT, typename Traits>
928 const std::basic_string<CharT,Traits>&
930 {
931  return tabIndentStr_;
932 }
933 
934 
935 template<typename CharT, typename Traits>
937 {
938  showLinePrefix_ = showLinePrefix;
939 }
940 
941 
942 template<typename CharT, typename Traits>
944 {
945  return showLinePrefix_;
946 }
947 
948 
949 template<typename CharT, typename Traits>
951 {
952  TEUCHOS_TEST_FOR_EXCEPT( !(maxLenLinePrefix>=5) );
953  maxLenLinePrefix_ = maxLenLinePrefix;
954 }
955 
956 
957 template<typename CharT, typename Traits>
959 {
960  return maxLenLinePrefix_;
961 }
962 
963 
964 template<typename CharT, typename Traits>
966 {
967  showTabCount_ = showTabCount;
968 }
969 
970 
971 template<typename CharT, typename Traits>
973 {
974  return showTabCount_;
975 }
976 
977 
978 template<typename CharT, typename Traits>
980 {
981  showProcRank_ = showProcRank;
982 }
983 
984 
985 template<typename CharT, typename Traits>
987 {
988  return showProcRank_;
989 }
990 
991 
992 template<typename CharT, typename Traits>
994  const int procRank, const int numProcs
995  )
996 {
997  procRank_ = procRank;
998  numProcs_ = numProcs;
999 }
1000 
1001 
1002 template<typename CharT, typename Traits>
1004 {
1005  return procRank_;
1006 }
1007 
1008 
1009 template<typename CharT, typename Traits>
1011 {
1012  return numProcs_;
1013 }
1014 
1015 
1016 template<typename CharT, typename Traits>
1018  const int rootRank
1019  )
1020 {
1021  rootRank_ = rootRank;
1022  if(rootRank >= 0) {
1023  if(rootRank == procRank_)
1024  oStream_ = oStreamSet_;
1025  else
1026  oStream_ = rcp(new oblackholestream());
1027  // Only processor is being output to so there is no need for line
1028  // batching!
1029  lineOut_ = null;
1030  }
1031  else {
1032  oStream_ = oStreamSet_;
1033  // Output is being sent to all processors so we need line batching to
1034  // insure that each line will be printed all together!
1035  lineOut_ = rcp(new std::ostringstream());
1036  }
1037 }
1038 
1039 
1040 template<typename CharT, typename Traits>
1042 {
1043  return rootRank_;
1044 }
1045 
1046 
1047 template<typename CharT, typename Traits>
1049 {
1050  if( tabIndent_ + tabs < 0 ) {
1051  tabIndentStack_.push_back(-tabIndent_);
1052  tabIndent_ = 0;
1053  }
1054  else {
1055  tabIndentStack_.push_back(tabs);
1056  tabIndent_ += tabs;
1057  }
1058 }
1059 
1060 
1061 template<typename CharT, typename Traits>
1063 {
1064  return tabIndent_;
1065 }
1066 
1067 
1068 template<typename CharT, typename Traits>
1070 {
1071  tabIndent_ -= tabIndentStack_.back();
1072  tabIndentStack_.pop_back();
1073 }
1074 
1075 
1076 template<typename CharT, typename Traits>
1078  const std::basic_string<char_type,traits_type> &linePrefix
1079  )
1080 {
1081  linePrefixStack_.push_back(linePrefix);
1082 }
1083 
1084 
1085 template<typename CharT, typename Traits>
1087 {
1088  linePrefixStack_.pop_back();
1089 }
1090 
1091 
1092 template<typename CharT, typename Traits>
1093 const std::basic_string<CharT,Traits>&
1095 {
1096  return linePrefixStack_.back();
1097 }
1098 
1099 
1100 template<typename CharT, typename Traits>
1102 {
1103  ++enableTabbingStack_;
1104 }
1105 
1106 
1107 template<typename CharT, typename Traits>
1109 {
1110  --enableTabbingStack_;
1111 }
1112 
1113 
1114 // protected
1115 
1116 
1117 template<typename CharT, typename Traits>
1119  const char_type* s, std::streamsize n
1120  )
1121 {
1122 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
1123  std::cerr << "\ncalled xsputn()\n";
1124 #endif
1125  writeChars(s,n);
1126  return n;
1127 }
1128 
1129 
1130 template<typename CharT, typename Traits>
1133 {
1134 #ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
1135  std::cerr << "\ncalled overflow()\n";
1136 #endif
1137  if(c != traits_type::eof()) {
1138  const char_type cc[] = { traits_type::to_char_type(c) };
1139  this->writeChars(cc,1);
1140  }
1141  return traits_type::not_eof(c);
1142  //return std::basic_streambuf<CharT,Traits>::overflow(c);
1143 }
1144 
1145 
1146 // private
1147 
1148 
1149 template<typename CharT, typename Traits>
1151 {
1152  if(lineOut_.get())
1153  return *lineOut_;
1154  return *oStream_;
1155 }
1156 
1157 
1158 template<typename CharT, typename Traits>
1160  const char_type s[], std::streamsize n
1161  )
1162 {
1163  if(n == 0) return;
1164  std::streamsize p = 0, first_p = 0;
1165  bool done_outputting = false;
1166  const char_type newline = '\n';
1167  while( !done_outputting ) {
1168  // Find the next newline
1169  for( p = first_p; p < n; ++p ) {
1170  if(s[p] == newline) {
1171  break;
1172  }
1173  }
1174  if(p == n) {
1175  // We did not find a newline at the end!
1176  --p;
1177  done_outputting = true;
1178  }
1179  else if( p == n-1 && s[p] == newline ) {
1180  // The last character in the std::string is a newline
1181  done_outputting = true;
1182  }
1183  // Write the beginning of the line if we need to
1184  if(wroteNewline_) {
1185  writeFrontMatter();
1186  wroteNewline_ = false;
1187  }
1188  // Write up to the newline or the end of the std::string
1189  out().write(s+first_p,p-first_p+1);
1190  if(s[p] == newline) {
1191  wroteNewline_ = true;
1192  if(lineOut_.get()) {
1193  *oStream_ << lineOut_->str() << std::flush;
1194  lineOut_->str("");
1195  }
1196  }
1197  // Update for next search
1198  if(!done_outputting)
1199  first_p = p+1;
1200  }
1201 }
1202 
1203 
1204 template<typename CharT, typename Traits>
1206 {
1207  bool didOutput = false;
1208  std::ostream &o = this->out();
1209  if(showProcRank_) {
1210  o << "p=" << std::right << std::setw(rankPrintWidth_) << procRank_;
1211  didOutput = true;
1212  }
1213  if(showLinePrefix_) {
1214  if(didOutput)
1215  o << ", ";
1216  std::string currLinePrefix = "";
1217  if ( linePrefixStack_.size() )
1218  currLinePrefix = this->getTopLinePrefix();
1219  const int localMaxLenLinePrefix =
1220  TEUCHOS_MAX( as<int>(currLinePrefix.length()), maxLenLinePrefix_ );
1221  o << std::left << std::setw(localMaxLenLinePrefix);
1222  o << currLinePrefix;
1223  didOutput = true;
1224  }
1225  if(showTabCount_) {
1226  if(didOutput)
1227  o << ", ";
1228  o << "tabs=" << std::right << std::setw(2) << tabIndent_;
1229  didOutput = true;
1230  }
1231  // ToDo: Add the Prefix name if asked
1232  // ToDo: Add the processor number if asked
1233  // ToDo: Add the number of indents if asked
1234  if(didOutput) {
1235  o << " |" << tabIndentStr_;
1236  }
1237  if(enableTabbingStack_==0) {
1238  for( int i = 0; i < tabIndent_; ++i )
1239  o << tabIndentStr_;
1240  }
1241 }
1242 
1243 
1244 //
1245 // basic_FancyOStream
1246 //
1247 
1248 
1249 template<typename CharT, typename Traits>
1251  const RCP< std::basic_ostream<char_type,traits_type> > &oStream
1252  ,const std::basic_string<char_type,traits_type> &tabIndentStr
1253  ,const int startingTab
1254  ,const bool showLinePrefix
1255  ,const int maxLenLinePrefix
1256  ,const bool showTabCount
1257  ,const bool showProcRank
1258  )
1259  :ostream_t(NULL),
1260  streambuf_(oStream,tabIndentStr,startingTab,showLinePrefix,
1261  maxLenLinePrefix,showTabCount,showProcRank)
1262 {
1263  this->init(&streambuf_);
1264 }
1265 
1266 
1267 template<typename CharT, typename Traits>
1269  const RCP< std::basic_ostream<char_type,traits_type> > &oStream
1270  ,const std::basic_string<char_type,traits_type> &tabIndentStr
1271  ,const int startingTab
1272  ,const bool showLinePrefix
1273  ,const int maxLenLinePrefix
1274  ,const bool showTabCount
1275  ,const bool showProcRank
1276  )
1277 {
1278  streambuf_.initialize(oStream,tabIndentStr,startingTab,
1279  showLinePrefix,maxLenLinePrefix,showTabCount,showProcRank);
1280  this->init(&streambuf_);
1281 }
1282 
1283 
1284 template<typename CharT, typename Traits>
1287 {
1288  return streambuf_.getOStream();
1289 }
1290 
1291 
1292 template<typename CharT, typename Traits>
1295  const std::basic_string<char_type,traits_type> &tabIndentStr
1296  )
1297 {
1298  streambuf_.setTabIndentStr(tabIndentStr);
1299  return *this;
1300 }
1301 
1302 
1303 template<typename CharT, typename Traits>
1304 const std::basic_string<CharT,Traits>&
1306 {
1307  return streambuf_.getTabIndentStr();
1308 }
1309 
1310 
1311 template<typename CharT, typename Traits>
1314  const bool showAllFrontMatter
1315  )
1316 {
1317  streambuf_.setShowLinePrefix(showAllFrontMatter);
1318  streambuf_.setShowTabCount(showAllFrontMatter);
1319  streambuf_.setShowProcRank(showAllFrontMatter);
1320  return *this;
1321 }
1322 
1323 
1324 template<typename CharT, typename Traits>
1327 {
1328  streambuf_.setShowLinePrefix(showLinePrefix);
1329  return *this;
1330 }
1331 
1332 
1333 template<typename CharT, typename Traits>
1336 {
1337  streambuf_.setMaxLenLinePrefix(maxLenLinePrefix);
1338  return *this;
1339 }
1340 
1341 
1342 template<typename CharT, typename Traits>
1345 {
1346  streambuf_.setShowTabCount(showTabCount);
1347  return *this;
1348 }
1349 
1350 
1351 template<typename CharT, typename Traits>
1354 {
1355  streambuf_.setShowProcRank(showProcRank);
1356  return *this;
1357 }
1358 
1359 
1360 template<typename CharT, typename Traits>
1362 basic_FancyOStream<CharT,Traits>::setProcRankAndSize( const int procRank, const int numProcs )
1363 {
1364  streambuf_.setProcRankAndSize(procRank,numProcs);
1365  return *this;
1366 }
1367 
1368 
1369 template<typename CharT, typename Traits>
1372 {
1373  streambuf_.setOutputToRootOnly(rootRank);
1374  return *this;
1375 }
1376 
1377 
1378 template<typename CharT, typename Traits>
1380 {
1381  return streambuf_.getOutputToRootOnly();
1382 }
1383 
1384 
1385 template<typename CharT, typename Traits>
1387  const basic_FancyOStream<CharT,Traits> &oStream )
1388 {
1389  //streambuf_.setTabIndentStr(oStream.streambuf_.getTabIndentStr());
1390  streambuf_.setShowLinePrefix(oStream.streambuf_.getShowLinePrefix());
1391  streambuf_.setMaxLenLinePrefix(oStream.streambuf_.getMaxLenLinePrefix());
1392  streambuf_.setShowTabCount(oStream.streambuf_.getShowTabCount());
1393  streambuf_.setShowProcRank(oStream.streambuf_.getShowProcRank());
1394  streambuf_.setProcRankAndSize(oStream.streambuf_.getProcRank(),
1395  oStream.streambuf_.getNumProcs());
1396  streambuf_.setOutputToRootOnly(oStream.streambuf_.getOutputToRootOnly());
1397 }
1398 
1399 
1400 template<typename CharT, typename Traits>
1402 {
1403  streambuf_.pushTab(tabs);
1404 }
1405 
1406 
1407 template<typename CharT, typename Traits>
1409 {
1410  return streambuf_.getNumCurrTabs();
1411 }
1412 
1413 
1414 template<typename CharT, typename Traits>
1416 {
1417  streambuf_.popTab();
1418 }
1419 
1420 
1421 template<typename CharT, typename Traits>
1423  const std::basic_string<char_type,traits_type> &linePrefix
1424  )
1425 {
1426  streambuf_.pushLinePrefix(linePrefix);
1427 }
1428 
1429 
1430 template<typename CharT, typename Traits>
1432 {
1433  streambuf_.popLinePrefix();
1434 }
1435 
1436 
1437 template<typename CharT, typename Traits>
1438 const std::basic_string<CharT,Traits>&
1440 {
1441  return streambuf_.getTopLinePrefix();
1442 }
1443 
1444 
1445 template<typename CharT, typename Traits>
1447 {
1448  streambuf_.pushDisableTabbing();
1449 }
1450 
1451 
1452 template<typename CharT, typename Traits>
1454 {
1455  return streambuf_.popDisableTabbing();
1456 }
1457 
1458 
1459 } // namespace Teuchos
1460 
1461 
1462 #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)
basic_OSTab< CharT, Traits > & incrTab(const int tabs=1)
T * get() const
Get the raw C++ pointer to the underlying object.
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 "front matter.".
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.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
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.
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)