10#ifndef TEUCHOS_FANCY_O_STREAM_HPP
11#define TEUCHOS_FANCY_O_STREAM_HPP
15#include "Teuchos_oblackholestream.hpp"
30template<
typename CharT,
typename Traits>
40 typedef typename traits_type::int_type
int_type;
42 typedef typename traits_type::pos_type
pos_type;
44 typedef typename traits_type::off_type
off_type;
48 const RCP<std::basic_ostream<char_type,traits_type> > &
oStream
49 ,
const std::basic_string<char_type,traits_type> &
tabIndentStr
59 const RCP<std::basic_ostream<char_type,traits_type> > &
oStream
60 ,
const std::basic_string<char_type,traits_type> &
tabIndentStr
164 const std::basic_string<char_type,traits_type> &
linePrefix
190#ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
194 std::cerr <<
"\ncalled imbue()\n";
195 std::basic_streambuf<CharT,Traits>::imbue(
l);
201 std::cerr <<
"\ncalled setbuf()\n";
202 return std::basic_streambuf<CharT,Traits>::setbuf(
s,n);
206 seekoff(
off_type a, ios_base::seekdir b,ios_base::openmode c)
208 std::cerr <<
"\ncalled seekoff()\n";
209 return std::basic_streambuf<CharT,Traits>::seekoff(a,b,c);
213 seekpos(
pos_type a, ios_base::openmode b)
215 std::cerr <<
"\ncalled seekpos()\n";
216 return std::basic_streambuf<CharT,Traits>::seekpos(a,b);
222 std::cerr <<
"\ncalled sync()\n";
223 return std::basic_streambuf<CharT,Traits>::sync();
229 std::cerr <<
"\ncalled showmanyc()\n";
230 return std::basic_streambuf<CharT,Traits>::showmanyc();
236 std::cerr <<
"\ncalled xsgetn()\n";
237 return std::basic_streambuf<CharT,Traits>::xsgetn(s,n);
243 std::cerr <<
"\ncalled underflow()\n";
244 return std::basic_streambuf<CharT,Traits>::underflow();
250 std::cerr <<
"\ncalled uflow()\n";
251 return std::basic_streambuf<CharT,Traits>::uflow();
257 std::cerr <<
"\ncalled pbackfail()\n";
258 return std::basic_streambuf<CharT,Traits>::pbackfail(c);
270 typedef std::basic_string<char_type,traits_type> string_t;
271 typedef std::deque<int> tabIndentStack_t;
272 typedef std::deque<string_t> linePrefixStack_t;
277 RCP<std::basic_ostream<char_type,traits_type> > oStreamSet_;
278 RCP<std::basic_ostream<char_type,traits_type> > oStream_;
279 std::basic_string<char_type,traits_type> tabIndentStr_;
280 bool showLinePrefix_;
281 int maxLenLinePrefix_;
289 RCP<std::ostringstream> lineOut_;
292 tabIndentStack_t tabIndentStack_;
293 linePrefixStack_t linePrefixStack_;
294 int enableTabbingStack_;
303 void writeChars(
const char_type s[], std::streamsize n );
305 void writeFrontMatter();
310 basic_FancyOStream_buf<CharT,Traits> operator=(
311 const basic_FancyOStream_buf<CharT,Traits>&
348template <
typename CharT,
typename Traits = std::
char_traits<CharT> >
371 typedef std::basic_ostream<char_type, traits_type>
ostream_t;
409 const RCP< std::basic_ostream<char_type,traits_type> > &
oStream
410 ,
const std::basic_string<char_type,traits_type> &
tabIndentStr =
" "
422 const RCP< std::basic_ostream<char_type,traits_type> > &
oStream
423 ,
const std::basic_string<char_type,traits_type> &
tabIndentStr =
" "
436 const std::basic_string<char_type,traits_type> &
tabIndentStr
566 const RCP< std::basic_ostream<char> >&
oStream,
600 return Teuchos::null;
620template <
typename CharT,
typename Traits = std::
char_traits<CharT> >
631 ,
const std::basic_string<CharT,Traits>
linePrefix =
""
633 :fancyOStream_(fancyOStream)
641 const RCP<std::basic_ostream<CharT,Traits> > &
oStream
643 ,
const std::basic_string<CharT,Traits>
linePrefix =
""
645 :fancyOStream_(getFancyOStream(
oStream))
655 ,
const std::basic_string<CharT,Traits>
linePrefix =
""
657 :fancyOStream_(
rcp(&fancyOStream,
false))
665 std::basic_ostream<CharT,Traits> &
oStream
667 ,
const std::basic_string<CharT,Traits>
linePrefix =
""
677 :fancyOStream_(
osTab.fancyOStream_)
685 if(fancyOStream_.
get()) {
687 fancyOStream_->popDisableTabbing();
689 fancyOStream_->popTab();
690 if(linePrefix_.length()) fancyOStream_->popLinePrefix();
696 fancyOStream_ =
osTab.fancyOStream_;
705 if(fancyOStream_.
get()) {
706 fancyOStream_->popTab();
707 fancyOStream_->pushTab(tabs_);
714 return *fancyOStream_;
719 return fancyOStream_.
get();
726 std::basic_string<CharT,Traits> linePrefix_;
730 if(fancyOStream_.
get()) {
732 fancyOStream_->pushDisableTabbing();
734 fancyOStream_->pushTab(tabs_);
735 if(linePrefix_.length()) fancyOStream_->pushLinePrefix(linePrefix_);
757template <
typename CharT,
typename Traits>
758RCP<basic_FancyOStream<CharT,Traits> >
762 const std::basic_string<CharT,Traits>
linePrefix =
""
766 return Teuchos::null;
794template <
typename CharT,
typename Traits>
797 const RCP<std::basic_ostream<CharT,Traits> > &out
799 ,
const std::basic_string<CharT,Traits>
linePrefix =
""
825#define TEUCHOS_OSTAB ::Teuchos::OSTab __localThisTab = this->getOSTab()
830#define TEUCHOS_OSTAB_DIFF( DIFF ) ::Teuchos::OSTab DIFF ## __localThisTab = this->getOSTab()
842template<
typename CharT,
typename Traits>
844 const RCP<std::basic_ostream<char_type,traits_type> > &
oStream
845 ,
const std::basic_string<char_type,traits_type> &
tabIndentStr
858template<
typename CharT,
typename Traits>
860 const RCP<std::basic_ostream<char_type,traits_type> > &
oStream
861 ,
const std::basic_string<char_type,traits_type> &
tabIndentStr
879 rankPrintWidth_ =
int(std::log10(
float(numProcs_)))+1;
881 tabIndentStack_.clear();
882 linePrefixStack_.clear();
883 wroteNewline_ =
true;
884 enableTabbingStack_ = 0;
888template<
typename CharT,
typename Traits>
896template<
typename CharT,
typename Traits>
898 const std::basic_string<char_type,traits_type> &
tabIndentStr
905template<
typename CharT,
typename Traits>
906const std::basic_string<CharT,Traits>&
909 return tabIndentStr_;
913template<
typename CharT,
typename Traits>
920template<
typename CharT,
typename Traits>
923 return showLinePrefix_;
927template<
typename CharT,
typename Traits>
935template<
typename CharT,
typename Traits>
938 return maxLenLinePrefix_;
942template<
typename CharT,
typename Traits>
949template<
typename CharT,
typename Traits>
952 return showTabCount_;
956template<
typename CharT,
typename Traits>
963template<
typename CharT,
typename Traits>
966 return showProcRank_;
970template<
typename CharT,
typename Traits>
980template<
typename CharT,
typename Traits>
987template<
typename CharT,
typename Traits>
994template<
typename CharT,
typename Traits>
1002 oStream_ = oStreamSet_;
1010 oStream_ = oStreamSet_;
1013 lineOut_ =
rcp(
new std::ostringstream());
1018template<
typename CharT,
typename Traits>
1025template<
typename CharT,
typename Traits>
1028 if( tabIndent_ +
tabs < 0 ) {
1029 tabIndentStack_.push_back(-tabIndent_);
1033 tabIndentStack_.push_back(
tabs);
1039template<
typename CharT,
typename Traits>
1046template<
typename CharT,
typename Traits>
1049 tabIndent_ -= tabIndentStack_.back();
1050 tabIndentStack_.pop_back();
1054template<
typename CharT,
typename Traits>
1056 const std::basic_string<char_type,traits_type> &
linePrefix
1063template<
typename CharT,
typename Traits>
1066 linePrefixStack_.pop_back();
1070template<
typename CharT,
typename Traits>
1071const std::basic_string<CharT,Traits>&
1074 return linePrefixStack_.back();
1078template<
typename CharT,
typename Traits>
1081 ++enableTabbingStack_;
1085template<
typename CharT,
typename Traits>
1088 --enableTabbingStack_;
1095template<
typename CharT,
typename Traits>
1100#ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
1101 std::cerr <<
"\ncalled xsputn()\n";
1108template<
typename CharT,
typename Traits>
1112#ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
1113 std::cerr <<
"\ncalled overflow()\n";
1115 if(
c != traits_type::eof()) {
1116 const char_type cc[] = { traits_type::to_char_type(
c) };
1117 this->writeChars(
cc,1);
1119 return traits_type::not_eof(
c);
1127template<
typename CharT,
typename Traits>
1136template<
typename CharT,
typename Traits>
1137void basic_FancyOStream_buf<CharT,Traits>::writeChars(
1138 const char_type s[], std::streamsize n
1142 std::streamsize p = 0, first_p = 0;
1143 bool done_outputting =
false;
1144 const char_type newline =
'\n';
1145 while( !done_outputting ) {
1147 for( p = first_p; p < n; ++p ) {
1148 if(s[p] == newline) {
1155 done_outputting =
true;
1157 else if( p == n-1 && s[p] == newline ) {
1159 done_outputting =
true;
1164 wroteNewline_ =
false;
1167 out().write(s+first_p,p-first_p+1);
1168 if(s[p] == newline) {
1169 wroteNewline_ =
true;
1170 if(lineOut_.get()) {
1171 *oStream_ << lineOut_->str() << std::flush;
1176 if(!done_outputting)
1182template<
typename CharT,
typename Traits>
1183void basic_FancyOStream_buf<CharT,Traits>::writeFrontMatter()
1185 bool didOutput =
false;
1186 std::ostream &o = this->out();
1188 o <<
"p=" << std::right << std::setw(rankPrintWidth_) << procRank_;
1191 if(showLinePrefix_) {
1194 std::string currLinePrefix =
"";
1195 if ( linePrefixStack_.size() )
1196 currLinePrefix = this->getTopLinePrefix();
1197 const int localMaxLenLinePrefix =
1198 TEUCHOS_MAX( as<int>(currLinePrefix.length()), maxLenLinePrefix_ );
1199 o << std::left << std::setw(localMaxLenLinePrefix);
1200 o << currLinePrefix;
1206 o <<
"tabs=" << std::right << std::setw(2) << tabIndent_;
1213 o <<
" |" << tabIndentStr_;
1215 if(enableTabbingStack_==0) {
1216 for(
int i = 0; i < tabIndent_; ++i )
1227template<
typename CharT,
typename Traits>
1229 const RCP< std::basic_ostream<char_type,traits_type> > &
oStream
1230 ,
const std::basic_string<char_type,traits_type> &
tabIndentStr
1241 this->init(&streambuf_);
1245template<
typename CharT,
typename Traits>
1247 const RCP< std::basic_ostream<char_type,traits_type> > &
oStream
1248 ,
const std::basic_string<char_type,traits_type> &
tabIndentStr
1258 this->init(&streambuf_);
1262template<
typename CharT,
typename Traits>
1266 return streambuf_.getOStream();
1270template<
typename CharT,
typename Traits>
1273 const std::basic_string<char_type,traits_type> &
tabIndentStr
1281template<
typename CharT,
typename Traits>
1282const std::basic_string<CharT,Traits>&
1285 return streambuf_.getTabIndentStr();
1289template<
typename CharT,
typename Traits>
1302template<
typename CharT,
typename Traits>
1311template<
typename CharT,
typename Traits>
1320template<
typename CharT,
typename Traits>
1329template<
typename CharT,
typename Traits>
1338template<
typename CharT,
typename Traits>
1347template<
typename CharT,
typename Traits>
1356template<
typename CharT,
typename Traits>
1359 return streambuf_.getOutputToRootOnly();
1363template<
typename CharT,
typename Traits>
1368 streambuf_.setShowLinePrefix(
oStream.streambuf_.getShowLinePrefix());
1369 streambuf_.setMaxLenLinePrefix(
oStream.streambuf_.getMaxLenLinePrefix());
1370 streambuf_.setShowTabCount(
oStream.streambuf_.getShowTabCount());
1371 streambuf_.setShowProcRank(
oStream.streambuf_.getShowProcRank());
1372 streambuf_.setProcRankAndSize(
oStream.streambuf_.getProcRank(),
1373 oStream.streambuf_.getNumProcs());
1374 streambuf_.setOutputToRootOnly(
oStream.streambuf_.getOutputToRootOnly());
1378template<
typename CharT,
typename Traits>
1381 streambuf_.pushTab(
tabs);
1385template<
typename CharT,
typename Traits>
1388 return streambuf_.getNumCurrTabs();
1392template<
typename CharT,
typename Traits>
1395 streambuf_.popTab();
1399template<
typename CharT,
typename Traits>
1401 const std::basic_string<char_type,traits_type> &
linePrefix
1408template<
typename CharT,
typename Traits>
1411 streambuf_.popLinePrefix();
1415template<
typename CharT,
typename Traits>
1416const std::basic_string<CharT,Traits>&
1419 return streambuf_.getTopLinePrefix();
1423template<
typename CharT,
typename Traits>
1426 streambuf_.pushDisableTabbing();
1430template<
typename CharT,
typename Traits>
1433 return streambuf_.popDisableTabbing();
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Reference-counted pointer class and non-member templated function implementations.
Definition of Teuchos::as, for conversions between types.
static int getRank()
The rank of the calling process in MPI_COMM_WORLD.
static int getNProc()
The number of processes in MPI_COMM_WORLD.
Smart reference counting pointer class for automatic garbage collection.
RCP(ENull null_arg=null)
Initialize RCP<T> to NULL.
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object.
T * get() const
Get the raw C++ pointer to the underlying object.
Stream buffering class that performs the magic of indenting data sent to an std::ostream object.
int_type overflow(int_type c)
int getNumProcs() const
Get the number of processes in the communicator.
void popTab()
Pop the current tab.
void setShowProcRank(const bool showProcRank)
void setOutputToRootOnly(const int rootRank)
Set the stream to print only on the (MPI) process with the given rank.
int getMaxLenLinePrefix() const
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
const std::basic_string< char_type, traits_type > & getTabIndentStr() const
void setTabIndentStr(const std::basic_string< char_type, traits_type > &tabIndentStr)
void setShowTabCount(const bool showTabCount)
int getNumCurrTabs() const
void pushLinePrefix(const std::basic_string< char_type, traits_type > &linePrefix)
void setProcRankAndSize(const int procRank, const int numProcs)
Set the (MPI) process rank and the number of processes in the communicator.
bool getShowProcRank() const
traits_type::pos_type pos_type
void pushDisableTabbing()
int getProcRank() const
Get the rank of the calling (MPI) process.
void setMaxLenLinePrefix(const int maxLenLinePrefix)
int getOutputToRootOnly() const
void pushTab(const int tabs)
Push one or more tabs.
bool getShowLinePrefix() const
traits_type::off_type off_type
traits_type::int_type int_type
const std::basic_string< char_type, traits_type > & getTopLinePrefix() const
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 setShowLinePrefix(const bool showLinePrefix)
bool getShowTabCount() const
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)
std::streamsize xsputn(const char_type *s, std::streamsize n)
std::ostream subclass that performs the magic of indenting data sent to an std::ostream object among ...
traits_type::off_type off_type
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.
basic_FancyOStream & setTabIndentStr(const std::basic_string< char_type, traits_type > &tabIndentStr)
Set the tab indent string.
basic_FancyOStream & setShowAllFrontMatter(const bool showAllFrontMatter)
Control whether this stream prints "front matter.".
basic_FancyOStream & setProcRankAndSize(const int procRank, const int numProcs)
Set the (MPI) process rank and the number of processes in the communicator.
void popTab()
Pop the current tab.
traits_type::pos_type pos_type
int getNumCurrTabs() const
const std::basic_string< char_type, traits_type > & getTabIndentStr() const
Get the tab indent string.
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
Set the stream to print only on the (MPI) process with the given rank.
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...
basic_FancyOStream & setMaxLenLinePrefix(const int maxLenLinePrefix)
std::basic_ostream< char_type, traits_type > ostream_t
traits_type::int_type int_type
void pushLinePrefix(const std::basic_string< char_type, traits_type > &linePrefix)
basic_FancyOStream & setShowLinePrefix(const bool showLinePrefix)
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
Get the output stream this object wraps.
basic_FancyOStream(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)
Constructor.
const std::basic_string< char_type, traits_type > & getTopLinePrefix() const
basic_FancyOStream_buf< CharT, Traits > streambuf_t
void copyAllOutputOptions(const basic_FancyOStream< CharT, Traits > &oStream)
int getOutputToRootOnly() const
basic_FancyOStream & setShowTabCount(const bool showTabCount)
void pushDisableTabbing()
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 & setShowProcRank(const bool showProcRank)
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...
void pushTab(const int tabs=1)
Push one or more tabs.
Tabbing class for helping to create formated, indented output for a basic_FancyOStream object.
basic_FancyOStream< CharT, Traits > * get() const
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.
static const int DISABLE_TABBING
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.
basic_OSTab< CharT, Traits > & operator=(const basic_OSTab &osTab)
basic_OSTab(const RCP< basic_FancyOStream< CharT, Traits > > &fancyOStream, const int tabs=1, const std::basic_string< CharT, Traits > linePrefix="")
basic_OSTab< CharT, Traits > & incrTab(const int tabs=1)
basic_FancyOStream< CharT, Traits > & o() const
basic_OSTab(const RCP< std::basic_ostream< CharT, Traits > > &oStream, const int tabs=1, const std::basic_string< CharT, Traits > linePrefix="")
basic_OSTab(const basic_OSTab &osTab)
#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.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
basic_FancyOStream< char > FancyOStream
basic_OSTab< char > OSTab
basic_oblackholestream< char, std::char_traits< char > > oblackholestream
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.