libMesh
ostream_proxy.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_OSTREAM_PROXY_H
21 #define LIBMESH_OSTREAM_PROXY_H
22 
23 
24 
25 // C++ includes
26 #include <iostream>
27 
28 namespace libMesh
29 {
30 
41 template <typename charT=char, typename traits=std::char_traits<charT>>
43 {
44 public:
49  typedef std::basic_ostream<charT,traits> streamT;
50 
55  typedef std::basic_streambuf<charT,traits> streambufT;
56 
62  BasicOStreamProxy (streamT & target) : _target(&target) {}
63 
71 
76  {
77  _target = &target;
78  return *this;
79  }
80 
85  {
86  _target = old._target;
87  return *this;
88  }
89 
93  ~BasicOStreamProxy () = default;
94 
95  //
96  // Functions that get passed to the proxied target:
97  //
98 
103  operator streamT & () { return *_target; }
104 
109  operator const streamT &() const { return *_target; }
110 
114  template<typename T>
116  {
117  (*_target) << in; return *this;
118  }
119 
124  {
125  (*_target) << in; return *this;
126  }
127 
131  BasicOStreamProxy & operator<< (std::basic_ios<charT,traits> & (*in)(std::basic_ios<charT,traits> &))
132  {
133  (*_target) << in; return *this;
134  }
135 
139  BasicOStreamProxy & operator<< (std::ios_base & (*in)(std::ios_base &))
140  {
141  (*_target) << in; return *this;
142  }
143 
147  streambufT * rdbuf () const { return _target->rdbuf(); }
148 
152  streambufT * rdbuf ( streambufT * sb ) { return _target->rdbuf(sb); }
153 
157  BasicOStreamProxy & flush () { _target->flush(); return *this; }
158 
162  std::ios_base::fmtflags flags ( ) const
163  { return _target->flags(); }
164 
168  std::ios_base::fmtflags flags ( std::ios_base::fmtflags fmtfl )
169  { return _target->flags(fmtfl); }
170 
174  std::ios_base::fmtflags setf ( std::ios_base::fmtflags fmtfl )
175  { return _target->setf(fmtfl); }
176 
180  std::ios_base::fmtflags setf ( std::ios_base::fmtflags fmtfl,
181  std::ios_base::fmtflags mask )
182  { return _target->setf(fmtfl, mask); }
183 
187  void unsetf ( std::ios_base::fmtflags mask )
188  { _target->unsetf(mask); }
189 
193  std::streamsize precision () const
194  { return _target->precision(); }
195 
199  std::streamsize precision ( std::streamsize prec )
200  { return _target->precision(prec); }
201 
202  //
203  // Functions that affect the Proxy class:
204  //
205 
211  void reset (streamT & target) { _target = &target; }
212 
217  streamT * get()
218  {
219  return _target;
220  }
221 
226  const streamT * get() const
227  {
228  return _target;
229  }
230 
234  std::streampos tellp() { return _target->tellp(); }
235 
236 private:
241 };
242 
244 
245 } // namespace libMesh
246 
247 #endif // LIBMESH_OSTREAM_PROXY_H
std::ios_base::fmtflags flags(std::ios_base::fmtflags fmtfl)
Set/get the associated format flags.
BasicOStreamProxy & operator<<(const T &in)
Redirect any output to the target.
std::basic_ostream< charT, traits > streamT
This class is going to be used to proxy for ostream, but other character and traits types are possibl...
Definition: ostream_proxy.h:49
std::streampos tellp()
Returns the position of the character in the current stream.
std::streamsize precision() const
Get the associated write precision.
The libMesh namespace provides an interface to certain functionality in the library.
void reset(streamT &target)
Reset the proxy to point to a different target.
This class is intended to be reseatable like a pointer-to-ostream for flexibility, but to look like a reference when used to produce less awkward user code.
Definition: ostream_proxy.h:42
streambufT * rdbuf(streambufT *sb)
Set the associated stream buffer.
std::basic_streambuf< charT, traits > streambufT
This class is going to be used to proxy for ostream, but other character and traits types are possibl...
Definition: ostream_proxy.h:55
BasicOStreamProxy OStreamProxy
void unsetf(std::ios_base::fmtflags mask)
Clear the associated flags.
streambufT * rdbuf() const
Get the associated stream buffer.
BasicOStreamProxy(streamT &target)
Default constructor.
Definition: ostream_proxy.h:62
BasicOStreamProxy & flush()
Flush the associated stream buffer.
std::ios_base::fmtflags flags() const
Get the associated format flags.
std::ios_base::fmtflags setf(std::ios_base::fmtflags fmtfl)
Set the associated flags.
streamT * _target
The pointer to the "real" ostream we send everything to.
BasicOStreamProxy(BasicOStreamProxy &old)
Shallow copy constructor.
Definition: ostream_proxy.h:70
std::streamsize precision(std::streamsize prec)
Set the associated write precision.
BasicOStreamProxy & operator=(streamT &target)
Reset the internal target to a new target output stream.
Definition: ostream_proxy.h:75
std::ios_base::fmtflags setf(std::ios_base::fmtflags fmtfl, std::ios_base::fmtflags mask)
Set the associated flags.
~BasicOStreamProxy()=default
Default destructor.