Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_FancyOStream_decl.hpp
1// @HEADER
2// *****************************************************************************
3// Teuchos: Common Tools Package
4//
5// Copyright 2004 NTESS and the Teuchos contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef TEUCHOS_FANCY_O_STREAM_DECL_HPP
11#define TEUCHOS_FANCY_O_STREAM_DECL_HPP
12
13#include "Teuchos_RCP.hpp"
15#include "Teuchos_oblackholestream.hpp"
16#include "Teuchos_as.hpp"
17#include <deque>
18
19namespace Teuchos {
20
21
30template<typename CharT, typename Traits>
31class basic_FancyOStream_buf : public std::basic_streambuf<CharT,Traits>
32{
33public:
34
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;
45
48 const RCP<std::basic_ostream<char_type,traits_type> > &oStream
49 ,const std::basic_string<char_type,traits_type> &tabIndentStr
50 ,const int startingTab
51 ,const bool showLinePrefix
52 ,const int maxLenLinePrefix
53 ,const bool showTabCount
54 ,const bool showProcRank
55 );
56
58 void initialize(
59 const RCP<std::basic_ostream<char_type,traits_type> > &oStream
60 ,const std::basic_string<char_type,traits_type> &tabIndentStr
61 ,const int startingTab
62 ,const bool showLinePrefix
63 ,const int maxLenLinePrefix
64 ,const bool showTabCount
65 ,const bool showProcRank
66 );
67
70
72 void setTabIndentStr(const std::basic_string<char_type,traits_type> &tabIndentStr);
73
75 const std::basic_string<char_type,traits_type>& getTabIndentStr() const;
76
78 void setShowLinePrefix(const bool showLinePrefix);
79
81 bool getShowLinePrefix() const;
82
85
87 int getMaxLenLinePrefix() const;
88
90 void setShowTabCount(const bool showTabCount);
91
93 bool getShowTabCount() const;
94
96 void setShowProcRank(const bool showProcRank);
97
99 bool getShowProcRank() const;
100
107 void setProcRankAndSize( const int procRank, const int numProcs );
108
113 int getProcRank() const;
114
119 int getNumProcs() const;
120
135 void setOutputToRootOnly( const int rootRank );
136
138 int getOutputToRootOnly() const;
139
148 void pushTab(const int tabs);
149
151 int getNumCurrTabs() const;
152
160 void popTab();
161
163 void pushLinePrefix(
164 const std::basic_string<char_type,traits_type> &linePrefix
165 );
166
168 void popLinePrefix();
169
171 const std::basic_string<char_type,traits_type>& getTopLinePrefix() const;
172
174 void pushDisableTabbing();
175
177 void popDisableTabbing();
178
179protected:
180
182
183
185 std::streamsize xsputn(const char_type* s, std::streamsize n);
186
189
190#ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
191
192 void imbue(const locale& l)
193 {
194 std::cerr << "\ncalled imbue()\n";
195 std::basic_streambuf<CharT,Traits>::imbue(l);
196 }
197
200 {
201 std::cerr << "\ncalled setbuf()\n";
202 return std::basic_streambuf<CharT,Traits>::setbuf(s,n);
203 }
204
206 seekoff(off_type a, ios_base::seekdir b,ios_base::openmode c)
207 {
208 std::cerr << "\ncalled seekoff()\n";
209 return std::basic_streambuf<CharT,Traits>::seekoff(a,b,c);
210 }
211
213 seekpos(pos_type a, ios_base::openmode b)
214 {
215 std::cerr << "\ncalled seekpos()\n";
216 return std::basic_streambuf<CharT,Traits>::seekpos(a,b);
217 }
218
219 int
220 sync()
221 {
222 std::cerr << "\ncalled sync()\n";
223 return std::basic_streambuf<CharT,Traits>::sync();
224 }
225
226 streamsize
227 showmanyc()
228 {
229 std::cerr << "\ncalled showmanyc()\n";
230 return std::basic_streambuf<CharT,Traits>::showmanyc();
231 }
232
233 streamsize
234 xsgetn(char_type* s, streamsize n)
235 {
236 std::cerr << "\ncalled xsgetn()\n";
237 return std::basic_streambuf<CharT,Traits>::xsgetn(s,n);
238 }
239
241 underflow()
242 {
243 std::cerr << "\ncalled underflow()\n";
244 return std::basic_streambuf<CharT,Traits>::underflow();
245 }
246
248 uflow()
249 {
250 std::cerr << "\ncalled uflow()\n";
251 return std::basic_streambuf<CharT,Traits>::uflow();
252 }
253
255 pbackfail(int_type c = traits_type::eof())
256 {
257 std::cerr << "\ncalled pbackfail()\n";
258 return std::basic_streambuf<CharT,Traits>::pbackfail(c);
259 }
260
261#endif // TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
262
264
265private:
266
267 // ////////////////////////
268 // Private types
269
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;
273
274 // ////////////////////////
275 // Private data members
276
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_;
282 bool showTabCount_;
283 bool showProcRank_;
284 int rootRank_;
285 int procRank_;
286 int numProcs_;
287 int rankPrintWidth_;
288
289 RCP<std::ostringstream> lineOut_;
290
291 int tabIndent_;
292 tabIndentStack_t tabIndentStack_;
293 linePrefixStack_t linePrefixStack_;
294 int enableTabbingStack_;
295
296 bool wroteNewline_;
297
298 // ////////////////////////
299 // Private member functions
300
301 std::ostream& out();
302
303 void writeChars( const char_type s[], std::streamsize n );
304
305 void writeFrontMatter();
306
307 // Not defined and not to be called
309 basic_FancyOStream_buf(const basic_FancyOStream_buf<CharT,Traits>&);
310 basic_FancyOStream_buf<CharT,Traits> operator=(
311 const basic_FancyOStream_buf<CharT,Traits>&
312 );
313
314};
315
316
348template <typename CharT, typename Traits = std::char_traits<CharT> >
349class basic_FancyOStream : public std::basic_ostream<CharT, Traits>
350{
351public:
352
354
355
361 typedef typename traits_type::int_type int_type;
363 typedef typename traits_type::pos_type pos_type;
365 typedef typename traits_type::off_type off_type;
371 typedef std::basic_ostream<char_type, traits_type> ostream_t;
372
374
376
377
407 explicit
409 const RCP< std::basic_ostream<char_type,traits_type> > &oStream
410 ,const std::basic_string<char_type,traits_type> &tabIndentStr = " "
411 ,const int startingTab = 0
412 ,const bool showLinePrefix = false
413 ,const int maxLenLinePrefix = 10
414 ,const bool showTabCount = false
415 ,const bool showProcRank = false
416 );
417
421 void initialize(
422 const RCP< std::basic_ostream<char_type,traits_type> > &oStream
423 ,const std::basic_string<char_type,traits_type> &tabIndentStr = " "
424 ,const int startingTab = 0
425 ,const bool showLinePrefix = false
426 ,const int maxLenLinePrefix = 10
427 ,const bool showTabCount = false
428 ,const bool showProcRank = false
429 );
430
433
436 const std::basic_string<char_type,traits_type> &tabIndentStr
437 );
438
440 const std::basic_string<char_type,traits_type>& getTabIndentStr() const;
441
457
460
463
466
469
477
493
495 int getOutputToRootOnly() const;
496
499
501
503
504
513 void pushTab(const int tabs = 1);
514
516 int getNumCurrTabs() const;
517
525 void popTab();
526
528 void pushLinePrefix(const std::basic_string<char_type,traits_type> &linePrefix);
529
531 void popLinePrefix();
532
534 const std::basic_string<char_type,traits_type>& getTopLinePrefix() const;
535
537 void pushDisableTabbing();
538
540 void popDisableTabbing();
541
543
544private:
545
546 streambuf_t streambuf_;
547
548 // Not defined and not to be called
552
553};
554
555
564fancyOStream(
565 const RCP< std::basic_ostream<char> >& oStream,
566 const std::basic_string<char>& tabIndentStr = " ",
567 const int startingTab = 0,
568 const bool showLinePrefix = false,
569 const int maxLenLinePrefix = 10,
570 const bool showTabCount = false,
571 const bool showProcRank = false
572 );
573
574
584getFancyOStream( const RCP<std::basic_ostream<char> > &out );
585
586
598template <typename CharT, typename Traits = std::char_traits<CharT> >
600{
601public:
602
604 static const int DISABLE_TABBING = -99999; // This magic number should be just fine!
607 const RCP<basic_FancyOStream<CharT,Traits> > &fancyOStream
608 ,const int tabs = 1
609 ,const std::basic_string<CharT,Traits> linePrefix = ""
610 )
611 :fancyOStream_(fancyOStream)
612 ,tabs_(tabs)
613 ,linePrefix_(linePrefix)
614 {
615 updateState();
616 }
619 const RCP<std::basic_ostream<CharT,Traits> > &oStream
620 ,const int tabs = 1
621 ,const std::basic_string<CharT,Traits> linePrefix = ""
622 )
623 :fancyOStream_(getFancyOStream(oStream))
624 ,tabs_(tabs)
625 ,linePrefix_(linePrefix)
626 {
627 updateState();
628 }
632 ,const int tabs = 1
633 ,const std::basic_string<CharT,Traits> linePrefix = ""
634 )
635 :fancyOStream_(rcp(&fancyOStream,false))
636 ,tabs_(tabs)
637 ,linePrefix_(linePrefix)
638 {
639 updateState();
640 }
643 std::basic_ostream<CharT,Traits> &oStream
644 ,const int tabs = 1
645 ,const std::basic_string<CharT,Traits> linePrefix = ""
646 )
647 :fancyOStream_(getFancyOStream(rcp(&oStream, false)))
648 ,tabs_(tabs)
649 ,linePrefix_(linePrefix)
650 {
651 updateState();
652 }
655 :fancyOStream_(osTab.fancyOStream_)
656 ,tabs_(osTab.tabs_)
657 {
658 updateState();
659 }
662 {
663 if(fancyOStream_.get()) {
664 if(tabs_==DISABLE_TABBING)
665 fancyOStream_->popDisableTabbing();
666 else
667 fancyOStream_->popTab();
668 if(linePrefix_.length()) fancyOStream_->popLinePrefix();
669 }
670 }
673 {
674 fancyOStream_ = osTab.fancyOStream_;
675 tabs_ = osTab.tabs_;
676 updateState();
677 return *this;
678 }
681 {
682 tabs_ += tabs;
683 if(fancyOStream_.get()) {
684 fancyOStream_->popTab();
685 fancyOStream_->pushTab(tabs_);
686 }
687 return *this;
688 }
691 {
692 return *fancyOStream_;
693 }
696 {
697 return fancyOStream_.get();
698 }
699
700private:
701
703 int tabs_;
704 std::basic_string<CharT,Traits> linePrefix_;
705
706 void updateState()
707 {
708 if(fancyOStream_.get()) {
709 if(tabs_==DISABLE_TABBING)
710 fancyOStream_->pushDisableTabbing();
711 else
712 fancyOStream_->pushTab(tabs_);
713 if(linePrefix_.length()) fancyOStream_->pushLinePrefix(linePrefix_);
714 }
715 }
716
717};
718
719
735template <typename CharT, typename Traits>
736RCP<basic_FancyOStream<CharT,Traits> >
737tab(
738 const RCP<basic_FancyOStream<CharT,Traits> > &out,
739 const int tabs = 1,
740 const std::basic_string<CharT,Traits> linePrefix = ""
741 );
742
743
759template <typename CharT, typename Traits>
760RCP<basic_FancyOStream<CharT,Traits> >
761tab(
762 const RCP<std::basic_ostream<CharT,Traits> > &out
763 ,const int tabs = 1
764 ,const std::basic_string<CharT,Traits> linePrefix = ""
765 );
766
767
768// ///////////////////////////////
769// Typedefs
770
771
776
777
782
783
787#define TEUCHOS_OSTAB ::Teuchos::OSTab __localThisTab = this->getOSTab()
788
792#define TEUCHOS_OSTAB_DIFF( DIFF ) ::Teuchos::OSTab DIFF ## __localThisTab = this->getOSTab()
793
794} // namespace Teuchos
795
796
797#endif // TEUCHOS_FANCY_O_STREAM_DECL_HPP
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.
Smart reference counting pointer class for automatic garbage collection.
RCP(ENull null_arg=null)
Initialize RCP<T> to NULL.
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 getNumProcs() const
Get the number of processes in the communicator.
void setShowProcRank(const bool showProcRank)
void setOutputToRootOnly(const int rootRank)
Set the stream to print only on the (MPI) process with the given rank.
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)
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.
int getProcRank() const
Get the rank of the calling (MPI) process.
void setMaxLenLinePrefix(const int maxLenLinePrefix)
void pushTab(const int tabs)
Push one or more tabs.
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)
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 ...
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.
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.
basic_FancyOStream & setMaxLenLinePrefix(const int maxLenLinePrefix)
std::basic_ostream< char_type, traits_type > ostream_t
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.
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)
basic_FancyOStream & setShowTabCount(const bool showTabCount)
basic_FancyOStream & setShowProcRank(const bool showProcRank)
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.
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)
basic_FancyOStream< char > FancyOStream
basic_OSTab< char > OSTab
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.